Property Auto-increment

90 views
Skip to first unread message

majaCap

unread,
Mar 17, 2021, 8:09:12 AM3/17/21
to TopBraid Suite Users
Hi,

I have an int property in my ontology. Now, I would like to increment that property each time a new instance of a class is created, starting from a particular number. How would I approach this in EDG? Let's say the class instance is Animal with property autoIncrementID. Each time I create a new Animal I would like the autoIncrementID to be incremented by 1.

Br,
Maja

David Price

unread,
Mar 17, 2021, 8:49:36 AM3/17/21
to topbrai...@googlegroups.com
Hi Maja,

There may be other ways, but you can do this kind of thing with an EDG Edit Rule as code. An Edit Rule can access the changes graph via a special graph called ui:addedGraph (ui:deletedGraph is also available) and find new instances being created and then search the current graph data for the max existing value and just add one. Something like this should be close and search for that property in any new instance of any class:

property-rules:Set_autoIncrementID
  a teamwork:EditRule ;
  teamwork:ruleMayUpdate true ;
  ui:prototype """
<ui:forEach ui:resultSet=\"{#
        SELECT DISTINCT ?i ?class
        WHERE {
            GRAPH ui:addedGraph {
                ?i a ?class .
                FILTER NOT EXISTS {
                    ?i myschema:autoIncrementID ?currentvalue .
                } .
            } .
        } }\">
    <ui:update ui:updateQuery=\"{!
            INSERT {
                ?i myschema:autoIncrementID ?nextvalue .
            }
            WHERE {
                GRAPH ui:addedGraph {
                    ?i a ?class .
                } .
                {
                    SELECT (MAX(?num) AS ?maxvalue)
                    WHERE {
                        ?noti a ?class .
                        ?noti myschema:autoIncrementID ?id .
                        BIND (ceil(xsd:decimal(?id)) AS ?num) .
                        FILTER bound(?num) .
                    }
                } .
                BIND (COALESCE((xsd:integer(?maxvalue) + 1), 1) AS ?nextvalue) .
            } }\"/>
</ui:forEach>
"""^^ui:Literal ;
  rdfs:comment "When creating a new thing set the autoIncrementID property value to be the next highest interger value" ;
  rdfs:label "Default autoIncrementID" ;
  rdfs:subClassOf teamwork:EditRules ;
.

Cheers,
David


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/topbraid-users/01759423-765a-4084-a991-2caa46eb49e8n%40googlegroups.com.


Irene Polikoff

unread,
Mar 17, 2021, 5:56:32 PM3/17/21
to topbrai...@googlegroups.com
Another approach is to use a property value rule to automatically calculate the autoIncrementID

It sounds like you want to get a count of all instances plus some number.

What is autoIncrementID a property of? 

Is it a property that each instance of a class has, so that for each instance you can get the count of ll instances of a class plus a number ? 

Then, you would define it for the class Animal and create a property value rule as follows




Select Count number of inverse property values


You will get this rule if you look at the source code

g:Animal-autoIncrementID
  a sh:PropertyShape ;
  sh:path g:autoIncrementID ;
  sh:datatype xsd:integer ;
  sh:name "autoIncrementID" ;
  sh:nodeKind sh:Literal ;
  sh:values [
      sh:count [
          sh:path [
              sh:inversePath rdf:type ;
            ] ;
        ] ;
    ] ;
.

Update it because the path you want to use for the count is from an instance to the class and then from the class to all instances via inverse  i.e., rdf:type/^rdf:type

g:Animal-autoIncrementID
  a sh:PropertyShape ;
  sh:path g:autoIncrementID ;
  sh:datatype xsd:integer ;
  sh:name "autoIncrementID" ;
  sh:nodeKind sh:Literal ;
  sh:values [
      sh:count [
          sh:path (
              rdf:type
              [
                sh:inversePath rdf:type ;
              ]
            ) ;
        ] ;
    ] ;
.

Now, when you access any instance of Animal, you can get a count of all Animals



If you want to add some number to the count, use sparql:add function in your rule - http://datashapes.org/sparql

You can also associate the property with the class itself, then you do not need to change the rule that will be generated by the template. 

To associate a property with a class, switch the class hierarchy root to Resource, find owl:Class in the tree and define the property and a rule for it.


Then, you will get the generated autoIncrementID value for every class in your ontology



You can also define a property for all classes to store the number you want to add to the count. The value of this property could be different for different classes.

The above approach for adding a property to a class is for 7.0. If you are using 6.4, use the following approach to add properties directly to a class: 


Holger Knublauch

unread,
Mar 17, 2021, 6:26:44 PM3/17/21
to topbrai...@googlegroups.com

This is in fact how the built-in URI generation policy "counter" is implemented. Open teamwork.ui.ttlx and go to

teamwork:IncrementCounterEditRule

The main difference is that we store this "counter" in the TCH graphs.

Holger

David Price

unread,
Mar 18, 2021, 8:56:59 AM3/18/21
to topbrai...@googlegroups.com

On 17 Mar 2021, at 21:56, Irene Polikoff <ir...@topquadrant.com> wrote:

Another approach is to use a property value rule to automatically calculate the autoIncrementID

It sounds like you want to get a count of all instances plus some number.

Hi Maja,

I’m not sure the above is what you mean by “I would like to increment that property each time a new instance of a class is created, starting from a particular number”. 

Either way, everyone should take care to understand the difference between "max of existing value" vs. "count of instances” logic. There is no difference in some scenarios, but they are not identical if your scenario allows people to delete instances. 

Example: 

Create 10 instances - the next Create integer would be 11 if no other change happens and at this moment max and count produce identical result.

Delete 5 instances.

Now:

- “count instances” approach would say 6 is the next available integer

- “max current value” approach could return anything from 6 to 11 depending on exactly which instances got deleted

Just suggesting to do a deep analysis of your reuirements before deciding which logic will work in all your user scenarios - and then do a lot of testing.

Cheers,
David


What is autoIncrementID a property of? 

Is it a property that each instance of a class has, so that for each instance you can get the count of ll instances of a class plus a number ? 

Then, you would define it for the class Animal and create a property value rule as follows



<PastedGraphic-65.png>

Select Count number of inverse property values

<PastedGraphic-67.png>
<PastedGraphic-70.png>


If you want to add some number to the count, use sparql:add function in your rule - http://datashapes.org/sparql

You can also associate the property with the class itself, then you do not need to change the rule that will be generated by the template. 

To associate a property with a class, switch the class hierarchy root to Resource, find owl:Class in the tree and define the property and a rule for it.

<PastedGraphic-68.png>

Then, you will get the generated autoIncrementID value for every class in your ontology

<PastedGraphic-69.png>

Irene Polikoff

unread,
Mar 18, 2021, 9:47:44 AM3/18/21
to topbrai...@googlegroups.com
Yes, a good point David.

majaCap

unread,
Mar 18, 2021, 12:07:49 PM3/18/21
to TopBraid Suite Users
Thank you all for support. I will try to explain it clearer. The  autoincrementID is a property that will refer to another system - and it will be  an ID in another system, therefore this is not a count of current instances. So there is an existing system that has some Animal instances and each of Animal has this autoincrementID property. Now I built another system also with Animals but containing  much more properties that I want to connect to the first system based on this autoincrementID property. Therefore whenever I add a new Animal to my data graph I also need to add the autoincrementID continuing the id count from another system. So let's say that I add to my data graph an Animal which will have URI Animal10254  and the last value of autoincrementID from another system is 959. Then my Animal10254 needs to have autoincrementID == 960. Does this makes  more sense?

Kind regards,
Maja

Irene Polikoff

unread,
Mar 18, 2021, 12:25:05 PM3/18/21
to topbrai...@googlegroups.com
I am confused:

  • You are creating new instances
  • Is this other system also continues to add new instances or is its set of instances complete?

In the former case:

When a new instance is created in this other system, how does it know that you already created Animal10254 and gave it autoincrementID == 960? 

It would seem that when the other system creates a new animal, it could be a different animal from the one you created. Yet, they will both have the same autoincrementID. Further, if you rely on querying this other system for the max autoincrementID, when you create another animal, after creating Animal10254 and you query the other system, it may not have added a new animal yet and it would again return 959 as the last id used.

In other words, if you have 2 systems that add new instances and you want them to use a common id counter, you need to have a common component for assigning it. There should be a service you could access that will give you the next available autoincrementID. And this other system should use the same service so that when it creates a new animal, it knows that you already used 960.

Provided that such service is available, you need to call it from EDG to get the autoincrementID.

In the latter case:

If all instances in the other system are already created and only you are adding new instances, then you could just store the max ID from the other system and can increment from this number.

majaCap

unread,
Mar 18, 2021, 12:31:12 PM3/18/21
to TopBraid Suite Users
Hi Irene,

It's the latter case. I am only interested in my system and continuing to increment the ID only in my system. We are not interested in the other system at all. Do you have any tips how to implement your suggestion i.e. storing the max ID from the other system and incrementing from this number?

Br,
Maja

Irene Polikoff

unread,
Mar 18, 2021, 1:15:59 PM3/18/21
to topbrai...@googlegroups.com
Whatever way you want. 

You could, for example, store it at the class level e.g.

ex:Animal ex:autoincrementStart 959.

With the approach David outlined, you only need this for the first instance you create. After that, you increment from the max number. So, you could also simply create the first instance and manually enter its autocrementID that takes into account offset. 
Or you could, within David’s approach, include logic that will use this fixed number (either hard coded in the code or stored at the class level) if no instances exist yet or will increment the max available ID if there are instances.

As Holger mentioned, another option is to set your URI generation to the counter method. This lets you put in a starting number - counter offset. URI construction rules are available on creation of an asset collection and, if you forget to set it on creation, you can later set it on the Manage tab



With this, TopBraid EDG will take care of auto incrementing. In your example, you would have ex:Animal960 as the URI. If you also want to have a separate property with 960 value, you could then use a property value rule to derive it from the URI.


Maja Kiszka

unread,
Mar 22, 2021, 11:30:01 AM3/22/21
to topbrai...@googlegroups.com
I think a solution that will work 100% for my problem is finding the current highest value for autoincrementID and whenever a new Animal is created just add 1 to it.
How would I deploy this in EDG? I do not see where in EGD I can add EDG Edit Rule like David suggested.

Thanks,
Maja


You received this message because you are subscribed to a topic in the Google Groups "TopBraid Suite Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/topbraid-users/mmBWNb72dgc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to topbraid-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/topbraid-users/BE4706BE-3976-4D1A-8D4E-8882CC4315A2%40topquadrant.com.

Irene Polikoff

unread,
Mar 22, 2021, 11:42:20 AM3/22/21
to topbrai...@googlegroups.com
David’s example is SWP code

https://www.topquadrant.com/technology/sparql-web-pages-swp/

You would need to create SWP file:

either using Files feature in EDG 7.0 - create a new workspace folder and then create a new file in that folder. Use ui.ttlx extension




or using TBC - create a new project for your file, create RDF/SWP file




If you are running EDG on the server, you should then upload the project with the new file created in TBC to the server. Otherwise, you do not need to do anything since local EDG shares a workspace with TBC.

On Mar 22, 2021, at 11:29 AM, Maja Kiszka <kiszk...@gmail.com> wrote:

I think a solution that will work 100% for my problem is finding the current highest value for autoincrementID and whenever a new Animal is created just add 1 to it.
How would I deploy this in EDG? I do not see where in EGD I can add EDG Edit Rule like David suggested.

Thanks,
Maja


tor. 18. mar. 2021 kl. 18:16 skrev Irene Polikoff <ir...@topquadrant.com>:
Whatever way you want. 

You could, for example, store it at the class level e.g.

ex:Animal ex:autoincrementStart 959.

With the approach David outlined, you only need this for the first instance you create. After that, you increment from the max number. So, you could also simply create the first instance and manually enter its autocrementID that takes into account offset. 
Or you could, within David’s approach, include logic that will use this fixed number (either hard coded in the code or stored at the class level) if no instances exist yet or will increment the max available ID if there are instances.

As Holger mentioned, another option is to set your URI generation to the counter method. This lets you put in a starting number - counter offset. URI construction rules are available on creation of an asset collection and, if you forget to set it on creation, you can later set it on the Manage tab


<PastedGraphic-71.png>
Reply all
Reply to author
Forward
0 new messages