Search in sources :

Example 66 with WhereValidator

use of org.apache.jena.arq.querybuilder.WhereValidator in project jena by apache.

the class WhereClauseTest method testSetVarsInUnion.

@ContractTest
public void testSetVarsInUnion() {
    Var v = Var.alloc("v");
    SelectBuilder sb1 = new SelectBuilder().addPrefix("pfx", "uri").addWhere("<one>", "<two>", v);
    WhereClause<?> whereClause = getProducer().newInstance();
    whereClause.addUnion(sb1);
    SelectBuilder sb2 = new SelectBuilder().addWhere("<uno>", "<dos>", "<tres>");
    AbstractQueryBuilder<?> builder = whereClause.addUnion(sb2);
    Query query = builder.build();
    Node one = NodeFactory.createURI("one");
    Node two = NodeFactory.createURI("two");
    Node three = NodeFactory.createURI("three");
    Node uno = NodeFactory.createURI("uno");
    Node dos = NodeFactory.createURI("dos");
    Node tres = NodeFactory.createURI("tres");
    ElementUnion union = new ElementUnion();
    ElementPathBlock epb = new ElementPathBlock();
    Triple t = new Triple(one, two, v.asNode());
    epb.addTriple(t);
    union.addElement(epb);
    ElementPathBlock epb2 = new ElementPathBlock();
    t = new Triple(uno, dos, tres);
    epb2.addTriple(t);
    union.addElement(epb2);
    WhereValidator visitor = new WhereValidator(union);
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
    builder.setVar(v, NodeFactory.createURI("three"));
    query = builder.build();
    union = new ElementUnion();
    epb = new ElementPathBlock();
    t = new Triple(one, two, three);
    epb.addTriple(t);
    union.addElement(epb);
    epb2 = new ElementPathBlock();
    t = new Triple(uno, dos, tres);
    epb2.addTriple(t);
    union.addElement(epb2);
    visitor = new WhereValidator(union);
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) FrontsTriple(org.apache.jena.graph.FrontsTriple) ElementUnion(org.apache.jena.sparql.syntax.ElementUnion) ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) Node(org.apache.jena.graph.Node) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) ContractTest(org.xenei.junit.contract.ContractTest)

Example 67 with WhereValidator

use of org.apache.jena.arq.querybuilder.WhereValidator in project jena by apache.

the class WhereClauseTest method testAddWhereValueVars_Node_Variable.

@ContractTest
public void testAddWhereValueVars_Node_Variable() {
    Map<Object, List<?>> map = new HashMap<Object, List<?>>();
    map.put(NodeFactory.createVariable("v"), Arrays.asList("<one>", "<two>"));
    map.put("?x", Arrays.asList("three", "four"));
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addWhereValueVars(map);
    Query query = builder.build();
    Var x = Var.alloc("x");
    Var v = Var.alloc("v");
    ElementData edat = new ElementData();
    edat.add(x);
    edat.add(v);
    setupBindings(edat, x, v);
    WhereValidator visitor = new WhereValidator(edat);
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) HashMap(java.util.HashMap) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) List(java.util.List) ElementData(org.apache.jena.sparql.syntax.ElementData) ContractTest(org.xenei.junit.contract.ContractTest)

Example 68 with WhereValidator

use of org.apache.jena.arq.querybuilder.WhereValidator in project jena by apache.

the class WhereClauseTest method testAddWhereValueVars_InSubQuery.

@ContractTest
public void testAddWhereValueVars_InSubQuery() {
    final Var v = Var.alloc("v");
    Map<Object, List<?>> map = new HashMap<Object, List<?>>();
    map.put(Var.alloc("v"), Arrays.asList("<one>", "<two>"));
    map.put("?x", Arrays.asList("three", "four"));
    WhereClause<?> whereClause = getProducer().newInstance();
    WhereClause<?> whereClause2 = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addWhereValueVars(map);
    builder = whereClause2.addSubQuery(builder);
    Query query = builder.build();
    Var x = Var.alloc("x");
    ElementData edat = new ElementData();
    edat.add(x);
    edat.add(v);
    setupBindings(edat, x, v);
    WhereValidator visitor = new WhereValidator(edat);
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) HashMap(java.util.HashMap) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) List(java.util.List) ElementData(org.apache.jena.sparql.syntax.ElementData) ContractTest(org.xenei.junit.contract.ContractTest)

Example 69 with WhereValidator

use of org.apache.jena.arq.querybuilder.WhereValidator in project jena by apache.

the class WhereClauseTest method testBindExprVar_Node_Variable.

@ContractTest
public void testBindExprVar_Node_Variable() {
    Node v = NodeFactory.createVariable("foo");
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addBind(new E_Random(), v);
    Query query = builder.build();
    WhereValidator visitor = new WhereValidator(new ElementBind(Var.alloc("foo"), new E_Random()));
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
    builder.setVar(v, NodeFactory.createURI("three"));
    query = builder.build();
    visitor = new WhereValidator(new ElementTriplesBlock());
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) E_Random(org.apache.jena.sparql.expr.E_Random) ElementBind(org.apache.jena.sparql.syntax.ElementBind) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) Node(org.apache.jena.graph.Node) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ContractTest(org.xenei.junit.contract.ContractTest)

Example 70 with WhereValidator

use of org.apache.jena.arq.querybuilder.WhereValidator in project jena by apache.

the class WhereClauseTest method testAddWhereValueRow_collection_Node_Variable.

@ContractTest
public void testAddWhereValueRow_collection_Node_Variable() {
    WhereClause<?> whereClause = getProducer().newInstance();
    whereClause = (WhereClause<?>) whereClause.addWhereValueVar(NodeFactory.createVariable("v"));
    whereClause = (WhereClause<?>) whereClause.addWhereValueVar(NodeFactory.createVariable("x"));
    whereClause = (WhereClause<?>) whereClause.addWhereValueRow(Arrays.asList("<one>", "three"));
    AbstractQueryBuilder<?> builder = whereClause.addWhereValueRow(Arrays.asList("<two>", "four"));
    Query query = builder.build();
    ElementData edat = new ElementData();
    final Var v = Var.alloc("v");
    final Var x = Var.alloc("x");
    edat.add(v);
    edat.add(x);
    setupBindings(edat, x, v);
    WhereValidator visitor = new WhereValidator(edat);
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) ElementData(org.apache.jena.sparql.syntax.ElementData) ContractTest(org.xenei.junit.contract.ContractTest)

Aggregations

WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)90 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)60 Triple (org.apache.jena.graph.Triple)54 TriplePath (org.apache.jena.sparql.core.TriplePath)49 Test (org.junit.Test)47 Query (org.apache.jena.query.Query)42 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)42 ContractTest (org.xenei.junit.contract.ContractTest)42 Var (org.apache.jena.sparql.core.Var)34 Node (org.apache.jena.graph.Node)30 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)22 FrontsTriple (org.apache.jena.graph.FrontsTriple)22 ExprVar (org.apache.jena.sparql.expr.ExprVar)21 ElementData (org.apache.jena.sparql.syntax.ElementData)18 ElementOptional (org.apache.jena.sparql.syntax.ElementOptional)16 HashMap (java.util.HashMap)10 ElementUnion (org.apache.jena.sparql.syntax.ElementUnion)8 LinkedHashMap (java.util.LinkedHashMap)7 Binding (org.apache.jena.sparql.engine.binding.Binding)6 E_Random (org.apache.jena.sparql.expr.E_Random)6