Skip to content

Commit

Permalink
[#184] Refactor out spin from AddDays function
Browse files Browse the repository at this point in the history
  • Loading branch information
blcham committed Aug 14, 2024
1 parent bd7023c commit 651c154
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -29,37 +27,37 @@ 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){
}

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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,25 +15,25 @@ 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);
}

@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);
Expand Down

0 comments on commit 651c154

Please sign in to comment.