Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ public Query toNextQuery(DescribeQueryAst describeQueryAst) {
describeQueryAst.valuesClause(),
compiler);
applyOrderBy(query, describeQueryAst.solutionModifier(), compiler);
List<Node> describedNodes = describeNodes(query, describeQueryAst, compiler);
lowerDescribeToConstructQuery(query, describedNodes);
DescribeQueryCompiler.compile(query, describeQueryAst, compiler);
query.setAST(describeQueryAst);
return query;
}
Expand Down Expand Up @@ -365,78 +364,6 @@ private Node groupByNode(Query query, String name) {
return null;
}

/**
* Resolves the resources of a {@code DESCRIBE} against the compiled body.
*
* <p>{@code DESCRIBE *} reuses the in-scope nodes of the body (like {@code SELECT *}).
* A described variable reuses its runtime node (so it is the one bound by the body) and
* fails fast when it is not visible; a described IRI becomes a fresh constant node.</p>
*/
private List<Node> describeNodes(
Query query, DescribeQueryAst describeQueryAst, WhereCompiler compiler) {
if (describeQueryAst.isDescribeAll()) {
return query.selectNodesFromPattern();
}
List<Node> nodes = new ArrayList<>();
for (TermAst term : describeQueryAst.described()) {
if (term instanceof VarAst(String name)) {
Node node = visibleBodyNode(query, name);
if (node == null) {
throw new IllegalArgumentException(
"DESCRIBE variable ?" + name + " is not visible in the compiled query body");
}
nodes.add(node);
} else {
nodes.add(compiler.termResolver().toNode(term));
}
}
return nodes;
}

/**
* Lowers {@code DESCRIBE} to the construct-like shape expected by the current
* KGRAM-next runtime.
*
* <p>This keeps the current KGRAM contract, inherited from the historical pipeline:
* {@code DESCRIBE} is executed through the construct runtime path.</p>
*/
private void lowerDescribeToConstructQuery(Query query, List<Node> describedNodes) {
Exp constructTemplate = Exp.create(Type.BGP);
int syntheticIndex = 0;
for (Node describedNode : describedNodes) {
DescribePattern describePattern = describePattern(describedNode, syntheticIndex++);
// KGRAM-next currently represents DESCRIBE with outgoing and incoming construct triples.
constructTemplate.add(describePattern.outgoing().getEdge());
constructTemplate.add(describePattern.incoming().getEdge());
query.getBody().add(Exp.create(Type.OPTIONAL, Exp.create(Type.AND), describePattern.optionalBody()));
}
query.setConstruct(constructTemplate);
query.setConstruct(true);
query.setConstructNodes(constructTemplate.getNodes());
}

private DescribePattern describePattern(Node describedNode, int index) {
Node outgoingPredicate = createSyntheticDescribeNode("p", index, 0);
Node outgoingValue = createSyntheticDescribeNode("v", index, 0);
Node incomingPredicate = createSyntheticDescribeNode("p", index, 1);
Node incomingValue = createSyntheticDescribeNode("v", index, 1);

Exp outgoing = Exp.create(Type.EDGE, new AstBackedEdge(describedNode, outgoingPredicate, outgoingValue));
Exp incoming = Exp.create(Type.EDGE, new AstBackedEdge(incomingValue, incomingPredicate, describedNode));
Exp outgoingBgp = Exp.create(Type.BGP);
outgoingBgp.add(outgoing);
Exp incomingBgp = Exp.create(Type.BGP);
incomingBgp.add(incoming);
return new DescribePattern(outgoing, incoming, Exp.create(Type.UNION, outgoingBgp, incomingBgp));
}

private Node createSyntheticDescribeNode(String role, int describedIndex, int directionIndex) {
return NodeImpl.forVariable("__describe_" + role + "_" + describedIndex + "_" + directionIndex);
}

private record DescribePattern(Exp outgoing, Exp incoming, Exp optionalBody) {
}

/**
* Maps {@code ORDER BY} conditions that are already expressible in runtime KGRAM terms.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package fr.inria.corese.core.next.query.impl.sparql.bridge;

import fr.inria.corese.core.next.query.impl.engine.model.ExpType.Type;
import fr.inria.corese.core.next.query.impl.engine.model.Node;
import fr.inria.corese.core.next.query.impl.engine.model.NodeImpl;
import fr.inria.corese.core.next.query.impl.engine.pattern.Exp;
import fr.inria.corese.core.next.query.impl.engine.pattern.Query;
import fr.inria.corese.core.next.query.impl.sparql.ast.DescribeQueryAst;
import fr.inria.corese.core.next.query.impl.sparql.ast.TermAst;
import fr.inria.corese.core.next.query.impl.sparql.ast.VarAst;

import java.util.ArrayList;
import java.util.List;

/** Compiles DESCRIBE resources into incoming and outgoing graph construction patterns. */
final class DescribeQueryCompiler {
private DescribeQueryCompiler() {
}

/**
* Adds the DESCRIBE patterns to an initialized query shell.
*
* @param query query with its body, dataset and modifiers already compiled
* @param description resources requested by DESCRIBE
* @param compiler compiler carrying the query prologue
* @throws IllegalArgumentException if a described variable is not visible
*/
static void compile(Query query, DescribeQueryAst description, WhereCompiler compiler) {
lowerDescribeToConstructQuery(query, describeNodes(query, description, compiler));
}

/**
* Resolves the resources of a {@code DESCRIBE} against the compiled body.
*
* <p>{@code DESCRIBE *} reuses the in-scope nodes of the body (like {@code SELECT *}).
* A described variable reuses its runtime node (so it is the one bound by the body) and
* fails fast when it is not visible; a described IRI becomes a fresh constant node.</p>
*/
private static List<Node> describeNodes(
Query query, DescribeQueryAst describeQueryAst, WhereCompiler compiler) {
if (describeQueryAst.isDescribeAll()) {
return query.selectNodesFromPattern();
}
List<Node> nodes = new ArrayList<>();
for (TermAst term : describeQueryAst.described()) {
if (term instanceof VarAst(String name)) {
Node node = query.selectNodesFromPattern().stream()
.filter(candidate -> candidate.getLabel().equals(name))
.findFirst().orElse(null);
if (node == null) {
throw new IllegalArgumentException(
"DESCRIBE variable ?" + name + " is not visible in the compiled query body");
}
nodes.add(node);
} else {
nodes.add(compiler.termResolver().toNode(term));
}
}
return nodes;
}

/**
* Lowers {@code DESCRIBE} to the construct-like shape expected by the current
* KGRAM-next runtime.
*
* <p>This keeps the current KGRAM contract, inherited from the historical pipeline:
* {@code DESCRIBE} is executed through the construct runtime path.</p>
*/
private static void lowerDescribeToConstructQuery(Query query, List<Node> describedNodes) {
Exp constructTemplate = Exp.create(Type.BGP);
int syntheticIndex = 0;
for (Node describedNode : describedNodes) {
DescribePattern describePattern = describePattern(describedNode, syntheticIndex++);
// KGRAM-next currently represents DESCRIBE with outgoing and incoming construct triples.
constructTemplate.add(describePattern.outgoing().getEdge());
constructTemplate.add(describePattern.incoming().getEdge());
query.getBody().add(Exp.create(Type.OPTIONAL, Exp.create(Type.AND), describePattern.optionalBody()));
}
query.setConstruct(constructTemplate);
query.setConstruct(true);
query.setConstructNodes(constructTemplate.getNodes());
}

private static DescribePattern describePattern(Node describedNode, int index) {
Node outgoingPredicate = createSyntheticDescribeNode("p", index, 0);
Node outgoingValue = createSyntheticDescribeNode("v", index, 0);
Node incomingPredicate = createSyntheticDescribeNode("p", index, 1);
Node incomingValue = createSyntheticDescribeNode("v", index, 1);

Exp outgoing = Exp.create(Type.EDGE, new AstBackedEdge(describedNode, outgoingPredicate, outgoingValue));
Exp incoming = Exp.create(Type.EDGE, new AstBackedEdge(incomingValue, incomingPredicate, describedNode));
Exp outgoingBgp = Exp.create(Type.BGP);
outgoingBgp.add(outgoing);
Exp incomingBgp = Exp.create(Type.BGP);
incomingBgp.add(incoming);
return new DescribePattern(outgoing, incoming, Exp.create(Type.UNION, outgoingBgp, incomingBgp));
}

private static Node createSyntheticDescribeNode(String role, int describedIndex, int directionIndex) {
return NodeImpl.forVariable("__describe_" + role + "_" + describedIndex + "_" + directionIndex);
}

private record DescribePattern(Exp outgoing, Exp incoming, Exp optionalBody) {
}

}
Loading
Loading