use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.
the class FlattenedOptional method getJoinArgs.
/**
* This method is used to retrieve a set view of all descendants of the
* rightArg of the LeftJoin (the optional part)
*
* @param tupleExpr
* - tupleExpr whose args are being retrieved
* @param joinArgs
* - set view of all non-join args that are descendants of
* tupleExpr
* @return joinArgs
*/
private Set<TupleExpr> getJoinArgs(TupleExpr tupleExpr, Set<TupleExpr> joinArgs) {
if (tupleExpr instanceof Join) {
if (!(((Join) tupleExpr).getLeftArg() instanceof FixedStatementPattern) && !(((Join) tupleExpr).getRightArg() instanceof DoNotExpandSP)) {
Join join = (Join) tupleExpr;
getJoinArgs(join.getLeftArg(), joinArgs);
getJoinArgs(join.getRightArg(), joinArgs);
}
} else if (tupleExpr instanceof LeftJoin) {
// TODO probably not
// necessary if not
// including leftarg
LeftJoin lj = (LeftJoin) tupleExpr;
joinArgs.add(new FlattenedOptional(lj));
getJoinArgs(lj.getLeftArg(), joinArgs);
} else if (tupleExpr instanceof Filter) {
getJoinArgs(((Filter) tupleExpr).getArg(), joinArgs);
} else {
joinArgs.add(tupleExpr);
}
return joinArgs;
}
use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.
the class OptionalJoinSegment method getJoinArgs.
/**
* @param tupleExpr
* - the query object that will be traversed by this method
* @param joinArgs
* - all nodes connected by Joins, LeftJoins, and Filters
* @return - List containing all nodes connected by Joins, LeftJoins, and
* Filters. This List contains the {@link ValueExpr} in place of the
* Filter and a {@link FlattenedOptional} in place of the LeftJoin
* for ease of comparison with PCJ nodes.
*/
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 LeftJoin) {
LeftJoin lj = (LeftJoin) tupleExpr;
joinArgs.add(new FlattenedOptional(lj));
getJoinArgs(lj.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 DomainRangeVisitorTest method testRewriteTypePattern.
@Test
public void testRewriteTypePattern() throws Exception {
final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
final Set<URI> domainPredicates = new HashSet<>();
final Set<URI> rangePredicates = new HashSet<>();
domainPredicates.add(advisor);
domainPredicates.add(takesCourse);
rangePredicates.add(advisor);
when(inferenceEngine.getPropertiesWithDomain(person)).thenReturn(domainPredicates);
when(inferenceEngine.getPropertiesWithRange(person)).thenReturn(rangePredicates);
final Var subjVar = new Var("s");
final StatementPattern originalSP = new StatementPattern(subjVar, new Var("p", RDF.TYPE), new Var("o", person));
final Projection query = new Projection(originalSP, new ProjectionElemList(new ProjectionElem("s", "subject")));
query.visit(new DomainRangeVisitor(conf, inferenceEngine));
// Resulting tree should consist of Unions of:
// 1. The original StatementPattern
// 2. A join checking for domain inference
// 3. A join checking for range inference
boolean containsOriginal = false;
boolean containsDomain = false;
boolean containsRange = false;
final Stack<TupleExpr> nodes = new Stack<>();
nodes.push(query.getArg());
while (!nodes.isEmpty()) {
final TupleExpr currentNode = nodes.pop();
if (currentNode instanceof Union) {
nodes.push(((Union) currentNode).getLeftArg());
nodes.push(((Union) currentNode).getRightArg());
} else if (currentNode instanceof StatementPattern) {
Assert.assertFalse(containsOriginal);
Assert.assertEquals(originalSP, currentNode);
containsOriginal = true;
} else if (currentNode instanceof Join) {
final TupleExpr left = ((Join) currentNode).getLeftArg();
final TupleExpr right = ((Join) currentNode).getRightArg();
Assert.assertTrue("Left-hand side should enumerate domain/range predicates", left instanceof FixedStatementPattern);
Assert.assertTrue("Right-hand side should be a non-expandable SP matching triples satisfying domain/range", right instanceof DoNotExpandSP);
final FixedStatementPattern fsp = (FixedStatementPattern) left;
final StatementPattern sp = (StatementPattern) right;
// fsp should be <predicate var, domain/range, original type var>
boolean isDomain = RDFS.DOMAIN.equals(fsp.getPredicateVar().getValue());
boolean isRange = RDFS.RANGE.equals(fsp.getPredicateVar().getValue());
Assert.assertTrue(isDomain || isRange);
Assert.assertEquals(originalSP.getObjectVar(), fsp.getObjectVar());
// sp should have same predicate var
Assert.assertEquals(fsp.getSubjectVar(), sp.getPredicateVar());
// collect predicates that are enumerated in the FixedStatementPattern
final Set<Resource> queryPredicates = new HashSet<>();
for (Statement statement : fsp.statements) {
queryPredicates.add(statement.getSubject());
}
if (isDomain) {
Assert.assertFalse(containsDomain);
// sp should be <original subject var, predicate var, unbound object var> for domain
Assert.assertEquals(originalSP.getSubjectVar(), sp.getSubjectVar());
Assert.assertFalse(sp.getObjectVar().hasValue());
// verify predicates
Assert.assertEquals(2, fsp.statements.size());
Assert.assertEquals(domainPredicates, queryPredicates);
Assert.assertTrue(queryPredicates.contains(advisor));
Assert.assertTrue(queryPredicates.contains(takesCourse));
containsDomain = true;
} else {
Assert.assertFalse(containsRange);
// sp should be <unbound subject var, predicate var, original subject var> for range
Assert.assertFalse(sp.getSubjectVar().hasValue());
Assert.assertEquals(originalSP.getSubjectVar(), sp.getObjectVar());
// verify predicates
Assert.assertEquals(1, fsp.statements.size());
Assert.assertEquals(rangePredicates, queryPredicates);
Assert.assertTrue(queryPredicates.contains(advisor));
containsRange = true;
}
} else {
Assert.fail("Expected nested Unions of Joins and StatementPatterns, found: " + currentNode.toString());
}
}
Assert.assertTrue(containsOriginal && containsDomain && containsRange);
}
use of org.apache.rya.rdftriplestore.utils.FixedStatementPattern in project incubator-rya by apache.
the class HasValueVisitorTest method testRewriteTypePattern.
@Test
public void testRewriteTypePattern() throws Exception {
// Configure a mock instance engine with an ontology:
final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
Map<URI, Set<Value>> vertebrateValues = new HashMap<>();
vertebrateValues.put(hasCharacteristic, new HashSet<>());
vertebrateValues.put(belongsTo, new HashSet<>());
vertebrateValues.get(hasCharacteristic).add(notochord);
vertebrateValues.get(hasCharacteristic).add(skull);
vertebrateValues.get(belongsTo).add(chordata);
when(inferenceEngine.getHasValueByType(vertebrate)).thenReturn(vertebrateValues);
// Query for a specific type and rewrite using the visitor:
final Projection query = new Projection(new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", vertebrate)), new ProjectionElemList(new ProjectionElem("s", "subject")));
query.visit(new HasValueVisitor(conf, inferenceEngine));
// Expected structure: two nested unions whose members are (in some order) the original
// statement pattern and two joins, one for each unique property involved in a relevant
// restriction. Each join should be between a StatementPattern for the property and a
// FixedStatementPattern providing the value(s).
// Collect the arguments to the unions, ignoring nesting order:
Assert.assertTrue(query.getArg() instanceof Union);
final Union union1 = (Union) query.getArg();
final Set<TupleExpr> unionArgs = new HashSet<>();
if (union1.getLeftArg() instanceof Union) {
unionArgs.add(((Union) union1.getLeftArg()).getLeftArg());
unionArgs.add(((Union) union1.getLeftArg()).getRightArg());
unionArgs.add(union1.getRightArg());
} else {
Assert.assertTrue(union1.getRightArg() instanceof Union);
unionArgs.add(union1.getLeftArg());
unionArgs.add(((Union) union1.getRightArg()).getLeftArg());
unionArgs.add(((Union) union1.getRightArg()).getRightArg());
}
// There should be one StatementPattern and two joins with structure Join(FSP, SP):
final StatementPattern directSP = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", vertebrate));
StatementPattern actualSP = null;
FixedStatementPattern hasCharacteristicFSP = null;
FixedStatementPattern belongsToFSP = null;
for (TupleExpr arg : unionArgs) {
if (arg instanceof StatementPattern) {
actualSP = (StatementPattern) arg;
} else {
Assert.assertTrue(arg instanceof Join);
final Join join = (Join) arg;
Assert.assertTrue(join.getLeftArg() instanceof FixedStatementPattern);
Assert.assertTrue(join.getRightArg() instanceof StatementPattern);
final FixedStatementPattern fsp = (FixedStatementPattern) join.getLeftArg();
final StatementPattern sp = (StatementPattern) join.getRightArg();
// Should join FSP([unused], property, ?value) with SP(subject, property, ?value)
Assert.assertEquals(directSP.getSubjectVar(), sp.getSubjectVar());
Assert.assertEquals(fsp.getPredicateVar(), sp.getPredicateVar());
Assert.assertEquals(fsp.getObjectVar(), sp.getObjectVar());
if (hasCharacteristic.equals(fsp.getPredicateVar().getValue())) {
hasCharacteristicFSP = fsp;
} else if (belongsTo.equals(fsp.getPredicateVar().getValue())) {
belongsToFSP = fsp;
} else {
Assert.fail("Unexpected property variable in rewritten query: " + fsp.getPredicateVar());
}
}
}
Assert.assertEquals(directSP, actualSP);
Assert.assertNotNull(hasCharacteristicFSP);
Assert.assertNotNull(belongsToFSP);
// Verify the expected FSPs for the appropriate properties:
Assert.assertEquals(2, hasCharacteristicFSP.statements.size());
Assert.assertTrue(hasCharacteristicFSP.statements.contains(vf.createStatement(vertebrate, hasCharacteristic, skull)));
Assert.assertTrue(hasCharacteristicFSP.statements.contains(vf.createStatement(vertebrate, hasCharacteristic, notochord)));
Assert.assertEquals(1, belongsToFSP.statements.size());
Assert.assertTrue(belongsToFSP.statements.contains(vf.createStatement(vertebrate, belongsTo, chordata)));
}
Aggregations