use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.
the class JoinSegment method getJoinArgs.
/**
* @param tupleExpr
* - the query object that will be traversed by this method
* @param joinArgs
* - all nodes connected by Joins and Filters
* @return - List containing all nodes connected by Joins, LeftJoins, and
* Filters. This List contains the
* @param ValueExpr
* in place of the Filter
*/
private List<QueryModelNode> getJoinArgs(TupleExpr tupleExpr, List<QueryModelNode> joinArgs) {
if (tupleExpr instanceof Join) {
if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern) && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) {
Join join = (Join) tupleExpr;
getJoinArgs(join.getRightArg(), joinArgs);
getJoinArgs(join.getLeftArg(), joinArgs);
}
} else if (tupleExpr instanceof Filter) {
Filter filter = (Filter) tupleExpr;
joinArgs.add(filter.getCondition());
conditionMap.put(filter.getCondition(), filter);
getJoinArgs(filter.getArg(), joinArgs);
} else {
joinArgs.add(tupleExpr);
}
return joinArgs;
}
use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern 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.rdftriplestore.utils.FixedStatementPattern 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.rdftriplestore.utils.FixedStatementPattern 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.rdftriplestore.utils.FixedStatementPattern 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);
}
Aggregations