use of org.apache.rya.mongodb.MongoDBRyaDAO in project incubator-rya by apache.
the class MongoStatementMetadataNodeIT method simpleQueryWithBindingSetJoinPropertyToSubject.
/**
* Tests to see if correct result is passed back when a metadata statement
* is joined with a StatementPattern statement (i.e. a common variable
* appears in a StatementPattern statement and a metadata statement).
* StatementPattern statements have either rdf:subject, rdf:predicate, or
* rdf:object as the predicate while a metadata statement is any statement
* in the reified query whose predicate is not rdf:type and not a
* StatementPattern predicate.
*/
@Test
public void simpleQueryWithBindingSetJoinPropertyToSubject() throws Exception {
MongoDBRyaDAO dao = new MongoDBRyaDAO();
try {
dao.setConf(conf);
dao.init();
StatementMetadata metadata = new StatementMetadata();
metadata.addMetadata(new RyaURI("http://createdBy"), new RyaURI("http://Joe"));
metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
RyaStatement statement2 = new RyaStatement(new RyaURI("http://Bob"), new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata);
dao.add(statement1);
dao.add(statement2);
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query2, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);
List<BindingSet> bsCollection = new ArrayList<>();
QueryBindingSet bsConstraint1 = new QueryBindingSet();
bsConstraint1.addBinding("y", new LiteralImpl("CoffeeShop"));
bsConstraint1.addBinding("z", new LiteralImpl("Virginia"));
QueryBindingSet bsConstraint2 = new QueryBindingSet();
bsConstraint2.addBinding("y", new LiteralImpl("HardwareStore"));
bsConstraint2.addBinding("z", new LiteralImpl("Maryland"));
bsCollection.add(bsConstraint1);
bsCollection.add(bsConstraint2);
CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsCollection);
QueryBindingSet expected = new QueryBindingSet();
expected.addBinding("y", new LiteralImpl("CoffeeShop"));
expected.addBinding("x", new URIImpl("http://Joe"));
expected.addBinding("z", new LiteralImpl("Virginia"));
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(1, bsList.size());
Assert.assertEquals(expected, bsList.get(0));
dao.delete(statement1, conf);
dao.delete(statement2, conf);
} finally {
dao.destroy();
}
}
use of org.apache.rya.mongodb.MongoDBRyaDAO in project incubator-rya by apache.
the class MongoStatementMetadataNodeIT method simpleQueryWithBindingSetCollection.
/**
* Tests if StatementMetadataNode joins BindingSet values correctly for
* variables appearing as the object in one of the StatementPattern
* statements (in the case ?x appears as the Object in the statement
* _:blankNode rdf:object ?x). StatementPattern statements have either
* rdf:subject, rdf:predicate, or rdf:object as the predicate.
*/
@Test
public void simpleQueryWithBindingSetCollection() throws Exception {
MongoDBRyaDAO dao = new MongoDBRyaDAO();
try {
dao.setConf(conf);
dao.init();
StatementMetadata metadata = new StatementMetadata();
metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe"));
metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata);
dao.add(statement1);
dao.add(statement2);
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);
List<BindingSet> bsCollection = new ArrayList<>();
QueryBindingSet bsConstraint1 = new QueryBindingSet();
bsConstraint1.addBinding("x", new LiteralImpl("CoffeeShop"));
bsConstraint1.addBinding("z", new LiteralImpl("Virginia"));
QueryBindingSet bsConstraint2 = new QueryBindingSet();
bsConstraint2.addBinding("x", new LiteralImpl("HardwareStore"));
bsConstraint2.addBinding("z", new LiteralImpl("Maryland"));
QueryBindingSet bsConstraint3 = new QueryBindingSet();
bsConstraint3.addBinding("x", new LiteralImpl("BurgerShack"));
bsConstraint3.addBinding("z", new LiteralImpl("Delaware"));
bsCollection.add(bsConstraint1);
bsCollection.add(bsConstraint2);
bsCollection.add(bsConstraint3);
CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsCollection);
Set<BindingSet> expected = new HashSet<>();
QueryBindingSet expected1 = new QueryBindingSet();
expected1.addBinding("x", new LiteralImpl("CoffeeShop"));
expected1.addBinding("y", new LiteralImpl("Joe"));
expected1.addBinding("z", new LiteralImpl("Virginia"));
QueryBindingSet expected2 = new QueryBindingSet();
expected2.addBinding("x", new LiteralImpl("HardwareStore"));
expected2.addBinding("y", new LiteralImpl("Joe"));
expected2.addBinding("z", new LiteralImpl("Maryland"));
expected.add(expected1);
expected.add(expected2);
Set<BindingSet> bsSet = new HashSet<>();
while (iteration.hasNext()) {
bsSet.add(iteration.next());
}
Assert.assertEquals(expected, bsSet);
dao.delete(statement1, conf);
dao.delete(statement2, conf);
} finally {
dao.destroy();
}
}
use of org.apache.rya.mongodb.MongoDBRyaDAO in project incubator-rya by apache.
the class MongoStatementMetadataNodeIT method simpleQueryWithBindingSet.
@Test
public void simpleQueryWithBindingSet() throws Exception {
MongoDBRyaDAO dao = new MongoDBRyaDAO();
try {
dao.setConf(conf);
dao.init();
StatementMetadata metadata = new StatementMetadata();
metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe"));
metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata);
dao.add(statement1);
dao.add(statement2);
SPARQLParser parser = new SPARQLParser();
ParsedQuery pq = parser.parseQuery(query, null);
List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
StatementMetadataNode<MongoDBRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);
QueryBindingSet bsConstraint = new QueryBindingSet();
bsConstraint.addBinding("x", new LiteralImpl("CoffeeShop"));
bsConstraint.addBinding("z", new LiteralImpl("Virginia"));
CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsConstraint);
QueryBindingSet expected = new QueryBindingSet();
expected.addBinding("x", new LiteralImpl("CoffeeShop"));
expected.addBinding("y", new LiteralImpl("Joe"));
expected.addBinding("z", new LiteralImpl("Virginia"));
List<BindingSet> bsList = new ArrayList<>();
while (iteration.hasNext()) {
bsList.add(iteration.next());
}
Assert.assertEquals(1, bsList.size());
Assert.assertEquals(expected, bsList.get(0));
dao.delete(statement1, conf);
dao.delete(statement2, conf);
} finally {
dao.destroy();
}
}
use of org.apache.rya.mongodb.MongoDBRyaDAO in project incubator-rya by apache.
the class PcjDocumentsIntegrationTest method populatePcj.
/**
* Ensure when results are already stored in Rya, that we are able to populate
* the PCJ table for a new SPARQL query using those results.
* <p>
* The method being tested is: {@link PcjTables#populatePcj(Connector, String, RepositoryConnection, String)}
*/
@Test
public void populatePcj() throws Exception {
final MongoDBRyaDAO dao = new MongoDBRyaDAO();
dao.setConf(new StatefulMongoDBRdfConfiguration(conf, getMongoClient()));
dao.init();
final RdfCloudTripleStore ryaStore = new RdfCloudTripleStore();
ryaStore.setRyaDAO(dao);
ryaStore.initialize();
final SailRepositoryConnection ryaConn = new RyaSailRepository(ryaStore).getConnection();
ryaConn.begin();
try {
// Load some Triples into Rya.
final Set<Statement> triples = new HashSet<>();
triples.add(new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://hasAge"), new NumericLiteralImpl(14, XMLSchema.INTEGER)));
triples.add(new StatementImpl(new URIImpl("http://Alice"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
triples.add(new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://hasAge"), new NumericLiteralImpl(16, XMLSchema.INTEGER)));
triples.add(new StatementImpl(new URIImpl("http://Bob"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
triples.add(new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://hasAge"), new NumericLiteralImpl(12, XMLSchema.INTEGER)));
triples.add(new StatementImpl(new URIImpl("http://Charlie"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
triples.add(new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://hasAge"), new NumericLiteralImpl(43, XMLSchema.INTEGER)));
triples.add(new StatementImpl(new URIImpl("http://Eve"), new URIImpl("http://playsSport"), new LiteralImpl("Soccer")));
for (final Statement triple : triples) {
ryaConn.add(triple);
}
// Create a PCJ table that will include those triples in its results.
final String sparql = "SELECT ?name ?age " + "{" + "FILTER(?age < 30) ." + "?name <http://hasAge> ?age." + "?name <http://playsSport> \"Soccer\" " + "}";
final String pcjTableName = "testPcj";
final MongoPcjDocuments pcjs = new MongoPcjDocuments(getMongoClient(), conf.getRyaInstanceName());
pcjs.createPcj(pcjTableName, sparql);
// Populate the PCJ table using a Rya connection.
pcjs.populatePcj(pcjTableName, ryaConn);
final Collection<BindingSet> fetchedResults = loadPcjResults(pcjTableName);
// Make sure the cardinality was updated.
final PcjMetadata metadata = pcjs.getPcjMetadata(pcjTableName);
assertEquals(3, metadata.getCardinality());
// Ensure the expected results match those that were stored.
final MapBindingSet alice = new MapBindingSet();
alice.addBinding("name", new URIImpl("http://Alice"));
alice.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
final MapBindingSet bob = new MapBindingSet();
bob.addBinding("name", new URIImpl("http://Bob"));
bob.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
final MapBindingSet charlie = new MapBindingSet();
charlie.addBinding("name", new URIImpl("http://Charlie"));
charlie.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
final Set<BindingSet> expected = Sets.<BindingSet>newHashSet(alice, bob, charlie);
assertEquals(expected, fetchedResults);
} finally {
ryaConn.close();
ryaStore.shutDown();
}
}
Aggregations