You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Darius Miliauskas <da...@gmail.com> on 2013/08/20 17:01:43 UTC

Fwd: The Better Looking Output After the SPARQL Query in Java Code

Subject: The Better Looking Output After the SPARQL Query in Java Code
To: users@jena.apache.org


Dear Sir or Madam,

I am using the SPARQL query in Java (actually, NetBeans). The query works
fine, however, the results are outputted not in the nice form.

The example of my code (Java) is the following:

String queryString2 ="PREFIX base: <
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#>"
                 + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#
>"
                + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
                + "PREFIX owl: <http://www.w3.org/2002/07/owl#>"
                + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>"
                + "SELECT ?person ?room WHERE { ?person a base:Person.
?room a base:Room."
                + "FILTER regex(STR(?person), \"Ana\") }"
                + "ORDER BY DESC (?person)";
Query query = QueryFactory.create(queryString2);
        QueryExecution qe = QueryExecutionFactory.create(query, model);
        com.hp.hpl.jena.query.ResultSet results = qe.execSelect();
        ResultSetFormatter.out(System.out, results, query);
        ResultSetFormatter.asText(results, query);
        qe.close();

I get the results in the following form:
----------------------------------------------------------------------------------------------------
| person   | room
                       |
====================================================================================================
| base:Ana | <
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Rue_de_Mont_Blanc,_Geneva>
|
----------------------------------------------------------------------------------------------------

Is there any possibility in SPARQL to cut or substring these additional
parts ("Ana instead of "base: Ana" or "Rue_de_Mont_Blanc,_Geneva" instead
of the "<
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Rue_de_Mont_Blanc,_Geneva>")?
I tried FILTER (SUBSTR(?person, 4)) in the query, but it did not give me
the desired results. Or it is recommended to do in Java? How to do it?


Thanks for any suggestions,

Darius Miliauskas

Re: Fwd: The Better Looking Output After the SPARQL Query in Java Code

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Wed, Aug 21, 2013 at 5:55 AM, Darius Miliauskas
<da...@gmail.com> wrote:
> FILTER controls which solutions are included in the final result as you
> said but why does not it work with the logical AND (&&), only with logical
> OR (||)?

None of the examples in this thread have mentioned boolean operators
in filter expressions.  Can you clarify what you mean here?  As far as
I know, both logical operators are permissible in filter expressions.
Do you have an example where something isn't working?

//JT

-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: Fwd: The Better Looking Output After the SPARQL Query in Java Code

Posted by Darius Miliauskas <da...@gmail.com>.
Dear All,

thanks everybody for the replies.

Thanks, Chris, it is a good piece of advice!

Thanks, Joshua, again,
yes, that's I wanted to do. I have read the specifications but they are not
so easy understandable because when I try something it does not work as it
is supposed to work that's the reason I ask. Perhaps it is helpful for
others newbies as well. It looks that it is sometimes very sensitive to
small details, and the errors are not identified so easily.
Even your example "bind( substr(str(?person),4) as ?pName)" did not work
for me at the beginning but, when I modified as "bind(
substr(str(?person),4) as ?person)" it started to work. You see how, the
hell, sensitive it is.
"bind( strafter(str(?person),"
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#")
as ?pName)" I changed as well as "bind( strafter(str(?person),"
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#")
as ?person)".
Moreover, it looks that the tags of functions are not case sensitive while
the variable names are like allways.
FILTER controls which solutions are included in the final result as you
said but why does not it work with the logical AND (&&), only with logical
OR (||)?


Thanks, Dave, that's an interesting example of syntax. It helped, and I
will use it in other queries.


Sincerely Yours,

Darius




2013/8/20 Chris Dollin <ch...@epimorphics.com>

> On Tuesday, August 20, 2013 06:01:43 PM Darius Miliauskas wrote:
>
> > The example of my code (Java) is the following:
> >
> > String queryString2 ="PREFIX base: <
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#>"
> >                  + "PREFIX rdf: <
> http://www.w3.org/1999/02/22-rdf-syntax-ns#
> > >"
>
> Aside: you'll find it helpful to start each of these strings with \n.
>
> This means you can print the query out without it rushing off to the right,
> and if a syntax error is reported the line number will be useful rather
> than 1.
>
> Chris
>
> --
> "You're down as expendable. You don't get a weapon."    /Dark Lord of
> Derkholm/
>
> Epimorphics Ltd, http://www.epimorphics.com
> Registered address: Court Lodge, 105 High Street, Portishead, Bristol BS20
> 6PT
> Epimorphics Ltd. is a limited company registered in England (number
> 7016688)
>
>

Re: Fwd: The Better Looking Output After the SPARQL Query in Java Code

Posted by Chris Dollin <ch...@epimorphics.com>.
On Tuesday, August 20, 2013 06:01:43 PM Darius Miliauskas wrote:

> The example of my code (Java) is the following:
> 
> String queryString2 ="PREFIX base: <
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#>"
>                  + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#
> >"

Aside: you'll find it helpful to start each of these strings with \n.

This means you can print the query out without it rushing off to the right,
and if a syntax error is reported the line number will be useful rather 
than 1.

Chris

-- 
"You're down as expendable. You don't get a weapon."    /Dark Lord of Derkholm/

Epimorphics Ltd, http://www.epimorphics.com
Registered address: Court Lodge, 105 High Street, Portishead, Bristol BS20 6PT
Epimorphics Ltd. is a limited company registered in England (number 7016688)


Re: The Better Looking Output After the SPARQL Query in Java Code

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Tue, Aug 20, 2013 at 11:01 AM, Darius Miliauskas
<da...@gmail.com> wrote:
> Subject: The Better Looking Output After the SPARQL Query in Java Code
> To: users@jena.apache.org
>
>
> Dear Sir or Madam,
>
> I am using the SPARQL query in Java (actually, NetBeans). The query works
> fine, however, the results are outputted not in the nice form.
>
> The example of my code (Java) is the following:
>
> String queryString2 ="PREFIX base: <
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#>"
>                  + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#
>>"
>                 + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
>                 + "PREFIX owl: <http://www.w3.org/2002/07/owl#>"
>                 + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>"
>                 + "SELECT ?person ?room WHERE { ?person a base:Person.
> ?room a base:Room."
>                 + "FILTER regex(STR(?person), \"Ana\") }"
>                 + "ORDER BY DESC (?person)";
> Query query = QueryFactory.create(queryString2);
>         QueryExecution qe = QueryExecutionFactory.create(query, model);
>         com.hp.hpl.jena.query.ResultSet results = qe.execSelect();
>         ResultSetFormatter.out(System.out, results, query);
>         ResultSetFormatter.asText(results, query);
>         qe.close();
>
> I get the results in the following form:
> ----------------------------------------------------------------------------------------------------
> | person   | room
>                        |
> ====================================================================================================
> | base:Ana | <
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Rue_de_Mont_Blanc,_Geneva>
> |
> ----------------------------------------------------------------------------------------------------
>
> Is there any possibility in SPARQL to cut or substring these additional
> parts ("Ana instead of "base: Ana" or "Rue_de_Mont_Blanc,_Geneva" instead
> of the "<
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Rue_de_Mont_Blanc,_Geneva>")?
> I tried FILTER (SUBSTR(?person, 4)) in the query, but it did not give me
> the desired results. Or it is recommended to do in Java? How to do it?

What did you expect FILTER( SUBSTR( ?person, 4 )) to do?  FILTER is
used to control which of the solutions are included in the final
result.  It sounds more like you wanted to do something like

    BIND( substr(str(?person),4) as ?personName )

and then add ?personName to your list of projected variables.
Adjusting the example I gave you earlier, you'd get


prefix rooms: <http://www.semanticweb.org/darius/ontologies/2013/6/rooms#>
prefix owl: <http://www.w3.org/2002/07/owl#>

select ?pName where {

  values (?person ?city) {
    (rooms:Ivan rooms:London)
  }

  ?person a rooms:Person ;
          ?personProperty ?gender .
  ?gender a rooms:Gender .
  ?city a rooms:City ;
        ?cityProperty ?gender .

  bind( substr(str(?person),4) as ?pName)
}


which would produce

$ arq --data data.rdf  --query query.sparql
-----------------------------------------------------------------
| pName                                                         |
=================================================================
| "p://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ivan" |
-----------------------------------------------------------------


As you can see, starting at 4 only trimmed off "htt" from the string.
What you actually want to do is


  bind( strafter(str(?person),"http://www.semanticweb.org/darius/ontologies/2013/6/rooms#")
as ?pName)


which will give you results like:


$ arq --data data.rdf  --query query.sparql
----------
| pName  |
==========
| "Ivan" |
----------


None of these functions are specific to Jena;  they are all standard
SPARQL 1.1 functions, clearly defined in the specification.  E.g.,
strafter is defined in section 17.4.3.10 [1].  I suggest that you at
least skim the specification so that you have an idea of what's in
there, and so that you can consult it when you have a specific problem
like this one.

//JT

[1] http://www.w3.org/TR/sparql11-query/#func-strafter






-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Re: The Better Looking Output After the SPARQL Query in Java Code

Posted by Dave Reynolds <da...@gmail.com>.
On 20/08/13 16:12, Darius Miliauskas wrote:
> Dear All,
>
> I already solved the issue using the manipulations with Java code. However,
> the question how to solve it in a SPARQL query is still open.

You can do things like:

BIND(REPLACE(str(?person),'.*[/#]([^/#]+)','$1') as ?label)

then display the ?label.

Dave

>
> My solution with Java code is:
>
>         String result:
>          while (results.hasNext()) {
>              QuerySolution solution = results.next();
>              result = solution.get("?room").toString();
>              result = result.substring(result.indexOf("#") + 1,
> result.length());
>              System.out.println(result);
>          }
>          qe.close();
>
>
> Best,
>
> Darius
>
>
> 2013/8/20 Darius Miliauskas <da...@gmail.com>
>
>> Subject: The Better Looking Output After the SPARQL Query in Java Code
>> To: users@jena.apache.org
>>
>>
>> Dear Sir or Madam,
>>
>> I am using the SPARQL query in Java (actually, NetBeans). The query works
>> fine, however, the results are outputted not in the nice form.
>>
>> The example of my code (Java) is the following:
>>
>> String queryString2 ="PREFIX base: <
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#>"
>>                   + "PREFIX rdf: <
>> http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
>>                  + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
>>                  + "PREFIX owl: <http://www.w3.org/2002/07/owl#>"
>>                  + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>"
>>                  + "SELECT ?person ?room WHERE { ?person a base:Person.
>> ?room a base:Room."
>>                  + "FILTER regex(STR(?person), \"Ana\") }"
>>                  + "ORDER BY DESC (?person)";
>> Query query = QueryFactory.create(queryString2);
>>          QueryExecution qe = QueryExecutionFactory.create(query, model);
>>          com.hp.hpl.jena.query.ResultSet results = qe.execSelect();
>>          ResultSetFormatter.out(System.out, results, query);
>>          ResultSetFormatter.asText(results, query);
>>          qe.close();
>>
>> I get the results in the following form:
>>
>> ----------------------------------------------------------------------------------------------------
>> | person   | room
>>                           |
>>
>> ====================================================================================================
>> | base:Ana | <
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Rue_de_Mont_Blanc,_Geneva>
>> |
>>
>> ----------------------------------------------------------------------------------------------------
>>
>> Is there any possibility in SPARQL to cut or substring these additional
>> parts ("Ana instead of "base: Ana" or "Rue_de_Mont_Blanc,_Geneva" instead
>> of the "<
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Rue_de_Mont_Blanc,_Geneva>")?
>> I tried FILTER (SUBSTR(?person, 4)) in the query, but it did not give me
>> the desired results. Or it is recommended to do in Java? How to do it?
>>
>>
>> Thanks for any suggestions,
>>
>> Darius Miliauskas
>>
>>
>>
>>
>>
>>
>


Re: The Better Looking Output After the SPARQL Query in Java Code

Posted by Darius Miliauskas <da...@gmail.com>.
Dear All,

I already solved the issue using the manipulations with Java code. However,
the question how to solve it in a SPARQL query is still open.

My solution with Java code is:

       String result:
        while (results.hasNext()) {
            QuerySolution solution = results.next();
            result = solution.get("?room").toString();
            result = result.substring(result.indexOf("#") + 1,
result.length());
            System.out.println(result);
        }
        qe.close();


Best,

Darius


2013/8/20 Darius Miliauskas <da...@gmail.com>

> Subject: The Better Looking Output After the SPARQL Query in Java Code
> To: users@jena.apache.org
>
>
> Dear Sir or Madam,
>
> I am using the SPARQL query in Java (actually, NetBeans). The query works
> fine, however, the results are outputted not in the nice form.
>
> The example of my code (Java) is the following:
>
> String queryString2 ="PREFIX base: <
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#>"
>                  + "PREFIX rdf: <
> http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
>                 + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
>                 + "PREFIX owl: <http://www.w3.org/2002/07/owl#>"
>                 + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>"
>                 + "SELECT ?person ?room WHERE { ?person a base:Person.
> ?room a base:Room."
>                 + "FILTER regex(STR(?person), \"Ana\") }"
>                 + "ORDER BY DESC (?person)";
> Query query = QueryFactory.create(queryString2);
>         QueryExecution qe = QueryExecutionFactory.create(query, model);
>         com.hp.hpl.jena.query.ResultSet results = qe.execSelect();
>         ResultSetFormatter.out(System.out, results, query);
>         ResultSetFormatter.asText(results, query);
>         qe.close();
>
> I get the results in the following form:
>
> ----------------------------------------------------------------------------------------------------
> | person   | room
>                          |
>
> ====================================================================================================
> | base:Ana | <
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Rue_de_Mont_Blanc,_Geneva>
> |
>
> ----------------------------------------------------------------------------------------------------
>
> Is there any possibility in SPARQL to cut or substring these additional
> parts ("Ana instead of "base: Ana" or "Rue_de_Mont_Blanc,_Geneva" instead
> of the "<
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Rue_de_Mont_Blanc,_Geneva>")?
> I tried FILTER (SUBSTR(?person, 4)) in the query, but it did not give me
> the desired results. Or it is recommended to do in Java? How to do it?
>
>
> Thanks for any suggestions,
>
> Darius Miliauskas
>
>
>
>
>
>