-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#184] Add SPINRDF custom function call from query test
- Loading branch information
Showing
4 changed files
with
217 additions
and
40 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
s-pipes-core/src/main/java/cz/cvut/spipes/function/time/AddDaysDeprecated.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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; | ||
|
||
/** | ||
* Extend specified `date` by number of `days`. Return typed literal with same datatype. | ||
* Currently, supports only xsd:date datatype. | ||
*/ | ||
public class AddDaysDeprecated extends AbstractFunction2 implements ValueFunction { | ||
|
||
|
||
private static final String TYPE_IRI = KBSS_TIMEF.uri + "add-days-deprecated"; | ||
|
||
@Override | ||
public String getTypeURI() { | ||
return TYPE_IRI; | ||
} | ||
|
||
@Override | ||
protected NodeValue exec(Node date, Node days, FunctionEnv env) { | ||
|
||
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); | ||
} | ||
} catch (DateTimeParseException e){ | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private Long getDays(Node days) { | ||
return Optional.of(days) | ||
.filter(Node::isLiteral) | ||
.filter(n -> n.getLiteralValue() instanceof Integer) | ||
.map(n -> ((Integer) n.getLiteralValue()).longValue()) | ||
.orElse(null); | ||
} | ||
|
||
RDFDatatype getDatatype(Node date) { | ||
|
||
return Optional.of(date) | ||
.filter(Node::isLiteral) | ||
.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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
s-pipes-core/src/test/java/cz/cvut/spipes/function/time/AddDaysDeprecatedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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 AddDaysDeprecatedTest { | ||
|
||
@Test | ||
public void execReturnsTimeFromPast() { | ||
|
||
AddDaysDeprecated addDays = new AddDaysDeprecated(); | ||
Node date = getDateNode("2022-01-01").asNode(); | ||
Node days = NodeValue.makeNodeDecimal("-1").asNode(); | ||
|
||
NodeValue returnedDate = addDays.exec(date, days, null); | ||
|
||
NodeValue expectedDate = getDateNode("2021-12-31"); | ||
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(); | ||
|
||
AddDaysDeprecated addDays = new AddDaysDeprecated(); | ||
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 getNode(String date, RDFDatatype datatype) { | ||
return NodeValue.makeNode( | ||
date, | ||
null, | ||
datatype.getURI() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters