Search in sources :

Example 41 with LiteralLabel

use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.

the class FBRuleInfGraph method checkLiteral.

/**
 * Check a given literal value for a property against the set of
 * known range constraints for it.
 * @param prop the property node whose range is under scrutiny
 * @param triple the statement whose object value is to be checked.
 * @return null if the range is legal, otherwise a ValidityReport.Report
 * which describes the problem.
 */
public ValidityReport.Report checkLiteral(Node prop, Triple triple) {
    Node value = triple.getObject();
    List<RDFDatatype> range = getDTRange().get(prop);
    if (range != null) {
        if (value.isBlank())
            return null;
        if (!value.isLiteral()) {
            return new ValidityReport.Report(true, "dtRange", "Property " + prop + " has a typed range but was given a non literal value " + value);
        }
        LiteralLabel ll = value.getLiteral();
        for (RDFDatatype dt : range) {
            if (!dt.isValidLiteral(ll)) {
                return new ValidityReport.Report(true, "dtRange", "Property " + prop + " has a typed range " + dt + "that is not compatible with " + value, triple);
            }
        }
    }
    return null;
}
Also used : RDFNode(org.apache.jena.rdf.model.RDFNode) RDFDatatype(org.apache.jena.datatypes.RDFDatatype) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 42 with LiteralLabel

use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.

the class ConvertersTest method makeNodeOrPathTest.

@Test
public void makeNodeOrPathTest() {
    PrefixMapping pMap = PrefixMapping.Factory.create();
    pMap.setNsPrefixes(PrefixMapping.Standard);
    Object n = Converters.makeNodeOrPath(null, pMap);
    assertEquals(Node.ANY, n);
    n = Converters.makeNodeOrPath(RDF.type, pMap);
    assertEquals(RDF.type.asNode(), n);
    Node n2 = NodeFactory.createBlankNode();
    n = Converters.makeNodeOrPath(n2, pMap);
    assertEquals(n2, n);
    pMap.setNsPrefix("demo", "http://example.com/");
    n = Converters.makeNodeOrPath("demo:type", pMap);
    assertEquals(NodeFactory.createURI("http://example.com/type"), n);
    n = Converters.makeNodeOrPath("<one>", pMap);
    assertEquals(NodeFactory.createURI("one"), n);
    UUID uuid = UUID.randomUUID();
    try {
        Converters.makeNodeOrPath(uuid, pMap);
        fail("Should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // do nothing
    }
    TypeMapper.getInstance().registerDatatype(new UuidDataType());
    n = Converters.makeNodeOrPath(uuid, pMap);
    LiteralLabel ll = LiteralLabelFactory.createTypedLiteral(uuid);
    assertEquals(NodeFactory.createLiteral(ll), n);
    n = Converters.makeNodeOrPath(NodeFactory.createVariable("foo"), pMap);
    assertTrue(n instanceof Var);
    Node node = (Node) n;
    assertTrue(node.isVariable());
    assertEquals("foo", node.getName());
    n = Converters.makeNodeOrPath("'text'@en", pMap);
    assertTrue(n instanceof Node);
    node = (Node) n;
    assertEquals("text", node.getLiteralLexicalForm());
    assertEquals("en", node.getLiteralLanguage());
    n = Converters.makeNodeOrPath("<one>/<two>", pMap);
    assertTrue(n instanceof Path);
    Path pth = (Path) n;
    assertEquals("<one>/<two>", pth.toString());
}
Also used : Path(org.apache.jena.sparql.path.Path) PrefixMapping(org.apache.jena.shared.PrefixMapping) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) FrontsNode(org.apache.jena.graph.FrontsNode) Node(org.apache.jena.graph.Node) UUID(java.util.UUID) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel) Test(org.junit.Test)

Example 43 with LiteralLabel

use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.

the class ConvertersTest method makeNodeTest.

@Test
public void makeNodeTest() {
    PrefixMapping pMap = PrefixMapping.Factory.create();
    pMap.setNsPrefixes(PrefixMapping.Standard);
    Node n = Converters.makeNode(null, pMap);
    assertEquals(Node.ANY, n);
    n = Converters.makeNode(RDF.type, pMap);
    assertEquals(RDF.type.asNode(), n);
    Node n2 = NodeFactory.createBlankNode();
    n = Converters.makeNode(n2, pMap);
    assertEquals(n2, n);
    pMap.setNsPrefix("demo", "http://example.com/");
    n = Converters.makeNode("demo:type", pMap);
    assertEquals(NodeFactory.createURI("http://example.com/type"), n);
    n = Converters.makeNode("<one>", pMap);
    assertEquals(NodeFactory.createURI("one"), n);
    UUID uuid = UUID.randomUUID();
    try {
        Converters.makeNode(uuid, pMap);
        fail("Should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // do nothing
    }
    TypeMapper.getInstance().registerDatatype(new UuidDataType());
    n = Converters.makeNode(uuid, pMap);
    LiteralLabel ll = LiteralLabelFactory.createTypedLiteral(uuid);
    assertEquals(NodeFactory.createLiteral(ll), n);
    n = Converters.makeNode(NodeFactory.createVariable("foo"), pMap);
    assertTrue(n.isVariable());
    assertEquals("foo", n.getName());
    assertTrue(n instanceof Var);
    n = Converters.makeNode("'text'@en", pMap);
    assertEquals("text", n.getLiteralLexicalForm());
    assertEquals("en", n.getLiteralLanguage());
}
Also used : PrefixMapping(org.apache.jena.shared.PrefixMapping) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) FrontsNode(org.apache.jena.graph.FrontsNode) Node(org.apache.jena.graph.Node) UUID(java.util.UUID) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel) Test(org.junit.Test)

Aggregations

LiteralLabel (org.apache.jena.graph.impl.LiteralLabel)43 Node (org.apache.jena.graph.Node)8 RDFDatatype (org.apache.jena.datatypes.RDFDatatype)7 DatatypeFormatException (org.apache.jena.datatypes.DatatypeFormatException)5 NodeFactory.createLiteral (org.apache.jena.graph.NodeFactory.createLiteral)5 Test (org.junit.Test)5 RDFLangString (org.apache.jena.datatypes.xsd.impl.RDFLangString)4 BigDecimal (java.math.BigDecimal)3 BaseDatatype (org.apache.jena.datatypes.BaseDatatype)3 SpatialIndexException (org.apache.jena.query.spatial.SpatialIndexException)3 Var (org.apache.jena.sparql.core.Var)3 UUID (java.util.UUID)2 FrontsNode (org.apache.jena.graph.FrontsNode)2 Triple (org.apache.jena.graph.Triple)2 PrefixMapping (org.apache.jena.shared.PrefixMapping)2 ExprVar (org.apache.jena.sparql.expr.ExprVar)2 FileReader (java.io.FileReader)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Duration (javax.xml.datatype.Duration)1