1. 程式人生 > >jena構建本體,讀取owl檔案,輸出owl檔案相關知識

jena構建本體,讀取owl檔案,輸出owl檔案相關知識

官方文件是最需要學習的

一種輸出方式:

最後貼上我的程式碼:

@Test
public void testJena() throws Exception{
	
	//讀取本體
	final String SOURCE = "http://ist.sjtu.edu.cn/visualization/ontology";
	final String NS = SOURCE + "#";
	OntDocumentManager ontDocMgr = new OntDocumentManager();
	// set ontDocMgr's properties here
	ontDocMgr.addAltEntry(SOURCE, "file:../testont.owl");
	OntModelSpec ontModelSpec = new OntModelSpec(OntModelSpec.OWL_MEM);
	ontModelSpec.setDocumentManager(ontDocMgr);
	// asserted ontology
	OntModel baseOnt = ModelFactory.createOntologyModel(ontModelSpec);
	baseOnt.read(SOURCE, "RDF/XML");
	// inferred ontology (after reasoning)
	OntModel infOnt = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF, baseOnt);
	
	//構建本體
	OntClass furniture = baseOnt.createClass(NS+"Furniture");
	OntClass chair = baseOnt.createClass(NS+"Chiar");
	furniture.addSubClass(chair);
	OntClass bed = baseOnt.createClass(NS+"Bed");
	furniture.addSubClass(bed);
	OntClass zhongwen = baseOnt.createClass(NS+"中文");
	
	//輸出owl檔案到檔案系統
	String filepath = "../testont.owl";
	FileOutputStream fileOS = new FileOutputStream(filepath);
	RDFWriter rdfWriter = baseOnt.getWriter("RDF/XML");
	rdfWriter.setProperty("showXMLDeclaration","true");
	rdfWriter.setProperty("showDoctypeDeclaration", "true");
	rdfWriter.write(baseOnt, fileOS, null);
	//用writer就不需要用下面的方法了
	//baseOnt.write(fileOS, "RDF/XML");
	fileOS.close();
}