Search in sources :

Example 96 with Statement

use of org.openrdf.model.Statement in project incubator-rya by apache.

the class RdfCloudTripleStoreConnectionTest method testDuplicateLiterals.

public void testDuplicateLiterals() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    URI loadPerc = vf.createURI(litdupsNS, "loadPerc");
    Literal lit1 = vf.createLiteral(0.0);
    Literal lit2 = vf.createLiteral(0.0);
    Literal lit3 = vf.createLiteral(0.0);
    conn.add(cpu, loadPerc, lit1);
    conn.add(cpu, loadPerc, lit2);
    conn.add(cpu, loadPerc, lit3);
    conn.commit();
    RepositoryResult<Statement> result = conn.getStatements(cpu, loadPerc, null, true, new Resource[0]);
    int count = 0;
    while (result.hasNext()) {
        count++;
        result.next();
    }
    result.close();
    assertEquals(1, count);
    // clean up
    conn.remove(cpu, loadPerc, lit1);
    conn.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) Statement(org.openrdf.model.Statement) Literal(org.openrdf.model.Literal) URI(org.openrdf.model.URI)

Example 97 with Statement

use of org.openrdf.model.Statement in project incubator-rya by apache.

the class RdfCloudTripleStoreConnectionTest method testNamedGraphLoad2.

public void testNamedGraphLoad2() throws Exception {
    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("namedgraphs.trig");
    assertNotNull(stream);
    RepositoryConnection conn = repository.getConnection();
    conn.add(stream, "", RDFFormat.TRIG);
    conn.commit();
    RepositoryResult<Statement> statements = conn.getStatements(null, vf.createURI("http://www.example.org/vocabulary#name"), null, true, vf.createURI("http://www.example.org/exampleDocument#G1"));
    int count = 0;
    while (statements.hasNext()) {
        statements.next();
        count++;
    }
    statements.close();
    assertEquals(1, count);
    conn.close();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) InputStream(java.io.InputStream) Statement(org.openrdf.model.Statement)

Example 98 with Statement

use of org.openrdf.model.Statement in project incubator-rya by apache.

the class RdfCloudTripleStoreConnectionTest method getSparqlUpdate.

private static String getSparqlUpdate() throws Exception {
    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("namedgraphs.trig");
    assertNotNull(stream);
    Model m = Rio.parse(stream, "", RDFFormat.TRIG);
    StringBuffer updateStr = new StringBuffer();
    updateStr.append("INSERT DATA {\n");
    for (Statement s : m) {
        if (s.getContext() != null) {
            updateStr.append("graph ");
            updateStr.append(escape(s.getContext()));
            updateStr.append("{ ");
        }
        updateStr.append(escape(s.getSubject()));
        updateStr.append(" ");
        updateStr.append(escape(s.getPredicate()));
        updateStr.append(" ");
        updateStr.append(escape(s.getObject()));
        if (s.getContext() != null) {
            updateStr.append("}");
        }
        updateStr.append(" . \n");
    }
    updateStr.append("}");
    return updateStr.toString();
}
Also used : InputStream(java.io.InputStream) Statement(org.openrdf.model.Statement) Model(org.openrdf.model.Model)

Example 99 with Statement

use of org.openrdf.model.Statement in project incubator-rya by apache.

the class HasValueVisitorTest method testRewriteValuePattern.

@Test
public void testRewriteValuePattern() throws Exception {
    // Configure a mock inference engine with an ontology:
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    Map<Resource, Set<Value>> typeToCharacteristic = new HashMap<>();
    Set<Value> chordateCharacteristics = new HashSet<>();
    Set<Value> vertebrateCharacteristics = new HashSet<>();
    chordateCharacteristics.add(notochord);
    vertebrateCharacteristics.addAll(chordateCharacteristics);
    vertebrateCharacteristics.add(skull);
    typeToCharacteristic.put(chordate, chordateCharacteristics);
    typeToCharacteristic.put(tunicate, chordateCharacteristics);
    typeToCharacteristic.put(vertebrate, vertebrateCharacteristics);
    typeToCharacteristic.put(mammal, vertebrateCharacteristics);
    when(inferenceEngine.getHasValueByProperty(hasCharacteristic)).thenReturn(typeToCharacteristic);
    // Query for a specific type and rewrite using the visitor:
    final Projection query = new Projection(new StatementPattern(new Var("s"), new Var("p", hasCharacteristic), new Var("o")), new ProjectionElemList(new ProjectionElem("s", "subject"), new ProjectionElem("o", "characteristic")));
    query.visit(new HasValueVisitor(conf, inferenceEngine));
    // Expected structure: Union(Join(FSP, SP), [original SP])
    Assert.assertTrue(query.getArg() instanceof Union);
    final Union union = (Union) query.getArg();
    final StatementPattern originalSP = new StatementPattern(new Var("s"), new Var("p", hasCharacteristic), new Var("o"));
    Join join;
    if (union.getLeftArg() instanceof Join) {
        join = (Join) union.getLeftArg();
        Assert.assertEquals(originalSP, union.getRightArg());
    } else {
        Assert.assertTrue(union.getRightArg() instanceof Join);
        join = (Join) union.getRightArg();
        Assert.assertEquals(originalSP, union.getLeftArg());
    }
    Assert.assertTrue(join.getLeftArg() instanceof FixedStatementPattern);
    Assert.assertTrue(join.getRightArg() instanceof StatementPattern);
    final FixedStatementPattern fsp = (FixedStatementPattern) join.getLeftArg();
    final StatementPattern sp = (StatementPattern) join.getRightArg();
    // Verify join: FSP{ ?t _ ?originalObjectVar } JOIN { ?originalSubjectVar rdf:type ?t }
    Assert.assertEquals(originalSP.getSubjectVar(), sp.getSubjectVar());
    Assert.assertEquals(RDF.TYPE, sp.getPredicateVar().getValue());
    Assert.assertEquals(fsp.getSubjectVar(), sp.getObjectVar());
    Assert.assertEquals(originalSP.getObjectVar(), fsp.getObjectVar());
    // Verify FSP: should provide (type, value) pairs
    final Set<Statement> expectedStatements = new HashSet<>();
    final URI fspPred = (URI) fsp.getPredicateVar().getValue();
    expectedStatements.add(vf.createStatement(chordate, fspPred, notochord));
    expectedStatements.add(vf.createStatement(tunicate, fspPred, notochord));
    expectedStatements.add(vf.createStatement(vertebrate, fspPred, notochord));
    expectedStatements.add(vf.createStatement(mammal, fspPred, notochord));
    expectedStatements.add(vf.createStatement(vertebrate, fspPred, skull));
    expectedStatements.add(vf.createStatement(mammal, fspPred, skull));
    final Set<Statement> actualStatements = new HashSet<>(fsp.statements);
    Assert.assertEquals(expectedStatements, actualStatements);
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) HashSet(java.util.HashSet) Set(java.util.Set) HasValueVisitor(org.apache.rya.rdftriplestore.inference.HasValueVisitor) HashMap(java.util.HashMap) Var(org.openrdf.query.algebra.Var) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) Projection(org.openrdf.query.algebra.Projection) Join(org.openrdf.query.algebra.Join) URI(org.openrdf.model.URI) Union(org.openrdf.query.algebra.Union) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) StatementPattern(org.openrdf.query.algebra.StatementPattern) InferenceEngine(org.apache.rya.rdftriplestore.inference.InferenceEngine) Value(org.openrdf.model.Value) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 100 with Statement

use of org.openrdf.model.Statement in project incubator-rya by apache.

the class IteratorFactory method getIterator.

public static CloseableIteration<BindingSet, QueryEvaluationException> getIterator(final StatementPattern match, final BindingSet bindings, final String queryText, final SearchFunction searchFunction) {
    return new CloseableIteration<BindingSet, QueryEvaluationException>() {

        private boolean isClosed = false;

        private CloseableIteration<Statement, QueryEvaluationException> statementIt = null;

        private String subjectBinding = match.getSubjectVar().getName();

        private String predicateBinding = match.getPredicateVar().getName();

        private String objectBinding = match.getObjectVar().getName();

        private String contextBinding = null;

        private void performQuery() throws QueryEvaluationException {
            StatementConstraints contraints = new StatementConstraints();
            // get the context (i.e. named graph) of the statement and use that in the query
            QueryModelNode parentNode = match.getSubjectVar().getParentNode();
            if (parentNode instanceof StatementPattern) {
                StatementPattern parentStatement = (StatementPattern) parentNode;
                Var contextVar = parentStatement.getContextVar();
                if (contextVar != null) {
                    contextBinding = contextVar.getName();
                    Resource context = (Resource) contextVar.getValue();
                    contraints.setContext(context);
                }
            }
            // get the subject constraint
            if (match.getSubjectVar().isConstant()) {
                // get the subject binding from the filter/statement pair
                Resource subject = (Resource) match.getSubjectVar().getValue();
                contraints.setSubject(subject);
            } else if (bindings.hasBinding(subjectBinding)) {
                // get the subject binding from the passed in bindings (eg from other statements/parts of the tree)
                Resource subject = (Resource) bindings.getValue(subjectBinding);
                contraints.setSubject(subject);
            }
            // get the predicate constraint
            if (match.getPredicateVar().isConstant()) {
                // get the predicate binding from the filter/statement pair
                Set<URI> predicates = new HashSet<URI>(getPredicateRestrictions(match.getPredicateVar()));
                contraints.setPredicates(predicates);
            } else if (bindings.hasBinding(predicateBinding)) {
                // get the predicate binding from the passed in bindings (eg from other statements/parts of the tree)
                URI predicateUri = (URI) bindings.getValue(predicateBinding);
                Set<URI> predicates = Collections.singleton(predicateUri);
                contraints.setPredicates(predicates);
            }
            statementIt = searchFunction.performSearch(queryText, contraints);
        }

        @Override
        public boolean hasNext() throws QueryEvaluationException {
            if (statementIt == null) {
                performQuery();
            }
            return statementIt.hasNext();
        }

        @Override
        public BindingSet next() throws QueryEvaluationException {
            if (!hasNext() || isClosed) {
                throw new NoSuchElementException();
            }
            Statement statment = statementIt.next();
            MapBindingSet bset = new MapBindingSet();
            if (!subjectBinding.startsWith("-const"))
                bset.addBinding(subjectBinding, statment.getSubject());
            if (!predicateBinding.startsWith("-const"))
                bset.addBinding(predicateBinding, statment.getPredicate());
            if (!objectBinding.startsWith("-const"))
                bset.addBinding(objectBinding, statment.getObject());
            if (contextBinding != null && !contextBinding.startsWith("-const"))
                bset.addBinding(contextBinding, statment.getContext());
            // merge with other bindings.
            for (String name : bindings.getBindingNames()) {
                bset.addBinding(name, bindings.getValue(name));
            }
            return bset;
        }

        @Override
        public void remove() throws QueryEvaluationException {
            throw new UnsupportedOperationException();
        }

        @Override
        public void close() throws QueryEvaluationException {
            if (statementIt != null) {
                statementIt.close();
            }
            isClosed = true;
        }
    };
}
Also used : Set(java.util.Set) MapBindingSet(org.openrdf.query.impl.MapBindingSet) HashSet(java.util.HashSet) BindingSet(org.openrdf.query.BindingSet) Var(org.openrdf.query.algebra.Var) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) URI(org.openrdf.model.URI) CloseableIteration(info.aduna.iteration.CloseableIteration) StatementPattern(org.openrdf.query.algebra.StatementPattern) MapBindingSet(org.openrdf.query.impl.MapBindingSet) NoSuchElementException(java.util.NoSuchElementException) HashSet(java.util.HashSet)

Aggregations

Statement (org.openrdf.model.Statement)359 Test (org.junit.Test)209 ValueFactory (org.openrdf.model.ValueFactory)114 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)106 URI (org.openrdf.model.URI)96 HashSet (java.util.HashSet)88 Resource (org.openrdf.model.Resource)69 Value (org.openrdf.model.Value)67 BindingSet (org.openrdf.query.BindingSet)60 RyaStatement (org.apache.rya.api.domain.RyaStatement)56 RdfToRyaConversions.convertStatement (org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement)54 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)53 MapBindingSet (org.openrdf.query.impl.MapBindingSet)43 ArrayList (java.util.ArrayList)41 LiteralImpl (org.openrdf.model.impl.LiteralImpl)40 StatementImpl (org.openrdf.model.impl.StatementImpl)40 ContextStatementImpl (org.openrdf.model.impl.ContextStatementImpl)36 URIImpl (org.openrdf.model.impl.URIImpl)30 LinearRing (com.vividsolutions.jts.geom.LinearRing)24 Polygon (com.vividsolutions.jts.geom.Polygon)24