Java Code Examples for org.eclipse.rdf4j.query.algebra.ProjectionElemList#addElement()

The following examples show how to use org.eclipse.rdf4j.query.algebra.ProjectionElemList#addElement() . 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 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 3
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Projection visitResultNodes(Resource resultNodes) throws RDF4JException {
	ProjectionElemList projElemList = new ProjectionElemList();
	Iteration<Resource, QueryEvaluationException> iter = TripleSources.listResources(resultNodes,
			store);
	while (iter.hasNext()) {
		Resource r = iter.next();
		ProjectionElem projElem = visitResultNode(r);
		projElemList.addElement(projElem);
	}

	Projection proj = new Projection();
	proj.setProjectionElemList(projElemList);

	tupleRoot = new DescribeOperator(proj);
	return proj;
}
 
Example 4
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Projection visitResultVariables(Resource resultVars, Map<String, ProjectionElem> previousProjElems)
		throws RDF4JException {
	ProjectionElemList projElemList = new ProjectionElemList();
	Iteration<Resource, QueryEvaluationException> iter = TripleSources.listResources(resultVars,
			store);
	while (iter.hasNext()) {
		Resource r = iter.next();
		ProjectionElem projElem = visitResultVariable(r, previousProjElems);
		projElemList.addElement(projElem);
	}

	Projection proj = new Projection();
	proj.setProjectionElemList(projElemList);

	tupleRoot = proj;
	return proj;
}
 
Example 5
Source File: AbstractQueryBuilder.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private UnaryTupleOperator projection() {
	if (!mProjectionPatterns.isEmpty()) {
		return multiProjection();
	} else {
		Extension aExt = null;

		ProjectionElemList aList = new ProjectionElemList();

		for (String aVar : mProjectionVars) {
			aList.addElement(new ProjectionElem(aVar));
		}

		Projection aProjection = new Projection();
		aProjection.setProjectionElemList(aList);

		if (aExt != null) {
			aProjection.setArg(aExt);
		}

		return aProjection;
	}
}
 
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: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private ProjectionElemList visitTemplate(Resource r) throws RDF4JException {
	ProjectionElemList projElems = new ProjectionElemList();
	Value subj = TripleSources.singleValue(r, SP.SUBJECT_PROPERTY, store);
	projElems.addElement(createProjectionElem(subj, "subject", null));
	Value pred = TripleSources.singleValue(r, SP.PREDICATE_PROPERTY, store);
	projElems.addElement(createProjectionElem(pred, "predicate", null));
	Value obj = TripleSources.singleValue(r, SP.OBJECT_PROPERTY, store);
	projElems.addElement(createProjectionElem(obj, "object", null));
	return projElems;
}
 
Example 9
Source File: PrepareOwnedTupleExpr.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean addBindingNames(StringBuilder builder, boolean alreadyMapping, Map<String, String> bindings,
		ProjectionElemList list, String name) {
	boolean mapping = alreadyMapping;
	if (variables.containsKey(name)) {
		String var = variables.get(name);
		builder.append(" ?").append(var);
		bindings.put(name, var);
		list.addElement(new ProjectionElem(var, name));
		if (!name.equals(var)) {
			mapping = true;
		}
	}
	return mapping;
}
 
Example 10
Source File: AggregationPipelineQueryNodeTest.java    From rya with Apache License 2.0 4 votes vote down vote up
@Test
public void testProject() {
    final AggregationPipelineQueryNode base = new AggregationPipelineQueryNode(
            collection,
            new LinkedList<>(),
            Sets.newHashSet("x", "y"),
            Sets.newHashSet("x", "y", "opt"),
            HashBiMap.create());
    // Add a single projection
    ProjectionElemList singleProjection = new ProjectionElemList();
    singleProjection.addElement(new ProjectionElem("x", "z"));
    singleProjection.addElement(new ProjectionElem("y", "y"));
    List<ProjectionElemList> projections = Arrays.asList(singleProjection);
    AggregationPipelineQueryNode node = base.clone();
    boolean success = node.project(projections);
    Assert.assertTrue(success);
    Assert.assertEquals(1, node.getPipeline().size());
    Assert.assertEquals(Sets.newHashSet("z", "y"),
            node.getAssuredBindingNames());
    Assert.assertEquals(Sets.newHashSet("z", "y"),
            node.getBindingNames());
    // Add a multi-projection
    ProjectionElemList p1 = new ProjectionElemList();
    p1.addElement(new ProjectionElem("x", "solution"));
    ProjectionElemList p2 = new ProjectionElemList();
    p2.addElement(new ProjectionElem("y", "solution"));
    ProjectionElemList p3 = new ProjectionElemList();
    p3.addElement(new ProjectionElem("x", "x"));
    p3.addElement(new ProjectionElem("x", "solution"));
    p3.addElement(new ProjectionElem("y", "y"));
    projections = Arrays.asList(p1, p2, p3);
    node = base.clone();
    success = node.project(projections);
    Assert.assertTrue(success);
    Assert.assertEquals(3, node.getPipeline().size());
    Assert.assertEquals(Sets.newHashSet("solution"),
            node.getAssuredBindingNames());
    Assert.assertEquals(Sets.newHashSet("x", "y", "solution"),
            node.getBindingNames());
    // Add no projections
    node = base.clone();
    success = node.project(Arrays.asList());
    Assert.assertFalse(success);
    Assert.assertEquals(base, node);
}