Jena: ¿Cómo agregar propiedades con valores numéricos?

How can I add numeric values like float, int or date a una Resource using Jena?

I assume I'd have to use resource.addProperty(Property, String, RDFDataType), but how do I instantiate the correct RDFDataType for the above mentioned data types?

preguntado el 10 de marzo de 12 a las 16:03

2 Respuestas

The 'official' documentation on typed literals is here:

Puede utilizar el addLiteral y add methods of a Jena Modelo, Por ejemplo:

    Model model = ...

    model.addLiteral (subject, predicate, 10);
    model.addLiteral (subject, predicate, 0.5);
    model.addLiteral (subject, predicate, (float)0.5);
    model.addLiteral (subject, predicate, ResourceFactory.createTypedLiteral(20));
    model.addLiteral (subject, predicate, ResourceFactory.createTypedLiteral(0.99));
    model.addLiteral (subject, predicate, true);
    model.add (subject, predicate, ResourceFactory.createTypedLiteral("2012-03-11", XSDDatatype.XSDdate));
    model.add (subject, predicate, ResourceFactory.createTypedLiteral("P2Y", XSDDatatype.XSDduration));

RDFDatatype is an interface so you cannot instantiate it directly. However, look at classes implementing that interface. You'll find XSDDatatype is one of those classes. There are others.

Si quieres ver un ejemplo completo, have a look here: The output of is the following RDF (serialized using Turtle format):

@prefix xsd:     <> .
@prefix example:  <> .

      example:p1 "10"^^xsd:int ;
      example:p2 "0.5"^^xsd:double ;
      example:p3 "0.5"^^xsd:float ;
      example:p4 "20"^^xsd:int ;
      example:p5 "0.99"^^xsd:double ;
      example:p6 "true"^^xsd:boolean ;
      example:p7 "2012-03-11"^^xsd:date ;
      example:p8 "P2Y"^^xsd:duration .

respondido 12 mar '12, 16:03

Estos son procesos literales in RDF. You can utilise, e.g., the rdf:value property to add numeric values as literals to your resource. You can specify tipos de datos on these literals, e.g., xsd:int.

respondido 10 mar '12, 18:03

how can we print datatype for string value? e.g. model.addLiteral (subject, predicate, "test str"); it should print like this but not printing: example:p11 "test str"^^xsd:string ; - Max Steel

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.