我可以根据我的本体中定义的基数规则限制在graphdb中插入数据。
我在带有“owl-max”规则集的graphdb存储库中加载了以下本体。根据讨论here。我试图限制一个人只能拥有一个“年龄”财产。
@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 .
我现在插入一个人记录如下
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 .
}
下次我为该人插入更新年龄时,
Insert DATA {
data:dow :hasAge 27 .
}
我期望27岁的三重超过26岁三倍或我得到一个插入错误。然而,这两个年龄都是为这个人存储的。
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 .
免责声明:这不是一个正确的答案,我只是使用它,因为评论格式很奇怪。
不确定owl-max
配置文件是否支持您在此处需要的不一致性规则。作为解决方法,您至少可以尝试添加自定义规则:
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]
-----------------------
}'''
}
感谢@aksw的优秀答案。根据http://graphdb.ontotext.com/documentation/standard/reasoning.html#predefined-rulesets的说法,owl-max和owl-rl应该按照要求做。
我想补充说明:@ trace-log,SPARQL中没有隐式的“覆盖”操作,并且您使用的规则集无关紧要。您必须使用DELETE ... INSERT ...语句以您描述的方式更新三元组。