Search in sources :

Example 16 with ElementOptional

use of org.apache.jena.sparql.syntax.ElementOptional in project jena by apache.

the class WhereHandlerTest method testAddOptionalStrings.

@Test
public void testAddOptionalStrings() {
    handler.addOptional(new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three"))));
    handler.build();
    TriplePath tp = new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three")));
    ElementPathBlock epb = new ElementPathBlock();
    epb.addTriple(tp);
    ElementOptional opt = new ElementOptional(epb);
    WhereValidator wv = new WhereValidator(opt);
    query.getQueryPattern().visit(wv);
    assertTrue(wv.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) ElementOptional(org.apache.jena.sparql.syntax.ElementOptional) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) TriplePath(org.apache.jena.sparql.core.TriplePath) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 17 with ElementOptional

use of org.apache.jena.sparql.syntax.ElementOptional in project jena by apache.

the class WhereHandlerTest method testAddOptionalObjectsWithPath.

@Test
public void testAddOptionalObjectsWithPath() {
    PrefixMapping pmap = new PrefixMappingImpl();
    pmap.setNsPrefix("ts", "urn:test:");
    Path path = PathParser.parse("ts:two/ts:dos", pmap);
    handler.addOptional(new TriplePath(NodeFactory.createURI("one"), path, ResourceFactory.createLangLiteral("three", "en-US").asNode()));
    handler.build();
    ElementPathBlock epb = new ElementPathBlock();
    ElementOptional optional = new ElementOptional(epb);
    TriplePath tp = new TriplePath(NodeFactory.createURI("one"), path, ResourceFactory.createLangLiteral("three", "en-US").asNode());
    epb.addTriplePath(tp);
    WhereValidator visitor = new WhereValidator(optional);
    handler.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : TriplePath(org.apache.jena.sparql.core.TriplePath) Path(org.apache.jena.sparql.path.Path) PrefixMapping(org.apache.jena.shared.PrefixMapping) ElementOptional(org.apache.jena.sparql.syntax.ElementOptional) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) TriplePath(org.apache.jena.sparql.core.TriplePath) PrefixMappingImpl(org.apache.jena.shared.impl.PrefixMappingImpl) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 18 with ElementOptional

use of org.apache.jena.sparql.syntax.ElementOptional in project jena by apache.

the class WhereHandlerTest method testSetVarsInOptional.

@Test
public void testSetVarsInOptional() {
    Var v = Var.alloc("v");
    handler.addOptional(new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), v)));
    handler.build();
    TriplePath tp = new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), v));
    ElementPathBlock epb = new ElementPathBlock();
    epb.addTriple(tp);
    ElementOptional opt = new ElementOptional(epb);
    WhereValidator wv = new WhereValidator(opt);
    query.getQueryPattern().visit(wv);
    assertTrue(wv.matching);
    Map<Var, Node> values = new HashMap<>();
    values.put(v, NodeFactory.createURI("three"));
    handler.setVars(values);
    handler.build();
    tp = new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three")));
    epb = new ElementPathBlock();
    epb.addTriple(tp);
    opt = new ElementOptional(epb);
    wv = new WhereValidator(opt);
    query.getQueryPattern().visit(wv);
    assertTrue(wv.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) ElementOptional(org.apache.jena.sparql.syntax.ElementOptional) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Var(org.apache.jena.sparql.core.Var) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) Node(org.apache.jena.graph.Node) TriplePath(org.apache.jena.sparql.core.TriplePath) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 19 with ElementOptional

use of org.apache.jena.sparql.syntax.ElementOptional in project jena by apache.

the class ElementRewriter method visit.

@Override
public void visit(ElementOptional el) {
    el.getOptionalElement().visit(this);
    push(new ElementOptional(getResult()));
}
Also used : ElementOptional(org.apache.jena.sparql.syntax.ElementOptional)

Aggregations

ElementOptional (org.apache.jena.sparql.syntax.ElementOptional)19 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)18 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)16 TriplePath (org.apache.jena.sparql.core.TriplePath)13 Triple (org.apache.jena.graph.Triple)12 ContractTest (org.xenei.junit.contract.ContractTest)10 FrontsTriple (org.apache.jena.graph.FrontsTriple)7 Test (org.junit.Test)6 Node (org.apache.jena.graph.Node)5 Var (org.apache.jena.sparql.core.Var)5 Path (org.apache.jena.sparql.path.Path)4 PrefixMapping (org.apache.jena.shared.PrefixMapping)3 PrefixMappingImpl (org.apache.jena.shared.impl.PrefixMappingImpl)3 ExprVar (org.apache.jena.sparql.expr.ExprVar)3 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)2 P_Link (org.apache.jena.sparql.path.P_Link)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Query (org.apache.jena.query.Query)1 P_Seq (org.apache.jena.sparql.path.P_Seq)1