Search in sources :

Example 1 with XSDDatatype

use of org.apache.jena.datatypes.xsd.XSDDatatype in project jena by apache.

the class TestFmtUtils method stringLiteral.

@Test
public void stringLiteral() throws Exception {
    Node_Literal nl = (Node_Literal) NodeFactory.createLiteral("abc", "no", new XSDDatatype("string"));
    assertEquals("\"abc\"@no", FmtUtils.stringForLiteral(nl, getContext()));
}
Also used : Node_Literal(org.apache.jena.graph.Node_Literal) XSDDatatype(org.apache.jena.datatypes.xsd.XSDDatatype) Test(org.junit.Test)

Example 2 with XSDDatatype

use of org.apache.jena.datatypes.xsd.XSDDatatype in project jena by apache.

the class TestFmtUtils method decimalLiteral.

@Test
public void decimalLiteral() throws Exception {
    Node_Literal nl = (Node_Literal) NodeFactory.createLiteral("2.4", new XSDDatatype("decimal"));
    assertEquals("2.4", FmtUtils.stringForLiteral(nl, getContext()));
}
Also used : Node_Literal(org.apache.jena.graph.Node_Literal) XSDDatatype(org.apache.jena.datatypes.xsd.XSDDatatype) Test(org.junit.Test)

Example 3 with XSDDatatype

use of org.apache.jena.datatypes.xsd.XSDDatatype in project jena by apache.

the class TestFmtUtils method integerLiteral.

@Test
public void integerLiteral() throws Exception {
    Node_Literal nl = (Node_Literal) NodeFactory.createLiteral("2", new XSDDatatype("int"));
    assertEquals("\"2\"^^<http://www.w3.org/2001/XMLSchema#int>", FmtUtils.stringForLiteral(nl, getContext()));
}
Also used : Node_Literal(org.apache.jena.graph.Node_Literal) XSDDatatype(org.apache.jena.datatypes.xsd.XSDDatatype) Test(org.junit.Test)

Example 4 with XSDDatatype

use of org.apache.jena.datatypes.xsd.XSDDatatype 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) });
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) XSDDateTime(org.apache.jena.datatypes.xsd.XSDDateTime) XSDDatatype(org.apache.jena.datatypes.xsd.XSDDatatype)

Example 5 with XSDDatatype

use of org.apache.jena.datatypes.xsd.XSDDatatype in project jena by apache.

the class NumLengthConstraint method nodeSatisfies.

@Override
public ReportItem nodeSatisfies(ValidationContext vCxt, Node n) {
    if (!n.isLiteral()) {
        String msg = format("NumericConstraint: Not numeric: %s ", ShexLib.displayStr(n));
        return new ReportItem(msg, n);
    }
    RDFDatatype rdfDT = n.getLiteralDatatype();
    if (!(rdfDT instanceof XSDDatatype)) {
        String msg = format("NumericConstraint: Not a numeric: %s ", ShexLib.displayStr(n));
        return new ReportItem(msg, n);
    }
    if (XSDDatatype.XSDfloat.equals(rdfDT) || XSDDatatype.XSDdouble.equals(rdfDT)) {
        String msg = format("NumericConstraint: Numeric not compatible with xsd:decimal: %s ", ShexLib.displayStr(n));
        return new ReportItem(msg, n);
    }
    String lexicalForm = n.getLiteralLexicalForm();
    if (!rdfDT.isValid(lexicalForm)) {
        String msg = format("NumericConstraint: Not a valid xsd:decimal: %s ", ShexLib.displayStr(n));
        return new ReportItem(msg, n);
    }
    String str = lexicalForm;
    int N = str.length();
    int idx = str.indexOf('.');
    switch(lengthType) {
        case FRACTIONDIGITS:
            {
                // Does not include trailing zeros.
                if (idx < 0)
                    return null;
                // int before = idx;
                int after = str.length() - idx - 1;
                for (int i = N - 1; i > idx; i--) {
                    if (str.charAt(i) != '0')
                        break;
                    after--;
                }
                if (after <= length)
                    return null;
                break;
            }
        case TOTALDIGITS:
            {
                // Canonical form.
                int start = 0;
                char ch1 = str.charAt(0);
                if (ch1 == '+' || ch1 == '-')
                    start++;
                // Leading zeros
                for (int i = start; i < N; i++) {
                    if (str.charAt(i) != '0')
                        break;
                    start++;
                }
                int finish = N;
                // Trailing zeros
                if (idx >= 0) {
                    finish--;
                    for (int i = N - 1; i > idx; i--) {
                        if (str.charAt(i) != '0')
                            break;
                        finish--;
                    }
                }
                int digits = finish - start;
                if (digits <= length)
                    return null;
                break;
            }
        default:
            break;
    }
    String msg = format("Expected %s %d : got = %d", lengthType.label(), length, str.length());
    return new ReportItem(msg, n);
}
Also used : XSDDatatype(org.apache.jena.datatypes.xsd.XSDDatatype) ReportItem(org.apache.jena.shex.sys.ReportItem) RDFDatatype(org.apache.jena.datatypes.RDFDatatype)

Aggregations

XSDDatatype (org.apache.jena.datatypes.xsd.XSDDatatype)8 Node_Literal (org.apache.jena.graph.Node_Literal)5 Test (org.junit.Test)5 RDFDatatype (org.apache.jena.datatypes.RDFDatatype)1 XSDDateTime (org.apache.jena.datatypes.xsd.XSDDateTime)1 InfGraph (org.apache.jena.reasoner.InfGraph)1 ReportItem (org.apache.jena.shex.sys.ReportItem)1