有谁用过Jena,读取OWL文件的

有谁用过Jena,读取OWL文件的,第1张

例子的主要代码如下列出。闷羡郑//创建使用OWL语言的内存模型OntModelontModel=ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM)ontModel.read("file:./Creature.owl")//读取当前路径下的文件,加载模型//定义一个类作为模型中Animal类的等等价类,并添加注释OntClasscls=ontModel.createClass(":DongwuClass")cls.addComment("theEquivalentClassofAnimal","EN")//通过完整的URI取得模型中的Animal类OntClassoc=ontModel.getOntClass("/marine.owl#Animal")oc.addEquivalentClass(cls)//将先前定义的派虚类添加为Animal的等价类//迭代显示模型中的类,在迭代过程中完成各种 *** 作for(Iteratori=ontModel.listClasses()i.hasNext()){OntClassc=(OntClass)i.next()//返回类型强制转换if(!c.isAnon()){//如果不是匿名类,则打印类的名字System.out.print("Class")//获取类的URI并输出,在输出时对URI做了简化(将命名空间前缀省略)System.out.println(c.getModel().getGraph().getPrefixMapping().shortForm(c.getURI()))//处理Animal类if(c.getLocalName().equals("Animal")){//如果当前类是AnimalSystem.out.println("URI@"+c.getURI())//输出它的完整URI//取得它的蚂颂的等价类并打印System.out.print("Animal'sEquivalentClassis"+c.getEquivalentClass())//输出等价类的注释System.out.println("[comments:"+c.getEquivalentClass().getComment("EN")+"]")}//处理Animal结束//迭代显示当前类的直接父类for(Iteratorit=c.listSuperClasses()it.hasNext()){OntClasssp=(OntClass)it.next()Stringstr=c.getModel().getGraph().getPrefixMapping().shortForm(c.getURI())//获取URI+"'ssuperClassis"StringstrSP=sp.getURI()try{//另一种简化处理URI的方法str=str+":"+strSP.substring(strSP.indexOf('#')+1)System.out.println("Class"+str)}catch(Exceptione){}}//superclassends//迭代显示当前类的直接子类for(Iteratorit=c.listSubClasses()it.hasNext()){System.out.print("Class")OntClasssb=(OntClass)it.next()System.out.println(c.getModel().getGraph().getPrefixMapping().shortForm(c.getURI())+"'ssuberClassis"+sb.getModel().getGraph().getPrefixMapping().shortForm(sb.getURI()))}//suberclassends//迭代显示与当前类相关的所有属性for(Iteratoripp=c.listDeclaredProperties()ipp.hasNext()){OntPropertyp=(OntProperty)ipp.next()System.out.println("associatedproperty:"+p.getLocalName())}//propertyends}//anonymityends

软件: 可打开OWL文件的软件: OWL owl.

描述:

写在OWL源代码 - 的派胡基础上,第四和FALSE编程语言“混淆奇怪的语言”郑缺,只支持整数和26个变量 - 从A到Z。

猫头尘丛拦鹰是用C语言编写,使用GNU扩展。 OWL文件可以使用OWL解释器,它可以在用gcc任何系统( GNU编译器集)安装运行。

将ShowRecord.owl文件存储到MySQL数据库中,数据库叫Jena,在Eclipse中创建工程OperaOntology,代码如下:importJava.io.*importjava.sql.SQLExceptionimportcom.hp.hpl.jena.db.*importcom.hp.hpl.jena.ontology.OntClassimportcom.hp.hpl.jena.rdf.model.*publicclassOperaOntology{publicstaticfinalStringstrDriver="com.mysql.jdbc.Driver"publicstaticfinalStringstrURL="jdbc:mysql://localhost:3306/jena"//localhost的后面要直接写冒号,再写3306;publicstaticfinalStringstrUser="root"publicstaticfinalStringstrPassword="root"publicstaticfinalStringstrDB="MySQL"publicstaticvoidmain(String[]args){try{DBConnectionconnection=newDBConnection(strURL,strUser,strPassword,strDB)System.out.println(connection)//创建连接时,第四个参数需要指定和瞎如所用的数据库类型;也就是说strDB的值应该是“MySQL”try{Class.forName("com.mysql.jdbc.Driver")System.out.println("驱动程序已经安装。")}catch(ClassNotFoundExceptione){System.out.println("ClassNotFoundException,Driverisnotavailable")}System.out.println("数据库连接成功。"唤启)//从此处开始读入一个OWL文件并且存储到数据库中;ModelMakermaker=ModelFactory.createModelRDBMaker(connection)//使用数据库连接神老参数创建一个模型制造器ModeldefModel=maker.createModel("ShowRecord")//创建一个默认模型,命名为CostumeModel,因为我要存入的OWL文件名是CostumeFileInputStreamread=nulltry{Filefile=newFile("e:/ontologies/ShowRecord.owl")read=newFileInputStream(file)}catch(FileNotFoundExceptione){e.printStackTrace()System.out.println("未找到要存储的本体文件,请检查文件地址及名称")}System.out.println("已将本体文件转换为字节流文件。")InputStreamReaderin=nulltry{in=newInputStreamReader((FileInputStream)read,"UTF-8")}catch(UnsupportedEncodingExceptione){e.printStackTrace()System.out.println("不支持上述字符集。")}System.out.println("已将字节流文件转换为UTF-8编码。")defModel.read(in,null)try{in.close()}catch(IOExceptione){e.printStackTrace()System.out.println("无法关闭字节流文件。")}System.out.println("已将字节流文件关闭。")defModel.commit()System.out.println("数据转换执行完毕,已将本体文件存入数据库。")try{connection.close()}catch(SQLExceptione){e.printStackTrace()System.out.println("文件无法关闭。")}}catch(RDFRDBExceptione){e.printStackTrace()System.out.println("出现异常")}System.out.println("已将本体文件持久化到数据库中")}}以上步骤成功完成以后,我登录到MySQL的界面查询工具查看Jena数据库的表,点击“Catalogs“


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/8220043.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存