Java Code Examples for org.eclipse.rdf4j.model.impl.BooleanLiteral#valueOf()

The following examples show how to use org.eclipse.rdf4j.model.impl.BooleanLiteral#valueOf() . 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: 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 2
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 3
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 4
Source File: ExtendedEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Value evaluate(Compare node, BindingSet bindings)
		throws ValueExprEvaluationException, QueryEvaluationException {
	Value leftVal = evaluate(node.getLeftArg(), bindings);
	Value rightVal = evaluate(node.getRightArg(), bindings);

	// return result of non-strict comparisson.
	return BooleanLiteral.valueOf(QueryEvaluationUtil.compare(leftVal, rightVal, node.getOperator(), false));
}
 
Example 5
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Determines whether the operand (a variable) contains a numeric datatyped literal, i.e. a literal with datatype xsd:float, xsd:double, xsd:decimal, or a
 * derived datatype of xsd:decimal.
 *
 * @return <tt>true</tt> if the operand contains a numeric datatyped literal,
 * <tt>false</tt> otherwise.
 */
private Value evaluate(IsNumeric node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value argValue = evaluate(node.getArg(), bindings);
    if (argValue instanceof Literal) {
        Literal lit = (Literal) argValue;
        IRI datatype = lit.getDatatype();
        return BooleanLiteral.valueOf(XMLDatatypeUtil.isNumericDatatype(datatype));
    } else {
        return BooleanLiteral.FALSE;
    }
}
 
Example 6
Source File: StrStarts.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 2) {
		throw new ValueExprEvaluationException("STRSTARTS 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.startsWith(rightLexVal));
		} else {
			throw new ValueExprEvaluationException("incompatible operands for STRSTARTS function");
		}
	} else {
		throw new ValueExprEvaluationException("STRSTARTS function expects literal operands");
	}

}
 
Example 7
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Determines whether the two operands match according to the <code>regex</code> operator.
 *
 * @return <tt>true</tt> if the operands match according to the <tt>regex</tt> operator, <tt>false</tt> otherwise.
 */
public Value evaluate(Regex node, BindingSet bindings)
		throws QueryEvaluationException {
	Value arg = evaluate(node.getArg(), bindings);
	Value parg = evaluate(node.getPatternArg(), bindings);
	Value farg = null;
	ValueExpr flagsArg = node.getFlagsArg();
	if (flagsArg != null) {
		farg = evaluate(flagsArg, bindings);
	}

	if (QueryEvaluationUtil.isStringLiteral(arg) && QueryEvaluationUtil.isSimpleLiteral(parg)
			&& (farg == null || QueryEvaluationUtil.isSimpleLiteral(farg))) {
		String text = ((Literal) arg).getLabel();
		String ptn = ((Literal) parg).getLabel();
		String flags = "";
		if (farg != null) {
			flags = ((Literal) farg).getLabel();
		}
		// TODO should this Pattern be cached?
		int f = 0;
		for (char c : flags.toCharArray()) {
			switch (c) {
			case 's':
				f |= Pattern.DOTALL;
				break;
			case 'm':
				f |= Pattern.MULTILINE;
				break;
			case 'i':
				f |= Pattern.CASE_INSENSITIVE;
				f |= Pattern.UNICODE_CASE;
				break;
			case 'x':
				f |= Pattern.COMMENTS;
				break;
			case 'd':
				f |= Pattern.UNIX_LINES;
				break;
			case 'u':
				f |= Pattern.UNICODE_CASE;
				break;
			case 'q':
				f |= Pattern.LITERAL;
				break;
			default:
				throw new ValueExprEvaluationException(flags);
			}
		}
		Pattern pattern = Pattern.compile(ptn, f);
		boolean result = pattern.matcher(text).find();
		return BooleanLiteral.valueOf(result);
	}

	throw new ValueExprEvaluationException();
}
 
Example 8
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Determines whether the operand (a variable) contains a Literal.
 *
 * @return <tt>true</tt> if the operand contains a Literal, <tt>false</tt> otherwise.
 */
public Value evaluate(IsLiteral node, BindingSet bindings)
		throws QueryEvaluationException {
	Value argValue = evaluate(node.getArg(), bindings);
	return BooleanLiteral.valueOf(argValue instanceof Literal);
}
 
Example 9
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Determines whether the operand (a variable) contains a BNode.
 *
 * @return <tt>true</tt> if the operand contains a BNode, <tt>false</tt> otherwise.
 */
public Value evaluate(IsBNode node, BindingSet bindings)
		throws QueryEvaluationException {
	Value argValue = evaluate(node.getArg(), bindings);
	return BooleanLiteral.valueOf(argValue instanceof BNode);
}
 
Example 10
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Determines whether the operand (a variable) contains a URI.
 *
 * @return <tt>true</tt> if the operand contains a URI, <tt>false</tt> otherwise.
 */
public Value evaluate(IsURI node, BindingSet bindings)
		throws QueryEvaluationException {
	Value argValue = evaluate(node.getArg(), bindings);
	return BooleanLiteral.valueOf(argValue instanceof IRI);
}
 
Example 11
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Determines whether the operand (a variable) contains a Resource.
 *
 * @return <tt>true</tt> if the operand contains a Resource, <tt>false</tt> otherwise.
 */
public Value evaluate(IsResource node, BindingSet bindings)
		throws QueryEvaluationException {
	Value argValue = evaluate(node.getArg(), bindings);
	return BooleanLiteral.valueOf(argValue instanceof Resource);
}
 
Example 12
Source File: GreaterThan.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected Value evaluate(ValueFactory valueFactory, Value arg1, Value arg2) throws ValueExprEvaluationException {
	return BooleanLiteral.valueOf(QueryEvaluationUtil.compare(arg1, arg2, CompareOp.GT));
}
 
Example 13
Source File: NotEqualTo.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected Value evaluate(ValueFactory valueFactory, Value arg1, Value arg2) throws ValueExprEvaluationException {
	return BooleanLiteral.valueOf(QueryEvaluationUtil.compare(arg1, arg2, CompareOp.NE));
}
 
Example 14
Source File: And.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected Value evaluate(ValueFactory valueFactory, Value arg1, Value arg2) throws ValueExprEvaluationException {
	return BooleanLiteral.valueOf(QueryEvaluationUtil.getEffectiveBooleanValue(arg1)
			&& QueryEvaluationUtil.getEffectiveBooleanValue(arg2));
}
 
Example 15
Source File: EqualTo.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected Value evaluate(ValueFactory valueFactory, Value arg1, Value arg2) throws ValueExprEvaluationException {
	return BooleanLiteral.valueOf(QueryEvaluationUtil.compare(arg1, arg2, CompareOp.EQ));
}
 
Example 16
Source File: LessThanOrEqualTo.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected Value evaluate(ValueFactory valueFactory, Value arg1, Value arg2) throws ValueExprEvaluationException {
	return BooleanLiteral.valueOf(QueryEvaluationUtil.compare(arg1, arg2, CompareOp.LE));
}
 
Example 17
Source File: StrictEvaluationStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Determines whether the two operands match according to the <code>like</code> operator. The operator is defined as
 * a string comparison with the possible use of an asterisk (*) at the end and/or the start of the second operand to
 * indicate substring matching.
 *
 * @return <tt>true</tt> if the operands match according to the <tt>like</tt> operator, <tt>false</tt> otherwise.
 */
public Value evaluate(Like node, BindingSet bindings)
		throws QueryEvaluationException {
	Value val = evaluate(node.getArg(), bindings);
	String strVal = null;

	if (val instanceof IRI) {
		strVal = val.toString();
	} else if (val instanceof Literal) {
		strVal = ((Literal) val).getLabel();
	}

	if (strVal == null) {
		throw new ValueExprEvaluationException();
	}

	if (!node.isCaseSensitive()) {
		// Convert strVal to lower case, just like the pattern has been done
		strVal = strVal.toLowerCase();
	}

	int valIndex = 0;
	int prevPatternIndex = -1;
	int patternIndex = node.getOpPattern().indexOf('*');

	if (patternIndex == -1) {
		// No wildcards
		return BooleanLiteral.valueOf(node.getOpPattern().equals(strVal));
	}

	String snippet;

	if (patternIndex > 0) {
		// Pattern does not start with a wildcard, first part must match
		snippet = node.getOpPattern().substring(0, patternIndex);
		if (!strVal.startsWith(snippet)) {
			return BooleanLiteral.FALSE;
		}

		valIndex += snippet.length();
		prevPatternIndex = patternIndex;
		patternIndex = node.getOpPattern().indexOf('*', patternIndex + 1);
	}

	while (patternIndex != -1) {
		// Get snippet between previous wildcard and this wildcard
		snippet = node.getOpPattern().substring(prevPatternIndex + 1, patternIndex);

		// Search for the snippet in the value
		valIndex = strVal.indexOf(snippet, valIndex);
		if (valIndex == -1) {
			return BooleanLiteral.FALSE;
		}

		valIndex += snippet.length();
		prevPatternIndex = patternIndex;
		patternIndex = node.getOpPattern().indexOf('*', patternIndex + 1);
	}

	// Part after last wildcard
	snippet = node.getOpPattern().substring(prevPatternIndex + 1);

	if (snippet.length() > 0) {
		// Pattern does not end with a wildcard.

		// Search last occurence of the snippet.
		valIndex = strVal.indexOf(snippet, valIndex);
		int i;
		while ((i = strVal.indexOf(snippet, valIndex + 1)) != -1) {
			// A later occurence was found.
			valIndex = i;
		}

		if (valIndex == -1) {
			return BooleanLiteral.FALSE;
		}

		valIndex += snippet.length();

		if (valIndex < strVal.length()) {
			// Some characters were not matched
			return BooleanLiteral.FALSE;
		}
	}

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

	Value v = evaluate(node.getExpression(), bindings);
	return BooleanLiteral.valueOf(QueryEvaluationUtil.getEffectiveBooleanValue(v));
}
 
Example 19
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 2 votes vote down vote up
/**
 * Determines whether the operand (a variable) contains a Resource.
 *
 * @return <tt>true</tt> if the operand contains a Resource, <tt>false</tt>
 * otherwise.
 */
private Value evaluate(IsResource node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value argValue = evaluate(node.getArg(), bindings);
    return BooleanLiteral.valueOf(argValue instanceof Resource);
}
 
Example 20
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 2 votes vote down vote up
/**
 * Evaluate if the left and right arguments of the {@link SameTerm} node are equal
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(SameTerm node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value leftVal = evaluate(node.getLeftArg(), bindings);
    Value rightVal = evaluate(node.getRightArg(), bindings);
    return BooleanLiteral.valueOf(leftVal != null && leftVal.equals(rightVal));
}