use of org.openrdf.query.algebra.Union in project incubator-rya by apache.
the class ReflexivePropertyVisitorTest method testReflexiveProperty.
@Test
public void testReflexiveProperty() throws Exception {
// Define a reflexive property
final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
when(inferenceEngine.isReflexiveProperty(HAS_FAMILY)).thenReturn(true);
// Construct a query, then visit it
final StatementPattern sp = new StatementPattern(new Var("s", ALICE), new Var("p", HAS_FAMILY), new Var("o"));
final Projection query = new Projection(sp, new ProjectionElemList(new ProjectionElem("o", "member")));
query.visit(new ReflexivePropertyVisitor(conf, inferenceEngine));
// Expected structure after rewriting SP(:Alice :hasFamilyMember ?member):
//
// Union(
// originalSP(:Alice :hasFamilyMember ?member),
// ZeroLengthPath(:Alice, ?member)
// )
Assert.assertTrue(query.getArg() instanceof Union);
final TupleExpr left = ((Union) query.getArg()).getLeftArg();
final TupleExpr right = ((Union) query.getArg()).getRightArg();
Assert.assertEquals(sp, left);
Assert.assertTrue(right instanceof ZeroLengthPath);
Assert.assertEquals(sp.getSubjectVar(), ((ZeroLengthPath) right).getSubjectVar());
Assert.assertEquals(sp.getObjectVar(), ((ZeroLengthPath) right).getObjectVar());
}
use of org.openrdf.query.algebra.Union in project incubator-rya by apache.
the class SomeValuesFromVisitorTest method testSomeValuesFrom.
@Test
public void testSomeValuesFrom() throws Exception {
// Configure a mock instance engine with an ontology:
final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
Map<Resource, Set<URI>> personSVF = new HashMap<>();
personSVF.put(gradCourse, Sets.newHashSet(takesCourse));
personSVF.put(course, Sets.newHashSet(takesCourse));
personSVF.put(department, Sets.newHashSet(headOf));
personSVF.put(organization, Sets.newHashSet(worksFor, headOf));
when(inferenceEngine.getSomeValuesFromByRestrictionType(person)).thenReturn(personSVF);
// 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 SomeValuesFromVisitor(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/value type combinations
// with another join querying for any nodes who are the subject of a triple with that
// predicate and with an object of that type.
//
// Union(
// SP(?node a :impliedType),
// Join(
// FSP(<?property someValuesFrom ?valueType> {
// takesCourse/Course;
// takesCourse/GraduateCourse;
// headOf/Department;
// headOf/Organization;
// worksFor/Organization;
// }),
// Join(
// SP(_:object a ?valueType),
// SP(?node ?property _:object)
// )
// )
Assert.assertTrue(query.getArg() instanceof Union);
TupleExpr left = ((Union) query.getArg()).getLeftArg();
TupleExpr right = ((Union) query.getArg()).getRightArg();
Assert.assertEquals(originalSP, left);
Assert.assertTrue(right instanceof Join);
final Join join = (Join) right;
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/type pairs
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(takesCourse, OWL.SOMEVALUESFROM, course)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(takesCourse, OWL.SOMEVALUESFROM, gradCourse)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(headOf, OWL.SOMEVALUESFROM, department)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(headOf, OWL.SOMEVALUESFROM, organization)));
Assert.assertTrue(fsp.statements.contains(new NullableStatementImpl(worksFor, OWL.SOMEVALUESFROM, organization)));
Assert.assertEquals(5, fsp.statements.size());
// Verify pattern for matching instances of each pair: a Join of <_:x rdf:type ?t> and
// <?s ?p _:x> where p and t are the predicate/type pair and s is the original subject
// variable.
StatementPattern leftSP = (StatementPattern) left;
StatementPattern rightSP = (StatementPattern) right;
Assert.assertEquals(rightSP.getObjectVar(), leftSP.getSubjectVar());
Assert.assertEquals(RDF.TYPE, leftSP.getPredicateVar().getValue());
Assert.assertEquals(fsp.getObjectVar(), leftSP.getObjectVar());
Assert.assertEquals(originalSP.getSubjectVar(), rightSP.getSubjectVar());
Assert.assertEquals(fsp.getSubjectVar(), rightSP.getPredicateVar());
}
use of org.openrdf.query.algebra.Union in project incubator-rya by apache.
the class IntersectionOfVisitor method meetSP.
@Override
protected void meetSP(final StatementPattern node) throws Exception {
final StatementPattern currentNode = node.clone();
final Var subVar = node.getSubjectVar();
final Var predVar = node.getPredicateVar();
final Var objVar = node.getObjectVar();
final Var conVar = node.getContextVar();
if (predVar != null && objVar != null && objVar.getValue() != null && RDF.TYPE.equals(predVar.getValue()) && !EXPANDED.equals(conVar)) {
final List<Set<Resource>> intersections = inferenceEngine.getIntersectionsImplying((URI) objVar.getValue());
if (intersections != null && !intersections.isEmpty()) {
final List<TupleExpr> joins = new ArrayList<>();
for (final Set<Resource> intersection : intersections) {
final Set<Resource> sortedIntersection = new TreeSet<>(new ResourceComparator());
sortedIntersection.addAll(intersection);
// Create a join tree of all statement patterns in the
// current intersection.
final TupleExpr joinTree = createJoinTree(new ArrayList<>(sortedIntersection), subVar, conVar);
if (joinTree != null) {
joins.add(joinTree);
}
}
if (!joins.isEmpty()) {
// Combine all the intersection join trees for the type
// together into a union tree. This will be a join tree if
// only one intersection exists.
final TupleExpr unionTree = createUnionTree(joins);
// Union the above union tree of intersections with the
// original node.
final Union union = new InferUnion(unionTree, currentNode);
node.replaceWith(union);
log.trace("Replacing node with inferred intersection union: " + union);
}
}
}
}
use of org.openrdf.query.algebra.Union in project incubator-rya by apache.
the class SymmetricPropertyVisitor method meetSP.
@Override
protected void meetSP(StatementPattern node) throws Exception {
StatementPattern sp = node.clone();
final Var predVar = sp.getPredicateVar();
URI pred = (URI) predVar.getValue();
String predNamespace = pred.getNamespace();
final Var objVar = sp.getObjectVar();
final Var cntxtVar = sp.getContextVar();
if (objVar != null && !RDF.NAMESPACE.equals(predNamespace) && !SESAME.NAMESPACE.equals(predNamespace) && !RDFS.NAMESPACE.equals(predNamespace) && !EXPANDED.equals(cntxtVar)) {
/**
* { ?a ?pred ?b .}\n" +
* " UNION " +
* " { ?b ?pred ?a }
*/
URI symmPropUri = (URI) predVar.getValue();
if (inferenceEngine.isSymmetricProperty(symmPropUri)) {
Var subjVar = sp.getSubjectVar();
Union union = new InferUnion();
union.setLeftArg(sp);
union.setRightArg(new StatementPattern(objVar, predVar, subjVar, cntxtVar));
node.replaceWith(union);
}
}
}
use of org.openrdf.query.algebra.Union 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());
}
Aggregations