use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class HasSelfVisitorTest method testTypePattern.
@Test
public void testTypePattern() throws Exception {
final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
final Set<URI> narcissistProps = new HashSet<>();
narcissistProps.add(love);
when(inferenceEngine.getHasSelfImplyingType(narcissist)).thenReturn(narcissistProps);
final Var subj = new Var("s");
final Var obj = new Var("o", narcissist);
obj.setConstant(true);
final Var pred = new Var("p", RDF.TYPE);
pred.setConstant(true);
final Projection query = new Projection(new StatementPattern(subj, pred, obj), new ProjectionElemList(new ProjectionElem("s", "subject")));
query.visit(new HasSelfVisitor(conf, inferenceEngine));
Assert.assertTrue(query.getArg() instanceof Union);
final Union union = (Union) query.getArg();
Assert.assertTrue(union.getRightArg() instanceof StatementPattern);
Assert.assertTrue(union.getLeftArg() instanceof StatementPattern);
final StatementPattern expectedLeft = new StatementPattern(subj, pred, obj);
final StatementPattern expectedRight = new StatementPattern(subj, new Var("urn:love", love), subj);
Assert.assertEquals(expectedLeft, union.getLeftArg());
Assert.assertEquals(expectedRight, union.getRightArg());
}
use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class IntersectionOfVisitorTest method testIntersectionOfDisabled.
@Test
public void testIntersectionOfDisabled() throws Exception {
// Configure a mock instance engine with an ontology:
final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
final Map<Resource, List<Set<Resource>>> intersections = new HashMap<>();
final List<Set<Resource>> motherIntersections = Arrays.asList(Sets.newHashSet(ANIMAL, FEMALE, PARENT), Sets.newHashSet(FEMALE, LEADER, NUN));
final List<Set<Resource>> fatherIntersections = Arrays.asList(Sets.newHashSet(MAN, PARENT));
intersections.put(MOTHER, motherIntersections);
when(inferenceEngine.getIntersectionsImplying(MOTHER)).thenReturn(motherIntersections);
when(inferenceEngine.getIntersectionsImplying(FATHER)).thenReturn(fatherIntersections);
// 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", MOTHER)), new ProjectionElemList(new ProjectionElem("s", "subject")));
final AccumuloRdfConfiguration disabledConf = conf.clone();
disabledConf.setInferIntersectionOf(false);
query.visit(new IntersectionOfVisitor(disabledConf, inferenceEngine));
// Expected structure: the original statement:
assertTrue(query.getArg() instanceof StatementPattern);
final StatementPattern actualMotherSp = (StatementPattern) query.getArg();
final StatementPattern expectedMotherSp = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", MOTHER));
assertEquals(expectedMotherSp, actualMotherSp);
// Query for a specific type and rewrite using the visitor:
final Projection query2 = new Projection(new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", FATHER)), new ProjectionElemList(new ProjectionElem("s", "subject")));
query2.visit(new IntersectionOfVisitor(disabledConf, inferenceEngine));
// Expected structure: the original statement:
assertTrue(query2.getArg() instanceof StatementPattern);
final StatementPattern actualFatherSp = (StatementPattern) query2.getArg();
final StatementPattern expectedFatherSp = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", FATHER));
assertEquals(expectedFatherSp, actualFatherSp);
}
use of org.openrdf.query.algebra.Projection 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.openrdf.query.algebra.Projection 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)));
}
use of org.openrdf.query.algebra.Projection in project incubator-rya by apache.
the class OneOfVisitorTest method testOneOfDisabled.
@Test
public void testOneOfDisabled() throws Exception {
// Configure a mock instance engine with an ontology:
final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
when(inferenceEngine.isEnumeratedType(SUITS)).thenReturn(true);
when(inferenceEngine.getEnumeration(SUITS)).thenReturn(CARD_SUIT_ENUMERATION);
when(inferenceEngine.isEnumeratedType(RANKS)).thenReturn(true);
when(inferenceEngine.getEnumeration(RANKS)).thenReturn(CARD_RANK_ENUMERATION);
// Query for a Suits and rewrite using the visitor:
final Projection query = new Projection(new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", SUITS)), new ProjectionElemList(new ProjectionElem("s", "subject")));
final AccumuloRdfConfiguration disabledConf = conf.clone();
disabledConf.setInferOneOf(false);
query.visit(new OneOfVisitor(disabledConf, inferenceEngine));
// Expected structure: the original statement:
assertTrue(query.getArg() instanceof StatementPattern);
final StatementPattern actualCardSuitSp = (StatementPattern) query.getArg();
final StatementPattern expectedCardSuitSp = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", SUITS));
assertEquals(expectedCardSuitSp, actualCardSuitSp);
}
Aggregations