-- 作者:mahongfu
-- 发布时间:4/6/2006 7:33:00 PM
-- 调试程序(修改后)
package readengineerdf; import java.io.FileInputStream; import java.io.IOException; import java.util.Iterator; 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.rdf.model.ModelFactory; import com.hp.hpl.jena.ontology.*; public class guhuazhen { public static void main(String [] args) { OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, null ); try { //FileInputStream a=new FileInputStream("sars.owl"); model.read(new FileInputStream("C:\\Borland\\readEngineeRDF\\readEngineeRDF\\ontology\\process2.owl"),""); // model.read("file:\\C:\\Borland\\readEngineeRDF\\readEngineeRDF\\ontology\\process2.owl"); } catch(IOException e) { System.err.println(e.toString()); } //the class number int j=0; //list classes for(Iterator i=model.listClasses();i.hasNext();) { //the subclass string String strSub=""; j++; OntClass c = (OntClass) i.next(); String strClass=c.getModel().usePrefix(c.getURI()); // getModel的作用是Return the model associated with this resource //usePrefix方法所在的位置doc\javadoc\com\hp\hpl\jena\shared\PrefixMapping.html strClass=strClass.substring(1); System.out.println(j+strClass); //to list sub-classes for each class for(Iterator k=c.listSubClasses(true);k.hasNext();) { //这里列出直接的subclass System.out.print(" "+"hasSubClass"); OntClass subclass=(OntClass)k.next(); //OntClass的作用是Interface that represents an ontology node //characterising a class description String strSubClass=subclass.getModel().usePrefix(subclass.getURI()); strSubClass=strSubClass.substring(1); System.out.println(strSubClass); strSub+=strSubClass+" "; try { Statement stmRelation=conn.createStatement(); String strsql="insert into relation values ('"+strClass+"','"+strSubClass+"',"+"'hasSubClass')"; stmRelation.execute(strsql); stmRelation.close(); } catch(Exception ex) { ex.printStackTrace(); } }//end of class for } } }
|