Java Code Examples for org.eclipse.rdf4j.query.algebra.evaluation.util.QueryEvaluationUtil#getEffectiveBooleanValue()

The following examples show how to use org.eclipse.rdf4j.query.algebra.evaluation.util.QueryEvaluationUtil#getEffectiveBooleanValue() . 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: FederationEvalStrategy.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
public Value evaluate(ConjunctiveFilterExpr node, BindingSet bindings) {
	
	ValueExprEvaluationException error = null;
	
	for (FilterExpr expr : node.getExpressions()) {
		
		try {
			Value v = evaluate(expr.getExpression(), bindings);
			if (QueryEvaluationUtil.getEffectiveBooleanValue(v) == false) {
				return BooleanLiteral.FALSE;
			}
		} catch (ValueExprEvaluationException e) {
			error = e;
		}
	}
	
	if (error!=null)
		throw error;
	
	return BooleanLiteral.TRUE;
}
 
Example 2
Source File: FederationEvalStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Value evaluate(ConjunctiveFilterExpr node, BindingSet bindings)
		throws ValueExprEvaluationException, QueryEvaluationException {

	ValueExprEvaluationException error = null;

	for (FilterExpr expr : node.getExpressions()) {

		try {
			Value v = evaluate(expr.getExpression(), bindings);
			if (QueryEvaluationUtil.getEffectiveBooleanValue(v) == false) {
				return BooleanLiteral.FALSE;
			}
		} catch (ValueExprEvaluationException e) {
			error = e;
		}
	}

	if (error != null) {
		throw error;
	}

	return BooleanLiteral.TRUE;
}
 
Example 3
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Value evaluate(If node, BindingSet bindings) throws QueryEvaluationException {
	Value result;

	boolean conditionIsTrue;

	try {
		Value value = evaluate(node.getCondition(), bindings);
		conditionIsTrue = QueryEvaluationUtil.getEffectiveBooleanValue(value);
	} catch (ValueExprEvaluationException e) {
		// in case of type error, if-construction should result in empty
		// binding.
		return null;
	}

	if (conditionIsTrue) {
		result = evaluate(node.getResult(), bindings);
	} else {
		result = evaluate(node.getAlternative(), bindings);
	}
	return result;
}
 
Example 4
Source File: QueryModelNormalizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void meet(Filter node) {
	super.meet(node);

	TupleExpr arg = node.getArg();
	ValueExpr condition = node.getCondition();

	if (arg instanceof EmptySet) {
		// see #meetUnaryTupleOperator
	} else if (condition instanceof ValueConstant) {
		boolean conditionValue;
		try {
			conditionValue = QueryEvaluationUtil.getEffectiveBooleanValue(((ValueConstant) condition).getValue());
		} catch (ValueExprEvaluationException e) {
			conditionValue = false;
		}

		if (conditionValue == false) {
			// Constraint is always false
			node.replaceWith(new EmptySet());
		} else {
			node.replaceWith(arg);
		}
	}
}
 
Example 5
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate an {@link If} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return
 * @throws QueryEvaluationException
 */
private Value evaluate(If node, BindingSet bindings) throws QueryEvaluationException {
    Value result;
    boolean conditionIsTrue;
    try {
        Value value = evaluate(node.getCondition(), bindings);
        conditionIsTrue = QueryEvaluationUtil.getEffectiveBooleanValue(value);
    } catch (ValueExprEvaluationException e) {
        // in case of type error, if-construction should result in empty
        // binding.
        return null;
    }
    if (conditionIsTrue) {
        result = evaluate(node.getResult(), bindings);
    } else {
        result = evaluate(node.getAlternative(), bindings);
    }
    return result;
}
 
Example 6
Source File: FilterEvaluator.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Checks to see if a {@link VisibilityBindingSet} should be included in the results or not.
 *
 * @param bs - The value that will be evaluated against the filter. (not null)
 * @return {@code true} if the binding set matches the filter and it should be included in the node's results,
 *   otherwise {@code false} and it should be excluded.
 */
public boolean filter(final VisibilityBindingSet bs) {
    requireNonNull(bs);

    try {
        final Value result = EVALUATOR.evaluate(condition, bs);
        return QueryEvaluationUtil.getEffectiveBooleanValue(result);
    } catch (final QueryEvaluationException e) {
        //False returned because for whatever reason, the ValueExpr could not be evaluated.
        //In the event that the ValueExpr is a FunctionCall, this Exception will be generated if
        //the Function IRI is a valid IRI that was found in the FunctionRegistry, but the arguments
        //for that Function could not be parsed.
        log.error("Could not evaluate a Filter.", e);
        return false;
    }
}
 
Example 7
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(LeftJoin leftJoin) {
	super.meet(leftJoin);
	TupleExpr leftArg = leftJoin.getLeftArg();
	TupleExpr rightArg = leftJoin.getRightArg();
	ValueExpr condition = leftJoin.getCondition();
	if (leftArg instanceof EmptySet) {
		leftJoin.replaceWith(leftArg);
	} else if (rightArg instanceof EmptySet) {
		leftJoin.replaceWith(leftArg);
	} else if (rightArg instanceof SingletonSet) {
		leftJoin.replaceWith(leftArg);
	} else if (condition instanceof ValueConstant) {
		boolean conditionValue;
		try {
			conditionValue = QueryEvaluationUtil
					.getEffectiveBooleanValue(((ValueConstant) condition).getValue());
		} catch (ValueExprEvaluationException e) {
			conditionValue = false;
		}
		if (conditionValue) {
			leftJoin.setCondition(null);
		} else {
			// Constraint is always false
			leftJoin.replaceWith(leftArg);
		}
	}
}
 
Example 8
Source File: QueryModelPruner.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(LeftJoin leftJoin) {
	super.meet(leftJoin);

	TupleExpr leftArg = leftJoin.getLeftArg();
	TupleExpr rightArg = leftJoin.getRightArg();
	ValueExpr condition = leftJoin.getCondition();

	if (leftArg instanceof EmptySet) {
		leftJoin.replaceWith(leftArg);
	} else if (rightArg instanceof EmptySet) {
		leftJoin.replaceWith(leftArg);
	} else if (rightArg instanceof SingletonSet) {
		leftJoin.replaceWith(leftArg);
	} else if (condition instanceof ValueConstant) {
		boolean conditionValue;
		try {
			conditionValue = QueryEvaluationUtil
					.getEffectiveBooleanValue(((ValueConstant) condition).getValue());
		} catch (ValueExprEvaluationException e) {
			conditionValue = false;
		}

		if (conditionValue == false) {
			// Constraint is always false
			leftJoin.replaceWith(leftArg);
		} else {
			leftJoin.setCondition(null);
		}
	}
}
 
Example 9
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean isTrue(ValueExpr expr, BindingSet bindings) throws QueryEvaluationException {
	try {
		Value value = evaluate(expr, bindings);
		return QueryEvaluationUtil.getEffectiveBooleanValue(value);
	} catch (ValueExprEvaluationException e) {
		return false;
	}
}
 
Example 10
Source File: QueryModelNormalizer.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void meet(LeftJoin leftJoin) {
	super.meet(leftJoin);

	TupleExpr leftArg = leftJoin.getLeftArg();
	TupleExpr rightArg = leftJoin.getRightArg();
	ValueExpr condition = leftJoin.getCondition();

	if (leftArg instanceof EmptySet) {
		leftJoin.replaceWith(leftArg);
	} else if (rightArg instanceof EmptySet) {
		leftJoin.replaceWith(leftArg);
	} else if (rightArg instanceof SingletonSet) {
		leftJoin.replaceWith(leftArg);
	} else if (condition instanceof ValueConstant) {
		boolean conditionValue;
		try {
			conditionValue = QueryEvaluationUtil.getEffectiveBooleanValue(((ValueConstant) condition).getValue());
		} catch (ValueExprEvaluationException e) {
			conditionValue = false;
		}

		if (conditionValue == false) {
			// Constraint is always false
			leftJoin.replaceWith(leftArg);
		} else {
			leftJoin.setCondition(null);
		}
	}
}
 
Example 11
Source File: TupleFunctionFederatedService.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean ask(Service service, BindingSet bindings, String baseUri) throws QueryEvaluationException {
	try (final CloseableIteration<BindingSet, QueryEvaluationException> iter = evaluate(service,
			new SingletonIteration<>(bindings), baseUri);) {
		while (iter.hasNext()) {
			BindingSet bs = iter.next();
			String firstVar = service.getBindingNames().iterator().next();
			return QueryEvaluationUtil.getEffectiveBooleanValue(bs.getValue(firstVar));
		}
	}
	return false;
}
 
Example 12
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Determines the "effective boolean value" of the {@link Value} returned by evaluating the expression.
 * See {@link QueryEvaluationUtil#getEffectiveBooleanValue(Value)} for the definition of "effective boolean value.
 * @param expr
 * @param bindings the set of named value bindings
 * @return
 * @throws QueryEvaluationException
 */
boolean isTrue(ValueExpr expr, BindingSet bindings) throws QueryEvaluationException {
    try {
        Value value = evaluate(expr, bindings);
        return QueryEvaluationUtil.getEffectiveBooleanValue(value);
    } catch (ValueExprEvaluationException e) {
        return false;
    }
}
 
Example 13
Source File: FilterResultUpdater.java    From rya with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluate a {@link BindingSet} to see if it is accepted by a filter's condition.
 *
 * @param condition - The filter condition. (not null)
 * @param bindings - The binding set to evaluate. (not null)
 * @return {@code true} if the binding set is accepted by the filter; otherwise {@code false}.
 * @throws QueryEvaluationException The condition couldn't be evaluated. In the case that the ValueExpr is a
 *             {@link FunctionCall}, this Exception is thrown because the Function could not be found in the
 *             {@link FunctionRegistry}.
 */
private static boolean isTrue(final ValueExpr condition, final BindingSet bindings) throws QueryEvaluationException {
    try {
        final Value value = evaluator.evaluate(condition, bindings);
        return QueryEvaluationUtil.getEffectiveBooleanValue(value);
    } catch (final ValueExprEvaluationException e) {
        //False returned because for whatever reason, the ValueExpr could not be evaluated.
        //In the event that the ValueExpr is a FunctionCall, this Exception will be generated if
        //the Function URI is a valid URI that was found in the FunctionRegistry, but the arguments
        //for that Function could not be parsed.
        return false;
    }
}
 
Example 14
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Value evaluate(Not node, BindingSet bindings) throws QueryEvaluationException {
	Value argValue = evaluate(node.getArg(), bindings);
	boolean argBoolean = QueryEvaluationUtil.getEffectiveBooleanValue(argValue);
	return BooleanLiteral.valueOf(!argBoolean);
}
 
Example 15
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 2 votes vote down vote up
/**
 * Evaluate a {@link Not} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(Not node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value argValue = evaluate(node.getArg(), bindings);
    boolean argBoolean = QueryEvaluationUtil.getEffectiveBooleanValue(argValue);
    return BooleanLiteral.valueOf(!argBoolean);
}