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 @@ -2,6 +2,7 @@

import fr.inria.corese.core.next.query.kgram.api.core.Edge;
import fr.inria.corese.core.next.query.kgram.api.core.Node;
import fr.inria.corese.core.next.query.kgram.tool.KgramNodes;

import java.util.Objects;

Expand All @@ -12,7 +13,8 @@
final class AstBackedEdge implements Edge {

private final Node subject;
private final Node predicate;
private final Node edgeNode;
private final Node edgeVariable;
private final Node object;
private final Node graph;

Expand All @@ -22,7 +24,14 @@ final class AstBackedEdge implements Edge {

AstBackedEdge(Node subject, Node predicate, Node object, Node graph) {
this.subject = Objects.requireNonNull(subject, "subject");
this.predicate = Objects.requireNonNull(predicate, "predicate");
Node checkedPredicate = Objects.requireNonNull(predicate, "predicate");
if (checkedPredicate.isVariable()) {
this.edgeNode = KgramNodes.rootProperty();
this.edgeVariable = checkedPredicate;
} else {
this.edgeNode = checkedPredicate;
this.edgeVariable = null;
}
this.object = Objects.requireNonNull(object, "object");
this.graph = graph;
}
Expand All @@ -39,17 +48,22 @@ public Node getNode(int i) {

@Override
public Node getProperty() {
return predicate;
return edgeVariable == null ? edgeNode : edgeVariable;
}

@Override
public Node getEdgeNode() {
return edgeNode;
}

@Override
public Node getEdgeVariable() {
return predicate.isVariable() ? predicate : null;
return edgeVariable;
}

@Override
public String getEdgeLabel() {
return predicate.getLabel();
return edgeNode.getLabel();
}

/**
Expand Down Expand Up @@ -82,6 +96,6 @@ public Edge getEdge() {

@Override
public String toString() {
return subject + " " + predicate + " " + object;
return subject + " " + getProperty() + " " + object;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package fr.inria.corese.core.next.query.kgram.tool;

import fr.inria.corese.core.next.query.kgram.api.core.Node;
import fr.inria.corese.core.next.query.kgram.path.Path;
import fr.inria.corese.core.sparql.api.IDatatype;
import fr.inria.corese.core.sparql.datatype.DatatypeMap;

/**
* Factory for Corese-specific KGRAM nodes used by the Corese-next runtime model.
*
* <p>These nodes encode runtime conventions that are not ordinary query terms.
* Keeping them here makes the convention explicit and prevents bridge classes
* from depending on historical parser constants.</p>
*/
public final class KgramNodes {

/**
* Runtime predicate used by KGRAM to represent a variable predicate edge.
*/
public static final String ROOT_PROPERTY_URI = "http://www.inria.fr/acacia/corese#Property";

private KgramNodes() {
}

/**
* Creates the KGRAM root property node.
*
* <p>KGRAM represents a triple pattern such as {@code ?s ?p ?o} with a
* root-property edge node and the predicate variable stored separately as
* the edge variable.</p>
*/
public static Node rootProperty() {
return new RootPropertyNode();
}

private static final class RootPropertyNode implements Node {

private static final IDatatype VALUE = DatatypeMap.newResource(ROOT_PROPERTY_URI);

private int index = -1;
private String key = INITKEY;

@Override
public int getIndex() {
return index;
}

@Override
public void setIndex(int n) {
index = n;
}

@Override
public String getKey() {
return key;
}

@Override
public void setKey(String str) {
key = str;
}

@Override
public boolean same(Node n) {
return n != null && !n.isVariable() && VALUE.sameTerm(n.getDatatypeValue());
}

@Override
public boolean match(Node n) {
return n != null && !n.isVariable() && VALUE.match(n.getDatatypeValue());
}

@Override
public int compare(Node node) {
return VALUE.compareTo(node.getDatatypeValue());
}

@Override
public String getLabel() {
return ROOT_PROPERTY_URI;
}

@Override
public boolean isVariable() {
return false;
}

@Override
public boolean isConstant() {
return true;
}

@Override
public boolean isBlank() {
return false;
}

@Override
public boolean isFuture() {
return false;
}

@Override
public IDatatype getValue() {
return VALUE;
}

@Override
public IDatatype getDatatypeValue() {
return getValue();
}

@Override
public Node getGraph() {
return null;
}

@Override
public Node getNode() {
return this;
}

@Override
public Object getNodeObject() {
return null;
}

@Override
public void setObject(Object o) {
// Root property is an immutable runtime constant; no attached object is stored.
}

@Override
public Path getPath() {
return null;
}

@Override
public String toString() {
return '<' + ROOT_PROPERTY_URI + '>';
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.inria.corese.core.next.query.impl.sparql.bridge;

import fr.inria.corese.core.next.query.kgram.api.core.Node;
import fr.inria.corese.core.next.query.kgram.tool.KgramNodes;
import fr.inria.corese.core.next.query.kgram.tool.NodeImpl;
import fr.inria.corese.core.sparql.triple.parser.Constant;
import fr.inria.corese.core.sparql.triple.parser.Variable;
Expand Down Expand Up @@ -50,17 +51,25 @@ void edgeDoesNotContainOtherNode() {
}

@Test
@DisplayName("getEdgeVariable() exposes the predicate when it is a variable, null otherwise")
@DisplayName("Variable predicate is exposed as edge variable with root property as edge node")
void edgeVariableExposedOnlyForVariablePredicate() {
Node predicateVar = new NodeImpl(Variable.create("p"));
AstBackedEdge withVarPredicate = new AstBackedEdge(
new NodeImpl(Variable.create("s")), predicateVar, new NodeImpl(Variable.create("o")));

assertEquals(KgramNodes.ROOT_PROPERTY_URI, withVarPredicate.getEdgeNode().getLabel());
assertEquals(KgramNodes.ROOT_PROPERTY_URI, withVarPredicate.getEdgeLabel());
assertSame(predicateVar, withVarPredicate.getProperty());
assertSame(predicateVar, withVarPredicate.getEdgeVariable());

Node iriPredicate = new NodeImpl(Constant.createResource("http://example.org/p"));
AstBackedEdge withIriPredicate = new AstBackedEdge(
new NodeImpl(Variable.create("s")),
new NodeImpl(Constant.createResource("http://example.org/p")),
iriPredicate,
new NodeImpl(Variable.create("o")));
assertSame(iriPredicate, withIriPredicate.getEdgeNode());
assertSame(iriPredicate, withIriPredicate.getProperty());
assertEquals("http://example.org/p", withIriPredicate.getEdgeLabel());
assertNull(withIriPredicate.getEdgeVariable());
}

Expand All @@ -87,4 +96,4 @@ void explicitGraphIsReturned() {

assertSame(graph, edge.getGraph());
}
}
}
Loading
Loading