use of org.openrdf.model.impl.StatementImpl in project incubator-rya by apache.
the class RdfCloudTripleStoreConnectionTest method testSubPropertyOf.
public void testSubPropertyOf() throws Exception {
if (internalInferenceEngine == null) {
// infer not supported;
return;
}
RepositoryConnection conn = repository.getConnection();
conn.add(new StatementImpl(vf.createURI(litdupsNS, "undergradDegreeFrom"), RDFS.SUBPROPERTYOF, vf.createURI(litdupsNS, "degreeFrom")));
conn.add(new StatementImpl(vf.createURI(litdupsNS, "gradDegreeFrom"), RDFS.SUBPROPERTYOF, vf.createURI(litdupsNS, "degreeFrom")));
conn.add(new StatementImpl(vf.createURI(litdupsNS, "degreeFrom"), RDFS.SUBPROPERTYOF, vf.createURI(litdupsNS, "memberOf")));
conn.add(new StatementImpl(vf.createURI(litdupsNS, "memberOf"), RDFS.SUBPROPERTYOF, vf.createURI(litdupsNS, "associatedWith")));
conn.add(new StatementImpl(vf.createURI(litdupsNS, "UgradA"), vf.createURI(litdupsNS, "undergradDegreeFrom"), vf.createURI(litdupsNS, "Harvard")));
conn.add(new StatementImpl(vf.createURI(litdupsNS, "GradB"), vf.createURI(litdupsNS, "gradDegreeFrom"), vf.createURI(litdupsNS, "Yale")));
conn.add(new StatementImpl(vf.createURI(litdupsNS, "ProfessorC"), vf.createURI(litdupsNS, "memberOf"), vf.createURI(litdupsNS, "Harvard")));
conn.commit();
conn.close();
internalInferenceEngine.refreshGraph();
conn = repository.getConnection();
String query = "PREFIX rdfs: <" + RDFS.NAMESPACE + ">\n" + "PREFIX rdf: <" + RDF.NAMESPACE + ">\n" + "PREFIX lit: <" + litdupsNS + ">\n" + "select * where {?s lit:degreeFrom lit:Harvard.}";
TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
CountTupleHandler tupleHandler = new CountTupleHandler();
tupleQuery.evaluate(tupleHandler);
assertEquals(1, tupleHandler.getCount());
query = "PREFIX rdfs: <" + RDFS.NAMESPACE + ">\n" + "PREFIX rdf: <" + RDF.NAMESPACE + ">\n" + "PREFIX lit: <" + litdupsNS + ">\n" + "select * where {?s lit:memberOf lit:Harvard.}";
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tupleHandler = new CountTupleHandler();
tupleQuery.evaluate(tupleHandler);
assertEquals(2, tupleHandler.getCount());
query = "PREFIX rdfs: <" + RDFS.NAMESPACE + ">\n" + "PREFIX rdf: <" + RDF.NAMESPACE + ">\n" + "PREFIX lit: <" + litdupsNS + ">\n" + "select * where {?s lit:associatedWith ?o.}";
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tupleHandler = new CountTupleHandler();
tupleQuery.evaluate(tupleHandler);
assertEquals(3, tupleHandler.getCount());
query = "PREFIX rdfs: <" + RDFS.NAMESPACE + ">\n" + "PREFIX rdf: <" + RDF.NAMESPACE + ">\n" + "PREFIX lit: <" + litdupsNS + ">\n" + "select * where {?s lit:gradDegreeFrom lit:Yale.}";
tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
tupleHandler = new CountTupleHandler();
tupleQuery.evaluate(tupleHandler);
assertEquals(1, tupleHandler.getCount());
conn.close();
}
use of org.openrdf.model.impl.StatementImpl in project incubator-rya by apache.
the class MongoTemporalIndexerIT method testQueryWithMultiplePredicates.
/**
* Test instant after a given instant WITH two different predicates as constraints.
*/
@Test
public void testQueryWithMultiplePredicates() throws IOException, QueryEvaluationException {
try (MongoTemporalIndexer tIndexer = new MongoTemporalIndexer()) {
tIndexer.setConf(conf);
tIndexer.init();
// tiB02_E30 read as: Begins 2 seconds, ends at 30 seconds
// these should not match as they are not instances.
tIndexer.storeStatement(convertStatement(spo_B03_E20));
tIndexer.storeStatement(convertStatement(spo_B02_E30));
tIndexer.storeStatement(convertStatement(spo_B02_E40));
tIndexer.storeStatement(convertStatement(spo_B02_E31));
tIndexer.storeStatement(convertStatement(spo_B30_E32));
// seriesSpo[s] and seriesTs[s] are statements and instant for s seconds after the uniform time.
final int searchForSeconds = 4;
final int expectedResultCount = 9;
for (int s = 0; s <= searchForSeconds + expectedResultCount; s++) {
// <== logic here
tIndexer.storeStatement(convertStatement(seriesSpo[s]));
}
final ValueFactory vf = new ValueFactoryImpl();
// this one to ignore.
final URI pred3_CIRCA_ = vf.createURI(URI_PROPERTY_CIRCA);
final URI pred2_eventTime = vf.createURI(URI_PROPERTY_EVENT_TIME);
final URI pred1_atTime = vf.createURI(URI_PROPERTY_AT_TIME);
// add the predicate = EventTime ; Store in an array for verification.
final Statement[] SeriesTs_EventTime = new Statement[expectedResultCount + 1];
for (int s = 0; s <= searchForSeconds + expectedResultCount; s++) {
// <== logic here
final Statement statement = new StatementImpl(vf.createURI("foo:EventTimeSubj0" + s), pred2_eventTime, vf.createLiteral(seriesTs[s].getAsReadable()));
tIndexer.storeStatement(convertStatement(statement));
if (s > searchForSeconds) {
SeriesTs_EventTime[s - searchForSeconds - 1] = statement;
}
}
// add the predicate = CIRCA ; to be ignored because it is not in the constraints.
for (int s = 0; s <= searchForSeconds + expectedResultCount; s++) {
// <== logic here
final Statement statement = new StatementImpl(vf.createURI("foo:CircaEventSubj0" + s), pred3_CIRCA_, vf.createLiteral(seriesTs[s].getAsReadable()));
tIndexer.storeStatement(convertStatement(statement));
}
CloseableIteration<Statement, QueryEvaluationException> iter;
final StatementConstraints constraints = new StatementConstraints();
constraints.setPredicates(new HashSet<>(Arrays.asList(pred2_eventTime, pred1_atTime)));
// EMPTY_CONSTRAINTS);//
iter = tIndexer.queryInstantAfterInstant(seriesTs[searchForSeconds], constraints);
int count_AtTime = 0;
int count_EventTime = 0;
while (iter.hasNext()) {
final Statement s = iter.next();
// <== logic here
final Statement nextExpectedStatement = seriesSpo[searchForSeconds + count_AtTime + 1];
if (s.getPredicate().equals(pred1_atTime)) {
assertTrue("Should match atTime: " + nextExpectedStatement + " == " + s, nextExpectedStatement.equals(s));
count_AtTime++;
} else if (s.getPredicate().equals(pred2_eventTime)) {
assertTrue("Should match eventTime: " + SeriesTs_EventTime[count_EventTime] + " == " + s, SeriesTs_EventTime[count_EventTime].equals(s));
count_EventTime++;
} else {
assertTrue("This predicate should not be returned: " + s, false);
}
}
assertEquals("Should find count of atTime rows.", expectedResultCount, count_AtTime);
assertEquals("Should find count of eventTime rows.", expectedResultCount, count_EventTime);
}
}
use of org.openrdf.model.impl.StatementImpl in project incubator-rya by apache.
the class MongoTemporalIndexerIT method testDelete.
@Test
public void testDelete() throws IOException, MongoException, TableNotFoundException, TableExistsException, NoSuchAlgorithmException {
try (MongoTemporalIndexer tIndexer = new MongoTemporalIndexer()) {
tIndexer.setConf(conf);
tIndexer.init();
final ValueFactory vf = new ValueFactoryImpl();
final URI pred1_atTime = vf.createURI(URI_PROPERTY_AT_TIME);
final URI pred2_circa = vf.createURI(URI_PROPERTY_CIRCA);
final String testDate2014InBRST = "2014-12-31T23:59:59-02:00";
final String testDate2016InET = "2016-12-31T20:59:59-05:00";
// These should be stored because they are in the predicate list.
// BUT they will get converted to the same exact datetime in UTC.
final Statement s1 = new StatementImpl(vf.createURI("foo:subj3"), pred1_atTime, vf.createLiteral(testDate2014InBRST));
final Statement s2 = new StatementImpl(vf.createURI("foo:subj4"), pred2_circa, vf.createLiteral(testDate2016InET));
tIndexer.storeStatement(convertStatement(s1));
tIndexer.storeStatement(convertStatement(s2));
final String dbName = conf.getMongoDBName();
final DB db = super.getMongoClient().getDB(dbName);
DBCollection collection = db.getCollection(conf.get(MongoDBRdfConfiguration.MONGO_COLLECTION_PREFIX, "rya") + tIndexer.getCollectionName());
printTables(tIndexer, "junit testing: Temporal entities stored in testDelete before delete");
// 4 index entries per statement
assertEquals("Number of rows stored.", 2, collection.count());
tIndexer.deleteStatement(convertStatement(s1));
tIndexer.deleteStatement(convertStatement(s2));
printTables(tIndexer, "junit testing: Temporal entities stored in testDelete after delete");
assertEquals("Number of rows stored after delete.", 0, collection.count());
}
}
use of org.openrdf.model.impl.StatementImpl in project incubator-rya by apache.
the class MongoTemporalIndexerIT method testStoreStatement.
/**
* Test method for {@link MongoTemporalIndexer#storeStatement(convertStatement(org.openrdf.model.Statement)}
*/
@Test
public void testStoreStatement() throws IOException {
try (MongoTemporalIndexer tIndexer = new MongoTemporalIndexer()) {
tIndexer.setConf(conf);
tIndexer.init();
final ValueFactory vf = new ValueFactoryImpl();
final URI pred1_atTime = vf.createURI(URI_PROPERTY_AT_TIME);
final URI pred2_circa = vf.createURI(URI_PROPERTY_CIRCA);
// Should not be stored because they are not in the predicate list
final String validDateStringWithThirteens = "1313-12-13T13:13:13Z";
tIndexer.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj1"), RDFS.LABEL, vf.createLiteral(validDateStringWithThirteens))));
final String invalidDateString = "ThisIsAnInvalidDate";
tIndexer.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj2"), pred1_atTime, vf.createLiteral(invalidDateString))));
// These are different datetimes instant but from different time zones.
// This is an arbitrary zone, BRST=Brazil, better if not local.
// same as "2015-01-01T01:59:59Z"
final String testDate2014InBRST = "2014-12-31T23:59:59-02:00";
// next year, same as "2017-01-01T01:59:59Z"
final String testDate2016InET = "2016-12-31T20:59:59-05:00";
// These should be stored because they are in the predicate list.
// BUT they will get converted to the same exact datetime in UTC.
final Statement s3 = new StatementImpl(vf.createURI("foo:subj3"), pred1_atTime, vf.createLiteral(testDate2014InBRST));
final Statement s4 = new StatementImpl(vf.createURI("foo:subj4"), pred2_circa, vf.createLiteral(testDate2016InET));
tIndexer.storeStatement(convertStatement(s3));
tIndexer.storeStatement(convertStatement(s4));
// This should not be stored because the object is not a literal
tIndexer.storeStatement(convertStatement(new StatementImpl(vf.createURI("foo:subj5"), pred1_atTime, vf.createURI("in:valid"))));
printTables(tIndexer, "junit testing: Temporal entities stored in testStoreStatement");
assertEquals(2, tIndexer.getCollection().find().count());
}
}
use of org.openrdf.model.impl.StatementImpl in project incubator-rya by apache.
the class AccumuloIndexSetTest method accumuloIndexSetTestWithTwoDirectProductBindingSet.
@Test
public void accumuloIndexSetTestWithTwoDirectProductBindingSet() throws RepositoryException, PcjException, TableNotFoundException, RyaTypeResolverException, MalformedQueryException, SailException, QueryEvaluationException, AccumuloException, AccumuloSecurityException {
// 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 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 = new PcjTableNameFactory().makeTableName(prefix, "testPcj");
// Create and populate the PCJ table.
PcjIntegrationTestingUtil.createAndPopulatePcj(ryaConn, accumuloConn, pcjTableName, sparql, new String[] { "name", "age" }, Optional.<PcjVarOrderFactory>absent());
final AccumuloIndexSet ais = new AccumuloIndexSet(conf, pcjTableName);
final QueryBindingSet bs = new QueryBindingSet();
bs.addBinding("birthDate", new LiteralImpl("1983-03-17", new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
bs.addBinding("location", new URIImpl("http://Virginia"));
final QueryBindingSet bs2 = new QueryBindingSet();
bs2.addBinding("birthDate", new LiteralImpl("1983-04-18", new URIImpl("http://www.w3.org/2001/XMLSchema#date")));
bs2.addBinding("location", new URIImpl("http://Georgia"));
final Set<BindingSet> bSets = Sets.<BindingSet>newHashSet(bs, bs2);
final CloseableIteration<BindingSet, QueryEvaluationException> results = ais.evaluate(bSets);
final QueryBindingSet alice1 = new QueryBindingSet();
alice1.addBinding("name", new URIImpl("http://Alice"));
alice1.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
alice1.addAll(bs);
final QueryBindingSet bob1 = new QueryBindingSet();
bob1.addBinding("name", new URIImpl("http://Bob"));
bob1.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
bob1.addAll(bs);
final QueryBindingSet charlie1 = new QueryBindingSet();
charlie1.addBinding("name", new URIImpl("http://Charlie"));
charlie1.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
charlie1.addAll(bs);
final QueryBindingSet alice2 = new QueryBindingSet();
alice2.addBinding("name", new URIImpl("http://Alice"));
alice2.addBinding("age", new NumericLiteralImpl(14, XMLSchema.INTEGER));
alice2.addAll(bs2);
final QueryBindingSet bob2 = new QueryBindingSet();
bob2.addBinding("name", new URIImpl("http://Bob"));
bob2.addBinding("age", new NumericLiteralImpl(16, XMLSchema.INTEGER));
bob2.addAll(bs2);
final QueryBindingSet charlie2 = new QueryBindingSet();
charlie2.addBinding("name", new URIImpl("http://Charlie"));
charlie2.addBinding("age", new NumericLiteralImpl(12, XMLSchema.INTEGER));
charlie2.addAll(bs2);
final Set<BindingSet> fetchedResults = new HashSet<>();
while (results.hasNext()) {
final BindingSet next = results.next();
System.out.println(next);
fetchedResults.add(next);
}
Assert.assertEquals(Sets.<BindingSet>newHashSet(alice1, bob1, charlie1, alice2, bob2, charlie2), fetchedResults);
}
Aggregations