001: package org.drools.xml;
002:
003: import java.io.InputStreamReader;
004: import java.util.List;
005:
006: import junit.framework.TestCase;
007:
008: import org.drools.RuleBase;
009: import org.drools.RuleBaseConfiguration;
010: import org.drools.RuleBaseFactory;
011: import org.drools.lang.descr.AccessorDescr;
012: import org.drools.lang.descr.AccumulateDescr;
013: import org.drools.lang.descr.AndDescr;
014: import org.drools.lang.descr.AttributeDescr;
015: import org.drools.lang.descr.CollectDescr;
016: import org.drools.lang.descr.EvalDescr;
017: import org.drools.lang.descr.ExistsDescr;
018: import org.drools.lang.descr.FieldBindingDescr;
019: import org.drools.lang.descr.FieldConstraintDescr;
020: import org.drools.lang.descr.ForallDescr;
021: import org.drools.lang.descr.FromDescr;
022: import org.drools.lang.descr.FunctionDescr;
023: import org.drools.lang.descr.GlobalDescr;
024: import org.drools.lang.descr.ImportDescr;
025: import org.drools.lang.descr.LiteralRestrictionDescr;
026: import org.drools.lang.descr.NotDescr;
027: import org.drools.lang.descr.OrDescr;
028: import org.drools.lang.descr.PackageDescr;
029: import org.drools.lang.descr.PatternDescr;
030: import org.drools.lang.descr.PredicateDescr;
031: import org.drools.lang.descr.QueryDescr;
032: import org.drools.lang.descr.ReturnValueRestrictionDescr;
033: import org.drools.lang.descr.RuleDescr;
034: import org.drools.lang.descr.VariableRestrictionDescr;
035:
036: public class XmlPackageReaderTest extends TestCase {
037:
038: protected RuleBase getRuleBase() throws Exception {
039:
040: return RuleBaseFactory.newRuleBase(RuleBase.RETEOO, null);
041: }
042:
043: protected RuleBase getRuleBase(final RuleBaseConfiguration config)
044: throws Exception {
045:
046: return RuleBaseFactory.newRuleBase(RuleBase.RETEOO, config);
047: }
048:
049: public void testParseFrom() throws Exception {
050: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
051: xmlPackageReader.read(new InputStreamReader(getClass()
052: .getResourceAsStream("test_ParseFrom.xml")));
053: final PackageDescr packageDescr = xmlPackageReader
054: .getPackageDescr();
055: assertNotNull(packageDescr);
056: RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
057: PatternDescr patterndescr = (PatternDescr) obj.getLhs()
058: .getDescrs().get(0);
059:
060: FromDescr from = (FromDescr) patterndescr.getSource();
061:
062: AccessorDescr accessordescriptor = (AccessorDescr) from
063: .getDataSource();
064: assertEquals(accessordescriptor.getVariableName(), "cheesery");
065:
066: assertEquals(patterndescr.getObjectType(), "Cheese");
067: assertEquals(patterndescr.getIdentifier(), "cheese");
068:
069: }
070:
071: public void testAccumulate() throws Exception {
072: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
073: xmlPackageReader.read(new InputStreamReader(getClass()
074: .getResourceAsStream("test_ParseAccumulate.xml")));
075: final PackageDescr packageDescr = xmlPackageReader
076: .getPackageDescr();
077: assertNotNull(packageDescr);
078: RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
079:
080: Object patternobj = obj.getLhs().getDescrs().get(0);
081: assertTrue(patternobj instanceof PatternDescr);
082: final PatternDescr patterncheese = (PatternDescr) patternobj;
083: assertEquals(patterncheese.getIdentifier(), "cheese");
084: assertEquals(patterncheese.getObjectType(), "Cheese");
085:
086: AccumulateDescr accumulatedescr = (AccumulateDescr) patterncheese
087: .getSource();
088: assertEquals("total += $cheese.getPrice();", accumulatedescr
089: .getActionCode());
090: assertEquals("int total = 0;", accumulatedescr.getInitCode());
091: assertEquals("new Integer( total ) );", accumulatedescr
092: .getResultCode());
093:
094: patternobj = obj.getLhs().getDescrs().get(1);
095: assertTrue(patternobj instanceof PatternDescr);
096:
097: final PatternDescr patternmax = (PatternDescr) patternobj;
098: assertEquals(patternmax.getIdentifier(), "max");
099: assertEquals(patternmax.getObjectType(), "Number");
100:
101: accumulatedescr = (AccumulateDescr) patternmax.getSource();
102:
103: assertTrue(accumulatedescr.isExternalFunction());
104:
105: assertEquals("max", accumulatedescr.getFunctionIdentifier());
106:
107: assertNull(accumulatedescr.getInitCode());
108: assertNull(accumulatedescr.getActionCode());
109: assertNull(accumulatedescr.getResultCode());
110: assertNull(accumulatedescr.getReverseCode());
111:
112: }
113:
114: public void testParseForall() throws Exception {
115: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
116: xmlPackageReader.read(new InputStreamReader(getClass()
117: .getResourceAsStream("test_ParseForall.xml")));
118: final PackageDescr packageDescr = xmlPackageReader
119: .getPackageDescr();
120: assertNotNull(packageDescr);
121:
122: RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
123: ForallDescr forall = (ForallDescr) obj.getLhs().getDescrs()
124: .get(0);
125: List forallPaterns = forall.getDescrs();
126:
127: PatternDescr pattarnState = (PatternDescr) forallPaterns.get(0);
128: PatternDescr personState = (PatternDescr) forallPaterns.get(1);
129: PatternDescr cheeseState = (PatternDescr) forallPaterns.get(2);
130:
131: assertEquals(pattarnState.getObjectType(), "State");
132: assertEquals(personState.getObjectType(), "Person");
133: assertEquals(cheeseState.getObjectType(), "Cheese");
134: }
135:
136: public void testParseExists() throws Exception {
137: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
138: xmlPackageReader.read(new InputStreamReader(getClass()
139: .getResourceAsStream("test_ParseExists.xml")));
140: final PackageDescr packageDescr = xmlPackageReader
141: .getPackageDescr();
142: assertNotNull(packageDescr);
143:
144: RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
145: Object existdescr = obj.getLhs().getDescrs().get(0);
146: assertTrue(existdescr instanceof ExistsDescr);
147:
148: Object patternDescriptor = ((ExistsDescr) existdescr)
149: .getDescrs().get(0);
150: assertTrue(patternDescriptor instanceof PatternDescr);
151: assertEquals(
152: ((PatternDescr) patternDescriptor).getObjectType(),
153: "Person");
154:
155: Object notDescr = obj.getLhs().getDescrs().get(1);
156:
157: assertEquals(notDescr.getClass().getName(), NotDescr.class
158: .getName());
159: existdescr = ((NotDescr) notDescr).getDescrs().get(0);
160: patternDescriptor = ((ExistsDescr) existdescr).getDescrs().get(
161: 0);
162: assertTrue(patternDescriptor instanceof PatternDescr);
163: assertEquals(
164: ((PatternDescr) patternDescriptor).getObjectType(),
165: "Cheese");
166: }
167:
168: public void testParseCollect() throws Exception {
169: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
170: xmlPackageReader.read(new InputStreamReader(getClass()
171: .getResourceAsStream("test_ParseCollect.xml")));
172: final PackageDescr packageDescr = xmlPackageReader
173: .getPackageDescr();
174:
175: assertNotNull(packageDescr);
176:
177: RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
178: Object objectpattern = obj.getLhs().getDescrs().get(0);
179: assertTrue(objectpattern instanceof PatternDescr);
180:
181: PatternDescr patterndescr = (PatternDescr) objectpattern;
182:
183: assertEquals(patterndescr.getObjectType(), "Cheese");
184:
185: Object collectobj = patterndescr.getSource();
186:
187: assertTrue(collectobj instanceof CollectDescr);
188:
189: CollectDescr collectDescr = (CollectDescr) collectobj;
190:
191: PatternDescr inputpattern = collectDescr.getInputPattern();
192:
193: assertEquals(inputpattern.getObjectType(), "Person");
194: Object fieldContraintObject = inputpattern.getConstraint()
195: .getDescrs().get(0);
196: assertTrue(fieldContraintObject instanceof FieldConstraintDescr);
197: FieldConstraintDescr fieldconstraintdescr = (FieldConstraintDescr) fieldContraintObject;
198: assertEquals(fieldconstraintdescr.getFieldName(), "hair");
199: Object literal1 = fieldconstraintdescr.getRestrictions().get(0);
200: assertTrue(literal1 instanceof LiteralRestrictionDescr);
201: LiteralRestrictionDescr literalDesc = (LiteralRestrictionDescr) literal1;
202: assertEquals(literalDesc.getEvaluator(), "==");
203: assertEquals(literalDesc.getText(), "pink");
204:
205: fieldContraintObject = patterndescr.getConstraint().getDescrs()
206: .get(0);
207: assertTrue(fieldContraintObject instanceof FieldConstraintDescr);
208: fieldconstraintdescr = (FieldConstraintDescr) fieldContraintObject;
209: assertEquals(fieldconstraintdescr.getFieldName(), "type");
210: literal1 = fieldconstraintdescr.getRestrictions().get(0);
211: assertTrue(literal1 instanceof LiteralRestrictionDescr);
212: literalDesc = (LiteralRestrictionDescr) literal1;
213: assertEquals(literalDesc.getEvaluator(), "==");
214: assertEquals(literalDesc.getText(), "1");
215:
216: }
217:
218: public void testParsePackageName() throws Exception {
219: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
220: xmlPackageReader.read(new InputStreamReader(getClass()
221: .getResourceAsStream("test_ParsePackageName.xml")));
222: final PackageDescr packageDescr = xmlPackageReader
223: .getPackageDescr();
224: assertNotNull(packageDescr);
225: assertEquals("com.sample", packageDescr.getName());
226: }
227:
228: public void testParseImport() throws Exception {
229: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
230: xmlPackageReader.read(new InputStreamReader(getClass()
231: .getResourceAsStream("test_ParseImport.xml")));
232: final PackageDescr packageDescr = xmlPackageReader
233: .getPackageDescr();
234: assertNotNull(packageDescr);
235: assertEquals("com.sample", packageDescr.getName());
236:
237: final List imports = packageDescr.getImports();
238: assertEquals(2, imports.size());
239: assertEquals("java.util.HashMap",
240: ((ImportDescr) imports.get(0)).getTarget());
241: assertEquals("org.drools.*", ((ImportDescr) imports.get(1))
242: .getTarget());
243: }
244:
245: public void testParseGlobal() throws Exception {
246: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
247: xmlPackageReader.read(new InputStreamReader(getClass()
248: .getResourceAsStream("test_ParseGlobal.xml")));
249: final PackageDescr packageDescr = xmlPackageReader
250: .getPackageDescr();
251: assertNotNull(packageDescr);
252: assertEquals("com.sample", packageDescr.getName());
253:
254: final List imports = packageDescr.getImports();
255: assertEquals(2, imports.size());
256: assertEquals("java.util.HashMap",
257: ((ImportDescr) imports.get(0)).getTarget());
258: assertEquals("org.drools.*", ((ImportDescr) imports.get(1))
259: .getTarget());
260:
261: final List globals = packageDescr.getGlobals();
262: assertEquals(2, globals.size());
263: final GlobalDescr x = (GlobalDescr) globals.get(0);
264: final GlobalDescr yada = (GlobalDescr) globals.get(1);
265: assertEquals("com.sample.X", x.getType());
266: assertEquals("x", x.getIdentifier());
267: assertEquals("com.sample.Yada", yada.getType());
268: assertEquals("yada", yada.getIdentifier());
269: }
270:
271: public void testParseFunction() throws Exception {
272: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
273: xmlPackageReader.read(new InputStreamReader(getClass()
274: .getResourceAsStream("test_ParseFunction.xml")));
275: final PackageDescr packageDescr = xmlPackageReader
276: .getPackageDescr();
277: assertNotNull(packageDescr);
278: assertEquals("com.sample", packageDescr.getName());
279:
280: final List imports = packageDescr.getImports();
281: assertEquals(2, imports.size());
282: assertEquals("java.util.HashMap",
283: ((ImportDescr) imports.get(0)).getTarget());
284: assertEquals("org.drools.*", ((ImportDescr) imports.get(1))
285: .getTarget());
286:
287: final List globals = packageDescr.getGlobals();
288: assertEquals(2, globals.size());
289: final GlobalDescr x = (GlobalDescr) globals.get(0);
290: final GlobalDescr yada = (GlobalDescr) globals.get(1);
291: assertEquals("com.sample.X", x.getType());
292: assertEquals("x", x.getIdentifier());
293: assertEquals("com.sample.Yada", yada.getType());
294: assertEquals("yada", yada.getIdentifier());
295:
296: final FunctionDescr functionDescr = (FunctionDescr) packageDescr
297: .getFunctions().get(0);
298: final List names = functionDescr.getParameterNames();
299: assertEquals("foo", names.get(0));
300: assertEquals("bada", names.get(1));
301:
302: final List types = functionDescr.getParameterTypes();
303: assertEquals("Bar", types.get(0));
304: assertEquals("Bing", types.get(1));
305:
306: assertEquals("System.out.println(\"hello world\");",
307: functionDescr.getText().trim());
308: }
309:
310: public void testParseRule() throws Exception {
311: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
312: xmlPackageReader.read(new InputStreamReader(getClass()
313: .getResourceAsStream("test_ParseRule.xml")));
314: final PackageDescr packageDescr = xmlPackageReader
315: .getPackageDescr();
316: assertNotNull(packageDescr);
317: assertEquals("com.sample", packageDescr.getName());
318:
319: final List imports = packageDescr.getImports();
320: assertEquals(2, imports.size());
321: assertEquals("java.util.HashMap",
322: ((ImportDescr) imports.get(0)).getTarget());
323: assertEquals("org.drools.*", ((ImportDescr) imports.get(1))
324: .getTarget());
325:
326: final List globals = packageDescr.getGlobals();
327: assertEquals(2, globals.size());
328: final GlobalDescr x = (GlobalDescr) globals.get(0);
329: final GlobalDescr yada = (GlobalDescr) globals.get(1);
330: assertEquals("com.sample.X", x.getType());
331: assertEquals("x", x.getIdentifier());
332: assertEquals("com.sample.Yada", yada.getType());
333: assertEquals("yada", yada.getIdentifier());
334:
335: final FunctionDescr functionDescr = (FunctionDescr) packageDescr
336: .getFunctions().get(0);
337: final List names = functionDescr.getParameterNames();
338: assertEquals("foo", names.get(0));
339: assertEquals("bada", names.get(1));
340:
341: final List types = functionDescr.getParameterTypes();
342: assertEquals("Bar", types.get(0));
343: assertEquals("Bing", types.get(1));
344:
345: assertEquals("System.out.println(\"hello world\");",
346: functionDescr.getText().trim());
347:
348: final RuleDescr ruleDescr = (RuleDescr) packageDescr.getRules()
349: .get(0);
350: assertEquals("simple_rule", ruleDescr.getName());
351:
352: assertEquals(4, ruleDescr.getAttributes().size());
353: final AttributeDescr attributeDescr = (AttributeDescr) ruleDescr
354: .getAttributes().get(0);
355: assertEquals("salience", attributeDescr.getName());
356: assertEquals("10", attributeDescr.getValue());
357:
358: final AndDescr lhs = ruleDescr.getLhs();
359: assertEquals(7, lhs.getDescrs().size());
360: final PatternDescr patternDescr = (PatternDescr) lhs
361: .getDescrs().get(0);
362: assertEquals("Bar", patternDescr.getObjectType());
363:
364: final String consequence = (String) ruleDescr.getConsequence();
365: assertNotNull(consequence);
366: }
367:
368: public void testParseLhs() throws Exception {
369: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
370: xmlPackageReader.read(new InputStreamReader(getClass()
371: .getResourceAsStream("test_ParseLhs.xml")));
372: final PackageDescr packageDescr = xmlPackageReader
373: .getPackageDescr();
374: assertNotNull(packageDescr);
375: assertEquals("com.sample", packageDescr.getName());
376:
377: final List imports = packageDescr.getImports();
378: assertEquals(2, imports.size());
379: assertEquals("java.util.HashMap",
380: ((ImportDescr) imports.get(0)).getTarget());
381: assertEquals("org.drools.*", ((ImportDescr) imports.get(1))
382: .getTarget());
383:
384: final List globals = packageDescr.getGlobals();
385: assertEquals(2, globals.size());
386: final GlobalDescr x = (GlobalDescr) globals.get(0);
387: final GlobalDescr yada = (GlobalDescr) globals.get(1);
388: assertEquals("com.sample.X", x.getType());
389: assertEquals("x", x.getIdentifier());
390: assertEquals("com.sample.Yada", yada.getType());
391: assertEquals("yada", yada.getIdentifier());
392:
393: final FunctionDescr functionDescr = (FunctionDescr) packageDescr
394: .getFunctions().get(0);
395: final List names = functionDescr.getParameterNames();
396: assertEquals("foo", names.get(0));
397: assertEquals("bada", names.get(1));
398:
399: final List types = functionDescr.getParameterTypes();
400: assertEquals("Bar", types.get(0));
401: assertEquals("Bing", types.get(1));
402:
403: assertEquals("System.out.println(\"hello world\");",
404: functionDescr.getText().trim());
405:
406: final RuleDescr ruleDescr = (RuleDescr) packageDescr.getRules()
407: .get(0);
408: assertEquals("my rule", ruleDescr.getName());
409:
410: final AndDescr lhsDescr = ruleDescr.getLhs();
411:
412: AndDescr andDescr = (AndDescr) lhsDescr.getDescrs().get(0);
413: OrDescr orDescr = (OrDescr) lhsDescr.getDescrs().get(1);
414: final PatternDescr pattern1 = (PatternDescr) lhsDescr
415: .getDescrs().get(2);
416: assertNull(pattern1.getIdentifier());
417: assertEquals("Foo", pattern1.getObjectType());
418:
419: final PatternDescr pattern2 = (PatternDescr) lhsDescr
420: .getDescrs().get(3);
421: assertEquals("Bar", pattern2.getObjectType());
422: assertEquals("bar", pattern2.getIdentifier());
423:
424: final PatternDescr pattern3 = (PatternDescr) lhsDescr
425: .getDescrs().get(4);
426: //final LiteralDescr literalDescr = (LiteralDescr) pattern3.getDescrs().get( 0 );
427: final FieldConstraintDescr fieldConstraintDescr = (FieldConstraintDescr) pattern3
428: .getDescrs().get(0);
429: final LiteralRestrictionDescr literalDescr = (LiteralRestrictionDescr) fieldConstraintDescr
430: .getRestrictions().get(0);
431: assertEquals("field1", fieldConstraintDescr.getFieldName());
432: assertEquals("==", literalDescr.getEvaluator());
433: assertEquals("value1", literalDescr.getText());
434:
435: final ReturnValueRestrictionDescr returnValueDescr = (ReturnValueRestrictionDescr) fieldConstraintDescr
436: .getRestrictions().get(1);
437: assertEquals("==", returnValueDescr.getEvaluator());
438: assertEquals("1==1", returnValueDescr.getContent());
439:
440: final VariableRestrictionDescr variableDescr = (VariableRestrictionDescr) fieldConstraintDescr
441: .getRestrictions().get(2);
442: assertEquals("==", variableDescr.getEvaluator());
443: assertEquals("var1", variableDescr.getIdentifier());
444:
445: final PredicateDescr predicateDescr = (PredicateDescr) pattern3
446: .getDescrs().get(1);
447: assertEquals("1==1", predicateDescr.getContent());
448:
449: final FieldBindingDescr fieldBindingDescr = (FieldBindingDescr) pattern3
450: .getDescrs().get(2);
451: assertEquals("field1", fieldBindingDescr.getFieldName());
452: assertEquals("var1", fieldBindingDescr.getIdentifier());
453:
454: final NotDescr notDescr = (NotDescr) lhsDescr.getDescrs()
455: .get(5);
456: assertEquals(1, notDescr.getDescrs().size());
457: PatternDescr patternDescr = (PatternDescr) notDescr.getDescrs()
458: .get(0);
459: assertEquals("Bar", patternDescr.getObjectType());
460:
461: final ExistsDescr existsDescr = (ExistsDescr) lhsDescr
462: .getDescrs().get(6);
463: assertEquals(1, existsDescr.getDescrs().size());
464: patternDescr = (PatternDescr) existsDescr.getDescrs().get(0);
465: assertEquals("Bar", patternDescr.getObjectType());
466:
467: andDescr = (AndDescr) lhsDescr.getDescrs().get(7);
468: assertEquals(2, andDescr.getDescrs().size());
469: orDescr = (OrDescr) andDescr.getDescrs().get(1);
470: patternDescr = (PatternDescr) orDescr.getDescrs().get(0);
471: assertEquals("Bar", patternDescr.getObjectType());
472: patternDescr = (PatternDescr) andDescr.getDescrs().get(0);
473: assertEquals("Yada", patternDescr.getObjectType());
474:
475: orDescr = (OrDescr) lhsDescr.getDescrs().get(8);
476: assertEquals(2, orDescr.getDescrs().size());
477: andDescr = (AndDescr) orDescr.getDescrs().get(1);
478: patternDescr = (PatternDescr) andDescr.getDescrs().get(0);
479: assertEquals("Foo", patternDescr.getObjectType());
480: patternDescr = (PatternDescr) orDescr.getDescrs().get(0);
481: assertEquals("Zaa", patternDescr.getObjectType());
482:
483: final EvalDescr evalDescr = (EvalDescr) lhsDescr.getDescrs()
484: .get(9);
485: assertEquals("1==1", evalDescr.getContent());
486: }
487:
488: public void testParseRhs() throws Exception {
489: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
490: xmlPackageReader.read(new InputStreamReader(getClass()
491: .getResourceAsStream("test_ParseRhs.xml")));
492: final PackageDescr packageDescr = xmlPackageReader
493: .getPackageDescr();
494: assertNotNull(packageDescr);
495: assertEquals("com.sample", packageDescr.getName());
496:
497: final List imports = packageDescr.getImports();
498: assertEquals(2, imports.size());
499: assertEquals("java.util.HashMap",
500: ((ImportDescr) imports.get(0)).getTarget());
501: assertEquals("org.drools.*", ((ImportDescr) imports.get(1))
502: .getTarget());
503:
504: final List globals = packageDescr.getGlobals();
505: assertEquals(2, globals.size());
506: final GlobalDescr x = (GlobalDescr) globals.get(0);
507: final GlobalDescr yada = (GlobalDescr) globals.get(1);
508: assertEquals("com.sample.X", x.getType());
509: assertEquals("x", x.getIdentifier());
510: assertEquals("com.sample.Yada", yada.getType());
511: assertEquals("yada", yada.getIdentifier());
512:
513: final FunctionDescr functionDescr = (FunctionDescr) packageDescr
514: .getFunctions().get(0);
515: final List names = functionDescr.getParameterNames();
516: assertEquals("foo", names.get(0));
517: assertEquals("bada", names.get(1));
518:
519: final List types = functionDescr.getParameterTypes();
520: assertEquals("Bar", types.get(0));
521: assertEquals("Bing", types.get(1));
522:
523: assertEquals("System.out.println(\"hello world\");",
524: functionDescr.getText().trim());
525:
526: final RuleDescr ruleDescr = (RuleDescr) packageDescr.getRules()
527: .get(0);
528: assertEquals("my rule", ruleDescr.getName());
529:
530: final String consequence = (String) ruleDescr.getConsequence();
531: assertNotNull(consequence);
532: assertEquals("System.out.println( \"hello\" );", consequence
533: .trim());
534: }
535:
536: public void testParseQuery() throws Exception {
537: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
538: xmlPackageReader.read(new InputStreamReader(getClass()
539: .getResourceAsStream("test_ParseQuery.xml")));
540: final PackageDescr packageDescr = xmlPackageReader
541: .getPackageDescr();
542: assertNotNull(packageDescr);
543: assertEquals("com.sample", packageDescr.getName());
544:
545: final List imports = packageDescr.getImports();
546: assertEquals(2, imports.size());
547: assertEquals("java.util.HashMap",
548: ((ImportDescr) imports.get(0)).getTarget());
549: assertEquals("org.drools.*", ((ImportDescr) imports.get(1))
550: .getTarget());
551:
552: final List globals = packageDescr.getGlobals();
553: assertEquals(2, globals.size());
554: final GlobalDescr x = (GlobalDescr) globals.get(0);
555: final GlobalDescr yada = (GlobalDescr) globals.get(1);
556: assertEquals("com.sample.X", x.getType());
557: assertEquals("x", x.getIdentifier());
558: assertEquals("com.sample.Yada", yada.getType());
559: assertEquals("yada", yada.getIdentifier());
560:
561: final FunctionDescr functionDescr = (FunctionDescr) packageDescr
562: .getFunctions().get(0);
563: final List names = functionDescr.getParameterNames();
564: assertEquals("foo", names.get(0));
565: assertEquals("bada", names.get(1));
566:
567: final List types = functionDescr.getParameterTypes();
568: assertEquals("Bar", types.get(0));
569: assertEquals("Bing", types.get(1));
570:
571: assertEquals("System.out.println(\"hello world\");",
572: functionDescr.getText().trim());
573:
574: final QueryDescr queryDescr = (QueryDescr) packageDescr
575: .getRules().get(0);
576: assertEquals("my query", queryDescr.getName());
577:
578: final AndDescr lhs = queryDescr.getLhs();
579: assertEquals(1, lhs.getDescrs().size());
580: final PatternDescr patternDescr = (PatternDescr) lhs
581: .getDescrs().get(0);
582: assertEquals("Foo", patternDescr.getObjectType());
583:
584: }
585: }
|