You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by "Samita Bai / PhD CS Scholar @ City Campus" <sb...@iba.edu.pk> on 2018/06/07 07:35:46 UTC

Combined query results

I need some help with the following code. I am using parameterized sparql string and I want to display combined results of both queries how can I do that. Any help or suggestion would be truly appreciated.



final static String filename = "/home/samita/turtleContent.ttl";

     static Model model= null;


    final static String turtleContent = "" +
            "@prefix : <http://example.org/> .\n" +
            "\n" +
            ":alice :hasName \"Alice\" .\n" +
            ":alice :hasAddress \"4222 Clinton Way\" .\n" +
            ":herman :hasName \"Herman\".\n" +
            ":herman :hasAddress \"1313 Mockingbird Lane\" .\n" +
            ":DrWho :hasAddress \"The TARDIS\"" +
            "";


   // Read the model from the turtle content
   final static Model model = ModelFactory.createDefaultModel()
           // .read( new ByteArrayInputStream( turtleContent.getBytes()), null, "TURTLE" );


    final static String findAlice = "prefix : <http://example.org/>" +
            "select ?alice where {" +
            "?alice :hasName \"Alice\" }"  ;

    final static String findAliceAddress = "prefix : <http://example.org/>" +
            "select ?address where {" +
            " ?alice :hasAddress ?address }";

 public static void useParameterizedSPARQLString() {
        System.out.println( "== useParameterizedSPARQLString ==" );
        // execute the query that finds a (single) binding for ?alice.  Then create
        // a query solution map containing those results.
        final ResultSet aliceResults = QueryExecutionFactory.create( findAlice, model ).execSelect();
        final QuerySolutionMap map = new QuerySolutionMap();
        map.addAll( aliceResults.next() );
        // Create a ParameterizedSparqlString from the findAliceAddress query string (if this
        // approach were taken, findAliceAddress could actually *be* a Param.SparqlString, of
        // course).
        final ParameterizedSparqlString pss = new ParameterizedSparqlString( findAliceAddress );
        System.out.println( pss.toString() );
        pss.setParams( map );
        System.out.println( pss.toString() );
        // execute the query and show the results
        ResultSetFormatter.out( QueryExecutionFactory.create( pss.toString(), model ).execSelect() );
    }

The results I get is:

----------------------
| address            |
======================
| "4222 Clinton Way" |
----------------------

Is there any way to display the variable ?alice also

Like

alice                                       address

http://example.org/alice            "4222 Clinton Way"<http://example.org/alice>





P : Please consider the environment before printing this e-mail

________________________________

CONFIDENTIALITY / DISCLAIMER NOTICE: This e-mail and any attachments may contain confidential and privileged information. If you are not the intended recipient, please notify the sender immediately by return e-mail, delete this e-mail and destroy any copies. Any dissemination or use of this information by a person other than the intended recipient is unauthorized and may be illegal.

________________________________

Re: Combined query results

Posted by Claude Warren <cl...@xenei.com>.
I can think of two ways to do this.  One is to use the bind statement to
bind the desired value.

prefix : <http://example.org/>

Select ?alice ?address where {
       ?alice :hasAddress ?address.
         bind(  "Alice" as ?alice )
}

The other is to use federated queries to do the join in one call.

On 08:08, Sat 9 Jun 2018 Samita Bai / PhD CS Scholar @ City Campus <
sbai@iba.edu.pk> wrote:
>
> Dear Claude,
>
>
> I am working on two systems to answer the query so I need to split a
query into two parts, one part will be sent to one system and its results
would be sent to another system as a parametrized query.
>
>
>
> Regards,
>
> Samita Bai
>
> ________________________________
> From: Claude Warren <cl...@xenei.com>
> Sent: 08 June 2018 22:26:55
> To: users@jena.apache.org
> Subject: Re: Combined query results
>
> How about:
>
> prefix : <http://example.org/>
>
> Select ?alice ?address where {
>        ?alice :hasAddress ?address;
>                   :hasName "Alice"
> }
>
>
>
>
> On Thu, Jun 7, 2018, 12:36 AM Samita Bai / PhD CS Scholar @ City Campus <
> sbai@iba.edu.pk> wrote:
>
> > I need some help with the following code. I am using parameterized
sparql
> > string and I want to display combined results of both queries how can I
do
> > that. Any help or suggestion would be truly appreciated.
> >
> >
> >
> > final static String filename = "/home/samita/turtleContent.ttl";
> >
> >      static Model model= null;
> >
> >
> >     final static String turtleContent = "" +
> >             "@prefix : <http://example.org/> .\n" +
> >             "\n" +
> >             ":alice :hasName \"Alice\" .\n" +
> >             ":alice :hasAddress \"4222 Clinton Way\" .\n" +
> >             ":herman :hasName \"Herman\".\n" +
> >             ":herman :hasAddress \"1313 Mockingbird Lane\" .\n" +
> >             ":DrWho :hasAddress \"The TARDIS\"" +
> >             "";
> >
> >
> >    // Read the model from the turtle content
> >    final static Model model = ModelFactory.createDefaultModel()
> >            // .read( new ByteArrayInputStream(
turtleContent.getBytes()),
> > null, "TURTLE" );
> >
> >
> >     final static String findAlice = "prefix : <http://example.org/>" +
> >             "select ?alice where {" +
> >             "?alice :hasName \"Alice\" }"  ;
> >
> >     final static String findAliceAddress = "prefix : <
http://example.org/>"
> > +
> >             "select ?address where {" +
> >             " ?alice :hasAddress ?address }";
> >
> >  public static void useParameterizedSPARQLString() {
> >         System.out.println( "== useParameterizedSPARQLString ==" );
> >         // execute the query that finds a (single) binding for ?alice.
> > Then create
> >         // a query solution map containing those results.
> >         final ResultSet aliceResults = QueryExecutionFactory.create(
> > findAlice, model ).execSelect();
> >         final QuerySolutionMap map = new QuerySolutionMap();
> >         map.addAll( aliceResults.next() );
> >         // Create a ParameterizedSparqlString from the findAliceAddress
> > query string (if this
> >         // approach were taken, findAliceAddress could actually *be* a
> > Param.SparqlString, of
> >         // course).
> >         final ParameterizedSparqlString pss = new
> > ParameterizedSparqlString( findAliceAddress );
> >         System.out.println( pss.toString() );
> >         pss.setParams( map );
> >         System.out.println( pss.toString() );
> >         // execute the query and show the results
> >         ResultSetFormatter.out( QueryExecutionFactory.create(
> > pss.toString(), model ).execSelect() );
> >     }
> >
> > The results I get is:
> >
> > ----------------------
> > | address            |
> > ======================
> > | "4222 Clinton Way" |
> > ----------------------
> >
> > Is there any way to display the variable ?alice also
> >
> > Like
> >
> > alice                                       address
> >
> > http://example.org/alice            "4222 Clinton Way"<
> > http://example.org/alice>
> >
> >
> >
> >
> >
> > P : Please consider the environment before printing this e-mail
> >
> > ________________________________
> >
> > CONFIDENTIALITY / DISCLAIMER NOTICE: This e-mail and any attachments may
> > contain confidential and privileged information. If you are not the
> > intended recipient, please notify the sender immediately by return
e-mail,
> > delete this e-mail and destroy any copies. Any dissemination or use of
this
> > information by a person other than the intended recipient is
unauthorized
> > and may be illegal.
> >
> > ________________________________
> >
>
> P : Please consider the environment before printing this e-mail
>
> ________________________________
>
> CONFIDENTIALITY / DISCLAIMER NOTICE: This e-mail and any attachments may
contain confidential and privileged information. If you are not the
intended recipient, please notify the sender immediately by return e-mail,
delete this e-mail and destroy any copies. Any dissemination or use of this
information by a person other than the intended recipient is unauthorized
and may be illegal.
>
> ________________________________

Re: Combined query results

Posted by "Samita Bai / PhD CS Scholar @ City Campus" <sb...@iba.edu.pk>.
Dear Claude,


I am working on two systems to answer the query so I need to split a query into two parts, one part will be sent to one system and its results would be sent to another system as a parametrized query.



Regards,

Samita Bai

________________________________
From: Claude Warren <cl...@xenei.com>
Sent: 08 June 2018 22:26:55
To: users@jena.apache.org
Subject: Re: Combined query results

How about:

prefix : <http://example.org/>

Select ?alice ?address where {
       ?alice :hasAddress ?address;
                  :hasName "Alice"
}




On Thu, Jun 7, 2018, 12:36 AM Samita Bai / PhD CS Scholar @ City Campus <
sbai@iba.edu.pk> wrote:

> I need some help with the following code. I am using parameterized sparql
> string and I want to display combined results of both queries how can I do
> that. Any help or suggestion would be truly appreciated.
>
>
>
> final static String filename = "/home/samita/turtleContent.ttl";
>
>      static Model model= null;
>
>
>     final static String turtleContent = "" +
>             "@prefix : <http://example.org/> .\n" +
>             "\n" +
>             ":alice :hasName \"Alice\" .\n" +
>             ":alice :hasAddress \"4222 Clinton Way\" .\n" +
>             ":herman :hasName \"Herman\".\n" +
>             ":herman :hasAddress \"1313 Mockingbird Lane\" .\n" +
>             ":DrWho :hasAddress \"The TARDIS\"" +
>             "";
>
>
>    // Read the model from the turtle content
>    final static Model model = ModelFactory.createDefaultModel()
>            // .read( new ByteArrayInputStream( turtleContent.getBytes()),
> null, "TURTLE" );
>
>
>     final static String findAlice = "prefix : <http://example.org/>" +
>             "select ?alice where {" +
>             "?alice :hasName \"Alice\" }"  ;
>
>     final static String findAliceAddress = "prefix : <http://example.org/>"
> +
>             "select ?address where {" +
>             " ?alice :hasAddress ?address }";
>
>  public static void useParameterizedSPARQLString() {
>         System.out.println( "== useParameterizedSPARQLString ==" );
>         // execute the query that finds a (single) binding for ?alice.
> Then create
>         // a query solution map containing those results.
>         final ResultSet aliceResults = QueryExecutionFactory.create(
> findAlice, model ).execSelect();
>         final QuerySolutionMap map = new QuerySolutionMap();
>         map.addAll( aliceResults.next() );
>         // Create a ParameterizedSparqlString from the findAliceAddress
> query string (if this
>         // approach were taken, findAliceAddress could actually *be* a
> Param.SparqlString, of
>         // course).
>         final ParameterizedSparqlString pss = new
> ParameterizedSparqlString( findAliceAddress );
>         System.out.println( pss.toString() );
>         pss.setParams( map );
>         System.out.println( pss.toString() );
>         // execute the query and show the results
>         ResultSetFormatter.out( QueryExecutionFactory.create(
> pss.toString(), model ).execSelect() );
>     }
>
> The results I get is:
>
> ----------------------
> | address            |
> ======================
> | "4222 Clinton Way" |
> ----------------------
>
> Is there any way to display the variable ?alice also
>
> Like
>
> alice                                       address
>
> http://example.org/alice            "4222 Clinton Way"<
> http://example.org/alice>
>
>
>
>
>
> P : Please consider the environment before printing this e-mail
>
> ________________________________
>
> CONFIDENTIALITY / DISCLAIMER NOTICE: This e-mail and any attachments may
> contain confidential and privileged information. If you are not the
> intended recipient, please notify the sender immediately by return e-mail,
> delete this e-mail and destroy any copies. Any dissemination or use of this
> information by a person other than the intended recipient is unauthorized
> and may be illegal.
>
> ________________________________
>

P : Please consider the environment before printing this e-mail

________________________________

CONFIDENTIALITY / DISCLAIMER NOTICE: This e-mail and any attachments may contain confidential and privileged information. If you are not the intended recipient, please notify the sender immediately by return e-mail, delete this e-mail and destroy any copies. Any dissemination or use of this information by a person other than the intended recipient is unauthorized and may be illegal.

________________________________

Re: Combined query results

Posted by Claude Warren <cl...@xenei.com>.
How about:

prefix : <http://example.org/>

Select ?alice ?address where {
       ?alice :hasAddress ?address;
                  :hasName "Alice"
}




On Thu, Jun 7, 2018, 12:36 AM Samita Bai / PhD CS Scholar @ City Campus <
sbai@iba.edu.pk> wrote:

> I need some help with the following code. I am using parameterized sparql
> string and I want to display combined results of both queries how can I do
> that. Any help or suggestion would be truly appreciated.
>
>
>
> final static String filename = "/home/samita/turtleContent.ttl";
>
>      static Model model= null;
>
>
>     final static String turtleContent = "" +
>             "@prefix : <http://example.org/> .\n" +
>             "\n" +
>             ":alice :hasName \"Alice\" .\n" +
>             ":alice :hasAddress \"4222 Clinton Way\" .\n" +
>             ":herman :hasName \"Herman\".\n" +
>             ":herman :hasAddress \"1313 Mockingbird Lane\" .\n" +
>             ":DrWho :hasAddress \"The TARDIS\"" +
>             "";
>
>
>    // Read the model from the turtle content
>    final static Model model = ModelFactory.createDefaultModel()
>            // .read( new ByteArrayInputStream( turtleContent.getBytes()),
> null, "TURTLE" );
>
>
>     final static String findAlice = "prefix : <http://example.org/>" +
>             "select ?alice where {" +
>             "?alice :hasName \"Alice\" }"  ;
>
>     final static String findAliceAddress = "prefix : <http://example.org/>"
> +
>             "select ?address where {" +
>             " ?alice :hasAddress ?address }";
>
>  public static void useParameterizedSPARQLString() {
>         System.out.println( "== useParameterizedSPARQLString ==" );
>         // execute the query that finds a (single) binding for ?alice.
> Then create
>         // a query solution map containing those results.
>         final ResultSet aliceResults = QueryExecutionFactory.create(
> findAlice, model ).execSelect();
>         final QuerySolutionMap map = new QuerySolutionMap();
>         map.addAll( aliceResults.next() );
>         // Create a ParameterizedSparqlString from the findAliceAddress
> query string (if this
>         // approach were taken, findAliceAddress could actually *be* a
> Param.SparqlString, of
>         // course).
>         final ParameterizedSparqlString pss = new
> ParameterizedSparqlString( findAliceAddress );
>         System.out.println( pss.toString() );
>         pss.setParams( map );
>         System.out.println( pss.toString() );
>         // execute the query and show the results
>         ResultSetFormatter.out( QueryExecutionFactory.create(
> pss.toString(), model ).execSelect() );
>     }
>
> The results I get is:
>
> ----------------------
> | address            |
> ======================
> | "4222 Clinton Way" |
> ----------------------
>
> Is there any way to display the variable ?alice also
>
> Like
>
> alice                                       address
>
> http://example.org/alice            "4222 Clinton Way"<
> http://example.org/alice>
>
>
>
>
>
> P : Please consider the environment before printing this e-mail
>
> ________________________________
>
> CONFIDENTIALITY / DISCLAIMER NOTICE: This e-mail and any attachments may
> contain confidential and privileged information. If you are not the
> intended recipient, please notify the sender immediately by return e-mail,
> delete this e-mail and destroy any copies. Any dissemination or use of this
> information by a person other than the intended recipient is unauthorized
> and may be illegal.
>
> ________________________________
>