org.eclipse.rdf4j.query.algebra.evaluation.util.QueryEvaluationUtil Java Examples

The following examples show how to use org.eclipse.rdf4j.query.algebra.evaluation.util.QueryEvaluationUtil. 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: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Value evaluate(Label node, BindingSet bindings)
		throws QueryEvaluationException {
	// FIXME: deprecate Label in favour of Str(?)
	Value argValue = evaluate(node.getArg(), bindings);

	if (argValue instanceof Literal) {
		Literal literal = (Literal) argValue;

		if (QueryEvaluationUtil.isSimpleLiteral(literal)) {
			return literal;
		} else {
			return tripleSource.getValueFactory().createLiteral(literal.getLabel());
		}
	} else {
		throw new ValueExprEvaluationException();
	}
}
 
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(Str node, BindingSet bindings) throws QueryEvaluationException {
	Value argValue = evaluate(node.getArg(), bindings);

	if (argValue instanceof IRI) {
		return tripleSource.getValueFactory().createLiteral(argValue.toString());
	} else if (argValue instanceof Literal) {
		Literal literal = (Literal) argValue;

		if (QueryEvaluationUtil.isSimpleLiteral(literal)) {
			return literal;
		} else {
			return tripleSource.getValueFactory().createLiteral(literal.getLabel());
		}
	} else if (argValue instanceof Triple) {
		return tripleSource.getValueFactory().createLiteral(argValue.toString());
	} else {
		throw new ValueExprEvaluationException();
	}
}
 
Example #4
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Value evaluate(LangMatches node, BindingSet bindings)
		throws QueryEvaluationException {
	Value langTagValue = evaluate(node.getLeftArg(), bindings);
	Value langRangeValue = evaluate(node.getRightArg(), bindings);

	if (QueryEvaluationUtil.isSimpleLiteral(langTagValue) && QueryEvaluationUtil.isSimpleLiteral(langRangeValue)) {
		String langTag = ((Literal) langTagValue).getLabel();
		String langRange = ((Literal) langRangeValue).getLabel();

		boolean result = false;
		if (langRange.equals("*")) {
			result = langTag.length() > 0;
		} else if (langTag.length() == langRange.length()) {
			result = langTag.equalsIgnoreCase(langRange);
		} else if (langTag.length() > langRange.length()) {
			// check if the range is a prefix of the tag
			String prefix = langTag.substring(0, langRange.length());
			result = prefix.equalsIgnoreCase(langRange) && langTag.charAt(langRange.length()) == '-';
		}

		return BooleanLiteral.valueOf(result);
	}

	throw new ValueExprEvaluationException();

}
 
Example #5
Source File: CastFunction.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException(
				getXsdName() + " cast requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];
		IRI datatype = literal.getDatatype();

		if (QueryEvaluationUtil.isStringLiteral(literal)) {
			String lexicalValue = XMLDatatypeUtil.collapseWhiteSpace(literal.getLabel());
			if (isValidForDatatype(lexicalValue)) {
				return valueFactory.createLiteral(lexicalValue, getXsdDatatype());
			}
		} else if (datatype != null) {
			if (datatype.equals(getXsdDatatype())) {
				return literal;
			}
		}
		return convert(valueFactory, literal);
	} else {
		return convert(valueFactory, args[0]);
	}
}
 
Example #6
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 #7
Source File: SHA256.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("SHA256 requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];

		if (QueryEvaluationUtil.isSimpleLiteral(literal) || XMLSchema.STRING.equals(literal.getDatatype())) {
			String lexValue = literal.getLabel();

			try {
				return valueFactory.createLiteral(hash(lexValue, "SHA-256"));
			} catch (NoSuchAlgorithmException e) {
				// SHA256 should always be available.
				throw new RuntimeException(e);
			}
		} else {
			throw new ValueExprEvaluationException("Invalid argument for SHA256: " + literal);
		}
	} else {
		throw new ValueExprEvaluationException("Invalid argument for SHA256: " + args[0]);
	}
}
 
Example #8
Source File: MD5.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("MD5 requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];

		if (QueryEvaluationUtil.isSimpleLiteral(literal) || XMLSchema.STRING.equals(literal.getDatatype())) {
			String lexValue = literal.getLabel();

			try {
				return valueFactory.createLiteral(hash(lexValue, "MD5"));
			} catch (NoSuchAlgorithmException e) {
				// MD5 should always be available.
				throw new RuntimeException(e);
			}
		} else {
			throw new ValueExprEvaluationException("Invalid argument for MD5: " + literal);
		}
	} else {
		throw new ValueExprEvaluationException("Invalid argument for Md5: " + args[0]);
	}
}
 
Example #9
Source File: SHA1.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("SHA1 requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];

		if (QueryEvaluationUtil.isSimpleLiteral(literal) || XMLSchema.STRING.equals(literal.getDatatype())) {
			String lexValue = literal.getLabel();

			try {
				return valueFactory.createLiteral(hash(lexValue, "SHA1"));
			} catch (NoSuchAlgorithmException e) {
				// SHA1 should always be available.
				throw new RuntimeException(e);
			}
		} else {
			throw new ValueExprEvaluationException("Invalid argument for SHA1: " + literal);
		}
	} else {
		throw new ValueExprEvaluationException("Invalid argument for SHA1: " + args[0]);
	}
}
 
Example #10
Source File: SHA384.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("SHA384 requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];

		if (QueryEvaluationUtil.isSimpleLiteral(literal) || XMLSchema.STRING.equals(literal.getDatatype())) {
			String lexValue = literal.getLabel();

			try {
				return valueFactory.createLiteral(hash(lexValue, "SHA-384"));
			} catch (NoSuchAlgorithmException e) {
				// SHA384 should always be available.
				throw new RuntimeException(e);
			}
		} else {
			throw new ValueExprEvaluationException("Invalid argument for SHA384: " + literal);
		}
	} else {
		throw new ValueExprEvaluationException("Invalid argument for SHA384: " + args[0]);
	}
}
 
Example #11
Source File: SHA512.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("SHA512 requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];

		if (QueryEvaluationUtil.isSimpleLiteral(literal) || XMLSchema.STRING.equals(literal.getDatatype())) {
			String lexValue = literal.getLabel();

			try {
				return valueFactory.createLiteral(hash(lexValue, "SHA-512"));
			} catch (NoSuchAlgorithmException e) {
				// SHA512 should always be available.
				throw new RuntimeException(e);
			}
		} else {
			throw new ValueExprEvaluationException("Invalid argument for SHA512: " + literal);
		}
	} else {
		throw new ValueExprEvaluationException("Invalid argument for SHA512: " + args[0]);
	}
}
 
Example #12
Source File: StrDt.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 2) {
		throw new ValueExprEvaluationException("STRDT requires 2 arguments, got " + args.length);
	}

	Value lexicalValue = args[0];
	Value datatypeValue = args[1];

	if (QueryEvaluationUtil.isSimpleLiteral(lexicalValue)) {
		Literal lit = (Literal) lexicalValue;
		if (datatypeValue instanceof IRI) {
			return valueFactory.createLiteral(lit.getLabel(), (IRI) datatypeValue);
		} else {
			throw new ValueExprEvaluationException("illegal value for operand: " + datatypeValue);
		}
	} else {
		throw new ValueExprEvaluationException("illegal value for operand: " + lexicalValue);
	}
}
 
Example #13
Source File: StrLang.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 2) {
		throw new ValueExprEvaluationException("STRLANG requires 2 arguments, got " + args.length);
	}

	Value lexicalValue = args[0];
	Value languageValue = args[1];

	if (QueryEvaluationUtil.isSimpleLiteral(lexicalValue)) {
		Literal lit = (Literal) lexicalValue;

		if (languageValue instanceof Literal) {
			return valueFactory.createLiteral(lit.getLabel(), ((Literal) languageValue).getLabel());
		} else {
			throw new ValueExprEvaluationException("illegal value for operand: " + languageValue);
		}
	} else {
		throw new ValueExprEvaluationException("illegal value for operand: " + lexicalValue);
	}

}
 
Example #14
Source File: StrEnds.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 2) {
		throw new ValueExprEvaluationException("STRENDS requires 2 arguments, got " + args.length);
	}

	Value leftVal = args[0];
	Value rightVal = args[1];

	if (leftVal instanceof Literal && rightVal instanceof Literal) {
		Literal leftLit = (Literal) leftVal;
		Literal rightLit = (Literal) rightVal;

		if (QueryEvaluationUtil.compatibleArguments(leftLit, rightLit)) {

			String leftLexVal = leftLit.getLabel();
			String rightLexVal = rightLit.getLabel();

			return BooleanLiteral.valueOf(leftLexVal.endsWith(rightLexVal));
		} else {
			throw new ValueExprEvaluationException("incompatible operands for STRENDS function");
		}
	} else {
		throw new ValueExprEvaluationException("STRENDS function expects literal operands");
	}
}
 
Example #15
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Value evaluate(CompareAny node, BindingSet bindings)
		throws QueryEvaluationException {
	Value leftValue = evaluate(node.getArg(), bindings);

	// Result is false until a match has been found
	boolean result = false;

	// Use first binding name from tuple expr to compare values
	String bindingName = node.getSubQuery().getBindingNames().iterator().next();

	try (CloseableIteration<BindingSet, QueryEvaluationException> iter = evaluate(node.getSubQuery(), bindings)) {
		while (!result && iter.hasNext()) {
			BindingSet bindingSet = iter.next();

			Value rightValue = bindingSet.getValue(bindingName);

			try {
				result = QueryEvaluationUtil.compare(leftValue, rightValue, node.getOperator());
			} catch (ValueExprEvaluationException e) {
				// ignore, maybe next value will match
			}
		}
	}

	return BooleanLiteral.valueOf(result);
}
 
Example #16
Source File: EncodeForUri.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 1) {
		throw new ValueExprEvaluationException("ENCODE_FOR_URI requires exactly 1 argument, got " + args.length);
	}

	if (args[0] instanceof Literal) {
		Literal literal = (Literal) args[0];

		if (QueryEvaluationUtil.isStringLiteral(literal)) {
			String lexValue = literal.getLabel();

			return valueFactory.createLiteral(encodeUri(lexValue));
		} else {
			throw new ValueExprEvaluationException("Invalid argument for ENCODE_FOR_URI: " + literal);
		}
	} else {
		throw new ValueExprEvaluationException("Invalid argument for ENCODE_FOR_URI: " + args[0]);
	}
}
 
Example #17
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 #18
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 #19
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 #20
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate a {@link CompareAll} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(CompareAll node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value leftValue = evaluate(node.getArg(), bindings);
    // Result is true until a mismatch has been found
    boolean result = true;
    // Use first binding name from tuple expr to compare values
    String bindingName = node.getSubQuery().getBindingNames().iterator().next();
    try (CloseableIteration<BindingSet, QueryEvaluationException> iter = parentStrategy.evaluate(node.getSubQuery(), bindings)) {
        while (result == true && iter.hasNext()) {
            BindingSet bindingSet = iter.next();
            Value rightValue = bindingSet.getValue(bindingName);
            try {
                result = QueryEvaluationUtil.compare(leftValue, rightValue, node.getOperator());
            } catch (ValueExprEvaluationException e) {
                // Exception thrown by ValueCompare.isTrue(...)
                result = false;
            }
        }
    }
    return BooleanLiteral.valueOf(result);
}
 
Example #21
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate a {@link CompareAny} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(CompareAny node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value leftValue = evaluate(node.getArg(), bindings);
    // Result is false until a match has been found
    boolean result = false;
    // Use first binding name from tuple expr to compare values
    String bindingName = node.getSubQuery().getBindingNames().iterator().next();
    try (CloseableIteration<BindingSet, QueryEvaluationException> iter = parentStrategy.evaluate(node.getSubQuery(), bindings)) {
        while (result == false && iter.hasNext()) {
            BindingSet bindingSet = iter.next();
            Value rightValue = bindingSet.getValue(bindingName);
            try {
                result = QueryEvaluationUtil.compare(leftValue, rightValue, node.getOperator());
            } catch (ValueExprEvaluationException e) {
                // ignore, maybe next value will match
            }
        }
    }
    return BooleanLiteral.valueOf(result);
}
 
Example #22
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate a {@link Str} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return a literal representation of the evaluation: a URI, the value of a simple literal or the label of any other literal
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(Str node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value argValue = evaluate(node.getArg(), bindings);
    if (argValue instanceof URI) {
        return valueFactory.createLiteral(argValue.toString());
    } else if (argValue instanceof Literal) {
        Literal literal = (Literal) argValue;
        if (QueryEvaluationUtil.isSimpleLiteral(literal)) {
            return literal;
        } else {
            return valueFactory.createLiteral(literal.getLabel());
        }
    } else {
        throw new ValueExprEvaluationException();
    }
}
 
Example #23
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 6 votes vote down vote up
/**
 * Determines whether the language tag or the node matches the language argument of the node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(LangMatches node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value langTagValue = evaluate(node.getLeftArg(), bindings);
    Value langRangeValue = evaluate(node.getRightArg(), bindings);
    if (QueryEvaluationUtil.isSimpleLiteral(langTagValue)
            && QueryEvaluationUtil.isSimpleLiteral(langRangeValue)) {
        String langTag = ((Literal) langTagValue).getLabel();
        String langRange = ((Literal) langRangeValue).getLabel();
        boolean result = false;
        if (langRange.equals("*")) {
            result = langTag.length() > 0;
        } else if (langTag.length() == langRange.length()) {
            result = langTag.equalsIgnoreCase(langRange);
        } else if (langTag.length() > langRange.length()) {
            // check if the range is a prefix of the tag
            String prefix = langTag.substring(0, langRange.length());
            result = prefix.equalsIgnoreCase(langRange) && langTag.charAt(langRange.length()) == '-';
        }
        return BooleanLiteral.valueOf(result);
    }
    throw new ValueExprEvaluationException();
}
 
Example #24
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 #25
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluate a {@link Label} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return the {@link Literal} resulting from the evaluation of the argument of the {@code Label}.
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(Label node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    // FIXME: deprecate Label in favour of Str(?)
    Value argValue = evaluate(node.getArg(), bindings);
    if (argValue instanceof Literal) {
        Literal literal = (Literal) argValue;
        if (QueryEvaluationUtil.isSimpleLiteral(literal)) {
            return literal;
        } else {
            return valueFactory.createLiteral(literal.getLabel());
        }
    } else {
        throw new ValueExprEvaluationException();
    }
}
 
Example #26
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 #27
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Value evaluate(Compare node, BindingSet bindings)
		throws QueryEvaluationException {
	Value leftVal = evaluate(node.getLeftArg(), bindings);
	Value rightVal = evaluate(node.getRightArg(), bindings);

	return BooleanLiteral.valueOf(QueryEvaluationUtil.compare(leftVal, rightVal, node.getOperator()));
}
 
Example #28
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluate a {@link ListMemberOperator}
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(ListMemberOperator node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    List<ValueExpr> args = node.getArguments();
    Value leftValue = evaluate(args.get(0), bindings);
    boolean result = false;
    ValueExprEvaluationException typeError = null;
    for (int i = 1; i < args.size(); i++) {
        ValueExpr arg = args.get(i);
        try {
            Value rightValue = evaluate(arg, bindings);
            result = leftValue == null && rightValue == null;
            if (!result) {
                result = QueryEvaluationUtil.compare(leftValue, rightValue, Compare.CompareOp.EQ);
            }
            if (result) {
                break;
            }
        } catch (ValueExprEvaluationException caught) {
            typeError = caught;
        }
    }
    if (typeError != null && !result) {
        // cf. SPARQL spec a type error is thrown if the value is not in the
        // list and one of the list members caused a type error in the
        // comparison.
        throw typeError;
    }
    return BooleanLiteral.valueOf(result);
}
 
Example #29
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 #30
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;
    }
}