Skip to content

Commit

Permalink
[refactor] MST Instance and Snapshot Types
Browse files Browse the repository at this point in the history
  • Loading branch information
amivanoff committed Aug 24, 2024
1 parent 9b85667 commit 8874f8d
Show file tree
Hide file tree
Showing 19 changed files with 259 additions and 193 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"strends",
"strstarts",
"Subcat",
"timeseries",
"triplestore",
"typeahead",
"undelegate",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@agentlab/sparql-jsld-client",
"version": "5.1.0",
"version": "5.2.0",
"description": "SPARQL JSON Schema Linked Data Client",
"license": "GPL-3.0",
"author": "Aleksei Ivanov <[email protected]>",
Expand Down
14 changes: 7 additions & 7 deletions src/ObjectProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export function idComparator(a: JsObject, b: JsObject): number {
return 0;
}

export type JSONSchema7PropertyDefinition_LD = JSONSchema7Property_LD;
export interface JSONSchema7Property_LD extends JSONSchema7 {
export type JSONSchema7LDPropertyDefinition = JSONSchema7LDProperty;
export interface JSONSchema7LDProperty extends JSONSchema7 {
/**
* json-ld Property
* https://github.com/json-ld/json-ld.org/blob/master/schemas/jsonld-schema.json
Expand All @@ -49,12 +49,12 @@ export interface JSONSchema7Property_LD extends JSONSchema7 {
shapeModifiability?: string; // user or non -- system

properties?: {
[key: string]: JSONSchema7PropertyDefinition_LD;
[key: string]: JSONSchema7LDPropertyDefinition;
};
}

export type JSONSchema7Definition_LD = JSONSchema7_LD;
export interface JSONSchema7_LD extends Omit<JSONSchema7, 'allOf'>, JsObject {
export type JSONSchema7LDDefinition = JSONSchema7LD;
export interface JSONSchema7LD extends Omit<JSONSchema7, 'allOf'>, JsObject {
allOf?: { $ref: string }[] | undefined; // override from this: "allOf?: JSONSchema6Definition[] | undefined;"
type: JSONSchema7TypeName; // restrict from this: "type?: JSONSchema6TypeName | JSONSchema6TypeName[] | undefined;"

Expand All @@ -79,7 +79,7 @@ export interface JSONSchema7_LD extends Omit<JSONSchema7, 'allOf'>, JsObject {
//iconReference?: string;

properties: {
[key: string]: JSONSchema7PropertyDefinition_LD;
[key: string]: JSONSchema7LDPropertyDefinition;
};
}

Expand Down Expand Up @@ -140,7 +140,7 @@ export function copyUniqueArrayElements(arrTo: any[], arrFrom: any[]): void {
});
}

export function getPropKeysByFormat(schema: JSONSchema7_LD, format: string): string[] {
export function getPropKeysByFormat(schema: JSONSchema7LD, format: string): string[] {
const props: string[] = [];
for (const key in schema.properties) {
if (schema.properties[key].format === format) {
Expand Down
14 changes: 7 additions & 7 deletions src/ObjectProviderImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import {
JSONSchema7_LD,
JSONSchema7LD,
JsObject,
JSONSchema7PropertyDefinition_LD,
JSONSchema7LDPropertyDefinition,
copyObjectProps,
copyUniqueArrayElements,
} from './ObjectProvider';

export const schemaNonPrimitivePropsKeys = ['@context', 'properties', 'required'];

function combineProperties(oldObj: any, newObj: any, schema: JSONSchema7_LD): any {
function combineProperties(oldObj: any, newObj: any, schema: JSONSchema7LD): any {
const newData: any = {};
Object.keys(oldObj).forEach((key) => {
if (schema.properties && oldObj[key] !== newObj[key]) {
Expand All @@ -31,7 +31,7 @@ function combineProperties(oldObj: any, newObj: any, schema: JSONSchema7_LD): an
return newData;
}

export function createObjectWithoutRepetitions(objects: any[], schema: JSONSchema7_LD): any[] {
export function createObjectWithoutRepetitions(objects: any[], schema: JSONSchema7LD): any[] {
const newData = new Map();
const usedUri: any[] = [];
objects.forEach((object) => {
Expand Down Expand Up @@ -231,8 +231,8 @@ function propertyShapeToUiSchema(

export function propertyShapesToSchemaProperties(
shapeProps: any[] | undefined,
): [{ [key: string]: JSONSchema7PropertyDefinition_LD }, JsObject, string[], JsObject] {
const schemaProps: { [key: string]: JSONSchema7PropertyDefinition_LD } = {};
): [{ [key: string]: JSONSchema7LDPropertyDefinition }, JsObject, string[], JsObject] {
const schemaProps: { [key: string]: JSONSchema7LDPropertyDefinition } = {};
const schemaContexts: JsObject = {};
const schemaReqs: string[] = [];
const uiSchema: JsObject = {};
Expand Down Expand Up @@ -261,7 +261,7 @@ export function propertyShapesToSchemaProperties(
* @param schema
* @param parentSchema
*/
export function addToSchemaParentSchema(schema: JSONSchema7_LD, parentSchema: JSONSchema7_LD): JSONSchema7_LD {
export function addToSchemaParentSchema(schema: JSONSchema7LD, parentSchema: JSONSchema7LD): JSONSchema7LD {
const parentCtx = parentSchema['@context'];
if (parentCtx && typeof parentCtx !== 'string' && !Array.isArray(parentCtx)) {
if (!schema['@context']) schema['@context'] = {};
Expand Down
23 changes: 14 additions & 9 deletions src/SparqlGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { BgpPattern, Generator, OptionalPattern } from 'sparqljs';

import { Bindings } from './SparqlClient';
import {
JSONSchema7_LD,
JSONSchema7Property_LD,
JSONSchema7LD,
JSONSchema7LDProperty,
JsObject,
copyObjectPropsWithRenameOrFilter,
JsStrObj,
Expand Down Expand Up @@ -69,11 +69,11 @@ export function propsToSparqlVars(entConstr: Pick<EntConstrInternal, 'props2vars
return variables;
}

export function getSchemaPropUri(schema: JSONSchema7_LD, propertyKey: string): string | string[] | undefined {
export function getSchemaPropUri(schema: JSONSchema7LD, propertyKey: string): string | string[] | undefined {
const properties = schema.properties;
const context = schema['@context'];
if (properties && context && typeof context !== 'string') {
const prop: JSONSchema7Property_LD | undefined = properties[propertyKey];
const prop: JSONSchema7LDProperty | undefined = properties[propertyKey];
if (prop !== undefined && propertyKey !== '@id') {
const propContext = (context as any)[propertyKey];
if (propContext) {
Expand All @@ -91,11 +91,11 @@ export function getSchemaPropUri(schema: JSONSchema7_LD, propertyKey: string): s
}

export function getSchemaPropType(
properties: { [key: string]: JSONSchema7Property_LD },
properties: { [key: string]: JSONSchema7LDProperty },
context: JsObject,
propertyKey: string,
): string | undefined {
const prop: JSONSchema7Property_LD | undefined = properties[propertyKey];
const prop: JSONSchema7LDProperty | undefined = properties[propertyKey];
const propContext = context[propertyKey];
if (prop && prop.type && propContext && propContext['@type']) {
if (prop.type === 'object') {
Expand Down Expand Up @@ -188,7 +188,7 @@ export interface IEntConstrJsOpt {
'@type'?: string;
'@parent'?: string;
// external properties from Sparql EntConstr could be changed by client-code only (GUI code, etc.), immutable within SPARQL generation
schema: JSONSchema7_LD;
schema: string | undefined | JSONSchema7LD;
conditions?: JsObject;
variables?: JsObject;
data?: JsObject;
Expand Down Expand Up @@ -225,7 +225,7 @@ export interface IEntConstrJs extends EntConstrData {
}
export interface EntConstrData {
// external properties from Sparql EntConstr could be changed by client-code only (GUI code, etc.), immutable within SPARQL generation
schema: JSONSchema7_LD;
schema: JSONSchema7LD;
conditions: JsObject;
variables?: JsObject;
data: JsObject;
Expand Down Expand Up @@ -394,7 +394,12 @@ export function genEntConstrSparqlSubject(
export function genUniqueVarName2(varPref: string, no: number, entConstrs: IEntConstrJsOpt[]): string {
let varName = varPref + no;
entConstrs.forEach((constr) => {
while (constr.schema.properties && constr.schema.properties[varName]) {
while (
constr?.schema &&
typeof constr.schema === 'object' &&
constr.schema.properties &&
constr.schema.properties[varName]
) {
varName += entConstrs.length;
}
});
Expand Down
6 changes: 3 additions & 3 deletions src/SparqlGenSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
copyObjectPropsWithRenameOrFilter,
copyUniqueObjectProps,
JsStrObjObj,
JSONSchema7_LD,
JSONSchema7LD,
JsStrObj,
} from './ObjectProvider';
import {
Expand Down Expand Up @@ -79,7 +79,7 @@ export async function selectObjectsArrayProperties(
for (const key of Object.keys(entConstr.schemaPropsWithArrays)) {
for (const object of objects) {
const prop = entConstr.schemaPropsWithArrays[key];
const schemaWithArrayProperty: JSONSchema7_LD = {
const schemaWithArrayProperty: JSONSchema7LD = {
...schema,
'@id': '_' + uuid62.v4(),
'@context': {
Expand All @@ -95,7 +95,7 @@ export async function selectObjectsArrayProperties(
if (prop && prop.type && propType) {
//let schemaUri: string | undefined;
let constrWithSchema2: EntConstrInternal | undefined;
let schema2: JSONSchema7_LD;
let schema2: JSONSchema7LD;
if ((prop.type === 'array' && prop.items) || prop.type === 'object') {
//schemaUri = repository.schemas.getByClassId(propType);
constrWithSchema2 = entConstrs.find((c) => c.schema.targetClass === propType);
Expand Down
16 changes: 8 additions & 8 deletions src/models/MstColl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import {

import { JsObject } from '../ObjectProvider';
import {
MstCollConstr as MstCollConstr,
MstCollConstr,
constructObjectsSnapshot,
deleteObjectSnapshot,
ICollConstrSnapshotOut,
TMstCollConstrSnapshotOut,
MstMapOfJsObject,
} from './MstCollConstr';

Expand Down Expand Up @@ -245,9 +245,9 @@ export const MstColl = types
//console.log('loadColl START');
self.isLoading = true;
if (self.collConstr) {
const collConstr = getSnapshot<ICollConstrSnapshotOut>(self.collConstr);
const collConstr = getSnapshot<TMstCollConstrSnapshotOut>(self.collConstr);
let parent: any | undefined = self.collConstr['@parent'];
if (parent) parent = getSnapshot<ICollConstrSnapshotOut>(parent);
if (parent) parent = getSnapshot<TMstCollConstrSnapshotOut>(parent);
//console.log('loadColl query', { collConstr, parent });
try {
const objects: JsObject[] | null = yield constructObjectsSnapshot(
Expand Down Expand Up @@ -283,12 +283,12 @@ export const MstColl = types
self.isLoading = true;
if (self.collConstr) {
const collConstr = {
...getSnapshot<ICollConstrSnapshotOut>(self.collConstr),
...getSnapshot<TMstCollConstrSnapshotOut>(self.collConstr),
limit: self.pageSize,
offset: self.dataIntrnl.length,
};
let parent: any = self.collConstr['@parent'];
if (parent) parent = getSnapshot<ICollConstrSnapshotOut>(parent);
if (parent) parent = getSnapshot<TMstCollConstrSnapshotOut>(parent);
//console.log('loadMore query', { collConstr, parent });
try {
const objects: JsObject[] | null = yield constructObjectsSnapshot(
Expand Down Expand Up @@ -355,15 +355,15 @@ export const MstColl = types
elem = elem['@id'];
if (!elem) return null;
}
let collConstr: any = getSnapshot<ICollConstrSnapshotOut>(self.collConstr);
let collConstr: any = getSnapshot<TMstCollConstrSnapshotOut>(self.collConstr);
// filter-out query modifiers like orderBy, limit...
collConstr = {
'@id': collConstr['@id'],
'@type': collConstr['@type'],
entConstrs: collConstr.entConstrs,
};
let parent: any = self.collConstr['@parent'];
if (parent) parent = getSnapshot<ICollConstrSnapshotOut>(parent);
if (parent) parent = getSnapshot<TMstCollConstrSnapshotOut>(parent);
yield deleteObjectSnapshot(rep.schemas, rep.ns.currentJs, client, collConstr, parent, { '@_id': elem });
//@ts-ignore
return self.delElemInternal(elem);
Expand Down
Loading

0 comments on commit 8874f8d

Please sign in to comment.