Search in sources :

Example 31 with StatementMetadata

use of org.apache.rya.api.domain.StatementMetadata in project incubator-rya by apache.

the class StatementMetadataExample method example4.

/**
 * This example demonstrates the ability to join across two differnt StatementMetadataNodes.
 * The query below consists of triple patterns with _:blankNode1 as the subject and patterns
 * with _:blankNode2 as the subject.  Each of these groups of patterns will be used to form
 * a @link{StatementMetadataNode} to efficiently query for the underlying triples and metadata.
 * The effective query plan of the following query will consist of two StatementMetaData nodes
 * that will be joined on the variables ?x and ?y -- the values of owl:annotatedTarget and the
 * metadata property http://createdBy.  So the query below will return the work locations that Joe
 * and Bob have in common that appear in triples created by the same person, in addition to the dates
 * that each triple was created on.
 *
 * @throws Exception
 */
private void example4() throws Exception {
    String query4 = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n" + "PREFIX owl: <http://www.w3.org/2002/07/owl#> \n" + "SELECT ?x ?y ?z ?a \n" + "WHERE {\n" + "_:blankNode1 rdf:type owl:Annotation. \n" + "_:blankNode1 owl:annotatedSource <http://Joe>. \n" + "_:blankNode1 owl:annotatedProperty <http://worksAt>. \n" + "_:blankNode1 owl:annotatedTarget ?x. \n" + "_:blankNode1 <http://createdBy> ?y. \n" + "_:blankNode1 <http://createdOn> ?z. \n" + "_:blankNode2 rdf:type owl:Annotation. \n" + "_:blankNode2 owl:annotatedSource <http://Bob>. \n" + "_:blankNode2 owl:annotatedProperty <http://worksAt>. \n" + "_:blankNode2 owl:annotatedTarget ?x. \n" + "_:blankNode2 <http://createdBy> ?y. \n" + "_:blankNode2 <http://createdOn> ?a }\n";
    StatementMetadata metadata1 = new StatementMetadata();
    metadata1.addMetadata(new RyaURI("http://createdBy"), new RyaURI("http://Dave"));
    metadata1.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-02"));
    RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata1);
    StatementMetadata metadata2 = new StatementMetadata();
    metadata2.addMetadata(new RyaURI("http://createdBy"), new RyaURI("http://Dave"));
    metadata2.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-02-04"));
    RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata2);
    StatementMetadata metadata3 = new StatementMetadata();
    metadata3.addMetadata(new RyaURI("http://createdBy"), new RyaURI("http://Fred"));
    metadata3.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-03-08"));
    RyaStatement statement3 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("Library"), new RyaURI("http://context"), "", metadata3);
    StatementMetadata metadata4 = new StatementMetadata();
    metadata4.addMetadata(new RyaURI("http://createdBy"), new RyaURI("http://Dave"));
    metadata4.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-04-16"));
    RyaStatement statement4 = new RyaStatement(new RyaURI("http://Bob"), new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata4);
    // add statements for querying
    dao.add(statement1);
    dao.add(statement2);
    dao.add(statement3);
    dao.add(statement4);
    System.out.println("**************************************************************************");
    System.out.println("                            RUNNING EXAMPLE 4");
    System.out.println("**************************************************************************");
    System.out.println("");
    // issue query - 1 result expected
    query(query4, 1);
    // delete statements to run next example
    dao.delete(Arrays.asList(statement1, statement2, statement3, statement4).iterator(), getConf());
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaType(org.apache.rya.api.domain.RyaType)

Example 32 with StatementMetadata

use of org.apache.rya.api.domain.StatementMetadata in project incubator-rya by apache.

the class AccumuloStatementMetadataNodeTest 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.
 *
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithBindingSetJoinPropertyToSubject() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    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<AccumuloRdfConfiguration> 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);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) URIImpl(org.openrdf.model.impl.URIImpl) RyaType(org.apache.rya.api.domain.RyaType) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) RyaURI(org.apache.rya.api.domain.RyaURI) StatementPattern(org.openrdf.query.algebra.StatementPattern) StatementMetadataNode(org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode) LiteralImpl(org.openrdf.model.impl.LiteralImpl) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Test(org.junit.Test)

Example 33 with StatementMetadata

use of org.apache.rya.api.domain.StatementMetadata in project incubator-rya by apache.

the class AccumuloStatementMetadataNodeTest method simpleQueryWithVariableContext.

/**
 * 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. Additionally,
 * this test also determines whether node passes back bindings corresponding
 * to a specified context.
 *
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithVariableContext() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    // query used to create StatementPatternMetadataNode
    String contextQuery = "prefix owl: <http://www.w3.org/2002/07/owl#> prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> select ?x ?y ?c where { graph ?c {_:blankNode rdf:type owl:Annotation; owl:annotatedSource <http://Joe>; " + "owl:annotatedProperty <http://worksAt>; owl:annotatedTarget ?x; <http://createdBy> ?y; <http://createdOn> \'2017-01-04\'^^xsd:date }}";
    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_1"), "", metadata);
    RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context_2"), "", metadata);
    dao.add(statement1);
    dao.add(statement2);
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(contextQuery, null);
    List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
    StatementMetadataNode<AccumuloRdfConfiguration> 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);
    QueryBindingSet expected1 = new QueryBindingSet();
    expected1.addBinding("x", new LiteralImpl("CoffeeShop"));
    expected1.addBinding("y", new LiteralImpl("Joe"));
    expected1.addBinding("z", new LiteralImpl("Virginia"));
    expected1.addBinding("c", new URIImpl("http://context_1"));
    QueryBindingSet expected2 = new QueryBindingSet();
    expected2.addBinding("x", new LiteralImpl("HardwareStore"));
    expected2.addBinding("y", new LiteralImpl("Joe"));
    expected2.addBinding("z", new LiteralImpl("Maryland"));
    expected2.addBinding("c", new URIImpl("http://context_2"));
    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }
    Assert.assertEquals(2, bsList.size());
    Assert.assertEquals(expected1, bsList.get(1));
    Assert.assertEquals(expected2, bsList.get(0));
    dao.delete(statement1, conf);
    dao.delete(statement2, conf);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) URIImpl(org.openrdf.model.impl.URIImpl) RyaType(org.apache.rya.api.domain.RyaType) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) RyaURI(org.apache.rya.api.domain.RyaURI) StatementPattern(org.openrdf.query.algebra.StatementPattern) StatementMetadataNode(org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode) LiteralImpl(org.openrdf.model.impl.LiteralImpl) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Test(org.junit.Test)

Example 34 with StatementMetadata

use of org.apache.rya.api.domain.StatementMetadata in project incubator-rya by apache.

the class AccumuloStatementMetadataNodeTest method simpleQueryWithoutBindingSetInvalidProperty.

/**
 * Tests if results are filtered correctly using the metadata properties. In
 * this case, the date for the ingested RyaStatement differs from the date
 * specified in the query.
 *
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithoutBindingSetInvalidProperty() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    StatementMetadata metadata = new StatementMetadata();
    metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Doug"));
    metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-02-15"));
    RyaStatement statement = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
    dao.add(statement);
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
    StatementMetadataNode<AccumuloRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(new QueryBindingSet());
    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }
    Assert.assertEquals(0, bsList.size());
    dao.delete(statement, conf);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaType(org.apache.rya.api.domain.RyaType) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) RyaURI(org.apache.rya.api.domain.RyaURI) StatementPattern(org.openrdf.query.algebra.StatementPattern) StatementMetadataNode(org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Test(org.junit.Test)

Example 35 with StatementMetadata

use of org.apache.rya.api.domain.StatementMetadata in project incubator-rya by apache.

the class AccumuloStatementMetadataNodeTest 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.
 *
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithBindingSetCollection() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    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<AccumuloRdfConfiguration> 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);
    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"));
    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }
    Assert.assertEquals(2, bsList.size());
    Assert.assertEquals(expected1, bsList.get(1));
    Assert.assertEquals(expected2, bsList.get(0));
    dao.delete(statement1, conf);
    dao.delete(statement2, conf);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaType(org.apache.rya.api.domain.RyaType) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) RyaURI(org.apache.rya.api.domain.RyaURI) StatementPattern(org.openrdf.query.algebra.StatementPattern) StatementMetadataNode(org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode) LiteralImpl(org.openrdf.model.impl.LiteralImpl) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Test(org.junit.Test)

Aggregations

StatementMetadata (org.apache.rya.api.domain.StatementMetadata)37 RyaStatement (org.apache.rya.api.domain.RyaStatement)36 RyaURI (org.apache.rya.api.domain.RyaURI)35 RyaType (org.apache.rya.api.domain.RyaType)34 Test (org.junit.Test)30 BindingSet (org.openrdf.query.BindingSet)28 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)28 ArrayList (java.util.ArrayList)25 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)20 StatementPattern (org.openrdf.query.algebra.StatementPattern)20 ParsedQuery (org.openrdf.query.parser.ParsedQuery)20 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)20 LiteralImpl (org.openrdf.model.impl.LiteralImpl)17 StatementMetadataNode (org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode)15 URIImpl (org.openrdf.model.impl.URIImpl)11 MongoDBRyaDAO (org.apache.rya.mongodb.MongoDBRyaDAO)10 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)8 TupleQueryResult (org.openrdf.query.TupleQueryResult)8 HashSet (java.util.HashSet)7 MongoDBRdfConfiguration (org.apache.rya.mongodb.MongoDBRdfConfiguration)5