001: package org.drools.rule.builder.dialect.mvel;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.List;
006: import java.util.Map;
007:
008: import junit.framework.TestCase;
009:
010: import org.drools.Cheese;
011: import org.drools.RuleBase;
012: import org.drools.RuleBaseFactory;
013: import org.drools.base.ClassFieldExtractorCache;
014: import org.drools.base.ClassObjectType;
015: import org.drools.common.InternalFactHandle;
016: import org.drools.common.InternalWorkingMemory;
017: import org.drools.compiler.DialectConfiguration;
018: import org.drools.compiler.DialectRegistry;
019: import org.drools.compiler.PackageBuilder;
020: import org.drools.compiler.PackageBuilderConfiguration;
021: import org.drools.lang.descr.PredicateDescr;
022: import org.drools.lang.descr.RuleDescr;
023: import org.drools.reteoo.ReteTuple;
024: import org.drools.rule.Declaration;
025: import org.drools.rule.Package;
026: import org.drools.rule.Pattern;
027: import org.drools.rule.PredicateConstraint;
028: import org.drools.rule.PredicateConstraint.PredicateContextEntry;
029: import org.drools.spi.FieldExtractor;
030:
031: public class MVELPredicateBuilderTest extends TestCase {
032:
033: public void setUp() {
034: }
035:
036: public void testSimpleExpression() {
037: final Package pkg = new Package("pkg1");
038: final RuleDescr ruleDescr = new RuleDescr("rule 1");
039:
040: PackageBuilder pkgBuilder = new PackageBuilder(pkg);
041: final PackageBuilderConfiguration conf = pkgBuilder
042: .getPackageBuilderConfiguration();
043: MVELDialect mvelDialect = (MVELDialect) ((DialectConfiguration) conf
044: .getDialectConfiguration("mvel")).getDialect();
045:
046: final InstrumentedBuildContent context = new InstrumentedBuildContent(
047: conf, pkg, ruleDescr, conf.getDialectRegistry(),
048: mvelDialect);
049:
050: final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
051: final FieldExtractor extractor = ClassFieldExtractorCache
052: .getExtractor(Cheese.class, "price", getClass()
053: .getClassLoader());
054:
055: final Pattern patternA = new Pattern(0, new ClassObjectType(
056: int.class));
057:
058: final Pattern patternB = new Pattern(1, new ClassObjectType(
059: int.class));
060:
061: final Declaration a = new Declaration("a", extractor, patternA);
062: final Declaration b = new Declaration("b", extractor, patternB);
063:
064: final Map map = new HashMap();
065: map.put("a", a);
066: map.put("b", b);
067: declarationResolver.setDeclarations(map);
068: context.setDeclarationResolver(declarationResolver);
069:
070: final PredicateDescr predicateDescr = new PredicateDescr();
071: predicateDescr.setContent("a == b");
072:
073: final MVELPredicateBuilder builder = new MVELPredicateBuilder();
074:
075: final List[] usedIdentifiers = new ArrayList[2];
076: final List list = new ArrayList();
077: usedIdentifiers[1] = list;
078:
079: final Declaration[] previousDeclarations = new Declaration[] { a };
080: final Declaration[] localDeclarations = new Declaration[] { b };
081:
082: final PredicateConstraint predicate = new PredicateConstraint(
083: null, localDeclarations);
084:
085: builder.build(context, usedIdentifiers, previousDeclarations,
086: localDeclarations, predicate, predicateDescr);
087:
088: final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
089: final InternalWorkingMemory wm = (InternalWorkingMemory) ruleBase
090: .newStatefulSession();
091:
092: final Cheese stilton = new Cheese("stilton", 10);
093:
094: final Cheese cheddar = new Cheese("cheddar", 10);
095: final InternalFactHandle f0 = (InternalFactHandle) wm
096: .insert(cheddar);
097: final ReteTuple tuple = new ReteTuple(f0);
098:
099: final PredicateContextEntry predicateContext = new PredicateContextEntry();
100: predicateContext.leftTuple = tuple;
101:
102: assertTrue(predicate.isAllowedCachedLeft(predicateContext,
103: stilton));
104:
105: cheddar.setPrice(9);
106: wm.update(f0, cheddar);
107:
108: assertFalse(predicate.isAllowedCachedLeft(predicateContext,
109: stilton));
110: }
111:
112: }
|