Search in sources :

Example 11 with ListBindingSet

use of org.openrdf.query.impl.ListBindingSet in project incubator-rya by apache.

the class PipelineQueryIT method testRequiredDerivationLevel.

@Test
public void testRequiredDerivationLevel() throws Exception {
    // Insert data
    URI person = VF.createURI("urn:Person");
    URI livingThing = VF.createURI("urn:LivingThing");
    URI human = VF.createURI("urn:Human");
    URI programmer = VF.createURI("urn:Programmer");
    URI thing = VF.createURI("urn:Thing");
    insert(programmer, RDFS.SUBCLASSOF, person);
    insert(person, RDFS.SUBCLASSOF, FOAF.PERSON);
    insert(FOAF.PERSON, RDFS.SUBCLASSOF, person);
    insert(person, OWL.EQUIVALENTCLASS, human);
    insert(person, RDFS.SUBCLASSOF, livingThing);
    insert(livingThing, RDFS.SUBCLASSOF, thing);
    insert(thing, RDFS.SUBCLASSOF, OWL.THING, 1);
    insert(OWL.THING, RDFS.SUBCLASSOF, thing);
    dao.flush();
    // Define query and expected results
    final String query = "SELECT ?A ?B WHERE {\n" + "  ?A rdfs:subClassOf ?B .\n" + "  ?B rdfs:subClassOf ?A .\n" + "}";
    List<String> varNames = Arrays.asList("A", "B");
    Multiset<BindingSet> expectedSolutions = HashMultiset.create();
    expectedSolutions.add(new ListBindingSet(varNames, person, FOAF.PERSON));
    expectedSolutions.add(new ListBindingSet(varNames, FOAF.PERSON, person));
    expectedSolutions.add(new ListBindingSet(varNames, thing, OWL.THING));
    expectedSolutions.add(new ListBindingSet(varNames, OWL.THING, thing));
    // Prepare query and convert to pipeline
    QueryRoot queryTree = new QueryRoot(PARSER.parseQuery(query, null).getTupleExpr());
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(getRyaCollection());
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) queryTree.getArg();
    // Extend the pipeline by requiring a derivation level of zero (should have no effect)
    pipelineNode.requireSourceDerivationDepth(0);
    Multiset<BindingSet> solutions = HashMultiset.create();
    CloseableIteration<BindingSet, QueryEvaluationException> iter = pipelineNode.evaluate(new QueryBindingSet());
    while (iter.hasNext()) {
        solutions.add(iter.next());
    }
    Assert.assertEquals(expectedSolutions, solutions);
    // Extend the pipeline by requiring a derivation level of one (should produce the thing/thing pair)
    expectedSolutions = HashMultiset.create();
    expectedSolutions.add(new ListBindingSet(varNames, thing, OWL.THING));
    expectedSolutions.add(new ListBindingSet(varNames, OWL.THING, thing));
    pipelineNode.requireSourceDerivationDepth(1);
    solutions = HashMultiset.create();
    iter = pipelineNode.evaluate(new QueryBindingSet());
    while (iter.hasNext()) {
        solutions.add(iter.next());
    }
    Assert.assertEquals(expectedSolutions, solutions);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) QueryRoot(org.openrdf.query.algebra.QueryRoot) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) URI(org.openrdf.model.URI) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Example 12 with ListBindingSet

use of org.openrdf.query.impl.ListBindingSet in project incubator-rya by apache.

the class PipelineQueryIT method testJoinTwoSharedVariables.

@Test
public void testJoinTwoSharedVariables() throws Exception {
    // Insert data
    URI person = VF.createURI("urn:Person");
    URI livingThing = VF.createURI("urn:LivingThing");
    URI human = VF.createURI("urn:Human");
    URI programmer = VF.createURI("urn:Programmer");
    URI thing = VF.createURI("urn:Thing");
    insert(programmer, RDFS.SUBCLASSOF, person);
    insert(person, RDFS.SUBCLASSOF, FOAF.PERSON);
    insert(FOAF.PERSON, RDFS.SUBCLASSOF, person);
    insert(person, OWL.EQUIVALENTCLASS, human);
    insert(person, RDFS.SUBCLASSOF, livingThing);
    insert(livingThing, RDFS.SUBCLASSOF, thing);
    insert(thing, RDFS.SUBCLASSOF, OWL.THING);
    insert(OWL.THING, RDFS.SUBCLASSOF, thing);
    dao.flush();
    // Define query and expected results
    final String query = "SELECT ?A ?B WHERE {\n" + "  ?A rdfs:subClassOf ?B .\n" + "  ?B rdfs:subClassOf ?A .\n" + "}";
    List<String> varNames = Arrays.asList("A", "B");
    Multiset<BindingSet> expectedSolutions = HashMultiset.create();
    expectedSolutions.add(new ListBindingSet(varNames, person, FOAF.PERSON));
    expectedSolutions.add(new ListBindingSet(varNames, FOAF.PERSON, person));
    expectedSolutions.add(new ListBindingSet(varNames, thing, OWL.THING));
    expectedSolutions.add(new ListBindingSet(varNames, OWL.THING, thing));
    // Execute query and verify results
    testPipelineQuery(query, expectedSolutions);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 13 with ListBindingSet

use of org.openrdf.query.impl.ListBindingSet in project incubator-rya by apache.

the class PipelineQueryIT method testVariableRename.

@Test
public void testVariableRename() throws Exception {
    // Insert data
    URI alice = VF.createURI("urn:Alice");
    URI bob = VF.createURI("urn:Bob");
    URI carol = VF.createURI("urn:Carol");
    URI dan = VF.createURI("urn:Dan");
    URI eve = VF.createURI("urn:Eve");
    URI friend = VF.createURI("urn:friend");
    insert(alice, friend, bob);
    insert(alice, friend, carol);
    insert(bob, friend, eve);
    insert(carol, friend, eve);
    insert(dan, friend, carol);
    insert(eve, friend, alice);
    // Define non-distinct query and expected results
    final String query1 = "SELECT ?x (?z as ?friendOfFriend) WHERE {\n" + "  ?x <urn:friend> ?y .\n" + "  ?y <urn:friend> ?z .\n" + "}";
    Multiset<BindingSet> expectedSolutions1 = HashMultiset.create();
    List<String> varNames = Arrays.asList("x", "friendOfFriend");
    expectedSolutions1.add(new ListBindingSet(varNames, alice, eve));
    expectedSolutions1.add(new ListBindingSet(varNames, alice, eve));
    expectedSolutions1.add(new ListBindingSet(varNames, bob, alice));
    expectedSolutions1.add(new ListBindingSet(varNames, carol, alice));
    expectedSolutions1.add(new ListBindingSet(varNames, dan, eve));
    expectedSolutions1.add(new ListBindingSet(varNames, eve, bob));
    expectedSolutions1.add(new ListBindingSet(varNames, eve, carol));
    // Define distinct query and expected results
    final String query2 = "SELECT DISTINCT ?x (?z as ?friendOfFriend) WHERE {\n" + "  ?x <urn:friend> ?y .\n" + "  ?y <urn:friend> ?z .\n" + "}";
    Multiset<BindingSet> expectedSolutions2 = HashMultiset.create();
    expectedSolutions2.add(new ListBindingSet(varNames, alice, eve));
    expectedSolutions2.add(new ListBindingSet(varNames, bob, alice));
    expectedSolutions2.add(new ListBindingSet(varNames, carol, alice));
    expectedSolutions2.add(new ListBindingSet(varNames, dan, eve));
    expectedSolutions2.add(new ListBindingSet(varNames, eve, bob));
    expectedSolutions2.add(new ListBindingSet(varNames, eve, carol));
    // Execute and verify results
    testPipelineQuery(query1, expectedSolutions1);
    testPipelineQuery(query2, expectedSolutions2);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 14 with ListBindingSet

use of org.openrdf.query.impl.ListBindingSet in project incubator-rya by apache.

the class PipelineQueryIT method testSingleStatementPattern.

@Test
public void testSingleStatementPattern() throws Exception {
    // Insert data
    insert(OWL.THING, RDF.TYPE, OWL.CLASS);
    insert(FOAF.PERSON, RDF.TYPE, OWL.CLASS, 1);
    insert(FOAF.PERSON, RDFS.SUBCLASSOF, OWL.THING);
    insert(VF.createURI("urn:Alice"), RDF.TYPE, FOAF.PERSON);
    dao.flush();
    // Define query and expected results
    final String query = "SELECT * WHERE {\n" + "  ?individual a ?type .\n" + "}";
    List<String> varNames = Arrays.asList("individual", "type");
    Multiset<BindingSet> expectedSolutions = HashMultiset.create();
    expectedSolutions.add(new ListBindingSet(varNames, OWL.THING, OWL.CLASS));
    expectedSolutions.add(new ListBindingSet(varNames, FOAF.PERSON, OWL.CLASS));
    expectedSolutions.add(new ListBindingSet(varNames, VF.createURI("urn:Alice"), FOAF.PERSON));
    // Execute pipeline and verify results
    testPipelineQuery(query, expectedSolutions);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) Test(org.junit.Test)

Example 15 with ListBindingSet

use of org.openrdf.query.impl.ListBindingSet in project incubator-rya by apache.

the class PipelineQueryIT method testMultiConstruct.

@Test
public void testMultiConstruct() throws Exception {
    // Insert data
    URI alice = VF.createURI("urn:Alice");
    URI bob = VF.createURI("urn:Bob");
    URI eve = VF.createURI("urn:Eve");
    URI friend = VF.createURI("urn:friend");
    URI knows = VF.createURI("urn:knows");
    URI person = VF.createURI("urn:Person");
    insert(alice, friend, bob);
    insert(bob, knows, eve);
    insert(eve, knows, alice);
    // Define query and expected results
    final String query = "CONSTRUCT {\n" + "    ?x rdf:type owl:Thing .\n" + "    ?x rdf:type <urn:Person> .\n" + "} WHERE { ?x <urn:knows> ?y }";
    final Multiset<BindingSet> expected = HashMultiset.create();
    List<String> varNames = Arrays.asList("subject", "predicate", "object");
    expected.add(new ListBindingSet(varNames, bob, RDF.TYPE, OWL.THING));
    expected.add(new ListBindingSet(varNames, bob, RDF.TYPE, person));
    expected.add(new ListBindingSet(varNames, eve, RDF.TYPE, OWL.THING));
    expected.add(new ListBindingSet(varNames, eve, RDF.TYPE, person));
    // Test query
    testPipelineQuery(query, expected);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) URI(org.openrdf.model.URI) Test(org.junit.Test)

Aggregations

BindingSet (org.openrdf.query.BindingSet)16 ListBindingSet (org.openrdf.query.impl.ListBindingSet)16 Test (org.junit.Test)14 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)8 HashSet (java.util.HashSet)7 URI (org.openrdf.model.URI)7 EmptyBindingSet (org.openrdf.query.impl.EmptyBindingSet)7 LinkedList (java.util.LinkedList)5 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)2 QueryRoot (org.openrdf.query.algebra.QueryRoot)2 HashMap (java.util.HashMap)1 StandardizingSPARQLParser (openrdffork.StandardizingSPARQLParser)1 Document (org.bson.Document)1 Value (org.openrdf.model.Value)1 Binding (org.openrdf.query.Binding)1 ParsedQuery (org.openrdf.query.parser.ParsedQuery)1 NoURIException (utility.NoURIException)1