KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > test > TestModelSpecRevised


1 /*
2   (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP, all rights reserved.
3   [See end of file]
4   $Id: TestModelSpecRevised.java,v 1.27 2005/04/10 12:45:50 chris-dollin Exp $
5 */

6 package com.hp.hpl.jena.rdf.model.test;
7
8 import java.util.*;
9
10 import com.hp.hpl.jena.graph.*;
11 import com.hp.hpl.jena.mem.GraphMem;
12 import com.hp.hpl.jena.rdf.model.*;
13 import com.hp.hpl.jena.rdf.model.impl.*;
14 import com.hp.hpl.jena.reasoner.*;
15 import com.hp.hpl.jena.reasoner.rulesys.*;
16 import com.hp.hpl.jena.shared.*;
17 import com.hp.hpl.jena.util.FileManager;
18 import com.hp.hpl.jena.vocabulary.*;
19
20 import junit.framework.TestSuite;
21
22 /**
23      TestModelSpecRevised
24      @author kers
25 */

26 public class TestModelSpecRevised extends ModelTestBase
27     {
28     public TestModelSpecRevised( String JavaDoc name )
29         { super( name ); }
30     
31     public static TestSuite suite()
32          {
33 // TestSuite result = new TestSuite();
34
// result.addTest( new TestModelSpecRevised( "testURLRulesets" ) );
35
// return result;
36
return new TestSuite( TestModelSpecRevised.class );
37          }
38     
39     public static final Resource A = resource( "_a" );
40
41     public void testTriples()
42         {
43         check( "a R b", "a R b", new Object JavaDoc[] {} );
44         check( "a R b", "a R ?0", new Object JavaDoc[] {"b"} );
45         check( "a P b", "a ?0 ?1", new Object JavaDoc[] {"P", "b"} );
46         check( "_a P 17; Q P _a", "?0 ?1 ?2; ?3 ?1 ?0", new Object JavaDoc[] {"_a", "P", "17", "Q"} );
47         }
48     
49     public void check( String JavaDoc wanted, String JavaDoc template, Object JavaDoc [] args )
50         {
51         Model m = modelWithStatements( template, args );
52         assertIsoModels( modelWithStatements( wanted ), m );
53         }
54     
55     /**
56      * @param string
57      * @param objects
58      * @return
59      */

60     private static Model modelWithStatements( String JavaDoc facts, Object JavaDoc[] objects )
61         {
62         Graph g = new GraphMem();
63         StringTokenizer semis = new StringTokenizer( facts, ";" );
64         while (semis.hasMoreTokens())
65             g.add( replace( triple( PrefixMapping.Extended, semis.nextToken() ), objects ) );
66         return ModelFactory.createModelForGraph( g );
67         }
68
69     /**
70      * @param t
71      * @param objects
72      * @return
73      */

74     private static Triple replace( Triple t, Object JavaDoc[] objects )
75         {
76         return Triple.create
77             ( replace( t.getSubject(), objects ), replace( t.getPredicate(), objects ), replace( t.getObject(), objects ) );
78         }
79
80     /**
81      * @param n
82      * @return
83      */

84     private static Node replace( Node n, Object JavaDoc [] objects )
85         {
86         if (n.isVariable())
87             {
88             String JavaDoc name = n.getName();
89             if (Character.isDigit( name.charAt(0)))
90                 return Node.create( (String JavaDoc) objects[Integer.parseInt( name )] );
91             }
92         return n;
93         }
94
95     protected static void assertSameRules( List wanted, List got )
96         {
97         assertEquals( new HashSet( wanted ), new HashSet( got ) );
98         }
99     
100     public void testNoReasonerSuppliedException()
101         {
102         Model rs = modelWithStatements( "_a rdf:type jms:ReasonerSpec" );
103         Resource A = resource( "_a" );
104         try { InfModelSpec.getReasonerFactory( A, rs ); fail( "should catch missing reasoner" ); }
105         catch (NoReasonerSuppliedException e) { pass(); }
106         }
107
108     public void testNoSuchReasonerException()
109         {
110         Model rs = modelWithStatements( "_a rdf:type jms:ReasonerSpec; _a jms:reasoner nosuch:reasoner" );
111         Resource A = resource( "_a" );
112         try
113             { InfModelSpec.getReasonerFactory( A, rs );
114             fail( "should catch unknown reasoner" ); }
115         catch (NoSuchReasonerException e)
116             { assertEquals( "nosuch:reasoner", e.getURI() );
117             assertContains( "nosuch:reasoner", e.toString() ); }
118         }
119     
120     public void testGetOWLFBReasoner()
121         {
122         testGetReasoner( OWLFBRuleReasonerFactory.URI, OWLFBRuleReasoner.class );
123         }
124         
125     public void testGetRDFSRuleReasoner()
126         {
127         testGetReasoner( RDFSRuleReasonerFactory.URI, RDFSRuleReasoner.class );
128         }
129     
130     public void testRulesetURLFails()
131         {
132         String JavaDoc uri = GenericRuleReasonerFactory.URI;
133         Model rs = modelWithStatements( "_a jms:reasoner ?0; _a jms:ruleSetURL nowhere:man", new Object JavaDoc[] { uri } );
134         try { InfModelSpec.getReasonerFactory( A, rs ).create( null ); fail( "should report ruleset failure" ); }
135         catch (RulesetNotFoundException e) { assertEquals( "nowhere:man", e.getURI() ); }
136         }
137     
138     public void testEmptyRulesetURLWorks()
139         {
140         testRuleSetURL( GenericRuleReasonerFactory.URI, file( "empty.rules" ) );
141         }
142
143     public void testNonEmptyRulesetURLWorks()
144         {
145         testRuleSetURL( GenericRuleReasonerFactory.URI, file( "example.rules" ) );
146         }
147     
148     public void testMultipleRulesetURLsWork()
149         {
150         String JavaDoc factoryURI = GenericRuleReasonerFactory.URI;
151         String JavaDoc rulesA = file( "example.rules" ), rulesB = file( "extra.rules" );
152         List rules = append( Rule.rulesFromURL( rulesA ), Rule.rulesFromURL( rulesB ) );
153         Model rs = modelWithStatements( "_a jms:reasoner ?0; _a jms:ruleSetURL ?1; _a jms:ruleSetURL ?2", new Object JavaDoc[] {factoryURI, rulesA, rulesB});
154         ReasonerFactory rf = InfModelSpec.getReasonerFactory( A, rs );
155         RuleReasoner gr = (RuleReasoner) rf.create( null );
156         assertSameRules( rules, gr.getRules() );
157         }
158     
159     public void testInlineRulesets()
160         {
161         String JavaDoc factoryURI = GenericRuleReasonerFactory.URI;
162         String JavaDoc ruleStringA = "[rdfs2: (?x ?p ?y), (?p rdfs:domain ?c) -> (?x rdf:type ?c)]";
163         String JavaDoc ruleStringB = "[rdfs9: (?x rdfs:subClassOf ?y), (?a rdf:type ?x) -> (?a rdf:type ?y)]";
164         List rules = append( Rule.parseRules( ruleStringA ), Rule.parseRules( ruleStringB ) );
165         Model rs = rSpec( factoryURI )
166             .add( A, JenaModelSpec.ruleSet, resource( "onward:rules" ) )
167             .add( resource( "onward:rules" ), JenaModelSpec.hasRule, ruleStringA )
168             .add( resource( "onward:rules" ), JenaModelSpec.hasRule, ruleStringB );
169         ReasonerFactory rf = InfModelSpec.getReasonerFactory( A, rs );
170         RuleReasoner gr = (RuleReasoner) rf.create( null );
171         assertSameRules( rules, gr.getRules() );
172         }
173     
174     public void testURLRulesets()
175         {
176         String JavaDoc factoryURI = GenericRuleReasonerFactory.URI;
177         String JavaDoc ruleFileA = file( "example.rules" );
178         String JavaDoc ruleFileB = file( "extra.rules" );
179         List rules = append( Rule.rulesFromURL( ruleFileA ), Rule.rulesFromURL( ruleFileB ) );
180         Model rs = rSpec( factoryURI )
181             .add( A, JenaModelSpec.ruleSet, resource( "onward:rules" ) )
182             .add( resource( "onward:rules" ), JenaModelSpec.ruleSetURL, resource( ruleFileA ) )
183             .add( resource( "onward:rules" ), JenaModelSpec.ruleSetURL, resource( ruleFileB ) );
184         ReasonerFactory rf = InfModelSpec.getReasonerFactory( A, rs );
185         RuleReasoner gr = (RuleReasoner) rf.create( null );
186         assertSameRules( rules, gr.getRules() );
187         }
188     
189     public void testSchema()
190         {
191         ReasonerRegistry.theRegistry().register( "fake:factory", new FakeFactory() );
192         String JavaDoc d =
193             "_root jms:reasonsWith _reasoner"
194             + "; _reasoner jms:reasoner fake:factory"
195             + "; _root jms:maker _maker"
196             + "; _maker rdf:type jms:MemMakerSpec"
197             + "; _maker jms:reificationMode jms:rsMinimal"
198             + "; _reasoner jms:schemaURL ?0"
199             + "; _reasoner jms:schemaURL ?1"
200             ;
201         Model desc = modelWithStatements( d, new Object JavaDoc[] {file("schema.n3"), file("schema2.n3")} );
202         ModelSpec spec = ModelFactory.createSpec( desc );
203         validateHasSchema( loadBoth( "schema.n3", "schema2.n3" ), spec.createModel() );
204         }
205     
206     /**
207      * @return
208      */

209     private Graph loadBoth( String JavaDoc x, String JavaDoc y )
210         {
211         Model schema = FileManager.get().loadModel( file( x ) );
212         Model schema2 = FileManager.get().loadModel( file( y ) );
213         schema.add( schema2 );
214         return schema.getGraph();
215         }
216
217     private void validateHasSchema( Graph schema, Model m )
218         {
219         ((FakeReasoner) ((InfGraph) m.getGraph()).getReasoner()).validate( schema );
220         }
221
222     protected static class FakeReasoner implements Reasoner
223         {
224         public Graph bound = new GraphMem();
225         
226         public void validate( Graph desired )
227             { assertTrue( "bound graph is not correct", desired.isIsomorphicWith( bound ) ); }
228         
229         public Reasoner bindSchema( Graph tbox ) throws ReasonerException
230             { bound.getBulkUpdateHandler().add( tbox ); return this; }
231
232         public Reasoner bindSchema( Model tbox ) throws ReasonerException
233             { return bindSchema( tbox.getGraph() ); }
234
235         public InfGraph bind( Graph data ) throws ReasonerException
236             { return new BasicForwardRuleInfGraph( this, new ArrayList(), new GraphMem(), new GraphMem() ); }
237
238         public void setDerivationLogging( boolean logOn )
239             { throw new JenaException( "fakes don't do this" ); }
240
241         public void setParameter( Property parameterUri, Object JavaDoc value )
242             { throw new JenaException( "fakes don't do this" ); }
243         
244         public Model getReasonerCapabilities()
245             { throw new JenaException( "fakes don't do this" ); }
246
247         public void addDescription( Model configSpec, Resource base )
248             { throw new JenaException( "fakes don't do this" ); }
249
250         public boolean supportsProperty( Property property )
251             { throw new JenaException( "fakes don't do this" ); }
252
253         public Capabilities getGraphCapabilities() {
254             return new BaseInfGraph.InfCapabilities();
255         }
256
257         }
258     
259     protected static class FakeFactory implements ReasonerFactory
260         {
261         public Reasoner create( Resource configuration )
262             { return new FakeReasoner();
263             }
264
265         public Model getCapabilities()
266             {
267             // TODO Auto-generated method stub
268
return null;
269             }
270
271         public String JavaDoc getURI()
272             {
273             // TODO Auto-generated method stub
274
return null;
275             }
276         }
277     
278     public void testCreateReasoningModel()
279         {
280         String JavaDoc ruleString = "[rdfs3a: (?x rdfs:range ?y), (?y rdfs:subClassOf ?z) -> (?x rdfs:range ?z)]";
281         List wanted = Rule.parseRules( ruleString );
282         ModelSpec spec = createInfModelSpec( ruleString );
283         Model m = spec.createModel();
284         Graph g = m.getGraph();
285         assertTrue( g instanceof InfGraph );
286         Reasoner r = ((InfGraph) g).getReasoner();
287         assertTrue( r instanceof RuleReasoner );
288         RuleReasoner rr = (RuleReasoner) r;
289         List rules = rr.getRules();
290         assertSameRules( wanted, rules );
291         }
292    
293     public void testDescription()
294         {
295         String JavaDoc ruleString = "[rdfs3a: (?x rdfs:range ?y), (?y rdfs:subClassOf ?z) -> (?x rdfs:range ?z)]";
296         Model desc = createInfModelDesc( A, GenericRuleReasonerFactory.URI, ruleString );
297         ModelSpec s = ModelSpecFactory.createSpec( desc );
298         assertIsoModels( desc, s.getDescription() );
299         }
300     
301     private ModelSpec createInfModelSpec( String JavaDoc ruleString )
302         {
303         Model desc = createInfModelDesc( A, GenericRuleReasonerFactory.URI, ruleString );
304         return ModelSpecFactory.createSpec( desc );
305         }
306     
307     public static Model createInfModelDesc( Resource root, String JavaDoc URI, String JavaDoc ruleString )
308         {
309         String JavaDoc d =
310             "_root jms:reasonsWith _reasoner"
311             + "; _reasoner jms:reasoner ?0"
312             + "; _root jms:maker _maker"
313             + "; _maker rdf:type jms:MemMakerSpec"
314             + "; _maker jms:reificationMode jms:rsMinimal"
315             + "; _reasoner jms:ruleSet _rules"
316             + "; _rules jms:hasRule ?1"
317             ;
318         return modelWithStatements( d, new Object JavaDoc[] {URI, "'" + ruleString + "'"} );
319         }
320    
321     /**
322      * @param factoryURI
323      * @param rulesURL
324     */

325     private void testRuleSetURL( String JavaDoc factoryURI, String JavaDoc rulesURL )
326         {
327         List rules = Rule.rulesFromURL( rulesURL );
328         Model rs = modelWithStatements( "_a jms:reasoner ?0; _a jms:ruleSetURL ?1", new Object JavaDoc[] {factoryURI, rulesURL} );
329         ReasonerFactory rf = InfModelSpec.getReasonerFactory( A, rs );
330         GenericRuleReasoner gr = (GenericRuleReasoner) rf.create( null );
331         assertSameRules( rules, gr.getRules() );
332         }
333     
334     protected void testGetReasoner( String JavaDoc uri, Class JavaDoc wantClass )
335         {
336         ReasonerFactory rf = InfModelSpec.getReasonerFactory( A, rSpec( uri ) );
337         assertEquals( wantClass, rf.create( null ).getClass() );
338         }
339
340     protected Model rSpec( String JavaDoc factoryURI )
341         { return modelWithStatements( "_a jms:reasoner " + factoryURI ); }
342     
343     protected static String JavaDoc file( String JavaDoc name )
344         { return "file:testing/modelspecs/" + name; }
345     
346     protected void assertContains( String JavaDoc x, String JavaDoc y )
347         {
348         if (y == null) fail( "<null> does not contain anything, especially '" + x + "'" );
349         if (y.indexOf( x ) < 0) fail( "'" + y + "' does not contain '" + x + "'" );
350         }
351     
352     }
353
354
355 /*
356     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
357     All rights reserved.
358     
359     Redistribution and use in source and binary forms, with or without
360     modification, are permitted provided that the following conditions
361     are met:
362     
363     1. Redistributions of source code must retain the above copyright
364        notice, this list of conditions and the following disclaimer.
365     
366     2. Redistributions in binary form must reproduce the above copyright
367        notice, this list of conditions and the following disclaimer in the
368        documentation and/or other materials provided with the distribution.
369     
370     3. The name of the author may not be used to endorse or promote products
371        derived from this software without specific prior written permission.
372     
373     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
374     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
375     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
376     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
377     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
378     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
379     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
380     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
381     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
382     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
383 */

384
Popular Tags