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


0001:        package org.drools.compiler;
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.ByteArrayInputStream;
0020:        import java.io.ByteArrayOutputStream;
0021:        import java.io.IOException;
0022:        import java.io.InputStream;
0023:        import java.io.InputStreamReader;
0024:        import java.io.ObjectInput;
0025:        import java.io.ObjectInputStream;
0026:        import java.io.ObjectOutput;
0027:        import java.io.ObjectOutputStream;
0028:        import java.io.StringReader;
0029:        import java.lang.reflect.Field;
0030:        import java.util.HashMap;
0031:        import java.util.List;
0032:        import java.util.Map;
0033:
0034:        import org.drools.Cheese;
0035:        import org.drools.DroolsTestCase;
0036:        import org.drools.FactHandle;
0037:        import org.drools.Primitives;
0038:        import org.drools.QueryResults;
0039:        import org.drools.RuleBase;
0040:        import org.drools.RuleBaseFactory;
0041:        import org.drools.StatefulSession;
0042:        import org.drools.WorkingMemory;
0043:        import org.drools.base.DefaultKnowledgeHelper;
0044:        import org.drools.common.ActivationGroupNode;
0045:        import org.drools.common.DroolsObjectInputStream;
0046:        import org.drools.common.InternalFactHandle;
0047:        import org.drools.common.LogicalDependency;
0048:        import org.drools.common.RuleFlowGroupNode;
0049:        import org.drools.commons.jci.compilers.EclipseJavaCompiler;
0050:        import org.drools.commons.jci.compilers.JaninoJavaCompiler;
0051:        import org.drools.commons.jci.compilers.JavaCompiler;
0052:        import org.drools.facttemplates.Fact;
0053:        import org.drools.lang.descr.AndDescr;
0054:        import org.drools.lang.descr.BaseDescr;
0055:        import org.drools.lang.descr.ConditionalElementDescr;
0056:        import org.drools.lang.descr.EvalDescr;
0057:        import org.drools.lang.descr.ExistsDescr;
0058:        import org.drools.lang.descr.FactTemplateDescr;
0059:        import org.drools.lang.descr.FieldBindingDescr;
0060:        import org.drools.lang.descr.FieldConstraintDescr;
0061:        import org.drools.lang.descr.FieldTemplateDescr;
0062:        import org.drools.lang.descr.GlobalDescr;
0063:        import org.drools.lang.descr.LiteralRestrictionDescr;
0064:        import org.drools.lang.descr.NotDescr;
0065:        import org.drools.lang.descr.OrDescr;
0066:        import org.drools.lang.descr.PackageDescr;
0067:        import org.drools.lang.descr.PatternDescr;
0068:        import org.drools.lang.descr.PredicateDescr;
0069:        import org.drools.lang.descr.QueryDescr;
0070:        import org.drools.lang.descr.ReturnValueRestrictionDescr;
0071:        import org.drools.lang.descr.RuleDescr;
0072:        import org.drools.lang.descr.VariableRestrictionDescr;
0073:        import org.drools.reteoo.ReteooRuleBase;
0074:        import org.drools.rule.Declaration;
0075:        import org.drools.rule.EvalCondition;
0076:        import org.drools.rule.GroupElement;
0077:        import org.drools.rule.LiteralConstraint;
0078:        import org.drools.rule.Package;
0079:        import org.drools.rule.Pattern;
0080:        import org.drools.rule.PredicateConstraint;
0081:        import org.drools.rule.ReturnValueConstraint;
0082:        import org.drools.rule.Rule;
0083:        import org.drools.rule.builder.dialect.java.JavaDialect;
0084:        import org.drools.rule.builder.dialect.java.JavaDialectConfiguration;
0085:        import org.drools.ruleflow.common.core.Process;
0086:        import org.drools.ruleflow.core.impl.RuleFlowProcessImpl;
0087:        import org.drools.spi.Activation;
0088:        import org.drools.spi.AgendaGroup;
0089:        import org.drools.spi.CompiledInvoker;
0090:        import org.drools.spi.PropagationContext;
0091:        import org.drools.spi.Tuple;
0092:        import org.drools.util.LinkedList;
0093:
0094:        public class PackageBuilderTest extends DroolsTestCase {
0095:
0096:            public void testErrors() throws Exception {
0097:                final PackageBuilder builder = new PackageBuilder();
0098:
0099:                final PackageDescr packageDescr = new PackageDescr("p1");
0100:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
0101:                packageDescr.addRule(ruleDescr);
0102:
0103:                final AndDescr lhs = new AndDescr();
0104:                ruleDescr.setLhs(lhs);
0105:
0106:                final PatternDescr pattern = new PatternDescr(Cheese.class
0107:                        .getName(), "stilton");
0108:                lhs.addDescr(pattern);
0109:
0110:                FieldBindingDescr fieldBindingDescr = new FieldBindingDescr(
0111:                        "price", "x");
0112:                pattern.addConstraint(fieldBindingDescr);
0113:                fieldBindingDescr = new FieldBindingDescr("price", "y");
0114:                pattern.addConstraint(fieldBindingDescr);
0115:
0116:                packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
0117:
0118:                final FieldConstraintDescr returnValue = new FieldConstraintDescr(
0119:                        "price");
0120:                returnValue.addRestriction(new ReturnValueRestrictionDescr(
0121:                        "==", "x"));
0122:
0123:                pattern.addConstraint(returnValue);
0124:
0125:                // There is no m this should produce errors.
0126:                ruleDescr.setConsequence("update(m);");
0127:
0128:                builder.addPackage(packageDescr);
0129:
0130:                assertLength(1, builder.getErrors().getErrors());
0131:            }
0132:
0133:            public void testErrorsInParser() throws Exception {
0134:                final PackageBuilder builder = new PackageBuilder();
0135:                builder.addPackageFromDrl(new InputStreamReader(this .getClass()
0136:                        .getResourceAsStream("bad_rule.drl")));
0137:                assertTrue(builder.hasErrors());
0138:            }
0139:
0140:            public void testReload() throws Exception {
0141:                final PackageBuilder builder = new PackageBuilder();
0142:
0143:                final PackageDescr packageDescr = new PackageDescr("p1");
0144:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
0145:                packageDescr.addRule(ruleDescr);
0146:
0147:                final AndDescr lhs = new AndDescr();
0148:                ruleDescr.setLhs(lhs);
0149:
0150:                packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
0151:
0152:                ruleDescr
0153:                        .setConsequence("map.put(\"value\", new Integer(1) );");
0154:
0155:                builder.addPackage(packageDescr);
0156:
0157:                Package pkg = builder.getPackage();
0158:                Rule rule = pkg.getRule("rule-1");
0159:
0160:                assertLength(0, builder.getErrors().getErrors());
0161:
0162:                final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory
0163:                        .newRuleBase();
0164:                ruleBase.getGlobals().put("map", Map.class);
0165:                final WorkingMemory workingMemory = ruleBase
0166:                        .newStatefulSession();
0167:
0168:                final HashMap map = new HashMap();
0169:                workingMemory.setGlobal("map", map);
0170:
0171:                final Tuple tuple = new MockTuple(new HashMap());
0172:                final Activation activation = new MockActivation(rule, 0, rule
0173:                        .getLhs(), tuple);
0174:
0175:                DefaultKnowledgeHelper knowledgeHelper = new org.drools.base.DefaultKnowledgeHelper(
0176:                        workingMemory);
0177:                knowledgeHelper.setActivation(activation);
0178:
0179:                rule.getConsequence().evaluate(knowledgeHelper, workingMemory);
0180:                assertEquals(new Integer(1), map.get("value"));
0181:
0182:                ruleDescr
0183:                        .setConsequence("map.put(\"value\", new Integer(2) );");
0184:                pkg.removeRule(rule);
0185:
0186:                // Make sure the compiled classes are also removed
0187:                assertEquals(0, pkg.getPackageCompilationData().list().length);
0188:
0189:                builder.addPackage(packageDescr);
0190:
0191:                pkg = builder.getPackage();
0192:
0193:                rule = pkg.getRule("rule-1");
0194:
0195:                knowledgeHelper = new org.drools.base.DefaultKnowledgeHelper(
0196:                        workingMemory);
0197:                knowledgeHelper.setActivation(activation);
0198:
0199:                rule.getConsequence().evaluate(knowledgeHelper, workingMemory);
0200:                assertEquals(new Integer(2), map.get("value"));
0201:
0202:            }
0203:
0204:            public void testSerializable() throws Exception {
0205:                final PackageBuilder builder = new PackageBuilder();
0206:
0207:                final PackageDescr packageDescr = new PackageDescr("p1");
0208:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
0209:                packageDescr.addRule(ruleDescr);
0210:
0211:                final AndDescr lhs = new AndDescr();
0212:                ruleDescr.setLhs(lhs);
0213:
0214:                packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
0215:
0216:                ruleDescr
0217:                        .setConsequence("map.put(\"value\", new Integer(1) );");
0218:                //check that packageDescr is serializable
0219:                final byte[] ast = serializeOut(packageDescr);
0220:                final PackageDescr back = (PackageDescr) serializeIn(ast);
0221:                assertNotNull(back);
0222:                assertEquals("p1", back.getName());
0223:
0224:                builder.addPackage(packageDescr);
0225:                final Package pkg = builder.getPackage();
0226:                final Rule rule = pkg.getRule("rule-1");
0227:
0228:                assertLength(0, builder.getErrors().getErrors());
0229:
0230:                final byte[] bytes = serializeOut(pkg);
0231:
0232:                // Deserialize from a byte array
0233:
0234:                final Package newPkg = (Package) serializeIn(bytes);
0235:
0236:                final Rule newRule = newPkg.getRule("rule-1");
0237:
0238:                final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory
0239:                        .newRuleBase();
0240:                ruleBase.getGlobals().put("map", Map.class);
0241:                final WorkingMemory workingMemory = ruleBase
0242:                        .newStatefulSession();
0243:
0244:                final HashMap map = new HashMap();
0245:
0246:                workingMemory.setGlobal("map", map);
0247:
0248:                final Tuple tuple = new MockTuple(new HashMap());
0249:                final Activation activation = new MockActivation(newRule, 0,
0250:                        newRule.getLhs(), tuple);
0251:
0252:                final DefaultKnowledgeHelper knowledgeHelper = new org.drools.base.DefaultKnowledgeHelper(
0253:                        workingMemory);
0254:                knowledgeHelper.setActivation(activation);
0255:
0256:                newRule.getConsequence().evaluate(knowledgeHelper,
0257:                        workingMemory);
0258:                assertEquals(new Integer(1), map.get("value"));
0259:            }
0260:
0261:            private Object serializeIn(final byte[] bytes) throws IOException,
0262:                    ClassNotFoundException {
0263:                final ObjectInput in = new DroolsObjectInputStream(
0264:                        new ByteArrayInputStream(bytes));
0265:                final Object obj = in.readObject();
0266:                in.close();
0267:                return obj;
0268:            }
0269:
0270:            private byte[] serializeOut(final Object obj) throws IOException {
0271:                // Serialize to a byte array
0272:                final ByteArrayOutputStream bos = new ByteArrayOutputStream();
0273:                final ObjectOutput out = new ObjectOutputStream(bos);
0274:                out.writeObject(obj);
0275:                out.close();
0276:
0277:                // Get the bytes of the serialized object
0278:                final byte[] bytes = bos.toByteArray();
0279:                return bytes;
0280:            }
0281:
0282:            public void testNoPackageName() throws Exception {
0283:                final PackageBuilder builder = new PackageBuilder();
0284:                try {
0285:                    builder.addPackage(new PackageDescr(null));
0286:                    fail("should have errored here.");
0287:                } catch (final RuntimeException e) {
0288:                    assertNotNull(e.getMessage());
0289:                }
0290:                try {
0291:                    builder.addPackage(new PackageDescr(""));
0292:                    fail("should have errored here.");
0293:                } catch (final RuntimeException e) {
0294:                    assertNotNull(e.getMessage());
0295:                }
0296:
0297:                builder.addPackageFromDrl(new StringReader("package foo"));
0298:                builder.addPackageFromDrl(new StringReader("rule x end"));
0299:
0300:            }
0301:
0302:            public void testErrorReset() throws Exception {
0303:                final PackageBuilder builder = new PackageBuilder();
0304:
0305:                builder.addPackageFromDrl(new StringReader(
0306:                        "package foo \n rule ORB"));
0307:                assertTrue(builder.hasErrors());
0308:
0309:                builder.resetErrors();
0310:                assertFalse(builder.hasErrors());
0311:
0312:                builder.addPackageFromDrl(new StringReader(
0313:                        "package foo \n rule ORB"));
0314:                assertTrue(builder.hasErrors());
0315:            }
0316:
0317:            public void testFactTemplate() {
0318:                final PackageBuilder builder = new PackageBuilder();
0319:
0320:                final PackageDescr packageDescr = new PackageDescr("p1");
0321:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
0322:                packageDescr.addRule(ruleDescr);
0323:
0324:                final AndDescr lhs = new AndDescr();
0325:                ruleDescr.setLhs(lhs);
0326:
0327:                final FactTemplateDescr cheese = new FactTemplateDescr("Cheese");
0328:                cheese
0329:                        .addFieldTemplate(new FieldTemplateDescr("name",
0330:                                "String"));
0331:                cheese.addFieldTemplate(new FieldTemplateDescr("price",
0332:                        "Integer"));
0333:
0334:                packageDescr.addFactTemplate(cheese);
0335:
0336:                final PatternDescr pattern = new PatternDescr("Cheese",
0337:                        "stilton");
0338:                lhs.addDescr(pattern);
0339:
0340:                final FieldConstraintDescr literalDescr = new FieldConstraintDescr(
0341:                        "name");
0342:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0343:                        "stilton"));
0344:
0345:                pattern.addConstraint(literalDescr);
0346:
0347:                ruleDescr
0348:                        .setConsequence("System.out.println( stilton.getFieldValue( \"name\" ) + \" \" + stilton.getFieldValue( \"price\" ) );");
0349:
0350:                builder.addPackage(packageDescr);
0351:
0352:                //        assertFalse( Arrays.toString( builder.getErrors() ),
0353:                //                     builder.hasErrors() );
0354:
0355:                final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
0356:                final Package pkg = builder.getPackage();
0357:                try {
0358:                    ruleBase.addPackage(pkg);
0359:                } catch (final Exception e) {
0360:                    e.printStackTrace();
0361:                }
0362:                final WorkingMemory workingMemory = ruleBase
0363:                        .newStatefulSession();
0364:                final Fact stilton = pkg.getFactTemplate("Cheese")
0365:                        .createFact(1);
0366:                stilton.setFieldValue("name", "stilton");
0367:                stilton.setFieldValue("price", new Integer(200));
0368:                workingMemory.insert(stilton);
0369:                workingMemory.fireAllRules();
0370:
0371:            }
0372:
0373:            public void testLiteral() throws Exception {
0374:                final PackageBuilder builder = new PackageBuilder();
0375:
0376:                final PackageDescr packageDescr = new PackageDescr("p1");
0377:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
0378:                packageDescr.addRule(ruleDescr);
0379:
0380:                final AndDescr lhs = new AndDescr();
0381:                ruleDescr.setLhs(lhs);
0382:
0383:                final PatternDescr pattern = new PatternDescr(Cheese.class
0384:                        .getName(), "stilton");
0385:                lhs.addDescr(pattern);
0386:
0387:                final FieldConstraintDescr literalDescr = new FieldConstraintDescr(
0388:                        "type");
0389:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0390:                        "stilton"));
0391:
0392:                pattern.addConstraint(literalDescr);
0393:
0394:                ruleDescr.setConsequence("update(stilton);");
0395:
0396:                builder.addPackage(packageDescr);
0397:
0398:                assertLength(0, builder.getErrors().getErrors());
0399:            }
0400:
0401:            public void testReturnValue() throws Exception {
0402:                final PackageBuilder builder = new PackageBuilder();
0403:
0404:                final PackageDescr packageDescr = new PackageDescr("p1");
0405:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
0406:                packageDescr.addRule(ruleDescr);
0407:
0408:                final AndDescr lhs = new AndDescr();
0409:                ruleDescr.setLhs(lhs);
0410:
0411:                final PatternDescr pattern = new PatternDescr(Cheese.class
0412:                        .getName(), "stilton");
0413:                lhs.addDescr(pattern);
0414:
0415:                FieldBindingDescr fieldBindingDescr = new FieldBindingDescr(
0416:                        "price", "x");
0417:                pattern.addConstraint(fieldBindingDescr);
0418:                fieldBindingDescr = new FieldBindingDescr("price", "y");
0419:                pattern.addConstraint(fieldBindingDescr);
0420:
0421:                packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
0422:
0423:                final FieldConstraintDescr returnValue = new FieldConstraintDescr(
0424:                        "price");
0425:                returnValue
0426:                        .addRestriction(new ReturnValueRestrictionDescr("==",
0427:                                "(( (Integer) map.get( new Integer( x )) ).intValue() * y)"));
0428:
0429:                pattern.addConstraint(returnValue);
0430:
0431:                ruleDescr.setConsequence("update(stilton);");
0432:
0433:                builder.addPackage(packageDescr);
0434:
0435:                assertEquals("Should not have any errors", 0, builder
0436:                        .getErrors().getErrors().length);
0437:            }
0438:
0439:            public void testReturnValueMethodCompare() {
0440:                final PackageBuilder builder1 = new PackageBuilder();
0441:                final PackageDescr packageDescr1 = new PackageDescr("package1");
0442:                createReturnValueRule(packageDescr1, " x + y ");
0443:                builder1.addPackage(packageDescr1);
0444:                final Pattern pattern1 = (Pattern) builder1.getPackage()
0445:                        .getRules()[0].getLhs().getChildren().get(0);
0446:                final ReturnValueConstraint returnValue1 = (ReturnValueConstraint) pattern1
0447:                        .getConstraints().get(2);
0448:
0449:                final PackageBuilder builder2 = new PackageBuilder();
0450:                final PackageDescr packageDescr2 = new PackageDescr("package2");
0451:                createReturnValueRule(packageDescr2, " x + y ");
0452:                builder2.addPackage(packageDescr2);
0453:                final Pattern pattern2 = (Pattern) builder2.getPackage()
0454:                        .getRules()[0].getLhs().getChildren().get(0);
0455:                final ReturnValueConstraint returnValue2 = (ReturnValueConstraint) pattern2
0456:                        .getConstraints().get(2);
0457:
0458:                final PackageBuilder builder3 = new PackageBuilder();
0459:                final PackageDescr packageDescr3 = new PackageDescr("package3");
0460:                createReturnValueRule(packageDescr3, " x - y ");
0461:                builder3.addPackage(packageDescr3);
0462:                final Pattern pattern3 = (Pattern) builder3.getPackage()
0463:                        .getRules()[0].getLhs().getChildren().get(0);
0464:                final ReturnValueConstraint returnValue3 = (ReturnValueConstraint) pattern3
0465:                        .getConstraints().get(2);
0466:
0467:                assertEquals(returnValue1, returnValue2);
0468:                assertFalse(returnValue1.equals(returnValue3));
0469:                assertFalse(returnValue2.equals(returnValue3));
0470:            }
0471:
0472:            public void testPredicate() throws Exception {
0473:                final PackageBuilder builder = new PackageBuilder();
0474:
0475:                final PackageDescr packageDescr = new PackageDescr("p1");
0476:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
0477:                packageDescr.addRule(ruleDescr);
0478:
0479:                final AndDescr lhs = new AndDescr();
0480:                ruleDescr.setLhs(lhs);
0481:
0482:                final PatternDescr pattern = new PatternDescr(Cheese.class
0483:                        .getName(), "stilton");
0484:                lhs.addDescr(pattern);
0485:
0486:                final FieldBindingDescr fieldBindingDescr = new FieldBindingDescr(
0487:                        "price", "x");
0488:                pattern.addConstraint(fieldBindingDescr);
0489:
0490:                final FieldBindingDescr fieldBindingDescr2 = new FieldBindingDescr(
0491:                        "price", "y");
0492:                pattern.addConstraint(fieldBindingDescr2);
0493:
0494:                packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
0495:
0496:                final PredicateDescr predicate = new PredicateDescr(
0497:                        "( ( Integer )map.get( new Integer(x) ) ).intValue() == y");
0498:                pattern.addConstraint(predicate);
0499:
0500:                ruleDescr.setConsequence("update(stilton);");
0501:
0502:                builder.addPackage(packageDescr);
0503:
0504:                assertLength(0, builder.getErrors().getErrors());
0505:            }
0506:
0507:            public void testPredicateMethodCompare() {
0508:                final PackageBuilder builder1 = new PackageBuilder();
0509:                final PackageDescr packageDescr1 = new PackageDescr("package1");
0510:                createPredicateRule(packageDescr1, "x==y");
0511:                builder1.addPackage(packageDescr1);
0512:                final Pattern pattern1 = (Pattern) builder1.getPackage()
0513:                        .getRules()[0].getLhs().getChildren().get(0);
0514:                final PredicateConstraint predicate1 = (PredicateConstraint) pattern1
0515:                        .getConstraints().get(2);
0516:
0517:                final PackageBuilder builder2 = new PackageBuilder();
0518:                final PackageDescr packageDescr2 = new PackageDescr("package2");
0519:                createPredicateRule(packageDescr2, "x==y");
0520:                builder2.addPackage(packageDescr2);
0521:                final Pattern pattern2 = (Pattern) builder2.getPackage()
0522:                        .getRules()[0].getLhs().getChildren().get(0);
0523:                final PredicateConstraint predicate2 = (PredicateConstraint) pattern2
0524:                        .getConstraints().get(2);
0525:
0526:                final PackageBuilder builder3 = new PackageBuilder();
0527:                final PackageDescr packageDescr3 = new PackageDescr("package3");
0528:                createPredicateRule(packageDescr3, "x!=y");
0529:                builder3.addPackage(packageDescr3);
0530:                final Pattern pattern3 = (Pattern) builder3.getPackage()
0531:                        .getRules()[0].getLhs().getChildren().get(0);
0532:                final PredicateConstraint predicate3 = (PredicateConstraint) pattern3
0533:                        .getConstraints().get(2);
0534:
0535:                assertEquals(predicate1, predicate2);
0536:                assertFalse(predicate1.equals(predicate3));
0537:                assertFalse(predicate2.equals(predicate3));
0538:            }
0539:
0540:            public void testEval() throws Exception {
0541:                final PackageBuilder builder = new PackageBuilder();
0542:
0543:                final PackageDescr packageDescr = new PackageDescr("p1");
0544:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
0545:                packageDescr.addRule(ruleDescr);
0546:
0547:                final AndDescr lhs = new AndDescr();
0548:                ruleDescr.setLhs(lhs);
0549:
0550:                final PatternDescr pattern = new PatternDescr(Cheese.class
0551:                        .getName(), "stilton");
0552:                lhs.addDescr(pattern);
0553:
0554:                FieldBindingDescr fieldBindingDescr = new FieldBindingDescr(
0555:                        "price", "x");
0556:                pattern.addConstraint(fieldBindingDescr);
0557:                fieldBindingDescr = new FieldBindingDescr("price", "y");
0558:                pattern.addConstraint(fieldBindingDescr);
0559:
0560:                packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
0561:
0562:                final EvalDescr evalDescr = new EvalDescr(
0563:                        "( ( Integer )map.get( new Integer(x) ) ).intValue() == y");
0564:                lhs.addDescr(evalDescr);
0565:
0566:                ruleDescr.setConsequence("update(stilton);");
0567:
0568:                builder.addPackage(packageDescr);
0569:
0570:                assertLength(0, builder.getErrors().getErrors());
0571:
0572:                final Package pkg = builder.getPackage();
0573:                final Rule rule = pkg.getRule("rule-1");
0574:                final EvalCondition eval = (EvalCondition) rule.getLhs()
0575:                        .getChildren().get(1);
0576:                final CompiledInvoker invoker = (CompiledInvoker) eval
0577:                        .getEvalExpression();
0578:                final List list = invoker.getMethodBytecode();
0579:            }
0580:
0581:            public void testEvalMethodCompare() {
0582:                final PackageBuilder builder1 = new PackageBuilder();
0583:                final PackageDescr packageDescr1 = new PackageDescr("package1");
0584:                createEvalRule(packageDescr1, "1==1");
0585:                builder1.addPackage(packageDescr1);
0586:                final EvalCondition eval1 = (EvalCondition) builder1
0587:                        .getPackage().getRules()[0].getLhs().getChildren().get(
0588:                        0);
0589:
0590:                final PackageBuilder builder2 = new PackageBuilder();
0591:                final PackageDescr packageDescr2 = new PackageDescr("package2");
0592:                createEvalRule(packageDescr2, "1==1");
0593:                builder2.addPackage(packageDescr2);
0594:                final EvalCondition eval2 = (EvalCondition) builder2
0595:                        .getPackage().getRules()[0].getLhs().getChildren().get(
0596:                        0);
0597:
0598:                final PackageBuilder builder3 = new PackageBuilder();
0599:                final PackageDescr packageDescr3 = new PackageDescr("package3");
0600:                createEvalRule(packageDescr3, "1==3");
0601:                builder3.addPackage(packageDescr3);
0602:                final EvalCondition eval3 = (EvalCondition) builder3
0603:                        .getPackage().getRules()[0].getLhs().getChildren().get(
0604:                        0);
0605:
0606:                assertEquals(eval1, eval2);
0607:                assertFalse(eval1.equals(eval3));
0608:                assertFalse(eval2.equals(eval3));
0609:            }
0610:
0611:            public void testOr() throws Exception {
0612:                final PackageBuilder builder = new PackageBuilder();
0613:                final Rule rule = createRule(new OrDescr(), builder,
0614:                        "update(stilton);");
0615:                assertLength(0, builder.getErrors().getErrors());
0616:
0617:                final GroupElement lhs = rule.getLhs();
0618:                assertLength(1, lhs.getChildren());
0619:
0620:                final GroupElement or = (GroupElement) lhs.getChildren().get(0);
0621:                assertLength(1, or.getChildren());
0622:                final Pattern pattern = (Pattern) or.getChildren().get(0);
0623:
0624:                final LiteralConstraint literalConstarint = (LiteralConstraint) pattern
0625:                        .getConstraints().get(0);
0626:            }
0627:
0628:            public void testAnd() throws Exception {
0629:                final PackageBuilder builder = new PackageBuilder();
0630:                final Rule rule = createRule(new AndDescr(), builder,
0631:                        "update(stilton);");
0632:                assertLength(0, builder.getErrors().getErrors());
0633:
0634:                final GroupElement lhs = rule.getLhs();
0635:                assertLength(1, lhs.getChildren());
0636:
0637:                final GroupElement and = (GroupElement) lhs.getChildren()
0638:                        .get(0);
0639:                assertLength(1, and.getChildren());
0640:                final Pattern pattern = (Pattern) and.getChildren().get(0);
0641:
0642:                final LiteralConstraint literalConstraint = (LiteralConstraint) pattern
0643:                        .getConstraints().get(0);
0644:            }
0645:
0646:            public void testNot() throws Exception {
0647:                PackageBuilder builder = new PackageBuilder();
0648:
0649:                // Make sure we can't accessa  variable bound inside the not node
0650:                Rule rule = createRule(new NotDescr(), builder,
0651:                        "update(stilton);");
0652:                assertEquals(1, builder.getErrors().getErrors().length);
0653:
0654:                builder = new PackageBuilder();
0655:                rule = createRule(new NotDescr(), builder, "");
0656:                assertEquals(0, builder.getErrors().getErrors().length);
0657:
0658:                final GroupElement lhs = rule.getLhs();
0659:                assertLength(1, lhs.getChildren());
0660:
0661:                final GroupElement not = (GroupElement) lhs.getChildren()
0662:                        .get(0);
0663:                assertLength(1, not.getChildren());
0664:                final Pattern pattern = (Pattern) not.getChildren().get(0);
0665:
0666:                final LiteralConstraint literalConstarint = (LiteralConstraint) pattern
0667:                        .getConstraints().get(0);
0668:            }
0669:
0670:            public void testExists() throws Exception {
0671:                PackageBuilder builder = new PackageBuilder();
0672:
0673:                // Make sure we can't accessa  variable bound inside the not node
0674:                Rule rule = createRule(new ExistsDescr(), builder,
0675:                        "update(stilton);");
0676:                assertEquals(1, builder.getErrors().getErrors().length);
0677:
0678:                builder = new PackageBuilder();
0679:                rule = createRule(new ExistsDescr(), builder, "");
0680:                assertEquals(0, builder.getErrors().getErrors().length);
0681:
0682:                final GroupElement lhs = rule.getLhs();
0683:                assertLength(1, lhs.getChildren());
0684:
0685:                final GroupElement exists = (GroupElement) lhs.getChildren()
0686:                        .get(0);
0687:                assertLength(1, exists.getChildren());
0688:                final Pattern pattern = (Pattern) exists.getChildren().get(0);
0689:
0690:                final LiteralConstraint literalConstarint = (LiteralConstraint) pattern
0691:                        .getConstraints().get(0);
0692:            }
0693:
0694:            public void testNumbers() throws Exception {
0695:                // test boolean
0696:                FieldConstraintDescr literalDescr = new FieldConstraintDescr(
0697:                        "booleanPrimitive");
0698:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0699:                        "true"));
0700:                createLiteralRule(literalDescr);
0701:
0702:                // test boolean
0703:                literalDescr = new FieldConstraintDescr("booleanPrimitive");
0704:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0705:                        "false"));
0706:                createLiteralRule(literalDescr);
0707:
0708:                // test char
0709:                literalDescr = new FieldConstraintDescr("charPrimitive");
0710:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0711:                        "a"));
0712:                createLiteralRule(literalDescr);
0713:
0714:                // test byte
0715:                literalDescr = new FieldConstraintDescr("bytePrimitive");
0716:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0717:                        "1"));
0718:                createLiteralRule(literalDescr);
0719:
0720:                literalDescr = new FieldConstraintDescr("bytePrimitive");
0721:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0722:                        "0"));
0723:                createLiteralRule(literalDescr);
0724:
0725:                literalDescr = new FieldConstraintDescr("bytePrimitive");
0726:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0727:                        "-1"));
0728:                createLiteralRule(literalDescr);
0729:
0730:                // test short
0731:                literalDescr = new FieldConstraintDescr("shortPrimitive");
0732:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0733:                        "1"));
0734:                createLiteralRule(literalDescr);
0735:
0736:                literalDescr = new FieldConstraintDescr("shortPrimitive");
0737:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0738:                        "0"));
0739:                createLiteralRule(literalDescr);
0740:
0741:                literalDescr = new FieldConstraintDescr("shortPrimitive");
0742:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0743:                        "-1"));
0744:                createLiteralRule(literalDescr);
0745:
0746:                // test int
0747:                literalDescr = new FieldConstraintDescr("intPrimitive");
0748:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0749:                        "1"));
0750:                createLiteralRule(literalDescr);
0751:
0752:                literalDescr = new FieldConstraintDescr("intPrimitive");
0753:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0754:                        "0"));
0755:                createLiteralRule(literalDescr);
0756:
0757:                literalDescr = new FieldConstraintDescr("intPrimitive");
0758:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0759:                        "-1"));
0760:                createLiteralRule(literalDescr);
0761:
0762:                // test long
0763:                literalDescr = new FieldConstraintDescr("longPrimitive");
0764:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0765:                        "1"));
0766:                createLiteralRule(literalDescr);
0767:
0768:                literalDescr = new FieldConstraintDescr("longPrimitive");
0769:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0770:                        "0"));
0771:                createLiteralRule(literalDescr);
0772:
0773:                literalDescr = new FieldConstraintDescr("longPrimitive");
0774:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0775:                        "0"));
0776:                createLiteralRule(literalDescr);
0777:
0778:                // test float
0779:                literalDescr = new FieldConstraintDescr("floatPrimitive");
0780:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0781:                        "1.1"));
0782:                createLiteralRule(literalDescr);
0783:
0784:                literalDescr = new FieldConstraintDescr("floatPrimitive");
0785:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0786:                        "0"));
0787:                createLiteralRule(literalDescr);
0788:
0789:                literalDescr = new FieldConstraintDescr("floatPrimitive");
0790:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0791:                        "-1.1"));
0792:                createLiteralRule(literalDescr);
0793:
0794:                // test double
0795:                literalDescr = new FieldConstraintDescr("doublePrimitive");
0796:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0797:                        "1.1"));
0798:                createLiteralRule(literalDescr);
0799:
0800:                literalDescr = new FieldConstraintDescr("doublePrimitive");
0801:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0802:                        "0"));
0803:                createLiteralRule(literalDescr);
0804:
0805:                literalDescr = new FieldConstraintDescr("floatPrimitive");
0806:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0807:                        "-1.1"));
0808:                createLiteralRule(literalDescr);
0809:            }
0810:
0811:            public void testNull() {
0812:                final PackageBuilder builder = new PackageBuilder();
0813:
0814:                final PackageDescr packageDescr = new PackageDescr("p1");
0815:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
0816:                packageDescr.addRule(ruleDescr);
0817:
0818:                final AndDescr lhs = new AndDescr();
0819:                ruleDescr.setLhs(lhs);
0820:
0821:                final PatternDescr patternDescr = new PatternDescr(Cheese.class
0822:                        .getName(), "stilton");
0823:
0824:                final FieldConstraintDescr literalDescr = new FieldConstraintDescr(
0825:                        "type");
0826:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0827:                        null));
0828:
0829:                patternDescr.addConstraint(literalDescr);
0830:
0831:                ruleDescr.setConsequence("");
0832:
0833:                builder.addPackage(packageDescr);
0834:
0835:                final Package pkg = (Package) builder.getPackage();
0836:                final Rule rule = pkg.getRule("rule-1");
0837:
0838:                assertLength(0, builder.getErrors().getErrors());
0839:            }
0840:
0841:            public void testQuery() throws Exception {
0842:                final PackageBuilder builder = new PackageBuilder();
0843:
0844:                final PackageDescr packageDescr = new PackageDescr("p1");
0845:                final QueryDescr queryDescr = new QueryDescr("query1");
0846:                queryDescr.setParameters(new String[] { "$type" });
0847:                queryDescr.setParameterTypes(new String[] { "String" });
0848:
0849:                packageDescr.addRule(queryDescr);
0850:
0851:                final AndDescr lhs = new AndDescr();
0852:                queryDescr.setLhs(lhs);
0853:
0854:                final PatternDescr pattern = new PatternDescr(Cheese.class
0855:                        .getName(), "stilton");
0856:                lhs.addDescr(pattern);
0857:
0858:                final FieldConstraintDescr literalDescr = new FieldConstraintDescr(
0859:                        "type");
0860:                literalDescr.addRestriction(new VariableRestrictionDescr("==",
0861:                        "$type"));
0862:
0863:                pattern.addConstraint(literalDescr);
0864:
0865:                queryDescr.setConsequence("update(stilton);");
0866:
0867:                builder.addPackage(packageDescr);
0868:
0869:                assertLength(0, builder.getErrors().getErrors());
0870:
0871:                RuleBase ruleBase = RuleBaseFactory.newRuleBase();
0872:                ruleBase.addPackage(builder.getPackage());
0873:
0874:                StatefulSession session = ruleBase.newStatefulSession();
0875:
0876:                session.insert(new Cheese("stilton", 15));
0877:
0878:                QueryResults results = session.getQueryResults("query1",
0879:                        new Object[] { "stilton" });
0880:                assertEquals(1, results.size());
0881:                Object object = results.get(0).get(0);
0882:                assertEquals(new Cheese("stilton", 15), object);
0883:
0884:                results = session.getQueryResults("query1",
0885:                        new Object[] { "cheddar" });
0886:            }
0887:
0888:            public void testDuplicateRuleNames() throws Exception {
0889:
0890:                final PackageBuilder builder = new PackageBuilder();
0891:
0892:                final PackageDescr packageDescr = new PackageDescr("p1");
0893:
0894:                RuleDescr ruleDescr = new RuleDescr("rule-1");
0895:                packageDescr.addRule(ruleDescr);
0896:                AndDescr lhs = new AndDescr();
0897:                ruleDescr.setLhs(lhs);
0898:                PatternDescr patternDescr = new PatternDescr(Cheese.class
0899:                        .getName(), "stilton");
0900:                FieldConstraintDescr literalDescr = new FieldConstraintDescr(
0901:                        "type");
0902:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0903:                        null));
0904:                patternDescr.addConstraint(literalDescr);
0905:                ruleDescr.setConsequence("");
0906:
0907:                ruleDescr = new RuleDescr("rule-1");
0908:                ruleDescr.setLocation(42, 43);
0909:                packageDescr.addRule(ruleDescr);
0910:                lhs = new AndDescr();
0911:                ruleDescr.setLhs(lhs);
0912:                patternDescr = new PatternDescr(Cheese.class.getName(),
0913:                        "stilton");
0914:                literalDescr = new FieldConstraintDescr("type");
0915:                literalDescr.addRestriction(new LiteralRestrictionDescr("!=",
0916:                        null));
0917:                patternDescr.addConstraint(literalDescr);
0918:                ruleDescr.setConsequence("");
0919:
0920:                ruleDescr = new RuleDescr("rule-2");
0921:                ruleDescr.setLocation(42, 43);
0922:                packageDescr.addRule(ruleDescr);
0923:                lhs = new AndDescr();
0924:                ruleDescr.setLhs(lhs);
0925:                patternDescr = new PatternDescr(Cheese.class.getName(),
0926:                        "stilton");
0927:
0928:                literalDescr = new FieldConstraintDescr("type");
0929:                literalDescr.addRestriction(new LiteralRestrictionDescr("!=",
0930:                        null));
0931:
0932:                patternDescr.addConstraint(literalDescr);
0933:                ruleDescr.setConsequence("");
0934:
0935:                builder.addPackage(packageDescr);
0936:
0937:                assertLength(1, builder.getErrors().getErrors());
0938:                final ParserError err = (ParserError) builder.getErrors()
0939:                        .getErrors()[0];
0940:                assertEquals(42, err.getRow());
0941:                assertEquals(43, err.getCol());
0942:
0943:            }
0944:
0945:            public void testDuplicateDeclaration() {
0946:                final PackageBuilder builder = new PackageBuilder();
0947:
0948:                final PackageDescr packageDescr = new PackageDescr("p1");
0949:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
0950:                packageDescr.addRule(ruleDescr);
0951:
0952:                final AndDescr lhs = new AndDescr();
0953:                ruleDescr.setLhs(lhs);
0954:
0955:                final PatternDescr pattern1 = new PatternDescr(Cheese.class
0956:                        .getName());
0957:                lhs.addDescr(pattern1);
0958:
0959:                final FieldBindingDescr fieldBindingDescr = new FieldBindingDescr(
0960:                        "type", "$type");
0961:
0962:                final FieldConstraintDescr literalDescr = new FieldConstraintDescr(
0963:                        "type");
0964:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
0965:                        "stilton"));
0966:
0967:                pattern1.addConstraint(fieldBindingDescr);
0968:                pattern1.addConstraint(literalDescr);
0969:
0970:                final PatternDescr pattern2 = new PatternDescr(Cheese.class
0971:                        .getName());
0972:                lhs.addDescr(pattern2);
0973:                pattern2.addConstraint(fieldBindingDescr);
0974:
0975:                ruleDescr.setConsequence("update(stilton);");
0976:
0977:                builder.addPackage(packageDescr);
0978:
0979:                assertLength(2, builder.getErrors().getErrors());
0980:            }
0981:
0982:            public void testCompilerConfiguration() throws Exception {
0983:                // test default is eclipse jdt core
0984:                PackageBuilder builder = new PackageBuilder();
0985:                final Field dialectField = builder.getClass().getDeclaredField(
0986:                        "dialect");
0987:                dialectField.setAccessible(true);
0988:                JavaDialect dialect = (JavaDialect) dialectField.get(builder);
0989:
0990:                final Field compilerField = dialect.getClass()
0991:                        .getDeclaredField("compiler");
0992:                compilerField.setAccessible(true);
0993:                JavaCompiler compiler = (JavaCompiler) compilerField
0994:                        .get(dialect);
0995:                assertSame(EclipseJavaCompiler.class, compiler.getClass());
0996:
0997:                // test JANINO with property settings
0998:                PackageBuilderConfiguration conf = new PackageBuilderConfiguration();
0999:                JavaDialectConfiguration javaConf = (JavaDialectConfiguration) conf
1000:                        .getDialectConfiguration("java");
1001:                javaConf.setCompiler(JavaDialectConfiguration.JANINO);
1002:                builder = new PackageBuilder(conf);
1003:                dialect = (JavaDialect) dialectField.get(builder);
1004:                compiler = (JavaCompiler) compilerField.get(dialect);
1005:                assertSame(JaninoJavaCompiler.class, compiler.getClass());
1006:
1007:                // test eclipse jdt core with property settings and default source level
1008:                conf = new PackageBuilderConfiguration();
1009:                javaConf = (JavaDialectConfiguration) conf
1010:                        .getDialectConfiguration("java");
1011:                javaConf.setCompiler(JavaDialectConfiguration.ECLIPSE);
1012:                builder = new PackageBuilder(conf);
1013:                dialect = (JavaDialect) dialectField.get(builder);
1014:                compiler = (JavaCompiler) compilerField.get(dialect);
1015:                assertSame(EclipseJavaCompiler.class, compiler.getClass());
1016:            }
1017:
1018:            public void testPackageMerge() throws Exception {
1019:                final PackageBuilder builder = new PackageBuilder();
1020:                try {
1021:                    builder.addPackage(new PackageDescr("org.drools"));
1022:
1023:                    builder.addPackageFromDrl(new StringReader(
1024:                            "package org.drools\n"
1025:                                    + "function boolean testIt() {\n"
1026:                                    + "  return true;\n" + "}\n"));
1027:                } catch (RuntimeException e) {
1028:                    fail("Should not raise any exception: " + e.getMessage());
1029:                }
1030:            }
1031:
1032:            private void createReturnValueRule(final PackageDescr packageDescr,
1033:                    final String expression) {
1034:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
1035:                packageDescr.addRule(ruleDescr);
1036:
1037:                final AndDescr lhs = new AndDescr();
1038:                ruleDescr.setLhs(lhs);
1039:
1040:                final PatternDescr pattern = new PatternDescr(Cheese.class
1041:                        .getName(), "stilton");
1042:                lhs.addDescr(pattern);
1043:
1044:                FieldBindingDescr fieldBindingDescr = new FieldBindingDescr(
1045:                        "price", "x");
1046:                pattern.addConstraint(fieldBindingDescr);
1047:                fieldBindingDescr = new FieldBindingDescr("price", "y");
1048:                pattern.addConstraint(fieldBindingDescr);
1049:
1050:                packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
1051:
1052:                final FieldConstraintDescr returnValue = new FieldConstraintDescr(
1053:                        "price");
1054:                returnValue.addRestriction(new ReturnValueRestrictionDescr(
1055:                        "==", expression));
1056:
1057:                pattern.addConstraint(returnValue);
1058:
1059:                ruleDescr.setConsequence("update(stilton);");
1060:            }
1061:
1062:            private void createPredicateRule(final PackageDescr packageDescr,
1063:                    final String expression) {
1064:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
1065:                packageDescr.addRule(ruleDescr);
1066:
1067:                final AndDescr lhs = new AndDescr();
1068:                ruleDescr.setLhs(lhs);
1069:
1070:                final PatternDescr pattern = new PatternDescr(Cheese.class
1071:                        .getName(), "stilton");
1072:                lhs.addDescr(pattern);
1073:
1074:                final FieldBindingDescr fieldBindingDescr = new FieldBindingDescr(
1075:                        "price", "x");
1076:                pattern.addConstraint(fieldBindingDescr);
1077:
1078:                final FieldBindingDescr fieldBindingDescr2 = new FieldBindingDescr(
1079:                        "price", "y");
1080:                pattern.addConstraint(fieldBindingDescr2);
1081:
1082:                packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
1083:
1084:                final PredicateDescr predicate = new PredicateDescr(expression);
1085:                pattern.addConstraint(predicate);
1086:
1087:                ruleDescr.setConsequence("update(stilton);");
1088:            }
1089:
1090:            private void createEvalRule(final PackageDescr packageDescr,
1091:                    final String expression) {
1092:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
1093:                packageDescr.addRule(ruleDescr);
1094:
1095:                final AndDescr lhs = new AndDescr();
1096:                ruleDescr.setLhs(lhs);
1097:
1098:                packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
1099:
1100:                final EvalDescr evalDescr = new EvalDescr(expression);
1101:                lhs.addDescr(evalDescr);
1102:
1103:                ruleDescr.setConsequence("");
1104:            }
1105:
1106:            private void createLiteralRule(
1107:                    final FieldConstraintDescr literalDescr) {
1108:                final PackageBuilder builder = new PackageBuilder();
1109:
1110:                final PackageDescr packageDescr = new PackageDescr("p1");
1111:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
1112:                packageDescr.addRule(ruleDescr);
1113:
1114:                final AndDescr lhs = new AndDescr();
1115:                ruleDescr.setLhs(lhs);
1116:
1117:                final PatternDescr pattern = new PatternDescr(Primitives.class
1118:                        .getName());
1119:                lhs.addDescr(pattern);
1120:
1121:                pattern.addConstraint(literalDescr);
1122:
1123:                ruleDescr.setConsequence("");
1124:
1125:                builder.addPackage(packageDescr);
1126:
1127:                assertLength(0, builder.getErrors().getErrors());
1128:            }
1129:
1130:            private Rule createRule(final ConditionalElementDescr ceDescr,
1131:                    final PackageBuilder builder, final String consequence)
1132:                    throws Exception {
1133:                final PackageDescr packageDescr = new PackageDescr("p1");
1134:                final RuleDescr ruleDescr = new RuleDescr("rule-1");
1135:                packageDescr.addRule(ruleDescr);
1136:
1137:                final AndDescr lhs = new AndDescr();
1138:                ruleDescr.setLhs(lhs);
1139:
1140:                lhs.addDescr((BaseDescr) ceDescr);
1141:
1142:                final PatternDescr patternDescr = new PatternDescr(Cheese.class
1143:                        .getName(), "stilton");
1144:
1145:                final FieldConstraintDescr literalDescr = new FieldConstraintDescr(
1146:                        "type");
1147:                literalDescr.addRestriction(new LiteralRestrictionDescr("==",
1148:                        "stilton"));
1149:
1150:                patternDescr.addConstraint(literalDescr);
1151:
1152:                ceDescr.addDescr(patternDescr);
1153:
1154:                ruleDescr.setConsequence(consequence);
1155:
1156:                builder.addPackage(packageDescr);
1157:
1158:                final Package pkg = (Package) builder.getPackage();
1159:                final Rule rule = pkg.getRule("rule-1");
1160:
1161:                assertEquals("rule-1", rule.getName());
1162:
1163:                return rule;
1164:            }
1165:
1166:            public void testRuleFlow() throws Exception {
1167:                PackageBuilder builder = new PackageBuilder();
1168:
1169:                InputStream in = this .getClass().getResourceAsStream(
1170:                        "/org/drools/integrationtests/ruleflow.rfm");
1171:                assertNotNull(in);
1172:
1173:                builder.addPackage(new PackageDescr("ya"));
1174:
1175:                builder.addRuleFlow(new InputStreamReader(in));
1176:                Package pkg = builder.getPackage();
1177:                assertNotNull(pkg);
1178:
1179:                Map flows = pkg.getRuleFlows();
1180:                assertNotNull(flows);
1181:                assertEquals(1, flows.size());
1182:
1183:                assertTrue(flows.containsKey("0"));
1184:
1185:                Process p = (Process) flows.get("0");
1186:                assertTrue(p instanceof  RuleFlowProcessImpl);
1187:
1188:                //now serialization
1189:                ByteArrayOutputStream data = new ByteArrayOutputStream();
1190:                ObjectOutputStream out = new ObjectOutputStream(data);
1191:                out.writeObject(pkg);
1192:
1193:                ObjectInputStream objIn = new DroolsObjectInputStream(
1194:                        new ByteArrayInputStream(data.toByteArray()));
1195:                Package pkg2 = (Package) objIn.readObject();
1196:                assertNotNull(pkg2);
1197:
1198:                flows = pkg2.getRuleFlows();
1199:                assertNotNull(flows);
1200:                assertEquals(1, flows.size());
1201:                assertTrue(flows.containsKey("0"));
1202:                p = (Process) flows.get("0");
1203:                assertTrue(p instanceof  RuleFlowProcessImpl);
1204:            }
1205:
1206:            public void testPackageRuleFlows() throws Exception {
1207:                Package pkg = new Package("boo");
1208:                Process rf = new MockRuleFlow("1");
1209:                pkg.addRuleFlow(rf);
1210:                assertTrue(pkg.getRuleFlows().containsKey("1"));
1211:                assertSame(rf, pkg.getRuleFlows().get("1"));
1212:
1213:                Process rf2 = new MockRuleFlow("2");
1214:                pkg.addRuleFlow(rf2);
1215:                assertTrue(pkg.getRuleFlows().containsKey("1"));
1216:                assertSame(rf, pkg.getRuleFlows().get("1"));
1217:                assertTrue(pkg.getRuleFlows().containsKey("2"));
1218:                assertSame(rf2, pkg.getRuleFlows().get("2"));
1219:
1220:                pkg.removeRuleFlow("1");
1221:                assertTrue(pkg.getRuleFlows().containsKey("2"));
1222:                assertSame(rf2, pkg.getRuleFlows().get("2"));
1223:                assertFalse(pkg.getRuleFlows().containsKey("1"));
1224:
1225:            }
1226:
1227:            public void testJaninoWithStaticImports() throws Exception {
1228:                PackageBuilderConfiguration cfg = new PackageBuilderConfiguration();
1229:                JavaDialectConfiguration javaConf = (JavaDialectConfiguration) cfg
1230:                        .getDialectConfiguration("java");
1231:                javaConf.setCompiler(JavaDialectConfiguration.JANINO);
1232:
1233:                PackageBuilder bldr = new PackageBuilder(cfg);
1234:                bldr
1235:                        .addPackageFromDrl(new StringReader(
1236:                                "package testBuilderPackageConfig \n import java.util.List"));
1237:                bldr
1238:                        .addPackageFromDrl(new StringReader(
1239:                                "function void doSomething() {\n System.err.println(List.class.toString()); }"));
1240:
1241:                assertFalse(bldr.hasErrors());
1242:
1243:            }
1244:
1245:            class MockRuleFlow implements  Process {
1246:
1247:                private String id;
1248:
1249:                MockRuleFlow(String id) {
1250:                    this .id = id;
1251:                }
1252:
1253:                public String getId() {
1254:                    return id;
1255:                }
1256:
1257:                public String getName() {
1258:                    return null;
1259:                }
1260:
1261:                public String getType() {
1262:                    return null;
1263:                }
1264:
1265:                public String getVersion() {
1266:                    return null;
1267:                }
1268:
1269:                public void setId(String id) {
1270:                }
1271:
1272:                public void setName(String name) {
1273:                }
1274:
1275:                public void setType(String type) {
1276:                }
1277:
1278:                public void setVersion(String version) {
1279:                }
1280:
1281:            }
1282:
1283:            class MockActivation implements  Activation {
1284:                private Rule rule;
1285:                private int salience;
1286:                private final GroupElement subrule;
1287:                private Tuple tuple;
1288:
1289:                public MockActivation(final Rule rule, int salience,
1290:                        final GroupElement subrule, final Tuple tuple) {
1291:                    this .rule = rule;
1292:                    this .salience = salience;
1293:                    this .tuple = tuple;
1294:                    this .subrule = subrule;
1295:                }
1296:
1297:                public Rule getRule() {
1298:                    return this .rule;
1299:                }
1300:
1301:                public int getSalience() {
1302:                    return this .salience;
1303:                }
1304:
1305:                public Tuple getTuple() {
1306:                    return this .tuple;
1307:                }
1308:
1309:                public PropagationContext getPropagationContext() {
1310:                    return null;
1311:                }
1312:
1313:                public long getActivationNumber() {
1314:                    return 0;
1315:                }
1316:
1317:                public void remove() {
1318:                }
1319:
1320:                public void addLogicalDependency(final LogicalDependency node) {
1321:                }
1322:
1323:                public LinkedList getLogicalDependencies() {
1324:                    return null;
1325:                }
1326:
1327:                public boolean isActivated() {
1328:                    return false;
1329:                }
1330:
1331:                public void setActivated(final boolean activated) {
1332:                }
1333:
1334:                public ActivationGroupNode getActivationGroupNode() {
1335:                    // TODO Auto-generated method stub
1336:                    return null;
1337:                }
1338:
1339:                public void setActivationGroupNode(
1340:                        final ActivationGroupNode activationGroupNode) {
1341:                    // TODO Auto-generated method stub
1342:
1343:                }
1344:
1345:                public GroupElement getSubRule() {
1346:                    return this .subrule;
1347:                }
1348:
1349:                public AgendaGroup getAgendaGroup() {
1350:                    // TODO Auto-generated method stub
1351:                    return null;
1352:                }
1353:
1354:                public RuleFlowGroupNode getRuleFlowGroupNode() {
1355:                    // TODO Auto-generated method stub
1356:                    return null;
1357:                }
1358:
1359:                public void setRuleFlowGroupNode(
1360:                        final RuleFlowGroupNode ruleFlowGroupNode) {
1361:                    // TODO Auto-generated method stub
1362:
1363:                }
1364:
1365:                public void setLogicalDependencies(LinkedList justified) {
1366:                    // TODO Auto-generated method stub
1367:
1368:                }
1369:            }
1370:
1371:            class MockTuple implements  Tuple {
1372:                private Map declarations;
1373:
1374:                public MockTuple(final Map declarations) {
1375:                    this .declarations = declarations;
1376:                }
1377:
1378:                public InternalFactHandle get(final int patern) {
1379:                    return null;
1380:                }
1381:
1382:                public InternalFactHandle get(final Declaration declaration) {
1383:                    return (InternalFactHandle) this .declarations
1384:                            .get(declaration);
1385:                }
1386:
1387:                public InternalFactHandle[] getFactHandles() {
1388:                    return (InternalFactHandle[]) this .declarations.values()
1389:                            .toArray(new FactHandle[0]);
1390:                }
1391:
1392:                public boolean dependsOn(final FactHandle handle) {
1393:                    return false;
1394:                }
1395:
1396:                public void setActivation(final Activation activation) {
1397:                }
1398:
1399:                public long getRecency() {
1400:                    // TODO Auto-generated method stub
1401:                    return 0;
1402:                }
1403:
1404:                public int size() {
1405:                    // TODO Auto-generated method stub
1406:                    return 0;
1407:                }
1408:
1409:            }
1410:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.