Source code: com/hp/hpl/jena/reasoner/rulesys/test/TestRDFSRules.java
1 /******************************************************************
2 * File: TestRDFSRules.java
3 * Created by: Dave Reynolds
4 * Created on: 08-Apr-03
5 *
6 * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7 * [See end of file]
8 * $Id: TestRDFSRules.java,v 1.21 2005/04/11 11:27:04 der Exp $
9 *****************************************************************/
10 package com.hp.hpl.jena.reasoner.rulesys.test;
11
12 import com.hp.hpl.jena.reasoner.*;
13 import com.hp.hpl.jena.reasoner.test.*;
14 import com.hp.hpl.jena.util.FileManager;
15 // import com.hp.hpl.jena.util.PrintUtil;
16 import com.hp.hpl.jena.vocabulary.OWL;
17 import com.hp.hpl.jena.vocabulary.RDF;
18 import com.hp.hpl.jena.vocabulary.RDFS;
19 import com.hp.hpl.jena.reasoner.rdfsReasoner1.RDFSReasonerFactory;
20 import com.hp.hpl.jena.reasoner.rulesys.*;
21 import com.hp.hpl.jena.rdf.model.*;
22
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25 import java.io.IOException;
26 import java.util.Iterator;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /** * Test suite to test the production rule version of the RDFS implementation.
31 * <p> The tests themselves have been replaced by an updated version
32 * of the top level TestRDFSReasoners but this file is maintained for now since
33 * the top level timing test can sometimes be useful. </p>
34 * * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a> * @version $Revision: 1.21 $ on $Date: 2005/04/11 11:27:04 $ */
35 public class TestRDFSRules extends TestCase {
36 /** Base URI for the test names */
37 public static final String NAMESPACE = "http://www.hpl.hp.com/semweb/2003/query_tester/";
38
39 protected static Log logger = LogFactory.getLog(TestRDFSRules.class);
40
41 /**
42 * Boilerplate for junit
43 */
44 public TestRDFSRules( String name ) {
45 super( name );
46 }
47
48 /**
49 * Boilerplate for junit.
50 * This is its own test suite
51 */
52 public static TestSuite suite() {
53 return new TestSuite(TestRDFSRules.class);
54 // TestSuite suite = new TestSuite();
55 // suite.addTest(new TestRDFSRules( "hiddenTestRDFSReasonerDebug" ));
56 // return suite;
57 }
58
59 /**
60 * Test a single RDFS case.
61 */
62 public void hiddenTestRDFSReasonerDebug() throws IOException {
63 ReasonerTester tester = new ReasonerTester("rdfs/manifest-nodirect-noresource.rdf");
64 ReasonerFactory rf = RDFSRuleReasonerFactory.theInstance();
65
66 assertTrue("RDFS hybrid-tgc reasoner test", tester.runTest("http://www.hpl.hp.com/semweb/2003/query_tester/rdfs/test11", rf, this, null));
67 }
68
69 /**
70 * Test the basic functioning of the hybrid RDFS rule reasoner
71 */
72 public void testRDFSFBReasoner() throws IOException {
73 ReasonerTester tester = new ReasonerTester("rdfs/manifest-nodirect-noresource.rdf");
74 ReasonerFactory rf = RDFSFBRuleReasonerFactory.theInstance();
75 assertTrue("RDFS hybrid reasoner tests", tester.runTests(rf, this, null));
76 }
77
78 /**
79 * Test the basic functioning of the hybrid RDFS rule reasoner with TGC cache
80 */
81 public void testRDFSExptReasoner() throws IOException {
82 ReasonerTester tester = new ReasonerTester("rdfs/manifest-nodirect-noresource.rdf");
83 ReasonerFactory rf = RDFSRuleReasonerFactory.theInstance();
84 assertTrue("RDFS experimental (hybrid+tgc) reasoner tests", tester.runTests(rf, this, null));
85 }
86
87 /**
88 * Test the capabilities description.
89 */
90 public void testRDFSDescription() {
91 ReasonerFactory rf = RDFSFBRuleReasonerFactory.theInstance();
92 Reasoner r = rf.create(null);
93 assertTrue(r.supportsProperty(RDFS.subClassOf));
94 assertTrue(r.supportsProperty(RDFS.domain));
95 assertTrue( ! r.supportsProperty(OWL.allValuesFrom));
96 }
97
98 /**
99 * Time a trial list of results from an inf graph.
100 */
101 private static void doTiming(Reasoner r, Model tbox, Model data, String name, int loop) {
102 Resource C1 = ResourceFactory.createResource("http://www.hpl.hp.com/semweb/2003/eg#C1");
103 Resource C2 = ResourceFactory.createResource("http://www.hpl.hp.com/semweb/2003/eg#C2");
104
105 long t1 = System.currentTimeMillis();
106 int count = 0;
107 for (int lp = 0; lp < loop; lp++) {
108 Model m = ModelFactory.createModelForGraph(r.bindSchema(tbox.getGraph()).bind(data.getGraph()));
109 count = 0;
110 for (Iterator i = m.listStatements(null, RDF.type, C1); i.hasNext(); i.next()) count++;
111 }
112 long t2 = System.currentTimeMillis();
113 long time10 = (t2-t1)*10/loop;
114 long time = time10/10;
115 long timeFraction = time10 - (time*10);
116 System.out.println(name + ": " + count +" results in " + time + "." + timeFraction +"ms");
117 // t1 = System.currentTimeMillis();
118 // for (int j = 0; j < 10; j++) {
119 // count = 0;
120 // for (Iterator i = m.listStatements(null, RDF.type, C1); i.hasNext(); i.next()) count++;
121 // }
122 // t2 = System.currentTimeMillis();
123 // System.out.println(name + ": " + count + " results in " + (t2-t1)/10 +"ms");
124 }
125
126 /**
127 * Simple timing test used to just a broad feel for how performance of the
128 * pure FPS rules compares with the hand-crafted version.
129 * The test ontology and data is very small. The test query is designed to
130 * require an interesting fraction of the inferences to be made but not all of them.
131 * The bigger the query the more advantage the FPS (which eagerly computes everything)
132 * would have over the normal approach.
133 */
134 public static void main(String[] args) {
135 try {
136 Model tbox = FileManager.get().loadModel("testing/reasoners/rdfs/timing-tbox.rdf");
137 Model data = FileManager.get().loadModel("testing/reasoners/rdfs/timing-data.rdf");
138 Reasoner rdfsFBRule = RDFSFBRuleReasonerFactory.theInstance().create(null);
139 Reasoner rdfs1 = RDFSReasonerFactory.theInstance().create(null);
140 Reasoner rdfsFinal = RDFSRuleReasonerFactory.theInstance().create(null);
141
142 doTiming(rdfs1, tbox, data, "RDFS1", 1);
143 doTiming(rdfsFBRule, tbox, data, "RDFS FB rule", 1);
144 doTiming(rdfsFinal, tbox, data, "RDFS final rule", 1);
145 doTiming(rdfs1, tbox, data, "RDFS1", 50);
146 doTiming(rdfsFBRule, tbox, data, "RDFS FB rule", 50);
147 doTiming(rdfsFinal, tbox, data, "RDFS final rule", 50);
148
149 } catch (Exception e) {
150 System.out.println("Problem: " + e.toString());
151 e.printStackTrace();
152 }
153 }
154
155 }
156
157 /*
158 (c) Copyright 2003, 2004, 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
165 1. Redistributions of source code must retain the above copyright
166 notice, this list of conditions and the following disclaimer.
167
168 2. Redistributions in binary form must reproduce the above copyright
169 notice, this list of conditions and the following disclaimer in the
170 documentation and/or other materials provided with the distribution.
171
172 3. The name of the author may not be used to endorse or promote products
173 derived from this software without specific prior written permission.
174
175 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
176 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
177 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
178 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
179 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
180 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
181 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
182 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
183 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
184 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
185 */
186