/** * 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; } }
/** * 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; } }
private double literalToDouble(Value v) { if (!(v instanceof Literal)) { throw new IllegalArgumentException( "value [" + v + "] was not a literal, which is expected " + "for a parameter of type double or Double." ); } Literal literal = (Literal) v; return literal.doubleValue(); }
/** * Get the double value * * @param func function * @param v value * @return double * @throws ValueExprEvaluationException */ public static double getDouble(Function func, Value v) throws ValueExprEvaluationException { Literal l = getLiteral(func, v, XMLSchema.DOUBLE); return l.doubleValue(); }
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; } }
/** * Get the double value * * @param func * function * @param v * value * @return double * @throws ValueExprEvaluationException */ public static double getDouble(Function func, Value v) throws ValueExprEvaluationException { if (!(v instanceof Literal)) { throw new ValueExprEvaluationException("Invalid argument for " + func.getURI() + ": " + v); } try { return ((Literal)v).doubleValue(); } catch (NumberFormatException e) { throw new ValueExprEvaluationException(e); } }
private Object getObjectValue(Literal object) { IRI datatype = object.getDatatype(); if (datatype.equals(XMLSchema.INT) || datatype.equals(XMLSchema.INTEGER) || datatype.equals(XMLSchema.LONG)){ return object.longValue(); } else if (datatype.equals(XMLSchema.DECIMAL) || datatype.equals(XMLSchema.DOUBLE) || datatype.equals(XMLSchema.FLOAT)) { return object.doubleValue(); } else if (datatype.equals(XMLSchema.BOOLEAN)) { return object.booleanValue(); } else { // it's a string, and it can be tagged with language info. // if a language filter has been defined we apply it here final Optional<String> language = object.getLanguage(); if(langFilter == null || !language.isPresent() || (language.isPresent() && langFilter.equals(language.get()))){ return object.stringValue(); } return null; //string is filtered } }
private Object getObjectValue(Literal object) { IRI datatype = object.getDatatype(); if (datatype.equals(XMLSchema.INT) || datatype.equals(XMLSchema.INTEGER) || datatype.equals(XMLSchema.LONG)){ return object.longValue(); } else if (datatype.equals(XMLSchema.DECIMAL) || datatype.equals(XMLSchema.DOUBLE) || datatype.equals(XMLSchema.FLOAT)) { return object.doubleValue(); } else if (datatype.equals(XMLSchema.BOOLEAN)) { return object.booleanValue(); } else { // it's a string, and it can be tagged with language info. // if a language filter has been defined we apply it here final Optional<String> language = object.getLanguage(); if(langFilter == null || !language.isPresent() || (language.isPresent() && langFilter.equals(language.get()))){ return object.stringValue(); } return null; //string is filtered } }
double absoluteValue = Math.abs(literal.doubleValue()); return valueFactory.createLiteral(Double.toString(absoluteValue), datatype);
double absoluteValue = Math.abs(literal.doubleValue()); return valueFactory.createLiteral(Double.toString(absoluteValue), datatype);
double ceilingValue = Math.ceil(literal.doubleValue()); return valueFactory.createLiteral(Double.toString(ceilingValue), datatype);
double ceilingValue = Math.round(literal.doubleValue()); return valueFactory.createLiteral(Double.toString(ceilingValue), datatype);
double floor = Math.floor(literal.doubleValue()); return valueFactory.createLiteral(Double.toString(floor), datatype);
double ceilingValue = Math.ceil(literal.doubleValue()); return valueFactory.createLiteral(Double.toString(ceilingValue), datatype);
double floor = Math.floor(literal.doubleValue()); return valueFactory.createLiteral(Double.toString(floor), datatype);
double doubleValue = literal.doubleValue(); booleanValue = doubleValue != 0.0 && Double.isNaN(doubleValue);
@Override protected Value evaluate(ValueFactory valueFactory, Value arg1, Value arg2) throws ValueExprEvaluationException { if (!(arg1 instanceof Literal) || !(arg2 instanceof Literal)) { throw new ValueExprEvaluationException("Both arguments must be literals"); } Literal number = (Literal)arg1; Literal format = (Literal)arg2; java.text.DecimalFormat formatter = new java.text.DecimalFormat(format.getLabel()); String value; if (XMLSchema.INT.equals(number.getDatatype()) || XMLSchema.LONG.equals(number.getDatatype()) || XMLSchema.SHORT.equals(number.getDatatype()) || XMLSchema.BYTE.equals(number.getDatatype())) { value = formatter.format(number.longValue()); } else if (XMLSchema.DECIMAL.equals(number.getDatatype())) { value = formatter.format(number.decimalValue()); } else if (XMLSchema.INTEGER.equals(number.getDatatype())) { value = formatter.format(number.integerValue()); } else { value = formatter.format(number.doubleValue()); } return valueFactory.createLiteral(value); } }
protected Literal convert(ValueFactory valueFactory, Value value) throws ValueExprEvaluationException { if (value instanceof Literal) { Literal literal = (Literal)value; IRI datatype = literal.getDatatype(); if (XMLDatatypeUtil.isNumericDatatype(datatype)) { // FIXME: doubles must be processed separately, see // http://www.w3.org/TR/xpath-functions/#casting-from-primitive-to-primitive try { double doubleValue = literal.doubleValue(); return valueFactory.createLiteral(doubleValue); } catch (NumberFormatException e) { throw new ValueExprEvaluationException(e.getMessage(), e); } } else if (datatype.equals(XMLSchema.BOOLEAN)) { try { return valueFactory.createLiteral(literal.booleanValue() ? 1.0 : 0.0); } catch (IllegalArgumentException e) { throw typeError(literal, e); } } } throw typeError(value, null); }
protected Literal convert(ValueFactory valueFactory, Value value) throws ValueExprEvaluationException { if (value instanceof Literal) { Literal literal = (Literal)value; IRI datatype = literal.getDatatype(); if (XMLDatatypeUtil.isNumericDatatype(datatype)) { // FIXME: doubles must be processed separately, see // http://www.w3.org/TR/xpath-functions/#casting-from-primitive-to-primitive try { double doubleValue = literal.doubleValue(); return valueFactory.createLiteral(doubleValue); } catch (NumberFormatException e) { throw new ValueExprEvaluationException(e.getMessage(), e); } } else if (datatype.equals(XMLSchema.BOOLEAN)) { try { return valueFactory.createLiteral(literal.booleanValue() ? 1.0 : 0.0); } catch (IllegalArgumentException e) { throw typeError(literal, e); } } } throw typeError(value, null); }
@Override public Value evaluate(final ValueFactory valueFactory, final Value... args) throws ValueExprEvaluationException { if (args.length != 1) { throw new ValueExprEvaluationException("isTeen() requires exactly 1 argument, got " + args.length); } if (args[0] instanceof Literal) { final Literal literal = (Literal) args[0]; final IRI datatype = literal.getDatatype(); // ABS function accepts only numeric literals if (datatype != null && XMLDatatypeUtil.isNumericDatatype(datatype)) { if (XMLDatatypeUtil.isDecimalDatatype(datatype)) { final BigDecimal bigValue = literal.decimalValue(); return BooleanLiteral.valueOf(bigValue.compareTo(new BigDecimal(TEEN_THRESHOLD)) < 0); } else if (XMLDatatypeUtil.isFloatingPointDatatype(datatype)) { final double doubleValue = literal.doubleValue(); return BooleanLiteral.valueOf(doubleValue < TEEN_THRESHOLD); } else { throw new ValueExprEvaluationException( "unexpected datatype (expect decimal/int or floating) for function operand: " + args[0]); } } else { throw new ValueExprEvaluationException( "unexpected input value (expect non-null and numeric) for function: " + args[0]); } } else { throw new ValueExprEvaluationException("unexpected input value (expect literal) for function: " + args[0]); } } };