New Individuals with SWRL

14 views
Skip to first unread message

temp...@gmail.com

unread,
Feb 15, 2016, 8:47:14 AM2/15/16
to Stardog
Hi,
I'm trying create individual in SWRL rules:

[] a rule:SPARQLRule ; rule:content """

prefix : <urn:test:>

if {
    $person a :Person .

    $person :hasName $name .
    $person :hasAge $age .

    # age filter ...

    bind(uuid() as $newPerson)
}

then {
    $newPerson a :Person ;
        :hasName 'New Name' ;
        :hasAge 999 ;
}

""" .

I can add literals to new individuals? This rule create several unique id. I need one unique with literals. Thanks.

Michael Grove

unread,
Feb 15, 2016, 8:54:29 AM2/15/16
to stardog
Due to the way that rule is normalized, it's broken into several equivalent rules, which has the effect as you've noticed of creating multiple new individuals.

There's more in the documentation [1] about this behavior.

Cheers,

Mike

 

--
-- --
You received this message because you are subscribed to the C&P "Stardog" group.
To post to this group, send email to sta...@clarkparsia.com
To unsubscribe from this group, send email to
stardog+u...@clarkparsia.com
For more options, visit this group at
http://groups.google.com/a/clarkparsia.com/group/stardog?hl=en

temp...@gmail.com

unread,
Feb 15, 2016, 10:53:45 AM2/15/16
to Stardog
As I understand it, there is no way to make a single individuals?

понедельник, 15 февраля 2016 г., 16:54:29 UTC+3 пользователь Michael Grove написал:

Zachary Whitley

unread,
Feb 15, 2016, 10:58:51 AM2/15/16
to sta...@clarkparsia.com
There is. There is an example at the end of the documentation in the section on new individuals in user defined rules. You'll need to rewrite it into multiple rules.

---
You received this message because you are subscribed to the Google Groups "Stardog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stardog+u...@clarkparsia.com.

Kendall Clark

unread,
Feb 15, 2016, 11:05:44 AM2/15/16
to sta...@clarkparsia.com
As Zachary said, there's a section in the docs devoted to exactly this issue:

http://docs.stardog.com/#_new_individuals_with_swrl

Cheers,
Kendall

temp...@gmail.com

unread,
Feb 16, 2016, 3:56:53 AM2/16/16
to Stardog
What am I doing wrong?

My data.ttl:

PREFIX rule: <tag:stardog:api:rule:>

PREFIX : <http://test#>

rs:Rectangle rdf:type owl:ObjectProperty .
rs:width rdf:type owl:DatatypeProperty .
rs:height rdf:type owl:DatatypeProperty .

:c a :Circle ;
   :radius 10 .

:t a :Triangle ;
   :base 4 ;
   :height 10 .

:r a :Rectangle ;
   :width 5 ;
   :height 8 .

:s a :Rectangle ;
   :width 10 ;
   :height 10 .

[] a rule:SPARQLRule ;
   rule:content """
     PREFIX : <http://test#>
     PREFIX rs: <http://test/reasoner#>
     IF {
        ?r1 a :Rectangle .
        ?r1 :width ?w1 .
        ?r1 :height ?h1 .

        ?r2 a :Rectangle .
        ?r2 :width ?w2 .
        ?r2 :height ?h2 .

        filter(?w1 != ?w2) .

        bind(uuid() as ?newRectangle) .
        bind(?w1 * ?w2 as ?w3) .
        bind(?h1 * ?h2 as ?h3) .
     }
     THEN {
         ?newRectangle a rs:Rectangle .
     }""" .

[] a rule:SPARQLRule ;
   rule:content """
     PREFIX : <http://test#>
     PREFIX rs: <http://test/reasoner#>
     IF {
        ?r1 a :Rectangle .
        ?r1 :width ?w1 .
        ?r1 :height ?h1 .

        ?r2 a :Rectangle .
        ?r2 :width ?w2 .
        ?r2 :height ?h2 .

        filter(?w1 != ?w2) .

        bind(uuid() as ?newRectangle) .
        bind(?w1 * ?w2 as ?w3) .
        bind(?h1 * ?h2 as ?h3) .
     }
     THEN {
         ?newRectangle rs:width '?w3' . # ?w3 not working
     }""" .

I get different id (select * where {?s ?p ?o}):

urn:uuid:1d834fdf-35a7-4049-95da-a77f585eb7c6 rdf:type http://test/reasoner#Rectangle
urn:uuid:e22a9f59-6b7a-4a6e-9c9d-f8a48c89f47a http://test/reasoner#width ?w3

I want to get something like:

urn:uuid:1111  rdf:type  http://test/reasoner#Rectangle
urn:uuid:1111  http://test/reasoner#width 80

Zachary Whitley

unread,
Feb 16, 2016, 6:15:43 AM2/16/16
to sta...@clarkparsia.com
You're close. You've just got both rules creating new individuals. Drop calculating the width on the first one and the uuid on the second one. See [1] for an example 



--
-- --
You received this message because you are subscribed to the C&P "Stardog" group.
To post to this group, send email to sta...@clarkparsia.com
To unsubscribe from this group, send email to
stardog+u...@clarkparsia.com
For more options, visit this group at
http://groups.google.com/a/clarkparsia.com/group/stardog?hl=en

temp...@gmail.com

unread,
Feb 17, 2016, 3:19:33 AM2/17/16
to Stardog
What am I doing wrong? please explain, I do not understand :(
     THEN {
         ?newRectangle a rs:Rectangle .
     }""" .

[] a rule:SPARQLRule ;
   rule:content """
     PREFIX : <http://test#>
     PREFIX rs: <http://test/reasoner#>
     IF {
        ?r1 a :Rectangle .
        ?r1 :width ?w1 .
        ?r1 :height ?h1 .

        ?r2 a :Rectangle .
        ?r2 :width ?w2 .
        ?r2 :height ?h2 .

        filter(?w1 != ?w2) .

        # ?newRectangle a rs:Rectangle . // ???

        bind(?w1 * ?w2 as ?w3) .
        bind(?h1 * ?h2 as ?h3) .
     }
     THEN {
         ?newRectangle rs:width ?w3
     }""" .

Message has been deleted

Zachary Whitley

unread,
Feb 17, 2016, 7:36:11 PM2/17/16
to Stardog
On Wed, Feb 17, 2016 at 3:19 AM, <temp...@gmail.com> wrote:
What am I doing wrong? please explain, I do not understand :(

First your previous attempt had an additional mistake that I didn't point out since I thought it was just a typo. You had quoted the variable ?w3

     THEN {
         ?newRectangle rs:width '?w3' . # ?w3 not working
     }

which resulted in

urn:uuid:e22a9f59-6b7a-4a6e-9c9d-f8a48c89f47a http://test/reasoner#width ?w3

 making the width equal to the literal "?w3".


It looks like you've fixed that up that problem in your latest attempt below. It's a little difficult to figure out what exactly you're doing. It might be helpful if you were to explain exactly what you're trying to accomplish.

Again, this section probably shouldn't be here but it's difficult to tell exactly what you're looking to do here because you bind the width (?w3) and height (?h3) as the product of the first and the second? I'm not sure how to interpret what you're trying to do here.
 
        ?r2 a :Rectangle .
        ?r2 :width ?w2 .
        ?r2 :height ?h2 .

        filter(?w1 != ?w2) .

        # ?newRectangle a rs:Rectangle . // ???

        bind(?w1 * ?w2 as ?w3) .
        bind(?h1 * ?h2 as ?h3) .
     }
     THEN {
         ?newRectangle rs:width ?w3

This isn't going to do anything as you've never bound ?newRectangle. The bindings are scoped to the rule so you won't see the previous binding to ?newRectangle. If you want to target only the new rectangles that you previously created you'll need to make it something uniquely identifiable in the previous rule like ?newRectangle a rs:StrictRectangle
 
     }""" .



You're close. You've just got both rules creating new individuals. Drop calculating the width on the first one and the uuid on the second one. See [1] for an example 

 

--

temp...@gmail.com

unread,
Feb 18, 2016, 3:05:27 AM2/18/16
to Stardog
Thanks for the detailed answer.
I'm trying to find all the rectangles whose width varies, and create a new rectangle having width (?w1 * ?w2) and height (?h1 * ?h2).
But I get different UUIDs.
Is it possible to make one unique UUID for each rectangle?

urn:uuid:1111    rdf:type                       http://test/reasoner#Rectangle
urn:uuid:1111    http://test/reasoner#width     80
urn:uuid:1111    http://test/reasoner#height    50

urn:uuid:2222    rdf:type                       http://test/reasoner#Rectangle
urn:uuid:2222    http://test/reasoner#width     30
urn:uuid:2222    http://test/reasoner#height    40

Zachary Whitley

unread,
Feb 18, 2016, 10:38:58 AM2/18/16
to Stardog
I think I've come up with something that I believe should do what you're looking for but it isn't working as expected so someone will have to let me know what I'm missing. There seemed to be some namespacing issues so I cleaned them up and used the default stardog empty namespace.

PREFIX rule: <tag:stardog:api:rule:>
insert data {

rs:Rectangle a owl:Class .
rs:Circle a owl:Class .
rs:Triangle a owl:Class .
 
rs:width a owl:DatatypeProperty .
rs:height a owl:DatatypeProperty .
rs:newWidth a owl:DatatypeProperty .
rs:newHeight a owl:DatatypeProperty .
rs:radius a owl:DatatypeProperty .
rs:parentRectangle a owl:ObjectProperty .
 
:c a rs:Circle ;
   rs:radius 10 .

:t a rs:Triangle ;
   rs:base 4 ;
   rs:height 10 .

:r a rs:Rectangle ;
   rs:width 5 ;
   rs:height 8 .

:s a rs:Rectangle ;
   rs:width 10 ;
   rs:height 10 .


[] a rule:SPARQLRule ;
   rule:content """
     PREFIX rs: <http://test/reasoner#>
     IF {
        ?r1 a rs:Rectangle .
        ?r1 rs:width ?w1 .
        ?r1 rs:height ?h1 .

        ?r2 a rs:Rectangle .
        ?r2 rs:width ?w2 .
        ?r2 rs:height ?h2 .


        filter(?w1 != ?w2) .

        bind(uuid() as ?newRectangle) .
     }
     THEN {
         ?newRectangle rs:parentRectangle ?r1 .

     }""" .

[] a rule:SPARQLRule ;
   rule:content """
     PREFIX rs: <http://test/reasoner#>
     IF {
        ?r1 rs:parentRectangle ?pr .
        ?pr rs:width ?w1 ;
            rs:height ?h1 .

        ?r2 a rs:Rectangle ;
            rs:width ?w2 ;
            rs:height ?h2 .

        filter(?w1 != ?w2) .

        bind(?w1 * ?w2 as ?w3) .
        bind(?h1 * ?h2 as ?h3) .
     }
     THEN {
         ?r1 rs:newWidth ?w3 ;
             rs:newHeight ?h3 .
     }""" .
  }

 The problem is I'm getting the two new individuals but I'm not seeing the results from the second rule. If I execute just the rule body I get the expected bindings and I don't see anything in the logs about problems.

PREFIX rs: <http://test/reasoner#>

select * {
?r1 rs:parentRectangle ?pr .
?pr rs:width ?w1 ;
    rs:height ?h1 .

        ?r2 a rs:Rectangle ;
            rs:width ?w2 ;
            rs:height ?h2 .

        filter(?w1 != ?w2) .

        bind(?w1 * ?w2 as ?w3) .
        bind(?h1 * ?h2 as ?h3) .
     }"
+-----------------------------------------------+-------+-------+-------+-------+-------+-------+-------+-------+
|                      r1                       |  pr   |  w1   |  h1   |  r2   |  w2   |  h2   |  w3   |  h3   |
+-----------------------------------------------+-------+-------+-------+-------+-------+-------+-------+-------+
| urn:uuid:4af434d5-bdfe-448f-89e1-353ce054b749 | :r    | 5     | 8     | :s    | 10    | 10    | 50    | 80    |
| urn:uuid:dbf248f6-a1ed-43b6-8da2-d0a442b76306 | :s    | 10    | 10    | :r    | 5     | 8     | 50    | 80    |
+-----------------------------------------------+-------+-------+-------+-------+-------+-------+-------+-------+



Evren Sirin

unread,
Feb 18, 2016, 10:42:41 AM2/18/16
to Stardog
There is no way to control the UUIDs generated at this point. Also
note that your description sounds exactly what the docs warn about
non-termination and "shoot[ing] yourselves in the foot if you aren’t
very careful." [1] If two rectangles create a third rectangle than
that third rectangle would match the rule body along with first two
rectangles to create two more rectangles and so on. But Stardog's
limitation on cycles [2] would exclude your rule as it is written now
before this non-termination occurs. I see Zachary just sent a version
of the rules that work around these two problems but I'm wondering
maybe what you want is not a reasoning rule but an INSERT/WHERE query
that you'd run once to create new rectangles. In the INSERT/WHERE
query you can generate URIs in any way you want.

Best,
Evren

[1] http://docs.stardog.com/#_new_individuals_with_swrl
[2] http://docs.stardog.com/#_rule_limitations_gotchas

On Thu, Feb 18, 2016 at 3:05 AM, <temp...@gmail.com> wrote:

Zachary Whitley

unread,
Feb 18, 2016, 11:39:13 AM2/18/16
to Stardog
Evren, any thoughts on why the version I came up with isn't working? I agree that INSERT/WHERE would be a better solution but it made for a nice little challenge and learning experience to try to get it to work.

---

Zachary Whitley

unread,
Feb 18, 2016, 4:11:22 PM2/18/16
to Stardog
Looks like you just wait 4 hrs. for a new release. Amazing turn around time :) It looks like it's returning the expected results for the second rule in 4.0.5  although there is the issue of unstable UUIDs that you identified earlier.

I did notice that when you have reasoning turned on you don't see any of the triples associated with a rule:SPARQLRule, not that I can think of a reason why you'd care but I thought I'd mention it.

temp...@gmail.com

unread,
Feb 24, 2016, 8:48:21 AM2/24/16
to Stardog
Thanks all for answers.
If something changes with a unique UUID notify me please.

temp...@gmail.com

unread,
Feb 25, 2016, 4:30:29 AM2/25/16
to Stardog, temp...@gmail.com
I tried the other way. I'm trying to create a new URI and it should help me.
But the problem that I encountered is an empty subject.
In Stardog 4.0.5 problem with empty predicates fixed, but i see empty subject.

prefix rule: <tag:stardog:api:rule:>

insert data {

:r a rs:Rectangle ;
   rs:width 5 ;
   rs:height 8 .

:s a rs:Rectangle ;
   rs:width 10 ;
   rs:height 10 .

[] a rule:SPARQLRule ;
   rule:content """
PREFIX rs: <http://test/reasoner#>
PREFIX test: <http://test/test#>
     IF {
        ?r1 a rs:Rectangle .
        ?r1 rs:width ?w1 .
        ?r1 rs:height ?h1 .

        ?r2 a rs:Rectangle .
        ?r2 rs:width ?w2 .
        ?r2 rs:height ?h2 .

        filter(?w1 != ?w2) .
   
        bind(?w1 * ?w2 as ?w3) .
        bind(?h1 * ?h2 as ?h3) .

        bind(uri(concat('newRectangle', md5(concat(str(?r1), str(?r2))))) as ?newRectangle) .
     }
     THEN {
?newRectangle a rs:newRectangle .
         ?newRectangle rs:newWidth ?w3 ;
             rs:newHeight ?h3 .

     }""" .
}

Evren Sirin

unread,
Feb 26, 2016, 11:38:17 AM2/26/16
to Stardog, temp...@gmail.com
This would not create a valid URI. A minimal valid URI would be in the form "scheme:path" like "urn:example".

Best,
Evren

 
     }
     THEN {
?newRectangle a rs:newRectangle .
         ?newRectangle rs:newWidth ?w3 ;
             rs:newHeight ?h3 .

     }""" .
}

Query select * where {?s ?p ?o} return:

This is a bug?

--

temp...@gmail.com

unread,
Feb 27, 2016, 1:28:09 PM2/27/16
to Stardog, temp...@gmail.com
I have tried several ways:

bind(uri(concat('urn:example:newRectangle', md5(concat(str(?r1), str(?r2))))) as ?newRectangle)
bind(uri(concat('http://test/reasoner#newRectangle', md5(concat(str(?r1), str(?r2))))) as ?newRectangle)

Do not work for me, field subject is empty.

Zachary Whitley

unread,
Feb 29, 2016, 9:50:05 AM2/29/16
to Stardog
Can confirm that I'm getting the same behavior described on Stardog 4.0.5

Here's a complete example that replicates the problem

prefix rs: <http://test/reasoner#>
prefix rule: <tag:stardog:api:rule:>

insert data {

:r a rs:Rectangle ;
   rs:width 5 ;
   rs:height 8 .

:s a rs:Rectangle ;
   rs:width 10 ;
   rs:height 10 .

[] a rule:SPARQLRule ;
   rule:content """
    PREFIX rs: <http://test/reasoner#>
    PREFIX test: <http://test/test#>
     IF {
        ?r1 a rs:Rectangle .
        ?r1 rs:width ?w1 .
        ?r1 rs:height ?h1 .

        ?r2 a rs:Rectangle .
        ?r2 rs:width ?w2 .
        ?r2 rs:height ?h2 .

        filter(?w1 != ?w2) .
  
        bind(?w1 * ?w2 as ?w3) .
        bind(?h1 * ?h2 as ?h3) .
        bind(uri(concat('http://test/reasoner#newRectangle', md5(concat(str(?r1), str(?r2))))) as ?newRectangle)
     }
     THEN {
        ?newRectangle a rs:newRectangle .
         ?newRectangle rs:newWidth ?w3 ;
             rs:newHeight ?h3 .

     }""" .
}


Adding definitions for rs:NewRectangle, rs:newWidth, and rs:newHeight didn't change the results

rs:newRectangle a owl:Class .

rs:newWidth a owl:DatatypeProperty .
rs:newHeight a owl:DatatypeProperty .


Nothing to note in the logs.

--
-- --
You received this message because you are subscribed to the C&P "Stardog" group.
To post to this group, send email to sta...@clarkparsia.com
To unsubscribe from this group, send email to
stardog+u...@clarkparsia.com
For more options, visit this group at
http://groups.google.com/a/clarkparsia.com/group/stardog?hl=en
---
Reply all
Reply to author
Forward
0 new messages