Skip to content

Commit

Permalink
fix: allow operators in InversePath value
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerfaulkner committed Oct 31, 2023
1 parent e82ed6c commit 241c244
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 43 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@comake/skl-js-engine",
"version": "0.15.4",
"version": "0.15.5",
"description": "Standard Knowledge Language Javascript Engine",
"keywords": [
"skl",
Expand Down
9 changes: 5 additions & 4 deletions src/storage/operator/InversePath.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { FindOperator } from '../FindOperator';

export interface InversePathValue {
export interface InversePathValue<T> {
subPath: string | FindOperator<any, 'sequencePath' | 'zeroOrMorePath' | 'oneOrMorePath'>;
value?: string;
value?: T | FindOperator<T, any>;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export function InversePath<
T extends InversePathValue
>(value: T): FindOperator<T, 'inversePath'> {
T,
TI extends InversePathValue<T>
>(value: TI): FindOperator<TI, 'inversePath'> {
return new FindOperator({
operator: 'inversePath',
value,
Expand Down
68 changes: 38 additions & 30 deletions src/storage/sparql/SparqlQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ import type { InverseRelationOperatorValue } from '../operator/InverseRelation';
import type { InverseRelationOrderValue } from '../operator/InverseRelationOrder';
import { VariableGenerator } from './VariableGenerator';

export interface WhereQueryData {
export interface NonGraphWhereQueryData {
values: ValuesPattern[];
triples: Triple[];
filters: OperationExpression[];
}

export interface WhereQueryData extends NonGraphWhereQueryData {
graphValues: ValuesPattern[];
graphTriples: Triple[];
graphFilters: OperationExpression[];
Expand Down Expand Up @@ -228,35 +231,35 @@ export class SparqlQueryBuilder {
value: IdFindOptionsWhereField,
isOnlyField: boolean,
): WhereQueryData {
let filter: OperationExpression | undefined;
let valuePattern: ValuesPattern | undefined;
let triple: Triple | undefined;
let filters: OperationExpression[] = [];
let values: ValuesPattern[] = [];
let triples: Triple[] = [];
if (FindOperator.isFindOperator(value)) {
({ filter, valuePattern, triple } =
({ filters, values, triples } =
this.resolveFindOperatorAsExpressionForId(term, value as FindOperator<string, any>));
} else {
valuePattern = {
values = [{
type: 'values',
values: [{
[`?${term.value}`]: DataFactory.namedNode(value as string),
}],
};
}];
}
if (isOnlyField) {
return {
values: [],
filters: [],
triples: [],
graphValues: valuePattern ? [ valuePattern ] : [],
graphFilters: filter ? [ filter ] : [],
graphTriples: triple ? [ triple ] : [],
graphValues: values,
graphFilters: filters,
graphTriples: triples,
};
}

return {
values: valuePattern ? [ valuePattern ] : [],
filters: filter ? [ filter ] : [],
triples: triple ? [ triple ] : [],
values,
filters,
triples,
graphValues: [],
graphFilters: [],
graphTriples: [],
Expand Down Expand Up @@ -572,38 +575,43 @@ export class SparqlQueryBuilder {
private resolveFindOperatorAsExpressionForId(
leftSide: Variable,
operator: FindOperator<any, any>,
): { filter?: OperationExpression; valuePattern?: ValuesPattern; triple?: Triple } {
): NonGraphWhereQueryData {
switch (operator.operator) {
case 'inversePath': {
const predicate = this.pathOperatorToPropertyPath(operator);
const triple = {
subject: leftSide,
predicate,
object: DataFactory.namedNode(operator.value.value),
};
return { triple };
return this.createWhereQueryDataFromKeyValue(leftSide, predicate, operator.value.value);
}
case 'in': {
const resolvedValue = this.resolveValueToExpression(operator.value) as NamedNode[];
return {
valuePattern: {
triples: [],
filters: [],
values: [{
type: 'values',
values: resolvedValue.map((value): ValuePatternRow => ({ [`?${leftSide.value}`]: value })),
},
}],
};
} case 'not':
return {
filter: this.buildNotOperationForId(
leftSide,
this.resolveValueToExpression(operator.value) as Expression | FindOperator<any, any>,
),
triples: [],
values: [],
filters: [
this.buildNotOperationForId(
leftSide,
this.resolveValueToExpression(operator.value) as Expression | FindOperator<any, any>,
),
],
};
case 'equal':
return {
filter: createSparqlEqualOperation(
leftSide,
this.resolveValueToExpression(operator.value) as Expression,
),
triples: [],
values: [],
filters: [
createSparqlEqualOperation(
leftSide,
this.resolveValueToExpression(operator.value) as Expression,
),
],
};
default:
throw new Error(`Unsupported operator "${operator.operator}"`);
Expand Down
11 changes: 6 additions & 5 deletions src/util/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,23 @@ export interface NodeShape extends BaseShape {
[SHACL.property]: OrArray<PropertyShape>;
}

export interface InverseShaclPath extends NodeShape {
export interface InverseShaclPath extends NodeObject {
[SHACL.inversePath]: PathShape;
}

export interface ZeroOrMoreShaclPath extends NodeShape {
export interface ZeroOrMoreShaclPath extends NodeObject {
[SHACL.zeroOrMorePath]: PathShape;
}

export interface OneOrMoreShaclPath extends NodeShape {
export interface OneOrMoreShaclPath extends NodeObject {
[SHACL.oneOrMorePath]: PathShape;
}

export interface ZeroOrOneShaclPath extends NodeShape {
export interface ZeroOrOneShaclPath extends NodeObject {
[SHACL.zeroOrOnePath]: PathShape;
}

export interface AlternativeShaclPath extends NodeShape {
export interface AlternativeShaclPath extends NodeObject {
[SHACL.alternativePath]: PathTypes[];
}

Expand All @@ -142,6 +142,7 @@ export type PathShape = OrArray<PathTypes>;
export type Verb = NodeObject & {
'@id': string;
'@type': typeof SKL.Verb;
[RDFS.label]?: ValueObject<string>;
[SKL.parametersContext]?: ValueObject<JSONObject>;
[SKL.parameters]?: NodeShape;
[SKL.returnValue]?: NodeObject;
Expand Down
2 changes: 1 addition & 1 deletion test/deploy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"main": "./dist/index.js",
"dependencies": {
"@comake/skl-js-engine": "file:./comake-skl-js-engine-0.15.4.tgz",
"@comake/skl-js-engine": "file:./comake-skl-js-engine-0.15.5.tgz",
"jsonld": "^6.0.0"
},
"devDependencies": {
Expand Down

0 comments on commit 241c244

Please sign in to comment.