Search in sources :

Example 6 with ProjectionElemList

use of org.openrdf.query.algebra.ProjectionElemList in project incubator-rya by apache.

the class IntersectionOfVisitorTest method testIntersectionOf.

@Test
public void testIntersectionOf() 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")));
    query.visit(new IntersectionOfVisitor(conf, inferenceEngine));
    // Expected structure: a union whose members are the original
    // statement pattern and a nested union of statement patterns and joins.
    // This will join both intersections of Mother together. The nested
    // union tree is unioned with the original statement pattern.
    // The nested union of Mother should be:
    // Union(
    // Join(
    // SP(Animal),
    // Join(
    // SP(Female),
    // SP(Parent)
    // )
    // ),
    // Join(
    // SP(Female),
    // Join(
    // SP(Leader),
    // SP(Nun)
    // )
    // )
    // )
    // 
    // Collect the arguments to the union:
    assertTrue(query.getArg() instanceof Union);
    final Union union1 = (Union) query.getArg();
    assertTrue(union1.getLeftArg() instanceof Union);
    final Union union2 = (Union) union1.getLeftArg();
    assertTrue(union2.getLeftArg() instanceof Join);
    final Join join1 = (Join) union2.getLeftArg();
    assertTrue(join1.getLeftArg() instanceof StatementPattern);
    final StatementPattern animalSp = (StatementPattern) join1.getLeftArg();
    assertStatementPattern(animalSp, ANIMAL);
    assertTrue(join1.getRightArg() instanceof Join);
    final Join join2 = (Join) join1.getRightArg();
    assertTrue(join2.getLeftArg() instanceof StatementPattern);
    final StatementPattern femaleSp = (StatementPattern) join2.getLeftArg();
    assertStatementPattern(femaleSp, FEMALE);
    assertTrue(join2.getRightArg() instanceof StatementPattern);
    final StatementPattern parentSp = (StatementPattern) join2.getRightArg();
    assertStatementPattern(parentSp, PARENT);
    assertTrue(union2.getRightArg() instanceof Join);
    final Join join3 = (Join) union2.getRightArg();
    assertTrue(join3.getLeftArg() instanceof StatementPattern);
    final StatementPattern femaleSp2 = (StatementPattern) join3.getLeftArg();
    assertStatementPattern(femaleSp2, FEMALE);
    assertTrue(join3.getRightArg() instanceof Join);
    final Join join4 = (Join) join3.getRightArg();
    assertTrue(join4.getLeftArg() instanceof StatementPattern);
    final StatementPattern leaderSp = (StatementPattern) join4.getLeftArg();
    assertStatementPattern(leaderSp, LEADER);
    assertTrue(join4.getRightArg() instanceof StatementPattern);
    final StatementPattern nunSp = (StatementPattern) join4.getRightArg();
    assertStatementPattern(nunSp, NUN);
    assertTrue(union1.getRightArg() instanceof StatementPattern);
    final StatementPattern actualMotherSp = (StatementPattern) union1.getRightArg();
    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(conf, inferenceEngine));
    // Since Father produces only one intersection it creates a nested join
    // that gets union with the original statement pattern.
    // The nested join of Father should be:
    // Join(
    // SP(Man),
    // SP(Parent)
    // )
    // Collect the arguments to the union:
    assertTrue(query2.getArg() instanceof Union);
    final Union union3 = (Union) query2.getArg();
    assertTrue(union3.getLeftArg() instanceof Join);
    final Join join5 = (Join) union3.getLeftArg();
    assertTrue(join5.getLeftArg() instanceof StatementPattern);
    final StatementPattern manSp = (StatementPattern) join5.getLeftArg();
    assertStatementPattern(manSp, MAN);
    assertTrue(join5.getRightArg() instanceof StatementPattern);
    final StatementPattern parentSp2 = (StatementPattern) join5.getRightArg();
    assertStatementPattern(parentSp2, PARENT);
    assertTrue(union3.getRightArg() instanceof StatementPattern);
    final StatementPattern actualFatherSp = (StatementPattern) union3.getRightArg();
    final StatementPattern expectedFatherSp = new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", FATHER));
    assertEquals(expectedFatherSp, actualFatherSp);
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) Set(java.util.Set) HashMap(java.util.HashMap) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) Projection(org.openrdf.query.algebra.Projection) Join(org.openrdf.query.algebra.Join) Union(org.openrdf.query.algebra.Union) StatementPattern(org.openrdf.query.algebra.StatementPattern) ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) List(java.util.List) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) Test(org.junit.Test)

Example 7 with ProjectionElemList

use of org.openrdf.query.algebra.ProjectionElemList 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);
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) HashSet(java.util.HashSet) Set(java.util.Set) HasValueVisitor(org.apache.rya.rdftriplestore.inference.HasValueVisitor) HashMap(java.util.HashMap) Var(org.openrdf.query.algebra.Var) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) Projection(org.openrdf.query.algebra.Projection) Join(org.openrdf.query.algebra.Join) URI(org.openrdf.model.URI) Union(org.openrdf.query.algebra.Union) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) StatementPattern(org.openrdf.query.algebra.StatementPattern) InferenceEngine(org.apache.rya.rdftriplestore.inference.InferenceEngine) Value(org.openrdf.model.Value) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with ProjectionElemList

use of org.openrdf.query.algebra.ProjectionElemList in project incubator-rya by apache.

the class OneOfVisitorTest method testOneOf.

@Test
public void testOneOf() 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")));
    query.visit(new OneOfVisitor(conf, inferenceEngine));
    // Expected structure: BindingSetAssignment containing the enumeration:
    // BindingSetAssignment(CLUBS, DIAMONDS, HEARTS, SPADES)
    // Collect the arguments to the BindingSetAssignment:
    assertTrue(query.getArg() instanceof BindingSetAssignment);
    final BindingSetAssignment bsa = (BindingSetAssignment) query.getArg();
    final Iterable<BindingSet> iterable = bsa.getBindingSets();
    final Iterator<BindingSet> iter = iterable.iterator();
    assertBindingSet(iter, CARD_SUIT_ENUMERATION.iterator());
    // Query for a Ranks and rewrite using the visitor:
    final Projection query2 = new Projection(new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", RANKS)), new ProjectionElemList(new ProjectionElem("s", "subject")));
    query2.visit(new OneOfVisitor(conf, inferenceEngine));
    // Expected structure: BindingSetAssignment containing the enumeration:
    // BindingSetAssignment(ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10, JACK, QUEEN, KING)
    // Collect the arguments to the BindingSetAssignment:
    assertTrue(query2.getArg() instanceof BindingSetAssignment);
    final BindingSetAssignment bsa2 = (BindingSetAssignment) query2.getArg();
    final Iterable<BindingSet> iterable2 = bsa2.getBindingSets();
    final Iterator<BindingSet> iter2 = iterable2.iterator();
    assertBindingSet(iter2, CARD_RANK_ENUMERATION.iterator());
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) StatementPattern(org.openrdf.query.algebra.StatementPattern) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) Var(org.openrdf.query.algebra.Var) BindingSetAssignment(org.openrdf.query.algebra.BindingSetAssignment) Projection(org.openrdf.query.algebra.Projection) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) Test(org.junit.Test)

Example 9 with ProjectionElemList

use of org.openrdf.query.algebra.ProjectionElemList 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());
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) StatementPattern(org.openrdf.query.algebra.StatementPattern) ZeroLengthPath(org.openrdf.query.algebra.ZeroLengthPath) Var(org.openrdf.query.algebra.Var) Projection(org.openrdf.query.algebra.Projection) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) Union(org.openrdf.query.algebra.Union) TupleExpr(org.openrdf.query.algebra.TupleExpr) Test(org.junit.Test)

Example 10 with ProjectionElemList

use of org.openrdf.query.algebra.ProjectionElemList 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());
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) Set(java.util.Set) HashMap(java.util.HashMap) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) Projection(org.openrdf.query.algebra.Projection) Join(org.openrdf.query.algebra.Join) Union(org.openrdf.query.algebra.Union) TupleExpr(org.openrdf.query.algebra.TupleExpr) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) StatementPattern(org.openrdf.query.algebra.StatementPattern) NullableStatementImpl(org.apache.rya.api.utils.NullableStatementImpl) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) Test(org.junit.Test)

Aggregations

ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)30 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)26 Test (org.junit.Test)25 Projection (org.openrdf.query.algebra.Projection)24 StatementPattern (org.openrdf.query.algebra.StatementPattern)24 Var (org.openrdf.query.algebra.Var)21 MultiProjection (org.openrdf.query.algebra.MultiProjection)10 Union (org.openrdf.query.algebra.Union)10 HashSet (java.util.HashSet)9 Resource (org.openrdf.model.Resource)9 Extension (org.openrdf.query.algebra.Extension)9 ExtensionElem (org.openrdf.query.algebra.ExtensionElem)8 Join (org.openrdf.query.algebra.Join)8 TupleExpr (org.openrdf.query.algebra.TupleExpr)8 HashMap (java.util.HashMap)7 Set (java.util.Set)7 FixedStatementPattern (org.apache.rya.rdftriplestore.utils.FixedStatementPattern)6 SingletonSet (org.openrdf.query.algebra.SingletonSet)5 URI (org.openrdf.model.URI)4 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)3