Skip to content

Commit

Permalink
[Upd] Support string datatype in add-days function
Browse files Browse the repository at this point in the history
  • Loading branch information
blcham committed Oct 25, 2021
1 parent 5fe4e52 commit fe0732d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cz.cvut.spipes.constants;

import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.ResourceFactory;

public class KBSS_TIMEF {

/**
* The namespace of the vocabulary as a string
*/
public static final String uri = "http://onto.fel.cvut.cz/ontologies/lib/function/time/";

protected static final Property property(String local )
{ return ResourceFactory.createProperty( uri, local ); }

/**
returns the URI for this schema
@return the URI for this schema
*/
public static String getURI() {
return uri;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package cz.cvut.spipes.function.time;

import cz.cvut.spipes.constants.KBSS_TIMEF;
import cz.cvut.spipes.function.ValueFunction;
import org.apache.jena.datatypes.RDFDatatype;
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 java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.Optional;

/**
Expand All @@ -19,7 +22,7 @@
public class AddDays extends AbstractFunction2 implements ValueFunction {


private static final String TYPE_IRI = "http://onto.fel.cvut.cz/ontologies/lib/function/time/add-days";
private static final String TYPE_IRI = KBSS_TIMEF.getURI() + "add-days";

@Override
public String getTypeURI() {
Expand All @@ -30,12 +33,15 @@ public String getTypeURI() {
protected NodeValue exec(Node date, Node days, FunctionEnv env) {

Long daysToAdd = getDays(days);
RDFDatatype datatype = getDatatype(date);

if (getDatatype(date).equals(XSDDatatype.XSDdate) && daysToAdd != null) {

String newDate = LocalDate.parse(date.getLiteral().getValue().toString()).plusDays(daysToAdd).toString();

return NodeValue.makeNode(newDate, XSDDatatype.XSDdate);
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);
}
} catch (DateTimeParseException e){
}

return null;
Expand All @@ -53,8 +59,17 @@ RDFDatatype getDatatype(Node date) {

return Optional.of(date)
.filter(Node::isLiteral)
.filter(n -> n.getLiteralDatatype() instanceof XSDDateType)
.map(Node::getLiteralDatatype)
.map(n -> getNewDatatype(n.getLiteralDatatype()))
.orElse(null);
}

RDFDatatype getNewDatatype(RDFDatatype datatype){
if (datatype instanceof XSDDateType) {
return XSDDatatype.XSDdate;
}
if (datatype instanceof XSDBaseStringType) {
return XSDDatatype.XSDstring;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cz.cvut.spipes.function.time;

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;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AddDaysTest {
Expand All @@ -22,12 +25,32 @@ public void execReturnsTimeFromPast() {
assertEquals(expectedDate, returnedDate);
}

@Test
public void execReturnsDatatypeOfInputLiteral() {
Node days = NodeValue.makeNodeDecimal("1").asNode();

Stream.of(XSDDatatype.XSDdate, XSDDatatype.XSDstring).forEach(
dt -> {
Node date = getNode("2021-12-31", dt).asNode();

AddDays addDays = new AddDays();
NodeValue returnedDate = addDays.exec(date, days, null);

NodeValue expectedDate = getNode("2022-01-01", dt);
assertEquals(expectedDate, returnedDate);
});
}


private NodeValue getDateNode(String date){
return getNode(date, XSDDatatype.XSDdate);
}

private NodeValue getDateNode(String date) {
private NodeValue getNode(String date, RDFDatatype datatype) {
return NodeValue.makeNode(
date,
null,
XSDDatatype.XSDdate.getURI()
datatype.getURI()
);
}
}

0 comments on commit fe0732d

Please sign in to comment.