前言

本文依据"The Description Logic Handbookd"中Appendxi1 Description Terminology中基本的描述逻辑构造,考察Jena对这些构造的支持,以自说明的代码片段予以说明。

内容

1 概念构造

2 关系构造

3 公理构造

1 概念构造

概念构造的具体语法

package constructor;

import java.util.List;

import com.hp.hpl.jena.ontology.AllValuesFromRestriction;
import com.hp.hpl.jena.ontology.CardinalityRestriction;
import com.hp.hpl.jena.ontology.ComplementClass;
import com.hp.hpl.jena.ontology.EnumeratedClass;
import com.hp.hpl.jena.ontology.HasValueRestriction;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.IntersectionClass;
import com.hp.hpl.jena.ontology.MaxCardinalityRestriction;
import com.hp.hpl.jena.ontology.MinCardinalityRestriction;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.ontology.SomeValuesFromRestriction;
import com.hp.hpl.jena.ontology.UnionClass;
import com.hp.hpl.jena.ontology.impl.OWLProfile;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFList;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.OWL2;

public class ConceptConstruct {

    public static void main(String[] args) {
        String ns = "http://jena/demo/constructors/concept";
        OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, null);
        // (1) Top
        System.out.println("(1)");
        OWLProfile owlProfile = new OWLProfile();
        Resource top = owlProfile.THING();
        System.out.println(top);

        // (2) Bottom
        System.out.println("(2)");
        Resource bottom = owlProfile.NOTHING();
        System.out.println(bottom);

        // (3) Intersection: C = A and B
        System.out.println("(3)");
        OntClass aClass = ontModel.createClass(ns + "A");
        System.out.println(aClass);
        OntClass bClass = ontModel.createClass(ns + "B");
        RDFNode[] nodes = new RDFNode[] { aClass, bClass };
        RDFList members = ontModel.createList(nodes);
        IntersectionClass intersection = ontModel.createIntersectionClass(ns + "C", members);
        System.out.println(intersection);
        List<? extends OntClass> operands = intersection.listOperands().toList();
        for (OntClass operand : operands) {
            System.out.println(operand);
        }
        Property operator = intersection.operator();
        System.out.println(operator);// http://www.w3.org/2002/07/owl#intersectionOf

        // (4) Union: D = A or B
        System.out.println("(4)");
        UnionClass union = ontModel.createUnionClass(ns + "D", ontModel.createList(new RDFNode[] { bClass, aClass }));
        System.out.println(union);
        operands = union.listOperands().toList();// BooleanClassDescription
        for (OntClass operand : operands) {
            System.out.println(operand);
        }
        operator = union.operator();
        System.out.println(operator);// http://www.w3.org/2002/07/owl#unionOf

        // (5) Negation: E = not A
        System.out.println("(5)");
        ComplementClass negation = ontModel.createComplementClass(ns + "E", aClass);
        System.out.println(negation);
        operands = negation.listOperands().toList();
        for (OntClass operand : operands) {
            System.out.println(operand);
        }
        operator = negation.operator();
        System.out.println(operator);// http://www.w3.org/2002/07/owl#complementOf

        // (6) Value restriction: G = all R.A
        System.out.println("(6)");
        ObjectProperty role = ontModel.createObjectProperty(ns + "R");
        System.out.println(role);
        AllValuesFromRestriction valueRestriction = ontModel.createAllValuesFromRestriction(ns + "G", role, aClass);
        System.out.println(valueRestriction);
        System.out.println(valueRestriction.getAllValuesFrom());
        System.out.println(valueRestriction.getOnProperty());
        // list all restrictions
        // List<Restriction> restrictions = ontModel.listRestrictions().toList();
        // for (Restriction restriction : restrictions) {
        // System.out.println(restriction);
        // }

        // (7) Limited existential quantification: H = some R
        System.out.println("(7)");
        SomeValuesFromRestriction limitedExistentialQuantification = ontModel.createSomeValuesFromRestriction(ns + "H", role, top);
        System.out.println(limitedExistentialQuantification);
        System.out.println(limitedExistentialQuantification.getSomeValuesFrom());
        System.out.println(limitedExistentialQuantification.getOnProperty());

        // (8) Existential quantification: I = some R A
        System.out.println("(8)");
        SomeValuesFromRestriction existentialQuantification = ontModel.createSomeValuesFromRestriction(ns + "I", role, aClass);
        System.out.println(existentialQuantification);
        System.out.println(existentialQuantification.getSomeValuesFrom());
        System.out.println(existentialQuantification.getOnProperty());

        // (9) At-least number restriction: J = at-least n R
        System.out.println("(9)");
        int cardinality = 2;
        MinCardinalityRestriction atLeastNumberRestriction = ontModel.createMinCardinalityRestriction(ns + "J", role, cardinality);
        System.out.println(atLeastNumberRestriction);
        System.out.println(atLeastNumberRestriction.getMinCardinality());
        System.out.println(atLeastNumberRestriction.getOnProperty());

        // (10) At-most number restriction: K = at-least n R
        System.out.println("(10)");
        MaxCardinalityRestriction atMosttNumberRestriction = ontModel.createMaxCardinalityRestriction(ns + "K", role, cardinality);
        System.out.println(atMosttNumberRestriction);
        System.out.println(atMosttNumberRestriction.getMaxCardinality());
        System.out.println(atMosttNumberRestriction.getOnProperty());

        // (11) Exact number restriction: L = exactly n R
        System.out.println("(11)");
        CardinalityRestriction exactNumberRestriction = ontModel.createCardinalityRestriction(ns + "L", role, cardinality);
        System.out.println(exactNumberRestriction);
        System.out.println(exactNumberRestriction.getCardinality());
        System.out.println(exactNumberRestriction.getOnProperty());

        // (12) Qualified at-least restriction : M = at-least n R A, OntModelSpec.OWL_DL_MEM不支持
        System.out.println("(12)");
        // (13) Qualified at-most restriction: N = at-most n R A, OntModelSpec.OWL_DL_MEM不支持
        System.out.println("(13)");
        // (14) Qualified exact restriction: O = exactly n R A, OntModelSpec.OWL_DL_MEM不支持
        System.out.println("(14)");

        // (15) Same-as, agreement: i1 = i2
        System.out.println("(15)");
        Individual i1 = ontModel.createIndividual(ns + "i1", aClass);
        Individual i2 = ontModel.createIndividual(ns + "i2", aClass);
        i1.addSameAs(i2);
        // navigate
        ResIterator sameIter = ontModel.listSubjectsWithProperty(OWL2.sameAs);
        while (sameIter.hasNext()) {
            Resource sameAsLeft = sameIter.next();
            System.out.println(sameAsLeft);
            System.out.println(sameAsLeft.as(Individual.class).getSameAs());
        }

        // (16) Role-value-map: subset R1 R2
        System.out.println("(16)");
        ObjectProperty role2 = ontModel.createObjectProperty(ns + "R2");
        role.addSuperProperty(role2);
        System.out.println(role.getSuperProperty());

        // (17) Role fillers: P = fillers R i1 i2
        System.out.println("(17)");
        HasValueRestriction hasValueRestriction = ontModel.createHasValueRestriction(null, role, i1);
        HasValueRestriction hasValueRestriction2 = ontModel.createHasValueRestriction(null, role, i2);
        OntClass pClass = ontModel.createClass(ns + "P");
        pClass.addEquivalentClass(ontModel.createIntersectionClass(null,
                ontModel.createList(new RDFNode[] { hasValueRestriction, hasValueRestriction2 })));
        System.out.println(pClass.getEquivalentClass());// 匿名

        // (18) One-of: Q = {i1, i2}
        System.out.println("(18)");
        EnumeratedClass oneOf = ontModel.createEnumeratedClass(ns + "Q", ontModel.createList(new RDFNode[] { i1, i2 }));
        List<? extends OntResource> instances = oneOf.listOneOf().toList();
        for (OntResource instance : instances) {
            System.out.println(instance);
        }
    }

    public static InfModel bindReasoner(Model model) {
        Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
        reasoner = reasoner.bindSchema(model);
        return ModelFactory.createInfModel(reasoner, model);
    }

    public static OntClass createCardinalityQRestriction(OntModel model, String uri, Property prop, int cardinality, OntClass clas) {
        OntClass klass = model.createCardinalityRestriction(uri, prop, cardinality);
        klass.removeAll(OWL.cardinality);
        klass.addLiteral(OWL2.qualifiedCardinality, cardinality);
        klass.addProperty(OWL2.onClass, clas);
        return klass;
    }
}

Jena暂不支持存在量词约束(qualified at-least/at-most/exact)的直接构造,采用下述URL中方法支持该构造的RDF序列化

http://mail-archives.apache.org/mod_mbox/jena-users/201303.mbox/%3CCA+Q4Jn=bDM2wiPSh4DHj58hzxR_oWx2jJoQxnroFScac=E7t3Q@mail.gmail.com%3E

2 关系构造

关系构造的具体语法

package constructor;

import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.TransitiveProperty;
import com.hp.hpl.jena.ontology.impl.OWLProfile;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;

/**
 * TransitiveProperty ,FunctionalProperty ,SymmetricProperty,InverseFunctionalProperty
 */
public class RoleConstruct {

    public static void main(String[] args) {
        String ns = "http://jena/demo/constructors/role#";
        OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, null);
        // (1) Universal role
        System.out.println("(1)");
        OWLProfile owlProfile = new OWLProfile();
        Resource topObjectProperty = owlProfile.OBJECT_PROPERTY();
        Resource topDatatypeProperty = owlProfile.DATATYPE_PROPERTY();
        System.out.println(topObjectProperty);
        System.out.println(topDatatypeProperty);

        // (2) Intersection: r = atomR1 and atomR2
        System.out.println("(2)");

        // (3) Union
        System.out.println("(3)");

        // (4) Complement
        System.out.println("(4)");

        // (5) Inverse
        System.out.println("(5)");
        ObjectProperty r = ontModel.createObjectProperty(ns + "r");
        ObjectProperty inverse_r = ontModel.createObjectProperty(ns + "ir");
        r.addInverseOf(inverse_r);
        System.out.println(inverse_r);
        System.out.println(inverse_r.getInverse());
        System.out.println(r.getInverse());// null

        // (6) Composition
        System.out.println("(6)");

        // (7) Transitive closure
        System.out.println("(7)");
        TransitiveProperty tr = ontModel.createTransitiveProperty(ns + "tr");
        System.out.println(tr);
        System.out.println(tr.isTransitiveProperty());

        // (8) Reflexive-transitive closure
        System.out.println("(8)");

        // (9) Role Restriction
        System.out.println("(9)");
        ObjectProperty rrest = ontModel.createObjectProperty(ns + "rrest");
        OntClass aClass = ontModel.createClass(ns + "A");
        rrest.addRange(aClass);
        System.out.println(rrest);
        System.out.println(rrest.getRange());

        // (10) Identity
        System.out.println("(10)");

    }

}

同OWL规范一样,Jena将关系划分对象属性(ObjectProperty)和数值属性(DatatypeProperty),显式支持传递属性TransitiveProperty, 函数属性FunctionalProperty, 对称属性SymmetricProperty, 逆函数属性InverseFunctionalProperty。

不支持关系与/并/补、关系链(role chain)、类标识(Identity)构造。、

3 公理构造

公理的具体语法

package constructor;

import java.util.List;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Statement;

public class AxiomConstruct {

    public static void main(String[] args) {
        String ns = "http://jena/demo/constructors/axiom#";
        OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, null);
        // (1) Concept definition
        System.out.println("(1)");
        OntClass aClass1 = ontModel.createClass(ns + "A1");
        OntClass cClass1 = ontModel.createClass(ns + "C1");
        aClass1.addEquivalentClass(cClass1);
        System.out.println(aClass1.getEquivalentClass());
        System.out.println(cClass1.getEquivalentClass());

        // (2) Primitive concept introduction
        System.out.println("(2)");
        OntClass aClass2 = ontModel.createClass(ns + "A2");
        OntClass cClass2 = ontModel.createClass(ns + "C2");
        aClass2.addSuperClass(cClass2);
        System.out.println(aClass2.getSuperClass());
        System.out.println(cClass2.getSubClass());

        // (3) General inclusion axiom
        System.out.println("(3)");
        // mostly like (2)

        // (4) Role definition
        System.out.println("(4)");
        ObjectProperty r11 = ontModel.createObjectProperty(ns + "r11");
        ObjectProperty r12 = ontModel.createObjectProperty(ns + "r12");
        r11.addEquivalentProperty(r12);
        System.out.println(r11.getEquivalentProperty());
        System.out.println(r12.getEquivalentProperty());

        // (5) Primitive role introduction
        System.out.println("(5)");
        ObjectProperty r21 = ontModel.createObjectProperty(ns + "r21");
        ObjectProperty r22 = ontModel.createObjectProperty(ns + "r22");
        r21.addSuperProperty(r22);
        System.out.println(r21.getSuperProperty());
        System.out.println(r22.getSubProperty());

        // (6) Concept assertion
        System.out.println("(6)");
        OntClass concept = ontModel.createClass(ns + "C");
        Individual indv = concept.createIndividual(ns + "c1");
        System.out.println(indv.getOntClass());
        List<? extends OntResource> indvs = concept.listInstances().toList();
        for (OntResource ind : indvs) {
            System.out.println(ind);
        }

        // (7) Role assertion
        System.out.println("(7)");
        ObjectProperty role = ontModel.createObjectProperty(ns + "R");
        OntClass roleDomain = ontModel.createClass(ns + "Domain");
        Individual d1 = roleDomain.createIndividual(ns + "domain1");
        OntClass roleRange = ontModel.createClass(ns + "Range");
        Individual r1 = roleRange.createIndividual(ns + "range1");
        role.addDomain(roleDomain);
        role.addRange(roleRange);
        Statement stmt = ontModel.createStatement(d1, role, r1);
        System.out.println(stmt);
        ontModel.add(stmt);
    }

}

Jena可以构建描述逻辑公理构造。

Jena对描述逻辑构造的支持的更多相关文章

  1. 描述逻辑(DL)基础知识

    Logic逻辑理论实际上是一个规范性(normative)的理论,而不是一个描述性的(descriptive)理论.即,它并不是用来描述人类究竟是采用何种的形式来推理的,而是来研究人类应该如何有效的进 ...

  2. Odoo 12 开发手册指南(八)—— 业务逻辑 – 业务流程的支持

    在前面的文章中,我们学习了模型层.如何创建应用数据结构以及如何使用 ORM API 来存储查看数据.本文中我们将利用前面所学的模型和记录集知识实现应用中常用的业务逻辑模式. 本文的主要内容有: 以文件 ...

  3. 第八章 Odoo 12开发之业务逻辑 - 业务流程的支持

    在前面的文章中,我们学习了模型层.如何创建应用数据结构以及如何使用 ORM API 来存储查看数据.本文中我们将利用前面所学的模型和记录集知识实现应用中常用的业务逻辑模式. 本文的主要内容有: 以文件 ...

  4. EnumHelper.cs枚举助手(枚举描述信息多语言支持)C#

    C#里面经常会用到枚举类型,枚举是值类型对象,如果你想用枚举类型的多属性特性,或者你想在MVC页面上通过简单的值类型转换,将某字段值所代表的含义转换为文字显示,这时候必须要将枚举扩展,是它支持文本描述 ...

  5. CMWAP上网补丁描述文件!!支持ios9

    由于ios移动4G使用cmnet网络,某些情况下需要使用cmwap,因此就有了这些文字... 两个网址都可以: http://www.clore.net/iphone/cmwap.html https ...

  6. 使用openSSL构造一个支持https的nodejs服务器

    首先通过下面的链接下载openSSL https://slproweb.com/products/Win32OpenSSL.html 下载完毕后,执行openssl进入交互式界面: 使用命令生成pri ...

  7. 最小栈问题:题目描述:设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。

    MinStack minStack = new MinStack();minStack.push(-2);minStack.push(0);minStack.push(-3);minStack.get ...

  8. Outline of Apache Jena Notes

    1 description 这篇是语义网应用框架Apache Jena学习记录的索引. 初始动机见Apache Jena - A Bootstrap 2 Content 内容组织基本上遵循Jena首页 ...

  9. Jena语义Web开发101

    2015/05/28更新 代码在 https://github.com/zhoujiagen/semanticWebTutorialUsingJena 前言 该手册参考和扩展“Hebeler J, F ...

随机推荐

  1. iOS开发数据库篇—SQL代码应用示例

    iOS开发数据库篇—SQL代码应用示例 一.使用代码的方式批量添加(导入)数据到数据库中 1.执行SQL语句在数据库中添加一条信息 插入一条数据的sql语句: 点击run执行语句之后,刷新数据 2.在 ...

  2. Linux 监控文件被什么进程修改

    安装: apt-get install auditd. auditd 是后台守护进程,负责监控记录 auditctl 配置规则的工具 auditsearch 搜索查看 aureport 根据监控记录生 ...

  3. Postman 安装及使用入门教程

    安装 本文只是基于 Chrome 浏览器的扩展插件来进行的安装,并非单独应用程序. 首先,你要台电脑,其次,安装有 Chrome 浏览器,那你接着往下看吧. 1. 官网安装(别看) 打开官网,http ...

  4. 【ubuntu】系统设置打不开

    如果系统设置打不开,请重新安装gnome-control-centersudo apt-get install gnome-control-center如果设置里只有很少的几个图标请重新安装unity ...

  5. [分享] 从定制Win7母盘到封装详细教程 By BILL ( 10月23日补充说明 )

    [分享] 从定制Win7母盘到封装详细教程 By BILL ( 10月23日补充说明 ) billcheung 发表于 2011-10-23 00:07:49 https://www.itsk.com ...

  6. 移动 Web 开发技巧之(后续)

    昨天的<移动 Web 开发技巧>的这篇文章,大家反响不错,因为这些问题在大家日常写移动端的页面时经常遇到的.所以那个文章还是超级实用的,那么我们今天继续来分享一下移动端的web开发技巧吧, ...

  7. Frame练习-打开图片

    import java.awt.EventQueue; import java.awt.event.*; import java.io.*; import javax.swing.*; public ...

  8. Node.js 常用工具 util

    util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.inherits util.inherits(constructor ...

  9. 用python定时文章发布wordpress

    用python定时文章发布wordpress: 流程: 采集 - 筛选文章 - wordpress文章发布. wordpress文章发布代码:python利用模块xmlrpclib发布文章非常便捷,省 ...

  10. C语言基础--for循环

    for循环格式: for (初始化表达式;条件表达式;循环后增量表达式) { 语句; ... } 条件表达式: 和while, dowhile一样, 只有条件满足才会执行循环体 初始化表达式: 在整个 ...