Source Code Cross Referenced for TestLPDerivation.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:        TestLPDerivation.java
003:         * Created by:  Dave Reynolds
004:         * Created on:  07-Oct-2005
005:         * 
006:         * (c) Copyright 2005, Hewlett-Packard Development Company, LP
007:         * [See end of file]
008:         * $Id: TestLPDerivation.java,v 1.4 2008/01/02 12:08:20 andy_seaborne Exp $
009:         *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.test;
010:
011:        import java.util.Arrays;
012:        import java.util.Iterator;
013:        import java.util.List;
014:
015:        import com.hp.hpl.jena.graph.Factory;
016:        import com.hp.hpl.jena.graph.Graph;
017:        import com.hp.hpl.jena.graph.Node;
018:        import com.hp.hpl.jena.graph.Triple;
019:        import com.hp.hpl.jena.graph.TripleMatch;
020:        import com.hp.hpl.jena.reasoner.InfGraph;
021:        import com.hp.hpl.jena.reasoner.rulesys.FBRuleInfGraph;
022:        import com.hp.hpl.jena.reasoner.rulesys.FBRuleReasoner;
023:        import com.hp.hpl.jena.reasoner.rulesys.Rule;
024:        import com.hp.hpl.jena.reasoner.rulesys.RuleDerivation;
025:        import com.hp.hpl.jena.util.iterator.ExtendedIterator;
026:
027:        import junit.framework.TestCase;
028:        import junit.framework.TestSuite;
029:
030:        /**
031:         * Test the derivation tracing of the LP system.
032:         * 
033:         * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
034:         * @version $Revision: 1.4 $
035:         */
036:
037:        public class TestLPDerivation extends TestCase {
038:
039:            /**
040:             * Boilerplate for junit
041:             */
042:            public TestLPDerivation(String name) {
043:                super (name);
044:            }
045:
046:            /**
047:             * Boilerplate for junit.
048:             * This is its own test suite
049:             */
050:            public static TestSuite suite() {
051:                return new TestSuite(TestLPDerivation.class);
052:            }
053:
054:            // Useful constants
055:            Node p = Node.createURI("p");
056:            Node q = Node.createURI("q");
057:            Node r = Node.createURI("r");
058:            Node s = Node.createURI("s");
059:            Node a = Node.createURI("a");
060:            Node b = Node.createURI("b");
061:            Node c = Node.createURI("c");
062:            Node d = Node.createURI("d");
063:            Node e = Node.createURI("e");
064:
065:            /**
066:             * Return an inference graph working over the given rule set and raw data.
067:             * Can be overridden by subclasses of this test class.
068:             * @param rules the rule set to use
069:             * @param data the graph of triples to process
070:             * @param tabled an array of predicates that should be tabled
071:             */
072:            public static InfGraph makeInfGraph(List rules, Graph data,
073:                    Node[] tabled) {
074:                FBRuleReasoner reasoner = new FBRuleReasoner(rules);
075:                FBRuleInfGraph infgraph = (FBRuleInfGraph) reasoner.bind(data);
076:                for (int i = 0; i < tabled.length; i++) {
077:                    infgraph.setTabled(tabled[i]);
078:                }
079:                //        infgraph.setTraceOn(true);
080:                infgraph.setDerivationLogging(true);
081:                return infgraph;
082:            }
083:
084:            /**
085:             * Run a single derivation test. Only tests the first derivation found.
086:             * @param ruleSrc source for a set of rules
087:             * @param tabled  array of predicate nodes which should be tabled by the rules
088:             * @param triples inital array of triple data
089:             * @param query   the query to be tested the first result will be checked
090:             * @param matches the set of triple matches which should occur in the derivation
091:             * @param rulenumber the index of the rule in the rule list which should occur in the derivation
092:             */
093:            private void doTest(String ruleSrc, Node[] tabled,
094:                    Triple[] triples, TripleMatch query, Triple[] matches,
095:                    int rulenumber) {
096:                List rules = Rule.parseRules(ruleSrc);
097:                Graph data = Factory.createGraphMem();
098:                for (int i = 0; i < triples.length; i++) {
099:                    data.add(triples[i]);
100:                }
101:                InfGraph infgraph = makeInfGraph(rules, data, tabled);
102:                ExtendedIterator results = infgraph.find(query);
103:                assertTrue(results.hasNext());
104:                Triple result = (Triple) results.next();
105:                results.close();
106:                Rule rule = (Rule) rules.get(rulenumber);
107:                List matchList = Arrays.asList(matches);
108:                Iterator derivations = infgraph.getDerivation(result);
109:                assertTrue(derivations.hasNext());
110:                RuleDerivation derivation = (RuleDerivation) derivations.next();
111:                //        PrintWriter pw = new PrintWriter(System.out);
112:                //        derivation.printTrace(pw, true);
113:                //        pw.close();
114:                assertEquals(result, derivation.getConclusion());
115:                assertEquals(matchList, derivation.getMatches());
116:                assertEquals(rule, derivation.getRule());
117:            }
118:
119:            /**
120:             * Test simple rule derivation.
121:             */
122:            public void testBasic() {
123:                doTest("(?x p ?y) <- (?x q ?y).", new Node[] {}, // Rules + tabling
124:                        new Triple[] { // Data
125:                        new Triple(a, q, b), }, new Triple(a, p, b), // query
126:                        new Triple[] { // Expected match list in derivation
127:                        new Triple(a, q, b) }, 0 // Expected rule in derivation
128:                );
129:            }
130:
131:            /**
132:             * Test simple rule derivation from pair
133:             */
134:            public void testBasic2() {
135:                doTest("(?x p ?y) <- (?x q ?y). (?x p ?y) <- (?x r ?y).",
136:                        new Node[] {}, // Rules + tabling
137:                        new Triple[] { // Data
138:                        new Triple(a, r, b), }, new Triple(a, p, b), // query
139:                        new Triple[] { // Expected match list in derivation
140:                        new Triple(a, r, b) }, 1 // Expected rule in derivation
141:                );
142:            }
143:
144:            /**
145:             * Test composite derivation.
146:             */
147:            public void testComposite() {
148:                doTest("(?x p ?y) <- (?x q ?y) (?x r ?y).",
149:                        new Node[] {}, // Rules + tabling
150:                        new Triple[] { // Data
151:                        new Triple(a, q, b), new Triple(a, r, b), },
152:                        new Triple(a, p, b), // query
153:                        new Triple[] { // Expected match list in derivation
154:                        new Triple(a, q, b), new Triple(a, r, b) }, 0 // Expected rule in derivation
155:                );
156:            }
157:
158:            /**
159:             * Test Chain derivation.
160:             */
161:            public void testChain() {
162:                doTest(
163:                        "(?x s ?y) <- (?x r ?y). (?x p ?y) <- (?x q ?y) (?x s ?y). ",
164:                        new Node[] {}, // Rules + tabling
165:                        new Triple[] { // Data
166:                        new Triple(a, q, b), new Triple(a, r, b), },
167:                        new Triple(a, p, b), // query
168:                        new Triple[] { // Expected match list in derivation
169:                        new Triple(a, q, b), new Triple(a, s, b) }, 1 // Expected rule in derivation
170:                );
171:            }
172:
173:            /**
174:             * Test tabled chaining
175:             */
176:            public void testTabled() {
177:                doTest("(?x p ?z) <- (?x p ?y) (?y p ?z).", new Node[] { p }, // Rules + tabling
178:                        new Triple[] { // Data
179:                        new Triple(a, p, b), new Triple(a, p, c),
180:                                new Triple(b, p, d), }, new Triple(a, p, d), // query
181:                        new Triple[] { // Expected match list in derivation
182:                        new Triple(a, p, b), new Triple(b, p, d) }, 0 // Expected rule in derivation
183:                );
184:            }
185:
186:        }
187:
188:        /*
189:         (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
190:         All rights reserved.
191:
192:         Redistribution and use in source and binary forms, with or without
193:         modification, are permitted provided that the following conditions
194:         are met:
195:
196:         1. Redistributions of source code must retain the above copyright
197:         notice, this list of conditions and the following disclaimer.
198:
199:         2. Redistributions in binary form must reproduce the above copyright
200:         notice, this list of conditions and the following disclaimer in the
201:         documentation and/or other materials provided with the distribution.
202:
203:         3. The name of the author may not be used to endorse or promote products
204:         derived from this software without specific prior written permission.
205:
206:         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
207:         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208:         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209:         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
210:         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
211:         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212:         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
213:         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
214:         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
215:         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
216:         */
w__w__w__.__j__a___v__a__2s___._co_m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.