Java Code Examples for org.eclipse.rdf4j.query.algebra.Join#getLeftArg()

The following examples show how to use org.eclipse.rdf4j.query.algebra.Join#getLeftArg() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(Join node) {

          if (compSet.contains(node.getRightArg())) {
              this.toBeReplaced = node.getRightArg();
              node.replaceChildNode(node.getRightArg(), replacement);
              return;
          } else if (compSet.contains(node.getLeftArg())) {
              this.toBeReplaced = node.getLeftArg();
              node.replaceChildNode(node.getLeftArg(), replacement);
              return;
          } else {
              super.meet(node);
          }

      }
 
Example 2
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(Join queryNode) {

          // if query tree contains external tuples and they are not
          // positioned above statement pattern node
          // reposition
          if (this.extTuples.size() > 0 && !(queryNode.getRightArg() instanceof ExternalTupleSet)
                  && !(queryNode.getRightArg() instanceof BindingSetAssignment)) {

              if (queryNode.getLeftArg() instanceof ExternalTupleSet) {
                  QueryModelNode temp = queryNode.getLeftArg();
                  queryNode.setLeftArg(queryNode.getRightArg());
                  queryNode.setRightArg((TupleExpr)temp);
              } else {

                  QNodeExchanger qnev = new QNodeExchanger(queryNode.getRightArg(), this.extTuples);
                  queryNode.visit(qnev);
                  queryNode.replaceChildNode(queryNode.getRightArg(), qnev.getReplaced());
                  super.meet(queryNode);
              }
          } else {
              super.meet(queryNode);
          }

      }
 
Example 3
Source File: BasicGroup.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private TupleExpr asJoin(Collection<TupleExpr> theList) {
	Join aJoin = new Join();

	if (theList.isEmpty()) {
		throw new RuntimeException("Can't have an empty or missing join.");
	} else if (theList.size() == 1) {
		return theList.iterator().next();
	}

	for (TupleExpr aExpr : theList) {
		if (aJoin.getLeftArg() == null) {
			aJoin.setLeftArg(aExpr);
		} else if (aJoin.getRightArg() == null) {
			aJoin.setRightArg(aExpr);
		} else {
			Join aNewJoin = new Join();

			aNewJoin.setLeftArg(aJoin);
			aNewJoin.setRightArg(aExpr);

			aJoin = aNewJoin;
		}
	}

	return aJoin;
}
 
Example 4
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSES1922PathSequenceWithValueConstant() throws Exception {

	StringBuilder qb = new StringBuilder();
	qb.append("ASK {?A (<foo:bar>)/<foo:foo> <foo:objValue>} ");

	ParsedQuery q = parser.parseQuery(qb.toString(), null);
	TupleExpr te = q.getTupleExpr();

	assertNotNull(te);

	assertTrue(te instanceof Slice);
	Slice s = (Slice) te;
	assertTrue(s.getArg() instanceof Join);
	Join j = (Join) s.getArg();

	assertTrue(j.getLeftArg() instanceof StatementPattern);
	assertTrue(j.getRightArg() instanceof StatementPattern);
	StatementPattern leftArg = (StatementPattern) j.getLeftArg();
	StatementPattern rightArg = (StatementPattern) j.getRightArg();

	assertTrue(leftArg.getObjectVar().equals(rightArg.getSubjectVar()));
	assertEquals(leftArg.getObjectVar().getName(), rightArg.getSubjectVar().getName());
}
 
Example 5
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSES1927UnequalLiteralValueConstants1() throws Exception {

	StringBuilder qb = new StringBuilder();
	qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"@en .} ");

	ParsedQuery q = parser.parseQuery(qb.toString(), null);
	TupleExpr te = q.getTupleExpr();

	assertNotNull(te);

	assertTrue(te instanceof Slice);
	Slice s = (Slice) te;
	assertTrue(s.getArg() instanceof Join);
	Join j = (Join) s.getArg();

	assertTrue(j.getLeftArg() instanceof StatementPattern);
	assertTrue(j.getRightArg() instanceof StatementPattern);
	StatementPattern leftArg = (StatementPattern) j.getLeftArg();
	StatementPattern rightArg = (StatementPattern) j.getRightArg();

	assertFalse(leftArg.getObjectVar().equals(rightArg.getObjectVar()));
	assertNotEquals(leftArg.getObjectVar().getName(), rightArg.getObjectVar().getName());
}
 
Example 6
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSES1927UnequalLiteralValueConstants2() throws Exception {

	StringBuilder qb = new StringBuilder();
	qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"^^<foo:bar> .} ");

	ParsedQuery q = parser.parseQuery(qb.toString(), null);
	TupleExpr te = q.getTupleExpr();

	assertNotNull(te);

	assertTrue(te instanceof Slice);
	Slice s = (Slice) te;
	assertTrue(s.getArg() instanceof Join);
	Join j = (Join) s.getArg();

	assertTrue(j.getLeftArg() instanceof StatementPattern);
	assertTrue(j.getRightArg() instanceof StatementPattern);
	StatementPattern leftArg = (StatementPattern) j.getLeftArg();
	StatementPattern rightArg = (StatementPattern) j.getRightArg();

	assertFalse(leftArg.getObjectVar().equals(rightArg.getObjectVar()));
	assertNotEquals(leftArg.getObjectVar().getName(), rightArg.getObjectVar().getName());
}
 
Example 7
Source File: SeparateFilterJoinsVisitor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(final Filter node) throws Exception {
    super.meet(node);

    final ValueExpr condition = node.getCondition();
    final TupleExpr arg = node.getArg();
    if (!(arg instanceof Join)) {
        return;
    }

    final Join join = (Join) arg;
    final TupleExpr leftArg = join.getLeftArg();
    final TupleExpr rightArg = join.getRightArg();

    if (leftArg instanceof StatementPattern && rightArg instanceof StatementPattern) {
        final Filter left = new Filter(leftArg, condition);
        final Filter right = new Filter(rightArg, condition);
        node.replaceWith(new Join(left, right));
    }

}
 
Example 8
Source File: PushJoinDownVisitor.java    From rya with Apache License 2.0 6 votes vote down vote up
@Override
public void meet(final Join node) throws Exception {
    super.meet(node);

    final TupleExpr leftArg = node.getLeftArg();
    final TupleExpr rightArg = node.getRightArg();

    /**
     * if join(join(1, 2), join(3,4))
     * should be:
     * join(join(join(1,2), 3), 4)
     */
    if (leftArg instanceof Join && rightArg instanceof Join) {
        final Join leftJoin = (Join) leftArg;
        final Join rightJoin = (Join) rightArg;
        final TupleExpr right_LeftArg = rightJoin.getLeftArg();
        final TupleExpr right_rightArg = rightJoin.getRightArg();
        final Join inner = new Join(leftJoin, right_LeftArg);
        final Join outer = new Join(inner, right_rightArg);
        node.replaceWith(outer);
    }

}
 
Example 9
Source File: TestSparqlStarParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testUseInStatementPatternWithVars() throws Exception {
	String simpleSparqlQuery = "SELECT * WHERE { <<?s ?p ?o>> <urn:pred> ?val}";

	ParsedQuery q = parser.parseQuery(simpleSparqlQuery, null);

	assertNotNull(q);
	assertTrue("expect projection", q.getTupleExpr() instanceof Projection);
	Projection proj = (Projection) q.getTupleExpr();
	List<ProjectionElem> list = proj.getProjectionElemList().getElements();
	final ArrayList<String> listNames = new ArrayList<>();
	list.forEach(el -> {
		listNames.add(el.getTargetName());
	});
	assertEquals("expect all bindings", 4, list.size());
	assertTrue("expect s", listNames.contains("s"));
	assertTrue("expect p", listNames.contains("p"));
	assertTrue("expect o", listNames.contains("o"));
	assertTrue("expect val", listNames.contains("val"));

	assertTrue("expect Join", proj.getArg() instanceof Join);
	Join join = (Join) proj.getArg();

	assertTrue("expect right arg of Join be StatementPattern", join.getRightArg() instanceof StatementPattern);
	StatementPattern pattern = (StatementPattern) join.getRightArg();
	String anonVar = pattern.getSubjectVar().getName();
	assertEquals("statement pattern predVar value", "urn:pred", pattern.getPredicateVar().getValue().toString());
	assertEquals("statement pattern obj var name", "val", pattern.getObjectVar().getName());

	assertTrue("expect left arg of Join be TripleRef", join.getLeftArg() instanceof TripleRef);
	TripleRef triple = (TripleRef) join.getLeftArg();

	assertEquals("ext var should match", anonVar, triple.getExprVar().getName());

	assertEquals("subj var name should match", "s", triple.getSubjectVar().getName());
	assertEquals("pred var name should match", "p", triple.getPredicateVar().getName());
	assertEquals("obj var name should match", "o", triple.getObjectVar().getName());
}
 
Example 10
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(Join node) {

          if (!compSet.contains(node.getRightArg())) {
              // looks for placed to position filter node. if right node is
              // contained in index
              // and left node is statement pattern node contained in index or
              // is a join, place
              // filter above join.
              if (node.getLeftArg() instanceof Join || !compSet.contains(node.getLeftArg())) {

                  QueryModelNode pNode = node.getParentNode();
                  ((Filter) filter).setArg(node);
                  pNode.replaceChildNode(node, filter);
                  filterPlaced = true;

                  return;
              } // otherwise place filter below join and above right arg
              else {
                  ((Filter) filter).setArg(node.getRightArg());
                  node.replaceChildNode(node.getRightArg(), filter);
                  filterPlaced = true;
                  return;

              }
          } else if (node.getLeftArg() instanceof StatementPattern && !compSet.contains(node.getLeftArg())) {

              ((Filter) filter).setArg(node.getLeftArg());
              node.replaceChildNode(node.getLeftArg(), filter);
              filterPlaced = true;

              return;
          } else {
              super.meet(node);
          }
      }
 
Example 11
Source File: SparqlFluoQueryBuilder.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(final Join node) {
    // Extract the metadata that will be stored from the node.
    final String joinNodeId = nodeIds.getOrMakeId(node);
    final QueryModelNode left = node.getLeftArg();
    final QueryModelNode right = node.getRightArg();

    // Update the metadata for the JoinMetadata.Builder.
    makeJoinMetadata(joinNodeId, JoinType.NATURAL_JOIN, left, right);

    // Walk to the next node.
    super.meet(node);
}
 
Example 12
Source File: GeoEnabledFilterFunctionOptimizer.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(final Join node) {
    final QueryModelNode lNode = node.getLeftArg();
    if (lNode instanceof StatementPattern) {
        exchangeVar.replaceWith(lNode);
        node.setLeftArg(exchangeVar);
    } else {
        super.meet(node);
    }
}
 
Example 13
Source File: FilterFunctionOptimizer.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public void meet(final Join node) {
    final QueryModelNode lNode = node.getLeftArg();
    if (lNode instanceof StatementPattern) {
        exchangeVar.replaceWith(lNode);
        node.setLeftArg(exchangeVar);
    } else {
        super.meet(node);
    }
}
 
Example 14
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Join join) {
	super.meet(join);

	TupleExpr leftArg = join.getLeftArg();
	TupleExpr rightArg = join.getRightArg();

	if (leftArg instanceof EmptySet || rightArg instanceof EmptySet) {
		join.replaceWith(new EmptySet());
	} else if (leftArg instanceof SingletonSet) {
		join.replaceWith(rightArg);
	} else if (rightArg instanceof SingletonSet) {
		join.replaceWith(leftArg);
	}
}
 
Example 15
Source File: SPARQLParserTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testWildCardPathComplexSubjectHandling() {

	String query = "PREFIX : <http://example.org/>\n ASK { ?a (:comment/^(:subClassOf|(:type/:label))/:type)* ?b } ";

	ParsedQuery parsedQuery = parser.parseQuery(query, null);
	TupleExpr tupleExpr = parsedQuery.getTupleExpr();

	Slice slice = (Slice) tupleExpr;

	ArbitraryLengthPath path = (ArbitraryLengthPath) slice.getArg();
	Var pathStart = path.getSubjectVar();
	Var pathEnd = path.getObjectVar();

	assertThat(pathStart.getName()).isEqualTo("a");
	assertThat(pathEnd.getName()).isEqualTo("b");

	Join pathSequence = (Join) path.getPathExpression();
	Join innerJoin = (Join) pathSequence.getLeftArg();
	Var commentObjectVar = ((StatementPattern) innerJoin.getLeftArg()).getObjectVar();

	Union union = (Union) innerJoin.getRightArg();
	Var subClassOfSubjectVar = ((StatementPattern) union.getLeftArg()).getSubjectVar();
	assertThat(subClassOfSubjectVar).isNotEqualTo(commentObjectVar);

	Var subClassOfObjectVar = ((StatementPattern) union.getLeftArg()).getObjectVar();

	assertThat(subClassOfObjectVar).isEqualTo(commentObjectVar);
}
 
Example 16
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Join join) {
	super.meet(join);

	TupleExpr leftArg = join.getLeftArg();
	TupleExpr rightArg = join.getRightArg();

	if (leftArg instanceof EmptySet || rightArg instanceof EmptySet) {
		join.replaceWith(new EmptySet());
	} else if (leftArg instanceof SingletonSet) {
		join.replaceWith(rightArg);
	} else if (rightArg instanceof SingletonSet) {
		join.replaceWith(leftArg);
	}
}
 
Example 17
Source File: GeneralizedExternalProcessor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
		public void meet(Join node) {

            Set<QueryModelNode> eSet = getQNodes(node);

            if (eSet.containsAll(sSet) && !(node.getRightArg() instanceof BindingSetAssignment)) {

//                System.out.println("Eset is " + eSet + " and sSet is " + sSet);

                if (eSet.equals(sSet)) {
                    node.replaceWith(set);
                    indexPlaced = true;
                    return;
                } else {
                    if (node.getLeftArg() instanceof StatementPattern && sSet.size() == 1) {
                        if(sSet.contains(node.getLeftArg())) {
                            node.setLeftArg(set);
                            indexPlaced = true;
                        } else if(sSet.contains(node.getRightArg())) {
                            node.setRightArg(set);
                            indexPlaced = true;
                        } else {
                            return;
                        }
                    }
                    else {
                        super.meet(node);
                    }
                }
            } else if (eSet.containsAll(sSet)) {

                super.meet(node);

            } else {
                return;
            }

        }
 
Example 18
Source File: EvaluationStatistics.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Join node) {
	double cost = 1;
	for (TupleExpr arg : new TupleExpr[] { node.getLeftArg(), // NOPMD
			node.getRightArg() }) {
		arg.visit(this);
		cost *= this.cardinality;
	}
	cardinality = cost;
}
 
Example 19
Source File: TestSparqlStarParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testUseNestedInStatementPatternWithVars() throws Exception {
	String simpleSparqlQuery = "SELECT * WHERE { <<<<?s ?p ?o>> ?q ?r>> <urn:pred> ?val}";

	ParsedQuery q = parser.parseQuery(simpleSparqlQuery, null);

	assertNotNull(q);
	assertTrue("expect projection", q.getTupleExpr() instanceof Projection);
	Projection proj = (Projection) q.getTupleExpr();
	List<ProjectionElem> list = proj.getProjectionElemList().getElements();
	final ArrayList<String> listNames = new ArrayList<>();
	list.forEach(el -> {
		listNames.add(el.getTargetName());
	});
	assertEquals("expect all bindings", 6, list.size());
	assertTrue("expect s", listNames.contains("s"));
	assertTrue("expect p", listNames.contains("p"));
	assertTrue("expect o", listNames.contains("o"));
	assertTrue("expect q", listNames.contains("q"));
	assertTrue("expect r", listNames.contains("r"));
	assertTrue("expect val", listNames.contains("val"));

	assertTrue("expect Join", proj.getArg() instanceof Join);
	Join join = (Join) proj.getArg();

	assertTrue("expect right arg of Join be StatementPattern", join.getRightArg() instanceof StatementPattern);
	StatementPattern pattern = (StatementPattern) join.getRightArg();
	String anonVar = pattern.getSubjectVar().getName();
	assertEquals("statement pattern predVar value", "urn:pred", pattern.getPredicateVar().getValue().toString());
	assertEquals("statement pattern obj var name", "val", pattern.getObjectVar().getName());

	assertTrue("expect left arg of first Join be Join", join.getLeftArg() instanceof Join);
	Join join2 = (Join) join.getLeftArg();

	assertTrue("expect left arg of second Join be TripleRef", join2.getLeftArg() instanceof TripleRef);
	TripleRef tripleLeft = (TripleRef) join2.getLeftArg();
	assertEquals("subj var name should match", "s", tripleLeft.getSubjectVar().getName());
	assertEquals("pred var name should match", "p", tripleLeft.getPredicateVar().getName());
	assertEquals("obj var name should match", "o", tripleLeft.getObjectVar().getName());
	String anonVarLeftTripleRef = tripleLeft.getExprVar().getName();

	assertTrue("expect right arg of second Join be TripleRef", join2.getRightArg() instanceof TripleRef);
	TripleRef triple = (TripleRef) join2.getRightArg();

	assertEquals("subj var name should match anon", anonVarLeftTripleRef, triple.getSubjectVar().getName());
	assertEquals("pred var name should match", "q", triple.getPredicateVar().getName());
	assertEquals("obj var name should match", "r", triple.getObjectVar().getName());

	assertEquals("ext var should match", anonVar, triple.getExprVar().getName());
}
 
Example 20
Source File: SomeValuesFromVisitorTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testSomeValuesFrom() throws Exception {
    // Configure a mock instance engine with an ontology:
    final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
    Map<Resource, Set<IRI>> 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());
}