Search in sources :

Example 26 with Projection

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

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

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

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

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

Aggregations

Projection (org.openrdf.query.algebra.Projection)76 Test (org.junit.Test)64 StatementPattern (org.openrdf.query.algebra.StatementPattern)41 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)37 ParsedQuery (org.openrdf.query.parser.ParsedQuery)34 TupleExpr (org.openrdf.query.algebra.TupleExpr)33 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)27 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)27 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)24 Var (org.openrdf.query.algebra.Var)24 ArrayList (java.util.ArrayList)23 QueryModelNode (org.openrdf.query.algebra.QueryModelNode)23 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)22 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)17 Join (org.openrdf.query.algebra.Join)15 HashSet (java.util.HashSet)14 Union (org.openrdf.query.algebra.Union)10 Resource (org.openrdf.model.Resource)9 MultiProjection (org.openrdf.query.algebra.MultiProjection)9 HashMap (java.util.HashMap)8