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

The following examples show how to use org.eclipse.rdf4j.model.Literal#doubleValue() . 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: DistanceQuerySpec.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public DistanceQuerySpec(FunctionCall distanceFunction, ValueExpr distanceExpr, String distVar, Filter filter) {
	this.distanceFunction = distanceFunction;
	this.distanceExpr = distanceExpr;
	this.distanceVar = distVar;
	this.filter = filter;
	if (distanceFunction != null) {
		List<ValueExpr> args = distanceFunction.getArgs();
		this.from = getLiteral(args.get(0));
		this.geoVar = getVarName(args.get(1));
		this.units = getURI(args.get(2));
	} else {
		this.from = null;
		this.geoVar = null;
		this.units = null;
	}
	if (distanceExpr != null) {
		Literal dist = getLiteral(distanceExpr);
		this.distance = (dist != null) ? dist.doubleValue() : Double.NaN;
	} else {
		this.distance = Double.NaN;
	}
}
 
Example 2
Source File: RandTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testEvaluate() {
	try {
		Literal random = rand.evaluate(f);

		assertNotNull(random);
		assertEquals(XMLSchema.DOUBLE, random.getDatatype());

		double randomValue = random.doubleValue();

		assertTrue(randomValue >= 0.0d);
		assertTrue(randomValue < 1.0d);
	} catch (ValueExprEvaluationException e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example 3
Source File: SpinxFunction.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	Bindings bindings = scriptEngine.createBindings();
	for (int i = 0; i < args.length; i++) {
		Argument argument = arguments.get(i);
		Value arg = args[i];
		Object jsArg;
		if (arg instanceof Literal) {
			Literal argLiteral = (Literal) arg;
			if (XMLSchema.INTEGER.equals(argLiteral.getDatatype())) {
				jsArg = argLiteral.intValue();
			} else if (XMLSchema.DECIMAL.equals(argLiteral.getDatatype())) {
				jsArg = argLiteral.doubleValue();
			} else {
				jsArg = argLiteral.getLabel();
			}
		} else {
			jsArg = arg.stringValue();
		}
		bindings.put(argument.getPredicate().getLocalName(), jsArg);
	}

	Object result;
	try {
		if (compiledScript == null && scriptEngine instanceof Compilable) {
			compiledScript = ((Compilable) scriptEngine).compile(script);
		}
		if (compiledScript != null) {
			result = compiledScript.eval(bindings);
		} else {
			result = scriptEngine.eval(script, bindings);
		}
	} catch (ScriptException e) {
		throw new ValueExprEvaluationException(e);
	}

	ValueFactory vf = ValueFactoryImpl.getInstance();
	return (returnType != null) ? vf.createLiteral(result.toString(), returnType) : vf.createURI(result.toString());
}
 
Example 4
Source File: BooleanCast.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 Literal) {
		Literal literal = (Literal) value;
		IRI datatype = literal.getDatatype();
		Boolean booleanValue = null;
		try {
			if (datatype.equals(XMLSchema.FLOAT)) {
				float floatValue = literal.floatValue();
				booleanValue = floatValue != 0.0f && Float.isNaN(floatValue);
			} else if (datatype.equals(XMLSchema.DOUBLE)) {
				double doubleValue = literal.doubleValue();
				booleanValue = doubleValue != 0.0 && Double.isNaN(doubleValue);
			} else if (datatype.equals(XMLSchema.DECIMAL)) {
				BigDecimal decimalValue = literal.decimalValue();
				booleanValue = !decimalValue.equals(BigDecimal.ZERO);
			} else if (datatype.equals(XMLSchema.INTEGER)) {
				BigInteger integerValue = literal.integerValue();
				booleanValue = !integerValue.equals(BigInteger.ZERO);
			} else if (XMLDatatypeUtil.isIntegerDatatype(datatype)) {
				booleanValue = literal.longValue() != 0L;
			}
		} catch (NumberFormatException e) {
			throw typeError(literal, e);
		}

		if (booleanValue != null) {
			return valueFactory.createLiteral(booleanValue);
		}
	}

	throw typeError(value, null);
}
 
Example 5
Source File: RoundTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testEvaluateDouble() {
	try {
		double dVal = 1.6;
		Literal rounded = round.evaluate(f, f.createLiteral(dVal));

		double roundValue = rounded.doubleValue();

		assertEquals((double) 2.0, roundValue, 0.001d);
	} catch (ValueExprEvaluationException e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example 6
Source File: Literals.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Gets the double value of the supplied literal. The fallback value is returned in case
 * {@link Literal#doubleValue()} throws a {@link NumberFormatException}.
 *
 * @param l        The literal to get the double value for.
 * @param fallback The value to fall back to in case no double value could gotten from the literal.
 * @return Either the literal's double value, or the fallback value.
 */
public static double getDoubleValue(Literal l, double fallback) {
	try {
		return l.doubleValue();
	} catch (NumberFormatException e) {
		return fallback;
	}
}