Search in sources :

Example 6 with StatementMetadata

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

the class MongoStatementMetadataNodeIT method simpleQueryWithoutBindingSet.

@Test
public void simpleQueryWithoutBindingSet() 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 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<?> node = new StatementMetadataNode<>(spList, conf);
        CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(new QueryBindingSet());
        QueryBindingSet bs = new QueryBindingSet();
        bs.addBinding("x", new LiteralImpl("CoffeeShop"));
        bs.addBinding("y", new LiteralImpl("Joe"));
        List<BindingSet> bsList = new ArrayList<>();
        while (iteration.hasNext()) {
            bsList.add(iteration.next());
        }
        Assert.assertEquals(1, bsList.size());
        Assert.assertEquals(bs, bsList.get(0));
        dao.delete(statement, conf);
    } finally {
        dao.destroy();
    }
}
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) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) MongoDBRyaDAO(org.apache.rya.mongodb.MongoDBRyaDAO) 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 7 with StatementMetadata

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

the class StatementMetadataExample method example1.

/**
 * This example demonstrates how a reified query can be used to return
 * metadata. In the example below, the query returns all the places that Joe
 * works at along with the people that created the triples containing those locations
 * and the dates that those triples were created on.
 *
 * @throws Exception
 */
private void example1() throws Exception {
    String query1 = "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 \n" + "WHERE {\n" + "_:blankNode rdf:type owl:Annotation. \n" + "_:blankNode owl:annotatedSource <http://Joe>. \n" + "_:blankNode owl:annotatedProperty <http://worksAt>. \n" + "_:blankNode owl:annotatedTarget ?x. \n" + "_:blankNode <http://createdBy> ?y. \n" + "_:blankNode <http://createdOn> ?z }\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);
    // add statements for querying
    dao.add(statement1);
    dao.add(statement2);
    dao.add(statement3);
    System.out.println("**************************************************************************");
    System.out.println("                            RUNNING EXAMPLE 1");
    System.out.println("**************************************************************************");
    System.out.println("");
    // issue query - 3 results expected
    query(query1, 3);
    // delete statements to run next example
    dao.delete(Arrays.asList(statement1, statement2, statement3).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 8 with StatementMetadata

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

the class StatementMetadataExample method example3.

/**
 * In addition to returning metadata, this example demonstrates how a
 * reified query can be used to return only those triples matching certain
 * metadata criteria.  The query below returns only those triples containing Joe's
 * work location that were created on 2017-02-04.  To filter by metadata property, simply set the
 * value for the property to a fixed constant ('2017-02-04'^^xsd:date is set for the property
 * http://createdOn in the query below).
 *
 * @throws Exception
 */
private void example3() throws Exception {
    String query3 = "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 \n" + "WHERE {\n" + "_:blankNode rdf:type owl:Annotation. \n" + "_:blankNode owl:annotatedSource <http://Joe>. \n" + "_:blankNode owl:annotatedProperty <http://worksAt>. \n" + "_:blankNode owl:annotatedTarget ?x. \n" + "_:blankNode <http://createdBy> ?y. \n" + "_:blankNode <http://createdOn> '2017-02-04'^^xsd:date }\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);
    // add statements for querying
    dao.add(statement1);
    dao.add(statement2);
    dao.add(statement3);
    System.out.println("**************************************************************************");
    System.out.println("                            RUNNING EXAMPLE 3");
    System.out.println("**************************************************************************");
    System.out.println("");
    // issue query - 1 result expected
    query(query3, 1);
    // delete statements to run next example
    dao.delete(Arrays.asList(statement1, statement2, statement3).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 9 with StatementMetadata

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

the class AccumuloStatementMetadataNodeTest method simpleQueryWithBindingSetJoinOnProperty.

/**
 * Tests if the StatementMetadataNode joins BindingSet correctly for
 * variables appearing in metadata statements. In this case, the metadata
 * statements are (_:blankNode <http://createdOn 2017-01-04 ) and
 * (_:blankNode <http://createdBy> ?y). The variable ?y appears as the
 * object in the above metadata statement and its values are joined to the
 * constraint BindingSets in the example below.
 *
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithBindingSetJoinOnProperty() 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);
    dao.add(statement1);
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
    StatementMetadataNode<AccumuloRdfConfiguration> node = new StatementMetadataNode<>(spList, conf);
    QueryBindingSet bsConstraint = new QueryBindingSet();
    bsConstraint.addBinding("x", new LiteralImpl("CoffeeShop"));
    bsConstraint.addBinding("y", new LiteralImpl("Doug"));
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = node.evaluate(bsConstraint);
    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }
    Assert.assertEquals(0, bsList.size());
    dao.delete(statement1, 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)

Example 10 with StatementMetadata

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

the class AccumuloStatementMetadataNodeTest method simpleQueryWithBindingSet.

@Test
public void simpleQueryWithBindingSet() 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);
    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);
}
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