On Tue, Aug 20, 2013 at 11:01 AM, Darius Miliauskas
<[email protected]> wrote:
> Subject: The Better Looking Output After the SPARQL Query in Java Code
> To: [email protected]
>
>
> 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/

Reply via email to