Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/main/antlr/SparqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ options {
tokenVocab = SparqlLexer;
}

queryUnit
: query EOF
query
: queryUnit EOF
| updateUnit EOF
;

query
queryUnit
: prologue (selectQuery | constructQuery | describeQuery | askQuery) valuesClause
;

updateUnit
: update EOF
: update
;

prologue
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ public void exitConstructTemplate(SparqlParser.ConstructTemplateContext ctx) {
for (var d : delegates) d.exitConstructTemplate(ctx);
}

@Override
public void enterLoad(SparqlParser.LoadContext ctx) {
for (var d : delegates) d.enterLoad(ctx);
}

@Override
public void exitLoad(SparqlParser.LoadContext ctx) {
for (var d : delegates) d.exitLoad(ctx);
}

@Override
public void enterClear(SparqlParser.ClearContext ctx) {
for (var d : delegates) d.enterClear(ctx);
}

@Override
public void exitClear(SparqlParser.ClearContext ctx) {
for (var d : delegates) d.exitClear(ctx);
}

@Override
public void enterWhereClause(SparqlParser.WhereClauseContext ctx) {
for (var d : delegates) d.enterWhereClause(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ private QueryAst buildAst(ParseTree tree, SparqlParserOptions options) {
new PrologueFeature(builder),
new BindFeature(builder),
new ServiceFeature(builder),
new ValuesFeature(builder)
new ValuesFeature(builder),
new LoadQueryFeature(builder),
new ClearQueryFeature(builder)
));

ParseTreeWalker walker = new ParseTreeWalker();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package fr.inria.corese.core.next.query.impl.parser.listener;

import fr.inria.corese.core.next.impl.parser.antlr.SparqlParser;
import fr.inria.corese.core.next.query.impl.parser.SparqlAstBuilder;

/**
* AST feature listener for CLEAR SPARQL update query
*/
public class ClearQueryFeature extends AbstractSparqlFeature {
public ClearQueryFeature(SparqlAstBuilder builder) {
super(builder);
}

@Override
public void exitClear(SparqlParser.ClearContext ctx) {
this.builder().exitClearQuery();
this.builder().setSilentFlag(ctx.SILENT() != null);
if(ctx.graphRefAll() != null) {
this.builder().setTargetGraphIri(this.builder().graphRefFromGraphRefAll(ctx.graphRefAll()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.inria.corese.core.next.query.impl.parser.listener;

import fr.inria.corese.core.next.impl.parser.antlr.SparqlParser;
import fr.inria.corese.core.next.query.impl.parser.SparqlAstBuilder;
import fr.inria.corese.core.next.query.impl.sparql.ast.IriAst;

/**
* AST feature listener for LOAD SPARQL update query
*/
public class LoadQueryFeature extends AbstractSparqlFeature {

public LoadQueryFeature(SparqlAstBuilder builder) {
super(builder);
}

public void exitLoad(SparqlParser.LoadContext ctx) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the "@OverRide" annotation above this method signature

this.builder().enterLoadQuery();
this.builder().setSilentFlag(ctx.SILENT() != null);
if(ctx.iriRef() != null) {
this.builder().setSourceGraphIri((IriAst) this.builder().termFromIriRef(ctx.iriRef()));
}
if(ctx.graphRef() != null) {
this.builder().setTargetGraphIri((IriAst) this.builder().termFromGraphRef(ctx.graphRef()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

import fr.inria.corese.core.next.query.api.validation.QueryDiagnostic;
import fr.inria.corese.core.next.query.impl.parser.semantic.support.VariableScopeAnalyzer;
import fr.inria.corese.core.next.query.impl.sparql.ast.AskQueryAst;
import fr.inria.corese.core.next.query.impl.sparql.ast.ConstructQueryAst;
import fr.inria.corese.core.next.query.impl.sparql.ast.DescribeQueryAst;
import fr.inria.corese.core.next.query.impl.sparql.ast.GroupGraphPatternAst;
import fr.inria.corese.core.next.query.impl.sparql.ast.OrderConditionAst;
import fr.inria.corese.core.next.query.impl.sparql.ast.ProjectionAst;
import fr.inria.corese.core.next.query.impl.sparql.ast.QueryAst;
import fr.inria.corese.core.next.query.impl.sparql.ast.SelectQueryAst;
import fr.inria.corese.core.next.query.impl.sparql.ast.SolutionModifierAst;
import fr.inria.corese.core.next.query.impl.sparql.ast.VarAst;
import fr.inria.corese.core.next.query.impl.sparql.ast.*;

import java.util.ArrayList;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -45,6 +36,8 @@ public List<QueryDiagnostic> validate(QueryAst queryAst) {
describeQueryAst.solutionModifier());
// TODO: handle ASK solution modifiers with SPARQL 1.1 support.
case AskQueryAst ignored -> List.of();
case LoadQueryAst ignored -> List.of();
case ClearQueryAst ignored -> List.of();
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import org.slf4j.Logger;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import org.slf4j.Logger is never used

import org.slf4j.LoggerFactory;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this unused import 'org.slf4j.LoggerFactory'.


import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

/**
* Collects visible and referenced variables from the next SPARQL AST.
Expand All @@ -26,8 +23,13 @@ public final class VariableScopeAnalyzer {
* @return the set of visible variable names, without {@code ?} or {@code $} in the patterns used for the resolution of the query
*/
public Set<String> collectVisibleVariables(QueryAst query) {
Set<String> visibleVariables = collectVisibleVariables(query.whereClause());
visibleVariables.addAll(collectVisibleVariables(query.valuesClause()));
Set<String> visibleVariables = new TreeSet<>();
if(query instanceof WhereClauseQueryAst whereClauseQueryAst) {
visibleVariables.addAll(collectVisibleVariables(whereClauseQueryAst.whereClause()));
}
if(query instanceof SparqlQueryAst sparqlQueryAst) {
visibleVariables.addAll(collectVisibleVariables(sparqlQueryAst.valuesClause()));
}
return visibleVariables;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public enum QUERY_TYPE {
DESCRIBE,
SELECT,

LOAD,
CLEAR,

UNDEFINED
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* }</pre>
*/
public record AskQueryAst(DatasetClauseAst datasetClause, GroupGraphPatternAst whereClause,
SolutionModifierAst solutionModifier, QueryPrologueAst prologue, ValuesAst valuesClause) implements QueryAst {
SolutionModifierAst solutionModifier, QueryPrologueAst prologue, ValuesAst valuesClause) implements SparqlQueryAst {

public AskQueryAst(DatasetClauseAst datasetClause, GroupGraphPatternAst whereClause) {
this(datasetClause, whereClause, null, null, null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.inria.corese.core.next.query.impl.sparql.ast;

/**
* Represents the CLEAR queries as defined in the <a href="https://www.w3.org/TR/2013/REC-sparql11-update-20130321/#clear">SPARQL 1.1 recommendation<a/>.
* @param graphRef targeted graph to be cleared
* @param silent Determine if the resolution of the query must be resolved silently or not.
*/
public record ClearQueryAst(GraphRefAst graphRef, boolean silent) implements UpdateQueryAst {
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public record ConstructQueryAst(
SolutionModifierAst solutionModifier,
QueryPrologueAst prologue,
ValuesAst valuesClause
) implements QueryAst {
) implements SparqlQueryAst {
public ConstructQueryAst {
if (constructTemplate == null) {
constructTemplate = new ConstructTemplateAst(List.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public record DescribeQueryAst(
SolutionModifierAst solutionModifier,
QueryPrologueAst prologue,
ValuesAst valuesClause
) implements QueryAst {
) implements SparqlQueryAst {
public DescribeQueryAst {
described = described != null ? List.copyOf(described) : List.of();
if (whereClause == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fr.inria.corese.core.next.query.impl.sparql.ast;

import fr.inria.corese.core.next.query.api.exception.QueryEvaluationException;

import java.util.ArrayList;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this unused import 'java.util.ArrayList'.

import java.util.List;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this unused import 'java.util.List'.


public record GraphRefAst(IriAst graph, boolean named, boolean all, boolean defaultGraph) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constructor allows invalid mixed states such as an explicit graph together with named, all, or defaultGraph.
It may be safer to reject these combinations here as well.

public GraphRefAst {
if(graph == null && !named && !all && !defaultGraph) {
throw new QueryEvaluationException("Graph reference does not actually reference any graph");
}
if(named && all) {
throw new QueryEvaluationException("Cannot have a Graph reference that is ALL and NAMED at the same time");
}
if(named && defaultGraph) {
throw new QueryEvaluationException("Cannot have a Graph reference that is NAMED and DEFAULT at the same time");
}
if(defaultGraph && all) {
throw new QueryEvaluationException("Cannot have a Graph reference that is ALL and DEFAULT at the same time");
}
}

public GraphRefAst(IriAst graph) {
this(graph, false, false, false);
}

public GraphRefAst(boolean named, boolean all, boolean defaultGraph) {
this(null, named, all, defaultGraph);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fr.inria.corese.core.next.query.impl.sparql.ast;

/**
* Constructor functions for Graph references
*/
public class GraphRefAsts {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a private constructor to hide the implicit public one.


public static GraphRefAst named() {
return new GraphRefAst(true, false, false);
}

public static GraphRefAst all() {
return new GraphRefAst(false, true, false);
}

public static GraphRefAst defaultGraph() {
return new GraphRefAst(false, false, true);
}

public static GraphRefAst graph(IriAst graphIri) {
return new GraphRefAst(graphIri);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.inria.corese.core.next.query.impl.sparql.ast;

import fr.inria.corese.core.next.query.api.exception.QueryEvaluationException;

import java.util.HashSet;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this unused import 'java.util.HashSet'.

import java.util.Set;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this unused import 'java.util.Set'.


/**
* Represents the LOAD queries as defined in the <a href="https://www.w3.org/TR/2013/REC-sparql11-update-20130321/#load">SPARQL 1.1 recommendation<a/>.
* @param fromClause From clause, set of graphs IRIs
* @param toClause To clause. Set of Graph IRIs
* @param silent Determine if the resolution of the query must be resolved silently or not.
*/
public record LoadQueryAst(GraphRefAst fromClause, GraphRefAst toClause, boolean silent) implements UpdateQueryAst {
public LoadQueryAst {
if(fromClause == null) {
throw new QueryEvaluationException("Load query must have at least an URI to load from");
}
}

public LoadQueryAst(GraphRefAst fromClause, GraphRefAst toClause) {
this(fromClause, toClause, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fr.inria.corese.core.next.query.impl.sparql.ast;

/**
* Represents queries that can be prefixed with a prologue declaration (i.e. Select, Insert, etc.)
*/
public sealed interface PrologueQueryAst permits SparqlQueryAst {
QueryPrologueAst prologue();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
package fr.inria.corese.core.next.query.impl.sparql.ast;

/**
* Minimal SPARQL query AST (SELECT, ASK, CONSTRUCT, DESCRIBE).
* Holds the WHERE clause as a group graph pattern; query-specific projection/template can be added later.
*/
public sealed interface QueryAst permits AskQueryAst, ConstructQueryAst, DescribeQueryAst, SelectQueryAst {
DatasetClauseAst datasetClause();
GroupGraphPatternAst whereClause();
QueryPrologueAst prologue();
ValuesAst valuesClause();
}
public sealed interface QueryAst permits SparqlQueryAst, UpdateQueryAst {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* {@link #prologue()} captures PREFIX/BASE for SELECT;
* for {@link QueryAst} compatibility.
*/
public record SelectQueryAst(ProjectionAst projection, DatasetClauseAst datasetClause, GroupGraphPatternAst whereClause, SolutionModifierAst solutionModifier, QueryPrologueAst prologue, ValuesAst valuesClause) implements QueryAst {
public record SelectQueryAst(ProjectionAst projection, DatasetClauseAst datasetClause, GroupGraphPatternAst whereClause, SolutionModifierAst solutionModifier, QueryPrologueAst prologue, ValuesAst valuesClause) implements SparqlQueryAst {

/** Constructor with default projection SELECT *. */
public SelectQueryAst(GroupGraphPatternAst whereClause) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fr.inria.corese.core.next.query.impl.sparql.ast;

/**
* Minimal SPARQL query AST (SELECT, ASK, CONSTRUCT, DESCRIBE).
* Holds the WHERE clause as a group graph pattern; query-specific projection/template can be added later.
*/
public sealed interface SparqlQueryAst extends QueryAst, PrologueQueryAst, WhereClauseQueryAst permits AskQueryAst, ConstructQueryAst, DescribeQueryAst, SelectQueryAst {
DatasetClauseAst datasetClause();
ValuesAst valuesClause();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.inria.corese.core.next.query.impl.sparql.ast;

/**
* Root interface for all queries related to the SPARQL Update queries listed in <a href="https://www.w3.org/TR/sparql11-update/">SPARQL 1.1 Update</>.
*/
public sealed interface UpdateQueryAst extends QueryAst permits LoadQueryAst, ClearQueryAst {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updates can start with BASE and PREFIX, but the update AST does not keep the prologue.

Example:

PREFIX ex: <http://example.org/>
LOAD ex:data

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Test
void updateShouldKeepPrologue() {
    QueryParser parser = newParserDefault();
    String query = """
            PREFIX ex: <http://example.org/>
            LOAD ex:data
            """;

    QueryAst queryAst = parser.parse(query);
    UpdateRequestAst update = assertInstanceOf(UpdateRequestAst.class, queryAst);

    assertEquals(1, update.operations().size());
    assertTrue(update.prologue().prefixDeclarations().stream()
            .anyMatch(prefixDecl ->
                    prefixDecl.prefix().equals("ex")
                            && prefixDecl.namespace().raw().equals("http://example.org/")));

    assertInstanceOf(LoadQueryAst.class, update.operations().get(0));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fr.inria.corese.core.next.query.impl.sparql.ast;

/**
* Represents the AST elements of a query that has a WHERE clause (SELECT, CONSTRUCT, INSERT, DELETE, etc.)
*/
public sealed interface WhereClauseQueryAst permits SparqlQueryAst {
GroupGraphPatternAst whereClause();
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ void whereClauseAccessor() {
GroupGraphPatternAst where = new GroupGraphPatternAst(List.of(
new BgpAst(List.of(new TriplePatternAst(
new VarAst("s"), new VarAst("p"), new VarAst("o"))))));
QueryAst q = new SelectQueryAst(where);
SparqlQueryAst q = new SelectQueryAst(where);
assertSame(where, q.whereClause());
}

Expand Down Expand Up @@ -426,7 +426,7 @@ void whereClauseAccessor() {
GroupGraphPatternAst where = new GroupGraphPatternAst(List.of(
new BgpAst(List.of(new TriplePatternAst(
new VarAst("s"), new VarAst("p"), new VarAst("o"))))));
QueryAst q = new AskQueryAst(DatasetClauseAst.none(), where);
SparqlQueryAst q = new AskQueryAst(DatasetClauseAst.none(), where);
assertSame(where, q.whereClause());
}

Expand Down Expand Up @@ -503,7 +503,7 @@ void queryAstWithNestedStructures() {
BgpAst bgp = new BgpAst(List.of(
new TriplePatternAst(new VarAst("s"), new IriAst("a"), new VarAst("o"))));
GroupGraphPatternAst where = new GroupGraphPatternAst(List.of(bgp));
QueryAst q = new SelectQueryAst(where);
SparqlQueryAst q = new SelectQueryAst(where);
assertEquals(1, q.whereClause().patterns().size());
assertEquals(1, ((BgpAst) q.whereClause().patterns().get(0)).triples().size());
}
Expand Down
Loading
Loading