Use of sh:update in SHACL statement

82 views
Skip to first unread message

Michael Johnson

unread,
Feb 20, 2017, 4:04:52 PM2/20/17
to TopBraid Suite Users
I am attempting to insert a triple when a particular SHACL violation occurs.  It seems as if I would use the sh:update, but I can't seem to get it right.  
I have a Person class that has a property Comment.  I can easily update the Person by
INSERT {
?s ex:Comment "inserted Comments" .
}
WHERE {
?s rdf:type ex:Person . 

I have tried to add the sh:update as a property constraint:

ex:Person
  rdf:type rdfs:Class ;
  rdf:type sh:Shape ;
  rdfs:label "Person" ;
  rdfs:subClassOf rdfs:Resource ;
  sh:property [
      sh:predicate ex:Comment ;
      sh:datatype xsd:string ;
      sh:minCount 1 ;
      sh:update """PREFIX ex: <http://example.com#>
INSERT {
?s ex:Comment \"inserted Comments\" .
}
WHERE {
?s rdf:type ex:Person . }""" ;
    ] ;
  sh:property [
      sh:predicate ex:firstName ;
      sh:datatype xsd:string ;
      sh:minCount 1 ;
      sh:name "first name" ;
    ] ;
  sh:property [
      sh:predicate ex:gender ;
      sh:class ex:Gender ;
      sh:description "A Person's gender" ;
      sh:maxCount 1 ;
      sh:minCount 1 ;
      sh:name "gender" ;
    ] ;
  sh:property [
      sh:predicate ex:lastName ;
      sh:datatype xsd:string ;
      sh:description "last name " ;
      sh:maxCount 1 ;
      sh:minCount 1 ;
      sh:name "last name" ;
    ] ;
  sh:property [
      sh:predicate ex:mother ;
      sh:class ex:Person ;
      sh:description "A Person's mother";
      sh:maxCount 1 ;
      sh:name "mother" ;
      sh:nodeKind sh:IRI ;
      sh:shape ex:FemaleShape ;
    ] ;
  sh:update """INSERT {
?s ex:Comment \"inserted Comments\" .
}
WHERE {
?s rdf:type ex:Person . }""" ;
.



ex:Person
  rdf:type rdfs:Class ;
  rdf:type sh:Shape ;
  rdfs:label "Person" ;
  rdfs:subClassOf rdfs:Resource ;
  sh:property [
      sh:predicate ex:Comment ;
      sh:datatype xsd:string ;
      sh:minCount 1 ;
      sh:update """INSERT {
?s ex:Comment \"inserted Comments\" .
}
WHERE {
?s rdf:type ex:Person . }""" ;
    ] ;
  sh:property [
      sh:predicate ex:firstName ;
      sh:datatype xsd:string ;
      sh:minCount 1 ;
      sh:name "first name" ;
    ] ;
  sh:property [
      sh:predicate ex:gender ;
      sh:class ex:Gender ;
      sh:description "A Person's gender" ;
      sh:maxCount 1 ;
      sh:minCount 1 ;
      sh:name "gender" ;
    ] ;
  sh:property [
      sh:predicate ex:lastName ;
      sh:datatype xsd:string ;
      sh:description "last name " ;
      sh:maxCount 1 ;
      sh:minCount 1 ;
      sh:name "last name" ;
    ] ;
  sh:property [
      sh:predicate ex:mother ;
      sh:class ex:Person ;
      sh:description "A Person's mother " ;
      sh:maxCount 1 ;
      sh:name "mother" ;
      sh:nodeKind sh:IRI ;
      sh:shape ex:FemaleShape ;
    ] ;
.


Holger Knublauch

unread,
Feb 20, 2017, 5:46:53 PM2/20/17
to topbrai...@googlegroups.com
Hi Michael,

sh:update is by default not doing anything in SHACL. It is a property that can be used by extensions. One such extension is the DASH suggestions framework supported by TopBraid:

    http://datashapes.org/suggestions.html

Please take a look and let me know if this would help. It is currently limited to constraint components, i.e. it is not yet possible to define suggestions that would be for a specific constraint instance only.

Holger
--
You received this message because you are subscribed to the Google Group "TopBraid Suite Users", the topics of which include the TopBraid Suite family of products and its base technologies such as SPARQLMotion, SPARQL Web Pages and SPIN.
To post to this group, send email to topbrai...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "TopBraid Suite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to topbraid-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Johnson

unread,
Mar 2, 2017, 10:18:26 AM3/2/17
to TopBraid Suite Users
Hey Holger.
Thanks for pointing me in the right direction.  
It seems as if the only suggestion that can be applied is to delete the offending triple, whereas I wish to add a triple.  Do you have any suggestions?

For example:  
:JohnDoe_1
  rdf:type :Person_1 ;
  :ssn "qweqeqweqweq" ;
.

This is a violation of the sh:MaxLengthConstraintComponent 

What if I want to insert a new triple:  
:Violations_1
  rdf:type :Violation ;
  :ssn "qweqeqweqweq" ;
.

Thanks so much for your time.

Michael

Holger Knublauch

unread,
Mar 3, 2017, 2:37:35 AM3/3/17
to topbrai...@googlegroups.com
That looks more like a use case for SPIN rules to me. It is not yet decided whether SHACL will have something similar.

Holger


Sent from my iPad

Michael Johnson

unread,
May 16, 2017, 10:09:26 AM5/16/17
to TopBraid Suite Users
Holger,
I have been reading the updated draft for SHACL (16 May 2017).  
It seems as if SHACL is migrating away from the use of CONSTRUCT.

My use case for CONSTRUCT is this:
1. It is revealed thru a SPARQL query that a number of conditions point to a particular type of event
2. An instance of class:event is created with the conditions of the SPARQL query as evidence (properties of) 
3. The presence of the event then creates another instance of class:recommendation

We have "parts" of this working.  We have SHACL that identifies the conditions.
We have SPIN that creates some instances.  However, we are trying to link the two together.  

I have attempted to use spin:constructor within SHACL (a bit of a shot in the dark...) with no success.  

1.  Do you have any recommendations how this would be done?
2.  Is it possible that the triples are "automagically" inserted after inference?

Thank you.
Michael

Irene Polikoff

unread,
May 16, 2017, 2:55:18 PM5/16/17
to topbrai...@googlegroups.com
Michael,

What SHACL document are you referring to?

In addition to the main document, there are several specifications that are being published as the Working Group Notes. 

Take a look at the full deliverables list here https://www.w3.org/2014/data-shapes/wiki/Main_Page

I believe your use cases would be supported by the SHACL Advanced Features spec http://w3c.github.io/data-shapes/shacl-asf/#rules-examples

Sent from my iPhone

Holger Knublauch

unread,
May 16, 2017, 6:51:11 PM5/16/17
to topbrai...@googlegroups.com
Hi Michael,

if you are referring to SHACL rules, they are part of the Advanced Features WG note. I worked on its implementation over the last month. Support for SPARQL CONSTRUCT queries is definitely there and will remain important. We did however also add a more declarative way of representing rules, for simpler and more predictable use cases. Full support of these features will be part of TopBraid 5.4, while the open source API has intermediate snapshots for those who are willing to use the lower levels.

On your specific scenario, I would be interested to hear what you are trying to do - please provide details.

Regards,
Holger

Michael Johnson

unread,
May 17, 2017, 10:56:51 AM5/17/17
to TopBraid Suite Users
Hey Holger,
We are receiving reports from a variety of domains.  All of these are ingested into the triple store.  We are looking for conditions/patterns that indicate an “event.”  
Once we identify that the conditions for a particular class of event have occurred, we want to create an instance of that class.  
Now that we have an instance of a particular type of event we will then determine what action needs to be taken.  
We have created several domain ontologies for this effort.  

We currently have SHACL statements that accurately find the data of concern.  We are using the sh:sparql as most of the queries tend to get fairly complex.  
With the exception of the violation report in TBC it really isn’t much more than a SPARQL query.  What we need to do is construct triples to be added to the graph.  
I have tried a variety of methods with little success.  I have started to incorporate SPIN as a method to create the triples, but seems that I should be able to do this with SHACL or at least call the SPIN rule and pass the data to create the triples.  

It seems as if the advanced features address what is needed. My understanding is that the Advanced Features will not be present until TBC 5.4.  

We are very pleased with the work you have done in SHACL and excited about the path forward.  

Thanks,
Michael

Michael Johnson

unread,
May 17, 2017, 11:01:18 AM5/17/17
to TopBraid Suite Users
Hi Irene,
Thanks for your reply.
Mistakenly, I was following the examples on the Advanced Features.  However, they are not yet implemented in TBC 5.3 and I have to wait patiently for TBC 5.4.
In this thread, I gave Holger a rough idea of our use case.  Basically find patterns and then create more triples based on those patterns.  
We have pieces working, but not the entire workflow. 
Michael

Holger Knublauch

unread,
May 17, 2017, 7:34:59 PM5/17/17
to topbrai...@googlegroups.com
Hi Michael,

thanks for the background. This sounds like Event-Condition-Action rules to me. I guess SHACL could be used to describe the pattern in the data for which the rules need to trigger, and the updates that need to happen. In SHACL Advanced you could use a custom target for that, to determine which focus nodes a rule needs to fire on. For example, assume that you want to update the status of ex:Customer to ex:GoldStatus once she has made more than $1000 in purchases:

ex:GoldStatusRuleShape
    a sh:NodeShape ;
    sh:target [
        a sh:SPARQLTarget ;
        sh:select """
            SELECT ?this
            WHERE {
                ?this a ex:Customer .
                ?this ex:purchasedValue ?value .
                FILTER (?value >= 1000) .
                FILTER NOT EXISTS { ?this ex:status ?any } .
            } """ ;
    sh:rule [
        a sh:TripleRule ;
        sh:subject sh:this ;
        sh:predicate ex:status ;
        sh:object ex:GoldStatus ;
    ] .

Alternatively, maybe you could use sh:condition...

ex:GoldStatusRuleShape
    a sh:NodeShape ;
    sh:targetClass ex:Customer ;   # Check for all customer instances
    sh:rule [
        a sh:TripleRule ;
        sh:subject sh:this ;
        sh:predicate ex:status ;
        sh:object ex:GoldStatus ;
        sh:condition [
            sh:property [   # Customer has no ex:status yet
                sh:path ex:status ;
                sh:maxCount 0 ;
            ] ;
            sh:property [   # and has purchased for more than $1000
                sh:path ex:purchasedValue ;
                sh:minInclusive 1000 ;
            ] ;
    ] .

There would need to be some outside "trigger" mechanism that monitors the data for changes and goes through all these rule shapes. I assume this is a feature that a platform such as TopBraid could provide.

Note that the conditions are worded in such a way so that they can only trigger once.

Does this look similar to what you are trying to do, or what else would be missing for your use case?

(And if you're interested, maybe we could make a pre-alpha release of 5.4 available so that you don't need to wait for half a year...)

Thanks,
Holger

Michael Johnson

unread,
May 18, 2017, 9:46:21 AM5/18/17
to TopBraid Suite Users
Holger,
That is exactly what I am looking for.  If we had a class of GoldStatusViolations (for example: those that don't keep a min balance...) and I wanted to create an instance for each...does this allow for multiple inserts.    I am assuming the sh:rule is a construct of sorts - similar to a spin rule.  
This looks to be the start of a migration or inclusion of spin into the larger SHACL hierarchy.  Very cool.  

Now comes the hard part:  making this demonstrable in about a month...

At this point, I believe that I am bound to using a SPIN/SHACL hybrid approach and once 5.4 is out in full release - I will be free to migrate.  

Dissecting your example a bit ....
Is it currently possible to call spin functions from within a SHACL statement?  

Something like:

ex:GoldStatusRuleShape
    a sh:NodeShape ;
    sh:target [
        a sh:SPARQLTarget ;
        sh:select """
            SELECT ?this
            WHERE {
                ?this a ex:Customer .
                ?this ex:purchasedValue ?value .
                FILTER (?value >= 1000) .
                FILTER NOT EXISTS { ?this ex:status ?any } .
            } """ ;
    spin:rule [
      rdf:type sp:Construct ;
      sp:text """
        CONSTRUCT {
            ?this ex:status ex:GoldStatus . 
        }
        """ ;
    ] .


Now I understand that this won't work because ?this is not bound to anything in the spin:rule, but if there were a way to bind it without restating the select in the SHACL....

Let me know what you think. 
I appreciate the dialogue.  It is helping keep things straight and giving me ideas of how I will incorporate rules of rules and what is "current" (for ex: if the current prime rate is < 4.0% and DJIA has not lost > 2%....change sh:minInclusive to 1200).  

Holger Knublauch

unread,
May 18, 2017, 6:41:01 PM5/18/17
to topbrai...@googlegroups.com
No, such mixing isn't going to work. But you can put the target select into the WHERE clause of the SPIN rule and would have the same effect.

Regards,
Holger
Reply all
Reply to author
Forward
0 new messages