1 17 18 21 import java.util.*; 24 25 import com.hp.hpl.jena.db.*; 26 import com.hp.hpl.jena.ontology.*; 27 import com.hp.hpl.jena.rdf.model.*; 28 29 30 40 public class PersistentOntology { 41 44 public static final String ONT1 = "urn:x-hp-jena:test1"; 45 public static final String ONT2 = "urn:x-hp-jena:test2"; 46 47 public static final String DB_URL = "jdbc:postgresql://localhost/jenatest"; 48 public static final String DB_USER = "ijd"; 49 public static final String DB_PASSWD = ""; 50 public static final String DB = "PostgreSQL"; 51 52 53 56 59 62 65 public static void main( String [] args ) { 66 new PersistentOntology().go( args ); 67 } 68 69 70 protected void go( String [] args ) { 71 if (args.length > 0 && args[0].equals( "reload" )) { 72 reloadDB(); 73 } 74 listClasses(); 75 } 76 77 78 protected void reloadDB() { 79 ModelMaker maker = getMaker(); 80 81 if (maker.hasModel( ONT1 )) maker.removeModel( ONT1 ); 83 if (maker.hasModel( ONT2 )) maker.removeModel( ONT2 ); 84 85 OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_MEM_RULE_INF ); 87 spec.setModelMaker( maker ); 88 89 Model base = maker.createModel( ONT1 ); 91 OntModel m = ModelFactory.createOntologyModel( spec, base ); 92 93 m.getDocumentManager().addAltEntry( ONT2, "file:test2.owl" ); 95 96 m.read( "file:test.owl" ); 98 } 99 100 protected void listClasses() { 101 OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_MEM_RULE_INF ); 102 spec.setModelMaker( getMaker() ); 103 104 OntModel m = ModelFactory.createOntologyModel( spec, getMaker().createModel( ONT1 ) ); 105 106 for (Iterator i = m.listClasses(); i.hasNext(); ) { 107 OntClass c = (OntClass) i.next(); 108 System.out.println( "Class " + c.getURI() ); 109 } 110 } 111 112 113 protected ModelMaker getMaker() { 114 try { 115 String className = "org.postgresql.Driver"; 117 Class.forName(className); 118 119 IDBConnection conn = new DBConnection ( DB_URL, DB_USER, DB_PASSWD, DB ); 121 122 return ModelFactory.createModelRDBMaker(conn); 124 } 125 catch (Exception e) { 126 e.printStackTrace(); 127 System.exit( 1 ); 128 } 129 130 return null; 131 } 132 133 134 135 136 139 143 } 144 | Popular Tags |