Source Code Cross Referenced for TestFBRules.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) 


0001:        /******************************************************************
0002:         * File:        TestFBRules.java
0003:         * Created by:  Dave Reynolds
0004:         * Created on:  29-May-2003
0005:         * 
0006:         * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
0007:         * [See end of file]
0008:         * $Id: TestFBRules.java,v 1.52 2008/01/02 12:08:20 andy_seaborne Exp $
0009:         *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.test;
0010:
0011:        import com.hp.hpl.jena.reasoner.*;
0012:        import com.hp.hpl.jena.reasoner.rulesys.*;
0013:        import com.hp.hpl.jena.reasoner.test.TestUtil;
0014:        import com.hp.hpl.jena.datatypes.xsd.*;
0015:        import com.hp.hpl.jena.graph.*;
0016:        import com.hp.hpl.jena.rdf.model.*;
0017:        import com.hp.hpl.jena.shared.ClosedException;
0018:        import com.hp.hpl.jena.shared.impl.JenaParameters;
0019:        import com.hp.hpl.jena.util.FileManager;
0020:        import com.hp.hpl.jena.util.PrintUtil;
0021:        import com.hp.hpl.jena.util.iterator.ExtendedIterator;
0022:        import com.hp.hpl.jena.vocabulary.*;
0023:
0024:        import junit.framework.TestCase;
0025:        import junit.framework.TestSuite;
0026:
0027:        import java.io.IOException;
0028:        import java.util.*;
0029:
0030:        import org.apache.commons.logging.Log;
0031:        import org.apache.commons.logging.LogFactory;
0032:
0033:        /**
0034:         * Test suite for the hybrid forward/backward rule system.
0035:         * 
0036:         * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
0037:         * @version $Revision: 1.52 $ on $Date: 2008/01/02 12:08:20 $
0038:         */
0039:        public class TestFBRules extends TestCase {
0040:
0041:            protected static Log logger = LogFactory.getLog(TestFBRules.class);
0042:
0043:            // Useful constants
0044:            protected Node p = Node.createURI("p");
0045:            protected Node q = Node.createURI("q");
0046:            protected Node n1 = Node.createURI("n1");
0047:            protected Node n2 = Node.createURI("n2");
0048:            protected Node n3 = Node.createURI("n3");
0049:            protected Node n4 = Node.createURI("n4");
0050:            protected Node n5 = Node.createURI("n5");
0051:            protected Node res = Node.createURI("res");
0052:            protected Node r = Node.createURI("r");
0053:            protected Node s = Node.createURI("s");
0054:            protected Node t = Node.createURI("t");
0055:            protected Node u = Node.createURI("u");
0056:            protected Node v = Node.createURI("v");
0057:            protected Node w = Node.createURI("w");
0058:            protected Node x = Node.createURI("x");
0059:            protected Node a = Node.createURI("a");
0060:            protected Node b = Node.createURI("b");
0061:            protected Node c = Node.createURI("c");
0062:            protected Node d = Node.createURI("d");
0063:            protected Node C1 = Node.createURI("C1");
0064:            protected Node C2 = Node.createURI("C2");
0065:            protected Node C3 = Node.createURI("C3");
0066:            protected Node sP = RDFS.Nodes.subPropertyOf;
0067:            protected Node sC = RDFS.Nodes.subClassOf;
0068:            protected Node ty = RDF.Nodes.type;
0069:
0070:            /**
0071:             * Boilerplate for junit
0072:             */
0073:            public TestFBRules(String name) {
0074:                super (name);
0075:            }
0076:
0077:            /**
0078:             * Boilerplate for junit.
0079:             * This is its own test suite
0080:             */
0081:            public static TestSuite suite() {
0082:                return new TestSuite(TestFBRules.class);
0083:                //        TestSuite suite = new TestSuite();
0084:                //        suite.addTest(new TestFBRules( "testNumericFunctors" ));
0085:                //        return suite;
0086:            }
0087:
0088:            /**
0089:             * Override in subclasses to test other reasoners.
0090:             */
0091:            public Reasoner createReasoner(List rules) {
0092:                FBRuleReasoner reasoner = new FBRuleReasoner(rules);
0093:                reasoner.tablePredicate(RDFS.Nodes.subClassOf);
0094:                reasoner.tablePredicate(RDF.Nodes.type);
0095:                reasoner.tablePredicate(p);
0096:                return reasoner;
0097:            }
0098:
0099:            /**
0100:             * Assemble a test infGraph from a rule source and starting data
0101:             */
0102:            public InfGraph createInfGraph(String rules, Graph data) {
0103:                return createReasoner(Rule.parseRules(rules)).bind(data);
0104:            }
0105:
0106:            /**
0107:             * Assemble a test infGraph from a rule source and empty data
0108:             */
0109:            public InfGraph createInfGraph(String rules) {
0110:                return createReasoner(Rule.parseRules(rules)).bind(
0111:                        Factory.createGraphMem());
0112:            }
0113:
0114:            /**
0115:             * Check parser extension for f/b distinction.
0116:             */
0117:            public void testParser() {
0118:                String rf = "(?a rdf:type ?t) -> (?t rdf:type rdfs:Class).";
0119:                String rb = "(?t rdf:type rdfs:Class) <- (?a rdf:type ?t).";
0120:                assertTrue(!Rule.parseRule(rf).isBackward());
0121:                assertTrue(Rule.parseRule(rb).isBackward());
0122:            }
0123:
0124:            /**
0125:             * Minimal rule tester to check basic pattern match, forward style.
0126:             */
0127:            public void testRuleMatcher() {
0128:                String rules = "[r1: (?a p ?b), (?b q ?c) -> (?a, q, ?c)]"
0129:                        + "[r2: (?a p ?b), (?b p ?c) -> (?a, p, ?c)]"
0130:                        + "[r3: (?a p ?a), (n1 p ?c), (n1, p, ?a) -> (?a, p, ?c)]"
0131:                        + "[r4: (n4 ?p ?a) -> (n4, ?a, ?p)]";
0132:
0133:                InfGraph infgraph = createInfGraph(rules);
0134:                infgraph.add(new Triple(n1, p, n2));
0135:                infgraph.add(new Triple(n2, p, n3));
0136:                infgraph.add(new Triple(n2, q, n3));
0137:                infgraph.add(new Triple(n4, p, n4));
0138:
0139:                TestUtil.assertIteratorValues(this , infgraph.find(null, null,
0140:                        null), new Triple[] { new Triple(n1, p, n2),
0141:                        new Triple(n2, p, n3), new Triple(n2, q, n3),
0142:                        new Triple(n4, p, n4), new Triple(n1, p, n3),
0143:                        new Triple(n1, q, n3), new Triple(n4, n4, p), });
0144:            }
0145:
0146:            /**
0147:             * Test functor handling
0148:             */
0149:            public void testEmbeddedFunctors() {
0150:                String rules = "(?C owl:onProperty ?P), (?C owl:allValuesFrom ?D) -> (?C rb:restriction all(?P, ?D))."
0151:                        + "(?C rb:restriction all(eg:p, eg:D)) -> (?C rb:restriction 'allOK')."
0152:                        + "[ -> (eg:foo eg:prop functor(eg:bar, 1)) ]"
0153:                        + "[ (?x eg:prop functor(eg:bar, ?v)) -> (?x eg:propbar ?v) ]"
0154:                        + "[ (?x eg:prop functor(?v, ?*)) -> (?x eg:propfunc ?v) ]"
0155:                        + "";
0156:
0157:                Model data = ModelFactory.createDefaultModel();
0158:                Resource R1 = data.createResource(PrintUtil.egNS + "R1");
0159:                Resource D = data.createResource(PrintUtil.egNS + "D");
0160:                Property p = data.createProperty(PrintUtil.egNS, "p");
0161:                Property propbar = data.createProperty(PrintUtil.egNS,
0162:                        "propbar");
0163:                Property rbr = data.createProperty(
0164:                        ReasonerVocabulary.RBNamespace, "restriction");
0165:                R1.addProperty(OWL.onProperty, p).addProperty(
0166:                        OWL.allValuesFrom, D);
0167:
0168:                InfGraph infgraph = createInfGraph(rules, data.getGraph());
0169:                Model infModel = ModelFactory.createModelForGraph(infgraph);
0170:                Resource foo = infModel.createResource(PrintUtil.egNS + "foo");
0171:
0172:                RDFNode flit = infModel.getResource(R1.getURI())
0173:                        .getRequiredProperty(rbr).getObject();
0174:                assertNotNull(flit);
0175:                assertEquals(flit.toString(), "allOK");
0176:                //        assertTrue(flit instanceof Literal);
0177:                //        Functor func = (Functor)((Literal)flit).getValue();
0178:                //        assertEquals("all", func.getName());
0179:                //        assertEquals(p.getNode(), func.getArgs()[0]);
0180:                //        assertEquals(D.getNode(), func.getArgs()[1]);
0181:
0182:                Literal one = (Literal) foo.getRequiredProperty(propbar)
0183:                        .getObject();
0184:                assertEquals(new Integer(1), one.getValue());
0185:            }
0186:
0187:            /**
0188:             * The the minimal machinery for supporting builtins
0189:             */
0190:            public void testBuiltins() {
0191:                String rules = //"[testRule1: (n1 ?p ?a) -> print('rule1test', ?p, ?a)]" +
0192:                "[r1: (n1 p ?x), addOne(?x, ?y) -> (n1 q ?y)]"
0193:                        + "[r2: (n1 p ?x), lessThan(?x, 3) -> (n2 q ?x)]"
0194:                        + "[axiom1: -> (n1 p 1)]" + "[axiom2: -> (n1 p 4)]"
0195:                        + "";
0196:
0197:                InfGraph infgraph = createInfGraph(rules);
0198:                TestUtil.assertIteratorValues(this , infgraph.find(n1, q, null),
0199:                        new Triple[] { new Triple(n1, q, Util.makeIntNode(2)),
0200:                                new Triple(n1, q, Util.makeIntNode(5)) });
0201:                TestUtil
0202:                        .assertIteratorValues(this , infgraph.find(n2, q, null),
0203:                                new Triple[] { new Triple(n2, q, Util
0204:                                        .makeIntNode(1)) });
0205:
0206:            }
0207:
0208:            /**
0209:             * Test schmea partial binding machinery, forward subset.
0210:             */
0211:            public void testSchemaBinding() {
0212:                String rules = "[testRule1: (n1 p ?a) -> (n2, p, ?a)]"
0213:                        + "[testRule2: (n1 q ?a) -> (n2, q, ?a)]"
0214:                        + "[testRule3: (n2 p ?a), (n2 q ?a) -> (res p ?a)]"
0215:                        + "[testBRule4: (n3 p ?a) <- (n1, p, ?a)]";
0216:                List ruleList = Rule.parseRules(rules);
0217:                Graph schema = Factory.createGraphMem();
0218:                schema.add(new Triple(n1, p, n3));
0219:                Graph data = Factory.createGraphMem();
0220:                data.add(new Triple(n1, q, n4));
0221:                data.add(new Triple(n1, q, n3));
0222:
0223:                Reasoner reasoner = createReasoner(ruleList);
0224:                Reasoner boundReasoner = reasoner.bindSchema(schema);
0225:                InfGraph infgraph = boundReasoner.bind(data);
0226:
0227:                TestUtil.assertIteratorValues(this , infgraph.find(null, null,
0228:                        null), new Triple[] { new Triple(n1, p, n3),
0229:                        new Triple(n2, p, n3), new Triple(n3, p, n3),
0230:                        new Triple(n1, q, n4), new Triple(n2, q, n4),
0231:                        new Triple(n1, q, n3), new Triple(n2, q, n3),
0232:                        new Triple(res, p, n3) });
0233:            }
0234:
0235:            /**
0236:             * The the "remove" builtin
0237:             */
0238:            public void testRemoveBuiltin() {
0239:                String rules = "[rule1: (?x p ?y), (?x q ?y) -> remove(0)]"
0240:                        + "";
0241:
0242:                InfGraph infgraph = createInfGraph(rules);
0243:                infgraph.add(new Triple(n1, p, Util.makeIntNode(1)));
0244:                infgraph.add(new Triple(n1, p, Util.makeIntNode(2)));
0245:                infgraph.add(new Triple(n1, q, Util.makeIntNode(2)));
0246:
0247:                TestUtil.assertIteratorValues(this , infgraph.find(n1, null,
0248:                        null), new Triple[] {
0249:                        new Triple(n1, p, Util.makeIntNode(1)),
0250:                        new Triple(n1, q, Util.makeIntNode(2)) });
0251:
0252:            }
0253:
0254:            /**
0255:             * Test the rebind operation.
0256:             */
0257:            public void testRebind() {
0258:                String rules = "[rule1: (?x p ?y) -> (?x q ?y)]";
0259:                Graph data = Factory.createGraphMem();
0260:                data.add(new Triple(n1, p, n2));
0261:                InfGraph infgraph = createInfGraph(rules, data);
0262:                TestUtil.assertIteratorValues(this , infgraph.find(n1, null,
0263:                        null), new Triple[] { new Triple(n1, p, n2),
0264:                        new Triple(n1, q, n2) });
0265:                Graph ndata = Factory.createGraphMem();
0266:                ndata.add(new Triple(n1, p, n3));
0267:                infgraph.rebind(ndata);
0268:                TestUtil.assertIteratorValues(this , infgraph.find(n1, null,
0269:                        null), new Triple[] { new Triple(n1, p, n3),
0270:                        new Triple(n1, q, n3) });
0271:            }
0272:
0273:            /**
0274:             * Test that reset does actually clear out all the data.
0275:             * We use the RDFS configuration because uses both TGC, forward and backward
0276:             * rules and so is a good check.
0277:             */
0278:            public void testRebind2() {
0279:                String NS = "http://jena.hpl.hp.com/test#";
0280:                Model base = ModelFactory.createDefaultModel();
0281:                Resource A = base.createResource(NS + "A");
0282:                Resource B = base.createResource(NS + "B");
0283:                Resource I = base.createResource(NS + "i");
0284:                A.addProperty(RDFS.subClassOf, B);
0285:                I.addProperty(RDF.type, A);
0286:                InfModel inf = ModelFactory.createInfModel(ReasonerRegistry
0287:                        .getRDFSReasoner(), base);
0288:                assertTrue(inf.containsResource(A) && inf.containsResource(I));
0289:                base.removeAll();
0290:                inf.rebind();
0291:                assertFalse(inf.containsResource(A) || inf.containsResource(I));
0292:            }
0293:
0294:            /**
0295:             * Test rebindAll reconsults a changed ruleset
0296:             */
0297:            public void testRebindAll() {
0298:                String NS = "http://jena.hpl.hp.com/example#";
0299:                List rules1 = Rule
0300:                        .parseRules("(?x http://jena.hpl.hp.com/example#p ?y) -> (?x http://jena.hpl.hp.com/example#q ?y).");
0301:                List rules2 = Rule
0302:                        .parseRules("(?x http://jena.hpl.hp.com/example#q ?y) -> (?x http://jena.hpl.hp.com/example#r ?y).");
0303:                Model m = ModelFactory.createDefaultModel();
0304:                Property p = m.createProperty(NS + "p");
0305:                Property q = m.createProperty(NS + "q");
0306:                Property r = m.createProperty(NS + "r");
0307:                Resource a = m.createResource(NS + "a");
0308:                Resource b = m.createResource(NS + "b");
0309:                Statement s1 = m.createStatement(a, p, b);
0310:                Statement s2 = m.createStatement(a, q, b);
0311:                Statement s3 = m.createStatement(a, r, b);
0312:                m.add(s1);
0313:                GenericRuleReasoner reasoner = new GenericRuleReasoner(rules1);
0314:                InfModel infModel = ModelFactory.createInfModel(reasoner, m);
0315:                reasoner.addRules(rules2);
0316:                TestUtil.assertIteratorValues(this , infModel.listStatements(a,
0317:                        null, (RDFNode) null), new Object[] { s1, s2 });
0318:                ((FBRuleInfGraph) infModel.getGraph()).rebindAll();
0319:                TestUtil.assertIteratorValues(this , infModel.listStatements(a,
0320:                        null, (RDFNode) null), new Object[] { s1, s2, s3 });
0321:            }
0322:
0323:            /**
0324:             * Test the close operation.
0325:             */
0326:            public void testClose() {
0327:                String rules = "[rule1: (?x p ?y) -> (?x q ?y)]";
0328:                Graph data = Factory.createGraphMem();
0329:                data.add(new Triple(n1, p, n2));
0330:                InfGraph infgraph = createInfGraph(rules, data);
0331:                TestUtil.assertIteratorValues(this , infgraph.find(n1, null,
0332:                        null), new Triple[] { new Triple(n1, p, n2),
0333:                        new Triple(n1, q, n2) });
0334:                infgraph.close();
0335:                boolean foundException = false;
0336:                try {
0337:                    infgraph.find(n1, null, null);
0338:                } catch (ClosedException e) {
0339:                    foundException = true;
0340:                }
0341:                assertTrue("Close detected", foundException);
0342:            }
0343:
0344:            /**
0345:             * Test example pure backchaining rules
0346:             */
0347:            public void testBackchain1() {
0348:                Graph data = Factory.createGraphMem();
0349:                data.add(new Triple(p, sP, q));
0350:                data.add(new Triple(q, sP, r));
0351:                data.add(new Triple(C1, sC, C2));
0352:                data.add(new Triple(C2, sC, C3));
0353:                data.add(new Triple(a, ty, C1));
0354:                String rules = "[rdfs8:  (?a rdfs:subClassOf ?c) <- (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c)]"
0355:                        + "[rdfs9:  (?a rdf:type ?y) <- (?x rdfs:subClassOf ?y), (?a rdf:type ?x)]"
0356:                        + "[-> (rdf:type rdfs:range rdfs:Class)]"
0357:                        + "[rdfs3:  (?y rdf:type ?c) <- (?x ?p ?y), (?p rdfs:range ?c)]"
0358:                        + "[rdfs7:  (?a rdfs:subClassOf ?a) <- (?a rdf:type rdfs:Class)]";
0359:                InfGraph infgraph = createInfGraph(rules, data);
0360:                TestUtil.assertIteratorValues(this , infgraph.find(a, ty, null),
0361:                        new Object[] { new Triple(a, ty, C1),
0362:                                new Triple(a, ty, C2), new Triple(a, ty, C3) });
0363:                TestUtil.assertIteratorValues(this , infgraph.find(C1, sC, a),
0364:                        new Object[] {});
0365:            }
0366:
0367:            /**
0368:             * Test complex rule head unification
0369:             */
0370:            public void testBackchain2() {
0371:                Graph data = Factory.createGraphMem();
0372:                data.add(new Triple(c, q, d));
0373:                String rules = "[r1: (c r ?x) <- (?x p f(?x b))]"
0374:                        + "[r2: (?y p f(a ?y)) <- (c q ?y)]";
0375:                InfGraph infgraph = createInfGraph(rules, data);
0376:                TestUtil.assertIteratorValues(this , infgraph.find(c, r, null),
0377:                        new Object[] {});
0378:
0379:                data.add(new Triple(c, q, a));
0380:                rules = "[r1: (c r ?x) <- (?x p f(?x a))]"
0381:                        + "[r2: (?y p f(a ?y)) <- (c q ?y)]";
0382:                infgraph = createInfGraph(rules, data);
0383:                TestUtil.assertIteratorValues(this , infgraph.find(c, r, null),
0384:                        new Object[] { new Triple(c, r, a) });
0385:
0386:                data = Factory.createGraphMem();
0387:                data.add(new Triple(a, q, a));
0388:                data.add(new Triple(a, q, b));
0389:                data.add(new Triple(a, q, c));
0390:                data.add(new Triple(b, q, d));
0391:                data.add(new Triple(b, q, b));
0392:                rules = "[r1: (c r ?x) <- (?x p ?x)]"
0393:                        + "[r2: (?x p ?y) <- (a q ?x), (b q ?y)]";
0394:                infgraph = createInfGraph(rules, data);
0395:                TestUtil.assertIteratorValues(this , infgraph.find(c, r, null),
0396:                        new Object[] { new Triple(c, r, b) });
0397:
0398:                rules = "[r1: (c r ?x) <- (?x p ?x)]"
0399:                        + "[r2: (a p ?x) <- (a q ?x)]";
0400:                infgraph = createInfGraph(rules, data);
0401:                TestUtil.assertIteratorValues(this , infgraph.find(c, r, null),
0402:                        new Object[] { new Triple(c, r, a) });
0403:            }
0404:
0405:            /**
0406:             * Test restriction example
0407:             */
0408:            public void testBackchain3() {
0409:                Graph data = Factory.createGraphMem();
0410:                data.add(new Triple(a, ty, r));
0411:                data.add(new Triple(a, p, b));
0412:                data.add(new Triple(r, sC, C1));
0413:                data.add(new Triple(C1, ty, OWL.Restriction.asNode()));
0414:                data.add(new Triple(C1, OWL.onProperty.asNode(), p));
0415:                data.add(new Triple(C1, OWL.allValuesFrom.asNode(), c));
0416:                String rules = "[rdfs9:   (?a rdf:type ?y) <- (?x rdfs:subClassOf ?y) (?a rdf:type ?x)]"
0417:                        + "[restriction2: (?C owl:equivalentClass all(?P, ?D)) <- (?C rdf:type owl:Restriction), (?C owl:onProperty ?P), (?C owl:allValuesFrom ?D)]"
0418:                        + "[rs2: (?X rdf:type all(?P,?C)) <- (?D owl:equivalentClass all(?P,?C)), (?X rdf:type ?D)]"
0419:                        + "[rp4: (?Y rdf:type ?C) <- (?X rdf:type all(?P, ?C)), (?X ?P ?Y)]";
0420:                InfGraph infgraph = createInfGraph(rules, data);
0421:                TestUtil.assertIteratorValues(this , infgraph.find(b, ty, c),
0422:                        new Object[] { new Triple(b, ty, c) });
0423:            }
0424:
0425:            /**
0426:             * Test example hybrid rule.
0427:             */
0428:            public void testHybrid1() {
0429:                Graph data = Factory.createGraphMem();
0430:                data.add(new Triple(a, p, b));
0431:                data.add(new Triple(p, ty, s));
0432:                String rules = "[r1: (?p rdf:type s) -> [r1b: (?x ?p ?y) <- (?y ?p ?x)]]";
0433:                InfGraph infgraph = createInfGraph(rules, data);
0434:                TestUtil.assertIteratorValues(this , infgraph
0435:                        .find(null, p, null), new Object[] {
0436:                        new Triple(a, p, b), new Triple(b, p, a) });
0437:            }
0438:
0439:            /**
0440:             * Test example hybrid rule.
0441:             */
0442:            public void testHybrid2() {
0443:                Graph data = Factory.createGraphMem();
0444:                data.add(new Triple(a, r, b));
0445:                data.add(new Triple(p, ty, s));
0446:                String rules = "[a1: -> (a rdf:type t)]"
0447:                        + "[r0: (?x r ?y) -> (?x p ?y)]"
0448:                        + "[r1: (?p rdf:type s) -> [r1b: (?x ?p ?y) <- (?y ?p ?x)]]"
0449:                        + "[r2: (?p rdf:type s) -> [r2b: (?x ?p ?x) <- (?x rdf:type t)]]";
0450:                FBRuleInfGraph infgraph = (FBRuleInfGraph) createInfGraph(
0451:                        rules, data);
0452:                infgraph.setDerivationLogging(true);
0453:                infgraph.prepare();
0454:                assertTrue("Forward rule count", infgraph.getNRulesFired() == 3);
0455:                TestUtil.assertIteratorValues(this , infgraph
0456:                        .find(null, p, null), new Object[] {
0457:                        new Triple(a, p, a), new Triple(a, p, b),
0458:                        new Triple(b, p, a) });
0459:                // Suppressed until LP engine implements rule counting, if ever
0460:                //        assertTrue("Backward rule count", infgraph.getNRulesFired() == 8);
0461:
0462:                // Check derivation tracing as well
0463:                // Suppressed until LP engine implements derivation tracing
0464:                Iterator di = infgraph.getDerivation(new Triple(b, p, a));
0465:                assertTrue(di.hasNext());
0466:                RuleDerivation d = (RuleDerivation) di.next();
0467:                assertTrue(d.getRule().getName().equals("r1b"));
0468:                TestUtil.assertIteratorValues(this , d.getMatches().iterator(),
0469:                        new Object[] { new Triple(a, p, b) });
0470:                assertTrue(!di.hasNext());
0471:            }
0472:
0473:            /**
0474:             * Test example hybrid rules for rdfs.
0475:             */
0476:            public void testHybridRDFS() {
0477:                Graph data = Factory.createGraphMem();
0478:                data.add(new Triple(a, p, b));
0479:                data.add(new Triple(p, RDFS.range.asNode(), C1));
0480:                String rules = "[rdfs2:  (?p rdfs:domain ?c) -> [(?x rdf:type ?c) <- (?x ?p ?y)] ]"
0481:                        + "[rdfs3:  (?p rdfs:range ?c)  -> [(?y rdf:type ?c) <- (?x ?p ?y)] ]"
0482:                        + "[rdfs5a: (?a rdfs:subPropertyOf ?b), (?b rdfs:subPropertyOf ?c) -> (?a rdfs:subPropertyOf ?c)]"
0483:                        + "[rdfs5b: (?a rdf:type rdf:Property) -> (?a rdfs:subPropertyOf ?a)]"
0484:                        + "[rdfs6:  (?p rdfs:subPropertyOf ?q) -> [ (?a ?q ?b) <- (?a ?p ?b)] ]"
0485:                        + "[rdfs7:  (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]"
0486:                        + "[rdfs8:  (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]"
0487:                        + "[rdfs9:  (?x rdfs:subClassOf ?y) -> [ (?a rdf:type ?y) <- (?a rdf:type ?x)] ]";
0488:                InfGraph infgraph = createInfGraph(rules, data);
0489:                //        ((FBRuleInfGraph)infgraph).setTraceOn(true);
0490:                TestUtil.assertIteratorValues(this , infgraph.find(b, ty, null),
0491:                        new Object[] { new Triple(b, ty, C1) });
0492:            }
0493:
0494:            /**
0495:             * Test example hybrid rules for rdfs.
0496:             */
0497:            public void testHybridRDFS2() {
0498:                Graph data = Factory.createGraphMem();
0499:                data.add(new Triple(a, p, b));
0500:                data.add(new Triple(p, sP, r));
0501:                data.add(new Triple(r, RDFS.range.asNode(), C1));
0502:                String rules = "[rdfs3:  (?p rdfs:range ?c)  -> [(?y rdf:type ?c) <- (?x ?p ?y)] ]"
0503:                        + "[rdfs6:  (?p rdfs:subPropertyOf ?q) -> [ (?a ?q ?b) <- (?a ?p ?b)] ]";
0504:                InfGraph infgraph = createInfGraph(rules, data);
0505:                //        ((FBRuleInfGraph)infgraph).setTraceOn(true);
0506:                TestUtil.assertIteratorValues(this , infgraph.find(b, ty, C1),
0507:                        new Object[] { new Triple(b, ty, C1) });
0508:            }
0509:
0510:            /**
0511:             * Test access to makeInstance machinery from a Brule.
0512:             */
0513:            public void testMakeInstance() {
0514:                Graph data = Factory.createGraphMem();
0515:                data.add(new Triple(a, ty, C1));
0516:                String rules = "[r1:  (?x p ?t) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]"
0517:                        + "[r2:  (?t rdf:type C2) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]";
0518:                InfGraph infgraph = createInfGraph(rules, data);
0519:
0520:                Node valueInstance = getValue(infgraph, a, p);
0521:                assertNotNull(valueInstance);
0522:                Node valueInstance2 = getValue(infgraph, a, p);
0523:                assertEquals(valueInstance, valueInstance2);
0524:                Node valueType = getValue(infgraph, valueInstance, RDF.type
0525:                        .asNode());
0526:                assertEquals(valueType, C2);
0527:            }
0528:
0529:            /**
0530:             * Test access to makeInstance machinery from a Brule.
0531:             */
0532:            public void testMakeInstances() {
0533:                Graph data = Factory.createGraphMem();
0534:                data.add(new Triple(a, ty, C1));
0535:                String rules = "[r1:  (?x p ?t) <- (?x rdf:type C1), makeInstance(?x, p, ?t)]";
0536:                InfGraph infgraph = createInfGraph(rules, data);
0537:
0538:                Node valueInstance = getValue(infgraph, a, p);
0539:                assertNotNull(valueInstance);
0540:                Node valueInstance2 = getValue(infgraph, a, p);
0541:                assertEquals(valueInstance, valueInstance2);
0542:            }
0543:
0544:            /**
0545:             * Test case for makeInstance which failed during development.
0546:             */
0547:            public void testMakeInstanceBug() {
0548:                Graph data = Factory.createGraphMem();
0549:                data.add(new Triple(a, ty, r));
0550:                data.add(new Triple(r, sC, Functor.makeFunctorNode("some",
0551:                        new Node[] { p, C1 })));
0552:                String rules = "[some1: (?C rdfs:subClassOf some(?P, ?D)) ->"
0553:                        + "[some1b: (?X ?P ?T) <- (?X rdf:type ?C), unbound(?T), noValue(?X, ?P), makeInstance(?X, ?P, ?D, ?T) ]"
0554:                        + "[some1b2: (?T rdf:type ?D) <- (?X rdf:type ?C), bound(?T), makeInstance(?X, ?P, ?D, ?T) ]"
0555:                        + "]";
0556:                InfGraph infgraph = createInfGraph(rules, data);
0557:
0558:                Node valueInstance = getValue(infgraph, a, p);
0559:                assertNotNull(valueInstance);
0560:                Node valueType = getValue(infgraph, valueInstance, ty);
0561:                assertEquals(valueType, C1);
0562:
0563:            }
0564:
0565:            /**
0566:             * Test numeric functors
0567:             */
0568:            public void testNumericFunctors() {
0569:                String rules = "[r1: (?x p f(a, ?x)) -> (?x q f(?x)) ]"
0570:                        + "[r1: (?x p f(a, 0)) -> (?x s res) ]" + "";
0571:                Graph data = Factory.createGraphMem();
0572:                data.add(new Triple(n1, p, Util.makeIntNode(2)));
0573:                data.add(new Triple(n2, p, Functor.makeFunctorNode("f",
0574:                        new Node[] { a, Util.makeIntNode(0) })));
0575:                data.add(new Triple(n3, p, Functor.makeFunctorNode("f",
0576:                        new Node[] {
0577:                                a,
0578:                                Node.createLiteral("0", "",
0579:                                        XSDDatatype.XSDnonNegativeInteger) })));
0580:                InfGraph infgraph = createInfGraph(rules, data);
0581:
0582:                TestUtil.assertIteratorValues(this , infgraph
0583:                        .find(null, s, null), new Triple[] {
0584:                        new Triple(n2, s, res), new Triple(n3, s, res), });
0585:            }
0586:
0587:            /**
0588:             * Test the builtins themselves
0589:             */
0590:            public void testBuiltins2() {
0591:                // Numeric comparisions
0592:                Node lt = Node.createURI("lt");
0593:                Node gt = Node.createURI("gt");
0594:                Node le = Node.createURI("le");
0595:                Node ge = Node.createURI("ge");
0596:                Node eq = Node.createURI("eq");
0597:                Node ne = Node.createURI("ne");
0598:                String rules = "[r1: (?x q ?vx), (?y q ?vy), lessThan(?vx, ?vy) -> (?x lt ?y)]"
0599:                        + "[r2: (?x q ?vx), (?y q ?vy), greaterThan(?vx, ?vy) -> (?x gt ?y)]"
0600:                        + "[r3: (?x q ?vx), (?y q ?vy), le(?vx, ?vy) -> (?x le ?y)]"
0601:                        + "[r4: (?x q ?vx), (?y q ?vy), ge(?vx, ?vy) -> (?x ge ?y)]"
0602:                        + "[r5: (?x q ?vx), (?y q ?vy), notEqual(?vx, ?vy) -> (?x ne ?y)]"
0603:                        + "[r6: (?x q ?vx), (?y q ?vy), equal(?vx, ?vy) -> (?x eq ?y)]"
0604:                        + "";
0605:                Graph data = Factory.createGraphMem();
0606:                data.add(new Triple(n1, q, Util.makeIntNode(2)));
0607:                data.add(new Triple(n2, q, Util.makeIntNode(2)));
0608:                data.add(new Triple(n3, q, Util.makeIntNode(3)));
0609:                InfGraph infgraph = createInfGraph(rules, data);
0610:
0611:                TestUtil.assertIteratorValues(this ,
0612:                        infgraph.find(n1, null, n2), new Triple[] {
0613:                                new Triple(n1, eq, n2), new Triple(n1, le, n2),
0614:                                new Triple(n1, ge, n2), });
0615:                TestUtil.assertIteratorValues(this ,
0616:                        infgraph.find(n1, null, n3), new Triple[] {
0617:                                new Triple(n1, ne, n3), new Triple(n1, lt, n3),
0618:                                new Triple(n1, le, n3), });
0619:                TestUtil.assertIteratorValues(this ,
0620:                        infgraph.find(n3, null, n1), new Triple[] {
0621:                                new Triple(n3, ne, n1), new Triple(n3, gt, n1),
0622:                                new Triple(n3, ge, n1), });
0623:
0624:                // Floating point comparisons
0625:                data = Factory.createGraphMem();
0626:                data.add(new Triple(n1, q, Util.makeIntNode(2)));
0627:                data.add(new Triple(n2, q, Util.makeDoubleNode(2.2)));
0628:                data.add(new Triple(n3, q, Util.makeDoubleNode(2.3)));
0629:                infgraph = createInfGraph(rules, data);
0630:
0631:                TestUtil.assertIteratorValues(this ,
0632:                        infgraph.find(n1, null, n2), new Triple[] {
0633:                                new Triple(n1, ne, n2), new Triple(n1, le, n2),
0634:                                new Triple(n1, lt, n2), });
0635:                TestUtil.assertIteratorValues(this ,
0636:                        infgraph.find(n2, null, n3), new Triple[] {
0637:                                new Triple(n2, ne, n3), new Triple(n2, le, n3),
0638:                                new Triple(n2, lt, n3), });
0639:
0640:                // XSD timeDate point comparisons
0641:                data = Factory.createGraphMem();
0642:                XSDDatatype dt = new XSDDatatype("dateTime");
0643:                data.add(new Triple(n1, q, Node.createLiteral(
0644:                        "2000-03-04T20:00:00Z", "", XSDDatatype.XSDdateTime)));
0645:                data.add(new Triple(n2, q, Node.createLiteral(
0646:                        "2001-03-04T20:00:00Z", "", XSDDatatype.XSDdateTime)));
0647:                data.add(new Triple(n3, q, Node.createLiteral(
0648:                        "2002-03-04T20:00:00Z", "", XSDDatatype.XSDdateTime)));
0649:                infgraph = createInfGraph(rules, data);
0650:
0651:                TestUtil.assertIteratorValues(this ,
0652:                        infgraph.find(n1, null, n2), new Triple[] {
0653:                                new Triple(n1, ne, n2), new Triple(n1, le, n2),
0654:                                new Triple(n1, lt, n2), });
0655:                TestUtil.assertIteratorValues(this ,
0656:                        infgraph.find(n2, null, n3), new Triple[] {
0657:                                new Triple(n2, ne, n3), new Triple(n2, le, n3),
0658:                                new Triple(n2, lt, n3), });
0659:                TestUtil.assertIteratorValues(this ,
0660:                        infgraph.find(n2, null, n1), new Triple[] {
0661:                                new Triple(n2, ne, n1), new Triple(n2, ge, n1),
0662:                                new Triple(n2, gt, n1), });
0663:                TestUtil.assertIteratorValues(this ,
0664:                        infgraph.find(n3, null, n2), new Triple[] {
0665:                                new Triple(n3, ne, n2), new Triple(n3, ge, n2),
0666:                                new Triple(n3, gt, n2), });
0667:
0668:                // Support for now(?x)
0669:                rules = "[r1: now(?x) -> (a p ?x)]";
0670:                infgraph = createInfGraph(rules);
0671:                infgraph.prepare();
0672:                Graph result = infgraph.getDeductionsGraph();
0673:                assertEquals(1, result.size());
0674:                Triple tr = (Triple) result.find(null, null, null).next();
0675:                Node nowN = tr.getObject();
0676:                assertTrue(nowN.isLiteral());
0677:                Object nowO = nowN.getLiteralValue();
0678:                assertTrue(nowO instanceof  XSDDateTime);
0679:
0680:                // Arithmetic            
0681:                rules = "[r1: (?x p ?a), (?x q ?b), sum(?a, ?b, ?c) -> (?x s ?c)]"
0682:                        + "[r2: (?x p ?a), (?x q ?b), product(?a, ?b, ?c) -> (?x t ?c)]"
0683:                        + "[r3: (?x p ?a), (?x q ?b), difference(?b, ?a, ?c) -> (?x u ?c)]"
0684:                        + "[r4: (?x p ?a), (?x q ?b), quotient(?b, ?a, ?c) -> (?x v ?c)]"
0685:                        + "[r4: (?x p ?a), (?x q ?b), min(?b, ?a, ?c) -> (?x r ?c)]"
0686:                        + "[r4: (?x p ?a), (?x q ?b), max(?b, ?a, ?c) -> (?x x ?c)]"
0687:                        + "";
0688:                data = Factory.createGraphMem();
0689:                data.add(new Triple(n1, p, Util.makeIntNode(3)));
0690:                data.add(new Triple(n1, q, Util.makeIntNode(5)));
0691:                infgraph = createInfGraph(rules, data);
0692:
0693:                TestUtil.assertIteratorValues(this , infgraph.find(n1, null,
0694:                        null), new Triple[] {
0695:                        new Triple(n1, p, Util.makeIntNode(3)),
0696:                        new Triple(n1, q, Util.makeIntNode(5)),
0697:                        new Triple(n1, s, Util.makeIntNode(8)),
0698:                        new Triple(n1, t, Util.makeIntNode(15)),
0699:                        new Triple(n1, u, Util.makeIntNode(2)),
0700:                        new Triple(n1, v, Util.makeIntNode(1)),
0701:                        new Triple(n1, r, Util.makeIntNode(3)),
0702:                        new Triple(n1, x, Util.makeIntNode(5)), });
0703:
0704:                // Note type checking   
0705:                rules = "[r1: (?x p ?y), isLiteral(?y) -> (?x s 'literal')]"
0706:                        + "[r1: (?x p ?y), notLiteral(?y) -> (?x s 'notLiteral')]"
0707:                        + "[r1: (?x p ?y), isBNode(?y) -> (?x s 'bNode')]"
0708:                        + "[r1: (?x p ?y), notBNode(?y) -> (?x s 'notBNode')]"
0709:                        + "";
0710:                data = Factory.createGraphMem();
0711:                data.add(new Triple(n1, p, Util.makeIntNode(3)));
0712:                data.add(new Triple(n2, p, res));
0713:                data.add(new Triple(n3, p, Node.createAnon()));
0714:                infgraph = createInfGraph(rules, data);
0715:
0716:                TestUtil.assertIteratorValues(this , infgraph.find(n1, s, null),
0717:                        new Triple[] {
0718:                                new Triple(n1, s, Node.createLiteral("literal",
0719:                                        "", null)),
0720:                                new Triple(n1, s, Node.createLiteral(
0721:                                        "notBNode", "", null)), });
0722:                TestUtil.assertIteratorValues(this , infgraph.find(n2, s, null),
0723:                        new Triple[] {
0724:                                new Triple(n2, s, Node.createLiteral(
0725:                                        "notLiteral", "", null)),
0726:                                new Triple(n2, s, Node.createLiteral(
0727:                                        "notBNode", "", null)), });
0728:                TestUtil.assertIteratorValues(this , infgraph.find(n3, s, null),
0729:                        new Triple[] {
0730:                                new Triple(n3, s, Node.createLiteral(
0731:                                        "notLiteral", "", null)),
0732:                                new Triple(n3, s, Node.createLiteral("bNode",
0733:                                        "", null)), });
0734:
0735:                // Data type checking
0736:                rules = "[r1: (?x p ?y), isDType(?y, rdfs:Literal) -> (?x s 'isLiteral')]"
0737:                        + "[r1: (?x p ?y), isDType(?y, http://www.w3.org/2001/XMLSchema#int) -> (?x s 'isXSDInt')]"
0738:                        + "[r1: (?x p ?y), isDType(?y, http://www.w3.org/2001/XMLSchema#string) -> (?x s 'isXSDString')]"
0739:                        + "[r1: (?x p ?y), notDType(?y, rdfs:Literal) -> (?x s 'notLiteral')]"
0740:                        + "[r1: (?x p ?y), notDType(?y, http://www.w3.org/2001/XMLSchema#int) -> (?x s 'notXSDInt')]"
0741:                        + "[r1: (?x p ?y), notDType(?y, http://www.w3.org/2001/XMLSchema#string) -> (?x s 'notXSDString')]"
0742:                        + "";
0743:                data = Factory.createGraphMem();
0744:                data.add(new Triple(n1, p, Util.makeIntNode(3)));
0745:                data
0746:                        .add(new Triple(n2, p, Node.createLiteral("foo", "",
0747:                                null)));
0748:                data.add(new Triple(n3, p, Node.createLiteral("foo", "",
0749:                        XSDDatatype.XSDstring)));
0750:                data.add(new Triple(n4, p, n4));
0751:                data.add(new Triple(n5, p, Node.createLiteral("-1", "",
0752:                        XSDDatatype.XSDnonNegativeInteger)));
0753:                infgraph = createInfGraph(rules, data);
0754:
0755:                TestUtil.assertIteratorValues(this , infgraph
0756:                        .find(null, s, null), new Triple[] {
0757:                        new Triple(n1, s, Node.createLiteral("isLiteral", "",
0758:                                null)),
0759:                        new Triple(n1, s, Node.createLiteral("isXSDInt", "",
0760:                                null)),
0761:                        new Triple(n1, s, Node.createLiteral("notXSDString",
0762:                                "", null)),
0763:
0764:                        new Triple(n2, s, Node.createLiteral("isLiteral", "",
0765:                                null)),
0766:                        new Triple(n2, s, Node.createLiteral("notXSDInt", "",
0767:                                null)),
0768:                        new Triple(n2, s, Node.createLiteral("isXSDString", "",
0769:                                null)),
0770:
0771:                        new Triple(n3, s, Node.createLiteral("isLiteral", "",
0772:                                null)),
0773:                        new Triple(n3, s, Node.createLiteral("notXSDInt", "",
0774:                                null)),
0775:                        new Triple(n3, s, Node.createLiteral("isXSDString", "",
0776:                                null)),
0777:
0778:                        new Triple(n4, s, Node.createLiteral("notLiteral", "",
0779:                                null)),
0780:                        new Triple(n4, s, Node.createLiteral("notXSDInt", "",
0781:                                null)),
0782:                        new Triple(n4, s, Node.createLiteral("notXSDString",
0783:                                "", null)),
0784:
0785:                        new Triple(n5, s, Node.createLiteral("notLiteral", "",
0786:                                null)),
0787:                        new Triple(n5, s, Node.createLiteral("notXSDInt", "",
0788:                                null)),
0789:                        new Triple(n5, s, Node.createLiteral("notXSDString",
0790:                                "", null)), });
0791:
0792:                // Literal counting
0793:                rules = "[r1: (?x p ?y), countLiteralValues(?x, p, ?c) -> (?x s ?c)]";
0794:                data = Factory.createGraphMem();
0795:                data.add(new Triple(n1, p, Util.makeIntNode(2)));
0796:                data.add(new Triple(n1, p, Util.makeIntNode(2)));
0797:                data.add(new Triple(n1, p, Util.makeIntNode(3)));
0798:                data.add(new Triple(n1, p, n2));
0799:                infgraph = createInfGraph(rules, data);
0800:                TestUtil
0801:                        .assertIteratorValues(this , infgraph.find(n1, s, null),
0802:                                new Triple[] { new Triple(n1, s, Util
0803:                                        .makeIntNode(2)), });
0804:
0805:                // Map list operation
0806:                rules = "[r1: (n1 p ?l) -> listMapAsSubject(?l, q, C1)]"
0807:                        + "[r2: (n1 p ?l) -> listMapAsObject ( a, q, ?l)]";
0808:                data = Factory.createGraphMem();
0809:                data.add(new Triple(n1, p, Util.makeList(
0810:                        new Node[] { b, c, d }, data)));
0811:                infgraph = createInfGraph(rules, data);
0812:                TestUtil.assertIteratorValues(this , infgraph
0813:                        .find(null, q, null), new Triple[] {
0814:                        new Triple(b, q, C1), new Triple(c, q, C1),
0815:                        new Triple(d, q, C1), new Triple(a, q, b),
0816:                        new Triple(a, q, c), new Triple(a, q, d), });
0817:            }
0818:
0819:            /**
0820:             * Check string manipulation builtins, new at 2.5.
0821:             */
0822:            public void testStringBuiltins() {
0823:                String rules = "[r1: (?x p ?y) strConcat(?y, rdf:type, 'foo', ?z) -> (?x q ?z) ] \n"
0824:                        + "[r1: (?x p ?y) strConcat(?z) -> (?x q ?z) ] \n"
0825:                        + "[r2: (?x p ?y) uriConcat('http://jena.hpl.hp.com/test#', ?y, ?z) -> (?x q ?z) ]";
0826:                Graph data = Factory.createGraphMem();
0827:                data.add(new Triple(n1, p, Node.createLiteral("test")));
0828:                InfGraph infgraph = createInfGraph(rules, data);
0829:
0830:                TestUtil
0831:                        .assertIteratorValues(
0832:                                this ,
0833:                                infgraph.find(null, q, null),
0834:                                new Triple[] {
0835:                                        new Triple(
0836:                                                n1,
0837:                                                q,
0838:                                                Node
0839:                                                        .createLiteral("testhttp://www.w3.org/1999/02/22-rdf-syntax-ns#typefoo")),
0840:                                        new Triple(n1, q, Node
0841:                                                .createLiteral("")),
0842:                                        new Triple(
0843:                                                n1,
0844:                                                q,
0845:                                                Node
0846:                                                        .createURI("http://jena.hpl.hp.com/test#test")), });
0847:
0848:                rules = "[r1: (?x p ?y) regex(?y, '(.*)\\\\s(.*) (f.*)') -> (?x q 'ok') ] \n"
0849:                        + "[r2: (?x p ?y) regex(?y, '(.*)\\\\s(.*) (f.*)', ?m1, ?m2, ?m3) -> (?x r ?m2) ] \n"
0850:                        + "";
0851:                data = Factory.createGraphMem();
0852:                data.add(new Triple(n1, p, Node.createLiteral("foo bar foo")));
0853:                data.add(new Triple(n2, p, Node.createLiteral("foo bar baz")));
0854:                infgraph = createInfGraph(rules, data);
0855:                TestUtil.assertIteratorValues(this , infgraph
0856:                        .find(null, q, null), new Triple[] { new Triple(n1, q,
0857:                        Node.createLiteral("ok")), });
0858:                TestUtil.assertIteratorValues(this , infgraph
0859:                        .find(null, r, null), new Triple[] { new Triple(n1, r,
0860:                        Node.createLiteral("bar")), });
0861:            }
0862:
0863:            /**
0864:             * More extensive check of arithmetic which checks that binding to an 
0865:             * expected answer also works
0866:             */
0867:            public void testArithmetic() {
0868:                doTestArithmetic("sum", 3, 5, 8);
0869:                doTestArithmetic("difference", 5, 3, 2);
0870:                doTestArithmetic("product", 3, 5, 15);
0871:                doTestArithmetic("quotient", 12, 3, 4);
0872:                doTestArithmetic("min", 3, 5, 3);
0873:                doTestArithmetic("max", 3, 5, 5);
0874:            }
0875:
0876:            /**
0877:             * Internals of testArithmetic which sets up a rule
0878:             * and executes it with expected and illegal answers.
0879:             */
0880:            private void doTestArithmetic(String op, int arg1, int arg2,
0881:                    int expected) {
0882:                String rules = "[r1: (?x p ?a), (?x q ?b), (?x r ?c) " + op
0883:                        + "(?a, ?b, ?c) -> (?x s ?c)]\n "
0884:                        + "[r2: (?x p ?a), (?x q ?b), (?x t ?c) " + op
0885:                        + "(?a, ?b, ?c) -> (?x u ?c)]";
0886:                Graph data = Factory.createGraphMem();
0887:                data.add(new Triple(n1, p, Util.makeIntNode(arg1)));
0888:                data.add(new Triple(n1, q, Util.makeIntNode(arg2)));
0889:                data.add(new Triple(n1, r, Util.makeIntNode(expected)));
0890:                data.add(new Triple(n1, t, Util.makeIntNode(expected + 1)));
0891:                InfGraph infgraph = createInfGraph(rules, data);
0892:                assertTrue(infgraph.contains(n1, s, Util.makeIntNode(expected)));
0893:                assertFalse(infgraph.contains(n1, u, Node.ANY));
0894:            }
0895:
0896:            /**
0897:             * Helper - returns the single object value for an s/p pair, asserts an error
0898:             * if there is more than one.
0899:             */
0900:            private Node getValue(Graph g, Node s, Node p) {
0901:                ExtendedIterator i = g.find(s, p, null);
0902:                assertTrue(i.hasNext());
0903:                Node result = ((Triple) i.next()).getObject();
0904:                if (i.hasNext()) {
0905:                    assertTrue("multiple values not expected", false);
0906:                    i.close();
0907:                }
0908:                return result;
0909:            }
0910:
0911:            /**
0912:             * Investigate a suspicious case in the OWL ruleset, is the backchainer 
0913:             * returning duplicate values?
0914:             */
0915:            public void testDuplicatesEC4() throws IOException {
0916:                boolean prior = JenaParameters.enableFilteringOfHiddenInfNodes;
0917:                try {
0918:                    JenaParameters.enableFilteringOfHiddenInfNodes = false;
0919:                    Model premisesM = FileManager.get().loadModel(
0920:                            "file:testing/wg/equivalentClass/premises004.rdf");
0921:                    Graph data = premisesM.getGraph();
0922:                    Reasoner reasoner = new OWLFBRuleReasoner(
0923:                            OWLFBRuleReasonerFactory.theInstance());
0924:                    InfGraph infgraph = reasoner.bind(data);
0925:                    Node rbPrototypeProp = Node
0926:                            .createURI(ReasonerVocabulary.RBNamespace
0927:                                    + "prototype");
0928:                    int count = 0;
0929:                    for (Iterator i = infgraph
0930:                            .find(null, rbPrototypeProp, null); i.hasNext();) {
0931:                        Object t = i.next();
0932:                        //                System.out.println(" - " + PrintUtil.print(t));
0933:                        count++;
0934:                    }
0935:                    //            listFBGraph("direct databind case", (FBRuleInfGraph)infgraph);
0936:                    assertEquals(5, count);
0937:
0938:                    infgraph = reasoner.bindSchema(data).bind(
0939:                            Factory.createGraphMem());
0940:                    count = 0;
0941:                    for (Iterator i = infgraph
0942:                            .find(null, rbPrototypeProp, null); i.hasNext();) {
0943:                        Object t = i.next();
0944:                        //                System.out.println(" - " + PrintUtil.print(t));
0945:                        count++;
0946:                    }
0947:                    //            listFBGraph("bindSchema case", (FBRuleInfGraph)infgraph);
0948:                    assertEquals(5, count);
0949:                } finally {
0950:                    JenaParameters.enableFilteringOfHiddenInfNodes = prior;
0951:                }
0952:            }
0953:
0954:            /**
0955:             * Check cost of creating an empty OWL closure.
0956:             */
0957:            public void temp() {
0958:                Graph data = Factory.createGraphMem();
0959:                Graph data2 = Factory.createGraphMem();
0960:                Reasoner reasoner = new OWLFBRuleReasoner(
0961:                        OWLFBRuleReasonerFactory.theInstance());
0962:                FBRuleInfGraph infgraph = (FBRuleInfGraph) reasoner.bind(data);
0963:                FBRuleInfGraph infgraph2 = (FBRuleInfGraph) reasoner
0964:                        .bind(data2);
0965:                long t1 = System.currentTimeMillis();
0966:                infgraph.prepare();
0967:                long t2 = System.currentTimeMillis();
0968:                System.out.println("Prepare on empty graph = " + (t2 - t1)
0969:                        + "ms");
0970:                t1 = System.currentTimeMillis();
0971:                infgraph2.prepare();
0972:                t2 = System.currentTimeMillis();
0973:                System.out.println("Prepare on empty graph = " + (t2 - t1)
0974:                        + "ms");
0975:            }
0976:
0977:            /**
0978:             * Helper function to list a graph out to logger.info
0979:             */
0980:            public void listGraph(Graph g) {
0981:                for (Iterator i = g.find(null, null, null); i.hasNext();) {
0982:                    Triple t = (Triple) i.next();
0983:                    logger.info(PrintUtil.print(t));
0984:                }
0985:                logger.info("  --------  ");
0986:            }
0987:
0988:            /**
0989:             * Helper function to list the interesting parts of an FBInfGraph.
0990:             */
0991:            public void listFBGraph(String message, FBRuleInfGraph graph) {
0992:                logger.info(message);
0993:                logger.info("Raw graph data");
0994:                listGraph(graph.getRawGraph());
0995:                logger.info("Static deductions");
0996:                listGraph(graph.getDeductionsGraph());
0997:            }
0998:
0999:        }
1000:
1001:        /*
1002:         (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
1003:         All rights reserved.
1004:
1005:         Redistribution and use in source and binary forms, with or without
1006:         modification, are permitted provided that the following conditions
1007:         are met:
1008:
1009:         1. Redistributions of source code must retain the above copyright
1010:         notice, this list of conditions and the following disclaimer.
1011:
1012:         2. Redistributions in binary form must reproduce the above copyright
1013:         notice, this list of conditions and the following disclaimer in the
1014:         documentation and/or other materials provided with the distribution.
1015:
1016:         3. The name of the author may not be used to endorse or promote products
1017:         derived from this software without specific prior written permission.
1018:
1019:         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1020:         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1021:         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1022:         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1023:         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1024:         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1025:         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1026:         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1027:         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1028:         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1029:         */
ww_w___._ja_v__a_2__s___._c__o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.