Skip to content

Commit

Permalink
Compare literals in SPARQL by value, not stricly, Closes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Sep 4, 2019
1 parent 6ce6c22 commit 4e0bc1d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 8 deletions.
9 changes: 7 additions & 2 deletions lib/testcase/sparql/QueryResultBindings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as stringify from "json-stable-stringify";
import * as RDF from "rdf-js";
import {fromRdf} from "rdf-literal";
import {termToString} from "rdf-string";
import {IQueryResult, IQueryResultBindings} from "./IQueryEngine";

Expand All @@ -18,12 +19,16 @@ export class QueryResultBindings implements IQueryResultBindings {
this.checkOrder = checkOrder;
}

public static serializeTerm(term: RDF.Term): any {
return term.termType === 'Literal' ? fromRdf(term) : termToString(term);
}

public static hashBindings(bindings: {[variable: string]: RDF.Term}[], checkOrder: boolean): string {
const hash = [];
for (const b of bindings) {
const bHash: {[id: string]: string} = {};
for (const variable in b) {
bHash[variable] = termToString(b[variable]);
bHash[variable] = QueryResultBindings.serializeTerm(b[variable]);
}
hash.push(stringify(bHash));
}
Expand All @@ -35,7 +40,7 @@ export class QueryResultBindings implements IQueryResultBindings {
for (const b of bindings) {
const bHash: {[id: string]: string} = {};
for (const variable in b) {
bHash[variable] = termToString(b[variable]);
bHash[variable] = QueryResultBindings.serializeTerm(b[variable]);
}
const bString: string = JSON.stringify(bHash);
if (!hash[bString]) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"n3": "^1.1.1",
"node-web-streams": "^0.2.2",
"rdf-isomorphic": "^1.1.0",
"rdf-literal": "^1.0.0",
"rdf-object": "^1.1.0",
"rdf-quad": "^1.3.0",
"rdf-string": "^1.3.1",
Expand Down
58 changes: 57 additions & 1 deletion test/testcase/sparql/QueryResultBindings-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {namedNode} from "@rdfjs/data-model";
import {literal, namedNode} from "@rdfjs/data-model";
import {QueryResultBindings} from "../../../lib/testcase/sparql/QueryResultBindings";

describe('QueryResultBindings', () => {
Expand All @@ -12,6 +12,8 @@ describe('QueryResultBindings', () => {
let bindingsBA1Empty;
let bindingsCD1;
let bindingsCD1Empty;
let bindingDecimalShort;
let bindingDecimalLong;

let bindingsAB1Order;
let bindingsAB1ooOrder;
Expand All @@ -20,6 +22,8 @@ describe('QueryResultBindings', () => {
let bindingsBA1EmptyOrder;
let bindingsCD1Order;
let bindingsCD1EmptyOrder;
let bindingDecimalShortOrder;
let bindingDecimalLongOrder;

beforeEach(() => {
bindingsAB1 = new QueryResultBindings([ '?a', '?b' ], [
Expand Down Expand Up @@ -117,6 +121,26 @@ describe('QueryResultBindings', () => {
},
], false);
bindingsCD1Empty = new QueryResultBindings([ '?c', '?d' ], [], false);
bindingDecimalShort = new QueryResultBindings([ '?c', '?d' ], [
{
'?c': literal('2', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
'?d': literal('3', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
},
{
'?c': literal('4.4', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
'?d': literal('5.5', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
},
], false);
bindingDecimalLong = new QueryResultBindings([ '?c', '?d' ], [
{
'?c': literal('2.000', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
'?d': literal('3.00', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
},
{
'?c': literal('4.40000', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
'?d': literal('5.50000000', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
},
], false);

bindingsAB1Order = new QueryResultBindings([ '?a', '?b' ], [
{
Expand Down Expand Up @@ -161,6 +185,26 @@ describe('QueryResultBindings', () => {
},
], true);
bindingsCD1EmptyOrder = new QueryResultBindings([ '?c', '?d' ], [], true);
bindingDecimalShortOrder = new QueryResultBindings([ '?c', '?d' ], [
{
'?c': literal('2', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
'?d': literal('3', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
},
{
'?c': literal('4.4', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
'?d': literal('5.5', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
},
], true);
bindingDecimalLongOrder = new QueryResultBindings([ '?c', '?d' ], [
{
'?c': literal('2.000', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
'?d': literal('3.00', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
},
{
'?c': literal('4.40000', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
'?d': literal('5.50000000', namedNode('http://www.w3.org/2001/XMLSchema#decimal')),
},
], true);
});

describe('when instantiated', () => {
Expand Down Expand Up @@ -224,6 +268,10 @@ describe('QueryResultBindings', () => {
it('should be false for for out-of-order results', () => {
return expect(bindingsAB1Order.equals(bindingsAB1ooOrder)).toBeFalsy();
});

it('should be true for literals with different lexical forms', () => {
return expect(bindingDecimalShortOrder.equals(bindingDecimalLongOrder)).toBeTruthy();
});
});

describe('with non-strict order', () => {
Expand Down Expand Up @@ -254,6 +302,10 @@ describe('QueryResultBindings', () => {
it('should be true for for out-of-order results', () => {
return expect(bindingsAB1.equals(bindingsAB1oo)).toBeTruthy();
});

it('should be true for literals with different lexical forms', () => {
return expect(bindingDecimalShort.equals(bindingDecimalLong)).toBeTruthy();
});
});
});

Expand Down Expand Up @@ -302,6 +354,10 @@ describe('QueryResultBindings', () => {
it('should be false for more results', () => {
return expect(bindingsAB1Reduced.equals(bindingsAB1Duplicates, true)).toBeFalsy();
});

it('should be true for literals with different lexical forms', () => {
return expect(bindingDecimalShort.equals(bindingDecimalLong, true)).toBeTruthy();
});
});
});
});
Expand Down
17 changes: 12 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2735,10 +2735,10 @@ ms@^2.1.1:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

n3@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/n3/-/n3-1.1.1.tgz#9201cdec6d73f1089d6ee60386f25095c8a1a4ac"
integrity sha512-GEJXn+wc0f4l2noP1N/rMUH9Gei1DQ8IDN03eBsH+uQKkNQUOLgL7ZJVaDjY+pP3LmbLxL1LpUg/AvZ7Kc7KVw==
n3@^1.1.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/n3/-/n3-1.2.0.tgz#4d04e953007438f8e39635e13562dcfa66fb1e9b"
integrity sha512-3VZD31lRyG2JoAa7imyhAIUZbqaZk4FSsa5QenNjUhNNJKomMLcwuhAOyl+i/suT7UB2H16Ta/7tSUP+Hkq/EQ==

nan@^2.12.1:
version "2.14.0"
Expand Down Expand Up @@ -3484,13 +3484,20 @@ ret@~0.1.10:
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"
integrity sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==

rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.2, rimraf@^2.6.3:
rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.3:
version "2.6.3"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab"
integrity sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA==
dependencies:
glob "^7.1.3"

rimraf@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.0.tgz#614176d4b3010b75e5c390eb0ee96f6dc0cebb9b"
integrity sha512-NDGVxTsjqfunkds7CqsOiEnxln4Bo7Nddl3XhS4pXg5OzwkLqJ971ZVAAnB+DDLnF76N+VnDEiBHaVV8I06SUg==
dependencies:
glob "^7.1.3"

rsvp@^4.8.4:
version "4.8.5"
resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-4.8.5.tgz#c8f155311d167f68f21e168df71ec5b083113734"
Expand Down

0 comments on commit 4e0bc1d

Please sign in to comment.