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();
}
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();
}
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();
}
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);
}
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;
}
};
}
Aggregations