Skip to content

Commit

Permalink
Remove unnecessary inverse paths
Browse files Browse the repository at this point in the history
  • Loading branch information
pheyvaer committed May 2, 2024
1 parent 6da531c commit b667264
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
25 changes: 23 additions & 2 deletions lib/ShapesGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ export class ShapesGraph {
alreadyProcessedPaths.push(p);
p = this.cleanPath(p);

if (p.startsWith('^')) {
p = p.substring(1);
if (this.isRealInversePath(p)) {
p = this.getRealPath(p);
mermaid += ` S${shapedId}_${this.counter}[ ]${link}|"${p}"|S${shapedId}\n`;
} else {
p = this.getRealPath(p);
mermaid += ` S${shapedId}${link}|"${p}"|S${shapedId}_${this.counter}[ ]\n`;
}

Expand All @@ -155,6 +156,26 @@ export class ShapesGraph {
return mermaid;
}

private isRealInversePath(path: string) {
const found = path.match(/^(\^+)[^\^]+/);

if (!found) {
return false;
}

return found[1].length % 2 !== 0;
}

private getRealPath(path: string) {
const found = path.match(/^\^*([^\^]+)/);

if (!found) {
return ''; //TODO: throw error.
}

return found[1];
}

protected constructPathPattern(shapeStore: RdfStore, listItem: Term): Path {
if (listItem.termType === "BlankNode") {
//Look for special types
Expand Down
2 changes: 1 addition & 1 deletion tests/07 - mermaid/double-inverse-path.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
flowchart TD
S1((Shape))
S1_0[ ]-->|http://example.org/p2|S1
S1-->|"http://example.org/p1"|S1_0[ ]
14 changes: 13 additions & 1 deletion tests/07 - mermaid/mermaid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,24 @@ describe("Test whether the correct Mermaid text is generated for a ShapesGraph",
assert.equal(actualMermaid, expectedMermaid);
});

it.skip("Double inverse path", async () => {
it("Double inverse path", async () => {
const actualMermaid = shapesGraph.toMermaid(df.namedNode("http://example.org/DoubleInversePathShape"));
const expectedMermaid = await fs.readFile('./tests/07 - mermaid/double-inverse-path.txt', 'utf-8');
assert.equal(actualMermaid, expectedMermaid);
});

it("Triple inverse path", async () => {
const actualMermaid = shapesGraph.toMermaid(df.namedNode("http://example.org/TripleInversePathShape"));
const expectedMermaid = await fs.readFile('./tests/07 - mermaid/triple-inverse-path.txt', 'utf-8');
assert.equal(actualMermaid, expectedMermaid);
});

it("Quadruple inverse path", async () => {
const actualMermaid = shapesGraph.toMermaid(df.namedNode("http://example.org/DoubleInversePathShape"));
const expectedMermaid = await fs.readFile('./tests/07 - mermaid/quadruple-inverse-path.txt', 'utf-8');
assert.equal(actualMermaid, expectedMermaid);
});

it("Zero or more path", async () => {
const actualMermaid = shapesGraph.toMermaid(df.namedNode("http://example.org/ZeroOrMorePathShape"));
const expectedMermaid = await fs.readFile('./tests/07 - mermaid/zero-or-more-path.txt', 'utf-8');
Expand Down
3 changes: 3 additions & 0 deletions tests/07 - mermaid/quadruple-inverse-path.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
flowchart TD
S1((Shape))
S1-->|"http://example.org/p1"|S1_0[ ]
18 changes: 17 additions & 1 deletion tests/07 - mermaid/shape.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,30 @@ ex:SequenceAndInversePathShape a sh:NodeShape ;
sh:minCount 1
] .

# Double inverse path
# Double inverse path
ex:DoubleInversePathShape a sh:NodeShape ;
sh:closed true;
sh:property [
sh:path ([sh:inversePath [sh:inversePath ex:p1 ] ]) ;
sh:minCount 1
] .

# Triple inverse path
ex:TripleInversePathShape a sh:NodeShape ;
sh:closed true;
sh:property [
sh:path ([sh:inversePath [sh:inversePath [sh:inversePath ex:p1 ] ] ]) ;
sh:minCount 1
] .

# Quadruple inverse path
ex:QuadrupleInversePathShape a sh:NodeShape ;
sh:closed true;
sh:property [
sh:path ([sh:inversePath [sh:inversePath [sh:inversePath [sh:inversePath ex:p1 ] ] ] ]) ;
sh:minCount 1
] .

# Zero or More path
ex:ZeroOrMorePathShape a sh:NodeShape ;
sh:closed true;
Expand Down
3 changes: 3 additions & 0 deletions tests/07 - mermaid/triple-inverse-path.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
flowchart TD
S1((Shape))
S1_0[ ]-->|"http://example.org/p1"|S1

0 comments on commit b667264

Please sign in to comment.