Java Code Examples for org.eclipse.rdf4j.query.algebra.Filter#setCondition()

The following examples show how to use org.eclipse.rdf4j.query.algebra.Filter#setCondition() . 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: ConjunctiveConstraintSplitter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(Filter filter) {
	super.meet(filter);

	List<ValueExpr> conjunctiveConstraints = new ArrayList<>(16);
	getConjunctiveConstraints(filter.getCondition(), conjunctiveConstraints);

	TupleExpr filterArg = filter.getArg();

	for (int i = conjunctiveConstraints.size() - 1; i >= 1; i--) {
		Filter newFilter = new Filter(filterArg, conjunctiveConstraints.get(i));
		filterArg = newFilter;
	}

	filter.setCondition(conjunctiveConstraints.get(0));
	filter.setArg(filterArg);
}
 
Example 2
Source File: FilterRangeVisitor.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 arg = node.getCondition();
    if (arg instanceof FunctionCall) {
        final FunctionCall fc = (FunctionCall) arg;
        if (RANGE.stringValue().equals(fc.getURI())) {
            //range(?var, start, end)
            final List<ValueExpr> valueExprs = fc.getArgs();
            if (valueExprs.size() != 3) {
                throw new QueryEvaluationException("org.apache:range must have 3 parameters: variable, start, end");
            }
            final Var var = (Var) valueExprs.get(0);
            final ValueConstant startVc = (ValueConstant) valueExprs.get(1);
            final ValueConstant endVc = (ValueConstant) valueExprs.get(2);
            final Value start = startVc.getValue();
            final Value end = endVc.getValue();
            rangeValues.put(var, new RangeValue(start, end));
            node.setCondition(new ValueConstant(BooleanLiteral.TRUE));
        }
    }
}
 
Example 3
Source File: FilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Union union) {
	Filter clone = new Filter();
	clone.setCondition(filter.getCondition().clone());

	relocate(filter, union.getLeftArg());
	relocate(clone, union.getRightArg());

	FilterRelocator.relocate(filter);
	FilterRelocator.relocate(clone);
}
 
Example 4
Source File: FilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Difference node) {
	Filter clone = new Filter();
	clone.setCondition(filter.getCondition().clone());

	relocate(filter, node.getLeftArg());
	relocate(clone, node.getRightArg());

	FilterRelocator.relocate(filter);
	FilterRelocator.relocate(clone);
}
 
Example 5
Source File: FilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(Intersection node) {
	Filter clone = new Filter();
	clone.setCondition(filter.getCondition().clone());

	relocate(filter, node.getLeftArg());
	relocate(clone, node.getRightArg());

	FilterRelocator.relocate(filter);
	FilterRelocator.relocate(clone);
}
 
Example 6
Source File: TupleExprsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void isFilterExistsFunctionOnNormalFilter() {
	Filter expr = new Filter();
	expr.setArg(new StatementPattern());
	expr.setCondition(new Compare(new Var("x", f.createBNode()), new Var("y", f.createBNode())));

	assertThat(isFilterExistsFunction(expr)).isFalse();
}
 
Example 7
Source File: TupleExprsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void isFilterExistsFunctionOnNormalNot() {
	Filter expr = new Filter();
	expr.setArg(new StatementPattern());
	expr.setCondition(new Not(new Compare(new Var("x", f.createBNode()), new Var("y", f.createBNode()))));

	assertThat(isFilterExistsFunction(expr)).isFalse();
}
 
Example 8
Source File: TupleExprsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void isFilterExistsFunctionOnExists() {
	Filter expr = new Filter();
	expr.setArg(new StatementPattern());
	expr.setCondition(new Exists(new StatementPattern()));

	assertThat(isFilterExistsFunction(expr)).isTrue();

}
 
Example 9
Source File: TupleExprsTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void isFilterExistsFunctionOnNotExist() {
	Filter expr = new Filter();
	expr.setArg(new StatementPattern());
	expr.setCondition(new Not(new Exists(new StatementPattern())));

	assertThat(isFilterExistsFunction(expr)).isTrue();
}
 
Example 10
Source File: BasicGroup.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private TupleExpr filteredTuple(TupleExpr theExpr) {
	TupleExpr aExpr = theExpr;

	for (ValueExpr aValEx : mFilters) {
		Filter aFilter = new Filter();
		aFilter.setCondition(aValEx);
		aFilter.setArg(aExpr);
		aExpr = aFilter;
	}

	return aExpr;
}
 
Example 11
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 12
Source File: FilterOptimizer.java    From CostFed with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void meet(Filter filter)  {
	
	if (filter.getArg() instanceof EmptyResult) {
		log.debug("Argument of filter expression does not yield results at the provided sources, replacing Filter node.");
		filter.replaceWith(filter.getArg());
		return;
	}
				
	/*
	 * TODO idea:
	 * if we have a FILTER such as ?s='a' OR ?s='b' OR ?s='c' handle this appropriately
	 */
	
	ValueExpr valueExpr = filter.getCondition();
	
	/*
	 * TODO transform condition into some normal form, e.g. CNF
	 */
	
	// determine conjunctive expressions
	List<ValueExpr> conjunctiveExpressions = new ArrayList<ValueExpr>();
	getConjunctiveExpressions(valueExpr, conjunctiveExpressions);
			
	FilterExprInsertVisitor filterExprVst = new FilterExprInsertVisitor();
	List<ValueExpr> remainingExpr = new ArrayList<ValueExpr>(conjunctiveExpressions.size());
	
	for (ValueExpr cond : conjunctiveExpressions) {
		
		/*
		 * Determine if this filter is applicable for optimization.
		 * Currently only leaf expressions are applicable, i.e.
		 * not combined expressions.
		 */
		if (isCompatibleExpr(cond)) {
						
			HashSet<String> exprVars = new VarFinder().findVars(cond);
			FilterExpr filterExpr = new FilterExpr(cond, exprVars);
			
			filterExprVst.initialize(filterExpr);
			filter.getArg().visit(filterExprVst);
			
			// if the filter expr. is handled in the stmt we do not have to keep it
			if (filterExprVst.canRemove())
				continue;
			
			remainingExpr.add(filterExpr.getExpression());
			
		} else {
			remainingExpr.add(cond);
		}
		
	}
	
	if (remainingExpr.size()==0) {
		filter.replaceWith(filter.getArg()); 	// remove the filter			
	}
	
	else if (remainingExpr.size()==1) {
		filter.setCondition(remainingExpr.get(0));		// just apply the remaining condition
	}
	
	else {
		
		// construct conjunctive value expr
		And root = new And();	
		root.setLeftArg(remainingExpr.get(0));
		And tmp = root;
		for (int i=1; i<remainingExpr.size()-1; i++) {
			And _a = new And();
			_a.setLeftArg(remainingExpr.get(i));
			tmp.setRightArg(_a);
			tmp = _a;				
		}
		tmp.setRightArg(remainingExpr.get(remainingExpr.size()-1));
		
		filter.setCondition(root);
	}
	
}
 
Example 13
Source File: FilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void meet(Filter filter) {

	if (filter.getArg() instanceof EmptyResult) {
		log.debug(
				"Argument of filter expression does not yield results at the provided sources, replacing Filter node.");
		filter.replaceWith(filter.getArg());
		return;
	}

	/*
	 * TODO idea: if we have a FILTER such as ?s='a' OR ?s='b' OR ?s='c' handle this appropriately
	 */

	ValueExpr valueExpr = filter.getCondition();

	/*
	 * TODO transform condition into some normal form, e.g. CNF
	 */

	// determine conjunctive expressions
	List<ValueExpr> conjunctiveExpressions = new ArrayList<>();
	getConjunctiveExpressions(valueExpr, conjunctiveExpressions);

	FilterExprInsertVisitor filterExprVst = new FilterExprInsertVisitor();
	List<ValueExpr> remainingExpr = new ArrayList<>(conjunctiveExpressions.size());

	for (ValueExpr cond : conjunctiveExpressions) {

		/*
		 * Determine if this filter is applicable for optimization. Currently only leaf expressions are applicable,
		 * i.e. not combined expressions.
		 */
		if (isCompatibleExpr(cond)) {

			HashSet<String> exprVars = new VarFinder().findVars(cond);
			FilterExpr filterExpr = new FilterExpr(cond, exprVars);

			filterExprVst.initialize(filterExpr);
			filter.getArg().visit(filterExprVst);

			// if the filter expr. is handled in the stmt we do not have to keep it
			if (filterExprVst.canRemove()) {
				continue;
			}

			remainingExpr.add(filterExpr.getExpression());

		} else {
			remainingExpr.add(cond);
		}

	}

	if (remainingExpr.isEmpty()) {
		filter.replaceWith(filter.getArg()); // remove the filter
	} else if (remainingExpr.size() == 1) {
		filter.setCondition(remainingExpr.get(0)); // just apply the remaining condition
	} else {

		// construct conjunctive value expr
		And root = new And();
		root.setLeftArg(remainingExpr.get(0));
		And tmp = root;
		for (int i = 1; i < remainingExpr.size() - 1; i++) {
			And _a = new And();
			_a.setLeftArg(remainingExpr.get(i));
			tmp.setRightArg(_a);
			tmp = _a;
		}
		tmp.setRightArg(remainingExpr.get(remainingExpr.size() - 1));

		filter.setCondition(root);
	}

}