Java Code Examples for org.eclipse.rdf4j.model.Literal#getLanguage()

The following examples show how to use org.eclipse.rdf4j.model.Literal#getLanguage() . 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 Datatype} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return a {@link Literal} representing the evaluation of the argument of the {@link Datatype}.
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(Datatype node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value v = evaluate(node.getArg(), bindings);
    if (v instanceof Literal) {
        Literal literal = (Literal) v;
        if (literal.getDatatype() != null) {
            // literal with datatype
            return literal.getDatatype();
        } else if (literal.getLanguage() != null) {
            return RDF.LANGSTRING;
        } else {
            // simple literal
            return XMLSchema.STRING;
        }
    }
    throw new ValueExprEvaluationException();
}
 
Example 2
Source File: FilterUtils.java    From CostFed with GNU Affero General Public License v3.0 5 votes vote down vote up
protected static StringBuilder appendLiteral(StringBuilder sb, Literal lit) {
	sb.append('"');
	sb.append(lit.getLabel().replace("\"", "\\\""));
	sb.append('"');

	if (lit.getLanguage() != null && lit.getLanguage().isPresent()) {
		sb.append('@');
		sb.append(lit.getLanguage());
	} else if (!XMLSchema.STRING.equals(lit.getDatatype()) && lit.getDatatype() != null) {
	    sb.append("^^<");
		sb.append(lit.getDatatype().stringValue());
		sb.append('>');
	}
	return sb;
}
 
Example 3
Source File: Substring.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Literal convert(String lexicalValue, Literal literal, ValueFactory valueFactory) {
	Optional<String> language = literal.getLanguage();
	if (language.isPresent()) {
		return valueFactory.createLiteral(lexicalValue, language.get());
	} else if (XMLSchema.STRING.equals(literal.getDatatype())) {
		return valueFactory.createLiteral(lexicalValue, XMLSchema.STRING);
	} else {
		return valueFactory.createLiteral(lexicalValue);
	}
}
 
Example 4
Source File: LowerCase.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("LCASE requires exactly 1 argument, got " + args.length);
	}

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

		// LowerCase function accepts only string literals.
		if (QueryEvaluationUtil.isStringLiteral(literal)) {
			String lexicalValue = literal.getLabel().toLowerCase();
			Optional<String> language = literal.getLanguage();

			if (language.isPresent()) {
				return valueFactory.createLiteral(lexicalValue, language.get());
			} else if (XMLSchema.STRING.equals(literal.getDatatype())) {
				return valueFactory.createLiteral(lexicalValue, XMLSchema.STRING);
			} else {
				return valueFactory.createLiteral(lexicalValue);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
		}
	} else {
		throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
	}

}
 
Example 5
Source File: UpperCase.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("UCASE requires exactly 1 argument, got " + args.length);
	}

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

		// UpperCase function accepts only string literal
		if (QueryEvaluationUtil.isStringLiteral(literal)) {
			String lexicalValue = literal.getLabel().toUpperCase();
			Optional<String> language = literal.getLanguage();

			if (language.isPresent()) {
				return valueFactory.createLiteral(lexicalValue, language.get());
			} else if (XMLSchema.STRING.equals(literal.getDatatype())) {
				return valueFactory.createLiteral(lexicalValue, XMLSchema.STRING);
			} else {
				return valueFactory.createLiteral(lexicalValue);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
		}
	} else {
		throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
	}

}
 
Example 6
Source File: TupleExprs.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String getConstVarName(Value value) {
	if (value == null) {
		throw new IllegalArgumentException("value can not be null");
	}

	// We use toHexString to get a more compact stringrep.
	String uniqueStringForValue = Integer.toHexString(value.stringValue().hashCode());

	if (value instanceof Literal) {
		uniqueStringForValue += "_lit";

		// we need to append datatype and/or language tag to ensure a unique
		// var name (see SES-1927)
		Literal lit = (Literal) value;
		if (lit.getDatatype() != null) {
			uniqueStringForValue += "_" + Integer.toHexString(lit.getDatatype().hashCode());
		}
		if (lit.getLanguage() != null) {
			uniqueStringForValue += "_" + Integer.toHexString(lit.getLanguage().hashCode());
		}
	} else if (value instanceof BNode) {
		uniqueStringForValue += "_node";
	} else {
		uniqueStringForValue += "_uri";
	}

	return "_const_" + uniqueStringForValue;
}
 
Example 7
Source File: HalyardValueExprEvaluation.java    From Halyard with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluate a {@link Lang} node
 * @param node the node to evaluate
 * @param bindings the set of named value bindings
 * @return a {@link Literal} of the language tag of the {@code Literal} returned by evaluating the argument of the {@code node} or a
 * {code Literal} representing an empty {@code String} if there is no tag
 * @throws ValueExprEvaluationException
 * @throws QueryEvaluationException
 */
private Value evaluate(Lang node, BindingSet bindings) throws ValueExprEvaluationException, QueryEvaluationException {
    Value argValue = evaluate(node.getArg(), bindings);
    if (argValue instanceof Literal) {
        Literal literal = (Literal) argValue;
        Optional<String> langTag = literal.getLanguage();
        if (langTag.isPresent()) {
            return valueFactory.createLiteral(langTag.get());
        }
        return valueFactory.createLiteral("");
    }
    throw new ValueExprEvaluationException();
}
 
Example 8
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 9
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);
	}
}
 
Example 10
Source File: ValueComparator.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private int compareLiterals(Literal leftLit, Literal rightLit) {
	// Additional constraint for ORDER BY: "A plain literal is lower
	// than an RDF literal with type xsd:string of the same lexical
	// form."

	if (!(QueryEvaluationUtil.isPlainLiteral(leftLit) || QueryEvaluationUtil.isPlainLiteral(rightLit))) {
		try {
			boolean isSmaller = QueryEvaluationUtil.compareLiterals(leftLit, rightLit, CompareOp.LT, strict);
			if (isSmaller) {
				return -1;
			} else {
				boolean isEquivalent = QueryEvaluationUtil.compareLiterals(leftLit, rightLit, CompareOp.EQ, strict);
				if (isEquivalent) {
					return 0;
				}
				return 1;
			}
		} catch (ValueExprEvaluationException e) {
			// literals cannot be compared using the '<' operator, continue
			// below
		}
	}

	int result = 0;

	// FIXME: Confirm these rules work with RDF-1.1
	// Sort by datatype first, plain literals come before datatyped literals
	IRI leftDatatype = leftLit.getDatatype();
	IRI rightDatatype = rightLit.getDatatype();

	if (leftDatatype != null) {
		if (rightDatatype != null) {
			// Both literals have datatypes
			result = compareDatatypes(leftDatatype, rightDatatype);
		} else {
			result = 1;
		}
	} else if (rightDatatype != null) {
		result = -1;
	}

	if (result == 0) {
		// datatypes are equal or both literals are untyped; sort by language
		// tags, simple literals come before literals with language tags
		Optional<String> leftLanguage = leftLit.getLanguage();
		Optional<String> rightLanguage = rightLit.getLanguage();

		if (leftLanguage.isPresent()) {
			if (rightLanguage.isPresent()) {
				result = leftLanguage.get().compareTo(rightLanguage.get());
			} else {
				result = 1;
			}
		} else if (rightLanguage.isPresent()) {
			result = -1;
		}
	}

	if (result == 0) {
		// Literals are equal as fas as their datatypes and language tags are
		// concerned, compare their labels
		result = leftLit.getLabel().compareTo(rightLit.getLabel());
	}

	return result;
}
 
Example 11
Source File: LexicalValueComparator.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private int compareLiterals(Literal leftLit, Literal rightLit) {
	// Additional constraint for ORDER BY: "A plain literal is lower
	// than an RDF literal with type xsd:string of the same lexical
	// form."
	int result = 0;
	// FIXME: Confirm these rules work with RDF-1.1
	// Sort by datatype first, plain literals come before datatyped literals
	IRI leftDatatype = leftLit.getDatatype();
	IRI rightDatatype = rightLit.getDatatype();

	if (leftDatatype != null) {
		if (rightDatatype != null) {
			// Both literals have datatypes
			result = compareDatatypes(leftDatatype, rightDatatype);
		} else {
			result = 1;
		}
	} else if (rightDatatype != null) {
		result = -1;
	}

	if (result == 0) {
		// datatypes are equal or both literals are untyped; sort by language
		// tags, simple literals come before literals with language tags
		Optional<String> leftLanguage = leftLit.getLanguage();
		Optional<String> rightLanguage = rightLit.getLanguage();

		if (leftLanguage.isPresent()) {
			if (rightLanguage.isPresent()) {
				result = leftLanguage.get().compareTo(rightLanguage.get());
			} else {
				result = 1;
			}
		} else if (rightLanguage.isPresent()) {
			result = -1;
		}
	}

	if (result == 0) {
		// Literals are equal as fas as their datatypes and language tags are
		// concerned, compare their labels
		result = leftLit.getLabel().compareTo(rightLit.getLabel());
	}

	return result;
}