Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/paths-query #7

Draft
wants to merge 49 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
b01e532
Paths keyword
psvkaushik Jun 25, 2024
8cd1dc7
Update package.json
psvkaushik Jun 25, 2024
d774266
Version 1.0.8
psvkaushik Jul 2, 2024
19e36cf
Merge branch 'paths' of https://github.com/frink-okn/SPARQL.js into p…
psvkaushik Jul 2, 2024
c74025b
removed self dependency
psvkaushik Jul 2, 2024
3b1ab35
package files in gitignore
psvkaushik Jul 15, 2024
18f942a
Rename a.sparql to a*.sparql
balhoff Jul 15, 2024
f53813f
Rename a.sparql to a?.sparql
balhoff Jul 15, 2024
585bad3
Rename a.json to a*.json
balhoff Jul 15, 2024
d2fe152
Rename a.json to a?.json
balhoff Jul 15, 2024
81a8965
Made changes as per the review comments
psvkaushik Jul 22, 2024
bff6903
excluded main.js, gitignore and package.json reverted to original state
psvkaushik Jul 22, 2024
b952798
removing main.js
psvkaushik Jul 22, 2024
274ab3d
remove main.js
psvkaushik Jul 22, 2024
84c027f
reverted package-lock.json to original state
psvkaushik Jul 22, 2024
68201ea
Adding the compatibility of graph patterns for the grammar
psvkaushik Jul 22, 2024
27566a0
updated type of via
psvkaushik Jul 22, 2024
e56970b
added the updated type definitons
psvkaushik Jul 22, 2024
3689814
made changes as suggested
psvkaushik Jul 23, 2024
2bce648
made = optional and updated via in parser
psvkaushik Jul 23, 2024
326ac02
Changes made to typing of the parameters for better integration
psvkaushik Jul 31, 2024
841e1c9
made changes to the typings along with the jison
psvkaushik Jul 31, 2024
e6a4f5e
Merge pull request #4 from frink-okn/paths-gp
psvkaushik Aug 5, 2024
2625c52
changed so that value an be undefined
psvkaushik Aug 5, 2024
dc4deef
Added new test cases and updated the parser to replicate stardog comp…
psvkaushik Aug 11, 2024
9eb15db
updated the types
psvkaushik Aug 11, 2024
d2f2c7f
Merge pull request #6 from frink-okn/test
psvkaushik Aug 12, 2024
f52dc8d
Merge branch 'paths' into paths-gp
psvkaushik Aug 12, 2024
c2b2db4
Merge pull request #5 from frink-okn/paths-gp
psvkaushik Aug 12, 2024
8bd830d
Parser now fully replicate stardog pt2
psvkaushik Aug 12, 2024
2c816f5
updated types
psvkaushik Aug 12, 2024
61d66e2
type def for prefix changes
psvkaushik Aug 13, 2024
fbcf54c
updated the pathsquery
psvkaushik Aug 13, 2024
ac7a36e
start undefined error fixed
psvkaushik Aug 14, 2024
17cfcd0
Rename a.sparql to a*.sparql
balhoff Aug 22, 2024
997ca58
Rename a.sparql to a?.sparql
balhoff Aug 22, 2024
130cc9c
Rename a.json to a*.json
balhoff Aug 22, 2024
d91cdfc
Rename a.json to a?.json
balhoff Aug 22, 2024
0781a55
Update .gitignore
balhoff Aug 22, 2024
59850b1
Merge branch 'main' into feature/paths-query
balhoff Aug 22, 2024
dbdf715
Merge branch 'feature/paths-query' of github.com:frink-okn/SPARQL.js …
balhoff Aug 22, 2024
abf77c9
Update .gitignore
balhoff Aug 22, 2024
bdfd43c
Simplify PATHS parser.
balhoff Aug 28, 2024
f45fb9c
Merge branch 'feature/paths-query' of github.com:frink-okn/SPARQL.js …
balhoff Aug 28, 2024
cb1aa87
Remove file added to git.
balhoff Aug 28, 2024
e160785
Merge branch 'main' into feature/paths-query
balhoff Aug 28, 2024
8cf1fec
Change type for VIA.
balhoff Sep 16, 2024
dc3cfc4
Update JSON structure.
balhoff Sep 18, 2024
3cfb7f3
Tag local version.
balhoff Oct 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
334 changes: 334 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
// Type definitions for sparqljs 3.1
// Project: https://github.com/RubenVerborgh/SPARQL.js
// Definitions by: Alexey Morozov <https://github.com/AlexeyMz>
// Ruben Taelman <https://github.com/rubensworks>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1

import * as RdfJs from '@rdfjs/types';

//type RdfJs = import('').RdfJs;

export const Parser: {
new (options?: ParserOptions): SparqlParser;
};

export interface ParserOptions {
prefixes?: { [prefix: string]: string } | undefined;
baseIRI?: string | undefined;
factory?: RdfJs.DataFactory | undefined;
sparqlStar?: boolean | undefined;
}

export const Generator: {
new (options?: GeneratorOptions): SparqlGenerator;
};

export interface GeneratorOptions {
allPrefixes?: boolean | undefined;
prefixes?: { [prefix: string]: string } | undefined;
indent?: string | undefined;
newline?: string | undefined;
sparqlStar?: boolean | undefined;
}

export interface SparqlParser {
parse(query: string): SparqlQuery;
}

export interface SparqlGenerator {
stringify(query: SparqlQuery): string;
createGenerator(): any;
}

export class Wildcard {
readonly termType: 'Wildcard';
readonly value: '*';
equals(other: RdfJs.Term | null | undefined): boolean;
}

export type Term = VariableTerm | IriTerm | LiteralTerm | BlankTerm | QuadTerm;

export type VariableTerm = RdfJs.Variable;
export type IriTerm = RdfJs.NamedNode;
export type LiteralTerm = RdfJs.Literal;
export type BlankTerm = RdfJs.BlankNode;
export type QuadTerm = RdfJs.Quad;

export type SparqlQuery = Query | Update;

export type Query = SelectQuery | ConstructQuery | AskQuery | DescribeQuery | PathsQuery;

export interface BaseQuery {
type: 'query';
base?: string | undefined;
prefixes: { [prefix: string]: string; };
where?: Pattern[] | undefined;
values?: ValuePatternRow[] | undefined;
}

export interface SelectQuery extends BaseQuery {
queryType: 'SELECT';
variables: Variable[] | [Wildcard];
distinct?: boolean | undefined;
from?: {
default: IriTerm[];
named: IriTerm[];
} | undefined;
reduced?: boolean | undefined;
group?: Grouping[] | undefined;
having?: Expression[] | undefined;
order?: Ordering[] | undefined;
limit?: number | undefined;
offset?: number | undefined;
}

export interface Grouping {
expression: Expression;
}

export interface Ordering {
expression: Expression;
descending?: boolean | undefined;
}

export interface ConstructQuery extends BaseQuery {
queryType: 'CONSTRUCT';
template?: Triple[] | undefined;
}

export interface AskQuery extends BaseQuery {
queryType: 'ASK';
}

export interface DescribeQuery extends BaseQuery {
queryType: 'DESCRIBE';
variables: Variable[] | [Wildcard];
}

export interface PathsQuery extends BaseQuery {
queryType: "PATHS";
shortest: boolean;
cyclic: boolean;
start: PathEndpoint;
end: PathEndpoint;
via: PathVia;
maxLength?: number;
limit?: number;
offset?: number;
}

export interface PathEndpoint {
variable: VariableTerm;
input?: { type: 'NamedNode', value: IriTerm } | { type: 'Pattern', value: GroupPattern };
}

export type PathVia =
{ type: 'Variable', value: VariableTerm } |
{ type: 'Path', value: IriTerm | PropertyPath } |
{ type: 'Pattern', value: GroupPattern }

export interface Update {
type: 'update';
prefixes: { [prefix: string]: string; };
updates: UpdateOperation[];
}

export type UpdateOperation = InsertDeleteOperation | ManagementOperation;

export interface InsertDeleteOperation {
updateType: 'insert' | 'delete' | 'deletewhere' | 'insertdelete';
graph?: IriTerm | undefined;
insert?: Quads[] | undefined;
delete?: Quads[] | undefined;
where?: Pattern[] | undefined;
}

export type Quads = BgpPattern | GraphQuads;

export type ManagementOperation =
| CopyMoveAddOperation
| LoadOperation
| CreateOperation
| ClearDropOperation;

export interface CopyMoveAddOperation {
type: 'copy' | 'move' | 'add';
silent: boolean;
source: GraphOrDefault;
destination: GraphOrDefault;
}

export interface LoadOperation {
type: 'load';
silent: boolean;
source: IriTerm;
destination: IriTerm | false;
}

export interface CreateOperation {
type: 'create';
silent: boolean;
graph: IriTerm;
}

export interface ClearDropOperation {
type: 'clear' | 'drop';
silent: boolean;
graph: GraphReference;
}

export interface GraphOrDefault {
type: 'graph';
name?: IriTerm | undefined;
default?: boolean | undefined;
}

export interface GraphReference extends GraphOrDefault {
named?: boolean | undefined;
all?: boolean | undefined;
}

/**
* Examples: '?var', '*',
* SELECT (?a as ?b) ... ==> { expression: '?a', variable: '?b' }
*/
export type Variable = VariableExpression | VariableTerm;

export interface VariableExpression {
expression: Expression;
variable: VariableTerm;
}

export type Pattern =
| BgpPattern
| BlockPattern
| FilterPattern
| BindPattern
| ValuesPattern
| SelectQuery;
//| PathsQuery;

/**
* Basic Graph Pattern
*/
export interface BgpPattern {
type: 'bgp';
triples: Triple[];
}

export interface GraphQuads {
type: 'graph';
name: IriTerm;
triples: Triple[];
}

export type BlockPattern =
| OptionalPattern
| UnionPattern
| GroupPattern
| GraphPattern
| MinusPattern
| ServicePattern;

export interface OptionalPattern {
type: 'optional';
patterns: Pattern[];
}

export interface UnionPattern {
type: 'union';
patterns: Pattern[];
}

export interface GroupPattern {
type: 'group';
patterns: Pattern[];
}

export interface GraphPattern {
type: 'graph';
name: IriTerm;
patterns: Pattern[];
}

export interface MinusPattern {
type: 'minus';
patterns: Pattern[];
}

export interface ServicePattern {
type: 'service';
name: IriTerm;
silent: boolean;
patterns: Pattern[];
}

export interface FilterPattern {
type: 'filter';
expression: Expression;
}

export interface BindPattern {
type: 'bind';
expression: Expression;
variable: VariableTerm;
}

export interface ValuesPattern {
type: 'values';
values: ValuePatternRow[];
}

export interface ValuePatternRow {
[variable: string]: IriTerm | BlankTerm | LiteralTerm | undefined;
}

export interface Triple {
subject: IriTerm | BlankTerm | VariableTerm | QuadTerm;
predicate: IriTerm | VariableTerm | PropertyPath;
object: Term;
}

export interface PropertyPath {
type: 'path';
pathType: '|' | '/' | '^' | '+' | '*' | '!';
items: Array<IriTerm | PropertyPath>;
}

export type Expression =
| OperationExpression
| FunctionCallExpression
| AggregateExpression
| BgpPattern
| GraphPattern
| GroupPattern
| Tuple
| Term;

// allow Expression circularly reference itself
export interface Tuple extends Array<Expression> {}

export interface BaseExpression {
type: string;
distinct?: boolean | undefined;
}

export interface OperationExpression extends BaseExpression {
type: 'operation';
operator: string;
args: Expression[];
}

export interface FunctionCallExpression extends BaseExpression {
type: 'functionCall';
function: string;
args: Expression[];
}

export interface AggregateExpression extends BaseExpression {
type: 'aggregate';
expression: Expression;
aggregation: string;
separator?: string | undefined;
}
43 changes: 41 additions & 2 deletions lib/SparqlGenerator.js
Original file line number Diff line number Diff line change
@@ -23,12 +23,51 @@ function Generator(options) {
this._explicitDatatype = Boolean(options.explicitDatatype);
}

// Add a new function to handle PATHS queries
Generator.prototype.toPathsQuery = function (q) {
let query = 'PATHS ';

if (q.start) {
query += `START = ${this.toEntity(q.start)} `;
}
if (q.end) {
query += `END = ${this.toEntity(q.end)} `;
}
if (q.via) {
query += `VIA = ${this.toEntity(q.via)} `;
}
if (q.maxLength !== undefined) {
query += `MAXLENGTH = ${q.maxLength} `;
}
if (q.shortest) {
query += 'SHORTEST ';
}
if (q.cyclic) {
query += 'CYCLIC ';
}
if (q.limit !== undefined) {
query += `LIMIT ${q.limit} `;
}
if (q.offset !== undefined) {
query += `OFFSET ${q.offset} `;
}

return query.trim();
};

// Converts the parsed query object into a SPARQL query
Generator.prototype.toQuery = function (q) {
var query = '';

if (q.queryType)
query += q.queryType.toUpperCase() + ' ';
if (q.queryType) {
switch (q.queryType.toUpperCase()) {
case "PATHS" :
return this.toPathsQuery(q);
default:
query += q.queryType.toUpperCase() + ' ';
}
}

if (q.reduced)
query += 'REDUCED ';
if (q.distinct)
Loading