KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PersistentOntology


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email ian.dickinson@hp.com
6  * Package Jena 2
7  * Web http://sourceforge.net/projects/jena/
8  * Created 25-Jul-2003
9  * Filename $RCSfile: PersistentOntology.java,v $
10  * Revision $Revision: 1.1 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2003/08/26 14:29:35 $
14  * by $Author: der $
15  *
16  *****************************************************************************/

17
18 // Package
19
///////////////
20

21 // Imports
22
///////////////
23
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 /**
31  * <p>
32  * Simple example of using the persistent db layer with ontology models. Assumes
33  * that a PostgreSQL database called 'jenatest' has been set up, for a user named ijd.
34  * </p>
35  *
36  * @author Ian Dickinson, HP Labs
37  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
38  * @version CVS $Id: PersistentOntology.java,v 1.1 2003/08/26 14:29:35 der Exp $
39  */

40 public class PersistentOntology {
41     // Constants
42
//////////////////////////////////
43

44     public static final String JavaDoc ONT1 = "urn:x-hp-jena:test1";
45     public static final String JavaDoc ONT2 = "urn:x-hp-jena:test2";
46
47     public static final String JavaDoc DB_URL = "jdbc:postgresql://localhost/jenatest";
48     public static final String JavaDoc DB_USER = "ijd";
49     public static final String JavaDoc DB_PASSWD = "";
50     public static final String JavaDoc DB = "PostgreSQL";
51
52
53     // Static variables
54
//////////////////////////////////
55

56     // Instance variables
57
//////////////////////////////////
58

59     // Constructors
60
//////////////////////////////////
61

62     // External signature methods
63
//////////////////////////////////
64

65     public static void main( String JavaDoc[] args ) {
66         new PersistentOntology().go( args );
67     }
68
69
70     protected void go( String JavaDoc[] 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         // clear out the old
82
if (maker.hasModel( ONT1 )) maker.removeModel( ONT1 );
83         if (maker.hasModel( ONT2 )) maker.removeModel( ONT2 );
84
85         // create a spec for the new ont model
86
OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_MEM_RULE_INF );
87         spec.setModelMaker( maker );
88
89         // create the base model as a persistent model
90
Model base = maker.createModel( ONT1 );
91         OntModel m = ModelFactory.createOntologyModel( spec, base );
92
93         // tell m where to find the content for ont2
94
m.getDocumentManager().addAltEntry( ONT2, "file:test2.owl" );
95
96         // now load the document for test1, which will import ONT2
97
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             // Load the Driver
116
String JavaDoc className = "org.postgresql.Driver";
117             Class.forName(className);
118
119             // Create database connection
120
IDBConnection conn = new DBConnection ( DB_URL, DB_USER, DB_PASSWD, DB );
121
122             // Create a model maker object
123
return ModelFactory.createModelRDBMaker(conn);
124         }
125         catch (Exception JavaDoc e) {
126             e.printStackTrace();
127             System.exit( 1 );
128         }
129
130         return null;
131     }
132
133
134
135
136     // Internal implementation methods
137
//////////////////////////////////
138

139     //==============================================================================
140
// Inner class definitions
141
//==============================================================================
142

143 }
144
Popular Tags