Java Code Examples for org.eclipse.rdf4j.query.algebra.Projection#setArg()

The following examples show how to use org.eclipse.rdf4j.query.algebra.Projection#setArg() . 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: 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 2
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void visitDescribe(Resource describe) throws RDF4JException {
	Value resultNodes = TripleSources.singleValue(describe, SP.RESULT_NODES_PROPERTY, store);
	if (!(resultNodes instanceof Resource)) {
		throw new MalformedSpinException(
				String.format("Value of %s is not a resource", SP.RESULT_NODES_PROPERTY));
	}

	projElems = new LinkedHashMap<>();
	Projection projection = visitResultNodes((Resource) resultNodes);
	TupleExpr whereExpr = visitWhere(describe);
	projection.setArg(whereExpr);
	addSourceExpressions(projection, projElems.values());
}
 
Example 3
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 4
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 5
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 6
Source File: TopOfQueryFilterRelocator.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 *
 * This method moves the Filters of a specified {@link TupleExpr} to the top
 * of the TupleExpr.
 *
 * @param query
 *            - query whose filters will be relocated
 * @return - TupleExpr with filters relocated to top
 */
public static TupleExpr moveFiltersToTop(TupleExpr query) {

    ProjectionAndFilterGatherer fg = new ProjectionAndFilterGatherer();
    query.visit(fg);
    List<ValueExpr> filterCond = new ArrayList<>(fg.filterCond);
    Projection projection = fg.projection;

    if (filterCond.size() == 0) {
        return query;
    }

    Filter first = new Filter();
    first.setCondition(filterCond.remove(0));
    Filter current = first;
    for (ValueExpr cond : filterCond) {
        Filter filter = new Filter(null, cond);
        current.setArg(filter);
        current = filter;
    }

    TupleExpr te = projection.getArg();
    projection.setArg(first);
    current.setArg(te);

    return query;

}
 
Example 7
Source File: SpinParser.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitSelect(Resource select) throws RDF4JException {
	Value resultVars = TripleSources.singleValue(select, SP.RESULT_VARIABLES_PROPERTY, store);
	if (!(resultVars instanceof Resource)) {
		throw new MalformedSpinException(
				String.format("Value of %s is not a resource", SP.RESULT_VARIABLES_PROPERTY));
	}

	Map<String, ProjectionElem> oldProjElems = projElems;
	projElems = new LinkedHashMap<>();
	Projection projection = visitResultVariables((Resource) resultVars, oldProjElems);
	TupleExpr whereExpr = visitWhere(select);
	projection.setArg(whereExpr);

	Value groupBy = TripleSources.singleValue(select, SP.GROUP_BY_PROPERTY, store);
	if (groupBy instanceof Resource) {
		visitGroupBy((Resource) groupBy);
	}
	if (group != null) {
		group.setArg(projection.getArg());
		projection.setArg(group);
	}

	Value having = TripleSources.singleValue(select, SP.HAVING_PROPERTY, store);
	if (having instanceof Resource) {
		TupleExpr havingExpr = visitHaving((Resource) having);
		projection.setArg(havingExpr);
	}

	addSourceExpressions(projection, projElems.values());
	projElems = oldProjElems;

	Value orderby = TripleSources.singleValue(select, SP.ORDER_BY_PROPERTY, store);
	if (orderby instanceof Resource) {
		Order order = visitOrderBy((Resource) orderby);
		order.setArg(projection.getArg());
		projection.setArg(order);
	}

	boolean distinct = TripleSources.booleanValue(select, SP.DISTINCT_PROPERTY, store);
	if (distinct) {
		tupleRoot = new Distinct(tupleRoot);
	}

	long offset = -1L;
	Value offsetValue = TripleSources.singleValue(select, SP.OFFSET_PROPERTY, store);
	if (offsetValue instanceof Literal) {
		offset = ((Literal) offsetValue).longValue();
	}
	long limit = -1L;
	Value limitValue = TripleSources.singleValue(select, SP.LIMIT_PROPERTY, store);
	if (limitValue instanceof Literal) {
		limit = ((Literal) limitValue).longValue();
	}
	if (offset > 0L || limit >= 0L) {
		Slice slice = new Slice(tupleRoot);
		if (offset > 0L) {
			slice.setOffset(offset);
		}
		if (limit >= 0L) {
			slice.setLimit(limit);
		}
		tupleRoot = slice;
	}
}