KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > impl > ModelSpecImpl


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: ModelSpecImpl.java,v 1.49 2005/04/10 12:45:49 chris-dollin Exp $
5 */

6
7 package com.hp.hpl.jena.rdf.model.impl;
8
9 import com.hp.hpl.jena.rdf.model.*;
10 import com.hp.hpl.jena.util.FileManager;
11 import com.hp.hpl.jena.vocabulary.*;
12 import com.hp.hpl.jena.shared.*;
13
14 import java.util.*;
15
16 /**
17     An abstract base class for implementations of ModelSpec. It provides the base
18     functionality of providing a ModelMaker (different sub-classes use this for different
19     purposes) and utility methods for reading and creating RDF descriptions. It also
20     provides a value table associating freshly-constructed bnodes with arbitrary Java
21     values, so program-constructed specifications can pass on database connections,
22     actual document managers, and so forth.
23     
24     @author kers
25 */

26 public abstract class ModelSpecImpl implements ModelSpec
27     {
28     /**
29         The ModelMaker that may be used by sub-classes.
30     */

31     protected ModelMaker maker;
32     
33     /**
34         The map which associates bnodes with Java values.
35     */

36     private static Map values = new HashMap();
37     
38     /**
39         Initialise this ModelSpec with the supplied non-nullModeMaker.
40         
41         @param maker the ModelMaker to use, or null to create a fresh one
42     */

43     public ModelSpecImpl( ModelMaker maker )
44         {
45         if (maker == null) throw new RuntimeException JavaDoc( "null maker not allowed" );
46         this.maker = maker;
47         }
48         
49     public ModelSpecImpl( Resource root, Model description )
50         {
51         this( createMaker( getMaker( root, description ), description ) );
52         this.root = root;
53         this.description = description;
54         }
55     
56     public static final Model emptyModel = ModelFactory.createDefaultModel();
57     
58     public static final Model defaultModel = ModelFactory.createDefaultModel();
59     
60     public static final Resource emptyResource = emptyModel.createResource();
61     
62     protected Model description = emptyModel;
63     
64     protected Resource root = ResourceFactory.createResource( "" );
65         
66     /**
67         Answer a Model created according to this ModelSpec; left abstract for subclasses
68         to implement.
69     */

70     public abstract Model createModel();
71     
72     public Model getModel()
73         { return defaultModel; }
74     
75     /**
76         Answer a Model created according to this ModelSpec and based on an underlying
77         Model with the given name.
78          
79         @see com.hp.hpl.jena.rdf.model.ModelSpec#createModelOver(java.lang.String)
80      */

81     public abstract Model createModelOver( String JavaDoc name );
82     
83     /**
84         Answer the JenaModelSpec subproperty of JenaModelSpec.maker that describes the relationship
85         between this specification and its ModelMaker.
86         
87         @return a sub-property of JenaModelSpec.maker
88     */

89     public abstract Property getMakerProperty();
90    
91     /**
92         Answer a Model, as per the specification of ModelSource; if the name
93         is useful, use it, otherwise don't bother. Default implementation is
94         to return a fresh model.
95     */

96     public Model openModel( String JavaDoc URI )
97         { return ModelFactory.createDefaultModel(); }
98     
99     /**
100         Answer null, as ModelSpecs as ModelSources don't remember any Models.
101         This is consistent with openModel() always creating a new Model.
102     */

103     public Model openModelIfPresent( String JavaDoc URI )
104         { return null; }
105         
106     public static Resource getMaker( Resource root, Model desc )
107         {
108         StmtIterator it = desc.listStatements( root, JenaModelSpec.maker, (RDFNode) null );
109         if (it.hasNext())
110             return it.nextStatement().getResource();
111         else
112             {
113             Resource r = desc.createResource();
114             desc.add( root, JenaModelSpec.maker, r );
115             return r;
116             // throw new BadDescriptionException( "no jms:maker for " + root, desc );
117
}
118         }
119         
120     /**
121         Answer the ModelMaker that this ModelSpec uses.
122         @return the embedded ModelMaker
123     */

124     public ModelMaker getModelMaker()
125         { return maker; }
126         
127     public Model getDescription()
128         { return getDescription( ResourceFactory.createResource() ); }
129         
130     public Model getDescription( Resource root )
131         { return addDescription( ModelFactory.createDefaultModel(), root ); }
132
133     public Model addDescription( Model desc, Resource root )
134         {
135         Resource makerRoot = desc.createResource();
136         desc.add( root, JenaModelSpec.maker, makerRoot );
137         maker.addDescription( desc, makerRoot );
138         return desc;
139         }
140         
141     /**
142         Answer a new bnode Resource associated with the given value. The mapping from
143         bnode to value is held in a single static table, and is not intended to hold many
144         objects; there is no provision for garbage-collecting them [this might eventually be
145         regarded as a bug].
146         
147         @param value a Java value to be remembered
148         @return a fresh bnode bound to <code>value</code>
149     */

150     public static Resource createValue( Object JavaDoc value )
151         {
152         Resource it = ResourceFactory.createResource();
153         values.put( it, value );
154         return it;
155         }
156         
157     /**
158         Answer the value bound to the supplied bnode, or null if there isn't one or the
159         argument isn't a bnode.
160         
161         @param it the RDF node to be looked up in the <code>createValue</code> table.
162         @return the associated value, or null if there isn't one.
163     */

164     public static Object JavaDoc getValue( RDFNode it )
165         { return values.get( it ); }
166         
167     /**
168         Answer the unique subject with the given rdf:type.
169         
170         @param m the model in which the typed subject is sought
171         @param type the RDF type the subject must have
172         @return the unique S such that (S rdf:type type)
173         @exception BadDescriptionException if there's not exactly one subject
174     */

175     public static Resource findRootByType( Model description, Resource type )
176         { return ModelSpecFactory.findRootByType( ModelSpecFactory.withSchema( description ), type ); }
177     
178     /**
179         Answer a ModelMaker that conforms to the supplied description. The Maker
180         is found from the ModelMakerCreatorRegistry by looking up the most
181         specific type of the unique object with type JenaModelSpec.MakerSpec.
182         
183         @param d the model containing the description
184         @return a ModelMaker fitting that description
185     */

186     public static ModelMaker createMaker( Model description )
187         { Model d = ModelSpecFactory.withSchema( description );
188         return createMakerByRoot( ModelSpecFactory.findRootByType( d, JenaModelSpec.MakerSpec ), d ); }
189         
190     public static ModelMaker createMaker( Resource root, Model d )
191         { return createMakerByRoot( root, ModelSpecFactory.withSchema( d ) ); }
192         
193     public static ModelMaker createMakerByRoot( Resource root, Model fullDesc )
194         { Resource type = ModelSpecFactory.findSpecificType( (Resource) root.inModel( fullDesc ), JenaModelSpec.MakerSpec );
195         ModelMakerCreator mmc = ModelMakerCreatorRegistry.findCreator( type );
196         if (mmc == null) throw new RuntimeException JavaDoc( "no maker type" );
197         return mmc.create( fullDesc, root ); }
198         
199     /**
200         Read a model from a given URI.
201         @param source the resource who's URI specifies what to laod
202         @return the model as loaded from the resource URI
203      */

204     public static Model readModel( Resource source )
205         {
206         String JavaDoc uri = source.getURI();
207         return FileManager.get().loadModel( uri );
208         }
209
210     protected Model loadFiles(Model m)
211         {
212         StmtIterator it = description.listStatements( root, JenaModelSpec.loadWith, (RDFNode) null );
213         while (it.hasNext()) FileManager.get().readModel( m, it.nextStatement().getResource().getURI() );
214         return m;
215         }
216                 
217     }
218
219 /*
220     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
221     All rights reserved.
222
223     Redistribution and use in source and binary forms, with or without
224     modification, are permitted provided that the following conditions
225     are met:
226
227     1. Redistributions of source code must retain the above copyright
228        notice, this list of conditions and the following disclaimer.
229
230     2. Redistributions in binary form must reproduce the above copyright
231        notice, this list of conditions and the following disclaimer in the
232        documentation and/or other materials provided with the distribution.
233
234     3. The name of the author may not be used to endorse or promote products
235        derived from this software without specific prior written permission.
236
237     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
238     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
239     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
240     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
241     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
242     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
243     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
245     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
246     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
247 */
Popular Tags