Search in sources :

Example 6 with ValueExprEvaluationException

use of org.openrdf.query.algebra.evaluation.ValueExprEvaluationException in project incubator-rya by apache.

the class DateTimeWithinPeriod method evaluate.

/**
 * Determines whether two datetimes occur within a specified period of time of one another. This method expects four
 * values, where the first two values are the datetimes, the third value is an integer indicating the period, and
 * the fourth value is a URI indicating the time unit of the period. The URI must be of Type DurationDescription in
 * the OWL-Time ontology (see <a href ="https://www.w3.org/TR/owl-time/">https://www.w3.org/TR/owl-time/</a>).
 * Examples of valid time unit URIs can be found in the class {@link OWLTime} and below
 * <ul>
 * <li>http://www.w3.org/2006/time#days</li>
 * <li>http://www.w3.org/2006/time#hours</li>
 * <li>http://www.w3.org/2006/time#minutes</li>
 * <li>http://www.w3.org/2006/time#seconds</li>
 * </ul>
 *
 * @param valueFactory - factory for creating values (not null)
 * @param values - array of Value arguments for this Function (not null).
 */
@Override
public Value evaluate(ValueFactory valueFactory, Value... values) throws ValueExprEvaluationException {
    checkNotNull(valueFactory);
    checkNotNull(values);
    try {
        // general validation of input
        checkArgument(values.length == 4);
        checkArgument(values[0] instanceof Literal);
        checkArgument(values[1] instanceof Literal);
        checkArgument(values[2] instanceof Literal);
        checkArgument(values[3] instanceof URI);
        Instant dateTime1 = convertToInstant((Literal) values[0]);
        Instant dateTime2 = convertToInstant((Literal) values[1]);
        long periodMillis = convertPeriodToMillis((Literal) values[2], (URI) values[3]);
        long timeBetween = Math.abs(Duration.between(dateTime1, dateTime2).toMillis());
        return valueFactory.createLiteral(timeBetween < periodMillis);
    } catch (Exception e) {
        throw new ValueExprEvaluationException(e);
    }
}
Also used : Literal(org.openrdf.model.Literal) Instant(java.time.Instant) URI(org.openrdf.model.URI) ValueExprEvaluationException(org.openrdf.query.algebra.evaluation.ValueExprEvaluationException) ValueExprEvaluationException(org.openrdf.query.algebra.evaluation.ValueExprEvaluationException)

Example 7 with ValueExprEvaluationException

use of org.openrdf.query.algebra.evaluation.ValueExprEvaluationException in project incubator-rya by apache.

the class TemporalIntervalRelationFunction 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);
    }
    final String[] strInterval = args[1].stringValue().split("/");
    if (strInterval.length != 2) {
        throw new ValueExprEvaluationException(getURI() + " requires the second argument: " + args[1] + " to be 2 dates seperated by a \'/\'");
    }
    try {
        final ZonedDateTime date1 = ZonedDateTime.parse(args[0].stringValue());
        final ZonedDateTime[] interval = new ZonedDateTime[] { ZonedDateTime.parse(strInterval[0]), ZonedDateTime.parse(strInterval[1]) };
        final boolean result = relation(date1, interval);
        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");
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) ZonedDateTime(java.time.ZonedDateTime) ValueExprEvaluationException(org.openrdf.query.algebra.evaluation.ValueExprEvaluationException)

Aggregations

ValueExprEvaluationException (org.openrdf.query.algebra.evaluation.ValueExprEvaluationException)7 Literal (org.openrdf.model.Literal)4 Value (org.openrdf.model.Value)4 MapBindingSet (org.openrdf.query.impl.MapBindingSet)4 BigDecimal (java.math.BigDecimal)2 ZonedDateTime (java.time.ZonedDateTime)2 DateTimeParseException (java.time.format.DateTimeParseException)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 Statement (org.openrdf.model.Statement)2 URI (org.openrdf.model.URI)2 ValueFactory (org.openrdf.model.ValueFactory)2 IntegerLiteralImpl (org.openrdf.model.impl.IntegerLiteralImpl)2 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)2 BindingSet (org.openrdf.query.BindingSet)2 Function (org.openrdf.query.algebra.evaluation.function.Function)2 BigInteger (java.math.BigInteger)1 Instant (java.time.Instant)1 DecimalLiteralImpl (org.openrdf.model.impl.DecimalLiteralImpl)1