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

The following examples show how to use org.eclipse.rdf4j.query.algebra.evaluation.util.QueryEvaluationUtil#isSimpleLiteral() . 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: 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 2
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 3
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 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: 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 6
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 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: 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 13
Source File: StringCast.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 != 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();

		// we override because unlike most other cast functions, xsd:string should not accept a language-tagged
		// string literal.
		if (QueryEvaluationUtil.isSimpleLiteral(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 14
Source File: StringCast.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected Literal convert(ValueFactory valueFactory, Value value) throws ValueExprEvaluationException {
	if (value instanceof IRI) {
		return valueFactory.createLiteral(value.toString(), XMLSchema.STRING);
	} else if (value instanceof Literal) {
		Literal literal = (Literal) value;
		IRI datatype = literal.getDatatype();

		if (QueryEvaluationUtil.isSimpleLiteral(literal)) {
			return valueFactory.createLiteral(literal.getLabel(), XMLSchema.STRING);
		} else if (!Literals.isLanguageLiteral(literal)) {
			if (XMLDatatypeUtil.isNumericDatatype(datatype) || datatype.equals(XMLSchema.BOOLEAN)
					|| datatype.equals(XMLSchema.DATETIME) || datatype.equals(XMLSchema.DATETIMESTAMP)) {
				// FIXME Slightly simplified wrt the spec, we just always use the
				// canonical value of the
				// source literal as the target lexical value. This is not 100%
				// compliant with handling of
				// some date-related datatypes.
				//
				// See
				// http://www.w3.org/TR/xpath-functions/#casting-from-primitive-to-primitive
				if (XMLDatatypeUtil.isValidValue(literal.getLabel(), datatype)) {
					String normalizedValue = XMLDatatypeUtil.normalize(literal.getLabel(), datatype);
					return valueFactory.createLiteral(normalizedValue, XMLSchema.STRING);
				} else {
					return valueFactory.createLiteral(literal.getLabel(), XMLSchema.STRING);
				}
			} else {
				// for unknown datatypes, just use the lexical value.
				return valueFactory.createLiteral(literal.getLabel(), XMLSchema.STRING);
			}
		}
	}

	throw typeError(value, null);
}
 
Example 15
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 16
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 17
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 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.
 */
private Value evaluate(Regex node, BindingSet bindings) throws ValueExprEvaluationException, 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;
                default:
                    throw new ValueExprEvaluationException(flags);
            }
        }
        Pattern pattern = Pattern.compile(ptn, f);
        boolean result = pattern.matcher(text).find();
        return BooleanLiteral.valueOf(result);
    }
    throw new ValueExprEvaluationException();
}