Search in sources :

Example 36 with StatementMetadata

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

the class AccumuloStatementMetadataNodeTest method simpleQueryWithVariableContextAndJoinOnContext.

/**
 * 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 and that a join across variable context is
 * performed properly.
 *
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithVariableContextAndJoinOnContext() 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"));
    bsConstraint1.addBinding("c", new URIImpl("http://context_1"));
    QueryBindingSet bsConstraint2 = new QueryBindingSet();
    bsConstraint2.addBinding("x", new LiteralImpl("CoffeeShop"));
    bsConstraint2.addBinding("z", new LiteralImpl("Maryland"));
    bsConstraint2.addBinding("c", new URIImpl("http://context_2"));
    QueryBindingSet bsConstraint4 = new QueryBindingSet();
    bsConstraint4.addBinding("x", new LiteralImpl("HardwareStore"));
    bsConstraint4.addBinding("z", new LiteralImpl("WestVirginia"));
    bsConstraint4.addBinding("c", new URIImpl("http://context_2"));
    QueryBindingSet bsConstraint3 = new QueryBindingSet();
    bsConstraint3.addBinding("x", new LiteralImpl("BurgerShack"));
    bsConstraint3.addBinding("z", new LiteralImpl("Delaware"));
    bsConstraint3.addBinding("c", new URIImpl("http://context_1"));
    bsCollection.add(bsConstraint1);
    bsCollection.add(bsConstraint2);
    bsCollection.add(bsConstraint3);
    bsCollection.add(bsConstraint4);
    // AccumuloRyaQueryEngine engine = dao.getQueryEngine();
    // //        CloseableIteration<RyaStatement, RyaDAOException> iter = engine.query(new RyaStatement(new RyaURI("http://Joe"),
    // //                new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context_2")), conf);
    // CloseableIteration<? extends Map.Entry<RyaStatement,BindingSet>, RyaDAOException> iter = engine.queryWithBindingSet(Arrays.asList(new RdfCloudTripleStoreUtils.CustomEntry<RyaStatement, BindingSet>(
    // new RyaStatement(new RyaURI("http://Joe"),
    // new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context_2")), bsConstraint4)), conf);
    // while (iter.hasNext()) {
    // System.out.println(iter.next());
    // }
    // 
    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("WestVirginia"));
    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 37 with StatementMetadata

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

the class SimpleMongoDBStorageStrategy method deserializeDBObject.

@Override
public RyaStatement deserializeDBObject(final DBObject queryResult) {
    final Map<?, ?> result = queryResult.toMap();
    final String subject = (String) result.get(SUBJECT);
    final String object = (String) result.get(OBJECT);
    final String objectType = (String) result.get(OBJECT_TYPE);
    final String predicate = (String) result.get(PREDICATE);
    final String context = (String) result.get(CONTEXT);
    DocumentVisibility documentVisibility = null;
    try {
        documentVisibility = DocumentVisibilityAdapter.toDocumentVisibility(queryResult);
    } catch (final MalformedDocumentVisibilityException e) {
        throw new RuntimeException("Unable to convert document visibility", e);
    }
    final Long timestamp = (Long) result.get(TIMESTAMP);
    final String statementMetadata = (String) result.get(STATEMENT_METADATA);
    RyaType objectRya = null;
    if (objectType.equalsIgnoreCase(ANYURI.stringValue())) {
        objectRya = new RyaURI(object);
    } else {
        objectRya = new RyaType(factory.createURI(objectType), object);
    }
    final RyaStatement statement;
    if (!context.isEmpty()) {
        statement = new RyaStatement(new RyaURI(subject), new RyaURI(predicate), objectRya, new RyaURI(context));
    } else {
        statement = new RyaStatement(new RyaURI(subject), new RyaURI(predicate), objectRya);
    }
    statement.setColumnVisibility(documentVisibility.flatten());
    if (timestamp != null) {
        statement.setTimestamp(timestamp);
    }
    if (statementMetadata != null) {
        try {
            final StatementMetadata metadata = new StatementMetadata(statementMetadata);
            statement.setStatementMetadata(metadata);
        } catch (final Exception ex) {
            LOG.debug("Error deserializing metadata for statement", ex);
        }
    }
    return statement;
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI) MalformedDocumentVisibilityException(org.apache.rya.mongodb.document.visibility.DocumentVisibilityAdapter.MalformedDocumentVisibilityException) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) RyaStatement(org.apache.rya.api.domain.RyaStatement) DocumentVisibility(org.apache.rya.mongodb.document.visibility.DocumentVisibility) RyaType(org.apache.rya.api.domain.RyaType) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MalformedDocumentVisibilityException(org.apache.rya.mongodb.document.visibility.DocumentVisibilityAdapter.MalformedDocumentVisibilityException)

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