From 651c154ff4a7b50d722057f9a8ba77d33aca5ab6 Mon Sep 17 00:00:00 2001 From: Miroslav Blasko Date: Wed, 14 Aug 2024 18:17:09 +0200 Subject: [PATCH] [#184] Refactor out spin from AddDays function --- .../cz/cvut/spipes/function/time/AddDays.java | 28 +++++++++---------- .../spipes/function/time/AddDaysTest.java | 13 ++++----- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/s-pipes-core/src/main/java/cz/cvut/spipes/function/time/AddDays.java b/s-pipes-core/src/main/java/cz/cvut/spipes/function/time/AddDays.java index aecd5b3f..6d2b8b0b 100644 --- a/s-pipes-core/src/main/java/cz/cvut/spipes/function/time/AddDays.java +++ b/s-pipes-core/src/main/java/cz/cvut/spipes/function/time/AddDays.java @@ -6,10 +6,8 @@ import org.apache.jena.datatypes.xsd.XSDDatatype; import org.apache.jena.datatypes.xsd.impl.XSDBaseStringType; import org.apache.jena.datatypes.xsd.impl.XSDDateType; -import org.apache.jena.graph.Node; import org.apache.jena.sparql.expr.NodeValue; -import org.apache.jena.sparql.function.FunctionEnv; -import org.topbraid.spin.arq.AbstractFunction2; +import org.apache.jena.sparql.function.FunctionBase2; import java.time.LocalDate; import java.time.format.DateTimeParseException; @@ -19,7 +17,7 @@ * Extend specified `date` by number of `days`. Return typed literal with same datatype. * Currently, supports only xsd:date datatype. */ -public class AddDays extends AbstractFunction2 implements ValueFunction { +public class AddDays extends FunctionBase2 implements ValueFunction { private static final String TYPE_IRI = KBSS_TIMEF.uri + "add-days"; @@ -29,17 +27,17 @@ public String getTypeURI() { return TYPE_IRI; } - @Override - protected NodeValue exec(Node date, Node days, FunctionEnv env) { + @Override + public NodeValue exec(NodeValue date, NodeValue days) { Long daysToAdd = getDays(days); RDFDatatype datatype = getDatatype(date); try { if (datatype != null && daysToAdd != null) { //TODO quite slow to parse everytime - String newDate = LocalDate.parse(date.getLiteral().getValue().toString()).plusDays(daysToAdd).toString(); - return NodeValue.makeNode(newDate, datatype); + String newDate = LocalDate.parse(date.asNode().getLiteral().getValue().toString()).plusDays(daysToAdd).toString(); + return NodeValue.makeNode(newDate, datatype); } } catch (DateTimeParseException e){ } @@ -47,19 +45,19 @@ protected NodeValue exec(Node date, Node days, FunctionEnv env) { return null; } - private Long getDays(Node days) { + private Long getDays(NodeValue days) { return Optional.of(days) - .filter(Node::isLiteral) - .filter(n -> n.getLiteralValue() instanceof Integer) - .map(n -> ((Integer) n.getLiteralValue()).longValue()) + .filter(NodeValue::isLiteral) + .filter(n -> n.asNode().getLiteralValue() instanceof Integer) + .map(n -> ((Integer) n.asNode().getLiteralValue()).longValue()) .orElse(null); } - RDFDatatype getDatatype(Node date) { + RDFDatatype getDatatype(NodeValue date) { return Optional.of(date) - .filter(Node::isLiteral) - .map(n -> getNewDatatype(n.getLiteralDatatype())) + .filter(NodeValue::isLiteral) + .map(n -> getNewDatatype(n.getNode().getLiteralDatatype())) .orElse(null); } diff --git a/s-pipes-core/src/test/java/cz/cvut/spipes/function/time/AddDaysTest.java b/s-pipes-core/src/test/java/cz/cvut/spipes/function/time/AddDaysTest.java index c81b2e66..bdc2d37c 100644 --- a/s-pipes-core/src/test/java/cz/cvut/spipes/function/time/AddDaysTest.java +++ b/s-pipes-core/src/test/java/cz/cvut/spipes/function/time/AddDaysTest.java @@ -2,7 +2,6 @@ import org.apache.jena.datatypes.RDFDatatype; import org.apache.jena.datatypes.xsd.XSDDatatype; -import org.apache.jena.graph.Node; import org.apache.jena.sparql.expr.NodeValue; import org.junit.jupiter.api.Test; @@ -16,10 +15,10 @@ public class AddDaysTest { public void execReturnsTimeFromPast() { AddDays addDays = new AddDays(); - Node date = getDateNode("2022-01-01").asNode(); - Node days = NodeValue.makeNodeDecimal("-1").asNode(); + NodeValue date = getDateNode("2022-01-01"); + NodeValue days = NodeValue.makeNodeDecimal("-1"); - NodeValue returnedDate = addDays.exec(date, days, null); + NodeValue returnedDate = addDays.exec(date, days); NodeValue expectedDate = getDateNode("2021-12-31"); assertEquals(expectedDate, returnedDate); @@ -27,14 +26,14 @@ public void execReturnsTimeFromPast() { @Test public void execReturnsDatatypeOfInputLiteral() { - Node days = NodeValue.makeNodeDecimal("1").asNode(); + NodeValue days = NodeValue.makeNodeDecimal("1"); Stream.of(XSDDatatype.XSDdate, XSDDatatype.XSDstring).forEach( dt -> { - Node date = getNode("2021-12-31", dt).asNode(); + NodeValue date = getNode("2021-12-31", dt); AddDays addDays = new AddDays(); - NodeValue returnedDate = addDays.exec(date, days, null); + NodeValue returnedDate = addDays.exec(date, days); NodeValue expectedDate = getNode("2022-01-01", dt); assertEquals(expectedDate, returnedDate);