use of org.apache.jena.sparql.syntax.ElementOptional in project jena by apache.
the class WhereClauseTest method testAddOptionalString.
@ContractTest
public void testAddOptionalString() {
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addOptional("<one>", "<two>", "three");
ElementPathBlock epb = new ElementPathBlock();
ElementOptional optional = new ElementOptional(epb);
Triple t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"));
epb.addTriple(t);
WhereValidator visitor = new WhereValidator(optional);
builder.build().getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementOptional in project jena by apache.
the class WhereClauseTest method testAddOptionalStringWithPath.
@ContractTest
public void testAddOptionalStringWithPath() {
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addOptional("<one>", "<two>/<dos>", "three");
Path path = new P_Seq(new P_Link(NodeFactory.createURI("two")), new P_Link(NodeFactory.createURI("dos")));
ElementPathBlock epb = new ElementPathBlock();
ElementOptional optional = new ElementOptional(epb);
TriplePath tp = new TriplePath(NodeFactory.createURI("one"), path, NodeFactory.createLiteral("three"));
epb.addTriplePath(tp);
WhereValidator visitor = new WhereValidator(optional);
builder.build().getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementOptional in project jena by apache.
the class WhereQuadHolder method addOptional.
/**
* Add an optional triple to the where clause
*
* @param t The triple path to add.
* @throws IllegalArgumentException If the triple is not a valid triple for a
* where clause.
*/
public void addOptional(TriplePath t) throws IllegalArgumentException {
testTriple(t);
ElementPathBlock epb = new ElementPathBlock();
epb.addTriple(t);
ElementOptional opt = new ElementOptional(epb);
getClause().addElement(opt);
}
use of org.apache.jena.sparql.syntax.ElementOptional in project jena by apache.
the class WhereClauseTest method testAddOptionalTriplePath.
@ContractTest
public void testAddOptionalTriplePath() {
WhereClause<?> whereClause = getProducer().newInstance();
PrefixMapping pmap = new PrefixMappingImpl();
pmap.setNsPrefix("ts", "urn:test:");
Path path = PathParser.parse("ts:two/ts:dos", pmap);
AbstractQueryBuilder<?> builder = whereClause.addOptional(new TriplePath(NodeFactory.createURI("one"), path, NodeFactory.createURI("three")));
ElementPathBlock epb = new ElementPathBlock();
ElementOptional optional = new ElementOptional(epb);
TriplePath tp = new TriplePath(NodeFactory.createURI("one"), path, NodeFactory.createURI("three"));
epb.addTriplePath(tp);
WhereValidator visitor = new WhereValidator(optional);
builder.build().getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
use of org.apache.jena.sparql.syntax.ElementOptional in project jena by apache.
the class WhereHandlerTest method testAddOptionalWhereHandler.
@Test
public void testAddOptionalWhereHandler() {
WhereHandler pattern = new WhereHandler(new Query());
Var s = Var.alloc("s");
Node q = NodeFactory.createURI("urn:q");
Node v = NodeFactory.createURI("urn:v");
Var x = Var.alloc("x");
Node n123 = NodeFactory.createLiteral(LiteralLabelFactory.createTypedLiteral(123));
pattern.addWhere(new TriplePath(new Triple(s, q, n123)));
pattern.addWhere(new TriplePath(new Triple(s, v, x)));
handler.addOptional(pattern);
handler.build();
ElementPathBlock epb = new ElementPathBlock();
ElementOptional optional = new ElementOptional(epb);
TriplePath tp = new TriplePath(new Triple(s, q, n123));
epb.addTriplePath(tp);
tp = new TriplePath(new Triple(s, v, x));
epb.addTriplePath(tp);
WhereValidator visitor = new WhereValidator(optional);
handler.getQueryPattern().visit(visitor);
assertTrue(visitor.matching);
}
Aggregations