• Protege OWL API体验


    前两天使用Jena来读取、创建和保存OWL文件。今天尝试使用Protege OWL API来操作。

    其实Protege OWL API也是通过封装了Jena包来操作OWL文件的,其结构与Jena类似。

    1. 创建OWL模型

    1 OWLModel owlModel = ProtegeOWL.createJenaOWLModel();

    2. 创建OWL类及数据属性

    //创建Person类
    OWLNamedClass personCls = owlModel.createOWLNamedClass("Person");
    //创建一个name的数据属性
    OWLDatatypeProperty nameProp = owlModel.createOWLDatatypeProperty("name");
    //name属性的数据值域为xsd:String
    nameProp.setRange(owlModel.getXSDstring());
    //name属性域为Person类
    nameProp.setDomain(personCls);

    3. 创建对象属性

    1 //创建对象属性children
    2 OWLObjectProperty childrenProp = owlModel.createOWLObjectProperty("children");
    3 //children属性的数据值域为person
    4 childrenProp.setRange(personCls);
    5 //children属性域为Person类
    6 childrenProp.setDomain(personCls);

    4. 创建类实例

    1 //创建person类的实例Darwin
    2 RDFIndividual darwin = personCls.createRDFIndividual("Darwin");
    3 //设置darwin对象的name属性
    4 darwin.setPropertyValue(nameProp, "Darwin");
    5 //设置darwin对象的age属性值为0
    6 darwin.setPropertyValue(ageProp, "0");

     5. 将其他数据类型被封装成一个RDFLiteral,如xsd:date

     1 //其他数据类型被封装成一个RDFLiteral
    2 //获取xsd:date的数据类型
    3 RDFSDatatype xsdDate = owlModel.getRDFSDatatypeByName("xsd:date");
    4 //以xsd:date数据类型作为值域创建日期的数据属性
    5 OWLDatatypeProperty dateProp = owlModel.createOWLDatatypeProperty("date", xsdDate);
    6 dateProp.setDomain(personCls);
    7
    8 //创建
    9 RDFSLiteral dateLiteral = owlModel.createRDFSLiteral("2010-09-19", xsdDate);
    10 //用RDFSLiteral对象设置hans实例的日期属性值
    11 hans.setPropertyValue(dateProp, dateLiteral);
    12
    13 //从hans实例中获取RDFSLiteral对象
    14 RDFSLiteral hansDate = hans.getPropertyValueLiteral(dateProp);
    15 System.out.println("hans'Date: " + hansDate);

    6. 源代码:Ex1.java

      1 package org.bluemaple;
    2
    3 import java.io.FileNotFoundException;
    4 import java.io.FileOutputStream;
    5 import java.io.IOException;
    6 import java.io.OutputStreamWriter;
    7 import java.io.UnsupportedEncodingException;
    8 import java.io.Writer;
    9 import java.util.Collection;
    10 import java.util.Iterator;
    11
    12 import edu.stanford.smi.protege.exception.OntologyLoadException;
    13 import edu.stanford.smi.protegex.owl.ProtegeOWL;
    14 import edu.stanford.smi.protegex.owl.model.OWLDatatypeProperty;
    15 import edu.stanford.smi.protegex.owl.model.OWLIndividual;
    16 import edu.stanford.smi.protegex.owl.model.OWLModel;
    17 import edu.stanford.smi.protegex.owl.model.OWLNamedClass;
    18 import edu.stanford.smi.protegex.owl.model.OWLObjectProperty;
    19 import edu.stanford.smi.protegex.owl.model.RDFIndividual;
    20 import edu.stanford.smi.protegex.owl.model.RDFSClass;
    21 import edu.stanford.smi.protegex.owl.model.RDFSDatatype;
    22 import edu.stanford.smi.protegex.owl.model.RDFSLiteral;
    23 import edu.stanford.smi.protegex.owl.writer.rdfxml.rdfwriter.OWLModelWriter;
    24
    25 /**
    26 * 示例代码,知识共享
    27 * @author tanyongbin
    28 * 2011-8-1
    29 * Email: briandottan@gmail.com
    30 */
    31 public class Ex1 {
    32
    33 /**
    34 * @param args
    35 */
    36 public static void main(String[] args) {
    37 // TODO Auto-generated method stub
    38 //获得OWLModel对象
    39 try {
    40 OWLModel owlModel = ProtegeOWL.createJenaOWLModel();
    41
    42 //创建Person类
    43 OWLNamedClass personCls = owlModel.createOWLNamedClass("Person");
    44 //创建一个name的数据属性
    45 OWLDatatypeProperty nameProp = owlModel.createOWLDatatypeProperty("name");
    46 //name属性的数据值域为xsd:String
    47 nameProp.setRange(owlModel.getXSDstring());
    48 //name属性域为Person类
    49 nameProp.setDomain(personCls);
    50
    51 //创建数据属性age
    52 OWLDatatypeProperty ageProp = owlModel.createOWLDatatypeProperty("age");
    53 //age属性的数据值域为xsd:int
    54 ageProp.setRange(owlModel.getXSDint());
    55 //age属性域为Person类
    56 ageProp.setDomain(personCls);
    57
    58 //创建对象属性children
    59 OWLObjectProperty childrenProp = owlModel.createOWLObjectProperty("children");
    60 //children属性的数据值域为person
    61 childrenProp.setRange(personCls);
    62 //children属性域为Person类
    63 childrenProp.setDomain(personCls);
    64
    65 //创建person类的实例Darwin
    66 RDFIndividual darwin = personCls.createRDFIndividual("Darwin");
    67 //设置darwin对象的name属性
    68 darwin.setPropertyValue(nameProp, "Darwin");
    69 //设置darwin对象的age属性值为0
    70 darwin.setPropertyValue(ageProp, "0");
    71
    72 //创建person类实例Holger
    73 RDFIndividual holger = personCls.createRDFIndividual("Holger");
    74 //设置holger对象的name属性
    75 holger.setPropertyValue(nameProp, "Holger");
    76 //设置holger对象的age属性值为0
    77 holger.setPropertyValue(ageProp, "33");
    78
    79 //设置Holger与Darwin之间的对象属性Childern
    80 holger.setPropertyValue(childrenProp, darwin);
    81
    82 //创建类Brother
    83 OWLNamedClass brotherCls = owlModel.createOWLNamedClass("Brother");
    84 //设置Brother类的父类为Person类
    85 brotherCls.addSuperclass(personCls);
    86 //移除Brother默认的父类owl:Thing,该类的父类为owl:Thing类
    87 brotherCls.removeSuperclass(owlModel.getOWLThingClass());
    88 //另外一种创建Person子类的方法
    89 OWLNamedClass sisterCls = owlModel.createOWLNamedSubclass("Sister", personCls);
    90
    91 //创建Brother类的实例Hans
    92 OWLIndividual hans = brotherCls.createOWLIndividual("Hans");
    93 //获取Brother的实例对象集合,参数为false说明只获取直接子类
    94 Collection brothers = brotherCls.getInstances(false);
    95 //输出brother的实例集合中是否包含Hans这个实例
    96 System.out.println("brother的实例集合中是否包含Hans:" + brothers.contains(hans));
    97 //输出brother的实例集合总数
    98 System.out.println("brother的实例集合总数:" + brothers.size());
    99
    100 //获得Person类的所有直接和间接对象
    101 Collection persons = personCls.getInstances(true);
    102 //输出person的实例集合总数
    103 System.out.println("person所有的实例集合总数:" + persons.size());
    104 //判断Person类对象的实例是否包含holgi
    105 System.out.println("判断Person类是否包含holgi实例:" + persons.contains(holger));
    106 //获取hans对象的类,然后判断是否跟Brother类相等
    107 System.out.println("判断hans的类型是否为Brother类:" + hans.getRDFType().equals(brotherCls));
    108 //判断hans是否为Brother类的对象
    109 System.out.println("判断hans是否为Brother类:" + hans.hasRDFType(brotherCls));
    110 //判断hans是否为Person类的直接实例
    111 System.out.println("判断hans是否为Person类的直接实例:" + hans.hasRDFType(personCls, false));
    112 //判断hans是否为Person类的间接实例
    113 System.out.println("判断hans是否为Person类的间接实例:" + hans.hasRDFType(personCls, true));
    114
    115 //获取Darwin实例的属性值
    116 System.out.println("Darwin实例的name属性值为:" + darwin.getPropertyValue(nameProp)
    117 + "age属性值为:" + darwin.getPropertyValue(ageProp));
    118
    119 //其他数据类型被封装成一个RDFLiteral
    120 //获取xsd:date的数据类型
    121 RDFSDatatype xsdDate = owlModel.getRDFSDatatypeByName("xsd:date");
    122 //以xsd:date数据类型作为值域创建日期的数据属性
    123 OWLDatatypeProperty dateProp = owlModel.createOWLDatatypeProperty("date", xsdDate);
    124 dateProp.setDomain(personCls);
    125 //创建
    126 RDFSLiteral dateLiteral = owlModel.createRDFSLiteral("2010-09-19", xsdDate);
    127 //用RDFSLiteral对象设置hans实例的日期属性值
    128 hans.setPropertyValue(dateProp, dateLiteral);
    129
    130 //从hans实例中获取RDFSLiteral对象
    131 RDFSLiteral hansDate = hans.getPropertyValueLiteral(dateProp);
    132 System.out.println("hans'Date: " + hansDate);
    133
    134 //打印所有类的层次结构
    135 printClassTree(personCls, "");
    136
    137 String filePath = "D:\\ss.owl";
    138 FileOutputStream outFile= new FileOutputStream(filePath);
    139 Writer out = new OutputStreamWriter(outFile,"UTF-8");
    140 OWLModelWriter omw = new OWLModelWriter(owlModel,
    141 owlModel.getTripleStoreModel().getActiveTripleStore(), out);
    142 omw.write();
    143 out.close();
    144 } catch (OntologyLoadException e) {
    145 // TODO Auto-generated catch block
    146 e.printStackTrace();
    147 System.exit(-1);
    148 } catch (FileNotFoundException e) {
    149 // TODO Auto-generated catch block
    150 e.printStackTrace();
    151 } catch (UnsupportedEncodingException e) {
    152 // TODO Auto-generated catch block
    153 e.printStackTrace();
    154 } catch (IOException e) {
    155 // TODO Auto-generated catch block
    156 e.printStackTrace();
    157 }
    158
    159 }
    160
    161 //递归调用打印所有类的层次关系,indentation参数是使的打印的子类比其父类更缩进
    162 private static void printClassTree(RDFSClass cls, String indentation) {
    163 // TODO Auto-generated method stub
    164 System.out.println(indentation + cls.getLocalName());
    165 //遍历打印子类
    166 for(Iterator subClses = cls.getSubclasses(false).iterator(); subClses.hasNext();){
    167 RDFSClass subCls = (RDFSClass)subClses.next();
    168 printClassTree(subCls, indentation + " ");
    169 }
    170 }
    171
    172 }

      

  • 相关阅读:
    凭这份pdf让我轻松拿下了蚂蚁金服、字节跳动、小米等大厂的offer
    阿里面试官看了我写的多线程博文,反问你也看过那本的(多线程)
    Docker简介及基本使用
    springboot常见问题之Rustful风格下,@PutMapping、@@DeleteMapping失效问题
    SpringBoot之国际化功能
    SpringBoot之Thymeleaf模板引擎
    SpringBoot之静态资源处理
    SpringBoot 配置原理
    SpringBoot: 配置文件用法
    SpringBoot版hello world
  • 原文地址:https://www.cnblogs.com/bluemaplestudio/p/2124686.html
Copyright © 2020-2023  润新知