use of org.apache.jena.datatypes.xsd.XSDDateTime in project jena by apache.
the class ModelCom method createTypedLiteral.
/**
* Create a typed literal xsd:dateTime from a Calendar object.
*/
@Override
public Literal createTypedLiteral(Calendar cal) {
Object value = new XSDDateTime(cal);
LiteralLabel ll = LiteralLabelFactory.createByValue(value, "", XSDDatatype.XSDdateTime);
return new LiteralImpl(NodeFactory.createLiteral(ll), this);
}
use of org.apache.jena.datatypes.xsd.XSDDateTime in project jena by apache.
the class TestFBRules method testBuiltins2.
/**
* Test the builtins themselves
*/
public void testBuiltins2() {
// Numeric comparisions
Node lt = NodeFactory.createURI("lt");
Node gt = NodeFactory.createURI("gt");
Node le = NodeFactory.createURI("le");
Node ge = NodeFactory.createURI("ge");
Node eq = NodeFactory.createURI("eq");
Node ne = NodeFactory.createURI("ne");
String rules = "[r1: (?x q ?vx), (?y q ?vy), lessThan(?vx, ?vy) -> (?x lt ?y)]" + "[r2: (?x q ?vx), (?y q ?vy), greaterThan(?vx, ?vy) -> (?x gt ?y)]" + "[r3: (?x q ?vx), (?y q ?vy), le(?vx, ?vy) -> (?x le ?y)]" + "[r4: (?x q ?vx), (?y q ?vy), ge(?vx, ?vy) -> (?x ge ?y)]" + "[r5: (?x q ?vx), (?y q ?vy), notEqual(?vx, ?vy) -> (?x ne ?y)]" + "[r6: (?x q ?vx), (?y q ?vy), equal(?vx, ?vy) -> (?x eq ?y)]" + "";
Graph data = Factory.createGraphMem();
data.add(new Triple(n1, q, Util.makeIntNode(2)));
data.add(new Triple(n2, q, Util.makeIntNode(2)));
data.add(new Triple(n3, q, Util.makeIntNode(3)));
InfGraph infgraph = createInfGraph(rules, data);
TestUtil.assertIteratorValues(this, infgraph.find(n1, null, n2), new Triple[] { new Triple(n1, eq, n2), new Triple(n1, le, n2), new Triple(n1, ge, n2) });
TestUtil.assertIteratorValues(this, infgraph.find(n1, null, n3), new Triple[] { new Triple(n1, ne, n3), new Triple(n1, lt, n3), new Triple(n1, le, n3) });
TestUtil.assertIteratorValues(this, infgraph.find(n3, null, n1), new Triple[] { new Triple(n3, ne, n1), new Triple(n3, gt, n1), new Triple(n3, ge, n1) });
// Floating point comparisons
data = Factory.createGraphMem();
data.add(new Triple(n1, q, Util.makeIntNode(2)));
data.add(new Triple(n2, q, Util.makeDoubleNode(2.2)));
data.add(new Triple(n3, q, Util.makeDoubleNode(2.3)));
infgraph = createInfGraph(rules, data);
TestUtil.assertIteratorValues(this, infgraph.find(n1, null, n2), new Triple[] { new Triple(n1, ne, n2), new Triple(n1, le, n2), new Triple(n1, lt, n2) });
TestUtil.assertIteratorValues(this, infgraph.find(n2, null, n3), new Triple[] { new Triple(n2, ne, n3), new Triple(n2, le, n3), new Triple(n2, lt, n3) });
// XSD timeDate point comparisons
data = Factory.createGraphMem();
XSDDatatype dt = new XSDDatatype("dateTime");
data.add(new Triple(n1, q, NodeFactory.createLiteral("2000-03-04T20:00:00Z", XSDDatatype.XSDdateTime)));
data.add(new Triple(n2, q, NodeFactory.createLiteral("2001-03-04T20:00:00Z", XSDDatatype.XSDdateTime)));
data.add(new Triple(n3, q, NodeFactory.createLiteral("2002-03-04T20:00:00Z", XSDDatatype.XSDdateTime)));
infgraph = createInfGraph(rules, data);
TestUtil.assertIteratorValues(this, infgraph.find(n1, null, n2), new Triple[] { new Triple(n1, ne, n2), new Triple(n1, le, n2), new Triple(n1, lt, n2) });
TestUtil.assertIteratorValues(this, infgraph.find(n2, null, n3), new Triple[] { new Triple(n2, ne, n3), new Triple(n2, le, n3), new Triple(n2, lt, n3) });
TestUtil.assertIteratorValues(this, infgraph.find(n2, null, n1), new Triple[] { new Triple(n2, ne, n1), new Triple(n2, ge, n1), new Triple(n2, gt, n1) });
TestUtil.assertIteratorValues(this, infgraph.find(n3, null, n2), new Triple[] { new Triple(n3, ne, n2), new Triple(n3, ge, n2), new Triple(n3, gt, n2) });
// Support for now(?x)
rules = "[r1: now(?x) -> (a p ?x)]";
infgraph = createInfGraph(rules);
infgraph.prepare();
Graph result = infgraph.getDeductionsGraph();
assertEquals(1, result.size());
Triple tr = result.find(null, null, null).next();
Node nowN = tr.getObject();
assertTrue(nowN.isLiteral());
Object nowO = nowN.getLiteralValue();
assertTrue(nowO instanceof XSDDateTime);
// Arithmetic
rules = "[r1: (?x p ?a), (?x q ?b), sum(?a, ?b, ?c) -> (?x s ?c)]" + "[r2: (?x p ?a), (?x q ?b), product(?a, ?b, ?c) -> (?x t ?c)]" + "[r3: (?x p ?a), (?x q ?b), difference(?b, ?a, ?c) -> (?x u ?c)]" + "[r4: (?x p ?a), (?x q ?b), quotient(?b, ?a, ?c) -> (?x v ?c)]" + "[r4: (?x p ?a), (?x q ?b), min(?b, ?a, ?c) -> (?x r ?c)]" + "[r4: (?x p ?a), (?x q ?b), max(?b, ?a, ?c) -> (?x x ?c)]" + "";
data = Factory.createGraphMem();
data.add(new Triple(n1, p, Util.makeIntNode(3)));
data.add(new Triple(n1, q, Util.makeIntNode(5)));
infgraph = createInfGraph(rules, data);
TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null), new Triple[] { new Triple(n1, p, Util.makeIntNode(3)), new Triple(n1, q, Util.makeIntNode(5)), new Triple(n1, s, Util.makeIntNode(8)), new Triple(n1, t, Util.makeIntNode(15)), new Triple(n1, u, Util.makeIntNode(2)), new Triple(n1, v, Util.makeIntNode(1)), new Triple(n1, r, Util.makeIntNode(3)), new Triple(n1, x, Util.makeIntNode(5)) });
// Note type checking
rules = "[r1: (?x p ?y), isLiteral(?y) -> (?x s 'literal')]" + "[r1: (?x p ?y), notLiteral(?y) -> (?x s 'notLiteral')]" + "[r1: (?x p ?y), isBNode(?y) -> (?x s 'bNode')]" + "[r1: (?x p ?y), notBNode(?y) -> (?x s 'notBNode')]" + "";
data = Factory.createGraphMem();
data.add(new Triple(n1, p, Util.makeIntNode(3)));
data.add(new Triple(n2, p, res));
data.add(new Triple(n3, p, NodeFactory.createBlankNode()));
infgraph = createInfGraph(rules, data);
TestUtil.assertIteratorValues(this, infgraph.find(n1, s, null), new Triple[] { new Triple(n1, s, NodeFactory.createLiteral("literal")), new Triple(n1, s, NodeFactory.createLiteral("notBNode")) });
TestUtil.assertIteratorValues(this, infgraph.find(n2, s, null), new Triple[] { new Triple(n2, s, NodeFactory.createLiteral("notLiteral")), new Triple(n2, s, NodeFactory.createLiteral("notBNode")) });
TestUtil.assertIteratorValues(this, infgraph.find(n3, s, null), new Triple[] { new Triple(n3, s, NodeFactory.createLiteral("notLiteral")), new Triple(n3, s, NodeFactory.createLiteral("bNode")) });
// Data type checking
rules = "[r1: (?x p ?y), isDType(?y, rdfs:Literal) -> (?x s 'isLiteral')]" + "[r1: (?x p ?y), isDType(?y, http://www.w3.org/2001/XMLSchema#int) -> (?x s 'isXSDInt')]" + "[r1: (?x p ?y), isDType(?y, http://www.w3.org/2001/XMLSchema#string) -> (?x s 'isXSDString')]" + "[r1: (?x p ?y), notDType(?y, rdfs:Literal) -> (?x s 'notLiteral')]" + "[r1: (?x p ?y), notDType(?y, http://www.w3.org/2001/XMLSchema#int) -> (?x s 'notXSDInt')]" + "[r1: (?x p ?y), notDType(?y, http://www.w3.org/2001/XMLSchema#string) -> (?x s 'notXSDString')]" + "";
data = Factory.createGraphMem();
data.add(new Triple(n1, p, Util.makeIntNode(3)));
data.add(new Triple(n2, p, NodeFactory.createLiteral("foo")));
data.add(new Triple(n3, p, NodeFactory.createLiteral("foo", XSDDatatype.XSDstring)));
data.add(new Triple(n4, p, n4));
data.add(new Triple(n5, p, NodeFactory.createLiteral("-1", XSDDatatype.XSDnonNegativeInteger)));
infgraph = createInfGraph(rules, data);
TestUtil.assertIteratorValues(this, infgraph.find(null, s, null), new Triple[] { new Triple(n1, s, NodeFactory.createLiteral("isLiteral")), new Triple(n1, s, NodeFactory.createLiteral("isXSDInt")), new Triple(n1, s, NodeFactory.createLiteral("notXSDString")), new Triple(n2, s, NodeFactory.createLiteral("isLiteral")), new Triple(n2, s, NodeFactory.createLiteral("notXSDInt")), new Triple(n2, s, NodeFactory.createLiteral("isXSDString")), new Triple(n3, s, NodeFactory.createLiteral("isLiteral")), new Triple(n3, s, NodeFactory.createLiteral("notXSDInt")), new Triple(n3, s, NodeFactory.createLiteral("isXSDString")), new Triple(n4, s, NodeFactory.createLiteral("notLiteral")), new Triple(n4, s, NodeFactory.createLiteral("notXSDInt")), new Triple(n4, s, NodeFactory.createLiteral("notXSDString")), new Triple(n5, s, NodeFactory.createLiteral("notLiteral")), new Triple(n5, s, NodeFactory.createLiteral("notXSDInt")), new Triple(n5, s, NodeFactory.createLiteral("notXSDString")) });
// Literal counting
rules = "[r1: (?x p ?y), countLiteralValues(?x, p, ?c) -> (?x s ?c)]";
data = Factory.createGraphMem();
data.add(new Triple(n1, p, Util.makeIntNode(2)));
data.add(new Triple(n1, p, Util.makeIntNode(2)));
data.add(new Triple(n1, p, Util.makeIntNode(3)));
data.add(new Triple(n1, p, n2));
infgraph = createInfGraph(rules, data);
TestUtil.assertIteratorValues(this, infgraph.find(n1, s, null), new Triple[] { new Triple(n1, s, Util.makeIntNode(2)) });
// Map list operation
rules = "[r1: (n1 p ?l) -> listMapAsSubject(?l, q, C1)]" + "[r2: (n1 p ?l) -> listMapAsObject ( a, q, ?l)]";
data = Factory.createGraphMem();
data.add(new Triple(n1, p, Util.makeList(new Node[] { b, c, d }, data)));
infgraph = createInfGraph(rules, data);
TestUtil.assertIteratorValues(this, infgraph.find(null, q, null), new Triple[] { new Triple(b, q, C1), new Triple(c, q, C1), new Triple(d, q, C1), new Triple(a, q, b), new Triple(a, q, c), new Triple(a, q, d) });
}
use of org.apache.jena.datatypes.xsd.XSDDateTime in project jena by apache.
the class NodeValue method _setByValue.
// Returns null for unrecognized literal.
private static NodeValue _setByValue(Node node) {
// nodeToNodeValue should have dealt with it.
if (NodeUtils.hasLang(node))
return new NodeValueLang(node);
LiteralLabel lit = node.getLiteral();
String lex = lit.getLexicalForm();
RDFDatatype datatype = lit.getDatatype();
// Quick check.
// Only XSD supported.
// And (for testing) roman numerals.
String datatypeURI = datatype.getURI();
if (!datatypeURI.startsWith(xsdNamespace) && !SystemARQ.EnableRomanNumerals) {
// Not XSD.
return null;
}
try {
// DatatypeFormatException - should not happen
if (XSDstring.isValidLiteral(lit))
// String - plain or xsd:string, or derived datatype.
return new NodeValueString(lit.getLexicalForm(), node);
if (!datatype.equals(XSDdecimal)) {
// XSD integer and derived types
if (XSDinteger.isValidLiteral(lit)) {
// .trim() implements the facet of whitespace collapse.
// BigInteger does not accept such whitespace.
String s = node.getLiteralLexicalForm().trim();
if (s.startsWith("+"))
// BigInteger does not accept leading "+"
s = s.substring(1);
// Includes subtypes (int, byte, postiveInteger etc).
// NB Known to be valid for type by now
BigInteger integer = new BigInteger(s);
return new NodeValueInteger(integer, node);
}
}
if (datatype.equals(XSDdecimal) && XSDdecimal.isValidLiteral(lit)) {
BigDecimal decimal = new BigDecimal(lit.getLexicalForm());
return new NodeValueDecimal(decimal, node);
}
if (datatype.equals(XSDfloat) && XSDfloat.isValidLiteral(lit)) {
// NB If needed, call to floatValue, then assign to double.
// Gets 1.3f != 1.3d right
float f = ((Number) lit.getValue()).floatValue();
return new NodeValueFloat(f, node);
}
if (datatype.equals(XSDdouble) && XSDdouble.isValidLiteral(lit)) {
double d = ((Number) lit.getValue()).doubleValue();
return new NodeValueDouble(d, node);
}
if ((datatype.equals(XSDdateTime) || datatype.equals(XSDdateTimeStamp)) && XSDdateTime.isValid(lex)) {
XSDDateTime dateTime = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDdate) && XSDdate.isValidLiteral(lit)) {
// Jena datatype support works on masked dataTimes.
XSDDateTime dateTime = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDtime) && XSDtime.isValidLiteral(lit)) {
// Jena datatype support works on masked dataTimes.
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgYear) && XSDgYear.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgYearMonth) && XSDgYearMonth.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgMonth) && XSDgMonth.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgMonthDay) && XSDgMonthDay.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgDay) && XSDgDay.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDduration) && XSDduration.isValid(lex)) {
Duration duration = xmlDatatypeFactory.newDuration(lex);
return new NodeValueDuration(duration, node);
}
if (datatype.equals(XSDyearMonthDuration) && XSDyearMonthDuration.isValid(lex)) {
Duration duration = xmlDatatypeFactory.newDuration(lex);
return new NodeValueDuration(duration, node);
}
if (datatype.equals(XSDdayTimeDuration) && XSDdayTimeDuration.isValid(lex)) {
Duration duration = xmlDatatypeFactory.newDuration(lex);
return new NodeValueDuration(duration, node);
}
if (datatype.equals(XSDboolean) && XSDboolean.isValidLiteral(lit)) {
boolean b = (Boolean) lit.getValue();
return new NodeValueBoolean(b, node);
}
// Not wired in
if (SystemARQ.EnableRomanNumerals) {
if (lit.getDatatypeURI().equals(RomanNumeralDatatype.get().getURI())) {
Object obj = RomanNumeralDatatype.get().parse(lit.getLexicalForm());
if (obj instanceof Integer)
return new NodeValueInteger(((Integer) obj).longValue());
if (obj instanceof RomanNumeral)
return new NodeValueInteger(((RomanNumeral) obj).intValue());
throw new ARQInternalErrorException("DatatypeFormatException: Roman numeral is unknown class");
}
}
} catch (DatatypeFormatException ex) {
// Should have been caught earlier by special test in nodeToNodeValue
throw new ARQInternalErrorException("DatatypeFormatException: " + lit, ex);
}
return null;
}
use of org.apache.jena.datatypes.xsd.XSDDateTime in project jena by apache.
the class Util method compareTypedLiterals.
/**
* General order comparator for typed literal nodes, works for all numbers and
* for date times.
*
*/
// Thanks to Bradley Schatz (Bradley@greystate.com) for the original suggestions
// for datetime comparison. This code is a rewrite based on those suggestions.
public static int compareTypedLiterals(Node n1, Node n2) {
if (n1.isLiteral() && n2.isLiteral()) {
Object v1 = n1.getLiteralValue();
Object v2 = n2.getLiteralValue();
if (v1 instanceof XSDDateTime && v2 instanceof XSDDateTime) {
XSDDateTime a = (XSDDateTime) v1;
XSDDateTime b = (XSDDateTime) v2;
return a.compare(b);
} else {
if (v1 instanceof Number && v2 instanceof Number) {
if (v1 instanceof Float || v1 instanceof Double || v2 instanceof Float || v2 instanceof Double) {
double d1 = ((Number) v1).doubleValue();
double d2 = ((Number) v2).doubleValue();
return (d1 < d2) ? -1 : ((d1 == d2) ? 0 : +1);
} else {
long l1 = ((Number) v1).longValue();
long l2 = ((Number) v2).longValue();
return (l1 < l2) ? -1 : ((l1 == l2) ? 0 : +1);
}
}
}
}
throw new ClassCastException("Compare typed literals can only compare numbers and datetimes");
}
use of org.apache.jena.datatypes.xsd.XSDDateTime in project jena by apache.
the class Util method compareInstants.
/**
* Compare two time Instant nodes.
* @param n1 the first time instant (XSDDateTime) valued literal node
* @param n2 the second time instant (XSDDateTime) valued literal node
* @return -1 if n1 is less than n2, 0 if n1 equals n2 and +1 if n1 greater than n2
* @throws ClassCastException if either not is not numeric
*/
public static int compareInstants(Node n1, Node n2) {
if (n1.isLiteral() && n2.isLiteral()) {
Object v1 = n1.getLiteralValue();
Object v2 = n2.getLiteralValue();
if (v1 instanceof XSDDateTime && v2 instanceof XSDDateTime) {
XSDDateTime a = (XSDDateTime) v1;
XSDDateTime b = (XSDDateTime) v2;
return a.compare(b);
}
}
throw new ClassCastException("Non-numeric literal in compareNumbers");
}
Aggregations