1

Can I restrict insert of data in graphdb based on cardinality rules defined in my ontology.

I loaded the following ontology in a graphdb repository with "owl-max" Ruleset. Based on discussion here. I am trying to restrict that a person can have only one "age" property.

@prefix :      <http://stackoverflow.com/q/24188632/1281433/people-have-exactly-one-age#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .

:Person  a               owl:Class ;
        rdfs:subClassOf  [ a                owl:Restriction ;
                           owl:cardinality  "1"^^xsd:nonNegativeInteger ;
                           owl:onProperty   :hasAge
                         ] .

<http://stackoverflow.com/q/24188632/1281433/people-have-exactly-one-age>
        a       owl:Ontology .

:hasAge  a      owl:DatatypeProperty .

I now insert a person record as below

prefix :      <http://stackoverflow.com/q/24188632/1281433/people-have-exactly-one-age#>
prefix data: <http://data.example.com/>

Insert DATA {
    data:dow a :Person ;
               :hasAge 26 .
}

The next time i insert an updated age for that person,

Insert DATA {
    data:dow :hasAge 27 .
}

I expected an that age 27 triple overrides age 26 triple or I get an insert error. However both ages are stored for the person.

data:dow a <http://stackoverflow.com/q/24188632/1281433/people-have-exactly-one-age#Person> ;
    <http://stackoverflow.com/q/24188632/1281433/people-have-exactly-one-age#hasAge> "26"^^xsd:integer , "27"^^xsd:integer .
Trace Log
  • 47
  • 5
  • that's not how max-cardinality in general works. in OWL there is no such concept of *constraint* like in e.g. databases. OWL is used for *inference*. On the other hand, for literals which are by default distinct from each other, your case should lead to a logical inconsistency. Did you enable the [check-for-inconsistency param](http://graphdb.ontotext.com/documentation/standard/configuring-a-repository.html#configuring-a-repository-configuration-parameters-check-for-inconsistencies) in your repository? – UninformedUser Jan 21 '19 at 04:03
  • Hi @AKSW , I enabled the check-for-inconsistency in a fresh repository and tried the ontology and multiple inserts with different value for "age" dataproperty. However still no error during the insert statement. – Trace Log Jan 21 '19 at 05:08
  • ok, so the question is whether the inconsistency rule exists in the OWL max profile. According to [this](http://graphdb.ontotext.com/documentation/standard/owl-compliance.html), the answer seems to be "no". But according to [here](http://graphdb.ontotext.com/documentation/standard/reasoning.html) it follows OWL RL semantics for inconsistency checks, but I guess it doesn't hold for the owl-max profile. Can you try to add a custom rule for the inconsistency check of the property age please? – UninformedUser Jan 21 '19 at 06:25

2 Answers2

3

disclaimer: This is not meant to be a proper nor correct answer, I just used it because formatting in comment is weird.

Not sure whether owl-max profile supports the inconsistency rule you'd need here. As a workaround, you could at least try to add a custom rule:

PREFIX sys: <http://www.ontotext.com/owlim/system#>
INSERT DATA {
    <_:custom> sys:addRuleset
        '''Prefices { 
                    x : http://stackoverflow.com/q/24188632/1281433/people-have-exactly-one-age#
           }
           Axioms {}
           Rules
           {
           Consistency: max_one_age_value
              a <x:hasAge> b
              a <x:hasAge> c [Constraint b != c]
              -----------------------
           }'''
}
UninformedUser
  • 8,397
  • 1
  • 14
  • 23
  • I tried this custom rule, however still no error when inserting multiple "age" for a person. I guess the workarround for me is to manually delete all "age" data for the user before adding new records. – Trace Log Jan 21 '19 at 16:44
  • yeah, this would indeed be possible via `DELETE { a b c } INSERT { x y z } WHERE { ... }` query. But strange that it doesn't work. Did you also change to the custom ruleset after additon? I guess one has to set the default ruleset via `INSERT DATA { _:b sys:defaultRuleset "custom" }` - I'll try it on my computer later, probably I did something wrong here. – UninformedUser Jan 22 '19 at 11:23
  • @TraceLog ok, I figured why it didn't work. For historical reasons, the URLs in `Prefices` section have to be defined without brackets...now it works, I checked it locally. Can you confirm? – UninformedUser Jan 22 '19 at 13:11
  • Thanks @AKSW , with your latest changes to the custom rule and setting the default ruleset to custom , it works. I get an error trying to insert inconsistent data. It is disappointing though that graphdb ```owl-max``` does not include this rule and have confusing [documentation](http://graphdb.ontotext.com/documentation/standard/reasoning.html?highlight=owl%20max#predefined-rulesets) – Trace Log Jan 23 '19 at 02:14
0

Thanks to @aksw for the excellent answers. According to http://graphdb.ontotext.com/documentation/standard/reasoning.html#predefined-rulesets, owl-max and owl-rl should do what was asked.

I want to add a clarification: @trace-log, there is no implicit "overwrite " operation in SPARQL, and it doesn't matter what rule set you use. You must use the DELETE...INSERT... statement to update triples in the way you describe.

Vladimir Alexiev
  • 2,477
  • 1
  • 20
  • 31