规则与推理机

规则推理文章:
https://blog.csdn.net/lyunabc/article/details/84180872

本体所使用的SWRL只是规则语言的一种。
2.2 规则的定义和访问
过去大部分的规则引擎开发并没有规范化,有其自有的API,这使得其与外部程序交互集成不够灵活。转而使用另外一种产品时往往意味需要重写应用程序逻辑和 API调用,代价较大。规则引擎工业中标准的缺乏成为令人关注的重要方面。2003年11月定稿并于2004年8月最终发布的JSR 94(Java规则引擎API)使得Java规则引擎的实现得以标准化。

JSR 94中没有涉及用来创建规则和动作的语言.规则语言是规则引擎应用程序的重要组成部分,所有的业务规则都必须用某种语言定义并且存储于规则执行集中,从而规则引擎可以装载和处理他们。规则语言的详情这里不作详细介绍,名称及其网址列出如下:

rule Markup language (RuleML)

Simple Rule Markup Language (SRML)

Business Rules Markup Language (BRML)

SWRL: A Semantic Web Rule Language Combining OWL and RuleML

多种规则语言的使用使得不同规则引擎实现之间的兼容性成为问题.通用的规则引擎API或许可以减轻不同厂家API之间的问题,但公用规则语言的缺乏将仍然阻碍不同规则引擎实现之间的互操作性。尽管业界在提出公用规则语言上做出了一些努力,
比如说RuleML,SRML的出现,但距离获得绝大部分规则引擎厂商同意的公用标准还有很长的路要走。

SWRL规则语言与RIF的区别
https://www.w3.org/2005/rules/wiki/RIF_FAQ
RIF被设计成一种交换格式,用于在规则系统(例如那些实现SWRL的系统)之间交换规则。SWRL是一种规则语言,是作为OWL的扩展而设计的。它是一种更简单的规则语言,除了“different-from”之外,大多数SWRL特性都由RIF- bld覆盖,因此应该可以通过RIF交换大多数SWRL规则。

与SWRL相比,RIF-BLD作为一种规则语言,支持多谓词,而SWRL仅限于一元和二元谓词。
RIF-BLD有功能,SWRL无功能。RIF-BLD有一个广泛的集合

SWRL规则语言官方文档:https://www.w3.org/Submission/SWRL/#:~:text=Example%20Rules
在该文档中,还给出了 built-ins for SWRL,常用的数学插件摘录如下:

swrlb:equal (from XQuery op:numeric-equal, op:compare, op:boolean-equal op:yearMonthDuration-equal, op:dayTimeDuration-equal, op:dateTime-equal, op:date-equal, op:time-equal, op:gYearMonth-equal, op:gYear-equal, op:gMonthDay-equal, op:gMonth-equal, op:gDay-equal, op:anyURI-equal)
Satisfied iff the first argument and the second argument are the same.

swrlb:notEqual (from swrlb:equal)
The negation of swrlb:equal.

swrlb:lessThan (from XQuery op:numeric-less-than, op:compare, op:yearMonthDuration-less-than, op:dayTimeDuration-less-than, op:dateTime-less-than, op:date-less-than, op:time-less-than)
Satisfied iff the first argument and the second argument are both in some implemented type and the first argument is less than the second argument according to a type-specific ordering (partial or total), if there is one defined for the type. The ordering function for the type of untyped literals is the partial order defined as string ordering when the language tags are the same (or both missing) and incomparable otherwise.

swrlb:lessThanOrEqual (from swrlb:lessThan, swrlb:equal)
Either less than, as above, or equal, as above.

swrlb:greaterThan (from XQuery op:numeric-greater-than, op:compare, op:yearMonthDuration-greater-than, op:dayTimeDuration-greater-than, op:dateTime-greater-than, op:date-greater-than, op:time-greater-than)
Similarly to swrlb:lessThan.

swrlb:greaterThanOrEqual (from swrlb:greaterThan, swrlb:equal)
Similarly to swrlb:lessThanOrEqual.
先不管RIF,仍然采用SWRL规则语言和SQRL规则查询语言进行编程开发。

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

protege 另存文件的格式介绍

在这里插入图片描述
在这里插入图片描述

RDF/XML语法:

在这里插入图片描述

OWL/XML语法:

是一种OWL2的xml语法,但是不遵从RDF习惯,而是紧密映射到函数式语法。优点是,它允许我们使用标准的XML编辑工具与本体交互。

<?xml version="1.0"?>
<Ontology xmlns="http://www.w3.org/2002/07/owl#"
     xml:base="http://www.semanticweb.org/bob/ontologies/2021/8/person"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     ontologyIRI="http://www.semanticweb.org/bob/ontologies/2021/8/person">
    <Prefix name="" IRI="http://www.semanticweb.org/bob/ontologies/2021/8/person#"/>
    <Prefix name="owl" IRI="http://www.w3.org/2002/07/owl#"/>
    <Prefix name="rdf" IRI="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
    <Prefix name="xml" IRI="http://www.w3.org/XML/1998/namespace"/>
    <Prefix name="xsd" IRI="http://www.w3.org/2001/XMLSchema#"/>
    <Prefix name="rdfs" IRI="http://www.w3.org/2000/01/rdf-schema#"/>
    <Declaration>
        <Class IRI="#Man"/>
    </Declaration>
    <Declaration>
        <Class IRI="#Adult"/>
    </Declaration>
    <Declaration>
        <DataProperty IRI="#hasAge"/>
    </Declaration>
    <Declaration>
        <Class IRI="#Driver"/>
    </Declaration>
    <Declaration>
        <DataProperty IRI="#hasCar"/>
    </Declaration>
    <Declaration>
        <NamedIndividual IRI="#zhangsan"/>
    </Declaration>
    <Declaration>
        <Class IRI="#Person"/>
    </Declaration>
    <SubClassOf>
        <Class IRI="#Adult"/>
        <Class IRI="#Person"/>
    </SubClassOf>
    <SubClassOf>
        <Class IRI="#Driver"/>
        <Class IRI="#Person"/>
    </SubClassOf>
    <SubClassOf>
        <Class IRI="#Man"/>
        <Class IRI="#Person"/>
    </SubClassOf>
    <ClassAssertion>
        <Class IRI="#Man"/>
        <NamedIndividual IRI="#zhangsan"/>
    </ClassAssertion>
    <SubDataPropertyOf>
        <DataProperty IRI="#hasAge"/>
        <DataProperty abbreviatedIRI="owl:topDataProperty"/>
    </SubDataPropertyOf>
    <DataPropertyRange>
        <DataProperty IRI="#hasCar"/>
        <Datatype abbreviatedIRI="xsd:boolean"/>
    </DataPropertyRange>
</Ontology>



<!-- Generated by the OWL API (version 4.2.8.20170104-2310) https://github.com/owlcs/owlapi -->


曼切斯特语法,protege等大多数本体编辑器在用户界面所使用的语法

Prefix(:=<http://www.semanticweb.org/bob/ontologies/2021/8/person#>)
Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)
Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)


Ontology(<http://www.semanticweb.org/bob/ontologies/2021/8/person>

Declaration(Class(:Adult))
Declaration(Class(:Driver))
Declaration(Class(:Man))
Declaration(Class(:Person))
Declaration(DataProperty(:hasAge))
Declaration(DataProperty(:hasCar))
Declaration(NamedIndividual(:zhangsan))
############################
#   Data Properties
############################

# Data Property: :hasAge (:hasAge)

SubDataPropertyOf(:hasAge owl:topDataProperty)

# Data Property: :hasCar (:hasCar)

DataPropertyRange(:hasCar xsd:boolean)



############################
#   Classes
############################

# Class: :Adult (:Adult)

SubClassOf(:Adult :Person)

# Class: :Driver (:Driver)

SubClassOf(:Driver :Person)

# Class: :Man (:Man)

SubClassOf(:Man :Person)


############################
#   Named Individuals
############################

# Individual: :zhangsan (:zhangsan)

ClassAssertion(:Man :zhangsan)


)

其他的语法还有turtle三元组语法

@prefix : <http://www.semanticweb.org/bob/ontologies/2021/8/person#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <http://www.semanticweb.org/bob/ontologies/2021/8/person> .

<http://www.semanticweb.org/bob/ontologies/2021/8/person> rdf:type owl:Ontology .

#################################################################
#    Data properties
#################################################################

###  http://www.semanticweb.org/bob/ontologies/2021/8/person#hasAge
:hasAge rdf:type owl:DatatypeProperty ;
        rdfs:subPropertyOf owl:topDataProperty .


###  http://www.semanticweb.org/bob/ontologies/2021/8/person#hasCar
:hasCar rdf:type owl:DatatypeProperty ;
        rdfs:range xsd:boolean .


#################################################################
#    Classes
#################################################################

###  http://www.semanticweb.org/bob/ontologies/2021/8/person#Adult
:Adult rdf:type owl:Class ;
       rdfs:subClassOf :Person .


###  http://www.semanticweb.org/bob/ontologies/2021/8/person#Driver
:Driver rdf:type owl:Class ;
        rdfs:subClassOf :Person .


###  http://www.semanticweb.org/bob/ontologies/2021/8/person#Man
:Man rdf:type owl:Class ;
     rdfs:subClassOf :Person .


###  http://www.semanticweb.org/bob/ontologies/2021/8/person#Person
:Person rdf:type owl:Class .


#################################################################
#    Individuals
#################################################################

###  http://www.semanticweb.org/bob/ontologies/2021/8/person#zhangsan
:zhangsan rdf:type owl:NamedIndividual ,
                   :Man . 


###  Generated by the OWL API (version 4.2.8.20170104-2310) https://github.com/owlcs/owlapi

在protege界面 和SWRLRuleEngineAPI代码中分别进行规则推理的示例

原始的、未经过推理的person本体,主要知识如下图:
在这里插入图片描述
person4API.owl文件内容如下:

<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.semanticweb.org/bob/ontologies/2021/8/person#"
     xml:base="http://www.semanticweb.org/bob/ontologies/2021/8/person"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
    <owl:Ontology rdf:about="http://www.semanticweb.org/bob/ontologies/2021/8/person"/>
    
    <!-- 
    ///
    //
    // Data properties
    //
    ///
     -->
    
    <!-- http://www.semanticweb.org/bob/ontologies/2021/8/person#hasAge -->
    <owl:DatatypeProperty rdf:about="http://www.semanticweb.org/bob/ontologies/2021/8/person#hasAge">
        <rdfs:subPropertyOf rdf:resource="http://www.w3.org/2002/07/owl#topDataProperty"/>
    </owl:DatatypeProperty>
    
    <!-- http://www.semanticweb.org/bob/ontologies/2021/8/person#hasCar -->
    <owl:DatatypeProperty rdf:about="http://www.semanticweb.org/bob/ontologies/2021/8/person#hasCar"/>
    
    <!-- 
    ///
    //
    // Classes
    //
    ///
     -->
    
    <!-- http://www.semanticweb.org/bob/ontologies/2021/8/person#Adult -->
    <owl:Class rdf:about="http://www.semanticweb.org/bob/ontologies/2021/8/person#Adult">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/bob/ontologies/2021/8/person#Person"/>
    </owl:Class>
    
    <!-- http://www.semanticweb.org/bob/ontologies/2021/8/person#Driver -->
    <owl:Class rdf:about="http://www.semanticweb.org/bob/ontologies/2021/8/person#Driver">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/bob/ontologies/2021/8/person#Person"/>
    </owl:Class>
    
    <!-- http://www.semanticweb.org/bob/ontologies/2021/8/person#Man -->
    <owl:Class rdf:about="http://www.semanticweb.org/bob/ontologies/2021/8/person#Man">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/bob/ontologies/2021/8/person#Person"/>
    </owl:Class>
    
    <!-- http://www.semanticweb.org/bob/ontologies/2021/8/person#Person -->
    <owl:Class rdf:about="http://www.semanticweb.org/bob/ontologies/2021/8/person#Person"/>
    
    <!-- 
    ///
    //
    // Individuals
    //
    ///
     -->
    
    <!-- http://www.semanticweb.org/bob/ontologies/2021/8/person#zhangsan -->
    <owl:NamedIndividual rdf:about="http://www.semanticweb.org/bob/ontologies/2021/8/person#zhangsan">
        <rdf:type rdf:resource="http://www.semanticweb.org/bob/ontologies/2021/8/person#Man"/>
        <hasAge rdf:datatype="http://www.w3.org/2001/XMLSchema#int">20</hasAge>
        <hasCar rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</hasCar>
    </owl:NamedIndividual>
</rdf:RDF>
<!-- Generated by the OWL API (version 4.2.8.20170104-2310) https://github.com/owlcs/owlapi -->

protege5.2 中的SWRL规则推理(注意在5.5版本中没有swrlTab插件)

在这里插入图片描述

在这里插入图片描述

SWRLRuleEngineAPI项目代码中的推理
package org.swrlapi.example;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.swrlapi.core.SWRLAPIRule;
import org.swrlapi.core.SWRLRuleEngine;
import org.swrlapi.exceptions.SWRLBuiltInException;
import org.swrlapi.factory.SWRLAPIFactory;
import org.swrlapi.parser.SWRLParseException;
import org.swrlapi.sqwrl.SQWRLQueryEngine;
import org.swrlapi.sqwrl.SQWRLResult;
import org.swrlapi.sqwrl.exceptions.SQWRLException;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

 


public class SWRLAPIExample {

	public static void main(String[] args) throws SQWRLException, SWRLParseException, OWLOntologyStorageException {
		
		File directory = new File("");// 参数为空
		String projectPath;
		try {
			
			String OWLFile ="D:\\……\\OWLFiles\\person4API.owl";  
			
//			CreatOntology(OWLFile);
			ReasoningTest(OWLFile);

		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

		System.out.println("main函数运行结束"); 

	}

//https://github.com/protegeproject/swrlapi/wiki/SWRLRuleEngineAPI
	public static void ReasoningTest(String OWLFile) throws SQWRLException, SWRLParseException, OWLOntologyStorageException
	{
		System.out.println("进入推理了");
		OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
		OWLOntology ontology;
		 
			try {
				File owlFile=new File(OWLFile);
				ontology = ontologyManager.loadOntologyFromOntologyDocument(owlFile);
 				 //Create a SWRL rule engine using the SWRLAPI   
				 SWRLRuleEngine ruleEngine = SWRLAPIFactory.createSWRLRuleEngine(ontology);				 
				 // Create a SWRL rule  
				 SWRLAPIRule rule = ruleEngine.createSWRLRule("RuleDriver",
				   "Person(?p) ^ hasCar(?p, true) -> Driver(?p)");  //   Person(?p) ^ hasCar(?p, true) -> Driver(?p)   .... Person(?p) ^ hasAge(?p, ?age) ^ swrlb:greaterThan(?age, 17) -> Adult(?p)
				 // Run the rule engine
				 ruleEngine.infer();  
				 ontology.saveOntology(); //保存该推理后的本体	
				 System.out.println("推理jiesu了");
			} catch (OWLOntologyCreationException | SWRLBuiltInException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		    System.out.println("成功使用代码加载了本体文件");
	}

  /**
	 * 
	 * @Description:增加本体的类和类间关系(即公理axiom)
	 * @throws OWLOntologyCreationException
	 * @throws OWLOntologyStorageException
	 * @throws FileNotFoundException
	 * void
	 * @exception:
	 * @author: zhangyang
	 * @time:2021年9月17日 下午8:35:52
	 */
	public static void CreatOntology() throws OWLOntologyCreationException, OWLOntologyStorageException, FileNotFoundException {
		String OWLFile1 ="D:\\Program Files (x86)\\Java\\java-2018-12\\zhangyangworkspace\\OWLFiles\\personNew.owl";
		String pathString="http://www.semanticweb.org/bob/ontologies/2021/8/person/person";
		IRI personIri=IRI.create(pathString);
		OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
		OWLOntology ontology=ontologyManager.createOntology(personIri);
		
		
		
		OWLDataFactory df = OWLManager.getOWLDataFactory();
		OWLClass Person = df.getOWLClass(IRI.create(personIri + "#Person"));
		OWLClass Women = df.getOWLClass(IRI.create(personIri + "#Women"));
		ontologyManager.getOWLDataFactory();
		 
		
 		OWLAxiom axiom = df.getOWLSubClassOfAxiom(Women, Person);
 		AddAxiom addAxiom = new AddAxiom(ontology, axiom);	
 		ontologyManager.applyChange(addAxiom);
// 		RemoveAxiom removeAxiom = new RemoveAxiom(ontology,axiom);
// 		ontologyManager.applyChange(removeAxiom);
		File file = new File(OWLFile1);
		 FileOutputStream outputStream = new FileOutputStream(file);		
		ontologyManager.saveOntology(ontology,outputStream);
	
	}

}

推理后的结果:
在这里插入图片描述

在这里插入图片描述

OWLAPI的常用操作

参考《熊振辉,于娟. 情报探索[M]. 情报探索, 2015: 72-76.》
在这里插入图片描述
[1]刘侨,王新房,雷鸣. 计算机系统应用[M]. 计算机系统应用, 2015: 163-167.
在这里插入图片描述
在这里插入图片描述

本体推理

前述是规则推理,其实利用本体中预定义词汇,例如owl:inverseOf等来进行本体推理。
定义下图本体(杨过,妻子,小龙女)(刘亦菲,丈夫,杨过),并设置丈夫与妻子是相反关系owl:inverseOf通过pellet推理机可知:(小龙女,丈夫,杨过)(杨过,妻子,刘亦菲)。再次设置对象属性 丈夫、妻子具有 函数functional特征(单值关系,一一对应关系),则可推理出来(小龙女,sameas,刘亦菲)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

如何将推理结果保存到本体模型中

怎么保存Protege 5.x中HermiT推理机reasoner的推理结果:https://blog.csdn.net/u012052268/article/details/88736783
注意:1、所有推理机的推理结果都可以通过该功能导出。2、注意按需选择要导出的公理以索性全选。
在这里插入图片描述

swrl规则中加入SQWRL查询

Zohreh, I’m not sure if this is exactly what you are looking for but there
are two things I can think of that might do what you need. First, if you
have rules that are already working you should have some info in your
ontology that is a result of the rules. Any information that is added to
the ontology as a result of inferences from the Reasoner (which includes
firing SWRL rules) is highlighted and has a “?” next to it. If you pick
some assertion that you know is the result of the rules you want to trace
and click on the “?” Protege will show you a trace of the inferencing that
the reasoner went through to deduce that assertion which will include the
rules that fired.

Another option is to use SQWRL. SQWRL is just like SWRL except SQWRL
doesn’t assert info into the ontology, it only displays info. So a common
trick is to write SQWRL rules where the left hand side is a SWRL rule you
want to trace and the right hand side is replaced with a SQWRL select
statement.

So for example if you have a rule like:

Person(?p) ^ hasAge(?p, ?a) ^ swrlb:greaterThanOrEqual(?a, 18) ->
LegalDrivingAge(?p)

and you want to see all the times that this rule fires you can write the
SQWRL rule:

Person(?p) ^ hasAge(?p, ?a) ^ swrlb:greaterThanOrEqual(?a, 18) ->
sqwrl:select(?p,?a)

And in the SQWRL pane you will see a trace for every time this rule fires
and what the values of p and a are when it does. You can put as many
variables as you want in the select statement, as long as those variables
are used in the left hand side of the rule.

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值