use of org.apache.rya.api.utils.NullableStatementImpl in project incubator-rya by apache.
the class AccumuloQueryRuleset method getRange.
/**
* Turn a single StatementPattern into a Range.
* @param conf
* @throws IOException if the range can't be resolved
*/
private Map.Entry<TABLE_LAYOUT, ByteRange> getRange(final StatementPattern sp) throws IOException {
final Var context = sp.getContextVar();
final Statement stmt = new NullableStatementImpl((Resource) sp.getSubjectVar().getValue(), (URI) sp.getPredicateVar().getValue(), sp.getObjectVar().getValue(), context == null ? null : (Resource) context.getValue());
final RyaStatement rs = RdfToRyaConversions.convertStatement(stmt);
final TriplePatternStrategy strategy = ryaContext.retrieveStrategy(rs);
final Map.Entry<TABLE_LAYOUT, ByteRange> entry = strategy.defineRange(rs.getSubject(), rs.getPredicate(), rs.getObject(), rs.getContext(), conf);
return entry;
}
use of org.apache.rya.api.utils.NullableStatementImpl in project incubator-rya by apache.
the class SameAsVisitor method getReplaceJoin.
private InferJoin getReplaceJoin(Set<Resource> uris, boolean subSubj, Var subjVar, Var objVar, Var predVar, Var cntxtVar) {
String s = UUID.randomUUID().toString();
Var dummyVar = new Var(s);
StatementPattern origStatement;
Var subVar;
if (subSubj) {
subVar = subjVar;
origStatement = new DoNotExpandSP(dummyVar, predVar, objVar, cntxtVar);
} else {
subVar = objVar;
origStatement = new DoNotExpandSP(subjVar, predVar, dummyVar, cntxtVar);
}
FixedStatementPattern fsp = new FixedStatementPattern(dummyVar, new Var("c-" + s, OWL.SAMEAS), subVar, cntxtVar);
for (Resource sameAs : uris) {
NullableStatementImpl newStatement = new NullableStatementImpl(sameAs, OWL.SAMEAS, (Resource) subVar.getValue(), getVarValue(cntxtVar));
fsp.statements.add(newStatement);
}
InferJoin join = new InferJoin(fsp, origStatement);
join.getProperties().put(InferConstants.INFERRED, InferConstants.TRUE);
return join;
}
use of org.apache.rya.api.utils.NullableStatementImpl in project incubator-rya by apache.
the class SubClassOfVisitor method meetSP.
@Override
protected void meetSP(final StatementPattern node) throws Exception {
final StatementPattern sp = node.clone();
final Var predVar = sp.getPredicateVar();
final Var objVar = sp.getObjectVar();
final Var conVar = sp.getContextVar();
if (predVar != null && objVar != null && objVar.getValue() != null && RDF.TYPE.equals(predVar.getValue()) && !EXPANDED.equals(conVar)) {
/**
* ?type sesame:directSubClassOf ub:Student . ?student rdf:type ?type +
*/
// String s = UUID.randomUUID().toString();
// Var typeVar = new Var(s);
// StatementPattern subClassOf = new StatementPattern(typeVar, new Var("c-" + s, SESAME.DIRECTSUBCLASSOF), objVar, SUBCLASS_EXPANDED);
// StatementPattern rdfType = new StatementPattern(sp.getSubjectVar(), sp.getPredicateVar(), typeVar, SUBCLASS_EXPANDED);
// InferJoin join = new InferJoin(subClassOf, rdfType);
// join.getProperties().put(InferConstants.INFERRED, InferConstants.TRUE);
// node.replaceWith(join);
final URI subclassof_uri = (URI) objVar.getValue();
final Collection<URI> parents = InferenceEngine.findParents(inferenceEngine.getSubClassOfGraph(), subclassof_uri);
if (parents != null && parents.size() > 0) {
final String s = UUID.randomUUID().toString();
final Var typeVar = new Var(s);
final FixedStatementPattern fsp = new FixedStatementPattern(typeVar, new Var("c-" + s, RDFS.SUBCLASSOF), objVar, conVar);
parents.add(subclassof_uri);
for (final URI u : parents) {
fsp.statements.add(new NullableStatementImpl(u, RDFS.SUBCLASSOF, subclassof_uri));
}
final StatementPattern rdfType = new DoNotExpandSP(sp.getSubjectVar(), sp.getPredicateVar(), typeVar, conVar);
final InferJoin join = new InferJoin(fsp, rdfType);
join.getProperties().put(InferConstants.INFERRED, InferConstants.TRUE);
node.replaceWith(join);
}
// if (parents != null && parents.size() > 0) {
// StatementPatterns statementPatterns = new StatementPatterns();
// statementPatterns.patterns.add(node);
// Var subjVar = node.getSubjectVar();
// for (URI u : parents) {
// statementPatterns.patterns.add(new StatementPattern(subjVar, predVar, new Var(objVar.getName(), u)));
// }
// node.replaceWith(statementPatterns);
// }
// if (parents != null && parents.size() > 0) {
// VarCollection vc = new VarCollection();
// vc.setName(objVar.getName());
// vc.values.add(objVar);
// for (URI u : parents) {
// vc.values.add(new Var(objVar.getName(), u));
// }
// Var subjVar = node.getSubjectVar();
// node.replaceWith(new StatementPattern(subjVar, predVar, vc, node.getContextVar()));
// }
}
}
use of org.apache.rya.api.utils.NullableStatementImpl in project incubator-rya by apache.
the class AllValuesFromVisitorTest method testRewriteTypePattern.
@Test
public void testRewriteTypePattern() throws Exception {
// Configure a mock instance engine with an ontology:
final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
Map<Resource, Set<URI>> personAVF = new HashMap<>();
personAVF.put(parentsAreTallPeople, new HashSet<>());
personAVF.put(parentsArePeople, new HashSet<>());
personAVF.put(relativesArePeople, new HashSet<>());
personAVF.get(parentsAreTallPeople).add(parent);
personAVF.get(parentsArePeople).add(parent);
personAVF.get(relativesArePeople).add(relative);
personAVF.get(relativesArePeople).add(parent);
Map<Resource, Set<URI>> dogAVF = new HashMap<>();
dogAVF.put(parentsAreDogs, new HashSet<>());
dogAVF.get(parentsAreDogs).add(parent);
when(inferenceEngine.getAllValuesFromByValueType(person)).thenReturn(personAVF);
when(inferenceEngine.getAllValuesFromByValueType(dog)).thenReturn(dogAVF);
// Query for a specific type and rewrite using the visitor:
StatementPattern originalSP = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", person));
final Projection query = new Projection(originalSP, new ProjectionElemList(new ProjectionElem("s", "subject")));
query.visit(new AllValuesFromVisitor(conf, inferenceEngine));
// Expected structure: a union of two elements: one is equal to the original statement
// pattern, and the other one joins a list of predicate/restriction type combinations
// with another join querying for values of that predicate for members of that type.
Assert.assertTrue(query.getArg() instanceof Union);
TupleExpr left = ((Union) query.getArg()).getLeftArg();
TupleExpr right = ((Union) query.getArg()).getRightArg();
final Join join;
if (left instanceof StatementPattern) {
Assert.assertEquals(originalSP, left);
Assert.assertTrue(right instanceof Join);
join = (Join) right;
} else {
Assert.assertEquals(originalSP, right);
Assert.assertTrue(left instanceof Join);
join = (Join) left;
}
Assert.assertTrue(join.getLeftArg() instanceof FixedStatementPattern);
Assert.assertTrue(join.getRightArg() instanceof Join);
FixedStatementPattern fsp = (FixedStatementPattern) join.getLeftArg();
left = ((Join) join.getRightArg()).getLeftArg();
right = ((Join) join.getRightArg()).getRightArg();
Assert.assertTrue(left instanceof StatementPattern);
Assert.assertTrue(right instanceof StatementPattern);
// Verify expected predicate/restriction pairs
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(parentsArePeople, OWL.ONPROPERTY, parent)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(relativesArePeople, OWL.ONPROPERTY, relative)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(relativesArePeople, OWL.ONPROPERTY, parent)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(parentsAreTallPeople, OWL.ONPROPERTY, parent)));
Assert.assertEquals(4, fsp.statements.size());
// Verify general pattern for matching instances of each pair: Join on unknown subject; left
// triple states it belongs to the restriction while right triple relates it to the original
// subject variable by the relevant property. Restriction and property variables are given
// by the FixedStatementPattern.
StatementPattern leftSP = (StatementPattern) left;
StatementPattern rightSP = (StatementPattern) right;
Assert.assertEquals(rightSP.getSubjectVar(), leftSP.getSubjectVar());
Assert.assertEquals(RDF.TYPE, leftSP.getPredicateVar().getValue());
Assert.assertEquals(fsp.getSubjectVar(), leftSP.getObjectVar());
Assert.assertEquals(fsp.getObjectVar(), rightSP.getPredicateVar());
Assert.assertEquals(originalSP.getSubjectVar(), rightSP.getObjectVar());
}
use of org.apache.rya.api.utils.NullableStatementImpl in project incubator-rya by apache.
the class SomeValuesFromVisitor method meetSP.
/**
* Checks whether the StatementPattern is a type query whose solutions could be inferred by
* someValuesFrom inference, and if so, replaces the node with a union of itself and any
* possible inference.
*/
@Override
protected void meetSP(StatementPattern node) throws Exception {
final Var subjVar = node.getSubjectVar();
final Var predVar = node.getPredicateVar();
final Var objVar = node.getObjectVar();
// Only applies to type queries where the type is defined
if (predVar != null && RDF.TYPE.equals(predVar.getValue()) && objVar != null && objVar.getValue() instanceof Resource) {
final Resource typeToInfer = (Resource) objVar.getValue();
Map<Resource, Set<URI>> relevantSvfRestrictions = inferenceEngine.getSomeValuesFromByRestrictionType(typeToInfer);
if (!relevantSvfRestrictions.isEmpty()) {
// We can infer the queried type if it is to a someValuesFrom restriction (or a
// supertype of one), and the node in question (subjVar) is the subject of a triple
// whose predicate is the restriction's property and whose object is an arbitrary
// node of the restriction's value type.
final Var valueTypeVar = new Var("t-" + UUID.randomUUID());
final Var svfPredVar = new Var("p-" + UUID.randomUUID());
final Var neighborVar = new Var("n-" + UUID.randomUUID());
neighborVar.setAnonymous(true);
final StatementPattern membershipPattern = new DoNotExpandSP(neighborVar, new Var(RDF.TYPE.stringValue(), RDF.TYPE), valueTypeVar);
final StatementPattern valuePattern = new StatementPattern(subjVar, svfPredVar, neighborVar);
final InferJoin svfPattern = new InferJoin(membershipPattern, valuePattern);
// Use a FixedStatementPattern to contain the appropriate (predicate, value type)
// pairs, and check each one against the general pattern.
final FixedStatementPattern svfPropertyTypes = new FixedStatementPattern(svfPredVar, new Var(OWL.SOMEVALUESFROM.stringValue(), OWL.SOMEVALUESFROM), valueTypeVar);
for (Resource svfValueType : relevantSvfRestrictions.keySet()) {
for (URI svfProperty : relevantSvfRestrictions.get(svfValueType)) {
svfPropertyTypes.statements.add(new NullableStatementImpl(svfProperty, OWL.SOMEVALUESFROM, svfValueType));
}
}
final InferJoin svfInferenceQuery = new InferJoin(svfPropertyTypes, svfPattern);
node.replaceWith(new InferUnion(node.clone(), svfInferenceQuery));
}
}
}
Aggregations