| « | June 2026 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
| 暂无公告... |
| Blog信息 |
|
blog名称: 日志总数:1 评论数量:3 留言数量:0 访问次数:11942 建立时间:2011年3月9日 |

| |
|
[收藏][原创]实现将本体文件存入数据库,再从数据库中读出,输出该本体中的所有类,所有属性及其对应属性的值 -- 作者:dorothyle 文章收藏
luxiao2233 发表于 2011/3/9 10:54:32 |
| 本文转载自W3CHINA.ORG讨论区(BBS.W3CHINA.ORG) 原文链接作者:dorothyle以下为原文:[原创]实现将本体文件存入数据库,再从数据库中读出,输出该本体中的所有类,所有属性及其对应属性的值今天终于实现了将一个本体文件存储到数据库中,再从数据库中将文本文件读出来,输出该本体中的所有类,所有属性及其对应属性的值。 import java.util.Iterator; import com.hp.hpl.jena.db.DBConnection; import com.hp.hpl.jena.db.IDBConnection; import com.hp.hpl.jena.ontology.Individual; 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.OntProperty; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.ModelMaker; import com.hp.hpl.jena.util.iterator.ExtendedIterator; public class ReadOntologyFromMysql { public static final String strDriver = &quot;com.mysql.jdbc.Driver&quot;; public static final String strURL = &quot;jdbc:mysql://localhost:3306/jena&quot;; public static final String strUser = &quot;root&quot;; public static final String strPassword = &quot;root&quot;; public static final String strDB = &quot;MySQL&quot;; // 连接数据库 public static IDBConnection connectDB(String DBURL, String DBUSER, String DBPASSWORD, String DBTYPE){ try { Class.forName(&quot;com.mysql.jdbc.Driver&quot;); System.out.print(&quot;数据库连接成功, 已经连接到&quot;); }catch (ClassNotFoundException e){ e.printStackTrace(); System.out.println(&quot;数据库连接不成功&quot;); } return new DBConnection(DBURL, DBUSER,DBPASSWORD,DBTYPE); } // 提取本体文件关于OWL架构等详细信息 public static OntModelSpec getModelSpec(ModelMaker maker) { OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM); spec.setImportModelGetter(maker); return spec; } // 从文件读取本体并将其存入数据库 public static OntModel ceateDBModelFromFile(IDBConnection con, String name, String filePath){ ModelMaker maker = ModelFactory.createModelRDBMaker(con); Model base = maker.createModel(name); OntModel newmodel = ModelFactory.createOntologyModel(getModelSpec(maker),base); newmodel.read(filePath); System.out.println(&quot;已将本体文件存入数据库。&quot;); System.out.println(); return newmodel; } // 从数据库中得到已存入本体 public static OntModel getModelFromDB(IDBConnection con, String name){ ModelMaker maker = ModelFactory.createModelRDBMaker(con); Model base = maker.getModel(name); OntModel newmodel = ModelFactory.createOntologyModel(getModelSpec(maker),base); System.out.println(&quot;已从数据库中读出本体文件。&quot;); System.out.println(); return newmodel; } // 简单读取本体中的所有类 public static void SimpleReadOntology(OntModel model){ System.out.println(&quot;该本体文件中包括以下类:&quot;); for (Iterator i = model.listClasses(); i.hasNext();){ OntClass c = (OntClass) i.next(); System.out.println(&quot; &quot; + c.getLocalName()); } System.out.println(); } public static void main(String args[]){ IDBConnection con = connectDB(strURL, strUser, strPassword, strDB); System.out.println(con + &quot;;&quot;); System.out.println(); String filePath = &quot;file:e:/ontologies/BeijingOpera/Costume.owl&quot;; String prefix = &quot;http://Costume.owl#&quot;; OntModel defModel = ceateDBModelFromFile(con, &quot;Costume&quot;, filePath); // 读取filePath目录下本体文件Costume的内容,将该本体文件的内容存储到数据库con中; OntModel model = getModelFromDB(con, &quot;Costume&quot;); // 将数据库con中存储的本体文件Costume的内容读出到一个本体文件Model中; SimpleReadOntology(model); OntClass CostumeClass = model.getOntClass(prefix + &quot;Costume&quot;); String name = CostumeClass.getLocalName(); //返回的是Costume本体中的类Costume的名字,是一个字符串; System.out.print(&quot;此处返回的是&quot; + name + &quot;类的名称&quot;); System.out.println(name); System.out.println(); ExtendedIterator it = CostumeClass.listInstances(); System.out.println(&quot;此处返回的是&quot; + name + &quot;类的所有实例:&quot;); while (it.hasNext()) { Individual oi = (Individual) it.next(); System.out.println(&quot; &quot; + oi.getLocalName()); for (Iterator ipp = CostumeClass.listDeclaredProperties(); ipp.hasNext();){ OntProperty p = (OntProperty) ipp.next(); System.out.println(&quot; associated property:&quot; + p.getLocalName() + &quot;:&quot; + oi.getPropertyValue(p)); } } } } 以上代码执行后,控制台的输出如下:数据库连接成功, 已经连接到com.hp.hpl.jena.db.DBConnection@157f0dc; 已将本体文件存入数据库。 已从数据库中读出本体文件。 该本体文件中包括以下类: Costume Document Image Master 此处返回的是Costume类的名称Costume 此处返回的是Costume类的所有实例: HongTuanLongMang associated property:kind:Mang^^http://www.w3.org/2000/01/rdf-schema#Literal associated property:image:null associated property:pattern:SanJianShui, Long^^http://www.w3.org/2000/01/rdf-schema#Literal associated property:daPei:null associated property:name:HuanTuanLongMang^^http://www.w3.org/2000/01/rdf-schema#Literal associated property:color:red^^http://www.w3.org/2000/01/rdf-schema#Literal associated property:hangDang:Sheng^^http://www.w3.org/2000/01/rdf-schema#Literal associated property:texture:DaDuan^^http://www.w3.org/2000/01/rdf-schema#Literal associated property:depiction:null associated property:style:DuiJin, YuanCollar^^http://www.w3.org/2000/01/rdf-schema#Literal HuangTuanLongMang associated property:kind:null associated property:image:null associated property:pattern:null associated property:daPei:null associated property:name:null associated property:color:null associated property:hangDang:null associated property:texture:null associated property:depiction:null associated property:style:null LvTuanLongMang associated property:kind:null associated property:image:null associated property:pattern:null associated property:daPei:null associated property:name:null associated property:color:null associated property:hangDang:null associated property:texture:null associated property:depiction:null associated property:style:null<完>参与讨论本主题 |
|
|