001: package org.drools.jsr94.rules;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.IOException;
020: import java.io.InputStreamReader;
021: import java.io.Reader;
022: import java.rmi.RemoteException;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Map;
027:
028: import javax.rules.ConfigurationException;
029: import javax.rules.ObjectFilter;
030: import javax.rules.RuleServiceProvider;
031: import javax.rules.RuleServiceProviderManager;
032: import javax.rules.StatelessRuleSession;
033: import javax.rules.admin.LocalRuleExecutionSetProvider;
034: import javax.rules.admin.RuleExecutionSet;
035: import javax.rules.admin.RuleExecutionSetCreateException;
036: import javax.rules.admin.RuleExecutionSetRegisterException;
037:
038: import junit.framework.TestCase;
039:
040: /**
041: * Test the <code>StatelessRuleSession</code> implementation.
042: *
043: * @author N. Alex Rupp (n_alex <at>codehaus.org)
044: * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a>
045: * @author <a href="mailto:michael.frandsen@syngenio.de">Michael Frandsen </a>
046: * @see StatelessRuleSession
047: */
048: public class StatelessRuleSessionTest extends TestCase {
049:
050: private ExampleRuleEngineFacade sessionBuilder;
051:
052: private final String bindUri = "sisters.drl";
053: private final String bindUri_drl = "sisters_expander.drl";
054: private final String bindUri_dsl = "sisters_expander.dsl";
055: private final String bindUri_xml = "sisters.xml";
056: private final String bindUri_globals = "sisters_globals.drl";
057:
058: /**
059: * Setup the test case.
060: * normal drl, drl with dsl, drl with global
061: */
062: protected void setUp() throws Exception {
063: super .setUp();
064: this .sessionBuilder = new ExampleRuleEngineFacade();
065: this .sessionBuilder.addRuleExecutionSet(this .bindUri,
066: StatelessRuleSessionTest.class
067: .getResourceAsStream(this .bindUri));
068:
069: final Map map = new HashMap();
070: final Reader reader = new InputStreamReader(
071: StatelessRuleSessionTest.class
072: .getResourceAsStream(this .bindUri_dsl));
073:
074: map.put("dsl", this .getDSLText(reader).toString());
075: this .sessionBuilder.addRuleExecutionSet(this .bindUri_drl,
076: StatelessRuleSessionTest.class
077: .getResourceAsStream(this .bindUri_drl), map);
078:
079: final Map map_xml = new HashMap();
080: map_xml.put("source", "xml");
081: this .sessionBuilder
082: .addRuleExecutionSet(this .bindUri_xml,
083: StatelessRuleSessionTest.class
084: .getResourceAsStream(this .bindUri_xml),
085: map_xml);
086:
087: this .sessionBuilder.addRuleExecutionSet(this .bindUri_globals,
088: StatelessRuleSessionTest.class
089: .getResourceAsStream(this .bindUri_globals));
090:
091: }
092:
093: /*
094: * Taken from DRLParser
095: */
096: private StringBuffer getDSLText(final Reader reader)
097: throws IOException {
098: final StringBuffer text = new StringBuffer();
099:
100: final char[] buf = new char[1024];
101: int len = 0;
102:
103: while ((len = reader.read(buf)) >= 0) {
104: text.append(buf, 0, len);
105: }
106: return text;
107: }
108:
109: public void testCreateRuleExecutionSetFromStreamWithXml() {
110:
111: try {
112: final Map map = new HashMap();
113: map.put("source", "xml");
114:
115: RuleServiceProvider ruleServiceProvider;
116: RuleServiceProviderManager
117: .registerRuleServiceProvider("http://drools.org/",
118: RuleServiceProviderImpl.class);
119:
120: ruleServiceProvider = RuleServiceProviderManager
121: .getRuleServiceProvider("http://drools.org/");
122:
123: LocalRuleExecutionSetProvider ruleSetProvider = ruleServiceProvider
124: .getRuleAdministrator()
125: .getLocalRuleExecutionSetProvider(null);
126: final RuleExecutionSet ruleExecutionSet = ruleSetProvider
127: .createRuleExecutionSet(
128: StatelessRuleSessionTest.class
129: .getResourceAsStream(this .bindUri_xml),
130: map);
131: assertNotNull(ruleExecutionSet);
132: } catch (RemoteException e) {
133: fail();
134: } catch (ConfigurationException e) {
135: fail();
136: } catch (RuleExecutionSetCreateException e) {
137: fail();
138: } catch (IOException e) {
139: fail();
140: }
141: }
142:
143: public void testCreateRuleExecutionSetFromStreamReaderWithXml() {
144: try {
145: final Map map = new HashMap();
146: map.put("source", "xml");
147:
148: RuleServiceProvider ruleServiceProvider;
149: RuleServiceProviderManager
150: .registerRuleServiceProvider("http://drools.org/",
151: RuleServiceProviderImpl.class);
152:
153: ruleServiceProvider = RuleServiceProviderManager
154: .getRuleServiceProvider("http://drools.org/");
155:
156: LocalRuleExecutionSetProvider ruleSetProvider = ruleServiceProvider
157: .getRuleAdministrator()
158: .getLocalRuleExecutionSetProvider(null);
159: final Reader ruleReader = new InputStreamReader(
160: StatelessRuleSessionTest.class
161: .getResourceAsStream(this .bindUri_xml));
162: final RuleExecutionSet ruleExecutionSet = ruleSetProvider
163: .createRuleExecutionSet(ruleReader, map);
164: assertNotNull(ruleExecutionSet);
165:
166: } catch (RemoteException e) {
167: fail();
168: } catch (ConfigurationException e) {
169: fail();
170: } catch (RuleExecutionSetCreateException e) {
171: fail();
172: } catch (IOException e) {
173: fail();
174: }
175: }
176:
177: /**
178: * Test executeRules with globals.
179: */
180: public void testExecuteRulesGlobals() throws Exception {
181: final java.util.Map map = new HashMap();
182: java.util.Vector v = new java.util.Vector();
183: map.put("vector", v);
184: final StatelessRuleSession statelessSession = this .sessionBuilder
185: .getStatelessRuleSession(this .bindUri_globals, map);
186:
187: final List inObjects = new ArrayList();
188:
189: final Person bob = new Person("bob");
190: inObjects.add(bob);
191:
192: final Person jeannie = new Person("jeannie");
193: jeannie.addSister("rebecca");
194: inObjects.add(jeannie);
195:
196: final Person rebecca = new Person("rebecca");
197: rebecca.addSister("jeannie");
198: inObjects.add(rebecca);
199:
200: // execute the rules
201: final List outList = statelessSession.executeRules(inObjects);
202:
203: assertEquals("incorrect size", 5, outList.size());
204:
205: assertContains(outList, bob);
206:
207: assertContains(outList, rebecca);
208:
209: assertContains(outList, jeannie);
210:
211: assertContains(outList, "rebecca and jeannie are sisters");
212:
213: assertContains(outList, "jeannie and rebecca are sisters");
214:
215: v = (java.util.Vector) map.get("vector");
216:
217: assertNotNull("Global Vector null", v);
218:
219: assertContains(v, "rebecca and jeannie are sisters");
220:
221: assertContains(v, "jeannie and rebecca are sisters");
222:
223: assertEquals("Vector v incorrect size", 2, v.size());
224:
225: statelessSession.release();
226: }
227:
228: /**
229: * Test executeRules with normal drl.
230: */
231: public void testExecuteRules() throws Exception {
232: final StatelessRuleSession statelessSession = this .sessionBuilder
233: .getStatelessRuleSession(this .bindUri);
234:
235: final List inObjects = new ArrayList();
236:
237: final Person bob = new Person("bob");
238: inObjects.add(bob);
239:
240: final Person jeannie = new Person("jeannie");
241: jeannie.addSister("rebecca");
242: inObjects.add(jeannie);
243:
244: final Person rebecca = new Person("rebecca");
245: rebecca.addSister("jeannie");
246: inObjects.add(rebecca);
247:
248: // execute the rules
249: final List outList = statelessSession.executeRules(inObjects);
250:
251: assertEquals("incorrect size", 5, outList.size());
252:
253: assertContains(outList, bob);
254:
255: assertContains(outList, rebecca);
256:
257: assertContains(outList, jeannie);
258:
259: assertContains(outList, "rebecca and jeannie are sisters");
260:
261: assertContains(outList, "jeannie and rebecca are sisters");
262:
263: statelessSession.release();
264: }
265:
266: /**
267: * Test executeRules with normal drl.
268: */
269: public void testExecuteRulesWithXml() throws Exception {
270: final StatelessRuleSession statelessSession = this .sessionBuilder
271: .getStatelessRuleSession(this .bindUri_xml);
272:
273: final List inObjects = new ArrayList();
274:
275: final Person bob = new Person("bob");
276: inObjects.add(bob);
277:
278: final Person jeannie = new Person("jeannie");
279: jeannie.addSister("rebecca");
280: inObjects.add(jeannie);
281:
282: final Person rebecca = new Person("rebecca");
283: rebecca.addSister("jeannie");
284: inObjects.add(rebecca);
285:
286: // execute the rules
287: final List outList = statelessSession.executeRules(inObjects);
288:
289: assertEquals("incorrect size", 5, outList.size());
290:
291: assertContains(outList, bob);
292:
293: assertContains(outList, rebecca);
294:
295: assertContains(outList, jeannie);
296:
297: assertContains(outList, "rebecca and jeannie are sisters");
298:
299: assertContains(outList, "jeannie and rebecca are sisters");
300:
301: statelessSession.release();
302: }
303:
304: /**
305: * Test executeRules drl with dsl.
306: */
307: public void xxxtestExecuteRules_dsl() throws Exception {
308: // @FIXME
309: final StatelessRuleSession statelessSession = this .sessionBuilder
310: .getStatelessRuleSession(this .bindUri_drl);
311:
312: final List inObjects = new ArrayList();
313:
314: final Person bob = new Person("bob");
315: inObjects.add(bob);
316:
317: final Person jeannie = new Person("jeannie");
318: jeannie.addSister("rebecca");
319: inObjects.add(jeannie);
320:
321: final Person rebecca = new Person("rebecca");
322: rebecca.addSister("jeannie");
323: inObjects.add(rebecca);
324:
325: // execute the rules
326: final List outList = statelessSession.executeRules(inObjects);
327:
328: assertEquals("incorrect size", 5, outList.size());
329:
330: assertContains(outList, bob);
331:
332: assertContains(outList, rebecca);
333:
334: assertContains(outList, jeannie);
335:
336: assertContains(outList, "rebecca and jeannie are sisters");
337:
338: assertContains(outList, "jeannie and rebecca are sisters");
339:
340: statelessSession.release();
341: }
342:
343: /**
344: * Test executeRules with ObjectFilter.
345: */
346: public void testExecuteRulesWithFilter() throws Exception {
347: final StatelessRuleSession statelessSession = this .sessionBuilder
348: .getStatelessRuleSession(this .bindUri);
349:
350: final List inObjects = new ArrayList();
351:
352: final Person bob = new Person("bob");
353: inObjects.add(bob);
354:
355: final Person rebecca = new Person("rebecca");
356: rebecca.addSister("jeannie");
357: inObjects.add(rebecca);
358:
359: final Person jeannie = new Person("jeannie");
360: jeannie.addSister("rebecca");
361: inObjects.add(jeannie);
362:
363: // execute the rules
364: final List outList = statelessSession.executeRules(inObjects,
365: new PersonFilter());
366: assertEquals("incorrect size", 3, outList.size());
367:
368: assertTrue("where is bob", outList.contains(bob));
369:
370: assertTrue("where is rebecca", outList.contains(rebecca));
371:
372: assertTrue("where is jeannie", outList.contains(jeannie));
373: }
374:
375: /**
376: * Test executeRules with ObjectFilter drl with dsl.
377: */
378: public void testExecuteRulesWithFilter_dsl() throws Exception {
379: final StatelessRuleSession statelessSession = this .sessionBuilder
380: .getStatelessRuleSession(this .bindUri_drl);
381:
382: final List inObjects = new ArrayList();
383:
384: final Person bob = new Person("bob");
385: inObjects.add(bob);
386:
387: final Person rebecca = new Person("rebecca");
388: rebecca.addSister("jeannie");
389: inObjects.add(rebecca);
390:
391: final Person jeannie = new Person("jeannie");
392: jeannie.addSister("rebecca");
393: inObjects.add(jeannie);
394:
395: // execute the rules
396: final List outList = statelessSession.executeRules(inObjects,
397: new PersonFilter());
398: assertEquals("incorrect size", 3, outList.size());
399:
400: assertTrue("where is bob", outList.contains(bob));
401:
402: assertTrue("where is rebecca", outList.contains(rebecca));
403:
404: assertTrue("where is jeannie", outList.contains(jeannie));
405: }
406:
407: /**
408: * Filter accepts only objects of type Person.
409: */
410: static class PersonFilter implements ObjectFilter {
411: public Object filter(final Object object) {
412: return (object instanceof Person ? object : null);
413: }
414:
415: public void reset() {
416: // nothing to reset
417: }
418: }
419:
420: protected void assertContains(final List expected,
421: final Object object) {
422: if (expected.contains(object)) {
423: return;
424: }
425:
426: fail(object + " not in " + expected);
427: }
428: }
|