Search in sources :

Example 1 with Union

use of org.openrdf.query.algebra.Union 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());
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) HashSet(java.util.HashSet) 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)

Example 2 with Union

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

the class HasSelfVisitorTest method testPropertyPattern_constantSubj.

@Test
public void testPropertyPattern_constantSubj() throws Exception {
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    final Set<Resource> loveTypes = new HashSet<>();
    loveTypes.add(narcissist);
    when(inferenceEngine.getHasSelfImplyingProperty(love)).thenReturn(loveTypes);
    final Var subj = new Var("s", self);
    subj.setConstant(true);
    final Var obj = new Var("o");
    final Var pred = new Var("p", love);
    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 Extension);
    final StatementPattern expectedRight = new StatementPattern(subj, pred, obj);
    final Extension expectedLeft = new Extension(new StatementPattern(subj, new Var(RDF.TYPE.stringValue(), RDF.TYPE), new Var("urn:Narcissist", narcissist)), new ExtensionElem(subj, "o"));
    Assert.assertEquals(expectedLeft, union.getLeftArg());
    Assert.assertEquals(expectedRight, union.getRightArg());
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) Projection(org.openrdf.query.algebra.Projection) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) Union(org.openrdf.query.algebra.Union) Extension(org.openrdf.query.algebra.Extension) StatementPattern(org.openrdf.query.algebra.StatementPattern) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with Union

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

the class HasSelfVisitorTest method testPropertyPattern_constantObj.

@Test
public void testPropertyPattern_constantObj() throws Exception {
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    final Set<Resource> loveTypes = new HashSet<>();
    loveTypes.add(narcissist);
    when(inferenceEngine.getHasSelfImplyingProperty(love)).thenReturn(loveTypes);
    final Var subj = new Var("s");
    final Var obj = new Var("o", self);
    obj.setConstant(true);
    final Var pred = new Var("p", love);
    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 Extension);
    final StatementPattern expectedRight = new StatementPattern(subj, pred, obj);
    final Extension expectedLeft = new Extension(new StatementPattern(obj, new Var(RDF.TYPE.stringValue(), RDF.TYPE), new Var("urn:Narcissist", narcissist)), new ExtensionElem(obj, "s"));
    Assert.assertEquals(expectedLeft, union.getLeftArg());
    Assert.assertEquals(expectedRight, union.getRightArg());
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) Var(org.openrdf.query.algebra.Var) Resource(org.openrdf.model.Resource) Projection(org.openrdf.query.algebra.Projection) ExtensionElem(org.openrdf.query.algebra.ExtensionElem) Union(org.openrdf.query.algebra.Union) Extension(org.openrdf.query.algebra.Extension) StatementPattern(org.openrdf.query.algebra.StatementPattern) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with Union

use of org.openrdf.query.algebra.Union 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 5 with Union

use of org.openrdf.query.algebra.Union 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)

Aggregations

StatementPattern (org.openrdf.query.algebra.StatementPattern)13 Union (org.openrdf.query.algebra.Union)13 Var (org.openrdf.query.algebra.Var)13 Test (org.junit.Test)10 Projection (org.openrdf.query.algebra.Projection)10 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)10 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)10 Resource (org.openrdf.model.Resource)8 HashSet (java.util.HashSet)7 Set (java.util.Set)6 URI (org.openrdf.model.URI)6 Join (org.openrdf.query.algebra.Join)6 TupleExpr (org.openrdf.query.algebra.TupleExpr)6 HashMap (java.util.HashMap)5 FixedStatementPattern (org.apache.rya.rdftriplestore.utils.FixedStatementPattern)5 NullableStatementImpl (org.apache.rya.api.utils.NullableStatementImpl)2 HasValueVisitor (org.apache.rya.rdftriplestore.inference.HasValueVisitor)2 InferenceEngine (org.apache.rya.rdftriplestore.inference.InferenceEngine)2 Statement (org.openrdf.model.Statement)2 Extension (org.openrdf.query.algebra.Extension)2