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.Person;
09: import org.drools.RuleBase;
10: import org.drools.RuleBaseFactory;
11: import org.drools.WorkingMemory;
12: import org.drools.base.ClassObjectType;
13: import org.drools.common.InternalFactHandle;
14: import org.drools.compiler.DialectConfiguration;
15: import org.drools.compiler.DialectRegistry;
16: import org.drools.compiler.PackageBuilder;
17: import org.drools.compiler.PackageBuilderConfiguration;
18: import org.drools.lang.descr.RuleDescr;
19: import org.drools.reteoo.ReteTuple;
20: import org.drools.rule.Declaration;
21: import org.drools.rule.Package;
22: import org.drools.rule.Pattern;
23: import org.drools.rule.builder.SalienceBuilder;
24: import org.drools.spi.ObjectType;
25: import org.drools.spi.PatternExtractor;
26:
27: public class MVELSalienceBuilderTest extends TestCase {
28: public void testSimpleExpression() {
29: final Package pkg = new Package("pkg1");
30: final RuleDescr ruleDescr = new RuleDescr("rule 1");
31: ruleDescr.setSalience("p.age + 20");
32: ruleDescr.setConsequence("");
33:
34: PackageBuilder pkgBuilder = new PackageBuilder(pkg);
35: final PackageBuilderConfiguration conf = pkgBuilder
36: .getPackageBuilderConfiguration();
37: MVELDialect mvelDialect = (MVELDialect) ((DialectConfiguration) conf
38: .getDialectConfiguration("mvel")).getDialect();
39:
40: final InstrumentedBuildContent context = new InstrumentedBuildContent(
41: conf, pkg, ruleDescr, conf.getDialectRegistry(),
42: mvelDialect);
43:
44: final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
45:
46: final ObjectType personObjeectType = new ClassObjectType(
47: Person.class);
48:
49: final Pattern pattern = new Pattern(0, personObjeectType);
50:
51: final PatternExtractor extractor = new PatternExtractor(
52: personObjeectType);
53:
54: final Declaration declaration = new Declaration("p", extractor,
55: pattern);
56: final Map map = new HashMap();
57: map.put("p", declaration);
58: declarationResolver.setDeclarations(map);
59: context.setDeclarationResolver(declarationResolver);
60:
61: final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
62: final WorkingMemory wm = ruleBase.newStatefulSession();
63:
64: final Person p = new Person("mark", "", 31);
65: final InternalFactHandle f0 = (InternalFactHandle) wm.insert(p);
66: final ReteTuple tuple = new ReteTuple(f0);
67:
68: SalienceBuilder salienceBuilder = new MVELSalienceBuilder();
69: salienceBuilder.build(context);
70:
71: assertEquals(51, context.getRule().getSalience().getValue(
72: tuple, wm));
73:
74: }
75: }
|