KickJava   Java API By Example, From Geeks To Geeks.

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


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

6
7 package com.hp.hpl.jena.rdf.model.impl;
8
9 import junit.framework.Assert;
10
11 import com.hp.hpl.jena.rdf.model.*;
12 import com.hp.hpl.jena.graph.*;
13 import com.hp.hpl.jena.reasoner.*;
14 import com.hp.hpl.jena.reasoner.rulesys.impl.WrappedReasonerFactory;
15 import com.hp.hpl.jena.shared.*;
16 import com.hp.hpl.jena.vocabulary.*;
17
18 /**
19     A ModelSpec for InfModels. The description of an InfModel is the description of a
20     ModelMaker [for the base graph] plus the JenaModelSpec.reasonsWith property to give the
21     Resource who's URI identifies the reasoner to use [as per the ReasonerRegistry].
22     
23     @author kers
24 */

25 public class InfModelSpec extends ModelSpecImpl
26     {
27     /**
28         The Resource who's URI identifies the reasoner to use.
29     */

30     protected Resource reasonerResource;
31     protected Resource reasonerRoot;
32     protected ReasonerFactory factory;
33     
34     /**
35         Initialise an InfModelSpec using the ModelMaker specification and the value of
36         the JenaModelSpec.reasoner property.
37     */

38     public InfModelSpec( Resource root, Model description )
39         {
40         super( root, description );
41         Statement st = description.getRequiredProperty( null, JenaModelSpec.reasoner );
42         Statement xx = description.listStatements( root, JenaModelSpec.reasonsWith, (RDFNode) null ).nextStatement();
43         reasonerRoot = st.getSubject();
44         Resource yy = xx.getResource();
45         reasonerResource = st.getResource();
46         factory = getReasonerFactory( st.getSubject(), description );
47         }
48
49     /**
50         Answer an InfModel that does the reasoning as defined by the reasoner URI over
51         a new graph that is created by the ModelMaker.
52         
53         @return a new InfModel reasoning over a new base graph.
54     */

55     public Model createModel()
56         { return createModel( maker.getGraphMaker().createGraph() ); }
57         
58     /**
59         Answer a new InfModel based over the graph named in the underlying ModelMaker.
60         @see com.hp.hpl.jena.rdf.model.ModelSpec#createModelOver(java.lang.String)
61      */

62     public Model createModelOver( String JavaDoc name )
63         { return createModel( maker.getGraphMaker().createGraph( name ) ); }
64         
65     /**
66         Answer an InfModel based on the given Graph which does reasoning as required
67         by this Specs reasonerResource.
68         
69         @param base the base graph that the inference is done over
70         @return an inference model that does this spec's reasoning over the base
71      */

72     protected Model createModel( Graph base )
73         {
74         Reasoner reasoner = factory.create( null );
75         return new InfModelImpl( reasoner.bind( base ) );
76         }
77     
78     /**
79         Answer the maker property needed by descriptions.
80         @return JenaModelSpec.maker
81     */

82     public Property getMakerProperty()
83         { return JenaModelSpec.maker; }
84     
85     /**
86         Add this ModelMaker and Reasoner description to the supplied model under the
87         given name, and answer the descrption model.
88         
89         @param desc the model to augment with this description
90         @param self the resource to use as our name
91         @return desc, for cascading
92     */

93     public Model addDescription( Model desc, Resource self )
94         {
95         super.addDescription( desc, self );
96         Resource r = reasonerRoot;
97         desc.add( self, JenaModelSpec.reasonsWith, r );
98         desc.add( r, JenaModelSpec.reasoner, reasonerResource );
99         new ModelExtract( notJMS ) .extractInto( desc, r, description );
100         return desc;
101         }
102     
103     private final static TripleBoundary notJMSTriple = new TripleBoundary()
104         { public boolean stopAt( Triple t ) { return !t.getPredicate().getNameSpace().equals( JenaModelSpec.baseURI ); }};
105     
106     private static final StatementBoundary notJMS = new StatementBoundary()
107         {
108         public boolean stopAt( Statement s ) { return notJMSTriple.stopAt( s.asTriple() ); }
109         public TripleBoundary asTripleBoundary( Model ignored ) { return notJMSTriple; }
110         };
111
112     /**
113          Answer a ReasonerFactory described by the properties of the resource
114          <code>R</code> in the model <code>rs</code>. Will throw
115          NoReasonerSuppliedException if no jms:reasoner is supplied, or
116          NoSuchReasonerException if the reasoner value isn't known to
117          ReasonerRegistry. If any <code>ruleSetURL</code>s are supplied, the
118          reasoner factory must be a RuleReasonerFactory, and is wrapped so that
119          the supplied rules are specific to this Factory.
120     */

121     public static ReasonerFactory getReasonerFactory( Resource R, Model rs )
122         {
123         StmtIterator r = rs.listStatements( R, JenaModelSpec.reasoner, (RDFNode) null );
124         if (r.hasNext() == false) throw new NoReasonerSuppliedException();
125         Resource rr = r.nextStatement().getResource();
126         String JavaDoc rrs = rr.getURI();
127         ReasonerFactory rf = ReasonerRegistry.theRegistry().getFactory( rrs );
128         if (rf == null) throw new NoSuchReasonerException( rrs );
129         return new WrappedReasonerFactory( rf, ((Resource) R.inModel( rs )) );
130         }
131     }
132
133
134 /*
135     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
136     All rights reserved.
137
138     Redistribution and use in source and binary forms, with or without
139     modification, are permitted provided that the following conditions
140     are met:
141
142     1. Redistributions of source code must retain the above copyright
143        notice, this list of conditions and the following disclaimer.
144
145     2. Redistributions in binary form must reproduce the above copyright
146        notice, this list of conditions and the following disclaimer in the
147        documentation and/or other materials provided with the distribution.
148
149     3. The name of the author may not be used to endorse or promote products
150        derived from this software without specific prior written permission.
151
152     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
153     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
154     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
155     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
156     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
157     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
158     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
159     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
160     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
161     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
162 */
Popular Tags