KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2     (c) Copyright 2005, Hewlett-Packard Development Company, LP
3     All rights reserved.
4     [See end of file]
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.shared.*;
11 import com.hp.hpl.jena.vocabulary.*;
12
13 /**
14      ModelSpecFactory is the [new] class for delivering new ModelSpec objects
15      described by their RDF specifications. The ModelSpec objects are created
16      by ModelSpecCreator objects found in a ModelSpecCreatorRegistry, where they
17      are identified by their RDF types as found in the RDF description.
18      ModelSpecFactory finds [if necessary] the root of the description, finds the
19      most specific type of that root which is a subclass of JenaModelSpec.ModelSpec, and
20      invokes the corresponding creator object.
21      
22      <p>ModelSpecFactory has no instance methods.
23      
24      @author kers
25 */

26 public class ModelSpecFactory
27     {
28     /**
29         The registry which is used when none is supplied to ModelSpecFactory.
30     */

31     protected static ModelSpecCreatorRegistry defaultRegistry = ModelSpecCreatorRegistry.instance;
32     
33     /**
34         Answer a ModelSpec as described by the resource <code>root</code> in the model
35         <code>m</code>, using the creator registry <code>registry</code>. If there is
36         no such creator, a <code>BadDescriptionException</code> is thrown.
37     */

38     protected static ModelSpec create( ModelSpecCreatorRegistry registry, Model m, Resource root )
39         { Resource type = findSpecificType( root, JenaModelSpec.ModelSpec );
40         ModelSpecCreator sc = registry.getCreator( type );
41         if (sc == null) throw new BadDescriptionException( "no model-spec creator found for " + type, m );
42         return sc.create( root, m ); }
43     
44     /**
45         As per <code>create(ModelSpecCreatorRegistry,Model,Resource), with the Resource
46         being the unique subject of <code>m</code> which has type <code>JenaModelSpec.ModelSpec</code>.
47     */

48     public static ModelSpec createSpec( ModelSpecCreatorRegistry registry, Model m )
49         { Model full = withSchema( m );
50         return create( registry, full, findRootByType( full, JenaModelSpec.ModelSpec ) ); }
51     
52     /**
53         As per <code>create(ModelSpecCreatorRegistry,Model,Resource), with the Registry
54         being the default instance of ModelSpecCreatorRegistry.
55     */

56     public static ModelSpec createSpec( Model m, Resource root )
57         { Model full = withSchema( m );
58         return create( defaultRegistry, full, (Resource) root.inModel( full ) ); }
59
60     /**
61         As per <code>create(ModelSpecCreatorRegistry,Model,Resource), with the Registry
62         being the default Registry and the Resource being the unique subject of
63         <code>m</code> with type <code>JenaModelSpec.ModelSpec</code>.
64     */

65     public static ModelSpec createSpec( Model m )
66         { Model full = withSchema( m );
67         return create( defaultRegistry, full, findRootByType( full, JenaModelSpec.ModelSpec ) ); }
68
69     /**
70         Answer a wrapping of <code>m</code> as an RDFS model using the JenaModelSpec schema.
71     */

72     public static Model withSchema( Model m )
73         { return withSpecSchema( m ); }
74
75     /**
76         Answer the unique subject of <code>m</code> which has type <code>type</code>.
77         If there is no such subject, throw a <code>BadDescriptionException</code>.
78     */

79     public static Resource findRootByType( Model m, Resource type )
80         { StmtIterator it = m.listStatements( null, RDF.type, type );
81         if (!it.hasNext()) throw new BadDescriptionNoRootException( m, type );
82         Resource root = it.nextStatement().getSubject();
83         if (it.hasNext()) throw new BadDescriptionMultipleRootsException( m, type );
84         return root; }
85     
86     /**
87         Answer the "most specific" type of root in desc which is an instance of type.
88         We assume a single inheritance thread starting with that type. The model
89         should contain the subclass closure (ie either be complete, or an inference
90         model which will generate completeness).
91         
92         @param root the subject whose type is to be found
93         @param type the base type for the search
94         @return T such that (root type T) and if (root type T') then (T' subclassof T)
95     */

96     public static Resource findSpecificType( Resource root, Resource type )
97         { StmtIterator it = root.listProperties( RDF.type );
98         Model desc = root.getModel();
99         while (it.hasNext())
100             { Resource candidate = it.nextStatement().getResource();
101             if (desc.contains( candidate, RDFS.subClassOf, type )) type = candidate; }
102         return type; }
103
104     public static Model withSpecSchema( Model m )
105         {
106         Model result = ModelFactory.createDefaultModel();
107         Model schema = JenaModelSpec.getSchema();
108         result.add( m );
109         addJMSSubclassesFrom( result, schema );
110         addDomainTypes( result, m, schema );
111         addSupertypesFrom( result, schema );
112         addSupertypesFrom( result, m );
113         return result;
114         }
115     
116     protected static final RDFNode nullObject = (RDFNode) null;
117
118     protected static void addDomainTypes( Model result, Model m, Model schema )
119         {
120         for (StmtIterator it = schema.listStatements( null, RDFS.domain, nullObject ); it.hasNext();)
121             {
122             Statement s = it.nextStatement();
123             Property property = (Property) s.getSubject().as( Property.class );
124             for (StmtIterator x = m.listStatements( null, property, nullObject ); x.hasNext();)
125                 {
126                 Statement t = x.nextStatement();
127                 result.add( t.getSubject(), RDF.type, s.getObject() );
128                 }
129             }
130         }
131
132     protected static void addJMSSubclassesFrom( Model result, Model schema )
133         {
134         for (StmtIterator it = schema.listStatements( null, RDFS.subClassOf, nullObject ); it.hasNext();)
135             {
136             Statement s = it.nextStatement();
137             if (s.getSubject().getNameSpace().equals( JenaModelSpec.baseURI ) && s.getResource().getNameSpace().equals( JenaModelSpec.baseURI ))
138                 result.add( s );
139             }
140         }
141
142     protected static void addSupertypesFrom( Model result, Model source )
143         {
144         Model temp = ModelFactory.createDefaultModel();
145         for (StmtIterator it = result.listStatements( null, RDF.type, nullObject ); it.hasNext();)
146             {
147             Statement s = it.nextStatement();
148             for (StmtIterator subclasses = source.listStatements( s.getResource(), RDFS.subClassOf, nullObject ); subclasses.hasNext();)
149                 temp.add( s.getSubject(), RDF.type, subclasses.nextStatement().getObject() );
150             }
151         result.add( temp );
152         }
153
154     }
155
156
157 /*
158  * (c) Copyright 2005 Hewlett-Packard Development Company, LP
159  * All rights reserved.
160  *
161  * Redistribution and use in source and binary forms, with or without
162  * modification, are permitted provided that the following conditions
163  * are met:
164  * 1. Redistributions of source code must retain the above copyright
165  * notice, this list of conditions and the following disclaimer.
166  * 2. Redistributions in binary form must reproduce the above copyright
167  * notice, this list of conditions and the following disclaimer in the
168  * documentation and/or other materials provided with the distribution.
169  * 3. The name of the author may not be used to endorse or promote products
170  * derived from this software without specific prior written permission.
171  *
172  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
173  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
174  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
175  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
176  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
177  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
178  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
179  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
180  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
181  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
182 */
Popular Tags