01: package org.drools.rule.builder.dialect.mvel;
02:
03: import java.util.HashMap;
04: import java.util.Map;
05:
06: import junit.framework.TestCase;
07:
08: import org.drools.Cheese;
09: import org.drools.RuleBase;
10: import org.drools.RuleBaseFactory;
11: import org.drools.WorkingMemory;
12: import org.drools.base.ClassFieldExtractorCache;
13: import org.drools.base.ClassObjectType;
14: import org.drools.common.InternalFactHandle;
15: import org.drools.compiler.DialectConfiguration;
16: import org.drools.compiler.DialectRegistry;
17: import org.drools.compiler.PackageBuilder;
18: import org.drools.compiler.PackageBuilderConfiguration;
19: import org.drools.lang.descr.EvalDescr;
20: import org.drools.lang.descr.RuleDescr;
21: import org.drools.reteoo.ReteTuple;
22: import org.drools.rule.Declaration;
23: import org.drools.rule.EvalCondition;
24: import org.drools.rule.Package;
25: import org.drools.rule.Pattern;
26: import org.drools.spi.FieldExtractor;
27:
28: public class MVELEvalBuilderTest extends TestCase {
29:
30: public void setUp() {
31: }
32:
33: public void testSimpleExpression() {
34: final Package pkg = new Package("pkg1");
35: final RuleDescr ruleDescr = new RuleDescr("rule 1");
36:
37: PackageBuilder pkgBuilder = new PackageBuilder(pkg);
38: final PackageBuilderConfiguration conf = pkgBuilder
39: .getPackageBuilderConfiguration();
40: MVELDialect mvelDialect = (MVELDialect) ((DialectConfiguration) conf
41: .getDialectConfiguration("mvel")).getDialect();
42:
43: final InstrumentedBuildContent context = new InstrumentedBuildContent(
44: conf, pkg, ruleDescr, conf.getDialectRegistry(),
45: mvelDialect);
46:
47: final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
48:
49: final FieldExtractor extractor = ClassFieldExtractorCache
50: .getExtractor(Cheese.class, "price", getClass()
51: .getClassLoader());
52:
53: final Pattern pattern = new Pattern(0, new ClassObjectType(
54: int.class));
55: final Declaration declaration = new Declaration("a", extractor,
56: pattern);
57: final Map map = new HashMap();
58: map.put("a", declaration);
59: declarationResolver.setDeclarations(map);
60: context.setDeclarationResolver(declarationResolver);
61:
62: final EvalDescr evalDescr = new EvalDescr();
63: evalDescr.setContent("a == 10");
64:
65: final MVELEvalBuilder builder = new MVELEvalBuilder();
66: final EvalCondition eval = (EvalCondition) builder.build(
67: context, evalDescr);
68:
69: final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
70: final WorkingMemory wm = ruleBase.newStatefulSession();
71:
72: final Cheese cheddar = new Cheese("cheddar", 10);
73: final InternalFactHandle f0 = (InternalFactHandle) wm
74: .insert(cheddar);
75: final ReteTuple tuple = new ReteTuple(f0);
76:
77: assertTrue(eval.isAllowed(tuple, wm));
78:
79: cheddar.setPrice(9);
80: wm.update(f0, cheddar);
81: assertFalse(eval.isAllowed(tuple, wm));
82: }
83:
84: }
|