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/19 15:38:09 UTC

SPARQL Query which Connects Individuals from Different Classes

Dear Sir or Madam,

let's say I have an ontology, to make things clear and simple it looks like
this one:

a) three classes, every one has two individuals:
Person: Ivan and Ana;
City: London and Paris;
Gender: Male and Female.

b) Ivan from the class "Person" is related to Male from the class "Gender";
London from the class "City" is related to Male from the class "Gender".

How to write the SPARQL query to find/connect Ivan from the class "Person"
to London from the class "City"?

In SQL it would look something the following way (between, it could be
wrong):
SELECT London FROM City WHERE  Ivan=Male AND London=Male;


Sincerely Yours,

Darius Miliauskas

Re: SPARQL Query which Connects Individuals from Different Classes

Posted by Milorad Tosic <mb...@yahoo.com>.
Yes, there is a syntax error in the query. Instead of 
VALUES (?gender ?city) {base:Gender base:City} 
it should be
VALUES (?gender ?city) {(base:Gender base:City)} 

Regards,
Milorad





>________________________________
> From: Darius Miliauskas <da...@gmail.com>
>To: users@jena.apache.org; joshuaaaron@gmail.com 
>Sent: Monday, August 19, 2013 8:32 PM
>Subject: Re: SPARQL Query which Connects Individuals from Different Classes
> 
>
>Dear Joshua and others,
>
>yes, the file was not correctly formatted because I just write it by hand,
>not with any editor. Thanks, Joshua! Anyway, it was just an example. Maybe
>I did not formulate the question precisely. It is how to write a query
>which results connects two categories ("person" and "city" in the example)
>because these categories are connected through the third category ("gender"
>in the example)?
>
>The question is how to get answer like the below without directly
>connecting "person" and "city" in the query itself:
>
>
>| person     | city     |
>| Ivan     | London     |
>
>
>Between, I got errors while I was using VALUES in the query. Is there any
>recommendation how to use this keyword correctly (I guess it is wrong
>something with brackets or other syntax)?
>
>I used the following way:
>
>SELECT DISTINCT ?person ?city WHERE{ VALUES (?gender ?city) {base:Gender
>base:City} ?person a base:Person; owl:topObjectProperty ?gender. ?city a
>base:City; owl:topObjectProperty ?gender};
>
>
>Yours,
>
>Darius
>
>
>2013/8/19 Joshua TAYLOR <jo...@gmail.com>
>
>> The ontology data that you provided is not well formed XML, let alone
>> RDF/XML.  It looks like, however, that you're aiming for something
>> like:
>>
>>
>> <rdf:RDF
>>     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>>     xmlns:owl="http://www.w3.org/2002/07/owl#"
>>     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
>>     xmlns="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#"
>>     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
>>   <owl:Ontology
>> rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms"/>
>>   <owl:Class rdf:about="
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>
>>   <owl:Class rdf:about="
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
>>   <owl:Class rdf:about="
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>
>>   <owl:NamedIndividual
>> rdf:about="
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#London">
>>     <rdf:type rdf:resource="
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>
>>     <owl:topObjectProperty>
>>       <owl:NamedIndividual
>> rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male
>> ">
>>         <rdf:type
>> rdf:resource="
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>
>>       </owl:NamedIndividual>
>>     </owl:topObjectProperty>
>>   </owl:NamedIndividual>
>>   <owl:NamedIndividual
>> rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ana">
>>     <rdf:type rdf:resource="
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
>>   </owl:NamedIndividual>
>>   <owl:NamedIndividual
>> rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ivan
>> ">
>>     <rdf:type rdf:resource="
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
>>     <owl:topObjectProperty
>> rdf:resource="
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male"/>
>>   </owl:NamedIndividual>
>> </rdf:RDF>
>>
>>
>> or, in the more readable Turtle syntax:
>>
>>
>> @prefix :        <
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#> .
>> @prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
>> @prefix owl:     <http://www.w3.org/2002/07/owl#> .
>> @prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
>> @prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
>>
>> :Gender
>>       a       owl:Class .
>>
>> <http://www.semanticweb.org/darius/ontologies/2013/6/rooms>
>>       a       owl:Ontology .
>>
>> :Male
>>       a       :Gender , owl:NamedIndividual .
>>
>> :London
>>       a       owl:NamedIndividual , :City ;
>>       owl:topObjectProperty
>>               :Male .
>>
>> :Ana  a       owl:NamedIndividual , :Person .
>>
>> :Ivan
>>       a       owl:NamedIndividual , :Person ;
>>       owl:topObjectProperty
>>               :Male .
>>
>> :Person
>>       a       owl:Class .
>>
>> :City
>>       a       owl:Class .
>>
>>
>> Given that ontology, you can use a query like this:
>>
>>
>> prefix rooms: <http://www.semanticweb.org/darius/ontologies/2013/6/rooms#>
>> prefix owl: <http://www.w3.org/2002/07/owl#>
>>
>> select ?person ?personProperty ?city ?cityProperty ?gender where {
>>
>>   values (?person ?city) {
>>     (rooms:Ivan rooms:London)
>>   }
>>
>>   ?person a rooms:Person ;
>>           ?personProperty ?gender .
>>   ?gender a rooms:Gender .
>>   ?city a rooms:City ;
>>         ?cityProperty ?gender .
>> }
>>
>>
>> and get results like:
>>
>>
>> $ arq --data data.rdf --query query.sparql
>>
>> ------------------------------------------------------------------------------------------
>> | person     | personProperty        | city         | cityProperty
>>      | gender     |
>>
>> ==========================================================================================
>> | rooms:Ivan | owl:topObjectProperty | rooms:London |
>> owl:topObjectProperty | rooms:Male |
>>
>> ------------------------------------------------------------------------------------------
>>
>>
>> which looks like it should at least get you started toward what you're
>> trying to achieve.
>>
>> //JT
>>
>>
>> On Mon, Aug 19, 2013 at 10:16 AM, Darius Miliauskas
>> <da...@gmail.com> wrote:
>> > Dear Arthur,
>> >
>> > Thanks for the prompt answer, I will try. Here, is the ontology as
>> RDF/XML
>> > file:
>> >
>> >
>> > <?xml version="1.0"?>
>> >
>> > <!DOCTYPE rdf:RDF [
>> >     <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
>> >     <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
>> >     <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
>> >     <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
>> > ]>
>> >
>> > <rdf:RDF xmlns="
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#"
>> >      xml:base="http://www.semanticweb.org/darius/ontologies/2013/6/rooms
>> "
>> >      xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
>> >      xmlns:owl="http://www.w3.org/2002/07/owl#"
>> >      xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
>> >      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
>> >     <owl:Ontology rdf:about="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms"/>
>> >
>> >   <!--
>> >
>> >
>> ///////////////////////////////////////////////////////////////////////////////////////
>> >     //
>> >     // Classes
>> >     //
>> >
>> >
>> ///////////////////////////////////////////////////////////////////////////////////////
>> >      -->
>> >
>> >     <!--
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person-->
>> >
>> >     <owl:Class rdf:about="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
>> >
>> >
>> >     <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City-->
>> >
>> >     <owl:Class rdf:about="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>
>> >
>> >
>> >     <!--
>> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender-->
>> >
>> >     <owl:Class rdf:about="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>
>> >
>> >
>> ///////////////////////////////////////////////////////////////////////////////////////
>> >     //
>> >     // Individuals
>> >     //
>> >
>> >
>> ///////////////////////////////////////////////////////////////////////////////////////
>> >      -->
>> >
>> > <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ivan -->
>> >
>> >     <owl:NamedIndividual rdf:about="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ivan">
>> >
>> >         <rdf:type rdf:resource="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
>> >         <owl:topObjectProperty rdf:resource="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male"/>
>> >
>> >
>> > <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ana -->
>> >
>> >     <owl:NamedIndividual rdf:about="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ana">
>> >
>> >         <rdf:type rdf:resource="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
>> >
>> >
>> >  <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male -->
>> >
>> >     <owl:NamedIndividual rdf:about="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male">
>> >
>> >         <rdf:type rdf:resource="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>
>> >
>> >
>> > <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#London-->
>> >
>> >     <owl:NamedIndividual rdf:about="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#London">
>> >
>> >         <rdf:type rdf:resource="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>
>> >         <owl:topObjectProperty rdf:resource="
>> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male"/>
>> >
>> >
>> > 2013/8/19 Arthur Vaïsse-Lesteven <ar...@yahoo.fr>
>> >
>> >> There is missing information about object properties. And the link
>> between
>> >> Ivan and London isn't clear.
>> >>
>> >> Assuming you are requesting a query to create a triple of type (Ivan, a
>> >> relation, London) you could try something like this:
>> >>
>> >> INSERT{ ?person isRelatedTo ?city}
>> >> WHERE{
>> >> ?person a Person.
>> >> ?person name "Ivan"^^xsd:String.
>> >> ?city a City.
>> >> ?city name "London"^^xsd:String.
>> >> }
>> >>
>> >>
>> >> where "isRelatedTo" is an object properties of your choice.
>> >> This query will create a triple for each city with a name of London and
>> >> every Person with a name equal to "Ivan".
>> >>
>> >> I hope this will help you. For more information about SPARQL queries I
>> >> suggest you to read the current W3C Recommendation (
>> >> http://www.w3.org/TR/sparql11-query/ )
>> >>
>> >> It would be much easier to help you with your minimal ontology include
>> in
>> >> the mail.
>> >>
>> >> VAÏSSE-LESTEVEN Arthur
>> >>
>> >> >Dear Sir or Madam,
>> >> >
>> >> >let's say I have an ontology, to make things clear and simple it looks
>> >> like
>> >> >this one:
>> >> >
>> >> >a) three classes, every one has two individuals:
>> >> >Person: Ivan and Ana;
>> >> >City: London and Paris;
>> >> >Gender: Male and Female.
>> >> >
>> >> >b) Ivan from the class "Person" is related to Male from the class
>> >> "Gender";
>> >> >London from the class "City" is related to Male from the class
>> "Gender".
>> >> >
>> >> >How to write the SPARQL query to find/connect Ivan from the class
>> "Person"
>> >> >to London from the class "City"?
>> >> >
>> >> >In SQL it would look something the following way (between, it could be
>> >> >wrong):
>> >> >SELECT London FROM City WHERE  Ivan=Male AND London=Male;
>> >> >
>> >> >
>> >> >Sincerely Yours,
>> >> >
>> >> >Darius Miliauskas
>> >>
>>
>>
>>
>> --
>> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>>
>
>

Re: SPARQL Query which Connects Individuals from Different Classes

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Wed, Aug 21, 2013 at 6:24 AM, Darius Miliauskas
<da...@gmail.com> wrote:
> Thanks, guys, I do not know what is wrong but when I try to use "VALUES" I
> got org.openrdf.query.MalformedQueryException. Encountered: " " after:
> "values". I have read the specification of SPARQL, tried several different
> ways but the same error is still on. Even my simplified query looks like
> this:
>
> SELECT ?person ?city WHERE {
> VALUES (?person ?city) {
> (base:person
> base:city)
>   }
> ?person a base:Person.
> ?city a base:city.
> }
>
> Error is on the line "VALUES (?person ?city) {".
>
>
> Yours,
>
> Darius


VALUES was introduced in SPARQL 1.1, so if you're querying against an
endpoint that uses the older version of SPARQL, VALUES will be
rejected. Similarly, if you're trying to *parse* the query as SPARQL
1.1, you'll get this sort of issue.

I only used VALUES in the first query that I sent you as a way of
restricting the results to one row so that they'd be easier to copy
and paste into an email without making it huge.  Your simplified query
doesn't end up leaving any variable *unbound*, and could just be:

SELECT ?person ?city WHERE {
  base:person a base:Person.
  base:cite a base:city.
}

VALUES is useful when you need to say "this sequence of variables, can
have each binding in this set."  If there's only one binding in that
set, you don't need values, and you can just write the values inline,
as I did above.

//JT

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

Re: SPARQL Query which Connects Individuals from Different Classes

Posted by Joshua TAYLOR <jo...@gmail.com>.
On Mon, Aug 19, 2013 at 2:32 PM, Darius Miliauskas
<da...@gmail.com> wrote:
> yes, the file was not correctly formatted because I just write it by hand,
> not with any editor. Thanks, Joshua! Anyway, it was just an example. Maybe I
> did not formulate the question precisely. It is how to write a query which
> results connects two categories ("person" and "city" in the example) because
> these categories are connected through the third category ("gender" in the
> example)?
>
> The question is how to get answer like the below without directly connecting
> "person" and "city" in the query itself:
>
> | person     | city     |
> | Ivan     | London     |
>
> Between, I got errors while I was using VALUES in the query. Is there any
> recommendation how to use this keyword correctly (I guess it is wrong
> something with brackets or other syntax)?
>
> I used the following way:
>
> SELECT DISTINCT ?person ?city WHERE{ VALUES (?gender ?city) {base:Gender
> base:City} ?person a base:Person; owl:topObjectProperty ?gender. ?city a
> base:City; owl:topObjectProperty ?gender};

Milorad Tosic already replied with the correct version of that query,
but the general syntax is defined in the publicly available W3C SPARQL
recommendation, section 10.2 VALUES: Providing inline data [1].  //JT

[1] http://www.w3.org/TR/sparql11-query/#inline-data

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

Re: SPARQL Query which Connects Individuals from Different Classes

Posted by Darius Miliauskas <da...@gmail.com>.
Dear Joshua and others,

yes, the file was not correctly formatted because I just write it by hand,
not with any editor. Thanks, Joshua! Anyway, it was just an example. Maybe
I did not formulate the question precisely. It is how to write a query
which results connects two categories ("person" and "city" in the example)
because these categories are connected through the third category ("gender"
in the example)?

The question is how to get answer like the below without directly
connecting "person" and "city" in the query itself:


| person     | city     |
| Ivan     | London     |


Between, I got errors while I was using VALUES in the query. Is there any
recommendation how to use this keyword correctly (I guess it is wrong
something with brackets or other syntax)?

I used the following way:

SELECT DISTINCT ?person ?city WHERE{ VALUES (?gender ?city) {base:Gender
base:City} ?person a base:Person; owl:topObjectProperty ?gender. ?city a
base:City; owl:topObjectProperty ?gender};


Yours,

Darius


2013/8/19 Joshua TAYLOR <jo...@gmail.com>

> The ontology data that you provided is not well formed XML, let alone
> RDF/XML.  It looks like, however, that you're aiming for something
> like:
>
>
> <rdf:RDF
>     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>     xmlns:owl="http://www.w3.org/2002/07/owl#"
>     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
>     xmlns="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#"
>     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
>   <owl:Ontology
> rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms"/>
>   <owl:Class rdf:about="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>
>   <owl:Class rdf:about="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
>   <owl:Class rdf:about="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>
>   <owl:NamedIndividual
> rdf:about="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#London">
>     <rdf:type rdf:resource="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>
>     <owl:topObjectProperty>
>       <owl:NamedIndividual
> rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male
> ">
>         <rdf:type
> rdf:resource="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>
>       </owl:NamedIndividual>
>     </owl:topObjectProperty>
>   </owl:NamedIndividual>
>   <owl:NamedIndividual
> rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ana">
>     <rdf:type rdf:resource="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
>   </owl:NamedIndividual>
>   <owl:NamedIndividual
> rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ivan
> ">
>     <rdf:type rdf:resource="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
>     <owl:topObjectProperty
> rdf:resource="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male"/>
>   </owl:NamedIndividual>
> </rdf:RDF>
>
>
> or, in the more readable Turtle syntax:
>
>
> @prefix :        <
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#> .
> @prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
> @prefix owl:     <http://www.w3.org/2002/07/owl#> .
> @prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
> @prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
>
> :Gender
>       a       owl:Class .
>
> <http://www.semanticweb.org/darius/ontologies/2013/6/rooms>
>       a       owl:Ontology .
>
> :Male
>       a       :Gender , owl:NamedIndividual .
>
> :London
>       a       owl:NamedIndividual , :City ;
>       owl:topObjectProperty
>               :Male .
>
> :Ana  a       owl:NamedIndividual , :Person .
>
> :Ivan
>       a       owl:NamedIndividual , :Person ;
>       owl:topObjectProperty
>               :Male .
>
> :Person
>       a       owl:Class .
>
> :City
>       a       owl:Class .
>
>
> Given that ontology, you can use a query like this:
>
>
> prefix rooms: <http://www.semanticweb.org/darius/ontologies/2013/6/rooms#>
> prefix owl: <http://www.w3.org/2002/07/owl#>
>
> select ?person ?personProperty ?city ?cityProperty ?gender where {
>
>   values (?person ?city) {
>     (rooms:Ivan rooms:London)
>   }
>
>   ?person a rooms:Person ;
>           ?personProperty ?gender .
>   ?gender a rooms:Gender .
>   ?city a rooms:City ;
>         ?cityProperty ?gender .
> }
>
>
> and get results like:
>
>
> $ arq --data data.rdf --query query.sparql
>
> ------------------------------------------------------------------------------------------
> | person     | personProperty        | city         | cityProperty
>      | gender     |
>
> ==========================================================================================
> | rooms:Ivan | owl:topObjectProperty | rooms:London |
> owl:topObjectProperty | rooms:Male |
>
> ------------------------------------------------------------------------------------------
>
>
> which looks like it should at least get you started toward what you're
> trying to achieve.
>
> //JT
>
>
> On Mon, Aug 19, 2013 at 10:16 AM, Darius Miliauskas
> <da...@gmail.com> wrote:
> > Dear Arthur,
> >
> > Thanks for the prompt answer, I will try. Here, is the ontology as
> RDF/XML
> > file:
> >
> >
> > <?xml version="1.0"?>
> >
> > <!DOCTYPE rdf:RDF [
> >     <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
> >     <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
> >     <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
> >     <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
> > ]>
> >
> > <rdf:RDF xmlns="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#"
> >      xml:base="http://www.semanticweb.org/darius/ontologies/2013/6/rooms
> "
> >      xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
> >      xmlns:owl="http://www.w3.org/2002/07/owl#"
> >      xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
> >      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
> >     <owl:Ontology rdf:about="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms"/>
> >
> >   <!--
> >
> >
> ///////////////////////////////////////////////////////////////////////////////////////
> >     //
> >     // Classes
> >     //
> >
> >
> ///////////////////////////////////////////////////////////////////////////////////////
> >      -->
> >
> >     <!--
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person-->
> >
> >     <owl:Class rdf:about="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
> >
> >
> >     <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City-->
> >
> >     <owl:Class rdf:about="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>
> >
> >
> >     <!--
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender-->
> >
> >     <owl:Class rdf:about="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>
> >
> >
> ///////////////////////////////////////////////////////////////////////////////////////
> >     //
> >     // Individuals
> >     //
> >
> >
> ///////////////////////////////////////////////////////////////////////////////////////
> >      -->
> >
> > <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ivan -->
> >
> >     <owl:NamedIndividual rdf:about="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ivan">
> >
> >         <rdf:type rdf:resource="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
> >         <owl:topObjectProperty rdf:resource="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male"/>
> >
> >
> > <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ana -->
> >
> >     <owl:NamedIndividual rdf:about="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ana">
> >
> >         <rdf:type rdf:resource="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
> >
> >
> >  <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male -->
> >
> >     <owl:NamedIndividual rdf:about="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male">
> >
> >         <rdf:type rdf:resource="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>
> >
> >
> > <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#London-->
> >
> >     <owl:NamedIndividual rdf:about="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#London">
> >
> >         <rdf:type rdf:resource="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>
> >         <owl:topObjectProperty rdf:resource="
> > http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male"/>
> >
> >
> > 2013/8/19 Arthur Vaïsse-Lesteven <ar...@yahoo.fr>
> >
> >> There is missing information about object properties. And the link
> between
> >> Ivan and London isn't clear.
> >>
> >> Assuming you are requesting a query to create a triple of type (Ivan, a
> >> relation, London) you could try something like this:
> >>
> >> INSERT{ ?person isRelatedTo ?city}
> >> WHERE{
> >> ?person a Person.
> >> ?person name "Ivan"^^xsd:String.
> >> ?city a City.
> >> ?city name "London"^^xsd:String.
> >> }
> >>
> >>
> >> where "isRelatedTo" is an object properties of your choice.
> >> This query will create a triple for each city with a name of London and
> >> every Person with a name equal to "Ivan".
> >>
> >> I hope this will help you. For more information about SPARQL queries I
> >> suggest you to read the current W3C Recommendation (
> >> http://www.w3.org/TR/sparql11-query/ )
> >>
> >> It would be much easier to help you with your minimal ontology include
> in
> >> the mail.
> >>
> >> VAÏSSE-LESTEVEN Arthur
> >>
> >> >Dear Sir or Madam,
> >> >
> >> >let's say I have an ontology, to make things clear and simple it looks
> >> like
> >> >this one:
> >> >
> >> >a) three classes, every one has two individuals:
> >> >Person: Ivan and Ana;
> >> >City: London and Paris;
> >> >Gender: Male and Female.
> >> >
> >> >b) Ivan from the class "Person" is related to Male from the class
> >> "Gender";
> >> >London from the class "City" is related to Male from the class
> "Gender".
> >> >
> >> >How to write the SPARQL query to find/connect Ivan from the class
> "Person"
> >> >to London from the class "City"?
> >> >
> >> >In SQL it would look something the following way (between, it could be
> >> >wrong):
> >> >SELECT London FROM City WHERE  Ivan=Male AND London=Male;
> >> >
> >> >
> >> >Sincerely Yours,
> >> >
> >> >Darius Miliauskas
> >>
>
>
>
> --
> Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
>

Re: SPARQL Query which Connects Individuals from Different Classes

Posted by Joshua TAYLOR <jo...@gmail.com>.
The ontology data that you provided is not well formed XML, let alone
RDF/XML.  It looks like, however, that you're aiming for something
like:


<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <owl:Ontology
rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms"/>
  <owl:Class rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>
  <owl:Class rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
  <owl:Class rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>
  <owl:NamedIndividual
rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#London">
    <rdf:type rdf:resource="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>
    <owl:topObjectProperty>
      <owl:NamedIndividual
rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male">
        <rdf:type
rdf:resource="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>
      </owl:NamedIndividual>
    </owl:topObjectProperty>
  </owl:NamedIndividual>
  <owl:NamedIndividual
rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ana">
    <rdf:type rdf:resource="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
  </owl:NamedIndividual>
  <owl:NamedIndividual
rdf:about="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ivan">
    <rdf:type rdf:resource="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
    <owl:topObjectProperty
rdf:resource="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male"/>
  </owl:NamedIndividual>
</rdf:RDF>


or, in the more readable Turtle syntax:


@prefix :        <http://www.semanticweb.org/darius/ontologies/2013/6/rooms#> .
@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:     <http://www.w3.org/2002/07/owl#> .
@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

:Gender
      a       owl:Class .

<http://www.semanticweb.org/darius/ontologies/2013/6/rooms>
      a       owl:Ontology .

:Male
      a       :Gender , owl:NamedIndividual .

:London
      a       owl:NamedIndividual , :City ;
      owl:topObjectProperty
              :Male .

:Ana  a       owl:NamedIndividual , :Person .

:Ivan
      a       owl:NamedIndividual , :Person ;
      owl:topObjectProperty
              :Male .

:Person
      a       owl:Class .

:City
      a       owl:Class .


Given that ontology, you can use a query like this:


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

select ?person ?personProperty ?city ?cityProperty ?gender where {

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

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


and get results like:


$ arq --data data.rdf --query query.sparql
------------------------------------------------------------------------------------------
| person     | personProperty        | city         | cityProperty
     | gender     |
==========================================================================================
| rooms:Ivan | owl:topObjectProperty | rooms:London |
owl:topObjectProperty | rooms:Male |
------------------------------------------------------------------------------------------


which looks like it should at least get you started toward what you're
trying to achieve.

//JT


On Mon, Aug 19, 2013 at 10:16 AM, Darius Miliauskas
<da...@gmail.com> wrote:
> Dear Arthur,
>
> Thanks for the prompt answer, I will try. Here, is the ontology as RDF/XML
> file:
>
>
> <?xml version="1.0"?>
>
> <!DOCTYPE rdf:RDF [
>     <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
>     <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
>     <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
>     <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
> ]>
>
> <rdf:RDF xmlns="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#"
>      xml:base="http://www.semanticweb.org/darius/ontologies/2013/6/rooms"
>      xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
>      xmlns:owl="http://www.w3.org/2002/07/owl#"
>      xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
>      xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
>     <owl:Ontology rdf:about="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms"/>
>
>   <!--
>
> ///////////////////////////////////////////////////////////////////////////////////////
>     //
>     // Classes
>     //
>
> ///////////////////////////////////////////////////////////////////////////////////////
>      -->
>
>     <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person-->
>
>     <owl:Class rdf:about="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
>
>
>     <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City -->
>
>     <owl:Class rdf:about="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>
>
>
>     <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender-->
>
>     <owl:Class rdf:about="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>
>
> ///////////////////////////////////////////////////////////////////////////////////////
>     //
>     // Individuals
>     //
>
> ///////////////////////////////////////////////////////////////////////////////////////
>      -->
>
> <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ivan -->
>
>     <owl:NamedIndividual rdf:about="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ivan">
>
>         <rdf:type rdf:resource="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
>         <owl:topObjectProperty rdf:resource="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male"/>
>
>
> <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ana -->
>
>     <owl:NamedIndividual rdf:about="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ana">
>
>         <rdf:type rdf:resource="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
>
>
>  <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male -->
>
>     <owl:NamedIndividual rdf:about="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male">
>
>         <rdf:type rdf:resource="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>
>
>
> <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#London -->
>
>     <owl:NamedIndividual rdf:about="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#London">
>
>         <rdf:type rdf:resource="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>
>         <owl:topObjectProperty rdf:resource="
> http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male"/>
>
>
> 2013/8/19 Arthur Vaïsse-Lesteven <ar...@yahoo.fr>
>
>> There is missing information about object properties. And the link between
>> Ivan and London isn't clear.
>>
>> Assuming you are requesting a query to create a triple of type (Ivan, a
>> relation, London) you could try something like this:
>>
>> INSERT{ ?person isRelatedTo ?city}
>> WHERE{
>> ?person a Person.
>> ?person name "Ivan"^^xsd:String.
>> ?city a City.
>> ?city name "London"^^xsd:String.
>> }
>>
>>
>> where "isRelatedTo" is an object properties of your choice.
>> This query will create a triple for each city with a name of London and
>> every Person with a name equal to "Ivan".
>>
>> I hope this will help you. For more information about SPARQL queries I
>> suggest you to read the current W3C Recommendation (
>> http://www.w3.org/TR/sparql11-query/ )
>>
>> It would be much easier to help you with your minimal ontology include in
>> the mail.
>>
>> VAÏSSE-LESTEVEN Arthur
>>
>> >Dear Sir or Madam,
>> >
>> >let's say I have an ontology, to make things clear and simple it looks
>> like
>> >this one:
>> >
>> >a) three classes, every one has two individuals:
>> >Person: Ivan and Ana;
>> >City: London and Paris;
>> >Gender: Male and Female.
>> >
>> >b) Ivan from the class "Person" is related to Male from the class
>> "Gender";
>> >London from the class "City" is related to Male from the class "Gender".
>> >
>> >How to write the SPARQL query to find/connect Ivan from the class "Person"
>> >to London from the class "City"?
>> >
>> >In SQL it would look something the following way (between, it could be
>> >wrong):
>> >SELECT London FROM City WHERE  Ivan=Male AND London=Male;
>> >
>> >
>> >Sincerely Yours,
>> >
>> >Darius Miliauskas
>>



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

Re: SPARQL Query which Connects Individuals from Different Classes

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

Thanks for the prompt answer, I will try. Here, is the ontology as RDF/XML
file:


<?xml version="1.0"?>

<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>

<rdf:RDF xmlns="http://www.semanticweb.org/darius/ontologies/2013/6/rooms#"
     xml:base="http://www.semanticweb.org/darius/ontologies/2013/6/rooms"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <owl:Ontology rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms"/>

  <!--

///////////////////////////////////////////////////////////////////////////////////////
    //
    // Classes
    //

///////////////////////////////////////////////////////////////////////////////////////
     -->

    <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person-->

    <owl:Class rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>


    <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City -->

    <owl:Class rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>


    <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender-->

    <owl:Class rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>

///////////////////////////////////////////////////////////////////////////////////////
    //
    // Individuals
    //

///////////////////////////////////////////////////////////////////////////////////////
     -->

<!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ivan -->

    <owl:NamedIndividual rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ivan">

        <rdf:type rdf:resource="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>
        <owl:topObjectProperty rdf:resource="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male"/>


<!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ana -->

    <owl:NamedIndividual rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Ana">

        <rdf:type rdf:resource="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Person"/>


 <!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male -->

    <owl:NamedIndividual rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male">

        <rdf:type rdf:resource="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Gender"/>


<!-- http://www.semanticweb.org/darius/ontologies/2013/6/rooms#London -->

    <owl:NamedIndividual rdf:about="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#London">

        <rdf:type rdf:resource="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#City"/>
        <owl:topObjectProperty rdf:resource="
http://www.semanticweb.org/darius/ontologies/2013/6/rooms#Male"/>


2013/8/19 Arthur Vaïsse-Lesteven <ar...@yahoo.fr>

> There is missing information about object properties. And the link between
> Ivan and London isn't clear.
>
> Assuming you are requesting a query to create a triple of type (Ivan, a
> relation, London) you could try something like this:
>
> INSERT{ ?person isRelatedTo ?city}
> WHERE{
> ?person a Person.
> ?person name "Ivan"^^xsd:String.
> ?city a City.
> ?city name "London"^^xsd:String.
> }
>
>
> where "isRelatedTo" is an object properties of your choice.
> This query will create a triple for each city with a name of London and
> every Person with a name equal to "Ivan".
>
> I hope this will help you. For more information about SPARQL queries I
> suggest you to read the current W3C Recommendation (
> http://www.w3.org/TR/sparql11-query/ )
>
> It would be much easier to help you with your minimal ontology include in
> the mail.
>
> VAÏSSE-LESTEVEN Arthur
>
> >Dear Sir or Madam,
> >
> >let's say I have an ontology, to make things clear and simple it looks
> like
> >this one:
> >
> >a) three classes, every one has two individuals:
> >Person: Ivan and Ana;
> >City: London and Paris;
> >Gender: Male and Female.
> >
> >b) Ivan from the class "Person" is related to Male from the class
> "Gender";
> >London from the class "City" is related to Male from the class "Gender".
> >
> >How to write the SPARQL query to find/connect Ivan from the class "Person"
> >to London from the class "City"?
> >
> >In SQL it would look something the following way (between, it could be
> >wrong):
> >SELECT London FROM City WHERE  Ivan=Male AND London=Male;
> >
> >
> >Sincerely Yours,
> >
> >Darius Miliauskas
>

Re: SPARQL Query which Connects Individuals from Different Classes

Posted by Arthur Vaïsse-Lesteven <ar...@yahoo.fr>.
There is missing information about object properties. And the link between Ivan and London isn't clear.

Assuming you are requesting a query to create a triple of type (Ivan, a relation, London) you could try something like this:

INSERT{ ?person isRelatedTo ?city}
WHERE{
?person a Person.
?person name "Ivan"^^xsd:String.
?city a City.
?city name "London"^^xsd:String.
}


where "isRelatedTo" is an object properties of your choice.
This query will create a triple for each city with a name of London and every Person with a name equal to "Ivan".

I hope this will help you. For more information about SPARQL queries I suggest you to read the current W3C Recommendation ( http://www.w3.org/TR/sparql11-query/ )

It would be much easier to help you with your minimal ontology include in the mail.

VAÏSSE-LESTEVEN Arthur

>Dear Sir or Madam,
>
>let's say I have an ontology, to make things clear and simple it looks like
>this one:
>
>a) three classes, every one has two individuals:
>Person: Ivan and Ana;
>City: London and Paris;
>Gender: Male and Female.
>
>b) Ivan from the class "Person" is related to Male from the class "Gender";
>London from the class "City" is related to Male from the class "Gender".
>
>How to write the SPARQL query to find/connect Ivan from the class "Person"
>to London from the class "City"?
>
>In SQL it would look something the following way (between, it could be
>wrong):
>SELECT London FROM City WHERE  Ivan=Male AND London=Male;
>
>
>Sincerely Yours,
>
>Darius Miliauskas