001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestReasonerFactoryAssembler.java,v 1.7 2008/01/02 12:05:57 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.test;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.assembler.*;
012: import com.hp.hpl.jena.assembler.assemblers.*;
013: import com.hp.hpl.jena.assembler.exceptions.*;
014: import com.hp.hpl.jena.rdf.model.*;
015: import com.hp.hpl.jena.reasoner.*;
016: import com.hp.hpl.jena.reasoner.dig.*;
017: import com.hp.hpl.jena.reasoner.rulesys.*;
018: import com.hp.hpl.jena.reasoner.transitiveReasoner.*;
019:
020: public class TestReasonerFactoryAssembler extends AssemblerTestBase {
021: private final Assembler ASSEMBLER = new ReasonerFactoryAssembler();
022:
023: public TestReasonerFactoryAssembler(String name) {
024: super (name);
025: }
026:
027: protected Class getAssemblerClass() {
028: return ReasonerFactoryAssembler.class;
029: }
030:
031: public void testReasonerFactoryAssemblerType() {
032: testDemandsMinimalType(new ReasonerFactoryAssembler(),
033: JA.ReasonerFactory);
034: }
035:
036: public void testCreateReasonerFactory() {
037: Resource root = resourceInModel("x rdf:type ja:ReasonerFactory");
038: assertInstanceOf(GenericRuleReasonerFactory.class, ASSEMBLER
039: .open(root));
040: }
041:
042: public void testStandardReasonerURLs() {
043: testReasonerURL(GenericRuleReasonerFactory.class,
044: GenericRuleReasonerFactory.URI);
045: testReasonerURL(TransitiveReasonerFactory.class,
046: TransitiveReasonerFactory.URI);
047: testReasonerURL(RDFSRuleReasonerFactory.class,
048: RDFSRuleReasonerFactory.URI);
049: testReasonerURL(OWLFBRuleReasonerFactory.class,
050: OWLFBRuleReasonerFactory.URI);
051: testReasonerURL(DAMLMicroReasonerFactory.class,
052: DAMLMicroReasonerFactory.URI);
053: testReasonerURL(DIGReasonerFactory.class,
054: DIGReasonerFactory.URI);
055: testReasonerURL(OWLMicroReasonerFactory.class,
056: OWLMicroReasonerFactory.URI);
057: testReasonerURL(OWLMiniReasonerFactory.class,
058: OWLMiniReasonerFactory.URI);
059: }
060:
061: public void testBadReasonerURLFails() {
062: Resource root = resourceInModel("x rdf:type ja:ReasonerFactory; x ja:reasonerURL bad:URL");
063: try {
064: ASSEMBLER.open(root);
065: fail("should detected unknown reasoner");
066: } catch (UnknownReasonerException e) {
067: assertEquals(resource("bad:URL"), e.getURL());
068: }
069: }
070:
071: public static class MockBase implements ReasonerFactory {
072: public Reasoner create(Resource configuration) {
073: return null;
074: }
075:
076: public Model getCapabilities() {
077: return null;
078: }
079:
080: public String getURI() {
081: return null;
082: }
083: }
084:
085: public static class MockFactory extends MockBase implements
086: ReasonerFactory {
087: public static final MockFactory instance = new MockFactory();
088:
089: public static ReasonerFactory theInstance() {
090: return instance;
091: }
092: }
093:
094: public void testReasonerClassThrowsIfClassNotFound() {
095: String description = "x rdf:type ja:ReasonerFactory; x ja:reasonerClass java:noSuchClass";
096: Resource root = resourceInModel(description);
097: try {
098: ASSEMBLER.open(root);
099: fail("should trap missing class noSuchClass");
100: } catch (CannotLoadClassException e) {
101: assertEquals("noSuchClass", e.getClassName());
102: }
103: }
104:
105: public void testReasonerClassThrowsIfClassNotFactory() {
106: String description = "x rdf:type ja:ReasonerFactory; x ja:reasonerClass java:java.util.ArrayList";
107: Resource root = resourceInModel(description);
108: try {
109: ASSEMBLER.open(root);
110: fail("should trap non-ReasonerFactory ArrayList");
111: } catch (NotExpectedTypeException e) {
112: assertEquals(root, e.getRoot());
113: assertEquals(ReasonerFactory.class, e.getExpectedType());
114: assertEquals(ArrayList.class, e.getActualType());
115: }
116: }
117:
118: public void testReasonerClassUsesTheInstance() {
119: String description = "x rdf:type ja:ReasonerFactory; x ja:reasonerClass java:";
120: String MockName = MockFactory.class.getName();
121: Resource root = resourceInModel(description + MockName);
122: assertEquals(MockFactory.instance, ASSEMBLER.open(root));
123: }
124:
125: public void testReasonerClassInstantiatesIfNoInstance() {
126: String description = "x rdf:type ja:ReasonerFactory; x ja:reasonerClass java:";
127: String MockName = MockBase.class.getName();
128: Resource root = resourceInModel(description + MockName);
129: assertInstanceOf(MockBase.class, ASSEMBLER.open(root));
130: assertNotSame(MockFactory.instance, ASSEMBLER.open(root));
131: }
132:
133: public void testMultipleURLsFails() {
134: Resource root = resourceInModel("x rdf:type ja:ReasonerFactory; x ja:reasonerURL bad:URL; x ja:reasonerURL another:bad/URL");
135: try {
136: ASSEMBLER.open(root);
137: fail("should detected multiple reasoner URLs");
138: } catch (NotUniqueException e) {
139: assertEquals(JA.reasonerURL, e.getProperty());
140: assertEquals(root, e.getRoot());
141: }
142: }
143:
144: public void testOnlyGenericReasonerCanHaveRules() {
145: String url = TransitiveReasonerFactory.URI;
146: Resource root = resourceInModel("x rdf:type ja:ReasonerFactory; x ja:rule '[->(a\\sP\\sb)]'; x ja:reasonerURL "
147: + url);
148: String ruleStringA = "[rdfs2: (?x ?p ?y), (?p rdfs:domain ?c) -> (?x rdf:type ?c)]";
149: final RuleSet rules = RuleSet.create(ruleStringA);
150: try {
151: ASSEMBLER.open(new FixedObjectAssembler(rules), root);
152: fail("only GenericRuleReasoners can have attached rules");
153: } catch (CannotHaveRulesException e) {
154: }
155: }
156:
157: public void testSchema() {
158: }
159:
160: public void testSingleRules() {
161: Resource root = resourceInModel("x rdf:type ja:ReasonerFactory; x ja:rules S");
162: String ruleStringA = "[rdfs2: (?x ?p ?y), (?p rdfs:domain ?c) -> (?x rdf:type ?c)]";
163: final RuleSet rules = RuleSet.create(ruleStringA);
164: Assembler mock = new AssemblerBase() {
165: public Object open(Assembler a, Resource root,
166: Mode irrelevant) {
167: assertEquals(root, resource("S"));
168: return rules;
169: }
170: };
171: ReasonerFactory r = (ReasonerFactory) ASSEMBLER
172: .open(mock, root);
173: GenericRuleReasoner grr = (GenericRuleReasoner) r.create(null);
174: assertEquals(new HashSet(rules.getRules()), new HashSet(grr
175: .getRules()));
176: }
177:
178: public void testMultipleRules() {
179: Resource root = resourceInModel("x rdf:type ja:ReasonerFactory; x ja:rules S; x ja:rules T");
180: String ruleStringA = "[rdfs2: (?x ?p ?y), (?p rdfs:domain ?c) -> (?x rdf:type ?c)]";
181: String ruleStringB = "[rdfs9: (?x rdfs:subClassOf ?y), (?a rdf:type ?x) -> (?a rdf:type ?y)]";
182: final RuleSet rulesA = RuleSet.create(ruleStringA);
183: final RuleSet rulesB = RuleSet.create(ruleStringB);
184: Assembler mock = new AssemblerBase() {
185: public Object open(Assembler a, Resource root,
186: Mode irrelevant) {
187: if (root.equals(resource("S")))
188: return rulesA;
189: if (root.equals(resource("T")))
190: return rulesB;
191: throw new RuntimeException("unknown resource in mock: "
192: + root);
193: }
194: };
195: ReasonerFactory r = (ReasonerFactory) ASSEMBLER
196: .open(mock, root);
197: GenericRuleReasoner grr = (GenericRuleReasoner) r.create(null);
198: HashSet wanted = new HashSet();
199: wanted.addAll(rulesA.getRules());
200: wanted.addAll(rulesB.getRules());
201: assertEquals(wanted, new HashSet(grr.getRules()));
202: }
203:
204: protected void testReasonerURL(Class wanted, String string) {
205: Resource root = resourceInModel("x rdf:type ja:ReasonerFactory; x ja:reasonerURL "
206: + string);
207: ReasonerFactory rf = (ReasonerFactory) ASSEMBLER.open(root);
208: assertInstanceOf(wanted, rf);
209: }
210: }
211:
212: /*
213: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
214: * All rights reserved.
215: *
216: * Redistribution and use in source and binary forms, with or without
217: * modification, are permitted provided that the following conditions
218: * are met:
219: * 1. Redistributions of source code must retain the above copyright
220: * notice, this list of conditions and the following disclaimer.
221: * 2. Redistributions in binary form must reproduce the above copyright
222: * notice, this list of conditions and the following disclaimer in the
223: * documentation and/or other materials provided with the distribution.
224: * 3. The name of the author may not be used to endorse or promote products
225: * derived from this software without specific prior written permission.
226: *
227: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
228: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
229: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
230: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
231: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
232: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
234: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
235: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
236: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237: */
|