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
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,25 @@
import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.ARQ;
import org.apache.jena.sparql.expr.ExprEvalException;
import org.apache.jena.sparql.expr.NodeValue;

public class TripleTermOps {
/**
* Create a triple term.
* Throws {@link ExprEvalException} if the predicate argument is not a URI.
* In strict mode, throw an exception if the triple is not an RDF Triple.
* Throws {@link ExprEvalException} if the arguments do not form an RDF triple.
*/
public static NodeValue fnTriple(NodeValue nv1, NodeValue nv2, NodeValue nv3) {
Node s = nv1.asNode();
if ( ARQ.isStrictMode() ) {
if ( s.isTripleTerm() )
throw new ExprEvalException("triple: Subject is a triple term: "+nv1);
if ( !s.isURI() && !s.isBlank() )
throw new ExprEvalException("triple: Subject is not a URI or blank node: "+nv2);
}
if ( !s.isURI() && !s.isBlank() )
throw new ExprEvalException("triple: Subject is not a URI or blank node: "+nv1);

Node p = nv2.asNode();
if ( ! p.isURI() )
throw new ExprEvalException("triple: Predicate not a URI: "+nv2);
Node o = nv3.asNode();
if ( !o.isURI() && !o.isBlank() && !o.isLiteral() && !o.isTripleTerm() )
throw new ExprEvalException("triple: Object is not an RDF term: "+nv3);
Node t = NodeFactory.createTripleTerm(s, p, o);
return NodeValue.makeNode(t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
import org.junit.jupiter.api.Test;

import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.graph.Node_Marker;
import org.apache.jena.shared.PrefixMapping;
import org.apache.jena.sparql.function.FunctionEnvBase;
import org.apache.jena.sparql.function.library.triple.TripleTermOps;
import org.apache.jena.sparql.sse.SSE;
import org.apache.jena.sparql.util.ExprUtils;
import org.apache.jena.sparql.util.NodeFactoryExtra;
Expand All @@ -52,6 +55,25 @@ public void tripleTerm_Bad1() {
assertThrows(ExprEvalException.class, ()-> eval("triple(:s1, 'bc', :o1)") );
}

@Test
public void tripleTerm_BadSubjectLiteral() {
assertThrows(ExprEvalException.class, ()-> eval("triple('abc', :p1, :o1)") );
}

@Test
public void tripleTerm_BadSubjectTripleTerm() {
assertThrows(ExprEvalException.class, ()-> eval("triple(triple(:s1, :p1, :o1), :p2, :o2)") );
}

@Test
public void tripleTerm_BadObjectExtensionNode() {
NodeValue subject = NodeValue.makeNode(NodeFactory.createURI("urn:s"));
NodeValue predicate = NodeValue.makeNode(NodeFactory.createURI("urn:p"));
NodeValue object = NodeValue.makeNode(Node_Marker.marker("object"));

assertThrows(ExprEvalException.class, ()-> TripleTermOps.fnTriple(subject, predicate, object));
}

@Test
public void tripleTerm_Access1() {
test("subject(triple(:s1, :p1, :o1))", ":s1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.query.ARQ;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QueryParseException;
Expand Down Expand Up @@ -407,22 +406,9 @@ public class TestExpressions
// Not symmetric RDF
@Test public void tripleterm_10() { assertThrows(ExprEvalException.class, ()->testEval("TRIPLE(<x:s>, BNODE(), <x:o>)")); }

// TRIPLE generates symmetric RDF (non-strict)
@Test public void tripleterm_20() { testEval("TRIPLE(123, <x:p>, <x:o>)"); }
@Test public void tripleterm_21() { testEval("TRIPLE(TRIPLE(<x:s>, <x:p>, <x:o>), <x:p>, <x:o>)"); }

// TRIPLE generates RDF triples (strict)
@Test public void tripleterm_30() {
strictMode(()->assertThrows(ExprEvalException.class, ()-> {
strictMode(()->testEval("TRIPLE(123, <x:p>, <x:o>)"));
}));
}

@Test public void tripleterm_31() {
strictMode(()->assertThrows(ExprEvalException.class, ()-> {
strictMode(()->testEval("TRIPLE(TRIPLE(<x:s>, <x:p>, <x:o>), <x:p>, <x:o>)"));
}));
}
// TRIPLE only generates RDF triples.
@Test public void tripleterm_20() { assertThrows(ExprEvalException.class, ()->testEval("TRIPLE(123, <x:p>, <x:o>)")); }
@Test public void tripleterm_21() { assertThrows(ExprEvalException.class, ()->testEval("TRIPLE(TRIPLE(<x:s>, <x:p>, <x:o>), <x:p>, <x:o>)")); }

// Accessors
@Test public void tripleterm_50() { testURI("SUBJECT( TRIPLE(<x:s>, <x:p>, 123) )", "x:s"); }
Expand Down Expand Up @@ -532,17 +518,6 @@ private static void testSyntax(String exprString) {
parseToEnd(exprString);
}

// Strict mode - just set the flag.
private static void strictMode(Runnable action) {
Object setting = ARQ.getContext().get(ARQ.strictSPARQL);
try {
ARQ.getContext().set(ARQ.strictSPARQL, true);
action.run();
} finally {
ARQ.getContext().set(ARQ.strictSPARQL, setting);
}
}

// "should evaluate", don't care what the result is.
private static void testEval(String string) {
Expr expr = parseToEnd(string);
Expand Down