如何在GraphDB中正确调用自定义推理规则?
需求背景
需要实现推理功能:当规则匹配时,将主语V(Valeur)与宾语TM(Type_Mesure)建立capt:Classifie_Metier关联。
原SWRL规则(Protege中编写)
使用三个本体:Profil_V2、Modele_V2和Capteur_V2,原规则如下:
modele:Modelisé_Avec(?MQ,?TM) ^ modele:Antecedent(?MS, ?MQ) ^ profil:A_Pour_Modèle_Système(?S, ?MS) ^ profil:A_Pour_Comportement(?MS, ?GC) ^ profil:A_Pour_Membre(?GC, ?C) ^ capt:A_Pour_Echantillon(?C, ?E) ^ capt:A_Pour_Valeur(?E,?V) ^ -> capt:Classifie_Metier(?V, ?TM)
GraphDB规则转换代码
由于GraphDB不直接支持SWRL,转换为GraphDB原生规则语言:
PREFIX sys: <www.ontotext.com/owlim/system#> PREFIX rdf: <www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX modele: <srv-rd-vm/ontology/Modele_V2#> PREFIX profil: <srv-rd-vm/ontology/Profil_V2#> PREFIX capt: <srv-rd-vm/ontology/Capteur_V2#> INSERT DATA { <_:profil_classification_Metier> sys:addRuleset '''Prefices { profil: <srv-rd-vm/ontology/Profil_V2#> modele: <srv-rd-vm/ontology/Modele_V2#> capt: <srv-rd-vm/ontology/Capteur_V2#> rdf: <www.w3.org/1999/02/22-rdf-syntax-ns#> } Axioms {} Rules { Id: profil_classification_Metier MQ <rdf:Type> <modele:MODELE_QUALIFICATION> MQ <modele:Modelisé_Avec> TM MS <modele:Antecedent> MQ S <profil:A_Pour_Modèle_Système> MS MS <profil:A_Pour_Comportement> GC GC <profil:A_Pour_Membre> C C <capt:A_Pour_Echantillon> E E <capt:A_Pour_Valeur> V ------------------------------- V <capt:Classifie_Metier> TM }''' }
当前调用方式及问题
当前插入数据时尝试触发规则的代码:
PREFIX capt: <http://srv-rd-vm/ontology/Capteur_V2#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX profil: <http://srv-rd-vm/ontology/Profil_V2#> INSERT { [] <http://www.ontotext.com/owlim/system#Test> [] . capt:test rdf:type capt:VALEUR . capt:test capt:Nom 'test'^^xsd:string . capt:test capt:Valeur '60.5042'^^xsd:float . capt:echantillon capt:A_Pour_Valeur capt:test . capt:echantillon rdf:type capt:ECHANTILLON . capt:echantillon capt:Nom 'echantillon'^^xsd:string . capt:echantillon capt:Date '1695717396'^^xsd:dateTime . profil:comportement capt:A_Pour_Echantillon capt:echantillon. profil:comportement rdf:type profil:COMPORTEMENT . profil:comportement profil:Nom 'comportement'^^xsd:string . profil:9e7b483e_cec8_4ce7_8ae2_6e693859a043 profil:A_Pour_Membre profil:comportement . } WHERE { };
问题:调用后生成了与规则同名的谓词及空白节点,但将规则设为默认规则时可正常工作,需明确自定义规则的正确调用方式。
解决方案
1. 移除错误的触发语句
当前插入语句中的[] <http://www.ontotext.com/owlim/system#Test> [] .是无效用法,sys:Test并非用于触发自定义规则,反而会生成无关的空白节点和谓词,直接删除该行即可。
2. 激活自定义规则集的两种方式
GraphDB的推理是数据驱动的,只要规则集激活且数据匹配前提,就会自动执行推理,无需手动触发。
方式一:加入默认推理配置(推荐)
在GraphDB工作台进入仓库的推理设置,将自定义规则集profil_classification_Metier添加到当前使用的推理配置中(可追加到现有规则集后),保存后所有数据插入/更新都会自动触发该规则。
方式二:动态切换规则集(临时场景)
若需临时触发该规则,可在插入数据前执行以下SPARQL切换规则集:
PREFIX sys: <http://www.ontotext.com/owlim/system#> INSERT DATA { [] sys:setRuleset "profil_classification_Metier" . }
插入完成后,若需恢复原有规则集,执行:
PREFIX sys: <http://www.ontotext.com/owlim/system#> INSERT DATA { [] sys:setRuleset "原有规则集名称" . }
注意:sys:setRuleset会替换当前规则集,而非追加。
3. 补全规则前提数据
当前插入的数据缺少规则前提中的关键节点(如modele:MODELE_QUALIFICATION类型的MQ、TM、MS、S等),导致规则无法匹配。需要补全完整的关联链数据,规则才能生成预期的capt:Classifie_Metier关联。
内容的提问来源于stack exchange,提问作者Yohann

