Skip to content

Commit

Permalink
sparqlee: Merge pull request comunica#12 from wschella/type-tree
Browse files Browse the repository at this point in the history
Type tree
  • Loading branch information
wschella committed Nov 29, 2017
1 parent d5bf9d2 commit cfb75f8
Show file tree
Hide file tree
Showing 9 changed files with 365 additions and 189 deletions.
1 change: 0 additions & 1 deletion packages/sparqlee/benchmarks/Examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Mapping } from '../src/core/Mapping';
import { DataType as DT } from '../src/util/Consts';



const parser = new Parser({'xsd': 'http://www.w3.org/2001/XMLSchema#'})

export class Example {
Expand Down
21 changes: 21 additions & 0 deletions packages/sparqlee/benchmarks/Testing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as RDF from 'rdf-data-model';

import { Example } from './Examples';
import { DataType as DT } from '../src/util/Consts';
import { SyncEvaluator } from '../src/evaluator/EvalSync';

const example1 = (() => {
const str = '?a = ?b';
// const str = '10000 > ?age';
const mapping = new Map([
['a', RDF.literal('20', RDF.namedNode(DT.XSD_INTEGER))],
['b', RDF.literal('30', RDF.namedNode(DT.XSD_INTEGER))],
['c', RDF.literal('50', RDF.namedNode(DT.XSD_INTEGER))]
]);
return new Example(str, mapping);
})();

console.log(JSON.stringify(example1.expression, null, 4));

const evaluator = new SyncEvaluator(example1.expression);
console.log(evaluator.evaluate(example1.mapping));
1 change: 1 addition & 0 deletions packages/sparqlee/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"test-debug-win": "node --debug-brk=5858 ./node_modules/jest/bin/jest.js",
"prepare": "npm run build",
"bench": "node dist/benchmarks/Compare.js",
"testm": "node dist/benchmarks/Testing.js",
"build": "tsc"
},
"repository": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('the evaluation of simple datetime expressions', () => {
edge1 edge2 = true
`
test('<=', table)
test('<=', table);
});

describe('like >= receiving', () => {
Expand All @@ -173,6 +173,7 @@ describe('the evaluation of simple datetime expressions', () => {
edge1 edge2 = true
`
test('>=', table);
});
})
});
26 changes: 19 additions & 7 deletions packages/sparqlee/src/evaluator/EvalSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ export class SyncEvaluator implements ExpressionEvaluator {
case '||': return new Ops.Or(args);
case '!': return new Ops.Not(args);
case '=': return new Ops.Equal(args);
case '!=': throw new UnimplementedError();
case '<': throw new UnimplementedError();
case '!=': return new Ops.NotEqual(args);
case '<': return new Ops.LesserThan(args);
case '>': return new Ops.GreaterThan(args);
case '<=': throw new UnimplementedError();
case '>=': throw new UnimplementedError();
case '*': throw new UnimplementedError();
case '/': throw new UnimplementedError();
case '<=': return new Ops.LesserThan(args);
case '>=': throw new Ops.GreaterThenEqual(args);
case '*': throw new Ops.Multiplication(args);
case '/': throw new Ops.Division(args);
case '+': return new Ops.Addition(args);
case '-': throw new UnimplementedError();
case '-': throw new Ops.Subtraction(args);
}
};
case ET.FunctionCall: throw new UnimplementedError();
Expand Down Expand Up @@ -148,7 +148,19 @@ export class SyncEvaluator implements ExpressionEvaluator {
case DT.XSD_DECIMAL:
case DT.XSD_FLOAT:
case DT.XSD_DOUBLE:

case DT.XSD_NEGATIVE_INTEGER:
case DT.XSD_NON_NEGATIVE_INTEGER:
case DT.XSD_NON_POSITIVE_INTEGER:
case DT.XSD_POSITIVE_INTEGER:
case DT.XSD_LONG:
case DT.XSD_INT:
case DT.XSD_SHORT:
case DT.XSD_BYTE:
case DT.XSD_UNSIGNED_LONG:
case DT.XSD_UNSIGNED_INT:
case DT.XSD_UNSIGNED_SHORT:
case DT.XSD_UNSIGNED_BYTE:
case DT.XSD_INT: {
let val: number = JSON.parse(lit.value);
if (typeof val != 'number') {
Expand Down
196 changes: 196 additions & 0 deletions packages/sparqlee/src/evaluator/expressions/BinOpImplementation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import { InvalidOperationError, UnimplementedError } from '../../util/Errors';
import { BooleanLiteral, DateTimeLiteral, NumericLiteral, SimpleLiteral,
StringLiteral, Term } from './Terms';

export enum ImplType {
Term ='Term',
String = 'String',
Numeric = 'Numeric',
Boolean = 'Boolean',
DateTime = 'DateTime',
Simple = 'Simple',
}

export interface Impl {
rdfEqual(left: Term, right: Term): boolean,

rdfNotEqual(left: Term, right: Term): boolean,

lt(left: Term, right: Term): boolean,
gt(left: Term, right: Term): boolean,
lte(left: Term, right: Term): boolean,
gte(left: Term, right: Term): boolean,

multiply(left: Term, right: Term): number,
divide(left: Term, right: Term): number,
add(left: Term, right: Term): number,
subtract(left: Term, right: Term): number,
}

export class TermImpl implements Impl {

rdfEqual(left: Term, right: Term): boolean {
throw new UnimplementedError();
}

rdfNotEqual(left: Term, right: Term): boolean {
return !this.rdfEqual(left, right);
}

lt(left: Term, right: Term): boolean { throw new InvalidOperationError(); }
gt(left: Term, right: Term): boolean { throw new InvalidOperationError(); }
lte(left: Term, right: Term): boolean { throw new InvalidOperationError(); }
gte(left: Term, right: Term): boolean { throw new InvalidOperationError(); }
multiply(left: Term, right: Term): number { throw new InvalidOperationError(); }
divide(left: Term, right: Term): number { throw new InvalidOperationError(); }
add(left: Term, right: Term): number { throw new InvalidOperationError(); }
subtract(left: Term, right: Term): number { throw new InvalidOperationError(); }
}

export class NumericImpl extends TermImpl {

rdfEqual(left: NumericLiteral, right: NumericLiteral): boolean {
return left.value === right.value;
}

rdfNotEqual(left: NumericLiteral, right: NumericLiteral): boolean {
return left.value !== right.value;
}

lt(left: NumericLiteral, right: NumericLiteral): boolean {
return left.value < right.value;
}

gt(left: NumericLiteral, right: NumericLiteral): boolean {
return left.value > right.value;
}

lte(left: NumericLiteral, right: NumericLiteral): boolean {
return left.value <= right.value;
}

gte(left: NumericLiteral, right: NumericLiteral): boolean {
return left.value >= right.value;
}

multiply(left: NumericLiteral, right: NumericLiteral): number {
return left.value * right.value;
}

divide(left: NumericLiteral, right: NumericLiteral): number {
return left.value / right.value;
}

add(left: NumericLiteral, right: NumericLiteral): number {
return left.value + right.value;
}

subtract(left: NumericLiteral, right: NumericLiteral): number {
return left.value - right.value;
}
}

export class StringImpl extends TermImpl {
rdfEqual(left: StringLiteral, right: StringLiteral): boolean {
return left.value === right.value;
}

rdfNotEqual(left: StringLiteral, right: StringLiteral): boolean {
return left.value !== right.value;
}

lt(left: StringLiteral, right: StringLiteral): boolean {
return left.value < right.value;
}

gt(left: StringLiteral, right: StringLiteral): boolean {
return left.value > right.value;
}

lte(left: StringLiteral, right: StringLiteral): boolean {
return left.value <= right.value;
}

gte(left: StringLiteral, right: StringLiteral): boolean {
return left.value >= right.value;
}
}

export class DateTimeImpl extends TermImpl {

rdfEqual(left: DateTimeLiteral, right: DateTimeLiteral): boolean {
return left.value.getTime() === right.value.getTime();
}

rdfNotEqual(left: DateTimeLiteral, right: DateTimeLiteral): boolean {
return left.value.getTime() !== right.value.getTime();
}

lt(left: DateTimeLiteral, right: DateTimeLiteral): boolean {
return left.value < right.value;
}

gt(left: DateTimeLiteral, right: DateTimeLiteral): boolean {
return left.value > right.value;
}

lte(left: DateTimeLiteral, right: DateTimeLiteral): boolean {
return left.value.getTime() <= right.value.getTime();
}

gte(left: DateTimeLiteral, right: DateTimeLiteral): boolean {
return left.value.getTime() >= right.value.getTime();
}
}

export class BooleanImpl extends TermImpl {
rdfEqual(left: BooleanLiteral, right: BooleanLiteral): boolean {
return left.value === right.value;
}

rdfNotEqual(left: BooleanLiteral, right: BooleanLiteral): boolean {
return left.value !== right.value;
}

lt(left: BooleanLiteral, right: BooleanLiteral): boolean {
return left.value < right.value;
}

gt(left: BooleanLiteral, right: BooleanLiteral): boolean {
return left.value > right.value;
}

lte(left: BooleanLiteral, right: BooleanLiteral): boolean {
return left.value <= right.value;
}

gte(left: BooleanLiteral, right: BooleanLiteral): boolean {
return left.value >= right.value;
}
}

export class SimpleImpl extends TermImpl {
rdfEqual(left: SimpleLiteral, right: SimpleLiteral): boolean {
return left.value === right.value;
}

rdfNotEqual(left: SimpleLiteral, right: SimpleLiteral): boolean {
return left.value !== right.value;
}

lt(left: SimpleLiteral, right: SimpleLiteral): boolean {
return left.value < right.value;
}

gt(left: SimpleLiteral, right: SimpleLiteral): boolean {
return left.value > right.value;
}

lte(left: SimpleLiteral, right: SimpleLiteral): boolean {
return left.value <= right.value;
}

gte(left: SimpleLiteral, right: SimpleLiteral): boolean {
return left.value >= right.value;
}
}

0 comments on commit cfb75f8

Please sign in to comment.