Source Code Cross Referenced for WebOntTestHarness.java in  » RSS-RDF » Jena-2.5.5 » com » hp » hpl » jena » reasoner » rulesys » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » RSS RDF » Jena 2.5.5 » com.hp.hpl.jena.reasoner.rulesys.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /******************************************************************
002:         * File:        WebOntTestHarness.java
003:         * Created by:  Dave Reynolds
004:         * Created on:  12-Sep-2003
005:         * 
006:         * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
007:         * [See end of file]
008:         * $Id: WebOntTestHarness.java,v 1.27 2008/01/02 12:08:20 andy_seaborne Exp $
009:         *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.test;
010:
011:        import com.hp.hpl.jena.graph.query.*;
012:        import com.hp.hpl.jena.graph.*;
013:        import com.hp.hpl.jena.ontology.OntModelSpec;
014:        import com.hp.hpl.jena.rdf.model.*;
015:        import com.hp.hpl.jena.reasoner.*;
016:        import com.hp.hpl.jena.reasoner.rulesys.FBRuleInfGraph;
017:        import com.hp.hpl.jena.reasoner.test.WGReasonerTester;
018:        import com.hp.hpl.jena.vocabulary.*;
019:
020:        import java.io.*;
021:        import java.util.*;
022:
023:        /**
024:         * Test harness for running the WebOnt working group tests relevant 
025:         * to the OWL rule reasoner. See also TestOWLRules which runs the
026:         * core WG tests as part of the routine unit tests.
027:         * 
028:         * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
029:         * @version $Revision: 1.27 $ on $Date: 2008/01/02 12:08:20 $
030:         */
031:        public class WebOntTestHarness {
032:
033:            //  =======================================================================
034:            //  Variables to control the test operations
035:
036:            /** Set to true to include modified test versions */
037:            public static boolean includeModified = false;
038:
039:            /** Set to true to use approved tests only */
040:            public static boolean approvedOnly = true;
041:
042:            /** Set to true to print LP engine profile information */
043:            public static boolean printProfile = false;
044:
045:            //  =======================================================================
046:            //  Internal state
047:
048:            /** The reasoner being tested */
049:            Reasoner reasoner;
050:
051:            /** The total set of known tests */
052:            Model testDefinitions;
053:
054:            /** The number of tests run */
055:            int testCount = 0;
056:
057:            /** The time cost in ms of the last test to be run */
058:            long lastTestDuration = 0;
059:
060:            /** Number of tests passed */
061:            int passCount = 0;
062:
063:            /** The model describing the results of the run */
064:            Model testResults;
065:
066:            /** The resource which acts as a description for the Jena2 instance being tested */
067:            Resource jena2;
068:
069:            //  =======================================================================
070:            //  Internal constants
071:
072:            /** The base directory for the working group test files to use */
073:            public static final String BASE_TESTDIR = "testing/wg/";
074:
075:            /** The base URI in which the files are purported to reside */
076:            public static String BASE_URI = "http://www.w3.org/2002/03owlt/";
077:
078:            /** The base URI for the results file */
079:            public static String BASE_RESULTS_URI = "http://jena.sourceforge.net/data/owl-results.rdf";
080:
081:            /** The list of subdirectories to process (omits the rdf/rdfs dirs) */
082:            public static final String[] TEST_DIRS = { "AllDifferent",
083:                    "AllDistinct", "AnnotationProperty", "DatatypeProperty",
084:                    "FunctionalProperty", "I3.2", "I3.4", "I4.1", "I4.5",
085:                    "I4.6", "I5.1", "I5.2", "I5.21", "I5.24", "I5.26", "I5.3",
086:                    "I5.5", "I5.8", "InverseFunctionalProperty", "Nothing",
087:                    "Restriction", "SymmetricProperty", "Thing",
088:                    "TransitiveProperty", "allValuesFrom", "amp-in-url",
089:                    "cardinality", "complementOf", "datatypes",
090:                    "differentFrom", "disjointWith", "distinctMembers",
091:                    "equivalentClass", "equivalentProperty", "imports",
092:                    "intersectionOf", "inverseOf", "localtests",
093:                    "maxCardinality", "miscellaneous", "oneOf",
094:                    "oneOfDistinct", "sameAs", "sameClassAs",
095:                    "sameIndividualAs", "samePropertyAs", "someValuesFrom",
096:                    "statement-entailment", "unionOf", "xmlbase",
097:                    "description-logic",
098:            //            "extra-credit", 
099:            };
100:
101:            /** List of tests that are blocked because they test language features beyond Lite */
102:            public static final String[] BLOCKED_TESTS = {
103:                    // Explicitly testing non-lite features
104:                    "http://www.w3.org/2002/03owlt/complementOf/Manifest001#test",
105:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest901#test",
106:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest903#test",
107:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest902#test",
108:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest904#test",
109:                    //        "http://www.w3.org/2002/03owlt/oneOf/Manifest002#test", 
110:                    //        "http://www.w3.org/2002/03owlt/oneOf/Manifest003#test", 
111:                    "http://www.w3.org/2002/03owlt/oneOf/Manifest004#test",
112:                    "http://www.w3.org/2002/03owlt/unionOf/Manifest001#test",
113:                    "http://www.w3.org/2002/03owlt/unionOf/Manifest002#test",
114:                    "http://www.w3.org/2002/03owlt/unionOf/Manifest003#test",
115:                    "http://www.w3.org/2002/03owlt/unionOf/Manifest004#test",
116:                    "http://www.w3.org/2002/03owlt/equivalentClass/Manifest006#test",
117:                    "http://www.w3.org/2002/03owlt/equivalentClass/Manifest007#test",
118:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest201#test",
119:                    "http://www.w3.org/2002/03owlt/I5.8/Manifest004#test",
120:                    "http://www.w3.org/2002/03owlt/I5.2/Manifest004#test",
121:
122:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest008#test",
123:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest011#test",
124:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest015#test",
125:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest019#test",
126:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest023#test",
127:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest026#test",
128:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest027#test",
129:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest029#test",
130:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest030#test",
131:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest032#test",
132:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest033#test",
133:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest035#test",
134:
135:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest101#test",
136:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest102#test",
137:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest103#test",
138:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest104#test",
139:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest105#test",
140:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest106#test",
141:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest107#test",
142:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest108#test",
143:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest109#test",
144:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest110#test",
145:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest111#test",
146:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest502#test",
147:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest504#test",
148:
149:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest202#test",
150:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest203#test",
151:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest204#test",
152:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest205#test",
153:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest206#test",
154:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest207#test",
155:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest208#test",
156:
157:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest209#test",
158:
159:                    "http://www.w3.org/2002/03owlt/miscellaneous/Manifest010#test",
160:                    "http://www.w3.org/2002/03owlt/miscellaneous/Manifest011#test",
161:                    "http://www.w3.org/2002/03owlt/SymmetricProperty/Manifest002#test",
162:
163:                    "http://www.w3.org/2002/03owlt/Thing/Manifest005#test",
164:
165:                    // Temporary block - incomplete (OOM eventually in some cases)
166:                    "http://www.w3.org/2002/03owlt/TransitiveProperty/Manifest002#test",
167:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest661#test",
168:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest662#test",
169:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest663#test",
170:
171:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest608#test",
172:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest611#test",
173:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest615#test",
174:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest623#test",
175:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest626#test",
176:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest627#test",
177:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest630#test",
178:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest668#test",
179:                    "http://www.w3.org/2002/03owlt/description-logic/Manifest668#test",
180:
181:            };
182:
183:            /** The list of status values to include. If approvedOnly then only the first
184:             *  entry is allowed */
185:            public static final String[] STATUS_FLAGS = { "APPROVED",
186:                    "PROPOSED" };
187:
188:            //  =======================================================================
189:            //  Constructor and associated support
190:
191:            public WebOntTestHarness() {
192:                testDefinitions = loadAllTestDefinitions();
193:                reasoner = ReasonerRegistry.getOWLReasoner();
194:                initResults();
195:            }
196:
197:            /** Load all of the known manifest files into a single model */
198:            public static Model loadAllTestDefinitions() {
199:                System.out.print("Loading manifests ");
200:                System.out.flush();
201:                Model testDefs = ModelFactory.createDefaultModel();
202:                int count = 0;
203:                for (int idir = 0; idir < TEST_DIRS.length; idir++) {
204:                    File dir = new File(BASE_TESTDIR + TEST_DIRS[idir]);
205:                    String[] manifests = dir.list(new FilenameFilter() {
206:                        public boolean accept(File df, String name) {
207:                            if (name.startsWith("Manifest")
208:                                    && name.endsWith(".rdf")) {
209:                                return includeModified
210:                                        || !name.endsWith("-mod.rdf");
211:                            } else {
212:                                return false;
213:                            }
214:                        }
215:                    });
216:                    for (int im = 0; im < manifests.length; im++) {
217:                        String manifest = manifests[im];
218:                        File mf = new File(dir, manifest);
219:                        try {
220:                            testDefs
221:                                    .read(new FileInputStream(mf), "file:" + mf);
222:                            count++;
223:                            if (count % 8 == 0) {
224:                                System.out.print(".");
225:                                System.out.flush();
226:                            }
227:                        } catch (FileNotFoundException e) {
228:                            System.out.println("File not readable - " + e);
229:                        }
230:                    }
231:                }
232:                System.out.println("loaded");
233:                return testDefs;
234:            }
235:
236:            /** 
237:             * Initialize the result model.
238:             */
239:            public void initResults() {
240:                testResults = ModelFactory.createDefaultModel();
241:                jena2 = testResults.createResource(BASE_RESULTS_URI + "#jena2");
242:                jena2
243:                        .addProperty(
244:                                RDFS.comment,
245:                                testResults
246:                                        .createLiteral(
247:                                                "<a xmlns=\"http://www.w3.org/1999/xhtml\" href=\"http://jena.sourceforce.net/\">Jena2</a> includes a rule-based inference engine for RDF processing, "
248:                                                        + "supporting both forward and backward chaining rules. Its OWL rule set is designed to provide sound "
249:                                                        + "but not complete instance resasoning for that fragment of OWL/Full limited to the OWL/lite vocabulary. In"
250:                                                        + "particular it does not support unionOf/complementOf.",
251:                                                true));
252:                jena2.addProperty(RDFS.label, "Jena2");
253:                testResults.setNsPrefix("results", OWLResults.NS);
254:            }
255:
256:            //  =======================================================================
257:            //  Main control methods
258:
259:            public static void main(String[] args) throws IOException {
260:                String resultFile = "owl-results.rdf";
261:                if (args.length >= 1) {
262:                    resultFile = args[0];
263:                }
264:                WebOntTestHarness harness = new WebOntTestHarness();
265:                harness.runTests();
266:                //        harness.runTest("http://www.w3.org/2002/03owlt/AnnotationProperty/Manifest004#test");
267:                //        harness.runTest("http://www.w3.org/2002/03owlt/AnnotationProperty/Manifest003#test");
268:                //        harness.runTest("http://www.w3.org/2002/03owlt/Thing/Manifest001#test");
269:                //        harness.runTest("http://www.w3.org/2002/03owlt/Thing/Manifest002#test");
270:                //        harness.runTest("http://www.w3.org/2002/03owlt/Thing/Manifest003#test");
271:                //        harness.runTest("http://www.w3.org/2002/03owlt/Thing/Manifest004#test");
272:                //        harness.runTest("http://www.w3.org/2002/03owlt/Thing/Manifest005#test");
273:                RDFWriter writer = harness.testResults
274:                        .getWriter("RDF/XML-ABBREV");
275:                OutputStream stream = new FileOutputStream(resultFile);
276:                writer.setProperty("showXmlDeclaration", "true");
277:                harness.testResults.setNsPrefix("",
278:                        "http://www.w3.org/1999/xhtml");
279:                writer.write(harness.testResults, stream, BASE_RESULTS_URI);
280:            }
281:
282:            /**
283:             * Run all relevant tests.
284:             */
285:            public void runTests() {
286:                System.out.println("Testing "
287:                        + (approvedOnly ? "only APPROVED"
288:                                : "APPROVED and PROPOSED"));
289:                System.out.println("Positive entailment: ");
290:                runTests(findTestsOfType(OWLTest.PositiveEntailmentTest));
291:                //        System.out.println("\nNegative entailment: ");
292:                //        runTests(findTestsOfType(OWLTest.NegativeEntailmentTest));
293:                System.out.println("\nTrue tests: ");
294:                runTests(findTestsOfType(OWLTest.TrueTest));
295:                System.out.println("\nOWL for OWL tests: ");
296:                runTests(findTestsOfType(OWLTest.OWLforOWLTest));
297:                System.out.println("\nImport entailment tests: ");
298:                runTests(findTestsOfType(OWLTest.ImportEntailmentTest));
299:                System.out.println("\nInconsistency tests: ");
300:                runTests(findTestsOfType(OWLTest.InconsistencyTest));
301:                System.out.println("\nPassed " + passCount + " out of "
302:                        + testCount);
303:            }
304:
305:            /**
306:             * Run all tests in the given list.
307:             */
308:            public void runTests(List tests) {
309:                for (Iterator i = tests.iterator(); i.hasNext();) {
310:                    runTest((Resource) i.next());
311:                }
312:            }
313:
314:            /**
315:             * Run a single test of any sort, performing any appropriate logging
316:             * and error reporting.
317:             */
318:            public void runTest(String test) {
319:                runTest(testDefinitions.getResource(test));
320:            }
321:
322:            /**
323:             * Run a single test of any sort, performing any appropriate logging
324:             * and error reporting.
325:             */
326:            public void runTest(Resource test) {
327:                System.out.println("Running " + test);
328:                boolean success = false;
329:                boolean fail = false;
330:                try {
331:                    success = doRunTest(test);
332:                } catch (Exception e) {
333:                    fail = true;
334:                    System.out.print("\nException: " + e);
335:                    e.printStackTrace();
336:                }
337:                testCount++;
338:                if (success) {
339:                    System.out.print((testCount % 40 == 0) ? ".\n" : ".");
340:                    System.out.flush();
341:                    passCount++;
342:                } else {
343:                    System.out.println("\nFAIL: " + test);
344:                }
345:                Resource resultType = null;
346:                if (fail) {
347:                    resultType = OWLResults.FailingRun;
348:                } else {
349:                    if (test.hasProperty(RDF.type,
350:                            OWLTest.NegativeEntailmentTest)
351:                            || test.hasProperty(RDF.type,
352:                                    OWLTest.ConsistencyTest)) {
353:                        resultType = success ? OWLResults.PassingRun
354:                                : OWLResults.FailingRun;
355:                    } else {
356:                        resultType = success ? OWLResults.PassingRun
357:                                : OWLResults.IncompleteRun;
358:                    }
359:                }
360:                // log to the rdf result format
361:                Resource result = testResults.createResource().addProperty(
362:                        RDF.type, OWLResults.TestRun).addProperty(RDF.type,
363:                        resultType).addProperty(OWLResults.test, test)
364:                        .addProperty(OWLResults.system, jena2);
365:            }
366:
367:            /**
368:             * Run a single test of any sort, return true if the test succeeds.
369:             */
370:            public boolean doRunTest(Resource test) throws IOException {
371:                if (test.hasProperty(RDF.type, OWLTest.PositiveEntailmentTest)
372:                        || test.hasProperty(RDF.type,
373:                                OWLTest.NegativeEntailmentTest)
374:                        || test.hasProperty(RDF.type, OWLTest.OWLforOWLTest)
375:                        || test.hasProperty(RDF.type,
376:                                OWLTest.ImportEntailmentTest)
377:                        || test.hasProperty(RDF.type, OWLTest.TrueTest)) {
378:                    // Entailment tests
379:                    boolean processImports = test.hasProperty(RDF.type,
380:                            OWLTest.ImportEntailmentTest);
381:                    Model premises = getDoc(test, RDFTest.premiseDocument,
382:                            processImports);
383:                    Model conclusions = getDoc(test, RDFTest.conclusionDocument);
384:                    comprehensionAxioms(premises, conclusions);
385:                    long t1 = System.currentTimeMillis();
386:                    InfGraph graph = reasoner.bind(premises.getGraph());
387:                    if (printProfile) {
388:                        ((FBRuleInfGraph) graph).resetLPProfile(true);
389:                    }
390:                    Model result = ModelFactory.createModelForGraph(graph);
391:                    boolean correct = testEntailment(conclusions.getGraph(),
392:                            result.getGraph());
393:                    long t2 = System.currentTimeMillis();
394:                    lastTestDuration = t2 - t1;
395:                    if (printProfile) {
396:                        ((FBRuleInfGraph) graph).printLPProfile();
397:                    }
398:                    if (test.hasProperty(RDF.type,
399:                            OWLTest.NegativeEntailmentTest)) {
400:                        correct = !correct;
401:                    }
402:                    return correct;
403:                } else if (test
404:                        .hasProperty(RDF.type, OWLTest.InconsistencyTest)) {
405:                    //            System.out.println("Starting: " + test);
406:                    Model input = getDoc(test, RDFTest.inputDocument);
407:                    long t1 = System.currentTimeMillis();
408:                    InfGraph graph = reasoner.bind(input.getGraph());
409:                    boolean correct = !graph.validate().isValid();
410:                    long t2 = System.currentTimeMillis();
411:                    lastTestDuration = t2 - t1;
412:                    return correct;
413:                } else if (test.hasProperty(RDF.type, OWLTest.ConsistencyTest)) {
414:                    // Not used normally becase we are not complete enough to prove consistency
415:                    //            System.out.println("Starting: " + test);
416:                    Model input = getDoc(test, RDFTest.inputDocument);
417:                    long t1 = System.currentTimeMillis();
418:                    InfGraph graph = reasoner.bind(input.getGraph());
419:                    boolean correct = graph.validate().isValid();
420:                    long t2 = System.currentTimeMillis();
421:                    lastTestDuration = t2 - t1;
422:                    return correct;
423:                } else {
424:                    for (StmtIterator i = test.listProperties(RDF.type); i
425:                            .hasNext();) {
426:                        System.out.println("Test type = "
427:                                + i.nextStatement().getObject());
428:                    }
429:                    throw new ReasonerException("Unknown test type");
430:                }
431:            }
432:
433:            /**
434:             * Load the premises or conclusions for the test, optional performing
435:             * import processing.
436:             */
437:            public Model getDoc(Resource test, Property docType,
438:                    boolean processImports) throws IOException {
439:                if (processImports) {
440:                    Model result = ModelFactory.createOntologyModel(
441:                            OntModelSpec.OWL_MEM, null);
442:                    StmtIterator si = test.listProperties(docType);
443:                    while (si.hasNext()) {
444:                        String fname = si.nextStatement().getObject()
445:                                .toString()
446:                                + ".rdf";
447:                        loadFile(fname, result);
448:                    }
449:                    return result;
450:                } else {
451:                    return getDoc(test, docType);
452:                }
453:            }
454:
455:            /**
456:             * Load the premises or conclusions for the test.
457:             */
458:            public Model getDoc(Resource test, Property docType)
459:                    throws IOException {
460:                Model result = ModelFactory.createDefaultModel();
461:                StmtIterator si = test.listProperties(docType);
462:                while (si.hasNext()) {
463:                    String fname = si.nextStatement().getObject().toString()
464:                            + ".rdf";
465:                    loadFile(fname, result);
466:                }
467:                return result;
468:            }
469:
470:            /**
471:             * Utility to load a file into a model a Model. 
472:             * Files are assumed to be relative to the BASE_URI.
473:             * @param file the file name, relative to baseDir
474:             * @return the loaded Model
475:             */
476:            public static Model loadFile(String file, Model model)
477:                    throws IOException {
478:                String langType = "RDF/XML";
479:                if (file.endsWith(".nt")) {
480:                    langType = "N-TRIPLE";
481:                } else if (file.endsWith("n3")) {
482:                    langType = "N3";
483:                }
484:                String fname = file;
485:                if (fname.startsWith(BASE_URI)) {
486:                    fname = fname.substring(BASE_URI.length());
487:                }
488:                Reader reader = new BufferedReader(new FileReader(BASE_TESTDIR
489:                        + fname));
490:                model.read(reader, BASE_URI + fname, langType);
491:                return model;
492:            }
493:
494:            /**
495:             * Test a conclusions graph against a result graph. This works by
496:             * translating the conclusions graph into a find query which contains one
497:             * variable for each distinct bNode in the conclusions graph.
498:             */
499:            public boolean testEntailment(Graph conclusions, Graph result) {
500:                QueryHandler qh = result.queryHandler();
501:                Query query = WGReasonerTester.graphToQuery(conclusions);
502:                Iterator i = qh.prepareBindings(query, new Node[] {})
503:                        .executeBindings();
504:                return i.hasNext();
505:            }
506:
507:            /**
508:             * Example the conclusions graph for introduction of restrictions which
509:             * require a comprehension rewrite and declare new (anon) classes
510:             * for those restrictions.
511:             */
512:            public void comprehensionAxioms(Model premises, Model conclusions) {
513:                // Comprehend all restriction declarations and note them in a map
514:                Map comprehension = new HashMap();
515:                StmtIterator ri = conclusions.listStatements(null, RDF.type,
516:                        OWL.Restriction);
517:                while (ri.hasNext()) {
518:                    Resource restriction = ri.nextStatement().getSubject();
519:                    StmtIterator pi = restriction
520:                            .listProperties(OWL.onProperty);
521:                    while (pi.hasNext()) {
522:                        Resource prop = (Resource) pi.nextStatement()
523:                                .getObject();
524:                        StmtIterator vi = restriction.listProperties();
525:                        while (vi.hasNext()) {
526:                            Statement rs = vi.nextStatement();
527:                            if (!rs.getPredicate().equals(OWL.onProperty)) {
528:                                // Have a restriction on(prop) of type rs in the conclusions
529:                                // So assert a premise that such a restriction could exisit
530:                                Resource comp = premises.createResource()
531:                                        .addProperty(RDF.type, OWL.Restriction)
532:                                        .addProperty(OWL.onProperty, prop)
533:                                        .addProperty(rs.getPredicate(),
534:                                                rs.getObject());
535:                                comprehension.put(restriction, comp);
536:                            }
537:                        }
538:                    }
539:                }
540:                // Comprehend any intersectionOf lists. Introduce anon class which has the form
541:                // of the intersection expression.
542:                // Rewrite queries of the form (X intersectionOf Y) to the form
543:                //   (X equivalentClass ?CC) (?CC intersectionOf Y)
544:                StmtIterator ii = conclusions.listStatements(null,
545:                        OWL.intersectionOf, (RDFNode) null);
546:                List intersections = new ArrayList();
547:                while (ii.hasNext()) {
548:                    intersections.add(ii.next());
549:                }
550:                for (Iterator i = intersections.iterator(); i.hasNext();) {
551:                    Statement is = (Statement) i.next();
552:                    // Declare in the premises that such an intersection exists
553:                    Resource comp = premises.createResource().addProperty(
554:                            RDF.type, OWL.Class).addProperty(
555:                            OWL.intersectionOf,
556:                            mapList(premises, (Resource) is.getObject(),
557:                                    comprehension));
558:                    // Rewrite the conclusions to be a test for equivalence between the class being
559:                    // queried and the comprehended interesection
560:                    conclusions.remove(is);
561:                    conclusions.add(is.getSubject(), OWL.equivalentClass, comp);
562:                }
563:                // Comprehend any oneOf lists
564:                StmtIterator io = conclusions.listStatements(null, OWL.oneOf,
565:                        (RDFNode) null);
566:                while (io.hasNext()) {
567:                    Statement s = io.nextStatement();
568:                    Resource comp = premises.createResource().addProperty(
569:                            OWL.oneOf, s.getObject());
570:                }
571:            }
572:
573:            /**
574:             * Helper. Adds to the target model a translation of the given RDF list
575:             * with each element replaced according to the map.
576:             */
577:            private Resource mapList(Model target, Resource list, Map map) {
578:                if (list.equals(RDF.nil)) {
579:                    return RDF.nil;
580:                } else {
581:                    Resource head = (Resource) list.getRequiredProperty(
582:                            RDF.first).getObject();
583:                    Resource rest = (Resource) list.getRequiredProperty(
584:                            RDF.rest).getObject();
585:                    Resource mapElt = target.createResource();
586:                    Resource mapHead = (Resource) map.get(head);
587:                    if (mapHead == null)
588:                        mapHead = head;
589:                    mapElt.addProperty(RDF.first, mapHead);
590:                    mapElt.addProperty(RDF.rest, mapList(target, rest, map));
591:                    return mapElt;
592:                }
593:            }
594:
595:            //  =======================================================================
596:            //  Internal helper functions
597:
598:            /** Return a list of all tests of the given type, according to the current filters */
599:            public List findTestsOfType(Resource testType) {
600:                ArrayList result = new ArrayList();
601:                StmtIterator si = testDefinitions.listStatements(null,
602:                        RDF.type, testType);
603:                while (si.hasNext()) {
604:                    Resource test = si.nextStatement().getSubject();
605:                    boolean accept = true;
606:                    // Check test status
607:                    Literal status = (Literal) test.getProperty(RDFTest.status)
608:                            .getObject();
609:                    if (approvedOnly) {
610:                        accept = status.getString().equals(STATUS_FLAGS[0]);
611:                    } else {
612:                        accept = false;
613:                        for (int i = 0; i < STATUS_FLAGS.length; i++) {
614:                            if (status.getString().equals(STATUS_FLAGS[i])) {
615:                                accept = true;
616:                                break;
617:                            }
618:                        }
619:                    }
620:                    // Check for blocked tests
621:                    for (int i = 0; i < BLOCKED_TESTS.length; i++) {
622:                        if (BLOCKED_TESTS[i].equals(test.toString())) {
623:                            accept = false;
624:                        }
625:                    }
626:                    // End of filter tests
627:                    if (accept) {
628:                        result.add(test);
629:                    }
630:                }
631:                return result;
632:            }
633:        }
634:
635:        /*
636:         (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
637:         All rights reserved.
638:
639:         Redistribution and use in source and binary forms, with or without
640:         modification, are permitted provided that the following conditions
641:         are met:
642:
643:         1. Redistributions of source code must retain the above copyright
644:         notice, this list of conditions and the following disclaimer.
645:
646:         2. Redistributions in binary form must reproduce the above copyright
647:         notice, this list of conditions and the following disclaimer in the
648:         documentation and/or other materials provided with the distribution.
649:
650:         3. The name of the author may not be used to endorse or promote products
651:         derived from this software without specific prior written permission.
652:
653:         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
654:         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
655:         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
656:         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
657:         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
658:         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
659:         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
660:         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
661:         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
662:         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
663:         */
w_ww___.j__a__v___a___2_s_.__com___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.