KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > ModelFactory


1 /*
2   (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: ModelFactory.java,v 1.42 2005/04/08 10:02:25 chris-dollin Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.model;
8
9 import com.hp.hpl.jena.graph.*;
10 import com.hp.hpl.jena.graph.compose.Union;
11 import com.hp.hpl.jena.graph.impl.*;
12 import com.hp.hpl.jena.db.*;
13 import com.hp.hpl.jena.db.impl.*;
14 import com.hp.hpl.jena.mem.*;
15 import com.hp.hpl.jena.rdf.model.impl.*;
16 import com.hp.hpl.jena.reasoner.*;
17 import com.hp.hpl.jena.shared.*;
18 import com.hp.hpl.jena.ontology.*;
19 import com.hp.hpl.jena.ontology.daml.DAMLModel;
20 import com.hp.hpl.jena.ontology.daml.impl.DAMLModelImpl;
21 import com.hp.hpl.jena.ontology.impl.OntModelImpl;
22
23 /**
24     ModelFactory provides methods for creating standard kinds of Model.
25     (ModelFactoryBase is helper functions for it).
26 */

27
28 public class ModelFactory extends ModelFactoryBase
29 {
30     /**
31         No-one can make instances of this.
32     */

33     private ModelFactory()
34         {}
35         
36     /**
37         The standard reification style; quadlets contribute to reified statements,
38         and are visible to listStatements().
39     */

40     public static final ReificationStyle Standard = ReificationStyle.Standard;
41     
42     /**
43         The convenient reification style; quadlets contribute to reified statements,
44         but are invisible to listStatements().
45     */

46     public static final ReificationStyle Convenient = ReificationStyle.Convenient;
47     
48     /**
49         The minimal reification style; quadlets do not contribute to reified statements,
50         and are visible to listStatements().
51     */

52     public static final ReificationStyle Minimal = ReificationStyle.Minimal;
53     
54     /**
55         Each Model created by ModelFactory has a default set of prefix mappings.
56         These mappings are copied from a (static) default PrefixMapping which is
57         set by setDefaultModelPrefixes. It is the reference to a PrefixMapping that
58         is retained, not a copy of it, so a user may set the defaults with this method
59         and continue to modify it; the modifications will appear in the next model to
60         be created.
61     <p>
62         When a Model is created from an existing Graph, the prefixes of that Graph
63         are not disturbed; only ones not present in the Graph are added.
64         
65         @param pm the default prefixes to use
66         @return the previous default prefix mapping
67     */

68     public static PrefixMapping setDefaultModelPrefixes( PrefixMapping pm )
69         { return ModelCom.setDefaultModelPrefixes( pm ); }
70     
71     /**
72         Answer the current default model prefixes PrefixMapping object.
73     */

74     public static PrefixMapping getDefaultModelPrefixes()
75         { return ModelCom.getDefaultModelPrefixes(); }
76     
77     /**
78         Answer a ModelSpec which can create models to the specifications in the RDF
79         description. The root of the description is the unique resource of type ModelSpec.
80     */

81     public static ModelSpec createSpec( Model desc )
82         { return ModelSpecFactory.createSpec( desc ); }
83         
84     /**
85         Answer a ModelSpec which can create models to the specifcations in the RDF
86         description rooted at the given root.
87     */

88     public static ModelSpec createSpec( Resource root, Model desc )
89         { return ModelSpecFactory.createSpec( ModelSpecFactory.withSchema( desc ), root ); }
90         
91     /**
92         Answer a fresh Model created according to the ModelSpec argument.
93     */

94     public static Model createModel( ModelSpec desc )
95         { return desc.createModel(); }
96         
97     /**
98         Answer a fresh Model created according to the given specification and based on
99         any underlying model with the given name.
100         
101         @param desc the ModelSpec which describes the kind of model to create
102         @param name the name of the base model in the underlying ModelMaker
103         @return a fresh model based over the named model
104      */

105     public static Model createModelOver( ModelSpec desc, String JavaDoc name )
106         { return desc.createModelOver( name ); }
107     
108     /**
109         Answer a fresh Model with the default specification and Standard reification style
110         [reification triples contribute to ReifiedStatements, and are visible to listStatements,
111         etc].
112     */

113     public static Model createDefaultModel()
114         { return createDefaultModel( Standard ); }
115                
116     /**
117         Answer a new memory-based model with the given reification style
118     */

119     public static Model createDefaultModel( ReificationStyle style )
120         { return new ModelCom( new GraphMem( style ) ); }
121
122     /**
123         Answer a read-only Model with all the statements of this Model and any
124         statements "hidden" by reification. That model is dynamic, ie
125         any changes this model will be reflected that one.
126     */

127     public static Model withHiddenStatements( Model m )
128         { return ModelReifier.withHiddenStatements( m ); }
129         
130     /**
131         construct a new memory-based model that does not capture reification triples
132         (but still handles reifyAs() and .as(ReifiedStatement).
133     */

134     public static Model createNonreifyingModel()
135         { return createDefaultModel( Minimal ); }
136         
137     /**
138         Answer a model that encapsulates the given graph. Existing prefixes are
139         undisturbed.
140         @param g A graph structure
141         @return A model presenting an API view of graph g
142     */

143     public static Model createModelForGraph( Graph g ) {
144         return new ModelCom( g );
145     }
146     
147     /**
148         Answer a ModelMaker that constructs memory-based Models that
149         are backed by files in the root directory. The Model is loaded from the
150         file when it is opened, and when the Model is closed it is written back.
151         The model is given the Standard reification style.
152         
153         @param root the name of the directory in which the backing files are held
154         @return a ModelMaker linked to the files in the root
155     */

156     public static ModelMaker createFileModelMaker( String JavaDoc root )
157         { return createFileModelMaker( root, Standard ); }
158     
159     /**
160         Answer a ModelMaker that constructs memory-based Models that
161         are backed by files in the root directory. The Model is loaded from the
162         file when it is opened, and when the Model is closed it is written back.
163         
164         @param root the name of the directory in which the backing files are held
165         @param style the desired reification style
166         @return a ModelMaker linked to the files in the root
167     */

168     public static ModelMaker createFileModelMaker( String JavaDoc root, ReificationStyle style )
169         { return new ModelMakerImpl( new FileGraphMaker( root, style ) ); }
170         
171     /**
172         Answer a ModelMaker that constructs memory-based Models that do
173         not persist past JVM termination. The model has the Standard reification
174         style.
175         
176         @return a ModelMaker that constructs memory-based models
177     */

178     public static ModelMaker createMemModelMaker()
179         { return createMemModelMaker( Standard ); }
180         
181     /**
182         Answer a ModelMaker that constructs memory-based Models that do
183         not persist past JVM termination, with the given reification style.
184         
185         @param style the reification style for the model
186         @return a ModelMaker that constructs memory-based models
187     */

188       public static ModelMaker createMemModelMaker( ReificationStyle style )
189         { return new ModelMakerImpl( new SimpleGraphMaker( style ) ); }
190         
191     /**
192         Answer a ModelMaker that accesses database-backed Models on
193         the database at the other end of the connection c with the usual
194         Standard reification style.
195         
196         @param c a connection to the database holding the models
197         @return a ModelMaker whose Models are held in the database at c
198     */

199     public static ModelMaker createModelRDBMaker( IDBConnection c )
200         { return createModelRDBMaker( c, Standard ); }
201         
202     /**
203         Answer a ModelMaker that accesses database-backed Models on
204         the database at the other end of the connection c with the given
205         reification style.
206         
207         @param c a connection to the database holding the models
208         @param style the desired reification style
209         @return a ModelMaker whose Models are held in the database at c
210     */

211     public static ModelMaker createModelRDBMaker
212         ( IDBConnection c, ReificationStyle style )
213         { return new ModelRDBMaker( new GraphRDBMaker( c, style ) ); }
214         
215     static class ModelRDBMaker extends ModelMakerImpl implements ModelMaker
216         {
217         public ModelRDBMaker( GraphRDBMaker gm ) { super( gm ); }
218         
219         public Model makeModel( Graph graphRDB )
220             { return new ModelRDB( (GraphRDB) graphRDB ); }
221         }
222     
223     /**
224         Answer a plain IDBConnection to a database with the given URL, with
225         the given user having the given password. For more complex ways of
226         forming a connection, see the DBConnection documentation.
227         
228         @param url the URL of the database
229         @param user the user name to use to access the database
230         @param password the password to use. WARNING: open text.
231         @param dbType the databate type: currently, "Oracle" or "MySQL".
232         @return the connection
233         @exception quite possibly
234     */

235     public static IDBConnection createSimpleRDBConnection
236         ( String JavaDoc url, String JavaDoc user, String JavaDoc password, String JavaDoc dbType )
237         { return new DBConnection( url, user, password, dbType ); }
238         
239     /**
240         Answer a plain IDBConnection to a database, with the arguments implicitly
241         supplied by system properties:
242     <p>
243         The database URL - jena.db.url
244         <br>The user - jena.db.user, or fails back to "test"
245         <br>The password - jena.db.password, or fails back to ""
246         <br>The db type - jena.db.type, or guessed from the URL
247     */

248     public static IDBConnection createSimpleRDBConnection()
249         {
250         return createSimpleRDBConnection
251             ( guessDBURL(), guessDBUser(), guessDBPassword(), guessDBType() );
252         }
253                
254     /**
255      * Return a Model through which all the RDFS entailments
256      * derivable from the given model are accessible. Some work is done
257      * when the inferenced model is created but each query will also trigger some
258      * additional inference work.
259      *
260      * @param model the Model containing both instance data and schema assertions to be inferenced over
261      */

262     public static InfModel createRDFSModel(Model model) {
263          Reasoner reasoner = ReasonerRegistry.getRDFSReasoner();
264          InfGraph graph = reasoner.bind( model.getGraph() );
265          return new InfModelImpl( graph );
266     }
267                
268     /**
269      * Return a Model through which all the RDFS entailments
270      * derivable from the given data and schema models are accessible.
271      * There is no strict requirement to separate schema and instance data between the two
272      * arguments.
273      *
274      * @param model a Model containing instance data assertions
275      * @param schema a Model containing RDFS schema data
276      */

277     public static InfModel createRDFSModel( Model schema, Model model ) {
278          Reasoner reasoner = ReasonerRegistry.getRDFSReasoner();
279          InfGraph graph = reasoner.bindSchema(schema.getGraph()).bind(model.getGraph());
280          return new InfModelImpl( graph );
281     }
282     
283     /**
284      * Build an inferred model by attaching the given RDF model to the given reasoner.
285      *
286      * @param reasoner the reasoner to use to process the data
287      * @param model the Model containing both instance data and schema assertions to be inferenced over
288      */

289     public static InfModel createInfModel( Reasoner reasoner, Model model ) {
290          InfGraph graph = reasoner.bind(model.getGraph());
291          return new InfModelImpl(graph);
292     }
293                
294     /**
295      * Build an inferred model by attaching the given RDF model to the given reasoner.
296      * This form of the call allows two data sets to be merged and reasoned over -
297      * conventionally one contains schema data and one instance data but this is not
298      * a formal requirement.
299      *
300      * @param reasoner the reasoner to use to process the data
301      * @param schema a Model containing RDFS schema data
302      * @param model a Model containing instance data assertions
303      */

304     public static InfModel createInfModel(Reasoner reasoner, Model schema, Model model) {
305          InfGraph graph = reasoner.bindSchema(schema.getGraph()).bind(model.getGraph());
306          return new InfModelImpl( graph );
307     }
308    
309     /**
310      * <p>
311      * Answer a new ontology model which will process in-memory models of
312      * ontologies expressed the default ontology language (OWL).
313      * The default document manager
314      * will be used to load the ontology's included documents.
315      * </p>
316      *
317      * @return A new ontology model
318      * @see OntModelSpec#getDefaultSpec
319      */

320     public static OntModel createOntologyModel() {
321         return createOntologyModel( ProfileRegistry.OWL_LANG );
322     }
323     
324     
325     /**
326      * <p>
327      * Answer a new ontology model which will process in-memory models of
328      * ontologies in the given language.
329      * The default document manager
330      * will be used to load the ontology's included documents.
331      * </p>
332      *
333      * @param languageURI The URI specifying the ontology language we want to process
334      * @return A new ontology model
335      * @see OntModelSpec#getDefaultSpec
336      */

337     public static OntModel createOntologyModel( String JavaDoc languageURI ) {
338         return createOntologyModel( OntModelSpec.getDefaultSpec( languageURI ), null );
339     }
340     
341     
342     /**
343      * <p>
344      * Answer a new ontology model which will process in-memory models of
345      * ontologies expressed the default ontology language (OWL).
346      * The default document manager
347      * will be used to load the ontology's included documents.
348      * </p>
349      *
350      * @param spec An ontology model specification that defines the language and reasoner to use
351      * @param maker A model maker that is used to get the initial store for the ontology (unless
352      * the base model is given),
353      * and create addtional stores for the models in the imports closure
354      * @param base The base model, which contains the contents of the ontology to be processed
355      * @return A new ontology model
356      * @see OntModelSpec
357      */

358     public static OntModel createOntologyModel( OntModelSpec spec, ModelMaker maker, Model base ) {
359         OntModelSpec _spec = new OntModelSpec( spec );
360         _spec.setImportModelMaker( maker );
361         
362         return createOntologyModel( _spec, base );
363     }
364     
365     
366     /**
367      * <p>
368      * Answer a new ontology model, constructed according to the given ontology model specification,
369      * and starting with the ontology data in the given model.
370      * </p>
371      *
372      * @param spec An ontology model specification object, that will be used to construct the ontology
373      * model with different options of ontology language, reasoner, document manager and storage model
374      * @param base An existing model to treat as an ontology model, or null.
375      * @return A new ontology model
376      * @see OntModelSpec
377      */

378     public static OntModel createOntologyModel( OntModelSpec spec, Model base ) {
379         return new OntModelImpl( spec, base );
380     }
381
382     /**
383      * Answer a new ontology model constructed according to the specification, which includes
384      * a ModelMaker which will create the necessary base model.
385     */

386     public static OntModel createOntologyModel( OntModelSpec spec )
387         { return new OntModelImpl( spec ); }
388         
389     
390     /**
391      * <p>Answer a model for processing DAML+OIL, using the legacy Jena1 DAML API. Users are encouraged
392      * to switch from the DAML-specific API to the new generic ontology API
393      * (see {@link #createOntologyModel(OntModelSpec, Model)}). The continuation of the DAML-specific
394      * API in Jena is not assured beyond Jena version 2.</p>
395      *
396      * @return A model for in-memory processing of DAML objects.
397      */

398     public static DAMLModel createDAMLModel() {
399         return new DAMLModelImpl( OntModelSpec.getDefaultSpec( ProfileRegistry.DAML_LANG ), null );
400     }
401
402     /**
403          Answer a new model that is the dynamic union of two other models.
404     */

405     public static Model createUnion(Model m1, Model m2)
406         { return createModelForGraph( new Union( m1.getGraph(), m2.getGraph() ) ); }
407
408     }
409     
410
411 /*
412     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
413     All rights reserved.
414
415     Redistribution and use in source and binary forms, with or without
416     modification, are permitted provided that the following conditions
417     are met:
418
419     1. Redistributions of source code must retain the above copyright
420        notice, this list of conditions and the following disclaimer.
421
422     2. Redistributions in binary form must reproduce the above copyright
423        notice, this list of conditions and the following disclaimer in the
424        documentation and/or other materials provided with the distribution.
425
426     3. The name of the author may not be used to endorse or promote products
427        derived from this software without specific prior written permission.
428
429     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
430     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
431     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
432     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
433     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
434     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
435     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
436     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
437     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
438     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
439 */
Popular Tags