use of org.openrdf.query.impl.ListBindingSet in project incubator-rya by apache.
the class MongoSpinIT method testPipelineStrategy.
@Test
public void testPipelineStrategy() throws Exception {
insertDataFile(Resources.getResource("data.ttl"), "http://example.org#");
insertDataFile(Resources.getResource("university.ttl"), "http://example.org#");
insertDataFile(Resources.getResource("owlrl.ttl"), "http://example.org#");
Set<BindingSet> solutions = executeQuery(Resources.getResource("query.sparql"));
Set<BindingSet> expected = new HashSet<>();
Assert.assertEquals(expected, solutions);
conf.setUseAggregationPipeline(true);
ForwardChainSpinTool tool = new ForwardChainSpinTool();
ToolRunner.run(conf, tool, new String[] {});
solutions = executeQuery(Resources.getResource("query.sparql"));
expected.add(new ListBindingSet(Arrays.asList("X", "Y"), VF.createURI(EX, "Alice"), VF.createURI(EX, "Department1")));
Assert.assertEquals(expected, solutions);
Assert.assertEquals(24, tool.getNumInferences());
}
use of org.openrdf.query.impl.ListBindingSet in project incubator-rya by apache.
the class PipelineResultIterationTest method testIterationGivenBindingSet.
@Test
public void testIterationGivenBindingSet() throws QueryEvaluationException {
BindingSet solution = new ListBindingSet(Arrays.asList("b", "c"), VF.createURI("urn:Bob"), VF.createURI("urn:Charlie"));
HashMap<String, String> nameMap = new HashMap<>();
nameMap.put("bName", "b");
nameMap.put("cName", "c");
nameMap.put("c", "cName");
PipelineResultIteration iter = new PipelineResultIteration(documentIterator(new Document("<VALUES>", new Document("a", "urn:Alice").append("b", "urn:Bob")), new Document("<VALUES>", new Document("a", "urn:Alice").append("b", "urn:Beth")), new Document("<VALUES>", new Document("a", "urn:Alice").append("bName", "urn:Bob")), new Document("<VALUES>", new Document("a", "urn:Alice").append("bName", "urn:Beth")), new Document("<VALUES>", new Document("a", "urn:Alice").append("cName", "urn:Carol")), new Document("<VALUES>", new Document("c", "urn:Carol").append("d", "urn:Dan"))), nameMap, solution);
Assert.assertTrue(iter.hasNext());
BindingSet bs = iter.next();
// Add 'c=Charlie' to first result ('b=Bob' matches)
Assert.assertEquals(Sets.newHashSet("a", "b", "c"), bs.getBindingNames());
Assert.assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
Assert.assertEquals("urn:Bob", bs.getBinding("b").getValue().stringValue());
Assert.assertEquals("urn:Charlie", bs.getBinding("c").getValue().stringValue());
Assert.assertTrue(iter.hasNext());
bs = iter.next();
// Skip second result ('b=Beth' incompatible with 'b=Bob')
// Add 'c=Charlie' to third result ('bName=Bob' resolves to 'b=Bob', matches)
Assert.assertEquals(Sets.newHashSet("a", "b", "c"), bs.getBindingNames());
Assert.assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
Assert.assertEquals("urn:Bob", bs.getBinding("b").getValue().stringValue());
Assert.assertEquals("urn:Charlie", bs.getBinding("c").getValue().stringValue());
Assert.assertTrue(iter.hasNext());
bs = iter.next();
// Skip fourth result ('bName=Beth' resolves to 'b=Beth', incompatible)
// Skip fifth result ('cName=Carol' resolves to 'c=Carol', incompatible with 'c=Charlie')
// Add 'b=Bob' and 'c=Charlie' to sixth result ('c=Carol' resolves to 'cName=Carol', compatible)
Assert.assertEquals(Sets.newHashSet("b", "c", "cName", "d"), bs.getBindingNames());
Assert.assertEquals("urn:Bob", bs.getBinding("b").getValue().stringValue());
Assert.assertEquals("urn:Charlie", bs.getBinding("c").getValue().stringValue());
Assert.assertEquals("urn:Carol", bs.getBinding("cName").getValue().stringValue());
Assert.assertEquals("urn:Dan", bs.getBinding("d").getValue().stringValue());
Assert.assertFalse(iter.hasNext());
}
use of org.openrdf.query.impl.ListBindingSet in project incubator-rya by apache.
the class InferenceIT method testReflexivePropertyQuery.
@Test
public void testReflexivePropertyQuery() throws Exception {
final String ontology = "INSERT DATA { GRAPH <http://updated/test> {\n" + " <urn:hasFamilyMember> a owl:ReflexiveProperty . \n" + "}}";
final String instances = "INSERT DATA { GRAPH <http://updated/test> {\n" + " <urn:Alice> <urn:hasFamilyMember> <urn:Bob> . \n" + " <urn:Alice> <urn:knows> <urn:Eve> . \n" + "}}";
conn.prepareUpdate(QueryLanguage.SPARQL, ontology).execute();
conn.prepareUpdate(QueryLanguage.SPARQL, instances).execute();
inferenceEngine.refreshGraph();
final URI alice = vf.createURI("urn:Alice");
final URI bob = vf.createURI("urn:Bob");
final URI carol = vf.createURI("urn:Carol");
final URI eve = vf.createURI("urn:Eve");
final List<String> varNames = new LinkedList<>();
varNames.add("x");
final Set<BindingSet> aliceAndBob = new HashSet<>();
aliceAndBob.add(new ListBindingSet(varNames, alice));
aliceAndBob.add(new ListBindingSet(varNames, bob));
final Set<BindingSet> carolOnly = new HashSet<>();
carolOnly.add(new ListBindingSet(varNames, carol));
// Queries where subject constant, object variable:
final String aliceFamilyQuery = "SELECT ?x { GRAPH <http://updated/test> { <urn:Alice> <urn:hasFamilyMember> ?x } }";
conn.prepareTupleQuery(QueryLanguage.SPARQL, aliceFamilyQuery).evaluate(resultHandler);
Assert.assertEquals(aliceAndBob, new HashSet<>(solutions));
final String carolFamilyQuery = "SELECT ?x { GRAPH <http://updated/test> { <urn:Carol> <urn:hasFamilyMember> ?x } }";
conn.prepareTupleQuery(QueryLanguage.SPARQL, carolFamilyQuery).evaluate(resultHandler);
Assert.assertEquals(carolOnly, new HashSet<>(solutions));
// Queries where subject variable, object constant:
final String familyOfBobQuery = "SELECT ?x { GRAPH <http://updated/test> { ?x <urn:hasFamilyMember> <urn:Bob> } }";
conn.prepareTupleQuery(QueryLanguage.SPARQL, familyOfBobQuery).evaluate(resultHandler);
Assert.assertEquals(aliceAndBob, new HashSet<>(solutions));
final String familyOfCarolQuery = "SELECT ?x { GRAPH <http://updated/test> { ?x <urn:hasFamilyMember> <urn:Carol> } }";
conn.prepareTupleQuery(QueryLanguage.SPARQL, familyOfCarolQuery).evaluate(resultHandler);
Assert.assertEquals(carolOnly, new HashSet<>(solutions));
varNames.add("y");
// Query where both subject and object are variables, but restricted by
// other statements
final Set<BindingSet> aliceAndBoth = new HashSet<>();
aliceAndBoth.add(new ListBindingSet(varNames, alice, bob));
aliceAndBoth.add(new ListBindingSet(varNames, alice, alice));
final String variableQuery = "SELECT * { GRAPH <http://updated/test> {\n" + " ?x <urn:knows> <urn:Eve> .\n" + " ?x <urn:hasFamilyMember> ?y .\n" + "} }";
conn.prepareTupleQuery(QueryLanguage.SPARQL, variableQuery).evaluate(resultHandler);
Assert.assertEquals(aliceAndBoth, new HashSet<>(solutions));
// Query where subject and object are unrestricted variables: match
// every known node (dangerous, but correct)
final URI hasFamily = vf.createURI("urn:hasFamilyMember");
final URI rp = vf.createURI(OWL.NAMESPACE, "ReflexiveProperty");
final Set<BindingSet> everything = new HashSet<>();
everything.add(new ListBindingSet(varNames, alice, alice));
everything.add(new ListBindingSet(varNames, bob, bob));
everything.add(new ListBindingSet(varNames, alice, bob));
everything.add(new ListBindingSet(varNames, eve, eve));
everything.add(new ListBindingSet(varNames, hasFamily, hasFamily));
everything.add(new ListBindingSet(varNames, rp, rp));
everything.add(new ListBindingSet(varNames, RdfCloudTripleStoreConstants.RTS_SUBJECT, RdfCloudTripleStoreConstants.RTS_SUBJECT));
everything.add(new ListBindingSet(varNames, RdfCloudTripleStoreConstants.VERSION, RdfCloudTripleStoreConstants.VERSION));
final String everythingQuery = "SELECT * { GRAPH <http://updated/test> {\n" + " ?x <urn:hasFamilyMember> ?y .\n" + "} }";
conn.prepareTupleQuery(QueryLanguage.SPARQL, everythingQuery).evaluate(resultHandler);
Assert.assertEquals(everything, new HashSet<>(solutions));
}
use of org.openrdf.query.impl.ListBindingSet in project incubator-rya by apache.
the class InferenceIT method testIntersectionOfQuery.
public void testIntersectionOfQuery() throws Exception {
final String ontology = "INSERT DATA { GRAPH <http://updated/test> {\n" + " <urn:Mother> owl:intersectionOf _:bnode1 . \n" + " _:bnode1 rdf:first <urn:Woman> . \n" + " _:bnode1 rdf:rest _:bnode2 . \n" + " _:bnode2 rdf:first <urn:Parent> . \n" + " _:bnode2 rdf:rest rdf:nil . \n" + " <urn:Father> owl:intersectionOf _:bnode3 . \n" + " _:bnode3 rdf:first <urn:Man> . \n" + " _:bnode3 rdf:rest _:bnode4 . \n" + " _:bnode4 rdf:first <urn:Parent> . \n" + " _:bnode4 rdf:rest rdf:nil . \n" + "}}";
final String instances = "INSERT DATA { GRAPH <http://updated/test> {\n" + " <urn:Susan> a <urn:Mother> . \n" + " <urn:Bob> a <urn:Man> . \n" + " <urn:Bob> a <urn:Parent> . \n" + "}}";
conn.prepareUpdate(QueryLanguage.SPARQL, ontology).execute();
conn.prepareUpdate(QueryLanguage.SPARQL, instances).execute();
inferenceEngine.refreshGraph();
final List<String> varNames = new LinkedList<>();
varNames.add("individual");
// Find all <urn:Mother> types (expect 1 result)
final String motherQuery = "SELECT ?individual { GRAPH <http://updated/test> { ?individual rdf:type <urn:Mother> } } \n";
conn.prepareTupleQuery(QueryLanguage.SPARQL, motherQuery).evaluate(resultHandler);
final Set<BindingSet> expectedMothers = new HashSet<>();
expectedMothers.add(new ListBindingSet(varNames, vf.createURI("urn:Susan")));
Assert.assertEquals(expectedMothers, new HashSet<>(solutions));
// Find all <urn:Father> types (expect 1 result)
final String fatherQuery = "SELECT ?individual { GRAPH <http://updated/test> { ?individual rdf:type <urn:Father> } } \n";
conn.prepareTupleQuery(QueryLanguage.SPARQL, fatherQuery).evaluate(resultHandler);
final Set<BindingSet> expectedFathers = new HashSet<>();
expectedFathers.add(new ListBindingSet(varNames, vf.createURI("urn:Bob")));
Assert.assertEquals(expectedFathers, new HashSet<>(solutions));
// Find all <urn:Parent> types (expect 2 results)
final String parentQuery = "SELECT ?individual { GRAPH <http://updated/test> { ?individual rdf:type <urn:Parent> } } \n";
conn.prepareTupleQuery(QueryLanguage.SPARQL, parentQuery).evaluate(resultHandler);
final Set<BindingSet> expectedParents = new HashSet<>();
expectedParents.add(new ListBindingSet(varNames, vf.createURI("urn:Bob")));
expectedParents.add(new ListBindingSet(varNames, vf.createURI("urn:Susan")));
Assert.assertEquals(expectedParents, new HashSet<>(solutions));
// Find all <urn:Woman> types (expect 1 result)
final String womanQuery = "SELECT ?individual { GRAPH <http://updated/test> { ?individual rdf:type <urn:Woman> } } \n";
conn.prepareTupleQuery(QueryLanguage.SPARQL, womanQuery).evaluate(resultHandler);
final Set<BindingSet> expectedWomen = new HashSet<>();
expectedWomen.add(new ListBindingSet(varNames, vf.createURI("urn:Susan")));
Assert.assertEquals(expectedWomen, new HashSet<>(solutions));
// Find all <urn:Man> types (expect 1 result)
final String manQuery = "SELECT ?individual { GRAPH <http://updated/test> { ?individual rdf:type <urn:Man> } } \n";
conn.prepareTupleQuery(QueryLanguage.SPARQL, manQuery).evaluate(resultHandler);
final Set<BindingSet> expectedMen = new HashSet<>();
expectedMen.add(new ListBindingSet(varNames, vf.createURI("urn:Bob")));
Assert.assertEquals(expectedMen, new HashSet<>(solutions));
}
use of org.openrdf.query.impl.ListBindingSet in project incubator-rya by apache.
the class InferenceIT method testOneOfQuery.
@Test
public void testOneOfQuery() throws Exception {
final String ontology = "INSERT DATA { GRAPH <http://updated/test> {\n" + " <urn:Suits> owl:oneOf _:bnodeS1 . \n" + " _:bnodeS1 rdf:first <urn:Clubs> . \n" + " _:bnodeS1 rdf:rest _:bnodeS2 . \n" + " _:bnodeS2 rdf:first <urn:Diamonds> . \n" + " _:bnodeS2 rdf:rest _:bnodeS3 . \n" + " _:bnodeS3 rdf:first <urn:Hearts> . \n" + " _:bnodeS3 rdf:rest _:bnodeS4 . \n" + " _:bnodeS4 rdf:first <urn:Spades> . \n" + " _:bnodeS4 rdf:rest rdf:nil . \n" + " <urn:Ranks> owl:oneOf _:bnodeR1 . \n" + " _:bnodeR1 rdf:first <urn:Ace> . \n" + " _:bnodeR1 rdf:rest _:bnodeR2 . \n" + " _:bnodeR2 rdf:first <urn:2> . \n" + " _:bnodeR2 rdf:rest _:bnodeR3 . \n" + " _:bnodeR3 rdf:first <urn:3> . \n" + " _:bnodeR3 rdf:rest _:bnodeR4 . \n" + " _:bnodeR4 rdf:first <urn:4> . \n" + " _:bnodeR4 rdf:rest _:bnodeR5 . \n" + " _:bnodeR5 rdf:first <urn:5> . \n" + " _:bnodeR5 rdf:rest _:bnodeR6 . \n" + " _:bnodeR6 rdf:first <urn:6> . \n" + " _:bnodeR6 rdf:rest _:bnodeR7 . \n" + " _:bnodeR7 rdf:first <urn:7> . \n" + " _:bnodeR7 rdf:rest _:bnodeR8 . \n" + " _:bnodeR8 rdf:first <urn:8> . \n" + " _:bnodeR8 rdf:rest _:bnodeR9 . \n" + " _:bnodeR9 rdf:first <urn:9> . \n" + " _:bnodeR9 rdf:rest _:bnodeR10 . \n" + " _:bnodeR10 rdf:first <urn:10> . \n" + " _:bnodeR10 rdf:rest _:bnodeR11 . \n" + " _:bnodeR11 rdf:first <urn:Jack> . \n" + " _:bnodeR11 rdf:rest _:bnodeR12 . \n" + " _:bnodeR12 rdf:first <urn:Queen> . \n" + " _:bnodeR12 rdf:rest _:bnodeR13 . \n" + " _:bnodeR13 rdf:first <urn:King> . \n" + " _:bnodeR13 rdf:rest rdf:nil . \n" + " <urn:Card> owl:intersectionOf (\n" + " [ owl:onProperty <urn:HasRank> ; owl:someValuesFrom <urn:Ranks> ]\n" + " [ owl:onProperty <urn:HasSuit> ; owl:someValuesFrom <urn:Suits> ]\n" + " ) . \n" + " <urn:HasRank> owl:range <urn:Ranks> . \n" + " <urn:HasSuit> owl:range <urn:Suits> . \n" + "}}";
final String instances = "INSERT DATA { GRAPH <http://updated/test> {\n" + " <urn:FlopCard1> a <urn:Card> . \n" + " <urn:FlopCard1> <urn:HasRank> <urn:Ace> . \n" + " <urn:FlopCard1> <urn:HasSuit> <urn:Diamonds> . \n" + " <urn:FlopCard2> a <urn:Card> . \n" + " <urn:FlopCard2> <urn:HasRank> <urn:Ace> . \n" + " <urn:FlopCard2> <urn:HasSuit> <urn:Hearts> . \n" + " <urn:FlopCard3> a <urn:Card> . \n" + " <urn:FlopCard3> <urn:HasRank> <urn:King> . \n" + " <urn:FlopCard3> <urn:HasSuit> <urn:Spades> . \n" + " <urn:TurnCard> a <urn:Card> . \n" + " <urn:TurnCard> <urn:HasRank> <urn:10> . \n" + " <urn:TurnCard> <urn:HasSuit> <urn:Clubs> . \n" + " <urn:RiverCard> a <urn:Card> . \n" + " <urn:RiverCard> <urn:HasRank> <urn:Queen> . \n" + " <urn:RiverCard> <urn:HasSuit> <urn:Hearts> . \n" + "}}";
conn.prepareUpdate(QueryLanguage.SPARQL, ontology).execute();
conn.prepareUpdate(QueryLanguage.SPARQL, instances).execute();
inferenceEngine.refreshGraph();
final List<String> varNames = new LinkedList<>();
varNames.add("card");
// Find all cards with a <urn:Suits> type (expect 5 results)
final String cardSuitQuery = "SELECT ?card { GRAPH <http://updated/test> { ?card a <urn:Card> . ?suit a <urn:Suits> . ?card <urn:HasSuit> ?suit} } \n";
conn.prepareTupleQuery(QueryLanguage.SPARQL, cardSuitQuery).evaluate(resultHandler);
final Set<BindingSet> expectedCardSuits = new HashSet<>();
expectedCardSuits.add(new ListBindingSet(varNames, vf.createURI("urn:FlopCard1")));
expectedCardSuits.add(new ListBindingSet(varNames, vf.createURI("urn:FlopCard2")));
expectedCardSuits.add(new ListBindingSet(varNames, vf.createURI("urn:FlopCard3")));
expectedCardSuits.add(new ListBindingSet(varNames, vf.createURI("urn:TurnCard")));
expectedCardSuits.add(new ListBindingSet(varNames, vf.createURI("urn:RiverCard")));
Assert.assertEquals(expectedCardSuits.size(), solutions.size());
Assert.assertEquals(expectedCardSuits, new HashSet<>(solutions));
// Find all cards with a <urn:Ranks> type (expect 5 results)
final String cardRankQuery = "SELECT ?card { GRAPH <http://updated/test> { ?card a <urn:Card> . ?rank a <urn:Ranks> . ?card <urn:HasRank> ?rank} } \n";
conn.prepareTupleQuery(QueryLanguage.SPARQL, cardRankQuery).evaluate(resultHandler);
final Set<BindingSet> expectedCardRanks = new HashSet<>();
expectedCardRanks.add(new ListBindingSet(varNames, vf.createURI("urn:FlopCard1")));
expectedCardRanks.add(new ListBindingSet(varNames, vf.createURI("urn:FlopCard2")));
expectedCardRanks.add(new ListBindingSet(varNames, vf.createURI("urn:FlopCard3")));
expectedCardRanks.add(new ListBindingSet(varNames, vf.createURI("urn:TurnCard")));
expectedCardRanks.add(new ListBindingSet(varNames, vf.createURI("urn:RiverCard")));
Assert.assertEquals(expectedCardRanks.size(), solutions.size());
Assert.assertEquals(expectedCardRanks, new HashSet<>(solutions));
}
Aggregations