You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Marco Tenti <te...@gmail.com> on 2015/03/02 15:53:44 UTC

Print a model without the option ^^

Hi, everyone,  my question today is a little strange but i need it for a
integration with a exisisting project, there is a way to write  a model
without the option of the literal object? In the specific only for the
string object.
I solved for now with other java code (Scanner,read,replace,ecc.) but I wanted
to know if jena allows me to do it directly in some way, for example:

I have this:

<Subject> <Predicate> "myLiteralString"^^<
http://www.w3.org/2001/XMLSchema#string>
.......

<Subject> <Predicate> "myLiteralString"^^<
http://www.w3.org/2001/XMLSchema#long>
<Subject> <Predicate> "myLiteralString"^^<
http://www.w3.org/2001/XMLSchema#decimal>
<Subject> <Predicate> "myLiteralString"^^<
http://www.w3.org/2001/XMLSchema#string>

but i want to print this:
<Subject> <Predicate> "myLiteralString"
.......
<Subject> <Predicate> "myLiteralString"^^<
http://www.w3.org/2001/XMLSchema#long>
<Subject> <Predicate> "myLiteralString"^^<
http://www.w3.org/2001/XMLSchema#decimal>
<Subject> <Predicate> "myLiteralString"

Ty, in advance. greetings.

Re: Print a model without the option ^^

Posted by Stian Soiland-Reyes <st...@apache.org>.
What if you try something like this?

https://gist.github.com/stain/be58129e5ac382668f53


    public static class SimpleLiteralStreamRDF extends StreamRDFProxy {
        public SimpleLiteralStreamRDF(StreamRDF writer) {
            super(writer);
        }

        @Override
        public void triple(Triple triple) {
            Node object = triple.getObject();
            if (object.isLiteral()) {
                Literal literal =
ResourceFactory.createPlainLiteral(object.getLiteralLexicalForm());
                triple = new Triple(triple.getSubject(),
triple.getPredicate(), literal.asNode());
            }
            super.triple(triple);
        }
    }


        Model model = ModelFactory.createDefaultModel();
        model.add(ResourceFactory.createResource(),
                ResourceFactory.createProperty("http://example.com/hasDouble"),
                ResourceFactory.createTypedLiteral("13.37",
XSDDatatype.XSDdouble));
        model.add(ResourceFactory.createResource(),
                ResourceFactory.createProperty("http://example.com/hasString"),
                ResourceFactory.createTypedLiteral("uhu",
XSDDatatype.XSDstring));
        model.add(ResourceFactory.createResource(),
                ResourceFactory.createProperty("http://example.com/hasLang"),
                ResourceFactory.createLangLiteral("Hello", "en-GB"));


        final StreamRDF writer =
StreamRDFWriter.getWriterStream(System.out, Lang.TURTLE);
        StreamRDF literalWriter = new SimpleLiteralStreamRDF(writer);
        StreamOps.graphToStream(model.getGraph(), literalWriter);



The above outputs:

_:b0    <http://example.com/hasLang>  "Hello" .
_:b1    <http://example.com/hasDouble>  "13.37" .
_:b2    <http://example.com/hasString>  "uhu" .




Note that I could not quickly find the equivalent of my StreamRDFProxy
(see the gist) - PipedTriplesStream is not quite that as it needs a
PipedRDFIterator instead of another StreamRDF.

On 2 March 2015 at 14:53, Marco Tenti <te...@gmail.com> wrote:
> Hi, everyone,  my question today is a little strange but i need it for a
> integration with a exisisting project, there is a way to write  a model
> without the option of the literal object? In the specific only for the
> string object.
> I solved for now with other java code (Scanner,read,replace,ecc.) but I wanted
> to know if jena allows me to do it directly in some way, for example:
>
> I have this:
>
> <Subject> <Predicate> "myLiteralString"^^<
> http://www.w3.org/2001/XMLSchema#string>
> .......
>
> <Subject> <Predicate> "myLiteralString"^^<
> http://www.w3.org/2001/XMLSchema#long>
> <Subject> <Predicate> "myLiteralString"^^<
> http://www.w3.org/2001/XMLSchema#decimal>
> <Subject> <Predicate> "myLiteralString"^^<
> http://www.w3.org/2001/XMLSchema#string>
>
> but i want to print this:
> <Subject> <Predicate> "myLiteralString"
> .......
> <Subject> <Predicate> "myLiteralString"^^<
> http://www.w3.org/2001/XMLSchema#long>
> <Subject> <Predicate> "myLiteralString"^^<
> http://www.w3.org/2001/XMLSchema#decimal>
> <Subject> <Predicate> "myLiteralString"
>
> Ty, in advance. greetings.



-- 
Stian Soiland-Reyes
Apache Taverna (incubating)
http://orcid.org/0000-0001-9842-9718

Re: Print a model without the option ^^

Posted by Marco Tenti <te...@gmail.com>.
K, I solved ty all for the help, you point me to the right direction for
anyone have the same trouble i put the code i used:

model = ......;
model2 = ......;
 com.hp.hpl.jena.rdf.model.StmtIterator stats = model.listStatements();
                while (stats.hasNext()) {
                    com.hp.hpl.jena.rdf.model.Statement stmt = stats.next();
                    com.hp.hpl.jena.rdf.model.RDFNode x = stmt.getObject();
                    if (x.isLiteral()) {
                        if(

x.asLiteral().getDatatype().equals(XSDDatatype.XSDstring)){
                            model2.add(stmt.getSubject(),
                                       stmt.getPredicate(),

ResourceFactory.createPlainLiteral(x.asLiteral().getLexicalForm()

));
                        }else{
                            model2.add(
                                    stmt.getSubject(),
                                    stmt.getPredicate(),
                                    ResourceFactory.createTypedLiteral(

stmt.getObject().asLiteral().toString(),

stmt.getObject().asLiteral().getDatatype()
                                    )
                            );
                        }

                    } else if (x.isURIResource()) {
                        model2.add(stmt);

                    }
                }
model.close();
//write model2

2015-03-02 17:36 GMT+01:00 Andy Seaborne <an...@apache.org>:

> On 02/03/15 14:53, Marco Tenti wrote:
>
>> Hi, everyone,  my question today is a little strange but i need it for a
>> integration with a exisisting project, there is a way to write  a model
>> without the option of the literal object? In the specific only for the
>> string object.
>> I solved for now with other java code (Scanner,read,replace,ecc.) but I
>> wanted
>> to know if jena allows me to do it directly in some way, for example:
>>
>> I have this:
>>
>> <Subject> <Predicate> "myLiteralString"^^<
>> http://www.w3.org/2001/XMLSchema#string>
>> .......
>>
>> <Subject> <Predicate> "myLiteralString"^^<
>> http://www.w3.org/2001/XMLSchema#long>
>> <Subject> <Predicate> "myLiteralString"^^<
>> http://www.w3.org/2001/XMLSchema#decimal>
>> <Subject> <Predicate> "myLiteralString"^^<
>> http://www.w3.org/2001/XMLSchema#string>
>>
>> but i want to print this:
>> <Subject> <Predicate> "myLiteralString"
>> .......
>> <Subject> <Predicate> "myLiteralString"^^<
>> http://www.w3.org/2001/XMLSchema#long>
>> <Subject> <Predicate> "myLiteralString"^^<
>> http://www.w3.org/2001/XMLSchema#decimal>
>> <Subject> <Predicate> "myLiteralString"
>>
>> Ty, in advance. greetings.
>>
>>
> i.e. write xsd:string as a simple literal.
>
> That's what you will get in RDF 1.1.  All simple literals are xsd:string
> and output does not print the ^^xsd:string.
>
> In RDF 1.0 (RDF 2004, RDF current), they are different RDF terms.
>
> The next release of Jena will have all the machinery for RDF 1.1 but it's
> switched off by default.  The plan is to turn it on at Jena3 because it
> impacts persistent data (TDB, SDB).
>
> You can try that, at your own risk, by taking a development build (or the
> next release 2.13.0) and setting
>
>   JenaRuntime.isRDF11 = true ;
>
> Do not run on a database if your data has any xsd:strings in it as yours
> clearly has.
>
> Also, if anyone is building directly from git, the tests will not all pass
> ... the first test is whether the system is in RDF 1.0 mode, not RDF 1.1,
> to make sure a release does not slip out with the wrong setting.  There are
> some ARQ tests that change - and a conversion script in the source tree.
>
>         Andy
>
>
>
>

Re: Print a model without the option ^^

Posted by Andy Seaborne <an...@apache.org>.
On 02/03/15 14:53, Marco Tenti wrote:
> Hi, everyone,  my question today is a little strange but i need it for a
> integration with a exisisting project, there is a way to write  a model
> without the option of the literal object? In the specific only for the
> string object.
> I solved for now with other java code (Scanner,read,replace,ecc.) but I wanted
> to know if jena allows me to do it directly in some way, for example:
>
> I have this:
>
> <Subject> <Predicate> "myLiteralString"^^<
> http://www.w3.org/2001/XMLSchema#string>
> .......
>
> <Subject> <Predicate> "myLiteralString"^^<
> http://www.w3.org/2001/XMLSchema#long>
> <Subject> <Predicate> "myLiteralString"^^<
> http://www.w3.org/2001/XMLSchema#decimal>
> <Subject> <Predicate> "myLiteralString"^^<
> http://www.w3.org/2001/XMLSchema#string>
>
> but i want to print this:
> <Subject> <Predicate> "myLiteralString"
> .......
> <Subject> <Predicate> "myLiteralString"^^<
> http://www.w3.org/2001/XMLSchema#long>
> <Subject> <Predicate> "myLiteralString"^^<
> http://www.w3.org/2001/XMLSchema#decimal>
> <Subject> <Predicate> "myLiteralString"
>
> Ty, in advance. greetings.
>

i.e. write xsd:string as a simple literal.

That's what you will get in RDF 1.1.  All simple literals are xsd:string 
and output does not print the ^^xsd:string.

In RDF 1.0 (RDF 2004, RDF current), they are different RDF terms.

The next release of Jena will have all the machinery for RDF 1.1 but 
it's switched off by default.  The plan is to turn it on at Jena3 
because it impacts persistent data (TDB, SDB).

You can try that, at your own risk, by taking a development build (or 
the next release 2.13.0) and setting

   JenaRuntime.isRDF11 = true ;

Do not run on a database if your data has any xsd:strings in it as yours 
clearly has.

Also, if anyone is building directly from git, the tests will not all 
pass ... the first test is whether the system is in RDF 1.0 mode, not 
RDF 1.1, to make sure a release does not slip out with the wrong 
setting.  There are some ARQ tests that change - and a conversion script 
in the source tree.

	Andy