001: /*
002: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: ModelFactory.java,v 1.54 2008/01/02 12:05:48 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model;
008:
009: import java.util.Set;
010:
011: import com.hp.hpl.jena.assembler.Assembler;
012: import com.hp.hpl.jena.assembler.AssemblerHelp;
013: import com.hp.hpl.jena.db.*;
014: import com.hp.hpl.jena.db.impl.GraphRDBMaker;
015: import com.hp.hpl.jena.graph.Factory;
016: import com.hp.hpl.jena.graph.Graph;
017: import com.hp.hpl.jena.graph.compose.Union;
018: import com.hp.hpl.jena.graph.impl.FileGraphMaker;
019: import com.hp.hpl.jena.graph.impl.SimpleGraphMaker;
020: import com.hp.hpl.jena.ontology.*;
021: import com.hp.hpl.jena.ontology.impl.OntModelImpl;
022: import com.hp.hpl.jena.rdf.model.impl.*;
023: import com.hp.hpl.jena.reasoner.*;
024: import com.hp.hpl.jena.shared.PrefixMapping;
025: import com.hp.hpl.jena.shared.ReificationStyle;
026:
027: /**
028: ModelFactory provides methods for creating standard kinds of Model.
029: (ModelFactoryBase is helper functions for it).
030: */
031:
032: public class ModelFactory extends ModelFactoryBase {
033: /**
034: No-one can make instances of this.
035: */
036: private ModelFactory() {
037: }
038:
039: /**
040: The standard reification style; quadlets contribute to reified statements,
041: and are visible to listStatements().
042: */
043: public static final ReificationStyle Standard = ReificationStyle.Standard;
044:
045: /**
046: The convenient reification style; quadlets contribute to reified statements,
047: but are invisible to listStatements().
048: */
049: public static final ReificationStyle Convenient = ReificationStyle.Convenient;
050:
051: /**
052: The minimal reification style; quadlets do not contribute to reified statements,
053: and are visible to listStatements().
054: */
055: public static final ReificationStyle Minimal = ReificationStyle.Minimal;
056:
057: /**
058: Each Model created by ModelFactory has a default set of prefix mappings.
059: These mappings are copied from a (static) default PrefixMapping which is
060: set by setDefaultModelPrefixes. It is the reference to a PrefixMapping that
061: is retained, not a copy of it, so a user may set the defaults with this method
062: and continue to modify it; the modifications will appear in the next model to
063: be created.
064: <p>
065: When a Model is created from an existing Graph, the prefixes of that Graph
066: are not disturbed; only ones not present in the Graph are added.
067:
068: @param pm the default prefixes to use
069: @return the previous default prefix mapping
070: */
071: public static PrefixMapping setDefaultModelPrefixes(PrefixMapping pm) {
072: return ModelCom.setDefaultModelPrefixes(pm);
073: }
074:
075: /**
076: Answer the current default model prefixes PrefixMapping object.
077: */
078: public static PrefixMapping getDefaultModelPrefixes() {
079: return ModelCom.getDefaultModelPrefixes();
080: }
081:
082: /**
083: Answer a Model constructed from the single resource in
084: <code>singleRoot</code> of type <code>ja:Model</code>.
085: See the Assembler howto (doc/assembler/assembler-howto.html)
086: for documentation of Assembler descriptions. See also
087: <code>findAssemblerRoots</code> to find the set of possible
088: roots in a description, and <code>assemblerModelFrom(Resource)</code>
089: for assembling a model from its single description.
090: */
091: public static Model assembleModelFrom(Model singleRoot) {
092: return assembleModelFrom(AssemblerHelp
093: .singleModelRoot(singleRoot));
094: }
095:
096: /**
097: Answer a Set of resources present in <code>m</code> that are
098: explicitly or implicitly of type ja:Object, ie, suitable as roots for
099: <code>assemblerModelFrom</code>. Note that the resource
100: objects returned need <i>not</i> have <code>m</code> as
101: their <code>getModel()</code> - they may be members of an
102: extended constructed model.
103: */
104: public static Set findAssemblerRoots(Model m) {
105: return AssemblerHelp.findAssemblerRoots(m);
106: }
107:
108: /**
109: Answer a Model as described the the Assembler specification rooted
110: at the Resource <code>root</code> in its Model. <code>Resource</code>
111: must be of rdf:type <code>ja:Object</code>, where <code>ja</code>
112: is the prefix of Jena Assembler objects.
113: */
114: public static Model assembleModelFrom(Resource root) {
115: return Assembler.general.openModel(root);
116: }
117:
118: /**
119: Answer a fresh Model with the default specification and Standard reification style
120: [reification triples contribute to ReifiedStatements, and are visible to listStatements,
121: etc].
122: */
123: public static Model createDefaultModel() {
124: return createDefaultModel(Standard);
125: }
126:
127: /**
128: Answer a new memory-based model with the given reification style
129: */
130: public static Model createDefaultModel(ReificationStyle style) {
131: return new ModelCom(Factory.createGraphMem(style));
132: }
133:
134: /**
135: Answer a read-only Model with all the statements of this Model and any
136: statements "hidden" by reification. That model is dynamic, ie
137: any changes this model will be reflected that one.
138: */
139: public static Model withHiddenStatements(Model m) {
140: return ModelReifier.withHiddenStatements(m);
141: }
142:
143: /**
144: construct a new memory-based model that does not capture reification triples
145: (but still handles reifyAs() and .as(ReifiedStatement).
146: */
147: public static Model createNonreifyingModel() {
148: return createDefaultModel(Minimal);
149: }
150:
151: /**
152: Answer a model that encapsulates the given graph. Existing prefixes are
153: undisturbed.
154: @param g A graph structure
155: @return A model presenting an API view of graph g
156: */
157: public static Model createModelForGraph(Graph g) {
158: return new ModelCom(g);
159: }
160:
161: /**
162: Answer a ModelMaker that constructs memory-based Models that
163: are backed by files in the root directory. The Model is loaded from the
164: file when it is opened, and when the Model is closed it is written back.
165: The model is given the Standard reification style.
166:
167: @param root the name of the directory in which the backing files are held
168: @return a ModelMaker linked to the files in the root
169: */
170: public static ModelMaker createFileModelMaker(String root) {
171: return createFileModelMaker(root, Standard);
172: }
173:
174: /**
175: Answer a ModelMaker that constructs memory-based Models that
176: are backed by files in the root directory. The Model is loaded from the
177: file when it is opened, and when the Model is closed it is written back.
178:
179: @param root the name of the directory in which the backing files are held
180: @param style the desired reification style
181: @return a ModelMaker linked to the files in the root
182: */
183: public static ModelMaker createFileModelMaker(String root,
184: ReificationStyle style) {
185: return new ModelMakerImpl(new FileGraphMaker(root, style));
186: }
187:
188: /**
189: Answer a ModelMaker that constructs memory-based Models that do
190: not persist past JVM termination. The model has the Standard reification
191: style.
192:
193: @return a ModelMaker that constructs memory-based models
194: */
195: public static ModelMaker createMemModelMaker() {
196: return createMemModelMaker(Standard);
197: }
198:
199: /**
200: Answer a ModelMaker that constructs memory-based Models that do
201: not persist past JVM termination, with the given reification style.
202:
203: @param style the reification style for the model
204: @return a ModelMaker that constructs memory-based models
205: */
206: public static ModelMaker createMemModelMaker(ReificationStyle style) {
207: return new ModelMakerImpl(new SimpleGraphMaker(style));
208: }
209:
210: /**
211: Answer a ModelMaker that accesses database-backed Models on
212: the database at the other end of the connection c with the usual
213: Standard reification style.
214:
215: @param c a connection to the database holding the models
216: @return a ModelMaker whose Models are held in the database at c
217: */
218: public static ModelMaker createModelRDBMaker(IDBConnection c) {
219: return createModelRDBMaker(c, Standard);
220: }
221:
222: /**
223: Answer a ModelMaker that accesses database-backed Models on
224: the database at the other end of the connection c with the given
225: reification style.
226:
227: @param c a connection to the database holding the models
228: @param style the desired reification style
229: @return a ModelMaker whose Models are held in the database at c
230: */
231: public static ModelMaker createModelRDBMaker(IDBConnection c,
232: ReificationStyle style) {
233: return new ModelRDBMaker(new GraphRDBMaker(c, style));
234: }
235:
236: static class ModelRDBMaker extends ModelMakerImpl implements
237: ModelMaker {
238: public ModelRDBMaker(GraphRDBMaker gm) {
239: super (gm);
240: }
241:
242: public Model makeModel(Graph graphRDB) {
243: return new ModelRDB((GraphRDB) graphRDB);
244: }
245: }
246:
247: /**
248: Answer a plain IDBConnection to a database with the given URL, with
249: the given user having the given password. For more complex ways of
250: forming a connection, see the DBConnection documentation.
251:
252: @param url the URL of the database
253: @param user the user name to use to access the database
254: @param password the password to use. WARNING: open text.
255: @param dbType the databate type: currently, "Oracle" or "MySQL".
256: @return the connection
257: @exception quite possibly
258: */
259: public static IDBConnection createSimpleRDBConnection(String url,
260: String user, String password, String dbType) {
261: return new DBConnection(url, user, password, dbType);
262: }
263:
264: /**
265: Answer a plain IDBConnection to a database, with the arguments implicitly
266: supplied by system properties:
267: <p>
268: The database URL - jena.db.url
269: <br>The user - jena.db.user, or fails back to "test"
270: <br>The password - jena.db.password, or fails back to ""
271: <br>The db type - jena.db.type, or guessed from the URL
272: */
273: public static IDBConnection createSimpleRDBConnection() {
274: return createSimpleRDBConnection(guessDBURL(), guessDBUser(),
275: guessDBPassword(), guessDBType());
276: }
277:
278: /**
279: * Return a Model through which all the RDFS entailments
280: * derivable from the given model are accessible. Some work is done
281: * when the inferenced model is created but each query will also trigger some
282: * additional inference work.
283: *
284: * @param model the Model containing both instance data and schema assertions to be inferenced over
285: */
286: public static InfModel createRDFSModel(Model model) {
287: Reasoner reasoner = ReasonerRegistry.getRDFSReasoner();
288: InfGraph graph = reasoner.bind(model.getGraph());
289: return new InfModelImpl(graph);
290: }
291:
292: /**
293: * Return a Model through which all the RDFS entailments
294: * derivable from the given data and schema models are accessible.
295: * There is no strict requirement to separate schema and instance data between the two
296: * arguments.
297: *
298: * @param model a Model containing instance data assertions
299: * @param schema a Model containing RDFS schema data
300: */
301: public static InfModel createRDFSModel(Model schema, Model model) {
302: Reasoner reasoner = ReasonerRegistry.getRDFSReasoner();
303: InfGraph graph = reasoner.bindSchema(schema.getGraph()).bind(
304: model.getGraph());
305: return new InfModelImpl(graph);
306: }
307:
308: /**
309: * Build an inferred model by attaching the given RDF model to the given reasoner.
310: *
311: * @param reasoner the reasoner to use to process the data
312: * @param model the Model containing both instance data and schema assertions to be inferenced over
313: */
314: public static InfModel createInfModel(Reasoner reasoner, Model model) {
315: InfGraph graph = reasoner.bind(model.getGraph());
316: return new InfModelImpl(graph);
317: }
318:
319: /**
320: * Build an inferred model by attaching the given RDF model to the given reasoner.
321: * This form of the call allows two data sets to be merged and reasoned over -
322: * conventionally one contains schema data and one instance data but this is not
323: * a formal requirement.
324: *
325: * @param reasoner the reasoner to use to process the data
326: * @param schema a Model containing RDFS schema data
327: * @param model a Model containing instance data assertions
328: */
329: public static InfModel createInfModel(Reasoner reasoner,
330: Model schema, Model model) {
331: InfGraph graph = reasoner.bindSchema(schema.getGraph()).bind(
332: model.getGraph());
333: return new InfModelImpl(graph);
334: }
335:
336: /**
337: * Build an inference model from an InfGraph. Graphs and InfGraphs
338: * are internal implementation level objects rather than normal user
339: * objects so this method should only be used if you are sure this is
340: * what you need.
341: * @param g and inference graph
342: * @return the same graph wrapped up as an InfModel
343: */
344: public static InfModel createInfModel(InfGraph g) {
345: return new InfModelImpl(g);
346: }
347:
348: /**
349: * <p>
350: * Answer a new ontology model which will process in-memory models of
351: * ontologies expressed the default ontology language (OWL).
352: * The default document manager
353: * will be used to load the ontology's included documents.
354: * </p>
355: * <p><strong>Note:</strong>The default model chosen for OWL, RDFS and DAML+OIL
356: * includes a weak reasoner that includes some entailments (such as
357: * transitive closure on the sub-class and sub-property hierarchies). Users
358: * who want either no inference at all, or alternatively
359: * more complete reasoning, should use
360: * one of the other <code>createOntologyModel</code> methods that allow the
361: * preferred OntModel specification to be stated.</p>
362: * @return A new ontology model
363: * @see OntModelSpec#getDefaultSpec
364: * @see #createOntologyModel(OntModelSpec, Model)
365: */
366: public static OntModel createOntologyModel() {
367: return createOntologyModel(ProfileRegistry.OWL_LANG);
368: }
369:
370: /**
371: * <p>
372: * Answer a new ontology model which will process in-memory models of
373: * ontologies in the given language.
374: * The default document manager
375: * will be used to load the ontology's included documents.
376: * </p>
377: *
378: * @param languageURI The URI specifying the ontology language we want to process
379: * @return A new ontology model
380: * @see OntModelSpec#getDefaultSpec
381: */
382: public static OntModel createOntologyModel(String languageURI) {
383: return createOntologyModel(OntModelSpec
384: .getDefaultSpec(languageURI), null);
385: }
386:
387: /**
388: * <p>
389: * Answer a new ontology model which will process in-memory models of
390: * ontologies expressed the default ontology language (OWL).
391: * The default document manager
392: * will be used to load the ontology's included documents.
393: * </p>
394: *
395: * @param spec An ontology model specification that defines the language and reasoner to use
396: * @param maker A model maker that is used to get the initial store for the ontology (unless
397: * the base model is given),
398: * and create addtional stores for the models in the imports closure
399: * @param base The base model, which contains the contents of the ontology to be processed
400: * @return A new ontology model
401: * @see OntModelSpec
402: */
403: public static OntModel createOntologyModel(OntModelSpec spec,
404: ModelMaker maker, Model base) {
405: OntModelSpec _spec = new OntModelSpec(spec);
406: _spec.setImportModelMaker(maker);
407:
408: return createOntologyModel(_spec, base);
409: }
410:
411: /**
412: * <p>
413: * Answer a new ontology model, constructed according to the given ontology model specification,
414: * and starting with the ontology data in the given model.
415: * </p>
416: *
417: * @param spec An ontology model specification object, that will be used to construct the ontology
418: * model with different options of ontology language, reasoner, document manager and storage model
419: * @param base An existing model to treat as an ontology model, or null.
420: * @return A new ontology model
421: * @see OntModelSpec
422: */
423: public static OntModel createOntologyModel(OntModelSpec spec,
424: Model base) {
425: return new OntModelImpl(spec, base);
426: }
427:
428: /**
429: * Answer a new ontology model constructed according to the specification, which includes
430: * a ModelMaker which will create the necessary base model.
431: */
432: public static OntModel createOntologyModel(OntModelSpec spec) {
433: return new OntModelImpl(spec);
434: }
435:
436: /**
437: Answer a new model that is the dynamic union of two other models. By
438: <i>dynamic union</i>, we mean that changes to either <code>m1</code>
439: or <code>m2</code> will be reflected in the result model, and
440: <i>vice versa</i>: specifically, additions to and removals from the union
441: will be implemented as operations on <code>m1</code>
442: <strong>only</strong>. See also the behaviour of OntModel
443: and the MultiUnion class.
444: <p>
445: <code>createUnion</code> only creates two-element unions.
446: */
447: public static Model createUnion(Model m1, Model m2) {
448: return createModelForGraph(new Union(m1.getGraph(), m2
449: .getGraph()));
450: }
451:
452: }
453:
454: /*
455: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
456: All rights reserved.
457:
458: Redistribution and use in source and binary forms, with or without
459: modification, are permitted provided that the following conditions
460: are met:
461:
462: 1. Redistributions of source code must retain the above copyright
463: notice, this list of conditions and the following disclaimer.
464:
465: 2. Redistributions in binary form must reproduce the above copyright
466: notice, this list of conditions and the following disclaimer in the
467: documentation and/or other materials provided with the distribution.
468:
469: 3. The name of the author may not be used to endorse or promote products
470: derived from this software without specific prior written permission.
471:
472: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
473: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
474: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
475: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
476: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
477: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
478: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
479: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
480: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
481: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
482: */
|