You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Miika Alonen <mi...@gofore.com> on 2019/06/12 12:06:50 UTC

Creating plain numeric literals using jena

Hi,

I have existing system that uses plain numeric literals in graphs like:

ex:prop sh:order 2 .

I'v been trying to add new literals using jena, but don't know how to create plain numeric literals. I tried this and it is working for Booleans but not Integers:

propShape.addLiteral(SH.order, 4)
propShape.addLiteral(SH.closed, true)

Result:
ex:prop sh:order    "4"^^xsd:long .
ex:prop sh:closed true .

Expected result:
ex:prop sh:order 4 .
ex:prop sh:closed true .

Is there a way to create these kind of numerical plain literals in Jena?

Br, Miika

Re: Creating plain numeric literals using jena

Posted by Martynas Jusevičius <ma...@atomgraph.com>.
Is it for human-readability?

Can you try model.createTypedLiteral(new Integer(4)) as the object?

https://jena.apache.org/documentation/notes/typed-literals.html

On Wed, Jun 12, 2019 at 2:44 PM Miika Alonen <mi...@gofore.com> wrote:
>
> How do this syntactic sugar actually work? It also works in json-ld so it not just turtle.
>
> I can achieve the same result using SPARQL:
>
>         Model model = ModelFactory.createDefaultModel();
>         String update = "PREFIX ns: <http://example.org/ns#> INSERT DATA { ns:foo ns:bar 1 . } ";
>         UpdateAction.parseExecute( update, model );
>         model.write( System.out, "TTL" );
>
> ... but I would prefer Jena API if possible.
>
> -----Original Message-----
> From: Martynas Jusevičius <ma...@atomgraph.com>
> Sent: keskiviikko 12. kesäkuuta 2019 15.22
> To: jena-users-ml <us...@jena.apache.org>
> Subject: Re: Creating plain numeric literals using jena
>
> AFAIK there are no "plain numeric literals". That's just syntactic sugar for numbers in Turtle: https://www.w3.org/TR/turtle/#abbrev
>
> The semantics of both forms are the same, so does it really matter?
>
> On Wed, Jun 12, 2019 at 2:07 PM Miika Alonen <mi...@gofore.com> wrote:
> >
> > Hi,
> >
> > I have existing system that uses plain numeric literals in graphs like:
> >
> > ex:prop sh:order 2 .
> >
> > I'v been trying to add new literals using jena, but don't know how to create plain numeric literals. I tried this and it is working for Booleans but not Integers:
> >
> > propShape.addLiteral(SH.order, 4)
> > propShape.addLiteral(SH.closed, true)
> >
> > Result:
> > ex:prop sh:order    "4"^^xsd:long .
> > ex:prop sh:closed true .
> >
> > Expected result:
> > ex:prop sh:order 4 .
> > ex:prop sh:closed true .
> >
> > Is there a way to create these kind of numerical plain literals in Jena?
> >
> > Br, Miika

RE: Creating plain numeric literals using jena

Posted by Miika Alonen <mi...@gofore.com>.
Thank you all for the clarifications!

It was confusing that the API created xsd:int by default, but this seems to work:
propertyShape.addLiteral(SH.order, ResourceFactory.createTypedLiteral(String.valueOf(order),XSDDatatype.XSDinteger));
               

-----Original Message-----
From: Andy Seaborne <an...@apache.org> 
Sent: keskiviikko 12. kesäkuuta 2019 15.53
To: users@jena.apache.org
Subject: Re: Creating plain numeric literals using jena

The parser (Turtle, SPARQL) does the magic (not that is very magic)

The turtle parse gets a token INTEGER and it calls

NodeFactory.createLiteral(lexical, XSDDatatype.XSDinteger) ;

All literals are typed in RDF 1.1

"abc" is "abc"^^xsd:string
2 is "2"^^xsd:integer

Once out the parser, it is a java object for "2"^^xsd:integer

JSON-LD is similar but the parser is a 3rd party jsonld-java.

(parsing happens at the lower Graph-triple-node level)

In Model-Statement:

ResourceFactory.createTypedLiteral

or

model.createTypedLiteral(long)
model.createTypedLiteral( String lxecicalform , RDFDatatype dType)

     Andy


On 12/06/2019 13:44, Miika Alonen wrote:
> How do this syntactic sugar actually work? It also works in json-ld so it not just turtle.
> 
> I can achieve the same result using SPARQL:
> 
>          Model model = ModelFactory.createDefaultModel();
>          String update = "PREFIX ns: <http://example.org/ns#> INSERT DATA { ns:foo ns:bar 1 . } ";
>          UpdateAction.parseExecute( update, model );
>          model.write( System.out, "TTL" );
> 
> ... but I would prefer Jena API if possible.
> 
> -----Original Message-----
> From: Martynas Jusevičius <ma...@atomgraph.com>
> Sent: keskiviikko 12. kesäkuuta 2019 15.22
> To: jena-users-ml <us...@jena.apache.org>
> Subject: Re: Creating plain numeric literals using jena
> 
> AFAIK there are no "plain numeric literals". That's just syntactic sugar for numbers in Turtle: https://www.w3.org/TR/turtle/#abbrev
> 
> The semantics of both forms are the same, so does it really matter?
> 
> On Wed, Jun 12, 2019 at 2:07 PM Miika Alonen <mi...@gofore.com> wrote:
>>
>> Hi,
>>
>> I have existing system that uses plain numeric literals in graphs like:
>>
>> ex:prop sh:order 2 .
>>
>> I'v been trying to add new literals using jena, but don't know how to create plain numeric literals. I tried this and it is working for Booleans but not Integers:
>>
>> propShape.addLiteral(SH.order, 4)
>> propShape.addLiteral(SH.closed, true)
>>
>> Result:
>> ex:prop sh:order    "4"^^xsd:long .
>> ex:prop sh:closed true .
>>
>> Expected result:
>> ex:prop sh:order 4 .
>> ex:prop sh:closed true .
>>
>> Is there a way to create these kind of numerical plain literals in Jena?
>>
>> Br, Miika

Re: Creating plain numeric literals using jena

Posted by Andy Seaborne <an...@apache.org>.
The parser (Turtle, SPARQL) does the magic (not that is very magic)

The turtle parse gets a token INTEGER and it calls

NodeFactory.createLiteral(lexical, XSDDatatype.XSDinteger) ;

All literals are typed in RDF 1.1

"abc" is "abc"^^xsd:string
2 is "2"^^xsd:integer

Once out the parser, it is a java object for "2"^^xsd:integer

JSON-LD is similar but the parser is a 3rd party jsonld-java.

(parsing happens at the lower Graph-triple-node level)

In Model-Statement:

ResourceFactory.createTypedLiteral

or

model.createTypedLiteral(long)
model.createTypedLiteral( String lxecicalform , RDFDatatype dType)

     Andy


On 12/06/2019 13:44, Miika Alonen wrote:
> How do this syntactic sugar actually work? It also works in json-ld so it not just turtle.
> 
> I can achieve the same result using SPARQL:
> 
>          Model model = ModelFactory.createDefaultModel();
>          String update = "PREFIX ns: <http://example.org/ns#> INSERT DATA { ns:foo ns:bar 1 . } ";
>          UpdateAction.parseExecute( update, model );
>          model.write( System.out, "TTL" );
> 
> ... but I would prefer Jena API if possible.
> 
> -----Original Message-----
> From: Martynas Jusevičius <ma...@atomgraph.com>
> Sent: keskiviikko 12. kesäkuuta 2019 15.22
> To: jena-users-ml <us...@jena.apache.org>
> Subject: Re: Creating plain numeric literals using jena
> 
> AFAIK there are no "plain numeric literals". That's just syntactic sugar for numbers in Turtle: https://www.w3.org/TR/turtle/#abbrev
> 
> The semantics of both forms are the same, so does it really matter?
> 
> On Wed, Jun 12, 2019 at 2:07 PM Miika Alonen <mi...@gofore.com> wrote:
>>
>> Hi,
>>
>> I have existing system that uses plain numeric literals in graphs like:
>>
>> ex:prop sh:order 2 .
>>
>> I'v been trying to add new literals using jena, but don't know how to create plain numeric literals. I tried this and it is working for Booleans but not Integers:
>>
>> propShape.addLiteral(SH.order, 4)
>> propShape.addLiteral(SH.closed, true)
>>
>> Result:
>> ex:prop sh:order    "4"^^xsd:long .
>> ex:prop sh:closed true .
>>
>> Expected result:
>> ex:prop sh:order 4 .
>> ex:prop sh:closed true .
>>
>> Is there a way to create these kind of numerical plain literals in Jena?
>>
>> Br, Miika

RE: Creating plain numeric literals using jena

Posted by Miika Alonen <mi...@gofore.com>.
How do this syntactic sugar actually work? It also works in json-ld so it not just turtle.

I can achieve the same result using SPARQL:

        Model model = ModelFactory.createDefaultModel();
        String update = "PREFIX ns: <http://example.org/ns#> INSERT DATA { ns:foo ns:bar 1 . } ";
        UpdateAction.parseExecute( update, model );
        model.write( System.out, "TTL" );

... but I would prefer Jena API if possible.

-----Original Message-----
From: Martynas Jusevičius <ma...@atomgraph.com> 
Sent: keskiviikko 12. kesäkuuta 2019 15.22
To: jena-users-ml <us...@jena.apache.org>
Subject: Re: Creating plain numeric literals using jena

AFAIK there are no "plain numeric literals". That's just syntactic sugar for numbers in Turtle: https://www.w3.org/TR/turtle/#abbrev

The semantics of both forms are the same, so does it really matter?

On Wed, Jun 12, 2019 at 2:07 PM Miika Alonen <mi...@gofore.com> wrote:
>
> Hi,
>
> I have existing system that uses plain numeric literals in graphs like:
>
> ex:prop sh:order 2 .
>
> I'v been trying to add new literals using jena, but don't know how to create plain numeric literals. I tried this and it is working for Booleans but not Integers:
>
> propShape.addLiteral(SH.order, 4)
> propShape.addLiteral(SH.closed, true)
>
> Result:
> ex:prop sh:order    "4"^^xsd:long .
> ex:prop sh:closed true .
>
> Expected result:
> ex:prop sh:order 4 .
> ex:prop sh:closed true .
>
> Is there a way to create these kind of numerical plain literals in Jena?
>
> Br, Miika

Re: Creating plain numeric literals using jena

Posted by ajs6f <aj...@apache.org>.
In RDF 1.1, all literals are typed, and the absence of an explicit type means that the type is xsd:string. So you could try forcing this by pushing your data in with that datatype (xsd:string) and I suspect the serializers will indeed give you the "plain" form you desire. But you will have changed the semantics of your data.

You may wish to consider a further processing step.

ajs6f

> On Jun 12, 2019, at 8:21 AM, Martynas Jusevičius <ma...@atomgraph.com> wrote:
> 
> AFAIK there are no "plain numeric literals". That's just syntactic
> sugar for numbers in Turtle: https://www.w3.org/TR/turtle/#abbrev
> 
> The semantics of both forms are the same, so does it really matter?
> 
> On Wed, Jun 12, 2019 at 2:07 PM Miika Alonen <mi...@gofore.com> wrote:
>> 
>> Hi,
>> 
>> I have existing system that uses plain numeric literals in graphs like:
>> 
>> ex:prop sh:order 2 .
>> 
>> I'v been trying to add new literals using jena, but don't know how to create plain numeric literals. I tried this and it is working for Booleans but not Integers:
>> 
>> propShape.addLiteral(SH.order, 4)
>> propShape.addLiteral(SH.closed, true)
>> 
>> Result:
>> ex:prop sh:order    "4"^^xsd:long .
>> ex:prop sh:closed true .
>> 
>> Expected result:
>> ex:prop sh:order 4 .
>> ex:prop sh:closed true .
>> 
>> Is there a way to create these kind of numerical plain literals in Jena?
>> 
>> Br, Miika


Re: Creating plain numeric literals using jena

Posted by Martynas Jusevičius <ma...@atomgraph.com>.
AFAIK there are no "plain numeric literals". That's just syntactic
sugar for numbers in Turtle: https://www.w3.org/TR/turtle/#abbrev

The semantics of both forms are the same, so does it really matter?

On Wed, Jun 12, 2019 at 2:07 PM Miika Alonen <mi...@gofore.com> wrote:
>
> Hi,
>
> I have existing system that uses plain numeric literals in graphs like:
>
> ex:prop sh:order 2 .
>
> I'v been trying to add new literals using jena, but don't know how to create plain numeric literals. I tried this and it is working for Booleans but not Integers:
>
> propShape.addLiteral(SH.order, 4)
> propShape.addLiteral(SH.closed, true)
>
> Result:
> ex:prop sh:order    "4"^^xsd:long .
> ex:prop sh:closed true .
>
> Expected result:
> ex:prop sh:order 4 .
> ex:prop sh:closed true .
>
> Is there a way to create these kind of numerical plain literals in Jena?
>
> Br, Miika