-
Notifications
You must be signed in to change notification settings - Fork 2
Feature/335 property path parser and ast #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prbblrypier
wants to merge
6
commits into
feature/corese-next
Choose a base branch
from
feature/335-property-path-parser-and-ast
base: feature/corese-next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f3cc438
path ast
prbblrypier 6b9b380
fix(next/parser): TriplePatternAst PathAst predicate + addTriple over…
prbblrypier 1d5bed5
feat(next/parser): map SPARQL property paths to PathAst in SparqlAstB…
prbblrypier 1a9e17e
path ast parser
prbblrypier 631ee4a
ast visitor after rebase
MaillPierre 523b180
bump gradle 9.1
prbblrypier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| import fr.inria.corese.core.next.query.impl.parser.semantic.support.VariableScopeAnalyzer; | ||
| import fr.inria.corese.core.next.query.impl.sparql.ast.*; | ||
| import fr.inria.corese.core.next.query.impl.sparql.ast.constraint.*; | ||
| import fr.inria.corese.core.next.query.impl.sparql.ast.path.*; | ||
| import org.antlr.v4.runtime.tree.ParseTree; | ||
| import org.antlr.v4.runtime.tree.TerminalNode; | ||
|
|
||
|
|
@@ -56,6 +57,11 @@ public abstract class SparqlAstBuilder { | |
| */ | ||
| protected final Deque<List<TriplePatternAst>> bgpStack = new ArrayDeque<>(); | ||
|
|
||
| /** | ||
| * Counter for anonymous blank nodes created while expanding {@code [ ... ]} and {@code ( ... )} subjects. | ||
| */ | ||
| private int anonymousBlankNodeCounter; | ||
|
|
||
| /** | ||
| * Stack of currently open SELECT operations (top-level SELECT and nested SELECT subqueries). | ||
| */ | ||
|
|
@@ -363,19 +369,24 @@ protected SelectFrame getCurrentSelectFrame() { | |
| } | ||
|
|
||
| /** | ||
| * Add a triple pattern (?s ?p ?o) to the current BGP (TriplesBlock). | ||
| * Add a triple pattern to the current BGP (TriplesBlock). | ||
| * This must be called while inside a TriplesBlock. | ||
| */ | ||
| public void addTriple(TermAst s, TermAst p, TermAst o) { | ||
| public void addTriple(TermAst s, PathAst p, TermAst o) { | ||
| if (bgpStack.isEmpty()) { | ||
| // This should not happen if listener wiring is correct, but we fail loudly | ||
| // because BGP boundaries matter (TriplesBlock). | ||
| throw new IllegalStateException("addTriple() called outside of TriplesBlock (BGP). " + | ||
| "Ensure you call enterBgp() on enterTriplesBlock."); | ||
| } | ||
| bgpStack.peek().add(new TriplePatternAst(s, p, o)); | ||
| } | ||
|
|
||
| /** | ||
| * Add a triple whose predicate is a single term (IRI, variable, etc.). | ||
| */ | ||
| public void addTriple(TermAst s, TermAst p, TermAst o) { | ||
| addTriple(s, PathAst.from(p), o); | ||
| } | ||
|
|
||
| // --- Filters --- | ||
|
|
||
| /** | ||
|
|
@@ -1278,12 +1289,221 @@ public TermAst termFromReplace(SparqlParser.StrReplaceExpressionContext ctx) { | |
| } | ||
|
|
||
| /** | ||
| * Predicate as a property path. | ||
| * For simple triples without a composed path, this is just an iriRef or 'a'. | ||
| * Composed property paths will be expanded in a later phase if needed. | ||
| * Predicate as a SPARQL 1.1 property path. | ||
| */ | ||
| public TermAst termFromVerbPath(SparqlParser.VerbPathContext ctx) { | ||
| return this.iri(ctx.getText()); | ||
| public PathAst pathFromVerbPath(SparqlParser.VerbPathContext ctx) { | ||
| return pathFromPath(ctx.path()); | ||
| } | ||
|
|
||
| public PathAst pathFromPath(SparqlParser.PathContext ctx) { | ||
| return pathFromPathAlternative(ctx.pathAlternative()); | ||
| } | ||
|
|
||
| private PathAst pathFromPathAlternative(SparqlParser.PathAlternativeContext ctx) { | ||
| return foldAlternatives(ctx.pathSequence().stream().map(this::pathFromPathSequence).toList()); | ||
| } | ||
|
|
||
| private PathAst pathFromPathSequence(SparqlParser.PathSequenceContext ctx) { | ||
| return foldSequences(ctx.pathEltOrInverse().stream().map(this::pathFromPathEltOrInverse).toList()); | ||
| } | ||
|
|
||
| private PathAst pathFromPathEltOrInverse(SparqlParser.PathEltOrInverseContext ctx) { | ||
| if (ctx.CARET() != null) { | ||
| return new InversePathAst(pathFromPathElt(ctx.pathElt())); | ||
| } | ||
| return pathFromPathElt(ctx.pathElt()); | ||
| } | ||
|
|
||
| private PathAst pathFromPathElt(SparqlParser.PathEltContext ctx) { | ||
| PathAst primary = pathFromPathPrimary(ctx.pathPrimary()); | ||
| SparqlParser.PathModContext mod = ctx.pathMod(); | ||
| if (mod == null) { | ||
| return primary; | ||
| } | ||
| if (mod.QUESTION() != null) { | ||
| return new OptionalPathAst(primary); | ||
| } | ||
| if (mod.STAR() != null) { | ||
| return new ZeroOrMorePathAst(primary); | ||
| } | ||
| if (mod.PLUS() != null) { | ||
| return new OneOrMorePathAst(primary); | ||
| } | ||
| throw new QueryEvaluationException("Unexpected path modifier in " + ctx.getText()); | ||
| } | ||
|
|
||
| private PathAst pathFromPathPrimary(SparqlParser.PathPrimaryContext ctx) { | ||
| if (ctx.iriRef() != null) { | ||
| return PathAst.from(termFromIriRef(ctx.iriRef())); | ||
| } | ||
| if (ctx.A() != null) { | ||
| return PathAst.from(iri("a")); | ||
| } | ||
| if (ctx.EXCLAMATION() != null) { | ||
| return pathFromPathNegatedPropertySet(ctx.pathNegatedPropertySet()); | ||
| } | ||
| if (ctx.path() != null) { | ||
| return pathFromPath(ctx.path()); | ||
| } | ||
| throw new QueryEvaluationException("Unexpected path primary in " + ctx.getText()); | ||
| } | ||
|
|
||
| private PathAst pathFromPathNegatedPropertySet(SparqlParser.PathNegatedPropertySetContext ctx) { | ||
| List<PathAst> excluded; | ||
| if (ctx.L_PAREN() != null) { | ||
| excluded = ctx.pathOneInPropertySet().stream().map(this::pathFromPathOneInPropertySet).toList(); | ||
| } else { | ||
| excluded = List.of(pathFromPathOneInPropertySet(ctx.pathOneInPropertySet().getFirst())); | ||
| } | ||
| return new NegatedPropertySetPathAst(excluded); | ||
| } | ||
|
|
||
| private PathAst pathFromPathOneInPropertySet(SparqlParser.PathOneInPropertySetContext ctx) { | ||
| if (ctx.CARET() != null) { | ||
| if (ctx.iriRef() != null) { | ||
| return new InversePathAst(PathAst.from(termFromIriRef(ctx.iriRef()))); | ||
| } | ||
| return new InversePathAst(PathAst.from(iri("a"))); | ||
| } | ||
| if (ctx.iriRef() != null) { | ||
| return PathAst.from(termFromIriRef(ctx.iriRef())); | ||
| } | ||
| return PathAst.from(iri("a")); | ||
| } | ||
|
|
||
| private PathAst foldAlternatives(List<PathAst> parts) { | ||
| if (parts.isEmpty()) { | ||
| throw new QueryEvaluationException("Empty property path alternative"); | ||
| } | ||
| PathAst result = parts.getFirst(); | ||
| for (int i = 1; i < parts.size(); i++) { | ||
| result = new AlternativePathAst(result, parts.get(i)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private PathAst foldSequences(List<PathAst> parts) { | ||
| if (parts.isEmpty()) { | ||
| throw new QueryEvaluationException("Empty property path sequence"); | ||
| } | ||
| PathAst result = parts.getFirst(); | ||
| for (int i = 1; i < parts.size(); i++) { | ||
| result = new SequencePathAst(result, parts.get(i)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a fresh anonymous blank node label for expanded BGP triples. | ||
| */ | ||
| public TermAst newAnonymousBlankNode() { | ||
| return iri("_:b" + anonymousBlankNodeCounter++); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
| } | ||
|
|
||
| /** | ||
| * Expands a {@code propertyListPathNotEmpty} into triple patterns for the given subject. | ||
| */ | ||
| public void addTriplesFromPropertyListPath( | ||
| TermAst subject, | ||
| SparqlParser.PropertyListPathNotEmptyContext propertyList) { | ||
| var verbPaths = propertyList.verbPath(); | ||
| var verbSimples = propertyList.verbSimple(); | ||
| var objectLists = propertyList.objectListPath(); | ||
|
|
||
| int verbPathIdx = 0; | ||
| int verbSimpleIdx = 0; | ||
|
|
||
| for (SparqlParser.ObjectListPathContext objectList : objectLists) { | ||
| PathAst predicate = predicateFromPropertyListPath( | ||
| verbPaths, verbSimples, verbPathIdx, verbSimpleIdx); | ||
| if (verbPathIdx < verbPaths.size() | ||
| && (verbSimpleIdx >= verbSimples.size() | ||
| || verbPaths.get(verbPathIdx).getStart().getTokenIndex() | ||
| < verbSimples.get(verbSimpleIdx).getStart().getTokenIndex())) { | ||
| verbPathIdx++; | ||
| } else { | ||
| verbSimpleIdx++; | ||
| } | ||
|
|
||
| for (TermAst object : termListFromObjectListPath(objectList)) { | ||
| addTriple(subject, predicate, object); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private PathAst predicateFromPropertyListPath( | ||
| List<SparqlParser.VerbPathContext> verbPaths, | ||
| List<SparqlParser.VerbSimpleContext> verbSimples, | ||
| int verbPathIdx, | ||
| int verbSimpleIdx) { | ||
| boolean useVerbPath = verbPathIdx < verbPaths.size() | ||
| && (verbSimpleIdx >= verbSimples.size() | ||
| || verbPaths.get(verbPathIdx).getStart().getTokenIndex() | ||
| < verbSimples.get(verbSimpleIdx).getStart().getTokenIndex()); | ||
| if (useVerbPath) { | ||
| return pathFromVerbPath(verbPaths.get(verbPathIdx)); | ||
| } | ||
| return PathAst.from(termFromVerbSimple(verbSimples.get(verbSimpleIdx))); | ||
| } | ||
|
|
||
| /** | ||
| * Builds triple patterns for a {@code triplesNodePath} subject and returns its head term. | ||
| */ | ||
| public TermAst subjectFromTriplesNodePath(SparqlParser.TriplesNodePathContext ctx) { | ||
| if (ctx.blankNodePropertyListPath() != null) { | ||
| return subjectFromBlankNodePropertyListPath(ctx.blankNodePropertyListPath()); | ||
| } | ||
| if (ctx.collectionPath() != null) { | ||
| return subjectFromCollectionPath(ctx.collectionPath()); | ||
| } | ||
| throw new QueryEvaluationException("Unexpected triples node path: " + ctx.getText()); | ||
| } | ||
|
|
||
| private TermAst subjectFromBlankNodePropertyListPath( | ||
| SparqlParser.BlankNodePropertyListPathContext ctx) { | ||
| TermAst blankNode = newAnonymousBlankNode(); | ||
| if (ctx.propertyListPathNotEmpty() != null) { | ||
| addTriplesFromPropertyListPath(blankNode, ctx.propertyListPathNotEmpty()); | ||
| } | ||
| return blankNode; | ||
| } | ||
|
|
||
| private TermAst subjectFromCollectionPath(SparqlParser.CollectionPathContext ctx) { | ||
| List<SparqlParser.GraphNodePathContext> nodes = ctx.graphNodePath(); | ||
| if (nodes.isEmpty()) { | ||
| throw new QueryEvaluationException("Empty RDF collection in triple pattern"); | ||
| } | ||
|
|
||
| TermAst head = newAnonymousBlankNode(); | ||
| TermAst current = head; | ||
| for (int i = 0; i < nodes.size(); i++) { | ||
| addTriple(current, rdfTerm(RDF.first), termFromGraphNodePath(nodes.get(i))); | ||
| if (i == nodes.size() - 1) { | ||
| addTriple(current, rdfTerm(RDF.rest), rdfTerm(RDF.nil)); | ||
| } else { | ||
| TermAst next = newAnonymousBlankNode(); | ||
| addTriple(current, rdfTerm(RDF.rest), next); | ||
| current = next; | ||
| } | ||
| } | ||
| return head; | ||
| } | ||
|
|
||
| /** | ||
| * Graph node inside a property-path triple (variable/term, blank node property list, or collection). | ||
| */ | ||
| public TermAst termFromGraphNodePath(SparqlParser.GraphNodePathContext ctx) { | ||
| if (ctx.varOrTerm() != null) { | ||
| return termFromVarOrTerm(ctx.varOrTerm()); | ||
| } | ||
| if (ctx.triplesNodePath() != null) { | ||
| return subjectFromTriplesNodePath(ctx.triplesNodePath()); | ||
| } | ||
| throw new QueryEvaluationException("Unexpected graph node path: " + ctx.getText()); | ||
| } | ||
|
|
||
| private TermAst rdfTerm(RDF term) { | ||
| return new IriAst("<" + term.getIRI().stringValue() + ">"); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1300,12 +1520,7 @@ public TermAst termFromVerbSimple(SparqlParser.VerbSimpleContext ctx) { | |
| public List<TermAst> termListFromObjectListPath(SparqlParser.ObjectListPathContext ctx) { | ||
| List<TermAst> out = new ArrayList<>(); | ||
| for (var objPath : ctx.objectPath()) { | ||
| var graphNodePath = objPath.graphNodePath(); | ||
| if (graphNodePath.varOrTerm() != null) { | ||
| out.add(termFromVarOrTerm(graphNodePath.varOrTerm())); | ||
| } else { | ||
| out.add(this.iri(graphNodePath.getText())); | ||
| } | ||
| out.add(termFromGraphNodePath(objPath.graphNodePath())); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This generates fresh blank nodes with labels like
_:b0. That can collide with user-written blank node labels and make two different nodes become equal in the AST.