| « | November 2025 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | | 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名称: 日志总数:9 评论数量:4 留言数量:0 访问次数:53241 建立时间:2008年4月14日 |

| |
|
使用 jena 读书笔记, 软件技术
xiawared 发表于 2008/4/16 15:46:05 |
| 参照网上的一些资料组合这个例子,给大家一个使用jena的大概印象。
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 test { public static IDBConnection connectDB(String DB_URL, String DB_USER,String DB_PASSWD, String DB_NAME)
{ return new DBConnection(DB_URL, DB_USER, DB_PASSWD, DB_NAME); } /* 从文件读取本体并将其存入数据库 */ public static OntModel createDBModelFromFile(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); 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); return newmodel; }
public static OntModelSpec getModelSpec(ModelMaker maker) { OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM); spec.setImportModelMaker(maker); return spec; }
public static void main(String[] args){// test JaneUtils = new test(); String DB_URL = "jdbc:mysql://localhost/protege_db"; String DB_USER = "root"; String DB_PASSWD = "25647335"; String DB = "MySQL"; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } String filePath = "file:F://eclipse//workspace//Animal.owl"; IDBConnection con = connectDB(DB_URL,DB_USER, DB_PASSWD, DB); System.out.println(con); createDBModelFromFile(con, "expert",filePath); OntModel model = getModelFromDB(con, "expert"); SimpleReadOntology(model); getInstance(model); getInstanceProperty(model);} public static void SimpleReadOntology(OntModel model) { System.out.println("Simeple read ontology : "); System.out.println("There are "+model.listClasses().toList().size()+"Classes : "); for (Iterator i = model.listClasses(); i.hasNext();) { OntClass c = (OntClass) i.next(); if(c.getLocalName()!=null){ System.out.println(c.getLocalName()); } if(c.getURI()!=null) System.out.println(c.getURI()); } } public static void getInstance(OntModel model){ System.out.println("Get Instance : "); //http://www.owl-ontologies.com/Expert.owl#"; String prix = "http://www.owl-ontologies.com/Ontology1208149104.owl#"; /*得到本体中的Expert类*/ OntClass expert = model.getOntClass(prix + "Zoo"); //打印类名 System.out.println(expert.getLocalName() + ":"); //获得其所以个体 ExtendedIterator it = expert.listInstances(); //打印其个体 while(it.hasNext()){ Individual oi = (Individual)it.next(); System.out.println(oi.getLocalName()); } } public static void getInstanceProperty(OntModel model) { System.out.println("Get Instance Property :"); String NS = "http://www.owl-ontologies.com/Ontology1208149104.owl#"; /* 得到本体中的Expert类 */ OntClass expert = model.getOntClass(NS + "Zoo"); // 打印类名 System.out.println(expert.getLocalName());
// 获得其所以个体 ExtendedIterator it = expert.listInstances(); // 打印其个体 while (it.hasNext()) { Individual oi = (Individual) it.next(); System.out.println(oi.getLocalName());
for (Iterator ipp = expert.listDeclaredProperties(); ipp.hasNext();) { OntProperty p = (OntProperty) ipp.next(); System.out.println(" associated property: " + p.getLocalName() + " : "+oi.getPropertyValue(p)); }// property ends } }
}
用到的本体:
500)this.width=500'>Animal.rar |
|
|