Skip to content

Commit

Permalink
Create separate types for Wildcard and GroupConcat
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Oct 11, 2019
1 parent 92d1949 commit 8cc2ffb
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 22 deletions.
23 changes: 19 additions & 4 deletions lib/algebra.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

import * as rdfjs from "rdf-js";
import {Wildcard} from "./wildcard";
import {Term} from "rdf-js";

// TODO: add aggregates?
// TODO: can't find a way to use these values as string types in the interfaces
Expand Down Expand Up @@ -44,6 +46,7 @@ export const expressionTypes = Object.freeze({
NAMED: 'named',
OPERATOR: 'operator',
TERM: 'term',
WILDCARD: 'wildcard',
});

// ----------------------- ABSTRACTS -----------------------
Expand Down Expand Up @@ -72,18 +75,24 @@ export interface PropertyPathSymbol extends Operation
export interface Expression extends Operation
{
type: 'expression';
expressionType: 'aggregate'|'existence'|'named'|'operator'|'term';
expressionType: 'aggregate'|'existence'|'named'|'operator'|'term'|'wildcard';
}

export interface AggregateExpression extends Expression
{
expressionType: 'aggregate',
aggregator: string;
aggregator: 'avg' | 'count' | 'group_concat' | 'max' | 'min' | 'sample' | 'sum';
distinct: boolean;
separator?: string; // used by GROUP_CONCAT
expression: Expression;
}

export interface GroupConcatExpression extends AggregateExpression
{
aggregator: 'group_concat';
separator?: string;
}


export interface ExistenceExpression extends Expression
{
expressionType: 'existence';
Expand All @@ -108,7 +117,13 @@ export interface OperatorExpression extends Expression
export interface TermExpression extends Expression
{
expressionType: 'term';
term: rdfjs.Term;
term: Term;
}

export interface WildcardExpression extends Expression
{
expressionType: 'wildcard',
wildcard: Wildcard;
}


Expand Down
7 changes: 3 additions & 4 deletions lib/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,17 @@ export default class Factory
createValues (variables: RDF.Variable[], bindings: {[key: string]: RDF.Term}[]): A.Values { return { type: 'values', variables, bindings }; }
createZeroOrMorePath (path: A.PropertyPathSymbol): A.ZeroOrMorePath { return { type: 'ZeroOrMorePath', path }; }
createZeroOrOnePath (path: A.PropertyPathSymbol): A.ZeroOrOnePath { return { type: 'ZeroOrOnePath', path }; }

createAggregateExpression (aggregator: string, expression: A.Expression, distinct: boolean, separator?: string): A.AggregateExpression
{
if (separator)
return { type: 'expression', expressionType: 'aggregate', aggregator, expression, separator, distinct};
return { type: 'expression', expressionType: 'aggregate', aggregator, expression, distinct };
return { type: 'expression', expressionType: 'aggregate', aggregator: <any> aggregator, expression, separator, distinct};
return { type: 'expression', expressionType: 'aggregate', aggregator: <any> aggregator, expression, distinct };
}
createExistenceExpression (not: boolean, input: A.Operation): A.ExistenceExpression { return { type: 'expression', expressionType: 'existence', not, input }; }
createNamedExpression (name: RDF.NamedNode, args: A.Expression[]): A.NamedExpression { return { type: 'expression', expressionType: 'named', name, args }; }
createOperatorExpression (operator: string, args: A.Expression[]): A.OperatorExpression { return { type: 'expression', expressionType: 'operator', operator, args }; }
createTermExpression (term: RDF.Term): A.TermExpression { return { type: 'expression', expressionType: 'term', term }; }
createWildcardExpression (): A.TermExpression { return this.createTermExpression(new Wildcard()); }
createWildcardExpression (): A.WildcardExpression { return { type: 'expression', expressionType: 'wildcard', wildcard: new Wildcard() } };

createTerm (str: string): RDF.Term
{
Expand Down
7 changes: 7 additions & 0 deletions lib/sparql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as RDF from 'rdf-js'
import Factory from "./factory";
import Util from "./util";
import {termToString} from "rdf-string";
import {Wildcard} from "./wildcard";
const SparqlGenerator = require('sparqljs').Generator;
const Wildcard = require('sparqljs').Wildcard;
const types = Algebra.types;
Expand Down Expand Up @@ -84,6 +85,7 @@ function translateExpression(expr: Algebra.Expression): any
case eTypes.NAMED: return translateNamedExpression(<Algebra.NamedExpression>expr);
case eTypes.OPERATOR: return translateOperatorExpression(<Algebra.OperatorExpression>expr);
case eTypes.TERM: return translateTermExpression(<Algebra.TermExpression>expr);
case eTypes.WILDCARD: return translateWildcardExpression(<Algebra.WildcardExpression>expr);
}

throw new Error('Unknown Expression Operation type ' + expr.expressionType);
Expand Down Expand Up @@ -174,6 +176,11 @@ function translateTermExpression(expr: Algebra.TermExpression): RDF.Term
return expr.term;
}

function translateWildcardExpression(expr: Algebra.WildcardExpression): Wildcard
{
return expr.wildcard;
}


// ------------------------- OPERATIONS -------------------------
// these get translated in the project function
Expand Down
9 changes: 5 additions & 4 deletions lib/sparqlAlgebra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function inScopeVariables(thingy: any) : {[key: string]: boolean}
let all = inScopeVariables(thingy.where); // always executing this makes sure `variables` gets filled correctly
for (let v of thingy.variables)
{
if (v.termType === "Wildcard")
if (Util.isWildcard(v))
Object.assign(inScope, all);
else if (v.variable) // aggregates
Object.assign(inScope, inScopeVariables(v.variable));
Expand Down Expand Up @@ -182,9 +182,10 @@ function translateGroupGraphPattern(thingy: any) : Algebra.Operation

function translateExpression(exp: any) : Algebra.Expression
{
if (Util.isTerm(exp)) {
if (Util.isTerm(exp))
return factory.createTermExpression(exp);
}
if (Util.isWildcard(exp))
return factory.createWildcardExpression();
if (exp.aggregation)
return factory.createAggregateExpression(exp.aggregation, translateExpression(exp.expression), exp.distinct, exp.separator);
if (exp.function)
Expand Down Expand Up @@ -481,7 +482,7 @@ function translateAggregates(query: any, res: Algebra.Operation, variables: Set<

if (query.queryType === 'SELECT' || query.queryType === 'DESCRIBE')
{
if (query.variables.some((e: any) => e && e.termType === "Wildcard"))
if (query.variables.some((e: any) => e && Util.isWildcard(e)))
PV = variables;
else
{
Expand Down
11 changes: 9 additions & 2 deletions lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ export default class Util
case expressionTypes.TERM:
const term = <A.TermExpression> expr;
return factory.createTermExpression(term.term);
case expressionTypes.WILDCARD:
return factory.createWildcardExpression();
default: throw new Error('Unknown Expression type ' + expr.expressionType);
}
}
Expand All @@ -441,8 +443,13 @@ export default class Util
return dataFactory.variable(labelLoop);
}

public static isTerm(term: any) : boolean {
return term.termType !== undefined;
// separate terms from wildcard since we handle them differently
public static isTerm(term: any): boolean {
return term.termType !== undefined && term.termType !== 'Wildcard';
}

public static isWildcard(term: any): boolean {
return term.termType === 'Wildcard';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@
"aggregator": "count",
"expression": {
"type": "expression",
"expressionType": "term",
"term": {
"expressionType": "wildcard",
"wildcard": {
"termType": "Wildcard",
"value": "*"
}
Expand Down
4 changes: 2 additions & 2 deletions test/algebra-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const canon = Util.getCanonicalizerInstance();
// https://www.w3.org/2001/sw/DataAccess/tests/r2#syntax-basic-01
// https://www.w3.org/2009/sparql/implementations/
// https://www.w3.org/2009/sparql/docs/tests/
describe('SPARQL Parser', () =>
describe('Algebra output', () =>
{
// dawg/sparql
let subfolders = fs.readdirSync(rootSparql);
Expand All @@ -33,7 +33,7 @@ function testPath(root: string, fileName: string, testName: string, blankToVaria
testPath(root, path.join(fileName, sub), testName + '/' + sub, blankToVariable);
} else {
let name = root + '/' + testName.replace(/\.sparql$/, '');
it (name, () =>
it (name + (blankToVariable ? ' (no blanks)' : ''), () =>
{
let query = fs.readFileSync(sparqlName, 'utf8');
let algebra = Util.objectify(translate(query, { quads: name.endsWith('(quads)'), blankToVariable }));
Expand Down
4 changes: 2 additions & 2 deletions test/algebra/sparql-1.1/aggregates/group_concat-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@
"aggregator": "count",
"expression": {
"type": "expression",
"expressionType": "term",
"term": {
"expressionType": "wildcard",
"wildcard": {
"termType": "Wildcard",
"value": "*"
}
Expand Down
2 changes: 1 addition & 1 deletion test/recurse-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const factory = new Factory();
// https://www.w3.org/2001/sw/DataAccess/tests/r2#syntax-basic-01
// https://www.w3.org/2009/sparql/implementations/
// https://www.w3.org/2009/sparql/docs/tests/
describe('Recurse function', () =>
describe('Util funtions', () =>
{
let subfolders = fs.readdirSync(rootJson);

Expand Down
2 changes: 1 addition & 1 deletion test/sparql-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const rootJson = 'test/algebra';
// https://www.w3.org/2001/sw/DataAccess/tests/r2#syntax-basic-01
// https://www.w3.org/2009/sparql/implementations/
// https://www.w3.org/2009/sparql/docs/tests/
describe('SPARQL Parser', () =>
describe('SPARQL output', () =>
{
// dawg/sparql
let subfolders = fs.readdirSync(rootJson);
Expand Down

0 comments on commit 8cc2ffb

Please sign in to comment.