Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sparqlalgebrajs

Package Overview
Dependencies
Maintainers
1
Versions
70
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sparqlalgebrajs - npm Package Compare versions

Comparing version 1.5.2 to 2.0.0

1

lib/factory.d.ts

@@ -45,3 +45,4 @@ import * as A from './algebra';

createTermExpression(term: RDF.Term): A.TermExpression;
createWildcardExpression(): A.TermExpression;
createTerm(str: string): RDF.Term;
}

@@ -5,2 +5,3 @@ "use strict";

const rdf_string_1 = require("rdf-string");
const Wildcard = require('sparqljs').Wildcard;
class Factory {

@@ -77,2 +78,3 @@ constructor(dataFactory) {

createTermExpression(term) { return { type: 'expression', expressionType: 'term', term }; }
createWildcardExpression() { return this.createTermExpression(new Wildcard()); }
createTerm(str) {

@@ -79,0 +81,0 @@ return rdf_string_1.stringToTerm(str, this.dataFactory);

84

lib/sparql.js

@@ -5,3 +5,6 @@ "use strict";

const factory_1 = require("./factory");
const util_1 = require("./util");
const rdf_string_1 = require("rdf-string");
const SparqlGenerator = require('sparqljs').Generator;
const Wildcard = require('sparqljs').Wildcard;
const types = Algebra.types;

@@ -86,19 +89,3 @@ const eTypes = Algebra.expressionTypes;

function translateTerm(term) {
if (term.termType === 'BlankNode')
return '_:' + term.value;
if (term.termType === 'Literal') {
// TODO: should check which safety checks are required
let lit = term;
let result = `"${term.value}"`;
if (lit.language)
result += '@' + lit.language;
else if (lit.datatype && lit.datatype.value !== 'http://www.w3.org/2001/XMLSchema#string')
result += '^^' + lit.datatype.value;
return result;
}
if (term.termType === 'NamedNode')
return term.value;
if (term.termType === 'Variable')
return '?' + term.value;
throw new Error('Unknown Term type ' + term.termType);
return rdf_string_1.termToString(term);
}

@@ -129,3 +116,3 @@ // ------------------------- EXPRESSIONS -------------------------

type: 'functionCall',
function: translateTerm(expr.name),
function: expr.name,
args: expr.args.map(translateExpression)

@@ -150,3 +137,3 @@ };

function translateTermExpression(expr) {
return translateTerm(expr.term);
return expr.term;
}

@@ -193,3 +180,3 @@ // ------------------------- OPERATIONS -------------------------

type: 'bind',
variable: translateTerm(op.variable),
variable: op.variable,
expression: translateExpression(op.expression)

@@ -204,4 +191,4 @@ }

obj.from = {
default: op.default.map(translateTerm),
named: op.named.map(translateTerm)
default: op.default,
named: op.named
};

@@ -223,3 +210,3 @@ return result;

patterns: flatten([translateOperation(op.input)]),
name: translateTerm(op.name)
name: op.name
};

@@ -281,5 +268,5 @@ }

triples: [{
subject: translateTerm(op.subject),
subject: op.subject,
predicate: translatePathComponent(op.predicate),
object: translateTerm(op.object)
object: op.object
}]

@@ -291,11 +278,12 @@ };

return {
subject: translateTerm(op.subject),
predicate: translateTerm(op.predicate),
object: translateTerm(op.object)
subject: op.subject,
predicate: op.predicate,
object: op.object
};
}
function replaceAggregatorVariables(s, map) {
if (typeof s === 'string') {
if (map[s])
return map[s];
let st = util_1.default.isTerm(s) ? translateTerm(s) : s;
if (typeof st === 'string') {
if (map[st])
return map[st];
}

@@ -318,3 +306,3 @@ else if (Array.isArray(s)) {

result.queryType = 'SELECT';
result.variables = op.variables.map(translateTerm);
result.variables = op.variables;
}

@@ -326,3 +314,3 @@ else if (type === types.ASK) {

result.queryType = 'DESCRIBE';
result.variables = op.terms.map(translateTerm);
result.variables = op.terms;
}

@@ -352,3 +340,4 @@ // backup values in case of nested queries

if (context.group.length > 0)
result.group = context.group.map(translateTerm).map(v => {
result.group = context.group.map(variable => {
let v = translateTerm(variable);
if (extensions[v]) {

@@ -358,7 +347,7 @@ let result = extensions[v];

return {
variable: v,
variable,
expression: result
};
}
return { expression: v };
return { expression: variable };
});

@@ -370,13 +359,14 @@ // descending expressions will already be in the correct format due to the structure of those

if (result.variables) {
result.variables = result.variables.map((v) => {
result.variables = result.variables.map((term) => {
let v = translateTerm(term);
if (extensions[v])
return {
variable: v,
variable: term,
expression: extensions[v]
};
return v;
return term;
});
// if the * didn't match any variables this would be empty
if (result.variables.length === 0)
result.variables = ['*'];
result.variables = [new Wildcard()];
}

@@ -401,2 +391,4 @@ // convert filter to 'having' if it contains an aggregator variable

function objectContainsValues(o, vals) {
if (util_1.default.isTerm(o))
return vals.indexOf(translateTerm(o)) >= 0;
if (Array.isArray(o))

@@ -422,3 +414,3 @@ return o.some(e => objectContainsValues(e, vals));

type: 'service',
name: translateTerm(op.name),
name: op.name,
silent: op.silent,

@@ -456,3 +448,3 @@ patterns

if (binding[s])
result[s] = translateTerm(binding[s]);
result[s] = binding[s];
else

@@ -493,3 +485,3 @@ result[s] = undefined;

pathType: '^',
items: [translateTerm(iri)]
items: [iri]
};

@@ -520,3 +512,3 @@ });

function translateLink(path) {
return translateTerm(path.iri);
return path.iri;
}

@@ -528,3 +520,3 @@ function translateNps(path) {

pathType: '!',
items: path.iris.map(translateTerm)
items: path.iris
};

@@ -537,3 +529,3 @@ return {

pathType: '|',
items: path.iris.map(translateTerm)
items: path.iris
}]

@@ -540,0 +532,0 @@ };

@@ -6,4 +6,5 @@ "use strict";

const util_1 = require("./util");
const equal = require("fast-deep-equal");
const rdf_string_1 = require("rdf-string");
const Parser = require('sparqljs').Parser;
const isEqual = require('lodash.isequal');
const types = Algebra.types;

@@ -60,5 +61,4 @@ let variables = new Set();

}
function isVariable(str) {
// there is also a '?' operator...
return isString(str) && str[0] === '?' && str.length > 1;
function isVariable(term) {
return term && term.termType === "Variable";
}

@@ -69,3 +69,3 @@ // 18.2.1

if (isVariable(thingy)) {
inScope[thingy] = true;
inScope[rdf_string_1.termToString(thingy)] = true;
variables.add(thingy); // keep track of all variables so we don't generate duplicates

@@ -81,3 +81,3 @@ }

for (let v of thingy.variables) {
if (v === '*')
if (v.termType === "Wildcard")
Object.assign(inScope, all);

@@ -143,8 +143,9 @@ else if (v.variable) // aggregates

function translateExpression(exp) {
if (isString(exp))
return factory.createTermExpression(factory.createTerm(exp));
if (util_1.default.isTerm(exp)) {
return factory.createTermExpression(exp);
}
if (exp.aggregation)
return factory.createAggregateExpression(exp.aggregation, translateExpression(exp.expression), exp.distinct, exp.separator);
if (exp.function)
return factory.createNamedExpression(factory.createTerm(exp.function), exp.args.map(translateExpression));
return factory.createNamedExpression(exp.function, exp.args.map(translateExpression));
if (exp.operator) {

@@ -187,10 +188,10 @@ if (exp.operator === 'exists' || exp.operator === 'notexists')

function translatePath(triple) {
let sub = factory.createTerm(triple.subject);
let sub = triple.subject;
let pred = translatePathPredicate(triple.predicate);
let obj = factory.createTerm(triple.object);
let obj = triple.object;
return simplifyPath(sub, pred, obj);
}
function translatePathPredicate(predicate) {
if (isString(predicate))
return factory.createLink(factory.createTerm(predicate));
if (util_1.default.isTerm(predicate) && predicate.termType === "NamedNode")
return factory.createLink(predicate);
if (predicate.pathType === '^')

@@ -208,3 +209,3 @@ return factory.createInv(translatePathPredicate(predicate.items[0]));

for (let item of items) {
if (isString(item))
if (util_1.default.isTerm(item))
normals.push(item);

@@ -217,4 +218,4 @@ else if (item.pathType === '^')

// NPS elements do not have the LINK function
let normalElement = factory.createNps(normals.map(factory.createTerm.bind(factory)));
let invertedElement = factory.createInv(factory.createNps(inverted.map(factory.createTerm.bind(factory))));
let normalElement = factory.createNps(normals);
let invertedElement = factory.createInv(factory.createNps(inverted));
if (inverted.length === 0)

@@ -236,3 +237,3 @@ return normalElement;

return factory.createZeroOrOnePath(translatePathPredicate(predicate.items[0]));
throw new Error('Unable to translate path expression ' + predicate);
throw new Error('Unable to translate path expression ' + JSON.stringify(predicate));
}

@@ -260,12 +261,11 @@ function simplifyPath(subject, predicate, object) {

function translateTriple(triple) {
return factory.createPattern(factory.createTerm(triple.subject), factory.createTerm(triple.predicate), factory.createTerm(triple.object));
return factory.createPattern(triple.subject, triple.predicate, triple.object);
}
function translateGraph(graph) {
let name = factory.createTerm(graph.name);
graph.type = 'group';
let result = translateGroupGraphPattern(graph);
if (useQuads)
result = recurseGraph(result, name);
result = recurseGraph(result, graph.name);
else
result = factory.createGraph(result, name);
result = factory.createGraph(result, graph.name);
return result;

@@ -309,7 +309,7 @@ }

else if (E.type === 'bind')
G = factory.createExtend(G, factory.createTerm(E.variable), translateExpression(E.expression));
G = factory.createExtend(G, E.variable, translateExpression(E.expression));
else if (E.type === 'service') {
// transform to group so childnodes get parsed correctly
E.type = 'group';
let A = factory.createService(translateGroupGraphPattern(E), factory.createTerm(E.name), E.silent);
let A = factory.createService(translateGroupGraphPattern(E), E.name, E.silent);
G = simplifiedJoin(G, A);

@@ -342,3 +342,3 @@ }

for (let key of keys)
map[key] = factory.createTerm(binding[key]);
map[key] = binding[key];
return map;

@@ -363,3 +363,3 @@ });

if (e.expression.type) {
const v = e.variable ? factory.createTerm(e.variable) : generateFreshVar();
const v = e.variable ? e.variable : generateFreshVar();
res = factory.createExtend(res, v, translateExpression(e.expression));

@@ -369,3 +369,3 @@ vars.push(v);

else
vars.push(factory.createTerm(e.expression)); // this will always be a var, otherwise sparql would be invalid
vars.push(e.expression); // this will always be a var, otherwise sparql would be invalid
}

@@ -385,3 +385,3 @@ }

if (query.queryType === 'SELECT' || query.queryType === 'DESCRIBE') {
if (query.variables.indexOf('*') >= 0)
if (query.variables.some((e) => e && e.termType === "Wildcard"))
PV = variables;

@@ -392,6 +392,6 @@ else {

if (isVariable(v) || !v.variable)
PV.add(factory.createTerm(v));
PV.add(v);
else if (v.variable) // ... AS ?x
{
PV.add(factory.createTerm(v.variable));
PV.add(v.variable);
E.push(v);

@@ -404,3 +404,3 @@ }

for (let v of E)
res = factory.createExtend(res, factory.createTerm(v.variable), translateExpression(v.expression));
res = factory.createExtend(res, v.variable, translateExpression(v.expression));
// 18.2.5

@@ -437,3 +437,3 @@ // not using toList and toMultiset

if (query.from)
res = factory.createFrom(res, query.from.default.map(factory.createTerm.bind(factory)), query.from.named.map(factory.createTerm.bind(factory)));
res = factory.createFrom(res, query.from.default, query.from.named);
return res;

@@ -449,4 +449,4 @@ }

for (let key of Object.keys(aggregates)) {
if (isEqual(aggregates[key], thingy)) {
v = key;
if (equal(aggregates[key], thingy)) {
v = factory.createTerm(key);
found = true;

@@ -457,6 +457,6 @@ break;

if (!found) {
v = '?' + generateFreshVar().value; // this is still in "sparql.js language" so a var string is still needed
aggregates[v] = thingy;
v = generateFreshVar();
aggregates[rdf_string_1.termToString(v)] = thingy;
}
return v; // this is still in "sparql.js language" so a var string is still needed
return v;
}

@@ -463,0 +463,0 @@ // non-aggregate expression

@@ -52,2 +52,3 @@ import * as A from "./algebra";

}, dataFactory: RDF.DataFactory): RDF.Variable;
static isTerm(term: any): boolean;
}

@@ -54,0 +55,0 @@ /**

@@ -397,4 +397,7 @@ "use strict";

}
static isTerm(term) {
return term.termType !== undefined;
}
}
exports.default = Util;
//# sourceMappingURL=util.js.map
{
"name": "sparqlalgebrajs",
"version": "1.5.2",
"version": "2.0.0",
"description": "Convert SPARQL to SPARQL algebra",

@@ -14,6 +14,6 @@ "author": "Joachim Van Herwegen",

"@rdfjs/data-model": "^1.1.1",
"lodash.isequal": "^4.5.0",
"fast-deep-equal": "^2.0.1",
"minimist": "^1.2.0",
"rdf-string": "^1.3.1",
"sparqljs": "^2.2.1"
"sparqljs": "^3.0.0"
},

@@ -25,3 +25,3 @@ "devDependencies": {

"@types/mocha": "^5.2.6",
"@types/node": "^11.9.5",
"@types/node": "^12.0.2",
"@types/rdf-js": "^2.0.1",

@@ -28,0 +28,0 @@ "chai": "^4.1.2",

@@ -133,3 +133,3 @@ # SPARQL to SPARQL Algebra converter

## A note on tests
Every test consists of a sparql file and a corresponding json file containg the algebra result.
Every test consists of a sparql file and a corresponding json file containing the algebra result.
Tests ending with `(quads)` in their name are tested/generated with `quads: true` in the options.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc