Java Code Examples for org.eclipse.rdf4j.query.algebra.Union#setRightArg()

The following examples show how to use org.eclipse.rdf4j.query.algebra.Union#setRightArg() . 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: QueryAlgebraUtil.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Construct a SELECT query for a grouped bound check.
 * 
 * Pattern:
 * 
 * SELECT DISTINCT ?o_1 .. ?o_N WHERE { { s1 p1 ?o_1 FILTER ?o_1=o1 } UNION ... UNION { sN pN ?o_N FILTER ?o_N=oN }}
 * 
 * @param stmt
 * @param unionBindings
 * @return
 */
public static TupleExpr selectQueryStringBoundCheck(StatementPattern stmt, List<BindingSet> unionBindings) {
	
	Set<String> varNames = new HashSet<String>();
	
	Union union = new Union();
	union.setLeftArg(constructStatementCheckId(stmt, 0, varNames, unionBindings.get(0)) );
	Union tmp = union;
	int idx;
	for (idx=1; idx<unionBindings.size()-1; idx++) {
		Union _u = new Union();
		_u.setLeftArg( constructStatementCheckId(stmt, idx, varNames, unionBindings.get(idx)) );
		tmp.setRightArg(_u);
		tmp = _u;
	}
	tmp.setRightArg( constructStatementCheckId(stmt, idx, varNames, unionBindings.get(idx) ));
	
	ProjectionElemList projList = new ProjectionElemList();
	for (String var : varNames)
		projList.addElement( new ProjectionElem(var));
	
	Projection proj = new Projection(union, projList);

	return proj;
}
 
Example 2
Source File: QueryAlgebraUtil.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
protected static Union constructInnerUnion(StatementPattern stmt, int outerID, Set<String> varNames, List<BindingSet> bindings) {
	
	Union union = new Union();
	union.setLeftArg(constructStatementId(stmt, outerID + "_0", varNames, bindings.get(0)) );
	Union tmp = union;
	int idx;
	for (idx=1; idx<bindings.size()-1; idx++) {
		Union _u = new Union();
		_u.setLeftArg( constructStatementId(stmt, outerID + "_" + idx, varNames, bindings.get(idx)) );
		tmp.setRightArg(_u);
		tmp = _u;
	}
	tmp.setRightArg( constructStatementId(stmt, outerID + "_" + idx, varNames, bindings.get(idx)));
	
	return union;
}
 
Example 3
Source File: QueryAlgebraUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Construct a SELECT query for a grouped bound check.
 *
 * Pattern:
 *
 * SELECT DISTINCT ?o_1 .. ?o_N WHERE { { s1 p1 ?o_1 FILTER ?o_1=o1 } UNION ... UNION { sN pN ?o_N FILTER ?o_N=oN }}
 *
 * @param stmt
 * @param unionBindings
 * @return the SELECT query
 */
public static TupleExpr selectQueryStringBoundCheck(StatementPattern stmt, List<BindingSet> unionBindings) {

	Set<String> varNames = new HashSet<>();

	Union union = new Union();
	union.setLeftArg(constructStatementCheckId(stmt, 0, varNames, unionBindings.get(0)));
	Union tmp = union;
	int idx;
	for (idx = 1; idx < unionBindings.size() - 1; idx++) {
		Union _u = new Union();
		_u.setLeftArg(constructStatementCheckId(stmt, idx, varNames, unionBindings.get(idx)));
		tmp.setRightArg(_u);
		tmp = _u;
	}
	tmp.setRightArg(constructStatementCheckId(stmt, idx, varNames, unionBindings.get(idx)));

	ProjectionElemList projList = new ProjectionElemList();
	for (String var : varNames) {
		projList.addElement(new ProjectionElem(var));
	}

	Projection proj = new Projection(union, projList);

	return proj;
}
 
Example 4
Source File: QueryAlgebraUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected static Union constructInnerUnion(StatementPattern stmt, int outerID, Set<String> varNames,
		List<BindingSet> bindings) {

	Union union = new Union();
	union.setLeftArg(constructStatementId(stmt, outerID + "_0", varNames, bindings.get(0)));
	Union tmp = union;
	int idx;
	for (idx = 1; idx < bindings.size() - 1; idx++) {
		Union _u = new Union();
		_u.setLeftArg(constructStatementId(stmt, outerID + "_" + idx, varNames, bindings.get(idx)));
		tmp.setRightArg(_u);
		tmp = _u;
	}
	tmp.setRightArg(constructStatementId(stmt, outerID + "_" + idx, varNames, bindings.get(idx)));

	return union;
}
 
Example 5
Source File: IterativeEvaluationOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(Union union) {
	super.meet(union);

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

	if (leftArg instanceof Join && rightArg instanceof Join) {
		Join leftJoinArg = (Join) leftArg;
		Join rightJoin = (Join) rightArg;

		if (leftJoinArg.getLeftArg().equals(rightJoin.getLeftArg())) {
			// factor out the left-most join argument
			Join newJoin = new Join();
			union.replaceWith(newJoin);
			newJoin.setLeftArg(leftJoinArg.getLeftArg());
			newJoin.setRightArg(union);
			union.setLeftArg(leftJoinArg.getRightArg());
			union.setRightArg(rightJoin.getRightArg());

			union.visit(this);
		}
	}
}
 
Example 6
Source File: QueryAlgebraUtil.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Construct a SELECT query expression for a bound union.
 * 
 * Pattern:
 * 
 * SELECT ?v_1 ?v_2 ?v_N WHERE { { ?v_1 p o } UNION { ?v_2 p o } UNION ... } 
 * 
 * Note that the filterExpr is not evaluated at the moment.
 * 
 * @param stmt
 * @param unionBindings
 * @param filterExpr
 * @param evaluated
 * 			parameter can be used outside this method to check whether FILTER has been evaluated, false in beginning
 * 
 * @return
 */
public static TupleExpr selectQueryBoundUnion( StatementPattern stmt, List<BindingSet> unionBindings, FilterValueExpr filterExpr, Boolean evaluated) {
	
	// TODO add FILTER expressions
	
	Set<String> varNames = new HashSet<String>();
	
	Union union = new Union();
	union.setLeftArg(constructStatementId(stmt, Integer.toString(0), varNames, unionBindings.get(0)) );
	Union tmp = union;
	int idx;
	for (idx=1; idx<unionBindings.size()-1; idx++) {
		Union _u = new Union();
		_u.setLeftArg( constructStatementId(stmt, Integer.toString(idx), varNames, unionBindings.get(idx)) );
		tmp.setRightArg(_u);
		tmp = _u;
	}
	tmp.setRightArg( constructStatementId(stmt, Integer.toString(idx), varNames, unionBindings.get(idx) ));
			
	ProjectionElemList projList = new ProjectionElemList();
	for (String var : varNames)
		projList.addElement( new ProjectionElem(var));
	
	Projection proj = new Projection(union, projList);

	return proj;
}
 
Example 7
Source File: QueryAlgebraUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Construct a SELECT query expression for a bound union.
 *
 * Pattern:
 *
 * SELECT ?v_1 ?v_2 ?v_N WHERE { { ?v_1 p o } UNION { ?v_2 p o } UNION ... }
 *
 * Note that the filterExpr is not evaluated at the moment.
 *
 * @param stmt
 * @param unionBindings
 * @param filterExpr
 * @param evaluated     parameter can be used outside this method to check whether FILTER has been evaluated, false
 *                      in beginning
 *
 * @return the SELECT query
 */
public static TupleExpr selectQueryBoundUnion(StatementPattern stmt, List<BindingSet> unionBindings,
		FilterValueExpr filterExpr, Boolean evaluated) {

	// TODO add FILTER expressions

	Set<String> varNames = new HashSet<>();

	Union union = new Union();
	union.setLeftArg(constructStatementId(stmt, Integer.toString(0), varNames, unionBindings.get(0)));
	Union tmp = union;
	int idx;
	for (idx = 1; idx < unionBindings.size() - 1; idx++) {
		Union _u = new Union();
		_u.setLeftArg(constructStatementId(stmt, Integer.toString(idx), varNames, unionBindings.get(idx)));
		tmp.setRightArg(_u);
		tmp = _u;
	}
	tmp.setRightArg(constructStatementId(stmt, Integer.toString(idx), varNames, unionBindings.get(idx)));

	ProjectionElemList projList = new ProjectionElemList();
	for (String var : varNames) {
		projList.addElement(new ProjectionElem(var));
	}

	Projection proj = new Projection(union, projList);

	return proj;
}
 
Example 8
Source File: QueryModelNormalizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testNormalizeUnionWithEmptyLeft() {
	Projection p = new Projection();
	Union union = new Union();
	SingletonSet s = new SingletonSet();
	union.setLeftArg(new EmptySet());
	union.setRightArg(s);
	p.setArg(union);

	subject.meet(union);

	assertThat(p.getArg()).isEqualTo(s);
}
 
Example 9
Source File: QueryModelNormalizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testNormalizeUnionWithEmptyRight() {
	Projection p = new Projection();
	Union union = new Union();
	SingletonSet s = new SingletonSet();
	union.setRightArg(new EmptySet());
	union.setLeftArg(s);
	p.setArg(union);

	subject.meet(union);

	assertThat(p.getArg()).isEqualTo(s);
}
 
Example 10
Source File: QueryModelNormalizerTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @see https://github.com/eclipse/rdf4j/issues/1404
 */
@Test
public void testNormalizeUnionWithTwoSingletons() {
	Projection p = new Projection();
	Union union = new Union();
	union.setRightArg(new SingletonSet());
	union.setLeftArg(new SingletonSet());
	p.setArg(union);

	subject.meet(union);

	assertThat(p.getArg()).isEqualTo(union);
}
 
Example 11
Source File: TupleExprBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Object visit(ASTPathAlternative pathAltNode, Object data) throws VisitorException {

	int altCount = pathAltNode.jjtGetNumChildren();

	if (altCount > 1) {
		GraphPattern parentGP = graphPattern;
		Union union = new Union();
		Union currentUnion = union;
		for (int i = 0; i < altCount - 1; i++) {
			graphPattern = new GraphPattern(parentGP);
			pathAltNode.jjtGetChild(i).jjtAccept(this, data);
			TupleExpr arg = graphPattern.buildTupleExpr();
			currentUnion.setLeftArg(arg);
			if (i == altCount - 2) { // second-to-last item
				graphPattern = new GraphPattern(parentGP);
				pathAltNode.jjtGetChild(i + 1).jjtAccept(this, data);
				arg = graphPattern.buildTupleExpr();
				currentUnion.setRightArg(arg);
			} else {
				Union newUnion = new Union();
				currentUnion.setRightArg(newUnion);
				currentUnion = newUnion;
			}
		}

		// when using union to execute path expressions, the scope does not not change
		union.setVariableScopeChange(false);
		parentGP.addRequiredTE(union);
		graphPattern = parentGP;
	} else {
		pathAltNode.jjtGetChild(0).jjtAccept(this, data);
	}

	return null;
}
 
Example 12
Source File: InverseOfVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
protected void meetSP(StatementPattern node) throws Exception {
    StatementPattern sp = node.clone();
    final Var predVar = sp.getPredicateVar();

    IRI pred = (IRI) 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 }
         */

        IRI predIri = (IRI) predVar.getValue();
        IRI invPropIri = inferenceEngine.findInverseOf(predIri);
        if (invPropIri != null) {
            Var subjVar = sp.getSubjectVar();
            Union union = new InferUnion();
            union.setLeftArg(sp);
            union.setRightArg(new StatementPattern(objVar, new Var(predVar.getName(), invPropIri), subjVar, cntxtVar));
            node.replaceWith(union);
        }
    }
}
 
Example 13
Source File: SymmetricPropertyVisitor.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
protected void meetSP(StatementPattern node) throws Exception {
    StatementPattern sp = node.clone();

    final Var predVar = sp.getPredicateVar();
    IRI pred = (IRI) 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 }
         */

        IRI symmPropIri = (IRI) predVar.getValue();
        if(inferenceEngine.isSymmetricProperty(symmPropIri)) {
            Var subjVar = sp.getSubjectVar();
            Union union = new InferUnion();
            union.setLeftArg(sp);
            union.setRightArg(new StatementPattern(objVar, predVar, subjVar, cntxtVar));
            node.replaceWith(union);
        }
    }
}