-- 作者:绿色心情1231
-- 发布时间:10/12/2009 1:38:00 PM
--
JavaBean源代码: OWLIndividualInfo.java ------------------------------------------------------------------------------------------------------------------- package com.cms.bean; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import java.util.ArrayList; import java.util.Map; import java.util.HashMap; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModelSpec; import com.hp.hpl.jena.ontology.OntClass; import com.hp.hpl.jena.ontology.Individual; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.util.iterator.ExtendedIterator; public class OWLIndividualInfo { private String filePath; // 所要操作的.owl文件的绝对路径 private OntModel ontModel; private String prefix; private List<String> classLst; // 用于存储无子类且拥有实例的概念类名称 private List<String> allIndiLst; // 用于存放所有实例名称 private Map<String, ArrayList<String>> classIndiMap; // 用于存储概念类-实例列表的Map private void loadModel() { try { FileInputStream file = new FileInputStream(filePath); InputStreamReader in = new InputStreamReader(file, "UTF-8"); ontModel.read(in, null); in.close(); file.close(); }catch(FileNotFoundException e) { e.printStackTrace(); }catch(IOException e) { e.printStackTrace(); } } private void setIndividualRelated() { ExtendedIterator classesIter = ontModel.listClasses(); while(classesIter.hasNext()) { OntClass ontClass = (OntClass)classesIter.next(); if(!ontClass.hasSubClass()) { String className = ontClass.getURI(); className = className.substring(className.indexOf('#') + 1); classLst.add(className); ExtendedIterator indiIter = ontClass.listInstances(); ArrayList<String> indiLst = new ArrayList<String>(); while(indiIter.hasNext()) { Individual indi = (Individual)indiIter.next(); String indiName = indi.getURI(); indiName = indiName.substring(indiName.indexOf('#') + 1); indiLst.add(indiName); allIndiLst.add(indiName); } if(indiLst.size() != 0) { classIndiMap.put(className, indiLst); } } } } public OWLIndividualInfo(String filePath) { this.filePath = filePath; ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null); loadModel(); prefix = ontModel.getNsPrefixURI(""); classLst = new ArrayList<String>(); classIndiMap = new HashMap<String, ArrayList<String>>(); allIndiLst = new ArrayList<String>(); setIndividualRelated(); } public List<String> getClassesList() { return classLst; } public Map<String, ArrayList<String>> getClassIndiMap() { return classIndiMap; } public List<String> getAllInstance() { return allIndiLst; } public static void main(String[] args) { OWLIndividualInfo owlIndiInfo = new OWLIndividualInfo("src\\ontology\\creatureTest.owl"); List<String> classLst = owlIndiInfo.getClassesList(); Map<String, ArrayList<String>> classIndiMap = owlIndiInfo.getClassIndiMap(); for(String className : classLst) { ArrayList<String> indiLst = classIndiMap.get(className); System.out.println(className + ":"); for(String indiName : indiLst) { System.out.println(" " + indiName); } } } } ------------------------------------------------------------------------------------------------------------------- 测试所用.owl文件: 新建该"creatureTest.owl"文件时,切记要以"UTF-8"的编码格式保存! ------------------------------------------------------------------------------------------------------------------- <?xml version="1.0"?> <rdf:RDF xmlns="http://www.owl-ontologies.com/Ontology1255315967.owl#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:protege="http://protege.stanford.edu/plugins/owl/protege#" xmlns:xsp="http://www.owl-ontologies.com/2005/08/07/xsp.owl#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:swrl="http://www.w3.org/2003/11/swrl#" xmlns:swrlb="http://www.w3.org/2003/11/swrlb#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xml:base="http://www.owl-ontologies.com/Ontology1255315967.owl"> <owl:Ontology rdf:about=""/> <owl:Class rdf:ID="虎"> <rdfs:subClassOf> <owl:Class rdf:ID="动物"/> </rdfs:subClassOf> </owl:Class> <owl:Class rdf:ID="植物"> <rdfs:subClassOf> <owl:Class rdf:ID="生物"/> </rdfs:subClassOf> </owl:Class> <owl:Class rdf:about="#动物"> <rdfs:subClassOf rdf:resource="#生物"/> </owl:Class> <owl:Class rdf:ID="树"> <rdfs:subClassOf rdf:resource="#植物"/> </owl:Class> <虎 rdf:ID="孟加拉虎"/> <树 rdf:ID="李树"/> <虎 rdf:ID="华南虎"/> <虎 rdf:ID="爪哇虎"/> <树 rdf:ID="桃树"/> <树 rdf:ID="梧桐树"/> <虎 rdf:ID="巴厘虎"/> <虎 rdf:ID="东北虎"/> <树 rdf:ID="柳树"/> <虎 rdf:ID="里海虎"/> </rdf:RDF> <!-- Created with Protege (with OWL Plugin 3.4.1, Build 536) http://protege.stanford.edu --> ------------------------------------------------------------------------------------------------------------------- 测试使用的JSP页面: AllInstancesDisplay.jsp ------------------------------------------------------------------------------------------------------------------- <%@ page contentType="text/html; charset=UTF-8" language="java" import="java.util.*" errorPage="" %> <%@ page import="com.cms.servlet.test.OWLIndividualInfo;"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>AllInstancesDisplay</title> </head> <body> <table width="400" border="1" align="center"> <tr> <td width = "200" align = "left">概念类</td> <td width = "200" align = "left">实例</td> </tr> <% String filePath = "\\creatureTest.owl"; filePath = request.getRealPath("\\WEB-INF\\classes\\ontology") + filePath; OWLIndividualInfo indiInfo = new OWLIndividualInfo(filePath); List<String> classLst = indiInfo.getClassesList(); Map<String, ArrayList<String>> classIndiMap = indiInfo.getClassIndiMap(); for(String className : classLst) { ArrayList<String> indiLst = classIndiMap.get(className); int i = 1; %><tr> <td width = "200" align = "left"><%=className %></td> <% for(String indiName : indiLst) { if(i == 1) { %><td with = "200" align = "left"><%=indiName %></td></tr> <% } else { %><tr> <td with = "200" align = "left"> </td> <td with = "200" align = "left"><%=indiName %></td> </tr> <% } i++; } } %> </table> </body> </html> ------------------------------------------------------------------------------------------------------------------- P.S. 考虑到展示实例的效果,该测试的JSP页面同时显示了实例及其直接所属概念类(叶子概 念类),没有直接实例的概念类(即其本身没有实例的概念类,处于概念类树形结构的根和枝干位置)只使用了OWLIndividualInfo类中List<String> getClassesList()方法和Map<String, ArrayList<String>> getClassIndiMap()方法来获取拥有直接实例的概念类名称和概念类名称-实例列表的映射map。 如果只是需要展示相应本体文件中的所有实例,则可以调用List<String> getAllInstance()方法获得。 另外如果本体是全英文的,则处理相对容易;如果是包含中文的,载入本体内容就要记得如loadModel() 中使用的一样设置编码格式为“UTF-8"。 -------------------------------------------------------------------------------------------------------------------
|