Source Code Cross Referenced for RuleParserTest.java in  » Rule-Engine » drolls-Rule-Engine » org » drools » lang » 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 » Rule Engine » drolls Rule Engine » org.drools.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        package org.drools.lang;
0002:
0003:        /*
0004:         * Copyright 2005 JBoss Inc
0005:         *
0006:         * Licensed under the Apache License, Version 2.0 (the "License");
0007:         * you may not use this file except in compliance with the License.
0008:         * You may obtain a copy of the License at
0009:         *
0010:         *      http://www.apache.org/licenses/LICENSE-2.0
0011:         *
0012:         * Unless required by applicable law or agreed to in writing, software
0013:         * distributed under the License is distributed on an "AS IS" BASIS,
0014:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015:         * See the License for the specific language governing permissions and
0016:         * limitations under the License.
0017:         */
0018:
0019:        import java.io.InputStream;
0020:        import java.io.InputStreamReader;
0021:        import java.io.Reader;
0022:        import java.util.Iterator;
0023:        import java.util.List;
0024:
0025:        import junit.framework.TestCase;
0026:
0027:        import org.antlr.runtime.ANTLRStringStream;
0028:        import org.antlr.runtime.CharStream;
0029:        import org.antlr.runtime.CommonToken;
0030:        import org.antlr.runtime.CommonTokenStream;
0031:        import org.antlr.runtime.Lexer;
0032:        import org.antlr.runtime.RecognitionException;
0033:        import org.antlr.runtime.TokenStream;
0034:        import org.drools.compiler.DrlParser;
0035:        import org.drools.lang.DRLParser.paren_chunk_return;
0036:        import org.drools.lang.descr.AccessorDescr;
0037:        import org.drools.lang.descr.AccumulateDescr;
0038:        import org.drools.lang.descr.AndDescr;
0039:        import org.drools.lang.descr.AttributeDescr;
0040:        import org.drools.lang.descr.CollectDescr;
0041:        import org.drools.lang.descr.EvalDescr;
0042:        import org.drools.lang.descr.ExistsDescr;
0043:        import org.drools.lang.descr.FactTemplateDescr;
0044:        import org.drools.lang.descr.FieldAccessDescr;
0045:        import org.drools.lang.descr.FieldBindingDescr;
0046:        import org.drools.lang.descr.FieldConstraintDescr;
0047:        import org.drools.lang.descr.FieldTemplateDescr;
0048:        import org.drools.lang.descr.ForallDescr;
0049:        import org.drools.lang.descr.FromDescr;
0050:        import org.drools.lang.descr.FunctionDescr;
0051:        import org.drools.lang.descr.FunctionImportDescr;
0052:        import org.drools.lang.descr.GlobalDescr;
0053:        import org.drools.lang.descr.ImportDescr;
0054:        import org.drools.lang.descr.LiteralRestrictionDescr;
0055:        import org.drools.lang.descr.NotDescr;
0056:        import org.drools.lang.descr.OrDescr;
0057:        import org.drools.lang.descr.PackageDescr;
0058:        import org.drools.lang.descr.PatternDescr;
0059:        import org.drools.lang.descr.PredicateDescr;
0060:        import org.drools.lang.descr.QualifiedIdentifierRestrictionDescr;
0061:        import org.drools.lang.descr.QueryDescr;
0062:        import org.drools.lang.descr.RestrictionConnectiveDescr;
0063:        import org.drools.lang.descr.ReturnValueRestrictionDescr;
0064:        import org.drools.lang.descr.RuleDescr;
0065:        import org.drools.lang.descr.VariableRestrictionDescr;
0066:        import org.drools.lang.dsl.DefaultExpander;
0067:
0068:        public class RuleParserTest extends TestCase {
0069:
0070:            private DRLParser parser;
0071:
0072:            protected void setUp() throws Exception {
0073:                super .setUp();
0074:                this .parser = null;
0075:            }
0076:
0077:            protected void tearDown() throws Exception {
0078:                this .parser = null;
0079:                super .tearDown();
0080:            }
0081:
0082:            public void testPackage_OneSegment() throws Exception {
0083:                final String packageName = parse("package foo")
0084:                        .package_statement();
0085:                assertEquals("foo", packageName);
0086:                assertFalse(this .parser.hasErrors());
0087:            }
0088:
0089:            public void testPackage_MultipleSegments() throws Exception {
0090:                final String packageName = parse("package foo.bar.baz;")
0091:                        .package_statement();
0092:                assertEquals("foo.bar.baz", packageName);
0093:                assertFalse(this .parser.hasErrors());
0094:            }
0095:
0096:            public void testCompilationUnit() throws Exception {
0097:                final String source = "package foo; import com.foo.Bar; import com.foo.Baz;";
0098:                parse(source).compilation_unit();
0099:                assertEquals("foo", this .parser.getPackageDescr().getName());
0100:                assertEquals(2, this .parser.getPackageDescr().getImports()
0101:                        .size());
0102:                ImportDescr impdescr = (ImportDescr) this .parser
0103:                        .getPackageDescr().getImports().get(0);
0104:                assertEquals("com.foo.Bar", impdescr.getTarget());
0105:                assertEquals(source.indexOf("import " + impdescr.getTarget()),
0106:                        impdescr.getStartCharacter());
0107:                assertEquals(source.indexOf("import " + impdescr.getTarget())
0108:                        + ("import " + impdescr.getTarget()).length() - 1,
0109:                        impdescr.getEndCharacter());
0110:
0111:                impdescr = (ImportDescr) this .parser.getPackageDescr()
0112:                        .getImports().get(1);
0113:                assertEquals("com.foo.Baz", impdescr.getTarget());
0114:                assertEquals(source.indexOf("import " + impdescr.getTarget()),
0115:                        impdescr.getStartCharacter());
0116:                assertEquals(source.indexOf("import " + impdescr.getTarget())
0117:                        + ("import " + impdescr.getTarget()).length() - 1,
0118:                        impdescr.getEndCharacter());
0119:
0120:                assertFalse(this .parser.hasErrors());
0121:            }
0122:
0123:            public void testEmptyRule() throws Exception {
0124:                final RuleDescr rule = parseResource("empty_rule.drl").rule();
0125:
0126:                assertNotNull(rule);
0127:
0128:                assertEquals("empty", rule.getName());
0129:                assertNull(rule.getLhs());
0130:                assertNotNull(rule.getConsequence());
0131:
0132:                assertFalse(this .parser.getErrorMessages().toString(),
0133:                        this .parser.hasErrors());
0134:            }
0135:
0136:            public void testKeywordCollisions() throws Exception {
0137:                final DRLParser parser = parseResource("eol_funny_business.drl");
0138:
0139:                parser.compilation_unit();
0140:                final PackageDescr pkg = parser.getPackageDescr();
0141:
0142:                assertEquals(1, pkg.getRules().size());
0143:
0144:                assertFalse(parser.getErrors().toString(), parser.hasErrors());
0145:
0146:            }
0147:
0148:            public void testPartialAST() throws Exception {
0149:                parseResource("pattern_partial.drl");
0150:
0151:                this .parser.compilation_unit();
0152:
0153:                assertTrue(this .parser.hasErrors());
0154:
0155:                final PackageDescr pkg = this .parser.getPackageDescr();
0156:                assertEquals(1, pkg.getRules().size());
0157:                final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
0158:
0159:                assertEquals(1, rule.getLhs().getDescrs().size());
0160:                final PatternDescr pattern = (PatternDescr) rule.getLhs()
0161:                        .getDescrs().get(0);
0162:
0163:                assertNotNull(pattern);
0164:                assertEquals("Bar", pattern.getObjectType());
0165:                assertEquals("foo3", pattern.getIdentifier());
0166:
0167:            }
0168:
0169:            public void testTemplates() throws Exception {
0170:
0171:                final DRLParser parser = parseResource("test_Templates.drl");
0172:
0173:                parser.compilation_unit();
0174:                final PackageDescr pkg = parser.getPackageDescr();
0175:
0176:                if (parser.hasErrors()) {
0177:                    System.err.println("FACT TEMPLATES FAILED: "
0178:                            + parser.getErrorMessages());
0179:                }
0180:                assertFalse(parser.hasErrors());
0181:
0182:                assertEquals(1, pkg.getRules().size());
0183:                assertEquals(2, pkg.getFactTemplates().size());
0184:
0185:                FactTemplateDescr fact1 = (FactTemplateDescr) pkg
0186:                        .getFactTemplates().get(0);
0187:                assertEquals("Cheese", fact1.getName());
0188:                assertEquals(2, fact1.getFields().size());
0189:
0190:                assertEquals("name", ((FieldTemplateDescr) fact1.getFields()
0191:                        .get(0)).getName());
0192:                assertEquals("String", ((FieldTemplateDescr) fact1.getFields()
0193:                        .get(0)).getClassType());
0194:
0195:                assertEquals("age", ((FieldTemplateDescr) fact1.getFields()
0196:                        .get(1)).getName());
0197:                assertEquals("Integer", ((FieldTemplateDescr) fact1.getFields()
0198:                        .get(1)).getClassType());
0199:
0200:                fact1 = null;
0201:
0202:                final FactTemplateDescr fact2 = (FactTemplateDescr) pkg
0203:                        .getFactTemplates().get(1);
0204:                assertEquals("Wine", fact2.getName());
0205:                assertEquals(3, fact2.getFields().size());
0206:
0207:                assertEquals("name", ((FieldTemplateDescr) fact2.getFields()
0208:                        .get(0)).getName());
0209:                assertEquals("String", ((FieldTemplateDescr) fact2.getFields()
0210:                        .get(0)).getClassType());
0211:
0212:                assertEquals("year", ((FieldTemplateDescr) fact2.getFields()
0213:                        .get(1)).getName());
0214:                assertEquals("String", ((FieldTemplateDescr) fact2.getFields()
0215:                        .get(1)).getClassType());
0216:
0217:                assertEquals("accolades", ((FieldTemplateDescr) fact2
0218:                        .getFields().get(2)).getName());
0219:                assertEquals("String[]", ((FieldTemplateDescr) fact2
0220:                        .getFields().get(2)).getClassType());
0221:            }
0222:
0223:            public void testTernaryExpression() throws Exception {
0224:
0225:                final DRLParser parser = parseResource("ternary_expression.drl");
0226:
0227:                parser.compilation_unit();
0228:                final PackageDescr pkg = parser.getPackageDescr();
0229:                final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
0230:                assertEquals(1, pkg.getRules().size());
0231:
0232:                assertFalse(parser.hasErrors());
0233:                assertEqualsIgnoreWhitespace(
0234:                        "if (speed > speedLimit ? true : false;) pullEmOver();",
0235:                        (String) rule.getConsequence());
0236:            }
0237:
0238:            public void FIX_ME_testLatinChars() throws Exception {
0239:                final DrlParser parser = new DrlParser();
0240:                final Reader drl = new InputStreamReader(this .getClass()
0241:                        .getResourceAsStream("latin-sample.dslr"));
0242:                final Reader dsl = new InputStreamReader(this .getClass()
0243:                        .getResourceAsStream("latin.dsl"));
0244:
0245:                final PackageDescr pkg = parser.parse(drl, dsl);
0246:
0247:                //MN: will get some errors due to the char encoding on my FC5 install
0248:                //others who use the right encoding may not see this, feel free to uncomment
0249:                //the following assertion.
0250:                assertFalse(parser.hasErrors());
0251:
0252:                assertEquals("br.com.auster.drools.sample", pkg.getName());
0253:                assertEquals(1, pkg.getRules().size());
0254:
0255:            }
0256:
0257:            public void testFunctionWithArrays() throws Exception {
0258:                final DRLParser parser = parseResource("function_arrays.drl");
0259:
0260:                parser.compilation_unit();
0261:                final PackageDescr pkg = parser.getPackageDescr();
0262:
0263:                if (parser.hasErrors()) {
0264:                    System.err.println(parser.getErrorMessages());
0265:                }
0266:                assertFalse(parser.hasErrors());
0267:                assertEquals("foo", pkg.getName());
0268:                assertEquals(1, pkg.getRules().size());
0269:
0270:                final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
0271:
0272:                assertEqualsIgnoreWhitespace(
0273:                        "yourFunction(new String[3] {\"a\",\"b\",\"c\"});",
0274:                        (String) rule.getConsequence());
0275:                final FunctionDescr func = (FunctionDescr) pkg.getFunctions()
0276:                        .get(0);
0277:
0278:                assertEquals("String[]", func.getReturnType());
0279:                assertEquals("args[]", func.getParameterNames().get(0));
0280:                assertEquals("String", func.getParameterTypes().get(0));
0281:            }
0282:
0283:            public void testAlmostEmptyRule() throws Exception {
0284:                final RuleDescr rule = parseResource("almost_empty_rule.drl")
0285:                        .rule();
0286:
0287:                assertNotNull(rule);
0288:
0289:                assertEquals("almost_empty", rule.getName());
0290:                assertNotNull(rule.getLhs());
0291:                assertEquals("", ((String) rule.getConsequence()).trim());
0292:                assertFalse(this .parser.hasErrors());
0293:            }
0294:
0295:            public void testQuotedStringNameRule() throws Exception {
0296:                final RuleDescr rule = parseResource(
0297:                        "quoted_string_name_rule.drl").rule();
0298:
0299:                assertNotNull(rule);
0300:
0301:                assertEquals("quoted string name", rule.getName());
0302:                assertNotNull(rule.getLhs());
0303:                assertEquals("", ((String) rule.getConsequence()).trim());
0304:                assertFalse(this .parser.hasErrors());
0305:            }
0306:
0307:            public void testNoLoop() throws Exception {
0308:                final RuleDescr rule = parseResource("no-loop.drl").rule();
0309:
0310:                assertNotNull(rule);
0311:
0312:                assertEquals("rule1", rule.getName());
0313:                final AttributeDescr att = (AttributeDescr) rule
0314:                        .getAttributes().get(0);
0315:                assertEquals("false", att.getValue());
0316:                assertEquals("no-loop", att.getName());
0317:                assertFalse(this .parser.getErrorMessages().toString(),
0318:                        this .parser.hasErrors());
0319:
0320:            }
0321:
0322:            public void testAutofocus() throws Exception {
0323:                final RuleDescr rule = parseResource("autofocus.drl").rule();
0324:
0325:                assertNotNull(rule);
0326:
0327:                assertEquals("rule1", rule.getName());
0328:                final AttributeDescr att = (AttributeDescr) rule
0329:                        .getAttributes().get(0);
0330:                assertEquals("true", att.getValue());
0331:                assertEquals("auto-focus", att.getName());
0332:
0333:                assertFalse(this .parser.hasErrors());
0334:            }
0335:
0336:            public void testRuleFlowGroup() throws Exception {
0337:                final RuleDescr rule = parseResource("ruleflowgroup.drl")
0338:                        .rule();
0339:
0340:                assertNotNull(rule);
0341:
0342:                assertEquals("rule1", rule.getName());
0343:                final AttributeDescr att = (AttributeDescr) rule
0344:                        .getAttributes().get(0);
0345:                assertEquals("a group", att.getValue());
0346:                assertEquals("ruleflow-group", att.getName());
0347:
0348:                assertFalse(this .parser.hasErrors());
0349:            }
0350:
0351:            public void testConsequenceWithDeclaration() throws Exception {
0352:                final RuleDescr rule = parseResource(
0353:                        "declaration-in-consequence.drl").rule();
0354:
0355:                assertNotNull(rule);
0356:
0357:                assertEquals("myrule", rule.getName());
0358:
0359:                final String expected = "int i = 0; i = 1; i / 1; i == 1; i(i); i = 'i'; i.i.i; i\\i; i<i; i>i; i=\"i\";  ++i;"
0360:                        + "i++; --i; i--; i += i; i -= i; i *= i; i /= i;"
0361:                        + "int i = 5;"
0362:                        + "for(int j; j<i; ++j) {"
0363:                        + "System.out.println(j);}"
0364:                        + "Object o = new String(\"Hello\");"
0365:                        + "String s = (String) o;";
0366:
0367:                assertEqualsIgnoreWhitespace(expected, (String) rule
0368:                        .getConsequence());
0369:                assertTrue(((String) rule.getConsequence()).indexOf("++") > 0);
0370:                assertTrue(((String) rule.getConsequence()).indexOf("--") > 0);
0371:                assertTrue(((String) rule.getConsequence()).indexOf("+=") > 0);
0372:                assertTrue(((String) rule.getConsequence()).indexOf("==") > 0);
0373:
0374:                //System.out.println(( String ) rule.getConsequence());
0375:                //note, need to assert that "i++" is preserved as is, no extra spaces.
0376:
0377:                assertFalse(this .parser.hasErrors());
0378:            }
0379:
0380:            public void testRuleParseLhs() throws Exception {
0381:                final String text = "Person(age < 42, location==\"atlanta\") \nor\nPerson(name==\"bob\") \n";
0382:                final AndDescr descrs = new AndDescr();
0383:                final CharStream charStream = new ANTLRStringStream(text);
0384:                final DRLLexer lexer = new DRLLexer(charStream);
0385:                final TokenStream tokenStream = new CommonTokenStream(lexer);
0386:                final DRLParser parser = new DRLParser(tokenStream);
0387:                parser.setLineOffset(descrs.getLine());
0388:                parser.normal_lhs_block(descrs);
0389:                if (parser.hasErrors()) {
0390:                    System.err.println(parser.getErrorMessages());
0391:                }
0392:                assertFalse(parser.hasErrors());
0393:            }
0394:
0395:            public void testLiteralBoolAndNegativeNumbersRule()
0396:                    throws Exception {
0397:                final DRLParser parser = parseResource("literal_bool_and_negative.drl");
0398:                final RuleDescr rule = parser.rule();
0399:                assertFalse(parser.hasErrors());
0400:
0401:                assertNotNull(rule);
0402:
0403:                assertEquals("simple_rule", rule.getName());
0404:                assertNotNull(rule.getLhs());
0405:                assertEqualsIgnoreWhitespace("cons();", (String) rule
0406:                        .getConsequence());
0407:
0408:                final AndDescr lhs = rule.getLhs();
0409:                assertEquals(3, lhs.getDescrs().size());
0410:
0411:                PatternDescr pattern = (PatternDescr) lhs.getDescrs().get(0);
0412:                assertEquals(1, pattern.getConstraint().getDescrs().size());
0413:                AndDescr fieldAnd = (AndDescr) pattern.getConstraint();
0414:                FieldConstraintDescr fld = (FieldConstraintDescr) fieldAnd
0415:                        .getDescrs().get(0);
0416:                LiteralRestrictionDescr lit = (LiteralRestrictionDescr) fld
0417:                        .getRestrictions().get(0);
0418:
0419:                assertEquals("==", lit.getEvaluator());
0420:                assertEquals("false", lit.getText());
0421:                assertEquals("bar", fld.getFieldName());
0422:
0423:                pattern = (PatternDescr) lhs.getDescrs().get(1);
0424:                assertEquals(1, pattern.getConstraint().getDescrs().size());
0425:
0426:                fieldAnd = (AndDescr) pattern.getConstraint();
0427:                fld = (FieldConstraintDescr) fieldAnd.getDescrs().get(0);
0428:                lit = (LiteralRestrictionDescr) fld.getRestrictions().get(0);
0429:
0430:                assertEquals(">", lit.getEvaluator());
0431:                assertEquals("-42", lit.getText());
0432:                assertEquals("boo", fld.getFieldName());
0433:
0434:                pattern = (PatternDescr) lhs.getDescrs().get(2);
0435:                assertEquals(1, pattern.getConstraint().getDescrs().size());
0436:
0437:                //lit = (LiteralDescr) col.getDescrs().get( 0 );
0438:
0439:                fieldAnd = (AndDescr) pattern.getConstraint();
0440:                fld = (FieldConstraintDescr) fieldAnd.getDescrs().get(0);
0441:                lit = (LiteralRestrictionDescr) fld.getRestrictions().get(0);
0442:                assertEquals(">", lit.getEvaluator());
0443:                assertEquals("-42.42", lit.getText());
0444:                assertEquals("boo", fld.getFieldName());
0445:
0446:                assertFalse(parser.hasErrors());
0447:            }
0448:
0449:            public void testChunkWithoutParens() throws Exception {
0450:                String input = "( foo )";
0451:                paren_chunk_return ret = parse(input).paren_chunk();
0452:                final String chunk = input.substring(((CommonToken) ret.start)
0453:                        .getStartIndex(), ((CommonToken) ret.stop)
0454:                        .getStopIndex() + 1);
0455:
0456:                assertEquals("( foo )", chunk);
0457:
0458:                assertFalse(this .parser.hasErrors());
0459:            }
0460:
0461:            public void testChunkWithParens() throws Exception {
0462:                String input = "(fnord())";
0463:                paren_chunk_return ret = parse(input).paren_chunk();
0464:                final String chunk = input.substring(((CommonToken) ret.start)
0465:                        .getStartIndex(), ((CommonToken) ret.stop)
0466:                        .getStopIndex() + 1);
0467:
0468:                assertEqualsIgnoreWhitespace("(fnord())", chunk);
0469:
0470:                assertFalse(this .parser.hasErrors());
0471:            }
0472:
0473:            public void testChunkWithParensAndQuotedString() throws Exception {
0474:                String input = "( fnord( \"cheese\" ) )";
0475:                paren_chunk_return ret = parse(input).paren_chunk();
0476:                final String chunk = input.substring(((CommonToken) ret.start)
0477:                        .getStartIndex(), ((CommonToken) ret.stop)
0478:                        .getStopIndex() + 1);
0479:
0480:                assertEqualsIgnoreWhitespace("( fnord( \"cheese\" ) )", chunk);
0481:
0482:                assertFalse(this .parser.hasErrors());
0483:            }
0484:
0485:            public void testChunkWithRandomCharac5ters() throws Exception {
0486:                String input = "( %*9dkj)";
0487:                paren_chunk_return ret = parse(input).paren_chunk();
0488:                final String chunk = input.substring(((CommonToken) ret.start)
0489:                        .getStartIndex(), ((CommonToken) ret.stop)
0490:                        .getStopIndex() + 1);
0491:
0492:                assertEqualsIgnoreWhitespace("( %*9dkj)", chunk);
0493:
0494:                assertFalse(this .parser.hasErrors());
0495:            }
0496:
0497:            public void testEmptyPattern() throws Exception {
0498:                parseResource("test_EmptyPattern.drl");
0499:                this .parser.compilation_unit();
0500:                final PackageDescr packageDescr = this .parser.getPackageDescr();
0501:                assertEquals(1, packageDescr.getRules().size());
0502:                final RuleDescr ruleDescr = (RuleDescr) packageDescr.getRules()
0503:                        .get(0);
0504:                assertEquals("simple rule", ruleDescr.getName());
0505:                assertNotNull(ruleDescr.getLhs());
0506:                assertEquals(1, ruleDescr.getLhs().getDescrs().size());
0507:                final PatternDescr patternDescr = (PatternDescr) ruleDescr
0508:                        .getLhs().getDescrs().get(0);
0509:                assertEquals(0, patternDescr.getConstraint().getDescrs().size()); //this may be null, not sure as the test doesn't get this far...
0510:                assertEquals("Cheese", patternDescr.getObjectType());
0511:
0512:            }
0513:
0514:            public void testSimpleMethodCallWithFrom() throws Exception {
0515:
0516:                final RuleDescr rule = parseResource(
0517:                        "test_SimpleMethodCallWithFrom.drl").rule();
0518:                final PatternDescr pattern = (PatternDescr) rule.getLhs()
0519:                        .getDescrs().get(0);
0520:                final FromDescr from = (FromDescr) pattern.getSource();
0521:                final AccessorDescr method = (AccessorDescr) from
0522:                        .getDataSource();
0523:
0524:                assertFalse(this .parser.getErrorMessages().toString(),
0525:                        this .parser.hasErrors());
0526:
0527:                assertEquals(
0528:                        "something.doIt( foo,bar,42,\"hello\",{ a => \"b\", \"something\" => 42, \"a\" => foo, x => {x=>y}},\"end\", [a, \"b\", 42] )",
0529:                        method.toString());
0530:            }
0531:
0532:            public void testSimpleFunctionCallWithFrom() throws Exception {
0533:
0534:                final RuleDescr rule = parseResource(
0535:                        "test_SimpleFunctionCallWithFrom.drl").rule();
0536:                final PatternDescr pattern = (PatternDescr) rule.getLhs()
0537:                        .getDescrs().get(0);
0538:                final FromDescr from = (FromDescr) pattern.getSource();
0539:                final AccessorDescr func = (AccessorDescr) from.getDataSource();
0540:
0541:                assertFalse(this .parser.getErrorMessages().toString(),
0542:                        this .parser.hasErrors());
0543:
0544:                assertEquals(
0545:                        "doIt( foo,bar,42,\"hello\",{ a => \"b\", \"something\" => 42, \"a\" => foo, x => {x=>y}},\"end\", [a, \"b\", 42] )",
0546:                        func.toString());
0547:            }
0548:
0549:            public void testSimpleAccessorWithFrom() throws Exception {
0550:
0551:                final RuleDescr rule = parseResource(
0552:                        "test_SimpleAccessorWithFrom.drl").rule();
0553:                final PatternDescr pattern = (PatternDescr) rule.getLhs()
0554:                        .getDescrs().get(0);
0555:                final FromDescr from = (FromDescr) pattern.getSource();
0556:                final AccessorDescr accessor = (AccessorDescr) from
0557:                        .getDataSource();
0558:
0559:                assertFalse(this .parser.getErrorMessages().toString(),
0560:                        this .parser.hasErrors());
0561:
0562:                assertNull(((FieldAccessDescr) accessor.getInvokers().get(0))
0563:                        .getArgument());
0564:
0565:                assertEquals("something.doIt", accessor.toString());
0566:            }
0567:
0568:            public void testSimpleAccessorAndArgWithFrom() throws Exception {
0569:
0570:                final RuleDescr rule = parseResource(
0571:                        "test_SimpleAccessorArgWithFrom.drl").rule();
0572:                final PatternDescr pattern = (PatternDescr) rule.getLhs()
0573:                        .getDescrs().get(0);
0574:                final FromDescr from = (FromDescr) pattern.getSource();
0575:                final AccessorDescr accessor = (AccessorDescr) from
0576:                        .getDataSource();
0577:
0578:                assertFalse(this .parser.getErrorMessages().toString(),
0579:                        this .parser.hasErrors());
0580:
0581:                assertNotNull(((FieldAccessDescr) accessor.getInvokers().get(0))
0582:                        .getArgument());
0583:
0584:                assertEquals("something.doIt[\"key\"]", accessor.toString());
0585:            }
0586:
0587:            public void testComplexChainedAcessor() throws Exception {
0588:                final RuleDescr rule = parseResource(
0589:                        "test_ComplexChainedCallWithFrom.drl").rule();
0590:                final PatternDescr pattern = (PatternDescr) rule.getLhs()
0591:                        .getDescrs().get(0);
0592:                final FromDescr from = (FromDescr) pattern.getSource();
0593:                final AccessorDescr accessor = (AccessorDescr) from
0594:                        .getDataSource();
0595:
0596:                assertFalse(this .parser.getErrorMessages().toString(),
0597:                        this .parser.hasErrors());
0598:
0599:                assertEquals(
0600:                        "doIt1( foo,bar,42,\"hello\",{ a => \"b\"}, [a, \"b\", 42] ).doIt2(bar, [a, \"b\", 42]).field[\"key\"]",
0601:                        accessor.toString());
0602:            }
0603:
0604:            //        public void testFrom() throws Exception {
0605:            //            final RuleDescr rule = parseResource( "from.drl" ).rule();
0606:            //
0607:            //            if(parser.hasErrors()) {
0608:            //                System.err.println(parser.getErrorMessages());
0609:            //            }
0610:            //            assertFalse(parser.hasErrors());
0611:            //
0612:            //            assertNotNull( rule );
0613:            //
0614:            //            assertEquals( "using_from",
0615:            //                          rule.getName() );
0616:            //
0617:            //            assertEquals(9, rule.getLhs().getDescrs().size());
0618:            //
0619:            //            FromDescr from = (FromDescr) rule.getLhs().getDescrs().get(0);
0620:            //
0621:            //            assertEquals(3, from.getLine());
0622:            //
0623:            //            assertEquals("Foo", from.getReturnedPattern().getObjectType());
0624:            //            assertTrue(from.getDataSource() instanceof FieldAccessDescr);
0625:            //            assertEquals("baz", ((FieldAccessDescr) from.getDataSource()).getFieldName());
0626:            //            assertEquals("bar", ((FieldAccessDescr) from.getDataSource()).getVariableName());
0627:            //
0628:            //
0629:            //            ArgumentValueDescr arg = null;
0630:            //
0631:            //            from = (FromDescr) rule.getLhs().getDescrs().get(1);
0632:            //            assertEquals("Foo", from.getReturnedPattern().getObjectType());
0633:            //            assertEquals(0, from.getReturnedPattern().getDescrs().size());
0634:            //            FieldAccessDescr fieldAccess = ( FieldAccessDescr ) from.getDataSource();
0635:            //            arg = ( ArgumentValueDescr ) fieldAccess.getArgument();
0636:            //            assertEquals(ArgumentValueDescr.STRING,  arg.getType() );
0637:            //
0638:            //            from = (FromDescr) rule.getLhs().getDescrs().get(2);
0639:            //            fieldAccess = ( FieldAccessDescr ) from.getDataSource();
0640:            //            arg = ( ArgumentValueDescr ) fieldAccess.getArgument();
0641:            //            assertEquals(ArgumentValueDescr.VARIABLE,  arg.getType() );
0642:            //
0643:            //            from = (FromDescr) rule.getLhs().getDescrs().get(3);
0644:            //            fieldAccess = ( FieldAccessDescr ) from.getDataSource();
0645:            //            arg = ( ArgumentValueDescr ) fieldAccess.getArgument();
0646:            //            assertEquals(ArgumentValueDescr.INTEGRAL,  arg.getType() );
0647:            //
0648:            //            from = (FromDescr) rule.getLhs().getDescrs().get(4);
0649:            //            assertEquals("Whee", from.getReturnedColumn().getObjectType());
0650:            //            assertEquals(1, from.getReturnedColumn().getDescrs().size());
0651:            //            assertTrue(from.getDataSource() instanceof FunctionCallDescr);
0652:            //            assertEquals("whee", ((FunctionCallDescr) from.getDataSource()).getName());
0653:            //            assertEquals(1, ((FunctionCallDescr) from.getDataSource()).getArguments().size());
0654:            //            arg = ( (ArgumentValueDescr )((FunctionCallDescr) from.getDataSource()).getArguments().get(0));
0655:            //            assertEquals("y", arg.getValue());
0656:            //            assertEquals(ArgumentValueDescr.STRING, arg.getType());
0657:            //
0658:            //            assertEquals(7, from.getLine());
0659:            //            assertEquals(7, from.getReturnedColumn().getLine());
0660:            //
0661:            //            from = (FromDescr) rule.getLhs().getDescrs().get(5);
0662:            //            assertEquals("Foo", from.getReturnedColumn().getObjectType());
0663:            //            assertEquals(1, from.getReturnedColumn().getDescrs().size());
0664:            //            assertEquals("f", from.getReturnedColumn().getIdentifier());
0665:            //            assertTrue(from.getDataSource() instanceof MethodAccessDescr);
0666:            //            assertEquals("bar", ((MethodAccessDescr) from.getDataSource()).getVariableName());
0667:            //            assertEquals("la", ((MethodAccessDescr) from.getDataSource()).getMethodName());
0668:            //            assertEquals(1, ((MethodAccessDescr) from.getDataSource()).getArguments().size());
0669:            //            arg = (ArgumentValueDescr) ((MethodAccessDescr) from.getDataSource()).getArguments().get(0);
0670:            //
0671:            //
0672:            //            assertEquals("x", arg.getValue());
0673:            //            assertEquals(ArgumentValueDescr.VARIABLE, arg.getType());
0674:            //
0675:            //            assertEqualsIgnoreWhitespace("whee();", ( String ) rule.getConsequence());
0676:            //
0677:            //            from = (FromDescr) rule.getLhs().getDescrs().get(6);
0678:            //            assertEquals("wa", ((FunctionCallDescr)from.getDataSource()).getName());
0679:            //
0680:            //            from = (FromDescr) rule.getLhs().getDescrs().get(7);
0681:            //            MethodAccessDescr meth = (MethodAccessDescr)from.getDataSource();
0682:            //            assertEquals("wa", meth.getMethodName());
0683:            //            assertEquals("la", meth.getVariableName());
0684:            //
0685:            //            arg = (ArgumentValueDescr) meth.getArguments().get(0);
0686:            //            assertEquals("42", arg.getValue());
0687:            //            assertEquals(ArgumentValueDescr.INTEGRAL, arg.getType());
0688:            //
0689:            //            arg = (ArgumentValueDescr) meth.getArguments().get(1);
0690:            //            assertEquals("42.42", arg.getValue());
0691:            //            assertEquals(ArgumentValueDescr.DECIMAL, arg.getType());
0692:            //
0693:            //            arg = (ArgumentValueDescr) meth.getArguments().get(2);
0694:            //            assertEquals("false", arg.getValue());
0695:            //            assertEquals(ArgumentValueDescr.BOOLEAN, arg.getType());
0696:            //
0697:            //            arg = (ArgumentValueDescr) meth.getArguments().get(3);
0698:            //            assertEquals("null", arg.getValue());
0699:            //            assertEquals(ArgumentValueDescr.NULL, arg.getType());
0700:            //
0701:            //            assertEquals("Bam", ((PatternDescr)rule.getLhs().getDescrs().get(8)).getObjectType());
0702:            //        }
0703:
0704:            public void testSimpleRule() throws Exception {
0705:                final RuleDescr rule = parseResource("simple_rule.drl").rule();
0706:
0707:                assertNotNull(rule);
0708:
0709:                assertEquals("simple_rule", rule.getName());
0710:
0711:                assertEquals(7, rule.getConsequenceLine());
0712:                assertEquals(2, rule.getConsequencePattern());
0713:
0714:                final AndDescr lhs = rule.getLhs();
0715:
0716:                assertNotNull(lhs);
0717:
0718:                assertEquals(3, lhs.getDescrs().size());
0719:
0720:                //System.err.println( lhs.getDescrs() );
0721:
0722:                // Check first pattern
0723:                final PatternDescr first = (PatternDescr) lhs.getDescrs()
0724:                        .get(0);
0725:                assertEquals("foo3", first.getIdentifier());
0726:                assertEquals("Bar", first.getObjectType());
0727:
0728:                assertEquals(1, first.getConstraint().getDescrs().size());
0729:
0730:                AndDescr fieldAnd = (AndDescr) first.getConstraint();
0731:                FieldConstraintDescr fld = (FieldConstraintDescr) fieldAnd
0732:                        .getDescrs().get(0);
0733:                LiteralRestrictionDescr constraint = (LiteralRestrictionDescr) fld
0734:                        .getRestrictions().get(0);
0735:
0736:                assertNotNull(constraint);
0737:
0738:                assertEquals("a", fld.getFieldName());
0739:                assertEquals("==", constraint.getEvaluator());
0740:                assertEquals("3", constraint.getText());
0741:
0742:                // Check second pattern
0743:                final PatternDescr second = (PatternDescr) lhs.getDescrs().get(
0744:                        1);
0745:                assertEquals("foo4", second.getIdentifier());
0746:                assertEquals("Bar", second.getObjectType());
0747:
0748:                //System.err.println( second.getDescrs() );
0749:
0750:                fieldAnd = (AndDescr) second.getConstraint();
0751:                assertEquals(2, fieldAnd.getDescrs().size());
0752:
0753:                final FieldBindingDescr fieldBindingDescr = (FieldBindingDescr) fieldAnd
0754:                        .getDescrs().get(0);
0755:                assertEquals("a", fieldBindingDescr.getFieldName());
0756:                assertEquals("a4", fieldBindingDescr.getIdentifier());
0757:
0758:                fld = (FieldConstraintDescr) fieldAnd.getDescrs().get(1);
0759:                constraint = (LiteralRestrictionDescr) fld.getRestrictions()
0760:                        .get(0);
0761:
0762:                assertNotNull(constraint);
0763:
0764:                assertEquals("a", fld.getFieldName());
0765:                assertEquals("==", constraint.getEvaluator());
0766:                assertEquals("4", constraint.getText());
0767:
0768:                // Check third pattern
0769:                final PatternDescr third = (PatternDescr) lhs.getDescrs()
0770:                        .get(2);
0771:                assertNull(third.getIdentifier());
0772:                assertEquals("Baz", third.getObjectType());
0773:
0774:                assertEqualsIgnoreWhitespace("if ( a == b ) { "
0775:                        + "  assert( foo3 );" + "} else {"
0776:                        + "  retract( foo4 );" + "}"
0777:                        + "  System.out.println( a4 );", (String) rule
0778:                        .getConsequence());
0779:
0780:                assertFalse(this .parser.hasErrors());
0781:            }
0782:
0783:            public void testRestrictionsMultiple() throws Exception {
0784:                final RuleDescr rule = parseResource("restrictions_test.drl")
0785:                        .rule();
0786:
0787:                assertFalse(this .parser.getErrors().toString(), this .parser
0788:                        .hasErrors());
0789:                assertNotNull(rule);
0790:
0791:                assertEqualsIgnoreWhitespace("consequence();", (String) rule
0792:                        .getConsequence());
0793:                assertEquals("simple_rule", rule.getName());
0794:                assertEquals(2, rule.getLhs().getDescrs().size());
0795:
0796:                //The first pattern, with 2 restrictions on a single field (plus a connective)
0797:                PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
0798:                        .get(0);
0799:                assertEquals("Person", pattern.getObjectType());
0800:                assertEquals(1, pattern.getConstraint().getDescrs().size());
0801:
0802:                AndDescr and = (AndDescr) pattern.getConstraint();
0803:                FieldConstraintDescr fld = (FieldConstraintDescr) and
0804:                        .getDescrs().get(0);
0805:                assertEquals(RestrictionConnectiveDescr.AND,
0806:                        ((RestrictionConnectiveDescr) fld.getRestriction())
0807:                                .getConnective());
0808:                assertEquals(2, fld.getRestrictions().size());
0809:                assertEquals("age", fld.getFieldName());
0810:
0811:                LiteralRestrictionDescr lit = (LiteralRestrictionDescr) fld
0812:                        .getRestrictions().get(0);
0813:                assertEquals(">", lit.getEvaluator());
0814:                assertEquals("30", lit.getText());
0815:
0816:                lit = (LiteralRestrictionDescr) fld.getRestrictions().get(1);
0817:                assertEquals("<", lit.getEvaluator());
0818:                assertEquals("40", lit.getText());
0819:
0820:                //the second col, with 2 fields, the first with 2 restrictions, the second field with one
0821:                pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
0822:                assertEquals("Vehicle", pattern.getObjectType());
0823:                assertEquals(2, pattern.getConstraint().getDescrs().size());
0824:
0825:                and = (AndDescr) pattern.getConstraint();
0826:                fld = (FieldConstraintDescr) and.getDescrs().get(0);
0827:                assertEquals("type", fld.getFieldName());
0828:                assertEquals(1, fld.getRestrictions().size());
0829:                RestrictionConnectiveDescr or = (RestrictionConnectiveDescr) fld
0830:                        .getRestrictions().get(0);
0831:                assertEquals(RestrictionConnectiveDescr.OR, or.getConnective());
0832:                assertEquals(2, or.getRestrictions().size());
0833:                lit = (LiteralRestrictionDescr) or.getRestrictions().get(0);
0834:                assertEquals("==", lit.getEvaluator());
0835:                assertEquals("sedan", lit.getText());
0836:
0837:                lit = (LiteralRestrictionDescr) or.getRestrictions().get(1);
0838:                assertEquals("==", lit.getEvaluator());
0839:                assertEquals("wagon", lit.getText());
0840:
0841:                //now the second field
0842:                fld = (FieldConstraintDescr) and.getDescrs().get(1);
0843:                assertEquals(1, fld.getRestrictions().size());
0844:                lit = (LiteralRestrictionDescr) fld.getRestrictions().get(0);
0845:                assertEquals("<", lit.getEvaluator());
0846:                assertEquals("3", lit.getText());
0847:
0848:            }
0849:
0850:            public void testLineNumberInAST() throws Exception {
0851:                //also see testSimpleExpander to see how this works with an expander (should be the same).
0852:
0853:                final RuleDescr rule = parseResource("simple_rule.drl").rule();
0854:
0855:                assertNotNull(rule);
0856:
0857:                assertEquals("simple_rule", rule.getName());
0858:
0859:                assertEquals(7, rule.getConsequenceLine());
0860:                assertEquals(2, rule.getConsequencePattern());
0861:
0862:                final AndDescr lhs = rule.getLhs();
0863:
0864:                assertNotNull(lhs);
0865:
0866:                assertEquals(3, lhs.getDescrs().size());
0867:
0868:                // Check first pattern
0869:                final PatternDescr first = (PatternDescr) lhs.getDescrs()
0870:                        .get(0);
0871:                assertEquals("foo3", first.getIdentifier());
0872:                assertEquals("Bar", first.getObjectType());
0873:                assertEquals(1, first.getConstraint().getDescrs().size());
0874:
0875:                // Check second pattern
0876:                final PatternDescr second = (PatternDescr) lhs.getDescrs().get(
0877:                        1);
0878:                assertEquals("foo4", second.getIdentifier());
0879:                assertEquals("Bar", second.getObjectType());
0880:
0881:                final PatternDescr third = (PatternDescr) lhs.getDescrs()
0882:                        .get(2);
0883:                assertEquals("Baz", third.getObjectType());
0884:
0885:                assertEquals(4, first.getLine());
0886:                assertEquals(5, second.getLine());
0887:                assertEquals(6, third.getLine());
0888:                assertFalse(this .parser.hasErrors());
0889:            }
0890:
0891:            public void testLineNumberIncludingCommentsInRHS() throws Exception {
0892:                parseResource("test_CommentLineNumbersInConsequence.drl")
0893:                        .compilation_unit();
0894:
0895:                assertFalse(this .parser.hasErrors());
0896:                final String rhs = (String) ((RuleDescr) this .parser
0897:                        .getPackageDescr().getRules().get(0)).getConsequence();
0898:                String expected = "  \t//woot\n  \tfirst\n  \t\n  \t//\n  \t\n  \t/* lala\n  \t\n  \t*/\n  \tsecond  \n";
0899:                assertEquals(expected, rhs);
0900:            }
0901:
0902:            public void testLhsSemicolonDelim() throws Exception {
0903:                final RuleDescr rule = parseResource("lhs_semicolon_delim.drl")
0904:                        .rule();
0905:
0906:                assertNotNull(rule);
0907:
0908:                assertEquals("simple_rule", rule.getName());
0909:
0910:                final AndDescr lhs = rule.getLhs();
0911:
0912:                assertNotNull(lhs);
0913:
0914:                assertEquals(3, lhs.getDescrs().size());
0915:
0916:                //System.err.println( lhs.getDescrs() );
0917:
0918:                // Check first pattern
0919:                final PatternDescr first = (PatternDescr) lhs.getDescrs()
0920:                        .get(0);
0921:                assertEquals("foo3", first.getIdentifier());
0922:                assertEquals("Bar", first.getObjectType());
0923:
0924:                assertEquals(1, first.getConstraint().getDescrs().size());
0925:
0926:                //LiteralDescr constraint = (LiteralDescr) first.getDescrs().get( 0 );
0927:                AndDescr and = (AndDescr) first.getConstraint();
0928:                FieldConstraintDescr fld = (FieldConstraintDescr) and
0929:                        .getDescrs().get(0);
0930:                LiteralRestrictionDescr constraint = (LiteralRestrictionDescr) fld
0931:                        .getRestrictions().get(0);
0932:
0933:                assertNotNull(constraint);
0934:
0935:                assertEquals("a", fld.getFieldName());
0936:                assertEquals("==", constraint.getEvaluator());
0937:                assertEquals("3", constraint.getText());
0938:
0939:                // Check second pattern
0940:                final PatternDescr second = (PatternDescr) lhs.getDescrs().get(
0941:                        1);
0942:                assertEquals("foo4", second.getIdentifier());
0943:                assertEquals("Bar", second.getObjectType());
0944:
0945:                and = (AndDescr) second.getConstraint();
0946:                assertEquals(2, and.getDescrs().size());
0947:
0948:                //System.err.println( second.getDescrs() );
0949:
0950:                final FieldBindingDescr fieldBindingDescr = (FieldBindingDescr) and
0951:                        .getDescrs().get(0);
0952:                assertEquals("a", fieldBindingDescr.getFieldName());
0953:                assertEquals("a4", fieldBindingDescr.getIdentifier());
0954:
0955:                fld = (FieldConstraintDescr) and.getDescrs().get(1);
0956:
0957:                constraint = (LiteralRestrictionDescr) fld.getRestrictions()
0958:                        .get(0);
0959:
0960:                assertNotNull(constraint);
0961:
0962:                assertEquals("a", fld.getFieldName());
0963:                assertEquals("==", constraint.getEvaluator());
0964:                assertEquals("4", constraint.getText());
0965:
0966:                // Check third pattern
0967:                final PatternDescr third = (PatternDescr) lhs.getDescrs()
0968:                        .get(2);
0969:                assertNull(third.getIdentifier());
0970:                assertEquals("Baz", third.getObjectType());
0971:
0972:                assertEqualsIgnoreWhitespace("if ( a == b ) { "
0973:                        + "  assert( foo3 );" + "} else {"
0974:                        + "  retract( foo4 );" + "}"
0975:                        + "  System.out.println( a4 );", (String) rule
0976:                        .getConsequence());
0977:
0978:                assertFalse(this .parser.hasErrors());
0979:            }
0980:
0981:            public void testNotNode() throws Exception {
0982:                final RuleDescr rule = parseResource("rule_not.drl").rule();
0983:
0984:                assertNotNull(rule);
0985:                assertEquals("simple_rule", rule.getName());
0986:
0987:                final AndDescr lhs = rule.getLhs();
0988:                assertEquals(1, lhs.getDescrs().size());
0989:                final NotDescr not = (NotDescr) lhs.getDescrs().get(0);
0990:                assertEquals(1, not.getDescrs().size());
0991:                final PatternDescr pattern = (PatternDescr) not.getDescrs()
0992:                        .get(0);
0993:
0994:                assertEquals("Cheese", pattern.getObjectType());
0995:                assertEquals(1, pattern.getConstraint().getDescrs().size());
0996:
0997:                final AndDescr and = (AndDescr) pattern.getConstraint();
0998:                final FieldConstraintDescr fld = (FieldConstraintDescr) and
0999:                        .getDescrs().get(0);
1000:                final LiteralRestrictionDescr lit = (LiteralRestrictionDescr) fld
1001:                        .getRestrictions().get(0);
1002:
1003:                assertEquals("==", lit.getEvaluator());
1004:                assertEquals("stilton", lit.getText());
1005:                assertEquals("type", fld.getFieldName());
1006:
1007:                assertFalse(this .parser.hasErrors());
1008:            }
1009:
1010:            public void testFunctionImport() throws Exception {
1011:                final DRLParser parser = parseResource("test_FunctionImport.drl");
1012:                parser.compilation_unit();
1013:                assertFalse(parser.hasErrors());
1014:
1015:                final PackageDescr pkg = parser.getPackageDescr();
1016:                assertEquals(2, pkg.getFunctionImports().size());
1017:
1018:                assertEquals("abd.def.x", ((FunctionImportDescr) pkg
1019:                        .getFunctionImports().get(0)).getTarget());
1020:                assertFalse(((FunctionImportDescr) pkg.getFunctionImports()
1021:                        .get(0)).getStartCharacter() == -1);
1022:                assertFalse(((FunctionImportDescr) pkg.getFunctionImports()
1023:                        .get(0)).getEndCharacter() == -1);
1024:                assertEquals("qed.wah.*", ((FunctionImportDescr) pkg
1025:                        .getFunctionImports().get(1)).getTarget());
1026:                assertFalse(((FunctionImportDescr) pkg.getFunctionImports()
1027:                        .get(1)).getStartCharacter() == -1);
1028:                assertFalse(((FunctionImportDescr) pkg.getFunctionImports()
1029:                        .get(1)).getEndCharacter() == -1);
1030:            }
1031:
1032:            public void testNotExistWithBrackets() throws Exception {
1033:
1034:                final DRLParser parser = parseResource("not_exist_with_brackets.drl");
1035:
1036:                parser.compilation_unit();
1037:                final PackageDescr pkg = parser.getPackageDescr();
1038:
1039:                final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
1040:
1041:                assertNotNull(rule);
1042:                assertEquals("simple_rule", rule.getName());
1043:
1044:                final AndDescr lhs = rule.getLhs();
1045:                assertEquals(2, lhs.getDescrs().size());
1046:                final NotDescr not = (NotDescr) lhs.getDescrs().get(0);
1047:                assertEquals(1, not.getDescrs().size());
1048:                final PatternDescr pattern = (PatternDescr) not.getDescrs()
1049:                        .get(0);
1050:
1051:                assertEquals("Cheese", pattern.getObjectType());
1052:
1053:                final ExistsDescr ex = (ExistsDescr) lhs.getDescrs().get(1);
1054:                assertEquals(1, ex.getDescrs().size());
1055:                final PatternDescr exPattern = (PatternDescr) ex.getDescrs()
1056:                        .get(0);
1057:                assertEquals("Foo", exPattern.getObjectType());
1058:
1059:                assertFalse(parser.hasErrors());
1060:            }
1061:
1062:            public void testNotBindindShouldBarf() throws Exception {
1063:                final DRLParser parser = parseResource("not_with_binding_error.drl");
1064:                parser.compilation_unit();
1065:                assertTrue(parser.hasErrors());
1066:            }
1067:
1068:            public void testSimpleQuery() throws Exception {
1069:                final QueryDescr query = parseResource("simple_query.drl")
1070:                        .query();
1071:
1072:                assertNotNull(query);
1073:
1074:                assertEquals("simple_query", query.getName());
1075:
1076:                final AndDescr lhs = query.getLhs();
1077:
1078:                assertNotNull(lhs);
1079:
1080:                assertEquals(3, lhs.getDescrs().size());
1081:
1082:                // Check first pattern
1083:                final PatternDescr first = (PatternDescr) lhs.getDescrs()
1084:                        .get(0);
1085:                assertEquals("foo3", first.getIdentifier());
1086:                assertEquals("Bar", first.getObjectType());
1087:
1088:                assertEquals(1, first.getConstraint().getDescrs().size());
1089:
1090:                AndDescr and = (AndDescr) first.getConstraint();
1091:                FieldConstraintDescr fld = (FieldConstraintDescr) and
1092:                        .getDescrs().get(0);
1093:                LiteralRestrictionDescr constraint = (LiteralRestrictionDescr) fld
1094:                        .getRestrictions().get(0);
1095:                //LiteralDescr constraint = (LiteralDescr) first.getDescrs().get( 0 );
1096:
1097:                assertNotNull(constraint);
1098:
1099:                assertEquals("a", fld.getFieldName());
1100:                assertEquals("==", constraint.getEvaluator());
1101:                assertEquals("3", constraint.getText());
1102:
1103:                // Check second pattern
1104:                final PatternDescr second = (PatternDescr) lhs.getDescrs().get(
1105:                        1);
1106:                assertEquals("foo4", second.getIdentifier());
1107:                assertEquals("Bar", second.getObjectType());
1108:
1109:                and = (AndDescr) second.getConstraint();
1110:                assertEquals(2, and.getDescrs().size());
1111:                //check it has field bindings.
1112:                final FieldBindingDescr fieldBindingDescr = (FieldBindingDescr) and
1113:                        .getDescrs().get(0);
1114:                assertEquals("a", fieldBindingDescr.getFieldName());
1115:                assertEquals("a4", fieldBindingDescr.getIdentifier());
1116:
1117:                fld = (FieldConstraintDescr) and.getDescrs().get(1);
1118:
1119:                constraint = (LiteralRestrictionDescr) fld.getRestrictions()
1120:                        .get(0);
1121:
1122:                assertNotNull(constraint);
1123:
1124:                assertEquals("a", fld.getFieldName());
1125:                assertEquals("==", constraint.getEvaluator());
1126:                assertEquals("4", constraint.getText());
1127:
1128:                assertFalse(this .parser.hasErrors());
1129:            }
1130:
1131:            public void testQueryRuleMixed() throws Exception {
1132:                final DRLParser parser = parseResource("query_and_rule.drl");
1133:                parser.compilation_unit();
1134:
1135:                final PackageDescr pack = parser.getPackageDescr();
1136:                assertEquals(4, pack.getRules().size()); //as queries are rules
1137:                RuleDescr rule = (RuleDescr) pack.getRules().get(0);
1138:                assertEquals("bar", rule.getName());
1139:
1140:                QueryDescr query = (QueryDescr) pack.getRules().get(1);
1141:                assertEquals("simple_query", query.getName());
1142:
1143:                rule = (RuleDescr) pack.getRules().get(2);
1144:                assertEquals("bar2", rule.getName());
1145:
1146:                query = (QueryDescr) pack.getRules().get(3);
1147:                assertEquals("simple_query2", query.getName());
1148:
1149:                assertFalse(parser.hasErrors());
1150:            }
1151:
1152:            public void testMultipleRules() throws Exception {
1153:                final DRLParser parser = parseResource("multiple_rules.drl");
1154:                parser.compilation_unit();
1155:
1156:                final PackageDescr pkg = parser.getPackageDescr();
1157:                final List rules = pkg.getRules();
1158:
1159:                assertEquals(2, rules.size());
1160:
1161:                final RuleDescr rule0 = (RuleDescr) rules.get(0);
1162:                assertEquals("Like Stilton", rule0.getName());
1163:
1164:                final RuleDescr rule1 = (RuleDescr) rules.get(1);
1165:                assertEquals("Like Cheddar", rule1.getName());
1166:
1167:                //checkout the first rule
1168:                AndDescr lhs = rule1.getLhs();
1169:                assertNotNull(lhs);
1170:                assertEquals(1, lhs.getDescrs().size());
1171:                assertEqualsIgnoreWhitespace(
1172:                        "System.out.println(\"I like \" + t);", (String) rule0
1173:                                .getConsequence());
1174:
1175:                // Check first pattern
1176:                PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
1177:                assertEquals("Cheese", first.getObjectType());
1178:
1179:                //checkout the second rule
1180:                lhs = rule1.getLhs();
1181:                assertNotNull(lhs);
1182:                assertEquals(1, lhs.getDescrs().size());
1183:                assertEqualsIgnoreWhitespace(
1184:                        "System.out.println(\"I like \" + t);", (String) rule1
1185:                                .getConsequence());
1186:
1187:                // Check first pattern
1188:                first = (PatternDescr) lhs.getDescrs().get(0);
1189:                assertEquals("Cheese", first.getObjectType());
1190:
1191:                assertFalse(parser.hasErrors());
1192:            }
1193:
1194:            public void testExpanderErrorsAfterExpansion() throws Exception {
1195:
1196:                final String name = "expander_post_errors.dslr";
1197:                final Expander expander = new DefaultExpander();
1198:                final String expanded = expander.expand(this .getReader(name));
1199:
1200:                final DRLParser parser = parse(name, expanded);
1201:                parser.compilation_unit();
1202:                assertTrue(parser.hasErrors());
1203:
1204:                final RecognitionException err = (RecognitionException) parser
1205:                        .getErrors().get(0);
1206:                assertEquals(1, parser.getErrors().size());
1207:
1208:                assertEquals(5, err.line);
1209:            }
1210:
1211:            public void testExpanderLineSpread() throws Exception {
1212:                final DrlParser parser = new DrlParser();
1213:                final PackageDescr pkg = parser.parse(this 
1214:                        .getReader("expander_spread_lines.dslr"), this 
1215:                        .getReader("complex.dsl"));
1216:
1217:                assertFalse(parser.getErrors().toString(), parser.hasErrors());
1218:
1219:                final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
1220:                assertEquals(1, rule.getLhs().getDescrs().size());
1221:
1222:                final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
1223:                assertEquals(2, or.getDescrs().size());
1224:                assertNotNull((String) rule.getConsequence());
1225:
1226:            }
1227:
1228:            public void testExpanderMultipleConstraints() throws Exception {
1229:                final DrlParser parser = new DrlParser();
1230:                final PackageDescr pkg = parser.parse(this 
1231:                        .getReader("expander_multiple_constraints.dslr"), this 
1232:                        .getReader("multiple_constraints.dsl"));
1233:
1234:                assertFalse(parser.getErrors().toString(), parser.hasErrors());
1235:
1236:                final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
1237:                assertEquals(2, rule.getLhs().getDescrs().size());
1238:
1239:                PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
1240:                        .get(0);
1241:                assertEquals("Person", pattern.getObjectType());
1242:
1243:                assertEquals(2, pattern.getConstraint().getDescrs().size());
1244:                assertEquals("age", ((FieldConstraintDescr) pattern
1245:                        .getConstraint().getDescrs().get(0)).getFieldName());
1246:                assertEquals("location", ((FieldConstraintDescr) pattern
1247:                        .getConstraint().getDescrs().get(1)).getFieldName());
1248:
1249:                pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
1250:                assertEquals("Bar", pattern.getObjectType());
1251:
1252:                assertNotNull((String) rule.getConsequence());
1253:
1254:            }
1255:
1256:            public void testExpanderMultipleConstraintsFlush() throws Exception {
1257:                final DrlParser parser = new DrlParser();
1258:                //this is similar to the other test, but it requires a flush to add the constraints
1259:                final PackageDescr pkg = parser.parse(this 
1260:                        .getReader("expander_multiple_constraints_flush.dslr"),
1261:                        this .getReader("multiple_constraints.dsl"));
1262:
1263:                assertFalse(parser.getErrors().toString(), parser.hasErrors());
1264:
1265:                final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
1266:                assertEquals(1, rule.getLhs().getDescrs().size());
1267:
1268:                final PatternDescr pattern = (PatternDescr) rule.getLhs()
1269:                        .getDescrs().get(0);
1270:                assertEquals("Person", pattern.getObjectType());
1271:
1272:                assertEquals(2, pattern.getConstraint().getDescrs().size());
1273:                assertEquals("age", ((FieldConstraintDescr) pattern
1274:                        .getConstraint().getDescrs().get(0)).getFieldName());
1275:                assertEquals("location", ((FieldConstraintDescr) pattern
1276:                        .getConstraint().getDescrs().get(1)).getFieldName());
1277:
1278:                assertNotNull((String) rule.getConsequence());
1279:
1280:            }
1281:
1282:            //    public void testExpanderUnExpandableErrorLines() throws Exception {
1283:            //
1284:            //        //stubb expander
1285:            //        final ExpanderResolver res = new ExpanderResolver() {
1286:            //            public Expander get(String name,
1287:            //                                String config) {
1288:            //                return new Expander() {
1289:            //                    public String expand(String scope,
1290:            //                                         String pattern) {
1291:            //                        if ( pattern.startsWith( "Good" ) ) {
1292:            //                            return pattern;
1293:            //                        } else {
1294:            //                            throw new IllegalArgumentException( "whoops" );
1295:            //                        }
1296:            //
1297:            //                    }
1298:            //                };
1299:            //            }
1300:            //        };
1301:            //
1302:            //        final DRLParser parser = parseResource( "expander_line_errors.dslr" );
1303:            //        parser.setExpanderResolver( res );
1304:            //        parser.compilation_unit();
1305:            //        assertTrue( parser.hasErrors() );
1306:            //
1307:            //        final List messages = parser.getErrorMessages();
1308:            //        assertEquals( messages.size(),
1309:            //                      parser.getErrors().size() );
1310:            //
1311:            //        assertEquals( 4,
1312:            //                      parser.getErrors().size() );
1313:            //        assertEquals( ExpanderException.class,
1314:            //                      parser.getErrors().get( 0 ).getClass() );
1315:            //        assertEquals( 8,
1316:            //                      ((RecognitionException) parser.getErrors().get( 0 )).line );
1317:            //        assertEquals( 10,
1318:            //                      ((RecognitionException) parser.getErrors().get( 1 )).line );
1319:            //        assertEquals( 12,
1320:            //                      ((RecognitionException) parser.getErrors().get( 2 )).line );
1321:            //        assertEquals( 13,
1322:            //                      ((RecognitionException) parser.getErrors().get( 3 )).line );
1323:            //
1324:            //        final PackageDescr pack = parser.getPackageDescr();
1325:            //        assertNotNull( pack );
1326:            //
1327:            //        final ExpanderException ex = (ExpanderException) parser.getErrors().get( 0 );
1328:            //        assertTrue( ex.getMessage().indexOf( "whoops" ) > -1 );
1329:            //
1330:            //    }
1331:
1332:            public void testBasicBinding() throws Exception {
1333:                final DRLParser parser = parseResource("basic_binding.drl");
1334:                parser.compilation_unit();
1335:
1336:                final PackageDescr pkg = parser.getPackageDescr();
1337:                final RuleDescr ruleDescr = (RuleDescr) pkg.getRules().get(0);
1338:
1339:                final AndDescr lhs = ruleDescr.getLhs();
1340:                assertEquals(1, lhs.getDescrs().size());
1341:                final PatternDescr cheese = (PatternDescr) lhs.getDescrs().get(
1342:                        0);
1343:                assertEquals(1, cheese.getConstraint().getDescrs().size());
1344:                assertEquals("Cheese", cheese.getObjectType());
1345:                assertEquals(1, lhs.getDescrs().size());
1346:                final FieldBindingDescr fieldBinding = (FieldBindingDescr) cheese
1347:                        .getConstraint().getDescrs().get(0);
1348:                assertEquals("type", fieldBinding.getFieldName());
1349:
1350:                assertFalse(parser.hasErrors());
1351:            }
1352:
1353:            public void testBoundVariables() throws Exception {
1354:                final DRLParser parser = parseResource("bindings.drl");
1355:                parser.compilation_unit();
1356:
1357:                final PackageDescr pkg = parser.getPackageDescr();
1358:                final RuleDescr ruleDescr = (RuleDescr) pkg.getRules().get(0);
1359:
1360:                final AndDescr lhs = ruleDescr.getLhs();
1361:                assertEquals(2, lhs.getDescrs().size());
1362:                final PatternDescr cheese = (PatternDescr) lhs.getDescrs().get(
1363:                        0);
1364:                assertEquals("Cheese", cheese.getObjectType());
1365:                assertEquals(2, lhs.getDescrs().size());
1366:                FieldBindingDescr fieldBinding = (FieldBindingDescr) cheese
1367:                        .getConstraint().getDescrs().get(0);
1368:                assertEquals("type", fieldBinding.getFieldName());
1369:
1370:                FieldConstraintDescr fld = (FieldConstraintDescr) cheese
1371:                        .getConstraint().getDescrs().get(1);
1372:                LiteralRestrictionDescr literalDescr = (LiteralRestrictionDescr) fld
1373:                        .getRestrictions().get(0);
1374:                //LiteralDescr literalDescr = (LiteralDescr) cheese.getDescrs().get( 1 );
1375:                assertEquals("type", fld.getFieldName());
1376:                assertEquals("==", literalDescr.getEvaluator());
1377:                assertEquals("stilton", literalDescr.getText());
1378:
1379:                final PatternDescr person = (PatternDescr) lhs.getDescrs().get(
1380:                        1);
1381:                fieldBinding = (FieldBindingDescr) person.getConstraint()
1382:                        .getDescrs().get(0);
1383:                assertEquals("name", fieldBinding.getFieldName());
1384:
1385:                fld = (FieldConstraintDescr) person.getConstraint().getDescrs()
1386:                        .get(1);
1387:                literalDescr = (LiteralRestrictionDescr) fld.getRestrictions()
1388:                        .get(0);
1389:
1390:                assertEquals("name", fld.getFieldName());
1391:                assertEquals("==", literalDescr.getEvaluator());
1392:                assertEquals("bob", literalDescr.getText());
1393:
1394:                fld = (FieldConstraintDescr) person.getConstraint().getDescrs()
1395:                        .get(2);
1396:                final VariableRestrictionDescr variableDescr = (VariableRestrictionDescr) fld
1397:                        .getRestrictions().get(0);
1398:
1399:                assertEquals("likes", fld.getFieldName());
1400:                assertEquals("==", variableDescr.getEvaluator());
1401:                assertEquals("$type", variableDescr.getIdentifier());
1402:
1403:                assertFalse(parser.hasErrors());
1404:            }
1405:
1406:            public void testOrNesting() throws Exception {
1407:                final DRLParser parser = parseResource("or_nesting.drl");
1408:                parser.compilation_unit();
1409:
1410:                final PackageDescr pack = parser.getPackageDescr();
1411:                assertNotNull(pack);
1412:                assertEquals(1, pack.getRules().size());
1413:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
1414:                assertEquals("simple_rule", rule.getName());
1415:
1416:                assertEquals(1, rule.getLhs().getDescrs().size());
1417:
1418:                final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
1419:                assertEquals(2, or.getDescrs().size());
1420:
1421:                final PatternDescr first = (PatternDescr) or.getDescrs().get(0);
1422:                assertEquals("Person", first.getObjectType());
1423:
1424:                final AndDescr and = (AndDescr) or.getDescrs().get(1);
1425:                assertEquals(2, and.getDescrs().size());
1426:
1427:                final PatternDescr left = (PatternDescr) and.getDescrs().get(0);
1428:                assertEquals("Person", left.getObjectType());
1429:
1430:                final PatternDescr right = (PatternDescr) and.getDescrs()
1431:                        .get(1);
1432:                assertEquals("Cheese", right.getObjectType());
1433:            }
1434:
1435:            /** Test that explicit "&&", "||" works as expected */
1436:            public void testAndOrRules() throws Exception {
1437:                final DRLParser parser = parseResource("and_or_rule.drl");
1438:                parser.compilation_unit();
1439:
1440:                final PackageDescr pack = parser.getPackageDescr();
1441:                assertNotNull(pack);
1442:                assertEquals(1, pack.getRules().size());
1443:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
1444:                assertEquals("simple_rule", rule.getName());
1445:
1446:                //we will have 2 children under the main And node
1447:                final AndDescr and = rule.getLhs();
1448:                assertEquals(2, and.getDescrs().size());
1449:
1450:                //check the "&&" part
1451:                final AndDescr join = (AndDescr) and.getDescrs().get(0);
1452:                assertEquals(2, join.getDescrs().size());
1453:
1454:                PatternDescr left = (PatternDescr) join.getDescrs().get(0);
1455:                PatternDescr right = (PatternDescr) join.getDescrs().get(1);
1456:                assertEquals("Person", left.getObjectType());
1457:                assertEquals("Cheese", right.getObjectType());
1458:
1459:                assertEquals(1, left.getConstraint().getDescrs().size());
1460:
1461:                FieldConstraintDescr fld = (FieldConstraintDescr) left
1462:                        .getConstraint().getDescrs().get(0);
1463:                LiteralRestrictionDescr literal = (LiteralRestrictionDescr) fld
1464:                        .getRestrictions().get(0);
1465:
1466:                assertEquals("==", literal.getEvaluator());
1467:                assertEquals("name", fld.getFieldName());
1468:                assertEquals("mark", literal.getText());
1469:
1470:                assertEquals(1, right.getConstraint().getDescrs().size());
1471:
1472:                fld = (FieldConstraintDescr) right.getConstraint().getDescrs()
1473:                        .get(0);
1474:                literal = (LiteralRestrictionDescr) fld.getRestrictions()
1475:                        .get(0);
1476:
1477:                assertEquals("==", literal.getEvaluator());
1478:                assertEquals("type", fld.getFieldName());
1479:                assertEquals("stilton", literal.getText());
1480:
1481:                //now the "||" part
1482:                final OrDescr or = (OrDescr) and.getDescrs().get(1);
1483:                assertEquals(2, or.getDescrs().size());
1484:                left = (PatternDescr) or.getDescrs().get(0);
1485:                right = (PatternDescr) or.getDescrs().get(1);
1486:                assertEquals("Person", left.getObjectType());
1487:                assertEquals("Cheese", right.getObjectType());
1488:                assertEquals(1, left.getConstraint().getDescrs().size());
1489:
1490:                fld = (FieldConstraintDescr) left.getConstraint().getDescrs()
1491:                        .get(0);
1492:                literal = (LiteralRestrictionDescr) fld.getRestrictions()
1493:                        .get(0);
1494:
1495:                assertEquals("==", literal.getEvaluator());
1496:                assertEquals("name", fld.getFieldName());
1497:                assertEquals("mark", literal.getText());
1498:
1499:                assertEquals(1, right.getConstraint().getDescrs().size());
1500:
1501:                fld = (FieldConstraintDescr) right.getConstraint().getDescrs()
1502:                        .get(0);
1503:                literal = (LiteralRestrictionDescr) fld.getRestrictions()
1504:                        .get(0);
1505:
1506:                assertEquals("==", literal.getEvaluator());
1507:                assertEquals("type", fld.getFieldName());
1508:                assertEquals("stilton", literal.getText());
1509:
1510:                assertEqualsIgnoreWhitespace(
1511:                        "System.out.println( \"Mark and Michael\" );",
1512:                        (String) rule.getConsequence());
1513:
1514:                assertFalse(parser.hasErrors());
1515:            }
1516:
1517:            /** test basic foo : Fact() || Fact() stuff */
1518:            public void testOrWithBinding() throws Exception {
1519:                final DRLParser parser = parseResource("or_binding.drl");
1520:                parser.compilation_unit();
1521:
1522:                final PackageDescr pack = parser.getPackageDescr();
1523:                assertEquals(1, pack.getRules().size());
1524:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
1525:                assertEquals(2, rule.getLhs().getDescrs().size());
1526:
1527:                final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
1528:                assertEquals(2, or.getDescrs().size());
1529:
1530:                final PatternDescr leftPattern = (PatternDescr) or.getDescrs()
1531:                        .get(0);
1532:                assertEquals("Person", leftPattern.getObjectType());
1533:                assertEquals("foo", leftPattern.getIdentifier());
1534:
1535:                final PatternDescr rightPattern = (PatternDescr) or.getDescrs()
1536:                        .get(1);
1537:                assertEquals("Person", rightPattern.getObjectType());
1538:                assertEquals("foo", rightPattern.getIdentifier());
1539:
1540:                final PatternDescr cheeseDescr = (PatternDescr) rule.getLhs()
1541:                        .getDescrs().get(1);
1542:                assertEquals("Cheese", cheeseDescr.getObjectType());
1543:                assertEquals(null, cheeseDescr.getIdentifier());
1544:
1545:                assertEqualsIgnoreWhitespace(
1546:                        "System.out.println( \"Mark and Michael\" + bar );",
1547:                        (String) rule.getConsequence());
1548:
1549:                assertFalse(parser.hasErrors());
1550:            }
1551:
1552:            /** test basic foo : Fact() || Fact() stuff binding to an "or"*/
1553:            public void testOrBindingComplex() throws Exception {
1554:                final DRLParser parser = parseResource("or_binding_complex.drl");
1555:                parser.compilation_unit();
1556:
1557:                final PackageDescr pack = parser.getPackageDescr();
1558:                assertEquals(1, pack.getRules().size());
1559:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
1560:                assertEquals(1, rule.getLhs().getDescrs().size());
1561:
1562:                assertEquals(1, rule.getLhs().getDescrs().size());
1563:
1564:                final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
1565:                assertEquals(2, or.getDescrs().size());
1566:
1567:                //first fact
1568:                final PatternDescr firstFact = (PatternDescr) or.getDescrs()
1569:                        .get(0);
1570:                assertEquals("Person", firstFact.getObjectType());
1571:                assertEquals("foo", firstFact.getIdentifier());
1572:
1573:                //second "option"
1574:                final PatternDescr secondFact = (PatternDescr) or.getDescrs()
1575:                        .get(1);
1576:                assertEquals("Person", secondFact.getObjectType());
1577:                assertEquals(1, secondFact.getConstraint().getDescrs().size());
1578:                assertEquals("foo", secondFact.getIdentifier());
1579:
1580:                assertEqualsIgnoreWhitespace(
1581:                        "System.out.println( \"Mark and Michael\" + bar );",
1582:                        (String) rule.getConsequence());
1583:
1584:                assertFalse(parser.hasErrors());
1585:            }
1586:
1587:            public void testOrBindingWithBrackets() throws Exception {
1588:                final DRLParser parser = parseResource("or_binding_with_brackets.drl");
1589:                parser.compilation_unit();
1590:
1591:                final PackageDescr pack = parser.getPackageDescr();
1592:                assertEquals(1, pack.getRules().size());
1593:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
1594:                assertEquals(1, rule.getLhs().getDescrs().size());
1595:
1596:                assertEquals(1, rule.getLhs().getDescrs().size());
1597:
1598:                final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
1599:                assertEquals(2, or.getDescrs().size());
1600:
1601:                //first fact
1602:                final PatternDescr firstFact = (PatternDescr) or.getDescrs()
1603:                        .get(0);
1604:                assertEquals("Person", firstFact.getObjectType());
1605:                assertEquals("foo", firstFact.getIdentifier());
1606:
1607:                //second "option"
1608:                final PatternDescr secondFact = (PatternDescr) or.getDescrs()
1609:                        .get(0);
1610:                assertEquals("Person", secondFact.getObjectType());
1611:                assertEquals("foo", secondFact.getIdentifier());
1612:
1613:                assertEqualsIgnoreWhitespace(
1614:                        "System.out.println( \"Mark and Michael\" + bar );",
1615:                        (String) rule.getConsequence());
1616:
1617:                assertFalse(parser.hasErrors());
1618:            }
1619:
1620:            /** */
1621:            public void testBracketsPrecedence() throws Exception {
1622:                final DRLParser parser = parseResource("brackets_precedence.drl");
1623:                parser.compilation_unit();
1624:
1625:                final PackageDescr pack = parser.getPackageDescr();
1626:                assertEquals(1, pack.getRules().size());
1627:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
1628:
1629:                assertEquals(1, rule.getLhs().getDescrs().size());
1630:
1631:                final AndDescr rootAnd = (AndDescr) rule.getLhs().getDescrs()
1632:                        .get(0);
1633:
1634:                assertEquals(2, rootAnd.getDescrs().size());
1635:
1636:                final OrDescr leftOr = (OrDescr) rootAnd.getDescrs().get(0);
1637:
1638:                assertEquals(2, leftOr.getDescrs().size());
1639:                final NotDescr not = (NotDescr) leftOr.getDescrs().get(0);
1640:                final PatternDescr foo1 = (PatternDescr) not.getDescrs().get(0);
1641:                assertEquals("Foo", foo1.getObjectType());
1642:                final PatternDescr foo2 = (PatternDescr) leftOr.getDescrs()
1643:                        .get(1);
1644:                assertEquals("Foo", foo2.getObjectType());
1645:
1646:                final OrDescr rightOr = (OrDescr) rootAnd.getDescrs().get(1);
1647:
1648:                assertEquals(2, rightOr.getDescrs().size());
1649:                final PatternDescr shoes = (PatternDescr) rightOr.getDescrs()
1650:                        .get(0);
1651:                assertEquals("Shoes", shoes.getObjectType());
1652:                final PatternDescr butt = (PatternDescr) rightOr.getDescrs()
1653:                        .get(1);
1654:                assertEquals("Butt", butt.getObjectType());
1655:
1656:                assertFalse(parser.hasErrors());
1657:            }
1658:
1659:            public void testEvalMultiple() throws Exception {
1660:                final DRLParser parser = parseResource("eval_multiple.drl");
1661:                parser.compilation_unit();
1662:
1663:                assertFalse(parser.getErrorMessages().toString(), parser
1664:                        .hasErrors());
1665:
1666:                final PackageDescr pack = parser.getPackageDescr();
1667:                assertEquals(1, pack.getRules().size());
1668:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
1669:                assertEquals(4, rule.getLhs().getDescrs().size());
1670:
1671:                final EvalDescr eval = (EvalDescr) rule.getLhs().getDescrs()
1672:                        .get(0);
1673:                assertEqualsIgnoreWhitespace("abc(\"foo\") + 5", (String) eval
1674:                        .getContent());
1675:
1676:                final PatternDescr pattern = (PatternDescr) rule.getLhs()
1677:                        .getDescrs().get(1);
1678:                assertEquals("Foo", pattern.getObjectType());
1679:
1680:            }
1681:
1682:            public void testWithEval() throws Exception {
1683:                final DRLParser parser = parseResource("with_eval.drl");
1684:                parser.compilation_unit();
1685:
1686:                final PackageDescr pack = parser.getPackageDescr();
1687:                assertEquals(1, pack.getRules().size());
1688:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
1689:                assertEquals(3, rule.getLhs().getDescrs().size());
1690:                PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
1691:                        .get(0);
1692:                assertEquals("Foo", pattern.getObjectType());
1693:                pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
1694:                assertEquals("Bar", pattern.getObjectType());
1695:
1696:                final EvalDescr eval = (EvalDescr) rule.getLhs().getDescrs()
1697:                        .get(2);
1698:                assertEqualsIgnoreWhitespace("abc(\"foo\")", (String) eval
1699:                        .getContent());
1700:                assertEqualsIgnoreWhitespace("Kapow", (String) rule
1701:                        .getConsequence());
1702:
1703:                assertFalse(parser.hasErrors());
1704:            }
1705:
1706:            public void testWithRetval() throws Exception {
1707:                final DRLParser parser = parseResource("with_retval.drl");
1708:                parser.compilation_unit();
1709:
1710:                final PackageDescr pack = parser.getPackageDescr();
1711:                assertEquals(1, pack.getRules().size());
1712:
1713:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
1714:                assertEquals(1, rule.getLhs().getDescrs().size());
1715:                final PatternDescr col = (PatternDescr) rule.getLhs()
1716:                        .getDescrs().get(0);
1717:                assertEquals(1, col.getConstraint().getDescrs().size());
1718:                assertEquals("Foo", col.getObjectType());
1719:                final FieldConstraintDescr fld = (FieldConstraintDescr) col
1720:                        .getConstraint().getDescrs().get(0);
1721:                final ReturnValueRestrictionDescr retval = (ReturnValueRestrictionDescr) fld
1722:                        .getRestrictions().get(0);
1723:
1724:                assertEquals("a + b", retval.getContent());
1725:                assertEquals("name", fld.getFieldName());
1726:                assertEquals("==", retval.getEvaluator());
1727:
1728:                assertFalse(parser.hasErrors());
1729:            }
1730:
1731:            public void testWithPredicate() throws Exception {
1732:                final DRLParser parser = parseResource("with_predicate.drl");
1733:                parser.compilation_unit();
1734:
1735:                final PackageDescr pack = parser.getPackageDescr();
1736:                assertEquals(1, pack.getRules().size());
1737:
1738:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
1739:                assertEquals(1, rule.getLhs().getDescrs().size());
1740:                final PatternDescr col = (PatternDescr) rule.getLhs()
1741:                        .getDescrs().get(0);
1742:                AndDescr and = (AndDescr) col.getConstraint();
1743:                assertEquals(2, and.getDescrs().size());
1744:
1745:                final FieldBindingDescr field = (FieldBindingDescr) and
1746:                        .getDescrs().get(0);
1747:                final PredicateDescr pred = (PredicateDescr) and.getDescrs()
1748:                        .get(1);
1749:                assertEquals("age", field.getFieldName());
1750:                assertEquals("$age2", field.getIdentifier());
1751:                assertEqualsIgnoreWhitespace("$age2 == $age1+2", (String) pred
1752:                        .getContent());
1753:
1754:                assertFalse(parser.getErrorMessages().toString(), parser
1755:                        .hasErrors());
1756:            }
1757:
1758:            public void testNotWithConstraint() throws Exception {
1759:                final DRLParser parser = parseResource("not_with_constraint.drl");
1760:                parser.compilation_unit();
1761:
1762:                final PackageDescr pack = parser.getPackageDescr();
1763:                assertEquals(1, pack.getRules().size());
1764:
1765:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
1766:                assertEquals(2, rule.getLhs().getDescrs().size());
1767:
1768:                PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
1769:                        .get(0);
1770:                final FieldBindingDescr fieldBinding = (FieldBindingDescr) pattern
1771:                        .getConstraint().getDescrs().get(0);
1772:                assertEquals("$likes", fieldBinding.getIdentifier());
1773:
1774:                final NotDescr not = (NotDescr) rule.getLhs().getDescrs()
1775:                        .get(1);
1776:                pattern = (PatternDescr) not.getDescrs().get(0);
1777:
1778:                final FieldConstraintDescr fld = (FieldConstraintDescr) pattern
1779:                        .getConstraint().getDescrs().get(0);
1780:                final VariableRestrictionDescr boundVariable = (VariableRestrictionDescr) fld
1781:                        .getRestrictions().get(0);
1782:
1783:                assertEquals("type", fld.getFieldName());
1784:                assertEquals("==", boundVariable.getEvaluator());
1785:                assertEquals("$likes", boundVariable.getIdentifier());
1786:
1787:                assertFalse(parser.hasErrors());
1788:            }
1789:
1790:            public void testGlobal() throws Exception {
1791:                final DRLParser parser = parseResource("globals.drl");
1792:                parser.compilation_unit();
1793:
1794:                final PackageDescr pack = parser.getPackageDescr();
1795:                assertEquals(1, pack.getRules().size());
1796:
1797:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
1798:                assertEquals(1, rule.getLhs().getDescrs().size());
1799:
1800:                assertEquals(1, pack.getImports().size());
1801:                assertEquals(2, pack.getGlobals().size());
1802:
1803:                final GlobalDescr foo = (GlobalDescr) pack.getGlobals().get(0);
1804:                assertEquals("java.lang.String", foo.getType());
1805:                assertEquals("foo", foo.getIdentifier());
1806:                final GlobalDescr bar = (GlobalDescr) pack.getGlobals().get(1);
1807:                assertEquals("java.lang.Integer", bar.getType());
1808:                assertEquals("bar", bar.getIdentifier());
1809:
1810:                assertFalse(parser.hasErrors());
1811:            }
1812:
1813:            public void testFunctions() throws Exception {
1814:                final DRLParser parser = parseResource("functions.drl");
1815:                parser.compilation_unit();
1816:
1817:                final PackageDescr pack = parser.getPackageDescr();
1818:                assertEquals(2, pack.getRules().size());
1819:
1820:                final List functions = pack.getFunctions();
1821:                assertEquals(2, functions.size());
1822:
1823:                FunctionDescr func = (FunctionDescr) functions.get(0);
1824:                assertEquals("functionA", func.getName());
1825:                assertEquals("String", func.getReturnType());
1826:                assertEquals(2, func.getParameterNames().size());
1827:                assertEquals(2, func.getParameterTypes().size());
1828:                assertEquals(4, func.getLine());
1829:                assertEquals(0, func.getColumn());
1830:
1831:                assertEquals("String", func.getParameterTypes().get(0));
1832:                assertEquals("s", func.getParameterNames().get(0));
1833:
1834:                assertEquals("Integer", func.getParameterTypes().get(1));
1835:                assertEquals("i", func.getParameterNames().get(1));
1836:
1837:                assertEqualsIgnoreWhitespace("foo();", func.getText());
1838:
1839:                func = (FunctionDescr) functions.get(1);
1840:                assertEquals("functionB", func.getName());
1841:                assertEqualsIgnoreWhitespace("bar();", func.getText());
1842:
1843:                assertFalse(parser.hasErrors());
1844:            }
1845:
1846:            public void testComment() throws Exception {
1847:                final DRLParser parser = parseResource("comment.drl");
1848:                parser.compilation_unit();
1849:
1850:                final PackageDescr pack = parser.getPackageDescr();
1851:                assertNotNull(pack);
1852:
1853:                assertEquals("foo.bar", pack.getName());
1854:
1855:                assertFalse(parser.getErrorMessages().toString(), parser
1856:                        .hasErrors());
1857:            }
1858:
1859:            public void testAttributes() throws Exception {
1860:                final RuleDescr rule = parseResource("rule_attributes.drl")
1861:                        .rule();
1862:                assertEquals("simple_rule", rule.getName());
1863:                assertEqualsIgnoreWhitespace("bar();", (String) rule
1864:                        .getConsequence());
1865:
1866:                final List attrs = rule.getAttributes();
1867:                assertEquals(6, attrs.size());
1868:
1869:                AttributeDescr at = (AttributeDescr) attrs.get(0);
1870:                assertEquals("salience", at.getName());
1871:                assertEquals("42", at.getValue());
1872:
1873:                at = (AttributeDescr) attrs.get(1);
1874:                assertEquals("agenda-group", at.getName());
1875:                assertEquals("my_group", at.getValue());
1876:
1877:                at = (AttributeDescr) attrs.get(2);
1878:                assertEquals("no-loop", at.getName());
1879:                assertEquals("true", at.getValue());
1880:
1881:                at = (AttributeDescr) attrs.get(3);
1882:                assertEquals("duration", at.getName());
1883:                assertEquals("42", at.getValue());
1884:
1885:                at = (AttributeDescr) attrs.get(4);
1886:                assertEquals("activation-group", at.getName());
1887:                assertEquals("my_activation_group", at.getValue());
1888:
1889:                at = (AttributeDescr) attrs.get(5);
1890:                assertEquals("lock-on-active", at.getName());
1891:                assertEquals("true", at.getValue());
1892:
1893:                assertFalse(this .parser.hasErrors());
1894:            }
1895:
1896:            public void testAttributes_alternateSyntax() throws Exception {
1897:                final RuleDescr rule = parseResource("rule_attributes_alt.drl")
1898:                        .rule();
1899:                assertEquals("simple_rule", rule.getName());
1900:                assertEqualsIgnoreWhitespace("bar();", (String) rule
1901:                        .getConsequence());
1902:
1903:                final List attrs = rule.getAttributes();
1904:                assertEquals(6, attrs.size());
1905:
1906:                AttributeDescr at = (AttributeDescr) attrs.get(0);
1907:                assertEquals("salience", at.getName());
1908:                assertEquals("42", at.getValue());
1909:
1910:                at = (AttributeDescr) attrs.get(1);
1911:                assertEquals("agenda-group", at.getName());
1912:                assertEquals("my_group", at.getValue());
1913:
1914:                at = (AttributeDescr) attrs.get(2);
1915:                assertEquals("no-loop", at.getName());
1916:                assertEquals("true", at.getValue());
1917:
1918:                at = (AttributeDescr) attrs.get(3);
1919:                assertEquals("lock-on-active", at.getName());
1920:                assertEquals("true", at.getValue());
1921:
1922:                at = (AttributeDescr) attrs.get(4);
1923:                assertEquals("duration", at.getName());
1924:                assertEquals("42", at.getValue());
1925:
1926:                at = (AttributeDescr) attrs.get(5);
1927:                assertEquals("activation-group", at.getName());
1928:                assertEquals("my_activation_group", at.getValue());
1929:
1930:                assertFalse(this .parser.hasErrors());
1931:            }
1932:
1933:            public void testEnumeration() throws Exception {
1934:                final RuleDescr rule = parseResource("enumeration.drl").rule();
1935:                assertEquals("simple_rule", rule.getName());
1936:                assertEquals(1, rule.getLhs().getDescrs().size());
1937:                final PatternDescr col = (PatternDescr) rule.getLhs()
1938:                        .getDescrs().get(0);
1939:                assertEquals("Foo", col.getObjectType());
1940:                assertEquals(1, col.getConstraint().getDescrs().size());
1941:                final FieldConstraintDescr fld = (FieldConstraintDescr) col
1942:                        .getConstraint().getDescrs().get(0);
1943:                final QualifiedIdentifierRestrictionDescr lit = (QualifiedIdentifierRestrictionDescr) fld
1944:                        .getRestrictions().get(0);
1945:
1946:                assertEquals("bar", fld.getFieldName());
1947:                assertEquals("==", lit.getEvaluator());
1948:                assertEquals("Foo.BAR", lit.getText());
1949:
1950:                assertFalse(this .parser.hasErrors());
1951:            }
1952:
1953:            public void testInvalidSyntax_Catches() throws Exception {
1954:                parseResource("invalid_syntax.drl").compilation_unit();
1955:                assertTrue(this .parser.hasErrors());
1956:            }
1957:
1958:            public void testMultipleErrors() throws Exception {
1959:                parseResource("multiple_errors.drl").compilation_unit();
1960:                assertTrue(this .parser.hasErrors());
1961:                assertEquals(2, this .parser.getErrors().size());
1962:            }
1963:
1964:            public void testExtraLhsNewline() throws Exception {
1965:                parseResource("extra_lhs_newline.drl").compilation_unit();
1966:                assertFalse(this .parser.hasErrors());
1967:            }
1968:
1969:            public void testPackageGarbage() throws Exception {
1970:
1971:                parseResource("package_garbage.drl").compilation_unit();
1972:                assertTrue(this .parser.hasErrors());
1973:            }
1974:
1975:            public void testPackageAttributes() throws Exception {
1976:                parseResource("package_attributes.drl").compilation_unit();
1977:                assertFalse(this .parser.getErrorMessages().toString(),
1978:                        this .parser.hasErrors());
1979:
1980:                PackageDescr pkg = this .parser.getPackageDescr();
1981:                AttributeDescr at = (AttributeDescr) pkg.getAttributes().get(0);
1982:                assertEquals("agenda-group", at.getName());
1983:                assertEquals("x", at.getValue());
1984:                at = (AttributeDescr) pkg.getAttributes().get(1);
1985:                assertEquals("dialect", at.getName());
1986:                assertEquals("java", at.getValue());
1987:
1988:                assertEquals(2, pkg.getRules().size());
1989:
1990:                assertEquals(2, pkg.getImports().size());
1991:
1992:                RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
1993:                assertEquals("bar", rule.getName());
1994:                at = (AttributeDescr) rule.getAttributes().get(0);
1995:                assertEquals("agenda-group", at.getName());
1996:                assertEquals("x", at.getValue());
1997:                at = (AttributeDescr) rule.getAttributes().get(1);
1998:                assertEquals("dialect", at.getName());
1999:                assertEquals("java", at.getValue());
2000:
2001:                rule = (RuleDescr) pkg.getRules().get(1);
2002:                assertEquals("baz", rule.getName());
2003:                at = (AttributeDescr) rule.getAttributes().get(0);
2004:                assertEquals("dialect", at.getName());
2005:                assertEquals("mvel", at.getValue());
2006:                at = (AttributeDescr) rule.getAttributes().get(1);
2007:                assertEquals("agenda-group", at.getName());
2008:                assertEquals("x", at.getValue());
2009:
2010:            }
2011:
2012:            public void testStatementOrdering1() throws Exception {
2013:                parseResource("statement_ordering_1.drl");
2014:                this .parser.compilation_unit();
2015:
2016:                final PackageDescr pkg = this .parser.getPackageDescr();
2017:
2018:                assertEquals(2, pkg.getRules().size());
2019:
2020:                assertEquals("foo", ((RuleDescr) pkg.getRules().get(0))
2021:                        .getName());
2022:                assertEquals("bar", ((RuleDescr) pkg.getRules().get(1))
2023:                        .getName());
2024:
2025:                assertEquals(2, pkg.getFunctions().size());
2026:
2027:                assertEquals("cheeseIt", ((FunctionDescr) pkg.getFunctions()
2028:                        .get(0)).getName());
2029:                assertEquals("uncheeseIt", ((FunctionDescr) pkg.getFunctions()
2030:                        .get(1)).getName());
2031:
2032:                assertEquals(4, pkg.getImports().size());
2033:                assertEquals("im.one", ((ImportDescr) pkg.getImports().get(0))
2034:                        .getTarget());
2035:                assertEquals("im.two", ((ImportDescr) pkg.getImports().get(1))
2036:                        .getTarget());
2037:                assertEquals("im.three",
2038:                        ((ImportDescr) pkg.getImports().get(2)).getTarget());
2039:                assertEquals("im.four", ((ImportDescr) pkg.getImports().get(3))
2040:                        .getTarget());
2041:
2042:                assertFalse(this .parser.hasErrors());
2043:            }
2044:
2045:            public void testRuleNamesStartingWithNumbers() throws Exception {
2046:                parseResource("rule_names_number_prefix.drl")
2047:                        .compilation_unit();
2048:
2049:                assertFalse(this .parser.getErrors().toString(), this .parser
2050:                        .hasErrors());
2051:
2052:                final PackageDescr pkg = this .parser.getPackageDescr();
2053:
2054:                assertEquals(2, pkg.getRules().size());
2055:
2056:                assertEquals("1. Do Stuff!",
2057:                        ((RuleDescr) pkg.getRules().get(0)).getName());
2058:                assertEquals("2. Do More Stuff!", ((RuleDescr) pkg.getRules()
2059:                        .get(1)).getName());
2060:            }
2061:
2062:            public void testEvalWithNewline() throws Exception {
2063:                parseResource("eval_with_newline.drl").compilation_unit();
2064:
2065:                if (this .parser.hasErrors()) {
2066:                    System.err.println(this .parser.getErrorMessages());
2067:                }
2068:                assertFalse(this .parser.hasErrors());
2069:            }
2070:
2071:            public void testEvalWithSemicolon() throws Exception {
2072:                parseResource("eval_with_semicolon.drl").compilation_unit();
2073:
2074:                assertTrue(this .parser.hasErrors());
2075:                assertEquals(1, this .parser.getErrorMessages().size());
2076:                assertTrue(((String) this .parser.getErrorMessages().get(0))
2077:                        .indexOf("Trailing semi-colon not allowed") >= 0);
2078:            }
2079:
2080:            public void testEndPosition() throws Exception {
2081:                parseResource("test_EndPosition.drl").compilation_unit();
2082:                final RuleDescr rule = (RuleDescr) this .parser
2083:                        .getPackageDescr().getRules().get(0);
2084:                final PatternDescr col = (PatternDescr) rule.getLhs()
2085:                        .getDescrs().get(0);
2086:                assertEquals(6, col.getLine());
2087:
2088:                assertEquals(8, col.getEndLine());
2089:
2090:            }
2091:
2092:            public void testQualifiedClassname() throws Exception {
2093:                parseResource("qualified_classname.drl").compilation_unit();
2094:
2095:                assertFalse(this .parser.hasErrors());
2096:
2097:                final PackageDescr pkg = this .parser.getPackageDescr();
2098:                final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
2099:
2100:                final PatternDescr p = (PatternDescr) rule.getLhs().getDescrs()
2101:                        .get(0);
2102:
2103:                assertEquals("com.cheeseco.Cheese", p.getObjectType());
2104:            }
2105:
2106:            public void testAccumulate() throws Exception {
2107:                final DRLParser parser = parseResource("accumulate.drl");
2108:                parser.compilation_unit();
2109:
2110:                if (parser.hasErrors()) {
2111:                    System.err.println(parser.getErrorMessages());
2112:                }
2113:
2114:                assertFalse(parser.getErrorMessages().toString(), parser
2115:                        .hasErrors());
2116:
2117:                final PackageDescr pack = parser.getPackageDescr();
2118:                assertEquals(1, pack.getRules().size());
2119:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
2120:                assertEquals(1, rule.getLhs().getDescrs().size());
2121:
2122:                final PatternDescr outPattern = (PatternDescr) rule.getLhs()
2123:                        .getDescrs().get(0);
2124:                final AccumulateDescr accum = (AccumulateDescr) outPattern
2125:                        .getSource();
2126:                assertEqualsIgnoreWhitespace("int x = 0 ;", accum.getInitCode());
2127:                assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
2128:                assertNull(accum.getReverseCode());
2129:                assertEqualsIgnoreWhitespace("new Integer(x)", accum
2130:                        .getResultCode());
2131:
2132:                assertFalse(accum.isExternalFunction());
2133:
2134:                final PatternDescr pattern = (PatternDescr) accum
2135:                        .getInputPattern();
2136:                assertEquals("Person", pattern.getObjectType());
2137:            }
2138:
2139:            public void testAccumulateWithBindings() throws Exception {
2140:                final DRLParser parser = parseResource("accumulate_with_bindings.drl");
2141:                parser.compilation_unit();
2142:
2143:                assertFalse(parser.getErrorMessages().toString(), parser
2144:                        .hasErrors());
2145:
2146:                final PackageDescr pack = parser.getPackageDescr();
2147:                assertEquals(1, pack.getRules().size());
2148:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
2149:                assertEquals(1, rule.getLhs().getDescrs().size());
2150:
2151:                final PatternDescr outPattern = (PatternDescr) rule.getLhs()
2152:                        .getDescrs().get(0);
2153:                final AccumulateDescr accum = (AccumulateDescr) outPattern
2154:                        .getSource();
2155:                assertEqualsIgnoreWhitespace("$counter", outPattern
2156:                        .getIdentifier());
2157:                assertEqualsIgnoreWhitespace("int x = 0 ;", accum.getInitCode());
2158:                assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
2159:                assertEqualsIgnoreWhitespace("new Integer(x)", accum
2160:                        .getResultCode());
2161:
2162:                final PatternDescr pattern = (PatternDescr) accum
2163:                        .getInputPattern();
2164:                assertEquals("Person", pattern.getObjectType());
2165:            }
2166:
2167:            public void testCollect() throws Exception {
2168:                final DRLParser parser = parseResource("collect.drl");
2169:                parser.compilation_unit();
2170:
2171:                if (parser.hasErrors()) {
2172:                    System.err.println(parser.getErrorMessages());
2173:                }
2174:
2175:                assertFalse(parser.hasErrors());
2176:
2177:                final PackageDescr pack = parser.getPackageDescr();
2178:                assertEquals(1, pack.getRules().size());
2179:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
2180:                assertEquals(1, rule.getLhs().getDescrs().size());
2181:
2182:                final PatternDescr outPattern = (PatternDescr) rule.getLhs()
2183:                        .getDescrs().get(0);
2184:                final CollectDescr collect = (CollectDescr) outPattern
2185:                        .getSource();
2186:
2187:                final PatternDescr pattern = (PatternDescr) collect
2188:                        .getInputPattern();
2189:                assertEquals("Person", pattern.getObjectType());
2190:            }
2191:
2192:            public void testPredicate() throws Exception {
2193:                final PatternDescr pattern = new PatternDescr();
2194:                parse("$var : attr -> ( $var.equals(\"xyz\") )").constraints(
2195:                        pattern);
2196:
2197:                assertFalse(this .parser.getErrorMessages().toString(),
2198:                        this .parser.hasErrors());
2199:
2200:                final List constraints = pattern.getConstraint().getDescrs();
2201:                assertEquals(2, constraints.size());
2202:
2203:                final FieldBindingDescr field = (FieldBindingDescr) constraints
2204:                        .get(0);
2205:                final PredicateDescr predicate = (PredicateDescr) constraints
2206:                        .get(1);
2207:                assertEquals("$var", field.getIdentifier());
2208:                assertEquals("attr", field.getFieldName());
2209:                assertEquals(" $var.equals(\"xyz\") ", predicate.getContent());
2210:            }
2211:
2212:            public void testPredicate2() throws Exception {
2213:                final PatternDescr pattern = new PatternDescr();
2214:                // predicates are also prefixed by the eval keyword
2215:                parse("eval( $var.equals(\"xyz\") )").constraints(pattern);
2216:
2217:                assertFalse(this .parser.getErrorMessages().toString(),
2218:                        this .parser.hasErrors());
2219:
2220:                final List constraints = pattern.getConstraint().getDescrs();
2221:                assertEquals(1, constraints.size());
2222:
2223:                final PredicateDescr predicate = (PredicateDescr) constraints
2224:                        .get(0);
2225:                assertEquals(" $var.equals(\"xyz\") ", predicate.getContent());
2226:            }
2227:
2228:            public void testEscapedStrings() throws Exception {
2229:                final RuleDescr rule = parseResource("escaped-string.drl")
2230:                        .rule();
2231:
2232:                assertNotNull(rule);
2233:
2234:                assertEquals("test_Quotes", rule.getName());
2235:
2236:                final String expected = "String s = \"\\\"\\n\\t\\\\\";";
2237:
2238:                assertEqualsIgnoreWhitespace(expected, (String) rule
2239:                        .getConsequence());
2240:
2241:                assertFalse(this .parser.getErrorMessages().toString(),
2242:                        this .parser.hasErrors());
2243:            }
2244:
2245:            public void testNestedCEs() throws Exception {
2246:                final RuleDescr rule = parseResource(
2247:                        "nested_conditional_elements.drl").rule();
2248:
2249:                assertFalse(this .parser.getErrorMessages().toString(),
2250:                        this .parser.hasErrors());
2251:
2252:                assertNotNull(rule);
2253:
2254:                final AndDescr root = rule.getLhs();
2255:                final NotDescr not1 = (NotDescr) root.getDescrs().get(0);
2256:                final AndDescr and1 = (AndDescr) not1.getDescrs().get(0);
2257:
2258:                final PatternDescr state = (PatternDescr) and1.getDescrs().get(
2259:                        0);
2260:                final NotDescr not2 = (NotDescr) and1.getDescrs().get(1);
2261:                final AndDescr and2 = (AndDescr) not2.getDescrs().get(0);
2262:                final PatternDescr person = (PatternDescr) and2.getDescrs()
2263:                        .get(0);
2264:                final PatternDescr cheese = (PatternDescr) and2.getDescrs()
2265:                        .get(1);
2266:
2267:                final PatternDescr person2 = (PatternDescr) root.getDescrs()
2268:                        .get(1);
2269:                final OrDescr or = (OrDescr) root.getDescrs().get(2);
2270:                final PatternDescr cheese2 = (PatternDescr) or.getDescrs().get(
2271:                        0);
2272:                final PatternDescr cheese3 = (PatternDescr) or.getDescrs().get(
2273:                        1);
2274:
2275:                assertEquals(state.getObjectType(), "State");
2276:                assertEquals(person.getObjectType(), "Person");
2277:                assertEquals(cheese.getObjectType(), "Cheese");
2278:                assertEquals(person2.getObjectType(), "Person");
2279:                assertEquals(cheese2.getObjectType(), "Cheese");
2280:                assertEquals(cheese3.getObjectType(), "Cheese");
2281:            }
2282:
2283:            public void testForall() throws Exception {
2284:                final DRLParser parser = parseResource("forall.drl");
2285:                parser.compilation_unit();
2286:
2287:                assertFalse(parser.getErrorMessages().toString(), parser
2288:                        .hasErrors());
2289:
2290:                final PackageDescr pack = parser.getPackageDescr();
2291:                assertEquals(1, pack.getRules().size());
2292:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
2293:                assertEquals(1, rule.getLhs().getDescrs().size());
2294:
2295:                final ForallDescr forall = (ForallDescr) rule.getLhs()
2296:                        .getDescrs().get(0);
2297:
2298:                assertEquals(2, forall.getDescrs().size());
2299:                final PatternDescr pattern = forall.getBasePattern();
2300:                assertEquals("Person", pattern.getObjectType());
2301:                final List remaining = forall.getRemainingPatterns();
2302:                assertEquals(1, remaining.size());
2303:                final PatternDescr cheese = (PatternDescr) remaining.get(0);
2304:                assertEquals("Cheese", cheese.getObjectType());
2305:            }
2306:
2307:            public void testMemberof() throws Exception {
2308:                final String text = "Country( $cities : city )\nPerson( city memberOf $cities )\n";
2309:                final AndDescr descrs = new AndDescr();
2310:                final CharStream charStream = new ANTLRStringStream(text);
2311:                final DRLLexer lexer = new DRLLexer(charStream);
2312:                final TokenStream tokenStream = new CommonTokenStream(lexer);
2313:                final DRLParser parser = new DRLParser(tokenStream);
2314:                parser.setLineOffset(descrs.getLine());
2315:                parser.normal_lhs_block(descrs);
2316:                if (parser.hasErrors()) {
2317:                    System.err.println(parser.getErrorMessages());
2318:                }
2319:                assertFalse(parser.hasErrors());
2320:
2321:                assertEquals(2, descrs.getDescrs().size());
2322:                PatternDescr pat = (PatternDescr) descrs.getDescrs().get(1);
2323:                FieldConstraintDescr fieldConstr = (FieldConstraintDescr) pat
2324:                        .getConstraint().getDescrs().get(0);
2325:                VariableRestrictionDescr restr = (VariableRestrictionDescr) fieldConstr
2326:                        .getRestrictions().get(0);
2327:
2328:                assertEquals("memberOf", restr.getEvaluator());
2329:                assertEquals("$cities", restr.getIdentifier());
2330:            }
2331:
2332:            public void testNotMemberof() throws Exception {
2333:                final String text = "Country( $cities : city )\nPerson( city not memberOf $cities )\n";
2334:                final AndDescr descrs = new AndDescr();
2335:                final CharStream charStream = new ANTLRStringStream(text);
2336:                final DRLLexer lexer = new DRLLexer(charStream);
2337:                final TokenStream tokenStream = new CommonTokenStream(lexer);
2338:                final DRLParser parser = new DRLParser(tokenStream);
2339:                parser.setLineOffset(descrs.getLine());
2340:                parser.normal_lhs_block(descrs);
2341:                if (parser.hasErrors()) {
2342:                    System.err.println(parser.getErrorMessages());
2343:                }
2344:                assertFalse(parser.hasErrors());
2345:
2346:                assertEquals(2, descrs.getDescrs().size());
2347:                PatternDescr pat = (PatternDescr) descrs.getDescrs().get(1);
2348:                FieldConstraintDescr fieldConstr = (FieldConstraintDescr) pat
2349:                        .getConstraint().getDescrs().get(0);
2350:                VariableRestrictionDescr restr = (VariableRestrictionDescr) fieldConstr
2351:                        .getRestrictions().get(0);
2352:
2353:                assertEquals("not memberOf", restr.getEvaluator());
2354:                assertEquals("$cities", restr.getIdentifier());
2355:            }
2356:
2357:            public void testInOperator() throws Exception {
2358:                final RuleDescr rule = parseResource("in_operator_test.drl")
2359:                        .rule();
2360:
2361:                assertFalse(this .parser.getErrors().toString(), this .parser
2362:                        .hasErrors());
2363:                assertNotNull(rule);
2364:
2365:                assertEqualsIgnoreWhitespace("consequence();", (String) rule
2366:                        .getConsequence());
2367:                assertEquals("simple_rule", rule.getName());
2368:                assertEquals(2, rule.getLhs().getDescrs().size());
2369:
2370:                //The first pattern, with 2 restrictions on a single field (plus a connective)
2371:                PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
2372:                        .get(0);
2373:                assertEquals("Person", pattern.getObjectType());
2374:                assertEquals(1, pattern.getConstraint().getDescrs().size());
2375:
2376:                FieldConstraintDescr fld = (FieldConstraintDescr) pattern
2377:                        .getConstraint().getDescrs().get(0);
2378:                assertEquals(RestrictionConnectiveDescr.AND, fld
2379:                        .getRestriction().getConnective());
2380:                assertEquals(2, fld.getRestrictions().size());
2381:                assertEquals("age", fld.getFieldName());
2382:
2383:                LiteralRestrictionDescr lit = (LiteralRestrictionDescr) fld
2384:                        .getRestrictions().get(0);
2385:                assertEquals(">", lit.getEvaluator());
2386:                assertEquals("30", lit.getText());
2387:
2388:                lit = (LiteralRestrictionDescr) fld.getRestrictions().get(1);
2389:                assertEquals("<", lit.getEvaluator());
2390:                assertEquals("40", lit.getText());
2391:
2392:                //the second col, with 2 fields, the first with 2 restrictions, the second field with one
2393:                pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
2394:                assertEquals("Vehicle", pattern.getObjectType());
2395:                assertEquals(2, pattern.getConstraint().getDescrs().size());
2396:
2397:                fld = (FieldConstraintDescr) pattern.getConstraint()
2398:                        .getDescrs().get(0);
2399:                assertEquals("type", fld.getFieldName());
2400:                assertEquals(1, fld.getRestrictions().size());
2401:
2402:                RestrictionConnectiveDescr or = (RestrictionConnectiveDescr) fld
2403:                        .getRestrictions().get(0);
2404:                assertEquals(RestrictionConnectiveDescr.OR, or.getConnective());
2405:                assertEquals(2, or.getRestrictions().size());
2406:
2407:                lit = (LiteralRestrictionDescr) or.getRestrictions().get(0);
2408:                assertEquals("==", lit.getEvaluator());
2409:                assertEquals("sedan", lit.getText());
2410:
2411:                lit = (LiteralRestrictionDescr) or.getRestrictions().get(1);
2412:                assertEquals("==", lit.getEvaluator());
2413:                assertEquals("wagon", lit.getText());
2414:
2415:                //now the second field
2416:                fld = (FieldConstraintDescr) pattern.getConstraint()
2417:                        .getDescrs().get(1);
2418:                assertEquals(1, fld.getRestrictions().size());
2419:                lit = (LiteralRestrictionDescr) fld.getRestrictions().get(0);
2420:                assertEquals("<", lit.getEvaluator());
2421:                assertEquals("3", lit.getText());
2422:
2423:            }
2424:
2425:            public void testNotInOperator() throws Exception {
2426:                final RuleDescr rule = parseResource("notin_operator_test.drl")
2427:                        .rule();
2428:
2429:                assertFalse(this .parser.getErrors().toString(), this .parser
2430:                        .hasErrors());
2431:                assertNotNull(rule);
2432:
2433:                assertEqualsIgnoreWhitespace("consequence();", (String) rule
2434:                        .getConsequence());
2435:                assertEquals("simple_rule", rule.getName());
2436:                assertEquals(2, rule.getLhs().getDescrs().size());
2437:
2438:                //The first pattern, with 2 restrictions on a single field (plus a connective)
2439:                PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs()
2440:                        .get(0);
2441:                assertEquals("Person", pattern.getObjectType());
2442:                assertEquals(1, pattern.getConstraint().getDescrs().size());
2443:
2444:                FieldConstraintDescr fld = (FieldConstraintDescr) pattern
2445:                        .getConstraint().getDescrs().get(0);
2446:                assertEquals(2, fld.getRestrictions().size());
2447:                assertEquals("age", fld.getFieldName());
2448:
2449:                LiteralRestrictionDescr lit = (LiteralRestrictionDescr) fld
2450:                        .getRestrictions().get(0);
2451:                assertEquals(">", lit.getEvaluator());
2452:                assertEquals("30", lit.getText());
2453:
2454:                lit = (LiteralRestrictionDescr) fld.getRestrictions().get(1);
2455:                assertEquals("<", lit.getEvaluator());
2456:                assertEquals("40", lit.getText());
2457:
2458:                //the second col, with 2 fields, the first with 2 restrictions, the second field with one
2459:                pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
2460:                assertEquals("Vehicle", pattern.getObjectType());
2461:                assertEquals(2, pattern.getConstraint().getDescrs().size());
2462:
2463:                fld = (FieldConstraintDescr) pattern.getConstraint()
2464:                        .getDescrs().get(0);
2465:                assertEquals(2, fld.getRestrictions().size());
2466:                lit = (LiteralRestrictionDescr) fld.getRestrictions().get(0);
2467:                assertEquals("type", fld.getFieldName());
2468:                assertEquals("!=", lit.getEvaluator());
2469:                assertEquals("sedan", lit.getText());
2470:
2471:                lit = (LiteralRestrictionDescr) fld.getRestrictions().get(1);
2472:                assertEquals("!=", lit.getEvaluator());
2473:                assertEquals("wagon", lit.getText());
2474:
2475:                //now the second field
2476:                fld = (FieldConstraintDescr) pattern.getConstraint()
2477:                        .getDescrs().get(1);
2478:                assertEquals(1, fld.getRestrictions().size());
2479:                lit = (LiteralRestrictionDescr) fld.getRestrictions().get(0);
2480:                assertEquals("<", lit.getEvaluator());
2481:                assertEquals("3", lit.getText());
2482:
2483:            }
2484:
2485:            public void testConstraintAndConnective() throws Exception {
2486:                final String text = "Person( age < 42 && location==\"atlanta\")";
2487:                final CharStream charStream = new ANTLRStringStream(text);
2488:                final DRLLexer lexer = new DRLLexer(charStream);
2489:                final TokenStream tokenStream = new CommonTokenStream(lexer);
2490:                final DRLParser parser = new DRLParser(tokenStream);
2491:
2492:                PatternDescr pattern = (PatternDescr) parser.fact(null);
2493:                assertFalse(parser.getErrorMessages().toString(), parser
2494:                        .hasErrors());
2495:
2496:                assertEquals(2, pattern.getDescrs().size());
2497:                FieldConstraintDescr fcd = (FieldConstraintDescr) pattern
2498:                        .getDescrs().get(0);
2499:                assertEquals("age", fcd.getFieldName());
2500:                fcd = (FieldConstraintDescr) pattern.getDescrs().get(1);
2501:                assertEquals("location", fcd.getFieldName());
2502:            }
2503:
2504:            public void testConstraintOrConnective() throws Exception {
2505:                final String text = "Person( age < 42 || location==\"atlanta\")";
2506:                final CharStream charStream = new ANTLRStringStream(text);
2507:                final DRLLexer lexer = new DRLLexer(charStream);
2508:                final TokenStream tokenStream = new CommonTokenStream(lexer);
2509:                final DRLParser parser = new DRLParser(tokenStream);
2510:
2511:                PatternDescr pattern = (PatternDescr) parser.fact(null);
2512:                assertFalse(parser.getErrorMessages().toString(), parser
2513:                        .hasErrors());
2514:
2515:                assertEquals(1, pattern.getDescrs().size());
2516:                OrDescr or = (OrDescr) pattern.getDescrs().get(0);
2517:                assertEquals(2, or.getDescrs().size());
2518:                FieldConstraintDescr fcd = (FieldConstraintDescr) or
2519:                        .getDescrs().get(0);
2520:                assertEquals("age", fcd.getFieldName());
2521:                fcd = (FieldConstraintDescr) or.getDescrs().get(1);
2522:                assertEquals("location", fcd.getFieldName());
2523:            }
2524:
2525:            public void testConstraintConnectivesPrecedence() throws Exception {
2526:                final String text = "Person( age < 42 && location==\"atlanta\" || age > 20 && location==\"Seatle\" || location == \"Chicago\")";
2527:                final CharStream charStream = new ANTLRStringStream(text);
2528:                final DRLLexer lexer = new DRLLexer(charStream);
2529:                final TokenStream tokenStream = new CommonTokenStream(lexer);
2530:                final DRLParser parser = new DRLParser(tokenStream);
2531:
2532:                PatternDescr pattern = (PatternDescr) parser.fact(null);
2533:                assertFalse(parser.getErrorMessages().toString(), parser
2534:                        .hasErrors());
2535:
2536:                assertEquals(1, pattern.getDescrs().size());
2537:                OrDescr or = (OrDescr) pattern.getDescrs().get(0);
2538:                assertEquals(3, or.getDescrs().size());
2539:
2540:                AndDescr and = (AndDescr) or.getDescrs().get(0);
2541:                assertEquals(2, and.getDescrs().size());
2542:                FieldConstraintDescr fcd = (FieldConstraintDescr) and
2543:                        .getDescrs().get(0);
2544:                assertEquals("age", fcd.getFieldName());
2545:                assertEquals("<", ((LiteralRestrictionDescr) fcd
2546:                        .getRestrictions().get(0)).getEvaluator());
2547:                assertEquals("42", ((LiteralRestrictionDescr) fcd
2548:                        .getRestrictions().get(0)).getText());
2549:                fcd = (FieldConstraintDescr) and.getDescrs().get(1);
2550:                assertEquals("location", fcd.getFieldName());
2551:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2552:                        .getRestrictions().get(0)).getEvaluator());
2553:                assertEquals("atlanta", ((LiteralRestrictionDescr) fcd
2554:                        .getRestrictions().get(0)).getText());
2555:
2556:                and = (AndDescr) or.getDescrs().get(1);
2557:                assertEquals(2, and.getDescrs().size());
2558:                fcd = (FieldConstraintDescr) and.getDescrs().get(0);
2559:                assertEquals("age", fcd.getFieldName());
2560:                assertEquals(">", ((LiteralRestrictionDescr) fcd
2561:                        .getRestrictions().get(0)).getEvaluator());
2562:                assertEquals("20", ((LiteralRestrictionDescr) fcd
2563:                        .getRestrictions().get(0)).getText());
2564:                fcd = (FieldConstraintDescr) and.getDescrs().get(1);
2565:                assertEquals("location", fcd.getFieldName());
2566:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2567:                        .getRestrictions().get(0)).getEvaluator());
2568:                assertEquals("Seatle", ((LiteralRestrictionDescr) fcd
2569:                        .getRestrictions().get(0)).getText());
2570:
2571:                fcd = (FieldConstraintDescr) or.getDescrs().get(2);
2572:                assertEquals("location", fcd.getFieldName());
2573:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2574:                        .getRestrictions().get(0)).getEvaluator());
2575:                assertEquals("Chicago", ((LiteralRestrictionDescr) fcd
2576:                        .getRestrictions().get(0)).getText());
2577:
2578:            }
2579:
2580:            public void testConstraintConnectivesPrecedenceWithBracks()
2581:                    throws Exception {
2582:                final String text = "Person( age < 42 && ( location==\"atlanta\" || age > 20 && location==\"Seatle\") || location == \"Chicago\")";
2583:                final CharStream charStream = new ANTLRStringStream(text);
2584:                final DRLLexer lexer = new DRLLexer(charStream);
2585:                final TokenStream tokenStream = new CommonTokenStream(lexer);
2586:                final DRLParser parser = new DRLParser(tokenStream);
2587:
2588:                PatternDescr pattern = (PatternDescr) parser.fact(null);
2589:                assertFalse(parser.getErrorMessages().toString(), parser
2590:                        .hasErrors());
2591:
2592:                assertEquals(1, pattern.getDescrs().size());
2593:                OrDescr or1 = (OrDescr) pattern.getDescrs().get(0);
2594:                assertEquals(2, or1.getDescrs().size());
2595:
2596:                AndDescr and1 = (AndDescr) or1.getDescrs().get(0);
2597:                assertEquals(2, and1.getDescrs().size());
2598:                FieldConstraintDescr fcd = (FieldConstraintDescr) and1
2599:                        .getDescrs().get(0);
2600:                assertEquals("age", fcd.getFieldName());
2601:                assertEquals("<", ((LiteralRestrictionDescr) fcd
2602:                        .getRestrictions().get(0)).getEvaluator());
2603:                assertEquals("42", ((LiteralRestrictionDescr) fcd
2604:                        .getRestrictions().get(0)).getText());
2605:
2606:                OrDescr or2 = (OrDescr) and1.getDescrs().get(1);
2607:                fcd = (FieldConstraintDescr) or2.getDescrs().get(0);
2608:                assertEquals("location", fcd.getFieldName());
2609:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2610:                        .getRestrictions().get(0)).getEvaluator());
2611:                assertEquals("atlanta", ((LiteralRestrictionDescr) fcd
2612:                        .getRestrictions().get(0)).getText());
2613:
2614:                AndDescr and2 = (AndDescr) or2.getDescrs().get(1);
2615:                assertEquals(2, and2.getDescrs().size());
2616:                fcd = (FieldConstraintDescr) and2.getDescrs().get(0);
2617:                assertEquals("age", fcd.getFieldName());
2618:                assertEquals(">", ((LiteralRestrictionDescr) fcd
2619:                        .getRestrictions().get(0)).getEvaluator());
2620:                assertEquals("20", ((LiteralRestrictionDescr) fcd
2621:                        .getRestrictions().get(0)).getText());
2622:                fcd = (FieldConstraintDescr) and2.getDescrs().get(1);
2623:                assertEquals("location", fcd.getFieldName());
2624:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2625:                        .getRestrictions().get(0)).getEvaluator());
2626:                assertEquals("Seatle", ((LiteralRestrictionDescr) fcd
2627:                        .getRestrictions().get(0)).getText());
2628:
2629:                fcd = (FieldConstraintDescr) or1.getDescrs().get(1);
2630:                assertEquals("location", fcd.getFieldName());
2631:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2632:                        .getRestrictions().get(0)).getEvaluator());
2633:                assertEquals("Chicago", ((LiteralRestrictionDescr) fcd
2634:                        .getRestrictions().get(0)).getText());
2635:            }
2636:
2637:            public void testConstraintConnectivesPrecedenceWithBracks2()
2638:                    throws Exception {
2639:                final String text = "Person( ( age == 70 && hair == \"black\" ) || ( age == 40 && hair == \"pink\" ) || ( age == 12 && ( hair == \"yellow\" || hair == \"blue\" ) ) )";
2640:                final CharStream charStream = new ANTLRStringStream(text);
2641:                final DRLLexer lexer = new DRLLexer(charStream);
2642:                final TokenStream tokenStream = new CommonTokenStream(lexer);
2643:                final DRLParser parser = new DRLParser(tokenStream);
2644:
2645:                PatternDescr pattern = (PatternDescr) parser.fact(null);
2646:                assertFalse(parser.getErrorMessages().toString(), parser
2647:                        .hasErrors());
2648:
2649:                assertEquals(1, pattern.getDescrs().size());
2650:                OrDescr or1 = (OrDescr) pattern.getDescrs().get(0);
2651:                assertEquals(3, or1.getDescrs().size());
2652:
2653:                AndDescr and1 = (AndDescr) or1.getDescrs().get(0);
2654:                AndDescr and2 = (AndDescr) or1.getDescrs().get(1);
2655:                AndDescr and3 = (AndDescr) or1.getDescrs().get(2);
2656:
2657:                assertEquals(2, and1.getDescrs().size());
2658:                FieldConstraintDescr fcd = (FieldConstraintDescr) and1
2659:                        .getDescrs().get(0);
2660:                assertEquals("age", fcd.getFieldName());
2661:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2662:                        .getRestrictions().get(0)).getEvaluator());
2663:                assertEquals("70", ((LiteralRestrictionDescr) fcd
2664:                        .getRestrictions().get(0)).getText());
2665:                fcd = (FieldConstraintDescr) and1.getDescrs().get(1);
2666:                assertEquals("hair", fcd.getFieldName());
2667:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2668:                        .getRestrictions().get(0)).getEvaluator());
2669:                assertEquals("black", ((LiteralRestrictionDescr) fcd
2670:                        .getRestrictions().get(0)).getText());
2671:
2672:                assertEquals(2, and2.getDescrs().size());
2673:                fcd = (FieldConstraintDescr) and2.getDescrs().get(0);
2674:                assertEquals("age", fcd.getFieldName());
2675:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2676:                        .getRestrictions().get(0)).getEvaluator());
2677:                assertEquals("40", ((LiteralRestrictionDescr) fcd
2678:                        .getRestrictions().get(0)).getText());
2679:                fcd = (FieldConstraintDescr) and2.getDescrs().get(1);
2680:                assertEquals("hair", fcd.getFieldName());
2681:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2682:                        .getRestrictions().get(0)).getEvaluator());
2683:                assertEquals("pink", ((LiteralRestrictionDescr) fcd
2684:                        .getRestrictions().get(0)).getText());
2685:
2686:                assertEquals(2, and3.getDescrs().size());
2687:                fcd = (FieldConstraintDescr) and3.getDescrs().get(0);
2688:                assertEquals("age", fcd.getFieldName());
2689:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2690:                        .getRestrictions().get(0)).getEvaluator());
2691:                assertEquals("12", ((LiteralRestrictionDescr) fcd
2692:                        .getRestrictions().get(0)).getText());
2693:                OrDescr or2 = (OrDescr) and3.getDescrs().get(1);
2694:                fcd = (FieldConstraintDescr) or2.getDescrs().get(0);
2695:                assertEquals("hair", fcd.getFieldName());
2696:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2697:                        .getRestrictions().get(0)).getEvaluator());
2698:                assertEquals("yellow", ((LiteralRestrictionDescr) fcd
2699:                        .getRestrictions().get(0)).getText());
2700:                fcd = (FieldConstraintDescr) or2.getDescrs().get(1);
2701:                assertEquals("hair", fcd.getFieldName());
2702:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2703:                        .getRestrictions().get(0)).getEvaluator());
2704:                assertEquals("blue", ((LiteralRestrictionDescr) fcd
2705:                        .getRestrictions().get(0)).getText());
2706:            }
2707:
2708:            public void testRestrictionConnectives() throws Exception {
2709:
2710:                // the bellow expression must generate the following tree:
2711:                //
2712:                //                       AND
2713:                //                        |
2714:                //                       OR
2715:                //        /---------------+-------------------\
2716:                //       AND             AND                 AND
2717:                //    /---+---\       /---+---\           /---+---\
2718:                //   FC       FC     FC       FC         FC       OR
2719:                //                                             /---+---\
2720:                //                                            FC       FC
2721:                //
2722:                final String text = "Person( ( age ( > 60 && < 70 ) || ( > 50 && < 55 ) && hair == \"black\" ) || ( age == 40 && hair == \"pink\" ) || ( age == 12 && ( hair == \"yellow\" || hair == \"blue\" ) ))";
2723:                final CharStream charStream = new ANTLRStringStream(text);
2724:                final DRLLexer lexer = new DRLLexer(charStream);
2725:                final TokenStream tokenStream = new CommonTokenStream(lexer);
2726:                final DRLParser parser = new DRLParser(tokenStream);
2727:
2728:                PatternDescr pattern = (PatternDescr) parser.fact(null);
2729:                assertFalse(parser.getErrorMessages().toString(), parser
2730:                        .hasErrors());
2731:
2732:                assertEquals(1, pattern.getDescrs().size());
2733:
2734:                OrDescr orConstr = (OrDescr) pattern.getDescrs().get(0);
2735:
2736:                assertEquals(3, orConstr.getDescrs().size());
2737:
2738:                AndDescr andConstr1 = (AndDescr) orConstr.getDescrs().get(0);
2739:
2740:                FieldConstraintDescr fcd = (FieldConstraintDescr) andConstr1
2741:                        .getDescrs().get(0);
2742:                assertEquals("age", fcd.getFieldName());
2743:                RestrictionConnectiveDescr or = (RestrictionConnectiveDescr) fcd
2744:                        .getRestriction().getRestrictions().get(0);
2745:                RestrictionConnectiveDescr and1 = (RestrictionConnectiveDescr) or
2746:                        .getRestrictions().get(0);
2747:                RestrictionConnectiveDescr and2 = (RestrictionConnectiveDescr) or
2748:                        .getRestrictions().get(1);
2749:
2750:                assertEquals(">", ((LiteralRestrictionDescr) and1
2751:                        .getRestrictions().get(0)).getEvaluator());
2752:                assertEquals("60", ((LiteralRestrictionDescr) and1
2753:                        .getRestrictions().get(0)).getText());
2754:
2755:                assertEquals("<", ((LiteralRestrictionDescr) and1
2756:                        .getRestrictions().get(1)).getEvaluator());
2757:                assertEquals("70", ((LiteralRestrictionDescr) and1
2758:                        .getRestrictions().get(1)).getText());
2759:
2760:                assertEquals(">", ((LiteralRestrictionDescr) and2
2761:                        .getRestrictions().get(0)).getEvaluator());
2762:                assertEquals("50", ((LiteralRestrictionDescr) and2
2763:                        .getRestrictions().get(0)).getText());
2764:
2765:                assertEquals("<", ((LiteralRestrictionDescr) and2
2766:                        .getRestrictions().get(1)).getEvaluator());
2767:                assertEquals("55", ((LiteralRestrictionDescr) and2
2768:                        .getRestrictions().get(1)).getText());
2769:
2770:                fcd = (FieldConstraintDescr) andConstr1.getDescrs().get(1);
2771:                assertEquals("hair", fcd.getFieldName());
2772:
2773:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2774:                        .getRestrictions().get(0)).getEvaluator());
2775:                assertEquals("black", ((LiteralRestrictionDescr) fcd
2776:                        .getRestrictions().get(0)).getText());
2777:
2778:                AndDescr andConstr2 = (AndDescr) orConstr.getDescrs().get(1);
2779:                assertEquals(2, andConstr2.getDescrs().size());
2780:                fcd = (FieldConstraintDescr) andConstr2.getDescrs().get(0);
2781:                assertEquals("age", fcd.getFieldName());
2782:
2783:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2784:                        .getRestrictions().get(0)).getEvaluator());
2785:                assertEquals("40", ((LiteralRestrictionDescr) fcd
2786:                        .getRestrictions().get(0)).getText());
2787:
2788:                fcd = (FieldConstraintDescr) andConstr2.getDescrs().get(1);
2789:                assertEquals("hair", fcd.getFieldName());
2790:
2791:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2792:                        .getRestrictions().get(0)).getEvaluator());
2793:                assertEquals("pink", ((LiteralRestrictionDescr) fcd
2794:                        .getRestrictions().get(0)).getText());
2795:
2796:                AndDescr andConstr3 = (AndDescr) orConstr.getDescrs().get(2);
2797:                assertEquals(2, andConstr3.getDescrs().size());
2798:                fcd = (FieldConstraintDescr) andConstr3.getDescrs().get(0);
2799:                assertEquals("age", fcd.getFieldName());
2800:
2801:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2802:                        .getRestrictions().get(0)).getEvaluator());
2803:                assertEquals("12", ((LiteralRestrictionDescr) fcd
2804:                        .getRestrictions().get(0)).getText());
2805:
2806:                OrDescr orConstr2 = (OrDescr) andConstr3.getDescrs().get(1);
2807:
2808:                fcd = (FieldConstraintDescr) orConstr2.getDescrs().get(0);
2809:                assertEquals("hair", fcd.getFieldName());
2810:
2811:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2812:                        .getRestrictions().get(0)).getEvaluator());
2813:                assertEquals("yellow", ((LiteralRestrictionDescr) fcd
2814:                        .getRestrictions().get(0)).getText());
2815:
2816:                fcd = (FieldConstraintDescr) orConstr2.getDescrs().get(1);
2817:                assertEquals("hair", fcd.getFieldName());
2818:
2819:                assertEquals("==", ((LiteralRestrictionDescr) fcd
2820:                        .getRestrictions().get(0)).getEvaluator());
2821:                assertEquals("blue", ((LiteralRestrictionDescr) fcd
2822:                        .getRestrictions().get(0)).getText());
2823:
2824:            }
2825:
2826:            public void testNotContains() throws Exception {
2827:                final String text = "City( $city : city )\nCountry( cities not contains $city )\n";
2828:                final AndDescr descrs = new AndDescr();
2829:                final CharStream charStream = new ANTLRStringStream(text);
2830:                final DRLLexer lexer = new DRLLexer(charStream);
2831:                final TokenStream tokenStream = new CommonTokenStream(lexer);
2832:                final DRLParser parser = new DRLParser(tokenStream);
2833:                parser.setLineOffset(descrs.getLine());
2834:                parser.normal_lhs_block(descrs);
2835:                assertFalse(parser.getErrorMessages().toString(), parser
2836:                        .hasErrors());
2837:
2838:                assertEquals(2, descrs.getDescrs().size());
2839:                PatternDescr pat = (PatternDescr) descrs.getDescrs().get(1);
2840:                FieldConstraintDescr fieldConstr = (FieldConstraintDescr) pat
2841:                        .getConstraint().getDescrs().get(0);
2842:                VariableRestrictionDescr restr = (VariableRestrictionDescr) fieldConstr
2843:                        .getRestrictions().get(0);
2844:
2845:                assertEquals("not contains", restr.getEvaluator());
2846:                assertEquals("$city", restr.getIdentifier());
2847:            }
2848:
2849:            public void testNotMatches() throws Exception {
2850:                final String text = "Message( text not matches '[abc]*' )\n";
2851:                final AndDescr descrs = new AndDescr();
2852:                final CharStream charStream = new ANTLRStringStream(text);
2853:                final DRLLexer lexer = new DRLLexer(charStream);
2854:                final TokenStream tokenStream = new CommonTokenStream(lexer);
2855:                final DRLParser parser = new DRLParser(tokenStream);
2856:                parser.setLineOffset(descrs.getLine());
2857:                parser.normal_lhs_block(descrs);
2858:                assertFalse(parser.getErrorMessages().toString(), parser
2859:                        .hasErrors());
2860:
2861:                assertEquals(1, descrs.getDescrs().size());
2862:                PatternDescr pat = (PatternDescr) descrs.getDescrs().get(0);
2863:                FieldConstraintDescr fieldConstr = (FieldConstraintDescr) pat
2864:                        .getConstraint().getDescrs().get(0);
2865:                LiteralRestrictionDescr restr = (LiteralRestrictionDescr) fieldConstr
2866:                        .getRestrictions().get(0);
2867:
2868:                assertEquals("not matches", restr.getEvaluator());
2869:                assertEquals("[abc]*", restr.getText());
2870:            }
2871:
2872:            public void testSemicolon() throws Exception {
2873:                parseResource("semicolon.drl");
2874:
2875:                this .parser.compilation_unit();
2876:
2877:                assertFalse(this .parser.getErrorMessages().toString(),
2878:                        this .parser.hasErrors());
2879:
2880:                final PackageDescr pkg = this .parser.getPackageDescr();
2881:                assertEquals("org.drools", pkg.getName());
2882:                assertEquals(1, pkg.getGlobals().size());
2883:                assertEquals(3, pkg.getRules().size());
2884:
2885:                final RuleDescr rule1 = (RuleDescr) pkg.getRules().get(0);
2886:                assertEquals(2, rule1.getLhs().getDescrs().size());
2887:
2888:                final RuleDescr query1 = (RuleDescr) pkg.getRules().get(1);
2889:                assertEquals(3, query1.getLhs().getDescrs().size());
2890:
2891:                final RuleDescr rule2 = (RuleDescr) pkg.getRules().get(2);
2892:                assertEquals(2, rule2.getLhs().getDescrs().size());
2893:            }
2894:
2895:            public void testEval() throws Exception {
2896:                parseResource("eval_parsing.drl");
2897:
2898:                this .parser.compilation_unit();
2899:
2900:                assertFalse(this .parser.getErrorMessages().toString(),
2901:                        this .parser.hasErrors());
2902:
2903:                final PackageDescr pkg = this .parser.getPackageDescr();
2904:                assertEquals("org.drools", pkg.getName());
2905:                assertEquals(1, pkg.getRules().size());
2906:
2907:                final RuleDescr rule1 = (RuleDescr) pkg.getRules().get(0);
2908:                assertEquals(1, rule1.getLhs().getDescrs().size());
2909:            }
2910:
2911:            public void testAccumulateReverse() throws Exception {
2912:                final DRLParser parser = parseResource("accumulateReverse.drl");
2913:                parser.compilation_unit();
2914:
2915:                if (parser.hasErrors()) {
2916:                    System.err.println(parser.getErrorMessages());
2917:                }
2918:
2919:                assertFalse(parser.getErrorMessages().toString(), parser
2920:                        .hasErrors());
2921:
2922:                final PackageDescr pack = parser.getPackageDescr();
2923:                assertEquals(1, pack.getRules().size());
2924:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
2925:                assertEquals(1, rule.getLhs().getDescrs().size());
2926:
2927:                final PatternDescr out = (PatternDescr) rule.getLhs()
2928:                        .getDescrs().get(0);
2929:                final AccumulateDescr accum = (AccumulateDescr) out.getSource();
2930:                assertEqualsIgnoreWhitespace("int x = 0 ;", accum.getInitCode());
2931:                assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
2932:                assertEqualsIgnoreWhitespace("x--;", accum.getReverseCode());
2933:                assertEqualsIgnoreWhitespace("new Integer(x)", accum
2934:                        .getResultCode());
2935:                assertFalse(accum.isExternalFunction());
2936:
2937:                final PatternDescr pattern = (PatternDescr) accum
2938:                        .getInputPattern();
2939:                assertEquals("Person", pattern.getObjectType());
2940:            }
2941:
2942:            public void testAccumulateExternalFunction() throws Exception {
2943:                final DRLParser parser = parseResource("accumulateExternalFunction.drl");
2944:                parser.compilation_unit();
2945:
2946:                if (parser.hasErrors()) {
2947:                    System.err.println(parser.getErrorMessages());
2948:                }
2949:
2950:                assertFalse(parser.getErrorMessages().toString(), parser
2951:                        .hasErrors());
2952:
2953:                final PackageDescr pack = parser.getPackageDescr();
2954:                assertEquals(1, pack.getRules().size());
2955:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
2956:                assertEquals(1, rule.getLhs().getDescrs().size());
2957:
2958:                final PatternDescr out = (PatternDescr) rule.getLhs()
2959:                        .getDescrs().get(0);
2960:                final AccumulateDescr accum = (AccumulateDescr) out.getSource();
2961:                assertEqualsIgnoreWhitespace("$age", accum.getExpression());
2962:                assertEqualsIgnoreWhitespace("average", accum
2963:                        .getFunctionIdentifier());
2964:                assertTrue(accum.isExternalFunction());
2965:
2966:                final PatternDescr pattern = (PatternDescr) accum
2967:                        .getInputPattern();
2968:                assertEquals("Person", pattern.getObjectType());
2969:            }
2970:
2971:            public void testCollectWithNestedFrom() throws Exception {
2972:                final DRLParser parser = parseResource("collect_with_nested_from.drl");
2973:                parser.compilation_unit();
2974:
2975:                if (parser.hasErrors()) {
2976:                    System.err.println(parser.getErrorMessages());
2977:                }
2978:
2979:                assertFalse(parser.hasErrors());
2980:
2981:                final PackageDescr pack = parser.getPackageDescr();
2982:                assertEquals(1, pack.getRules().size());
2983:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
2984:                assertEquals(1, rule.getLhs().getDescrs().size());
2985:
2986:                final PatternDescr out = (PatternDescr) rule.getLhs()
2987:                        .getDescrs().get(0);
2988:                final CollectDescr collect = (CollectDescr) out.getSource();
2989:
2990:                PatternDescr person = (PatternDescr) collect.getInputPattern();
2991:                assertEquals("Person", person.getObjectType());
2992:
2993:                final CollectDescr collect2 = (CollectDescr) person.getSource();
2994:
2995:                final PatternDescr people = collect2.getInputPattern();
2996:                assertEquals("People", people.getObjectType());
2997:            }
2998:
2999:            public void testAccumulateWithNestedFrom() throws Exception {
3000:                final DRLParser parser = parseResource("accumulate_with_nested_from.drl");
3001:                parser.compilation_unit();
3002:
3003:                if (parser.hasErrors()) {
3004:                    System.err.println(parser.getErrorMessages());
3005:                }
3006:
3007:                assertFalse(parser.hasErrors());
3008:
3009:                final PackageDescr pack = parser.getPackageDescr();
3010:                assertEquals(1, pack.getRules().size());
3011:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
3012:                assertEquals(1, rule.getLhs().getDescrs().size());
3013:
3014:                final PatternDescr out = (PatternDescr) rule.getLhs()
3015:                        .getDescrs().get(0);
3016:                final AccumulateDescr accumulate = (AccumulateDescr) out
3017:                        .getSource();
3018:
3019:                PatternDescr person = (PatternDescr) accumulate
3020:                        .getInputPattern();
3021:                assertEquals("Person", person.getObjectType());
3022:
3023:                final CollectDescr collect2 = (CollectDescr) person.getSource();
3024:
3025:                final PatternDescr people = collect2.getInputPattern();
3026:                assertEquals("People", people.getObjectType());
3027:            }
3028:
3029:            public void testAccessorPaths() throws Exception {
3030:                final String text = "org   .   drools/*comment*/\t  .Message( text not matches $c#comment\n. property )\n";
3031:                final AndDescr descrs = new AndDescr();
3032:                final CharStream charStream = new ANTLRStringStream(text);
3033:                final DRLLexer lexer = new DRLLexer(charStream);
3034:                final TokenStream tokenStream = new CommonTokenStream(lexer);
3035:                final DRLParser parser = new DRLParser(tokenStream);
3036:                parser.setLineOffset(descrs.getLine());
3037:                parser.normal_lhs_block(descrs);
3038:                assertFalse(parser.getErrorMessages().toString(), parser
3039:                        .hasErrors());
3040:
3041:                assertEquals(1, descrs.getDescrs().size());
3042:                PatternDescr pat = (PatternDescr) descrs.getDescrs().get(0);
3043:                assertEquals("org.drools.Message", pat.getObjectType());
3044:
3045:                FieldConstraintDescr fieldConstr = (FieldConstraintDescr) pat
3046:                        .getConstraint().getDescrs().get(0);
3047:                QualifiedIdentifierRestrictionDescr restr = (QualifiedIdentifierRestrictionDescr) fieldConstr
3048:                        .getRestrictions().get(0);
3049:
3050:                assertEquals("not matches", restr.getEvaluator());
3051:                assertEquals("$c.property", restr.getText());
3052:            }
3053:
3054:            public void testOrCE() throws Exception {
3055:                final DRLParser parser = parseResource("or_ce.drl");
3056:                parser.compilation_unit();
3057:
3058:                assertFalse(parser.getErrorMessages().toString(), parser
3059:                        .hasErrors());
3060:
3061:                final PackageDescr pack = parser.getPackageDescr();
3062:                assertEquals(1, pack.getRules().size());
3063:                final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
3064:                assertEquals(2, rule.getLhs().getDescrs().size());
3065:
3066:                final PatternDescr person = (PatternDescr) rule.getLhs()
3067:                        .getDescrs().get(0);
3068:                assertEquals("Person", person.getObjectType());
3069:                assertEquals("$p", person.getIdentifier());
3070:
3071:                final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(1);
3072:                assertEquals(2, or.getDescrs().size());
3073:
3074:                final PatternDescr cheese1 = (PatternDescr) or.getDescrs().get(
3075:                        0);
3076:                assertEquals("Cheese", cheese1.getObjectType());
3077:                assertEquals("$c", cheese1.getIdentifier());
3078:                final PatternDescr cheese2 = (PatternDescr) or.getDescrs().get(
3079:                        1);
3080:                assertEquals("Cheese", cheese2.getObjectType());
3081:                assertNull(cheese2.getIdentifier());
3082:            }
3083:
3084:            public void testRuleParseLhs2() throws Exception {
3085:                final String text = "Message( Message.HELLO )\n";
3086:                final AndDescr descrs = new AndDescr();
3087:                final CharStream charStream = new ANTLRStringStream(text);
3088:                final DRLLexer lexer = new DRLLexer(charStream);
3089:                final TokenStream tokenStream = new CommonTokenStream(lexer);
3090:                final DRLParser parser = new DRLParser(tokenStream);
3091:                parser.setLineOffset(descrs.getLine());
3092:                parser.normal_lhs_block(descrs);
3093:                assertTrue(parser.hasErrors());
3094:            }
3095:
3096:            public void testRuleParseLhs3() throws Exception {
3097:                final String text = "(or\nnot Person()\n(and Cheese()\nMeat()\nWine()))";
3098:                final AndDescr descrs = new AndDescr();
3099:                final CharStream charStream = new ANTLRStringStream(text);
3100:                final DRLLexer lexer = new DRLLexer(charStream);
3101:                final TokenStream tokenStream = new CommonTokenStream(lexer);
3102:                final DRLParser parser = new DRLParser(tokenStream);
3103:                parser.setLineOffset(descrs.getLine());
3104:                parser.normal_lhs_block(descrs);
3105:                if (parser.hasErrors()) {
3106:                    System.err.println(parser.getErrorMessages());
3107:                }
3108:                assertFalse(parser.hasErrors());
3109:                assertEquals(1, descrs.getDescrs().size());
3110:                OrDescr or = (OrDescr) descrs.getDescrs().get(0);
3111:                assertEquals(2, or.getDescrs().size());
3112:                NotDescr not = (NotDescr) or.getDescrs().get(0);
3113:                AndDescr and = (AndDescr) or.getDescrs().get(1);
3114:                assertEquals(1, not.getDescrs().size());
3115:                PatternDescr person = (PatternDescr) not.getDescrs().get(0);
3116:                assertEquals("Person", person.getObjectType());
3117:                assertEquals(3, and.getDescrs().size());
3118:                PatternDescr cheese = (PatternDescr) and.getDescrs().get(0);
3119:                assertEquals("Cheese", cheese.getObjectType());
3120:                PatternDescr meat = (PatternDescr) and.getDescrs().get(1);
3121:                assertEquals("Meat", meat.getObjectType());
3122:                PatternDescr wine = (PatternDescr) and.getDescrs().get(2);
3123:                assertEquals("Wine", wine.getObjectType());
3124:
3125:            }
3126:
3127:            private DRLParser parse(final String text) throws Exception {
3128:                this .parser = newParser(newTokenStream(newLexer(newCharStream(text))));
3129:                return this .parser;
3130:            }
3131:
3132:            private DRLParser parse(final String source, final String text)
3133:                    throws Exception {
3134:                this .parser = newParser(newTokenStream(newLexer(newCharStream(text))));
3135:                this .parser.setSource(source);
3136:                return this .parser;
3137:            }
3138:
3139:            private Reader getReader(final String name) throws Exception {
3140:                final InputStream in = getClass().getResourceAsStream(name);
3141:
3142:                return new InputStreamReader(in);
3143:            }
3144:
3145:            private DRLParser parseResource(final String name) throws Exception {
3146:
3147:                //        System.err.println( getClass().getResource( name ) );
3148:                final Reader reader = getReader(name);
3149:
3150:                final StringBuffer text = new StringBuffer();
3151:
3152:                final char[] buf = new char[1024];
3153:                int len = 0;
3154:
3155:                while ((len = reader.read(buf)) >= 0) {
3156:                    text.append(buf, 0, len);
3157:                }
3158:
3159:                return parse(name, text.toString());
3160:            }
3161:
3162:            private CharStream newCharStream(final String text) {
3163:                return new ANTLRStringStream(text);
3164:            }
3165:
3166:            private DRLLexer newLexer(final CharStream charStream) {
3167:                return new DRLLexer(charStream);
3168:            }
3169:
3170:            private TokenStream newTokenStream(final Lexer lexer) {
3171:                return new CommonTokenStream(lexer);
3172:            }
3173:
3174:            private DRLParser newParser(final TokenStream tokenStream) {
3175:                final DRLParser p = new DRLParser(tokenStream);
3176:                //p.setParserDebug( true );
3177:                return p;
3178:            }
3179:
3180:            private void assertEqualsIgnoreWhitespace(final String expected,
3181:                    final String actual) {
3182:                final String cleanExpected = expected.replaceAll("\\s+", "");
3183:                final String cleanActual = actual.replaceAll("\\s+", "");
3184:
3185:                assertEquals(cleanExpected, cleanActual);
3186:            }
3187:
3188:            private void prettyPrintErrors() {
3189:                final List msgs = this .parser.getErrorMessages();
3190:                for (final Iterator iter = msgs.iterator(); iter.hasNext();) {
3191:                    final String err = (String) iter.next();
3192:                    System.out.println(err);
3193:
3194:                }
3195:            }
3196:
3197:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.