count occurrences of elements in a bag

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

count occurrences of elements in a bag

Loveleen
This post was updated on .
Hello All,
I am trying to write a query in SQWRL that counts the occurrences of
elements in a bag.
Given Class C1 has elements A,B,C,D
         Class C2 has elements B,C,D,E
         Class C3 has elements C,D
then a single Bag of  C1,C2,C3 will consist of the elements
A,B,C,D,B,C,D,E,C,D
I want the SQWRL query to print the output in the following manner
A 1
B 2
C 3
D 3
E 1
Can anyone please help me out with this?
Thanks in advance!
Loveleen



--
Sent from: http://protege-project.136.n4.nabble.com/Protege-User-f4659818.html
_______________________________________________
protege-user mailing list
protege-user@lists.stanford.edu
https://mailman.stanford.edu/mailman/listinfo/protege-user
Reply | Threaded
Open this post in threaded view
|

Re: count occurrences of elements in a bag

Daniel Schiebel
Dear Loveleen,

I tried to solve your issue. I assume you have 3 classes (Let's call them
Class1, Class2 and Class3). In my opinion it's not possible for an object
property (here: contains) to have a class as domain. So you create
individuals for each class (X1 of type Class1, X2: type Class2, X3: type
Class 3).
Then you create another class called Types_of_content with subclasses A, B,
C, D, E. Then you create individuals for each element e.g. 4 individuals for
Class1 -> A1 (Type: A), B1 (Type: B), C1, D1 and connect them to the
individual X1 with object property 'contains' (domain: Class1 or Class2 or
Class3; Range: Types_of_content). The same you do with the other elements
(B2, C2, D2, E2 to X2; C3, D3 to X3)
Then I use a trick. I created a DataProperty called value (domain:
Types_of_content) and gave every individual this DataProperty with the value
1. You can do that manual or with SWRL-Rule.
Then I can create the following SPARQL-Query. I hope it's ok that it's no
SQWRL-Query:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX abc: <Link-of-your-ontology!!!!#>
SELECT ?elementOrder  (SUM(?value) AS ?sum) WHERE {?element abc:Anzahl
?value . ?element rdf:type ?elementOrder} GROUP BY ?elementOrder

I hope it will help!

Best regards,
Daniel



--
Sent from: http://protege-project.136.n4.nabble.com/Protege-User-f4659818.html
_______________________________________________
protege-user mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-user
Reply | Threaded
Open this post in threaded view
|

Re: count occurrences of elements in a bag

Michael DeBellis-2
>  In my opinion it's not possible for an object
> property (here: contains) to have a class as domain. 

I don't understand what you mean by this. It certainly is possible in OWL and Protege for an object property to have a class as it's domain. In fact, if an object property has a domain then by definition that domain defines a class. It could be some class generated on the fly by some DL statement but any time you have a domain for an object property it's going to be a class that is either explicitly or implicitly defined. It's not required and for some kinds of applications it's better to not define the domain and/or range of your properties but it certainly is possible and people do it all the time. Perhaps you just mean that you don't think it's a good way to model things? In which case I don't understand why. 

Michael



On Sat, May 18, 2019 at 10:31 AM Daniel Schiebel <[hidden email]> wrote:
Dear Loveleen,

I tried to solve your issue. I assume you have 3 classes (Let's call them
Class1, Class2 and Class3). In my opinion it's not possible for an object
property (here: contains) to have a class as domain. So you create
individuals for each class (X1 of type Class1, X2: type Class2, X3: type
Class 3).
Then you create another class called Types_of_content with subclasses A, B,
C, D, E. Then you create individuals for each element e.g. 4 individuals for
Class1 -> A1 (Type: A), B1 (Type: B), C1, D1 and connect them to the
individual X1 with object property 'contains' (domain: Class1 or Class2 or
Class3; Range: Types_of_content). The same you do with the other elements
(B2, C2, D2, E2 to X2; C3, D3 to X3)
Then I use a trick. I created a DataProperty called value (domain:
Types_of_content) and gave every individual this DataProperty with the value
1. You can do that manual or with SWRL-Rule.
Then I can create the following SPARQL-Query. I hope it's ok that it's no
SQWRL-Query:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX abc: <Link-of-your-ontology!!!!#>
SELECT ?elementOrder  (SUM(?value) AS ?sum) WHERE {?element abc:Anzahl
?value . ?element rdf:type ?elementOrder} GROUP BY ?elementOrder

I hope it will help!

Best regards,
Daniel



--
Sent from: http://protege-project.136.n4.nabble.com/Protege-User-f4659818.html
_______________________________________________
protege-user mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-user

_______________________________________________
protege-user mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-user
Reply | Threaded
Open this post in threaded view
|

Re: count occurrences of elements in a bag

Daniel Schiebel
Dear Michael,

I have to admit, that this sentence of my statement was very imprecisely. Of
course you are right that you can define a class as domain for an object
property. I meant Object Property Assertions. I'm sorry for my mistake.

In my example you have the object property contains with domain (Class1 or
Class2 or Class3) and the range Types_of_content. Now I have created
elements (individuals) from this Types_of_content (A1, B1, C1, D1).
Now I wanted to set an object property assertion for every element that
Class1 contains A1, Class1 contains B1 ... and I don't think this is
possible this way. So I created an individual X1 from Type Class1. And now I
can set the object property assertion X1 contains A1, X1 contains B1 ...

Best regards,
Daniel

P.S. If it is possible to set an object property assertion with a class this
exceeds my knowledge. In case please let me know. I'm far away of being an
expert, just had an idea for this issue and wanted to help ;)



--
Sent from: http://protege-project.136.n4.nabble.com/Protege-User-f4659818.html
_______________________________________________
protege-user mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-user
Reply | Threaded
Open this post in threaded view
|

Re: count occurrences of elements in a bag

Lorenz Buehmann
Hi all,
not sure about your discussion, but the question was more or less clear, or I just misunderstood the example:  I think the TO wants an SQWRL query that computes the number of classes to which each individual is asserted to.

The SQWRL query should be:

abox:caa(?cls, ?ind)  ^  sqwrl:makeBag(?bag, ?cls) ^ sqwrl:groupBy(?bag, ?ind)  ^ sqwrl:size(?n, ?bag) -> sqwrl:select(?ind, ?n)


The SPARQL query is by the way also wrong ... you have to count by individual, not by class and I don't know why you want to introduce an new property.

select ?ind (count(?cls) as ?cnt) where {
   ?ind a ?cls
}
group by ?ind



Dear Michael,

I have to admit, that this sentence of my statement was very imprecisely. Of
course you are right that you can define a class as domain for an object
property. I meant Object Property Assertions. I'm sorry for my mistake.

In my example you have the object property contains with domain (Class1 or
Class2 or Class3) and the range Types_of_content. Now I have created
elements (individuals) from this Types_of_content (A1, B1, C1, D1).
Now I wanted to set an object property assertion for every element that
Class1 contains A1, Class1 contains B1 ... and I don't think this is
possible this way. So I created an individual X1 from Type Class1. And now I
can set the object property assertion X1 contains A1, X1 contains B1 ...

Best regards,
Daniel

P.S. If it is possible to set an object property assertion with a class this
exceeds my knowledge. In case please let me know. I'm far away of being an
expert, just had an idea for this issue and wanted to help ;)



--
Sent from: http://protege-project.136.n4.nabble.com/Protege-User-f4659818.html
_______________________________________________
protege-user mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-user

-- 
Lorenz Bühmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center

_______________________________________________
protege-user mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-user
Reply | Threaded
Open this post in threaded view
|

Re: count occurrences of elements in a bag

Loveleen
Hi all,
Thank You so much for taking out the time and attempting to solve my query.
I feel Lorenz's answer was the closest to what I wanted. Thank you Lorenz, i
really appreciate it!
However, I have some questions regarding the abox:caa plugin, does it work
for inferred classes?
And secondly, could you please guide me so as to what the exact sqwrl query
would be if we had multiple classes as i had stated in my original query.
Thanks in advance.



--
Sent from: http://protege-project.136.n4.nabble.com/Protege-User-f4659818.html
_______________________________________________
protege-user mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-user
Reply | Threaded
Open this post in threaded view
|

Re: count occurrences of elements in a bag

Michael DeBellis-2
In reply to this post by Daniel Schiebel
Daniel, no need to apologize, thanks for clarifying what you meant.  Regarding your question: 

> If it is possible to set an object property assertion with a class... please let me know.

It actually is possible. There is an OWL feature called Punning: https://www.w3.org/TR/owl2-new-features/#F12:_Punning 

With Punning you can create an Individual with the same name as a Class. This is useful for a (very basic) form of meta-object capability, when you want to add property assertions to a class. For example, if you had an ontology for cars and a class called Prius you might want to have a property called firstManufacturedOnDate  i.e., the first date a Prius was made, which you would want to store as information about the class. Or averageListPrice for the average price that a class of Car is recommended to be sold by a dealer. 

Michael

On Sun, May 19, 2019 at 8:08 AM Daniel Schiebel <[hidden email]> wrote:
Dear Michael,

I have to admit, that this sentence of my statement was very imprecisely. Of
course you are right that you can define a class as domain for an object
property. I meant Object Property Assertions. I'm sorry for my mistake.

In my example you have the object property contains with domain (Class1 or
Class2 or Class3) and the range Types_of_content. Now I have created
elements (individuals) from this Types_of_content (A1, B1, C1, D1).
Now I wanted to set an object property assertion for every element that
Class1 contains A1, Class1 contains B1 ... and I don't think this is
possible this way. So I created an individual X1 from Type Class1. And now I
can set the object property assertion X1 contains A1, X1 contains B1 ...

Best regards,
Daniel

P.S. If it is possible to set an object property assertion with a class this
exceeds my knowledge. In case please let me know. I'm far away of being an
expert, just had an idea for this issue and wanted to help ;)



--
Sent from: http://protege-project.136.n4.nabble.com/Protege-User-f4659818.html
_______________________________________________
protege-user mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-user

_______________________________________________
protege-user mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-user
Reply | Threaded
Open this post in threaded view
|

Re: count occurrences of elements in a bag

Michael DeBellis-2
In reply to this post by Loveleen

 >I have some questions regarding the abox:caa plugin, does it work
 >for inferred classes?  

Loveleen, First, I just wanted to correct you on some terminology. A plugin is a tool that has been developed by someone that works with Protege but is not part of the default build you get with Protege. They are meant for specific uses that won't apply to everyone who uses Protege so they are left as options for each user to decide which ones to load. They should show up in a box the first time you start Protege every day or if you do "File>Check for plugins" you should see that same dialog. 

"abox:caa" is not a plugin, it is part of the SWRLABoxBuiltInLibrary. The documentation can be found here:  https://github.com/protegeproject/swrlapi/wiki/SWRLABoxBuiltInLibrary  I've never used these but from my quick look at the documentation I think these only apply to user declared axioms not to things inferred by the reasoner. Note that on that page it says; "Note that these built-ins query only asserted axioms."  

One trick that I often use to get around this is the "File>Export inferred axioms as ontology" option. This will take you through a wizard to save the inferences from the reasoner as if they were asserted by the user.  If you do this make sure to check the boxes for the inferences that you care about, you may get a warning that it will take a long time, for most ontologies that is not the case but I've seen a few that were very complex even though not that large and that took forever with some of those options checked. My advice if you use this is first try it by checking all the boxes and then if it takes a long time think more carefully about some of the options. Also, note there is a minor bug, the file that is saved is an OWL file but it's not saved with a ".owl" extension. You can edit the file and add the extension after or when you specify the file name include the ".owl" at the end (which isn't intuitive, normally a dialog like that would add the extension for you). 

If you do this the exported file should have all the inferences as asserted axioms and I think you should be able to use the SWRLABoxBuiltInLibrary for them.Unfortunately, I can't help with the bit about how to do the query with multiple classes as I haven't used these built-ins. 

Michael

On Mon, May 20, 2019 at 8:05 AM Loveleen <[hidden email]> wrote:
Hi all,
Thank You so much for taking out the time and attempting to solve my query.
I feel Lorenz's answer was the closest to what I wanted. Thank you Lorenz, i
really appreciate it!
However, I have some questions regarding the abox:caa plugin, does it work
for inferred classes?
And secondly, could you please guide me so as to what the exact sqwrl query
would be if we had multiple classes as i had stated in my original query.
Thanks in advance.



--
Sent from: http://protege-project.136.n4.nabble.com/Protege-User-f4659818.html
_______________________________________________
protege-user mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-user

_______________________________________________
protege-user mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-user
Reply | Threaded
Open this post in threaded view
|

Re: count occurrences of elements in a bag

Lorenz Buehmann
In reply to this post by Loveleen

However, I have some questions regarding the abox:caa plugin, does it work
for inferred classes?
No, it doesn't.
And secondly, could you please guide me so as to what the exact sqwrl query
would be if we had multiple classes as i had stated in my original query.
What do you mean here? The query I provided does it for all individuals. I don't get your question. Did you try my SQWRL query?

_______________________________________________
protege-user mailing list
[hidden email]
https://mailman.stanford.edu/mailman/listinfo/protege-user