use of org.openrdf.query.algebra.evaluation.ValueExprEvaluationException in project incubator-rya by apache.
the class SumFunction method update.
@Override
public void update(final AggregationElement aggregation, final AggregationState state, final VisibilityBindingSet childBindingSet) {
checkArgument(aggregation.getAggregationType() == AggregationType.SUM, "The SumFunction only accepts SUM AggregationElements.");
requireNonNull(state);
requireNonNull(childBindingSet);
// Only add values to the sum if the child contains the binding that we are summing.
final String aggregatedName = aggregation.getAggregatedBindingName();
if (childBindingSet.hasBinding(aggregatedName)) {
final MapBindingSet result = state.getBindingSet();
final String resultName = aggregation.getResultBindingName();
final boolean newBinding = !result.hasBinding(resultName);
// Get the starting number for the sum.
Literal sum;
if (newBinding) {
sum = new IntegerLiteralImpl(BigInteger.ZERO);
} else {
sum = (Literal) state.getBindingSet().getValue(resultName);
}
// Add the child binding set's value if it is a numeric literal.
final Value childValue = childBindingSet.getValue(aggregatedName);
if (childValue instanceof Literal) {
final Literal childLiteral = (Literal) childValue;
if (childLiteral.getDatatype() != null && XMLDatatypeUtil.isNumericDatatype(childLiteral.getDatatype())) {
try {
sum = MathUtil.compute(sum, childLiteral, MathOp.PLUS);
} catch (final ValueExprEvaluationException e) {
log.error("A problem was encountered while updating a Sum Aggregation. This binding set will be ignored: " + childBindingSet);
return;
}
}
}
// Update the state to include the new sum.
result.addBinding(resultName, sum);
}
}
use of org.openrdf.query.algebra.evaluation.ValueExprEvaluationException in project incubator-rya by apache.
the class AverageFunction method update.
@Override
public void update(final AggregationElement aggregation, final AggregationState state, final VisibilityBindingSet childBindingSet) {
checkArgument(aggregation.getAggregationType() == AggregationType.AVERAGE, "The AverageFunction only accepts AVERAGE AggregationElements.");
requireNonNull(state);
requireNonNull(childBindingSet);
// Only update the average if the child contains the binding that we are averaging.
final String aggregatedName = aggregation.getAggregatedBindingName();
if (childBindingSet.hasBinding(aggregatedName)) {
final MapBindingSet result = state.getBindingSet();
final String resultName = aggregation.getResultBindingName();
final boolean newBinding = !result.hasBinding(resultName);
// Get the state of the average.
final Map<String, AverageState> averageStates = state.getAverageStates();
AverageState averageState = newBinding ? new AverageState() : averageStates.get(resultName);
// Update the state of the average.
final Value childValue = childBindingSet.getValue(aggregatedName);
if (childValue instanceof Literal) {
final Literal childLiteral = (Literal) childValue;
if (childLiteral.getDatatype() != null && XMLDatatypeUtil.isNumericDatatype(childLiteral.getDatatype())) {
try {
// Update the sum.
final Literal oldSum = new DecimalLiteralImpl(averageState.getSum());
final BigDecimal sum = MathUtil.compute(oldSum, childLiteral, MathOp.PLUS).decimalValue();
// Update the count.
final BigInteger count = averageState.getCount().add(BigInteger.ONE);
// Update the BindingSet to include the new average.
final Literal sumLiteral = new DecimalLiteralImpl(sum);
final Literal countLiteral = new IntegerLiteralImpl(count);
final Literal average = MathUtil.compute(sumLiteral, countLiteral, MathOp.DIVIDE);
result.addBinding(resultName, average);
// Update the average state that is stored.
averageState = new AverageState(sum, count);
averageStates.put(resultName, averageState);
} catch (final ValueExprEvaluationException e) {
log.error("A problem was encountered while updating an Average Aggregation. This binding set will be ignored: " + childBindingSet);
return;
}
}
}
}
}
use of org.openrdf.query.algebra.evaluation.ValueExprEvaluationException in project incubator-rya by apache.
the class GeoFunctionsIT method withGeoFilters.
@Test
public void withGeoFilters() throws Exception {
final String sparql = "PREFIX geo: <http://www.opengis.net/ont/geosparql#> " + "PREFIX ryageo: <tag:rya.apache.org,2017:function/geo#> " + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/> " + "SELECT ?feature ?point ?wkt {" + " ?feature a geo:Feature . " + " ?feature geo:hasGeometry ?point . " + " ?point a geo:Point . " + " ?point geo:asWKT ?wkt . " + " FILTER(ryageo:ehContains(?wkt, \"POLYGON((-77 39, -76 39, -76 38, -77 38, -77 39))\"^^geo:wktLiteral)) " + "}";
final ValueFactory vf = new ValueFactoryImpl();
final Set<Statement> statements = Sets.newHashSet(vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#feature"), vf.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), vf.createURI("http://www.opengis.net/ont/geosparql#Feature")), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#feature"), vf.createURI("http://www.opengis.net/ont/geosparql#hasGeometry"), vf.createURI("tag:rya.apache.org,2017:ex#test_point")), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#test_point"), vf.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), vf.createURI("http://www.opengis.net/ont/geosparql#Point")), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#test_point"), vf.createURI("http://www.opengis.net/ont/geosparql#asWKT"), vf.createLiteral("Point(-77.03524 38.889468)", vf.createURI("http://www.opengis.net/ont/geosparql#wktLiteral"))));
// Create a Geo function.
final Function geoFunction = new Function() {
@Override
public String getURI() {
return "tag:rya.apache.org,2017:function/geo#ehContains";
}
@Override
public Value evaluate(final ValueFactory valueFactory, final Value... args) throws ValueExprEvaluationException {
if (args.length != 2) {
throw new ValueExprEvaluationException(getURI() + " requires exactly 3 arguments, got " + args.length);
}
return valueFactory.createLiteral(true);
}
};
// Add our new function to the registry
FunctionRegistry.getInstance().add(geoFunction);
// The expected results of the SPARQL query once the PCJ has been computed.
final Set<BindingSet> expectedResults = new HashSet<>();
final MapBindingSet bs = new MapBindingSet();
bs.addBinding("wkt", vf.createLiteral("Point(-77.03524 38.889468)", vf.createURI("http://www.opengis.net/ont/geosparql#wktLiteral")));
bs.addBinding("feature", vf.createURI("tag:rya.apache.org,2017:ex#feature"));
bs.addBinding("point", vf.createURI("tag:rya.apache.org,2017:ex#test_point"));
expectedResults.add(bs);
runTest(sparql, statements, expectedResults);
}
use of org.openrdf.query.algebra.evaluation.ValueExprEvaluationException in project incubator-rya by apache.
the class QueryIT method withCustomFilters.
@Test
public void withCustomFilters() throws Exception {
final String sparql = "prefix ryafunc: <tag:rya.apache.org,2017:function#> " + "SELECT ?name ?age " + "{ " + "FILTER( ryafunc:isTeen(?age) ) . " + "?name <http://hasAge> ?age . " + "?name <http://playsSport> \"Soccer\" . " + "}";
// Register a custom Filter.
final Function fooFunction = new Function() {
@Override
public String getURI() {
return "tag:rya.apache.org,2017:function#isTeen";
}
static final int TEEN_THRESHOLD = 20;
@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 URI 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 BooleanLiteralImpl.valueOf(bigValue.compareTo(new BigDecimal(TEEN_THRESHOLD)) < 0);
} else if (XMLDatatypeUtil.isFloatingPointDatatype(datatype)) {
final double doubleValue = literal.doubleValue();
return BooleanLiteralImpl.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]);
}
}
};
// Add our new function to the registry
FunctionRegistry.getInstance().add(fooFunction);
// Create the Statements that will be loaded into Rya.
final ValueFactory vf = new ValueFactoryImpl();
final Collection<Statement> statements = Sets.newHashSet(vf.createStatement(vf.createURI("http://Alice"), vf.createURI("http://hasAge"), vf.createLiteral(18)), vf.createStatement(vf.createURI("http://Bob"), vf.createURI("http://hasAge"), vf.createLiteral(30)), vf.createStatement(vf.createURI("http://Charlie"), vf.createURI("http://hasAge"), vf.createLiteral(14)), vf.createStatement(vf.createURI("http://David"), vf.createURI("http://hasAge"), vf.createLiteral(16)), vf.createStatement(vf.createURI("http://Eve"), vf.createURI("http://hasAge"), vf.createLiteral(35)), vf.createStatement(vf.createURI("http://Alice"), vf.createURI("http://playsSport"), vf.createLiteral("Soccer")), vf.createStatement(vf.createURI("http://Bob"), vf.createURI("http://playsSport"), vf.createLiteral("Soccer")), vf.createStatement(vf.createURI("http://Charlie"), vf.createURI("http://playsSport"), vf.createLiteral("Basketball")), vf.createStatement(vf.createURI("http://Charlie"), vf.createURI("http://playsSport"), vf.createLiteral("Soccer")), vf.createStatement(vf.createURI("http://David"), vf.createURI("http://playsSport"), vf.createLiteral("Basketball")));
// Create the expected results of the SPARQL query once the PCJ has been computed.
final Set<BindingSet> expectedResults = new HashSet<>();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("name", vf.createURI("http://Alice"));
bs.addBinding("age", vf.createLiteral("18", XMLSchema.INTEGER));
expectedResults.add(bs);
bs = new MapBindingSet();
bs.addBinding("name", vf.createURI("http://Charlie"));
bs.addBinding("age", vf.createLiteral("14", XMLSchema.INTEGER));
expectedResults.add(bs);
// Verify the end results of the query match the expected results.
runTest(sparql, statements, expectedResults, ExportStrategy.RYA);
}
use of org.openrdf.query.algebra.evaluation.ValueExprEvaluationException in project incubator-rya by apache.
the class TemporalInstantRelationFunction method evaluate.
@Override
public Value evaluate(final ValueFactory valueFactory, final Value... args) throws ValueExprEvaluationException {
if (args.length != 2) {
throw new ValueExprEvaluationException(getURI() + " requires exactly 2 arguments, got " + args.length);
}
try {
final ZonedDateTime date1 = ZonedDateTime.parse(args[0].stringValue());
final ZonedDateTime date2 = ZonedDateTime.parse(args[1].stringValue());
final boolean result = relation(date1, date2);
return valueFactory.createLiteral(result);
} catch (final DateTimeParseException e) {
throw new ValueExprEvaluationException("Date/Times provided must be of the ISO-8601 format. Example: 2007-04-05T14:30Z");
}
}
Aggregations