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

The following examples show how to use org.eclipse.rdf4j.query.algebra.evaluation.util.QueryEvaluationUtil#compatibleArguments() . 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: 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 2
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 3
Source File: StrBefore.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 2) {
		throw new ValueExprEvaluationException("Incorrect number of arguments for STRBEFORE: " + args.length);
	}

	Value leftArg = args[0];
	Value rightArg = args[1];

	if (leftArg instanceof Literal && rightArg instanceof Literal) {
		Literal leftLit = (Literal) leftArg;
		Literal rightLit = (Literal) rightArg;

		if (QueryEvaluationUtil.compatibleArguments(leftLit, rightLit)) {
			Optional<String> leftLanguage = leftLit.getLanguage();
			IRI leftDt = leftLit.getDatatype();

			String lexicalValue = leftLit.getLabel();
			String substring = rightLit.getLabel();

			int index = lexicalValue.indexOf(substring);

			String substringBefore = "";
			if (index > -1) {
				substringBefore = lexicalValue.substring(0, index);
			} else {
				// no match, return empty string with no language or datatype
				leftLanguage = Optional.empty();
				leftDt = null;
			}

			if (leftLanguage.isPresent()) {
				return valueFactory.createLiteral(substringBefore, leftLanguage.get());
			} else if (leftDt != null) {
				return valueFactory.createLiteral(substringBefore, leftDt);
			} else {
				return valueFactory.createLiteral(substringBefore);
			}
		} else {
			throw new ValueExprEvaluationException(
					"incompatible operands for STRBEFORE: " + leftArg + ", " + rightArg);
		}
	} else {
		throw new ValueExprEvaluationException("incompatible operands for STRBEFORE: " + leftArg + ", " + rightArg);
	}
}
 
Example 4
Source File: StrAfter.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length != 2) {
		throw new ValueExprEvaluationException("Incorrect number of arguments for STRAFTER: " + args.length);
	}

	Value leftArg = args[0];
	Value rightArg = args[1];

	if (leftArg instanceof Literal && rightArg instanceof Literal) {
		Literal leftLit = (Literal) leftArg;
		Literal rightLit = (Literal) rightArg;

		if (QueryEvaluationUtil.compatibleArguments(leftLit, rightLit)) {
			String lexicalValue = leftLit.getLabel();
			String substring = rightLit.getLabel();

			Optional<String> leftLanguage = leftLit.getLanguage();
			IRI leftDt = leftLit.getDatatype();

			int index = lexicalValue.indexOf(substring);

			String substringAfter = "";
			if (index > -1) {
				index += substring.length() - 1;
				substringAfter = lexicalValue.substring(index + 1, lexicalValue.length());
			} else {
				// no match, return empty string with no language or datatype
				leftLanguage = Optional.empty();
				leftDt = null;
			}

			if (leftLanguage.isPresent()) {
				return valueFactory.createLiteral(substringAfter, leftLanguage.get());
			} else if (leftDt != null) {
				return valueFactory.createLiteral(substringAfter, leftDt);
			} else {
				return valueFactory.createLiteral(substringAfter);
			}
		} else {
			throw new ValueExprEvaluationException(
					"incompatible operands for STRAFTER: " + leftArg + ", " + rightArg);
		}
	} else {
		throw new ValueExprEvaluationException("incompatible operands for STRAFTER: " + leftArg + ", " + rightArg);
	}
}