?? myontology.java
字號:
package testing;
import java.util.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.ontology.*;
public class MyOntology
{
public static void main(String[] args) {
// 創建本體模型
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
ontModel.read("file:./Creature.owl"); // 讀取文件,加載模型
// 定義一個類作為模型中Animal類的等等價類,并添加注釋
OntClass cls = ontModel.createClass(":DongwuClass");
cls.addComment("the EquivalentClass of Animal...", "EN");
OntClass oc = ontModel.getOntClass("http://www.owl-ontologies.com/marine.owl#Animal");
oc.addEquivalentClass(cls); // 將先前定義的類添加為Animal的等價類
// 迭代顯示模型中的類
for (Iterator i = ontModel.listClasses(); i.hasNext();) {
OntClass c = (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")) { // 如果當前類是Animal
System.out.println(" URI@" + c.getURI()); // 輸出它的完整URI
// 取得特定類的等價類
System.out.print(" Animal's EquivalentClass is "+ c.getEquivalentClass());
// 輸出等價類的注釋
System.out.println(" [comments:" + c.getEquivalentClass().getComment("EN")+"]");
}
// 迭代顯示當前類的父類
for (Iterator it = c.listSuperClasses(); it.hasNext();)
{
OntClass sp = (OntClass) it.next();
String str = c.getModel().getGraph()
.getPrefixMapping().shortForm(c.getURI()) // 獲取URI
+ "'s superClass is " ;
String strSP = sp.getURI();
try{ // 另一種簡化處理URI的方法
str = str + ":" + strSP.substring(strSP.indexOf('#')+1);
System.out.println(" Class" +str);
}
catch( Exception e ){
}
}
// 迭代顯示當前類的子類
for (Iterator it = c.listSubClasses(); it.hasNext();)
{
System.out.print(" Class");
OntClass sb = (OntClass) it.next();
System.out.println(c.getModel().getGraph().getPrefixMapping()
.shortForm(c.getURI())
+ "'s suberClass is "
+ sb.getModel().getGraph().getPrefixMapping()
.shortForm(sb.getURI()));
}
// 迭代顯示與當前類相關的所有屬性
for(Iterator ipp = c.listDeclaredProperties(); ipp.hasNext();)
{
OntProperty p = (OntProperty)ipp.next();
System.out.println(" associated property: " + p.getLocalName());
}
}
}
}
}
/**************************************
Class:DongwuClass
associated property: eat
associated property: beEated
associated property: mainEat
Class:MeatAnimal
Class:MeatAnimal's superClass is :Animal
associated property: eat
associated property: beEated
associated property: mainEat
Class:Creature
Class:Creature's suberClass is :Animal
Class:Creature's suberClass is :Plant
associated property: eat
associated property: beEated
associated property: mainEat
Class:Animal
URI@http://www.owl-ontologies.com/marine.owl#Animal
Animal's EquivalentClass is :DongwuClass [comments:the EquivalentClass of Animal...]
Class:Animal's superClass is :Creature
Class:Animal's suberClass is :MixAnimal
Class:Animal's suberClass is :GrassAnimal
Class:Animal's suberClass is :MeatAnimal
associated property: eat
associated property: beEated
associated property: mainEat
Class:Grass
Class:Grass's superClass is :Plant
associated property: eat
associated property: beEated
associated property: mainEat
Class:Leaf
Class:Leaf's superClass is :Tree
associated property: eat
associated property: beEated
associated property: mainEat
Class:Branch
Class:Branch's superClass is :Tree
associated property: eat
associated property: beEated
associated property: mainEat
Class:Plant
Class:Plant's superClass is :Creature
Class:Plant's suberClass is :Grass
Class:Plant's suberClass is :Tree
associated property: eat
associated property: beEated
associated property: mainEat
Class:Human
Class:Human's superClass is :MixAnimal
associated property: eat
associated property: beEated
associated property: mainEat
Class:MixAnimal
Class:MixAnimal's superClass is :Animal
Class:MixAnimal's suberClass is :Human
associated property: eat
associated property: beEated
associated property: mainEat
Class:GrassAnimal
Class:GrassAnimal's superClass is :Animal
associated property: eat
associated property: beEated
associated property: mainEat
Class:Tree
Class:Tree's superClass is :Plant
Class:Tree's suberClass is :Leaf
Class:Tree's suberClass is :Branch
associated property: eat
associated property: beEated
associated property: mainEat
**********************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -