Skip to content

Commit 8831129

Browse files
Merge pull request #223 from terminusdb/path_queries
Path queries
2 parents 8ccd363 + 05d2458 commit 8831129

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

lib/query/woqlCore.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,9 @@ function getClauseAndRemainder(pat) {
709709

710710
function compilePredicate(pp, q) {
711711
if (pp.indexOf('<') !== -1 && pp.indexOf('>') !== -1) {
712-
let pred = pp.slice(1, pp.length - 1);
712+
const pred = pp.slice(1, pp.length - 1);
713713
// eslint-disable-next-line no-multi-assign,no-constant-condition
714-
const cleaned = (pred = '*' ? 'owl:topObjectProperty' : q.cleanPathPredicate(pred));
714+
const cleaned = pred === '.' ? null : q.cleanPathPredicate(pred);
715715
return {
716716
'@type': 'PathOr',
717717
or: [{
@@ -725,18 +725,15 @@ function compilePredicate(pp, q) {
725725
};
726726
} if (pp.indexOf('<') !== -1) {
727727
const pred = pp.slice(1, pp.length);
728-
// let cleaned = pred === '*' ? 'owl:topObjectProperty' : q.cleanPathPredicate(pred)
729-
const cleaned = q.cleanPathPredicate(pred);
728+
const cleaned = pred === '.' ? null : q.cleanPathPredicate(pred);
730729
return { '@type': 'InversePathPredicate', predicate: cleaned };
731730
} if (pp.indexOf('>') !== -1) {
732731
const pred = pp.slice(0, pp.length - 1);
733-
// let cleaned = pred === '*' ? 'owl:topObjectProperty' : q.cleanPathPredicate(pred)
734-
const cleaned = q.cleanPathPredicate(pred);
732+
const cleaned = pred === '.' ? null : q.cleanPathPredicate(pred);
735733
return { '@type': 'PathPredicate', predicate: cleaned };
736734
}
737-
// let pred = pp === '*' ? 'owl:topObjectProperty' : q.cleanPathPredicate(pp)
738-
const pred = q.cleanPathPredicate(pp);
739-
return { '@type': 'PathPredicate', predicate: pred };
735+
const cleaned = pp === '.' ? null : q.cleanPathPredicate(pp);
736+
return { '@type': 'PathPredicate', predicate: cleaned };
740737
}
741738

742739
/**
@@ -843,6 +840,8 @@ function copyJSON(orig, rollup) {
843840
}
844841
}
845842
nuj[k] = nupart;
843+
} else if (part === null) {
844+
// do nothing
846845
} else if (typeof part === 'object') {
847846
const q = copyJSON(part, rollup);
848847
if (!q || !UTILS.empty(q)) nuj[k] = q;

lib/query/woqlPrinter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ WOQLPrinter.prototype.decompilePathPattern = function (pstruct) {
357357
// eslint-disable-next-line default-case
358358
switch (t) {
359359
case 'InversePathPredicate':
360-
return `<${pstruct.predicate}`;
360+
return pstruct.predicate ? `<${pstruct.predicate}` : '<.';
361361
case 'PathPredicate':
362-
return pstruct.predicate;
362+
return pstruct.predicate ? `${pstruct.predicate}` : '.';
363363
case 'PathPlus':
364364
var next = pstruct.plus;
365365
if (Array.isArray(next)) next = next[0];

test/woqlPathquery.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,38 @@ describe('woql path query and path query prettyprint', () => {
2727
expect(query.prettyPrint()).to.eql('WOQL.path("v:X", "hop", "v:Y", "v:Path")');
2828
});
2929

30+
it('simple any path query', () => {
31+
const query = WOQL.path('v:X', '.*,name', 'v:Y');
32+
const json = {
33+
'@type': 'Path',
34+
subject: {
35+
'@type': 'NodeValue',
36+
variable: 'X',
37+
},
38+
pattern: {
39+
'@type': "PathSequence",
40+
'sequence': [
41+
{
42+
'@type': "PathStar",
43+
'star': {
44+
'@type': "PathPredicate"
45+
}
46+
},
47+
{
48+
'@type': "PathPredicate",
49+
'predicate' : "name"
50+
}
51+
]
52+
},
53+
object: {
54+
'@type': 'Value',
55+
variable: 'Y',
56+
}
57+
};
58+
expect(query.json()).to.eql(json);
59+
expect(query.prettyPrint()).to.eql('WOQL.path("v:X", "(.*),name", "v:Y")');
60+
});
61+
3062
it('test plus directed path query', () => {
3163
const query = WOQL.path('v:X', '<hop+', 'v:Y', 'v:Path');
3264
const json = {

0 commit comments

Comments
 (0)