KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > test > DebugOWL


1 /******************************************************************
2  * File: DebugOWL.java
3  * Created by: Dave Reynolds
4  * Created on: 12-Jun-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: DebugOWL.java,v 1.30 2005/04/11 11:26:59 der Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.test;
11
12 import java.util.Iterator JavaDoc;
13
14 import com.hp.hpl.jena.graph.*;
15 import com.hp.hpl.jena.graph.compose.Union;
16 import com.hp.hpl.jena.mem.GraphMem;
17 //import com.hp.hpl.jena.rdf.model.*;
18
import com.hp.hpl.jena.util.FileManager;
19 import com.hp.hpl.jena.util.PrintUtil;
20 import com.hp.hpl.jena.vocabulary.*;
21 import com.hp.hpl.jena.reasoner.*;
22 import com.hp.hpl.jena.reasoner.rulesys.*;
23 import com.hp.hpl.jena.reasoner.rulesys.impl.oldCode.*;
24 import com.hp.hpl.jena.shared.WrappedIOException;
25 //import com.hp.hpl.jena.reasoner.transitiveReasoner.TransitiveReasonerFactory;
26

27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import java.util.*;
30
31 /**
32  * Test harnness for investigating OWL reasoner correctness and performance
33  * on specific local test files. Unit testing is done using OWLWGTester or simplar,
34  * this code is a debugging tools rather than a tester.
35  *
36  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
37  * @version $Revision: 1.30 $ on $Date: 2005/04/11 11:26:59 $
38  */

39 public class DebugOWL {
40
41     /** The base reasoner being tested */
42     Reasoner reasoner;
43     
44     /** The raw tests data as a Graph */
45     Graph testdata;
46     
47     /** The (optional) schema graph used in interpreting the test data */
48     Graph schema;
49     
50     /** The inference graph under test */
51     InfGraph infgraph;
52     
53     /** Concepts created by testGenerator, [layer, index] */
54     Node[] concepts;
55     
56     /** Instances of each concept */
57     Node[] instances;
58     
59     /** Instance properties */
60     Node[] properties;
61         
62     static Log logger = LogFactory.getLog(DebugOWL.class);
63     
64     /** reasoner config: experimental ruleset and config */
65     public static final int EXPT = 1;
66     
67     /** reasoner config: normal OWL-FB */
68     public static final int OWLFB = 2;
69     
70     /** reasoner config: normal OWL forward */
71     public static final int OWL = 3;
72     
73     /** reasoner config: normal RDFS */
74     public static final int RDFSFB = 4;
75     
76     /** reasoner config: final RDFS - hybrid + TGC */
77     public static final int RDFSFinal = 5;
78     
79     /** reasoner config: experimental OWL */
80     public static final int OWLExpt = 6;
81     
82     /** reasoner config: LP RDFS exp */
83     public static final int RDFSLPExpt = 7;
84     
85     
86     /**
87      * Construct an empty test harness.
88      */

89     public DebugOWL(int config) {
90         testdata = new GraphMem();
91         schema = null;
92         
93         switch(config) {
94             
95         case EXPT:
96             reasoner = GenericRuleReasonerFactory.theInstance().create(null);
97             GenericRuleReasoner grr = (GenericRuleReasoner)reasoner;
98             grr.setMode(GenericRuleReasoner.HYBRID);
99             try {
100                 grr.setRules(Rule.parseRules(Util.loadRuleParserFromResourceFile("etc/expt.rules")));
101             } catch (WrappedIOException e) {
102                 System.out.println("Failed to open rules file: " + e.getCause() );
103                 System.exit(1);
104             }
105 // grr.setTransitiveClosureCaching(true);
106
// grr.setOWLTranslation(true);
107
// grr.setTraceOn(true);
108
break;
109             
110             case OWLFB:
111                 reasoner = OWLFBRuleReasonerFactory.theInstance().create(null);
112 // ((OWLFBRuleReasoner)reasoner).setTraceOn(true);
113
break;
114             
115             case OWL:
116                 reasoner = OWLRuleReasonerFactory.theInstance().create(null);
117 // ((OWLRuleReasoner)reasoner).setTraceOn(true);
118
break;
119             
120             case RDFSFB:
121                 reasoner = RDFSFBRuleReasonerFactory.theInstance().create(null);
122                 break;
123             
124             case RDFSFinal:
125                 reasoner = RDFSRuleReasonerFactory.theInstance().create(null);
126                 break;
127             
128             case OWLExpt:
129                 reasoner = OWLExptRuleReasonerFactory.theInstance().create(null);
130 // ((OWLExptRuleReasoner)reasoner).setTraceOn(true);
131
break;
132             
133             case RDFSLPExpt:
134                 try {
135                     List rules = Rule.parseRules(Util.loadRuleParserFromResourceFile("etc/expt.rules"));
136                     reasoner = new FBRuleReasoner(rules);
137                 } catch (WrappedIOException e) {
138                     System.out.println("Failed to open rules file: " + e.getCause());
139                     System.exit(1);
140                 }
141                 break;
142             
143         }
144         
145     }
146     
147     /**
148      * Load a test data set from file.
149      */

150     public void load(String JavaDoc testFile) {
151         testdata = FileManager.get().loadModel(testFile).getGraph();
152         schema = null;
153     }
154     
155     /**
156      * Load both a schema and an instance data file.
157      */

158     public void load(String JavaDoc schemaFile, String JavaDoc testFile) {
159         testdata = FileManager.get().loadModel(testFile).getGraph();
160         schema = FileManager.get().loadModel(schemaFile).getGraph();
161     }
162     
163     /**
164      * Create an artificial data set. This variant puts schema and
165      * instance data into the same testdata graph.
166      * @param depth the depth of the concept tree
167      * @param NS the number of subclasses at each tree level
168      * @param NI the number of instances of each concept
169      * @param withProps if true then properties are created for each concept and instiated for every third instance
170      */

171     public void createTest(int depth, int NS, int NI, boolean withProps) {
172         // Calculate total store sizes and allocate
173
int numClasses = 0;
174         int levelSize = 1;
175         for (int i = 0; i < depth; i++) {
176             levelSize *= NS;
177             numClasses += levelSize;
178         }
179         concepts = new Node[numClasses];
180         properties = new Node[numClasses];
181         instances = new Node[numClasses * NI];
182         logger.info("Classes: " + numClasses +" Instances: " + (numClasses * NI)
183                         + (withProps ? " with properties" : ""));
184         
185         // Create the tree
186
testdata = new GraphMem();
187         // First level
188
int conceptPtr = 0;
189         int levelStart = 0;
190         int levelEnd = 0;
191         int instancePtr = 0;
192         for (int i = 0; i < depth; i++) {
193             // Class tree
194
Node property = null;
195             if (i == 0) {
196                 for (int j = 0; j < NS; j++) {
197                     Node concept = Node.createURI("concept" + conceptPtr);
198                     if (withProps) {
199                         property = Node.createURI("prop" + conceptPtr);
200                         properties[conceptPtr] = property;
201                     }
202                     concepts[conceptPtr++] = concept;
203                 }
204             } else {
205                 for (int j = levelStart; j < levelEnd; j++) {
206                     Node superConcept = concepts[j];
207                     for (int k = 0; k < NS; k++) {
208                         Node concept = Node.createURI("concept" + conceptPtr);
209                         if (withProps) {
210                             property = Node.createURI("prop" + conceptPtr);
211                             properties[conceptPtr] = property;
212                         }
213                         concepts[conceptPtr++] = concept;
214                         testdata.add(new Triple(concept, RDFS.subClassOf.asNode(), superConcept));
215                     }
216                 }
217             }
218             levelStart = levelEnd;
219             levelEnd = conceptPtr;
220             // Instance data
221
for (int j = levelStart; j < levelEnd; j++) {
222                 Node concept = concepts[j];
223                 for (int k = 0; k < NI; k++) {
224                     Node instance = Node.createURI("instance"+instancePtr);
225                     testdata.add(new Triple(instance, RDF.type.asNode(), concept));
226                     if (withProps && (k-1)%3 == 0) {
227                         testdata.add(new Triple(instances[instancePtr-1], property, instance));
228                     }
229                     instances[instancePtr++] = instance;
230                 }
231             }
232         }
233     }
234     
235     /**
236      * Configure the inference graph ready for testing.
237      */

238     public void init() {
239         if (schema == null) {
240             infgraph = reasoner.bind(testdata);
241         } else {
242 // infgraph = reasoner.bindSchema(schema).bind(testdata);
243
infgraph = reasoner.bind(new Union(schema, testdata));
244         }
245 // if (infgraph instanceof FBRuleInfGraph) {
246
// ((FBRuleInfGraph)infgraph).resetLPProfile(true);
247
// }
248
if (infgraph instanceof FBRuleInfGraph) {
249             System.out.println("Starting prepare");
250             ((FBRuleInfGraph)infgraph).prepare();
251             System.out.println(".. finished");
252         }
253     }
254     
255     /**
256      * Test and time an predefined class extension listing
257      */

258     long listC0(boolean print) {
259         return list(null, RDF.type.asNode(), concepts[0], print);
260     }
261     
262     /**
263      * Test and time an general access operation.
264      */

265     long list(Node s, Node p, Node o, boolean print) {
266         long t1 = System.currentTimeMillis();
267         init();
268         int count = 0;
269         for (Iterator JavaDoc i = infgraph.find(s,p,o); i.hasNext(); ) {
270             Triple t = (Triple)i.next();
271             count++;
272             if (print) {
273                 logger.info(PrintUtil.print(t));
274             }
275         }
276         long t2 = System.currentTimeMillis();
277         System.out.println("Found " + count + " results");
278         return (t2 - t1);
279     }
280    
281     /**
282      * Create and run a list classes test.
283      */

284     public void runListClassesTest(int depth, int NS, int NI, boolean withProps) {
285         createTest(depth, NS, NI, withProps);
286         long t = list(null, RDF.type.asNode(), RDFS.Class.asNode(), false);
287         System.out.println("Took " + t + "ms");
288     }
289    
290     /**
291      * Create and run a volz test.
292      */

293     public void runVolz(int depth, int NS, int NI, boolean withProps) {
294         createTest(depth, NS, NI, withProps);
295         long t = listC0(false);
296         System.out.println("Took " + t + "ms");
297         if (infgraph instanceof FBRuleInfGraph) {
298             ((FBRuleInfGraph)infgraph).printLPProfile();
299         }
300     }
301     
302     /**
303      * Run a standard test squence based on Volz et al sets
304      */

305     public void runVolz() {
306         runVolz(3,5,10, false);
307         runVolz(3,5,10, false);
308         runVolz(4,5,10, false);
309         runVolz(5,5,10, false);
310         
311 // runVolz(3,5,30, false);
312
// runVolz(4,5,30, false);
313
// runVolz(5,5,30, false);
314
// run(3,5,10, true);
315
// run(4,5,10, true);
316
// run(5,5,10, true);
317
}
318     
319     /**
320      * Run default test on a named file.
321      */

322     public void listClassesOn(String JavaDoc filename) {
323         load(filename);
324         System.out.println("Testing: " + filename);
325         long t = list(null, RDF.type.asNode(), RDFS.Class.asNode(), false);
326         System.out.println("Took " + t + "ms");
327     }
328     
329     public static void main(String JavaDoc[] args) {
330         try {
331             String JavaDoc dataFile = "file:testing/ontology/owl/list-syntax/test-with-import.rdf";
332             String JavaDoc schemaFile = "file:vocabularies/owl.owl";
333             String JavaDoc schemaFile2 = "file:testing/reasoners/bugs/owl-partial.owl";
334             String JavaDoc dataFile2 = "file:testing/reasoners/bugs/test.owl";
335             String JavaDoc food = "file:testing/reasoners/bugs/food.owl";
336
337             // Example from ontology development which takes s rather than ms
338
// new DebugOWL(OWLExpt).listClassesOn(dataFile2);
339

340             // owl.owl goes into meltdown with even the forward rules
341
// new DebugOWL(OWLFB).run(schemaFile);
342
// new DebugOWL(OWL).run("file:temp/owl-subset.owl");
343

344             // Test volz examples on OWL config
345
// new DebugOWL(OWLFB).runVolz();
346
// new DebugOWL(OWLExpt).runVolz();
347

348             // Test volz examples on RDFS config
349
System.out.println("Volz tests on normal RDFS, tgc + type rules");
350             new DebugOWL(RDFSFinal).runVolz();
351 // System.out.println("Volz tests on lp + expt RDFS rules");
352
// new DebugOWL(RDFSLPExpt).runVolz();
353

354 // System.out.println("Volz tests on normal RDFS fb rules");
355
// new DebugOWL(RDFSFB).runVolz();
356
// System.out.println("Volz tests on lp + expt owl rules");
357
// new DebugOWL(OWLExpt).runVolz();
358
// System.out.println("Volz tests on normal OWL-FB");
359
// new DebugOWL(OWLFB).runVolz();
360

361 // DebugOWL tester = new DebugOWL(OWLFB);
362
// tester.load(dataFile2);
363
// System.out.println("Test schema + data started ...");
364
// long t = tester.list(null, RDF.type.asNode(), RDFS.Class.asNode(), false);
365
// System.out.println("Took " + t + "ms");
366

367 // DebugOWL tester = new DebugOWL(EXPT);
368
// tester.runListClassesTest(1,4,10,false);
369
// tester.runListClassesTest(1,4,10,false);
370
// tester.runListClassesTest(2,4,10,false);
371
// tester.runListClassesTest(3,4,10,false);
372
// tester.runListClassesTest(3,5,10,false);
373
// tester.runListClassesTest(3,6,10,false);
374

375         } catch (Exception JavaDoc e) {
376             System.out.println("Problem: " + e);
377             e.printStackTrace();
378         }
379     }
380     
381 }
382
383
384 /*
385     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
386     All rights reserved.
387
388     Redistribution and use in source and binary forms, with or without
389     modification, are permitted provided that the following conditions
390     are met:
391
392     1. Redistributions of source code must retain the above copyright
393        notice, this list of conditions and the following disclaimer.
394
395     2. Redistributions in binary form must reproduce the above copyright
396        notice, this list of conditions and the following disclaimer in the
397        documentation and/or other materials provided with the distribution.
398
399     3. The name of the author may not be used to endorse or promote products
400        derived from this software without specific prior written permission.
401
402     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
403     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
404     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
405     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
406     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
407     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
408     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
409     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
410     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
411     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
412 */
Popular Tags