Skip to content

Commit

Permalink
Paths keyword added to sparql algebra
Browse files Browse the repository at this point in the history
  • Loading branch information
psvkaushik committed Jul 2, 2024
1 parent 7f0d223 commit 508d618
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 11 deletions.
17 changes: 14 additions & 3 deletions lib/algebra.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as rdfjs from '@rdfjs/types';
import { Wildcard } from 'sparqljs';
import { IriTerm, Wildcard } from 'sparqljs-nrt';
import { Term } from '@rdfjs/types';

export enum types {
Expand Down Expand Up @@ -35,7 +35,7 @@ export enum types {
VALUES= 'values',
ZERO_OR_MORE_PATH= 'ZeroOrMorePath',
ZERO_OR_ONE_PATH= 'ZeroOrOnePath',

PATHS= 'paths',
COMPOSITE_UPDATE= 'compositeupdate',
DELETE_INSERT= 'deleteinsert',
LOAD= 'load',
Expand Down Expand Up @@ -63,7 +63,7 @@ type valueOf<T> = T[keyof T];
export type Operation =
Ask | Expression | Bgp | Construct | Describe | Distinct | Extend | From | Filter | Graph | Group | Join | LeftJoin |
Minus | Nop | OrderBy | Path | Pattern | Project | PropertyPathSymbol | Reduced | Service | Slice | Union | Values |
Update;
Update | Paths;

export type Expression = AggregateExpression | GroupConcatExpression | ExistenceExpression | NamedExpression |
OperatorExpression | TermExpression | WildcardExpression | BoundAggregate;
Expand All @@ -83,6 +83,17 @@ export interface BaseOperation
metadata?: Record<string, unknown>;
type: types;
}
// The interface for paths is added with types corresponding to the definitions in sparql-nrt
export interface Paths extends BaseOperation
{
type : types.PATHS;
shortest?: boolean;
cyclic?: boolean;
start?: rdfjs.Variable | IriTerm;
via?: rdfjs.Variable | IriTerm;
end?: rdfjs.Variable | IriTerm;
maxlength?: number
}

export interface Single extends BaseOperation
{
Expand Down
13 changes: 12 additions & 1 deletion lib/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as A from './algebra';
import * as RDF from '@rdfjs/types';
import { DataFactory } from 'rdf-data-factory';
import { stringToTerm } from "rdf-string";
import { Wildcard } from 'sparqljs';
import { Wildcard } from 'sparqljs-nrt';

export default class Factory
{
Expand All @@ -22,6 +22,17 @@ export default class Factory
result.variable = variable;
return result;
}
createPaths(shortest?: boolean, cyclic?: boolean, start?: RDF.Variable | RDF.NamedNode, via?: RDF.Variable | RDF.NamedNode, end?: RDF.Variable | RDF.NamedNode, maxlength?: number): A.Paths {
return {
type: A.types.PATHS,
shortest,
cyclic,
start,
via,
end,
maxlength
};
}
createBgp (patterns: A.Pattern[]): A.Bgp { return { type: A.types.BGP, patterns }; }
createConstruct (input: A.Operation, template: A.Pattern[]): A.Construct { return { type: A.types.CONSTRUCT, input, template }; }
createDescribe (input: A.Operation, terms: (RDF.Variable | RDF.NamedNode)[]): A.Describe { return { type: A.types.DESCRIBE, input, terms }; }
Expand Down
20 changes: 19 additions & 1 deletion lib/sparql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
PropertyPath,
Query,
SelectQuery,
PathsQuery,
ServicePattern,
Triple,
UnionPattern,
Expand All @@ -31,7 +32,7 @@ import {
ValuesPattern,
Variable,
Wildcard
} from 'sparqljs';
} from 'sparqljs-nrt';
import * as Algebra from './algebra';
import Factory from './factory';
import Util from './util';
Expand Down Expand Up @@ -108,11 +109,27 @@ function translateOperation(op: Algebra.Operation): any
case types.ADD: return translateAdd(op);
case types.MOVE: return translateMove(op);
case types.COPY: return translateCopy(op);
case types.PATHS: return translatePaths(op);
}

throw new Error(`Unknown Operation type ${op.type}`);
}

function translatePaths(op: Algebra.Paths): PathsQuery {
return {
type: 'query',
queryType: 'PATHS',
prefixes: {},
shortest: op.shortest || false,
cyclic: op.cyclic || false,
start: op.start ? op.start as Variable : undefined,
via: op.via ? op.via as Variable : undefined,
end: op.end ? op.end as Variable : undefined,
maxlength: op.maxlength || undefined,
};
}


function translateExpression(expr: Algebra.Expression): any
{
switch(expr.expressionType)
Expand Down Expand Up @@ -206,6 +223,7 @@ function translateOperatorExpression(expr: Algebra.OperatorExpression): Ordering

if (result.operator === 'in' || result.operator === 'notin')
result.args = [result.args[0]].concat([result.args.slice(1)]);
//result.args = [result.args[0], result.args.slice(1) as any];

return result;
}
Expand Down
23 changes: 21 additions & 2 deletions lib/sparqlAlgebra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,28 @@ import {
GroupPattern,
InsertDeleteOperation,
IriTerm,
// VarorIriOrListOfIris,
// ListOfIris,
LoadOperation,
Ordering,
Pattern,
PropertyPath,
Query,
SelectQuery,
PathsQuery,
SparqlQuery,
Triple,
Update,
UpdateOperation, ValuesPattern,
Variable,
VariableExpression,
Wildcard
} from 'sparqljs';
} from 'sparqljs-nrt';
import * as Algebra from './algebra';
import Factory from './factory';
import Util from './util';

const Parser = require('sparqljs').Parser;
const Parser = require('sparqljs-nrt').Parser;
const types = Algebra.types;

let variables = new Set<string>();
Expand Down Expand Up @@ -98,10 +101,15 @@ function translateQuery(sparql: SparqlQuery, quads?: boolean, blankToVariable?:
findAllVariables(sparql);

if (sparql.type === 'query') {
if (sparql.queryType == 'PATHS'){
res = translatePathsQuery(sparql as PathsQuery);
}
else{
// group and where are identical, having only 1 makes parsing easier, can be undefined in DESCRIBE
const group: GroupPattern = { type: 'group', patterns: sparql.where || [] };
res = translateGraphPattern(group);
res = translateAggregates(sparql, res);
}
}
else if(sparql.type === 'update') {
res = translateUpdate(sparql);
Expand Down Expand Up @@ -249,6 +257,17 @@ function inScopeVariables(thingy: SparqlQuery | Pattern | PropertyPath | RDF.Ter

return inScope;
}
function translatePathsQuery(sparql: PathsQuery): Algebra.Operation {
return factory.createPaths(
sparql.shortest,
sparql.cyclic,
sparql.start ? sparql.start as RDF.Variable | RDF.NamedNode : undefined,
sparql.via ? sparql.via as RDF.Variable | RDF.NamedNode : undefined,
sparql.end ? sparql.end as RDF.Variable | RDF.NamedNode : undefined,
sparql.maxlength
);
}


function translateGraphPattern(thingy: Pattern) : Algebra.Operation
{
Expand Down
16 changes: 14 additions & 2 deletions lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Wildcard } from 'sparqljs';
import { Wildcard } from 'sparqljs-nrt';
import * as A from "./algebra";
import { Expression, Operation, expressionTypes, types, TypedOperation, TypedExpression } from './algebra';
import Factory from "./factory";
Expand Down Expand Up @@ -180,7 +180,7 @@ export default class Util
return;

let recurseOp = (op: A.Operation) => Util.recurseOperation(op, callbacks);

switch (result.type)
{
case types.ALT:
Expand Down Expand Up @@ -351,6 +351,17 @@ export default class Util

return result;
}
if (result.type === types.PATHS) {
result = factory.createPaths(
result.shortest,
result.cyclic,
result.start ? result.start as RDF.Variable | RDF.NamedNode : undefined,
result.via ? result.via as RDF.Variable | RDF.NamedNode : undefined,
result.end ? result.end as RDF.Variable | RDF.NamedNode : undefined,
result.maxlength
);
}
else{

let mapOp = (op: A.Operation) => Util.mapOperation(op, callbacks, factory);

Expand Down Expand Up @@ -493,6 +504,7 @@ export default class Util
break;
default: throw new Error(`Unknown Operation type ${(result as any).type}`);
}
}

// Inherit metadata
if (toCopyMetadata) {
Expand Down
62 changes: 61 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"rdf-isomorphic": "^1.3.0",
"rdf-string": "^1.6.0",
"rdf-terms": "^1.10.0",
"sparqljs": "^3.7.1"
"sparqlalgebrajs": "^4.3.7",
"sparqljs-nrt": "^1.0.8"
},
"devDependencies": {
"@tsconfig/node12": "^12.0.0",
Expand Down

0 comments on commit 508d618

Please sign in to comment.