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

The following examples show how to use org.eclipse.rdf4j.query.algebra.Filter#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: 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: TupleReArranger.java    From rya with Apache License 2.0 6 votes vote down vote up
private static List<TupleExpr> getFilterChain(List<Filter> filters) {
    List<TupleExpr> filterTopBottom = Lists.newArrayList();
    Filter filterChainTop = null;
    Filter filterChainBottom = null;

    for (Filter filter : filters) {
        if (filterChainTop == null) {
            filterChainTop = filter.clone();
        } else if (filterChainBottom == null) {
            filterChainBottom = filter.clone();
            filterChainTop.setArg(filterChainBottom);
        } else {
            Filter newFilter = filter.clone();
            filterChainBottom.setArg(newFilter);
            filterChainBottom = newFilter;
        }
    }
    if (filterChainTop != null) {
        filterTopBottom.add(filterChainTop);
    }
    if (filterChainBottom != null) {
        filterTopBottom.add(filterChainBottom);
    }
    return filterTopBottom;
}
 
Example 3
Source File: QueryNodesToTupleExpr.java    From rya with Apache License 2.0 6 votes vote down vote up
private static List<Filter> getFilterChain(Set<Filter> filters) {
    final List<Filter> filterTopBottom = Lists.newArrayList();
    Filter filterChainTop = null;
    Filter filterChainBottom = null;

    for (final Filter filter : filters) {
        if (filterChainTop == null) {
            filterChainTop = filter;
            filter.setParentNode(null);
        } else if (filterChainBottom == null) {
            filterChainBottom = filter;
            filterChainTop.setArg(filterChainBottom);
        } else {
            filterChainBottom.setArg(filter);
            filterChainBottom = filter;
        }
    }
    if (filterChainTop != null) {
        filterTopBottom.add(filterChainTop);
    }
    if (filterChainBottom != null) {
        filterTopBottom.add(filterChainBottom);
    }
    return filterTopBottom;
}
 
Example 4
Source File: FilterOptimizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void relocate(Filter filter, TupleExpr newFilterArg) {
	if (filter.getArg() != newFilterArg) {
		if (filter.getParentNode() != null) {
			// Remove filter from its original location
			filter.replaceWith(filter.getArg());
		}

		// Insert filter at the new location
		newFilterArg.replaceWith(filter);
		filter.setArg(newFilterArg);
	}
}
 
Example 5
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 6
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 7
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 8
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 9
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 10
Source File: FilterSerializer.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a {@link Filter} to a SPARQL query containing only the SPARQL representation
 * of the Filter along with a Select clause that return all variables.  The argument of the
 * Filter is replaced by a {@link SingletonSet} so that the body of the SPARQL query consists of only a
 * single Filter clause.  
 * @param filter - Filter to be serialized
 * @return - SPARQL String containing a single Filter clause that represents the serialized Filter
 * @throws FilterParseException
 */
public static String serialize(Filter filter) throws FilterParseException {
    Filter clone = filter.clone();
    clone.setArg(new SingletonSet());
    try {
        return removeAngularBracketsFromNonUriFunctions(renderer.render(new ParsedTupleQuery(clone)));
    } catch (Exception e) {
        throw new FilterParseException("Unable to parse Filter.", e);
    }
}
 
Example 11
Source File: PCJOptimizerUtilities.java    From rya with Apache License 2.0 5 votes vote down vote up
protected void relocate(final Filter filter, final TupleExpr newFilterArg) {
	if (!filter.getArg().equals(newFilterArg)) {
		if (filter.getParentNode() != null) {
			// Remove filter from its original location
			filter.replaceWith(filter.getArg());
		}
		// Insert filter at the new location
		newFilterArg.replaceWith(filter);
		filter.setArg(newFilterArg);
	}
}
 
Example 12
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;

}