private void setupTest(String consequence, Map<String, Object> namedConsequences) { builder = new MVELConsequenceBuilder(); InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools.compiler.test"); pkg.addImport(new ImportDeclaration(Cheese.class.getCanonicalName())); KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl(); KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg, conf); ruleDescr = new RuleDescr("test consequence builder"); ruleDescr.setConsequence(consequence); ruleDescr.addAttribute(new AttributeDescr("dialect", "mvel")); for (Entry<String, Object> entry : namedConsequences.entrySet()) { ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue()); } RuleImpl rule = new RuleImpl(ruleDescr.getName()); rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese")); rule.addPattern(new Pattern(0, new ClassObjectType(Map.class), "$map")); PackageRegistry pkgRegistry = pkgBuilder.getPackageRegistry(pkg.getName()); DialectCompiletimeRegistry reg = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry(); context = new RuleBuildContext( pkgBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect())); context.getBuildStack().push(rule.getLhs()); context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME); for (String name : namedConsequences.keySet()) { context.getDialect().getConsequenceBuilder().build(context, name); } context.getDialect().addRule(context); pkgRegistry.getPackage().addRule(context.getRule()); pkgBuilder.compileAll(); pkgBuilder.reloadAll(); if (pkgBuilder.hasErrors()) { fail(pkgBuilder.getErrors().toString()); } }
/* (non-Javadoc) * @see junit.framework.TestCase#setUp() */ @Before public void setUp() throws Exception { kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(); listener1 = new TestRuleBaseListener("(listener-1) "); listener2 = new TestRuleBaseListener("(listener-2) "); kBase.addEventListener(listener1); kBase.addEventListener(listener2); final RuleImpl rule1 = new RuleImpl("test1"); final ClassObjectType cheeseObjectType = new ClassObjectType(Cheese.class); final Pattern pattern = new Pattern(0, cheeseObjectType); ClassFieldAccessorStore store = new ClassFieldAccessorStore(); store.setClassFieldAccessorCache( new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader())); store.setEagerWire(true); final ClassFieldReader extractor = store.getReader(Cheese.class, "type"); final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar"); final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor); pattern.addConstraint(constraint); rule1.addPattern(pattern); rule1.setConsequence( new Consequence() { private static final long serialVersionUID = 510l; public void evaluate( final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {} public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {} public void writeExternal(ObjectOutput out) throws IOException {} public String getName() { return "default"; } }); final RuleImpl rule2 = new RuleImpl("test2"); final ClassObjectType cheeseObjectType2 = new ClassObjectType(Cheese.class); final Pattern pattern2 = new Pattern(0, cheeseObjectType2); final FieldValue field2 = FieldFactory.getInstance().getFieldValue("stilton"); final MvelConstraint constraint2 = new MvelConstraintTestUtil("type == \"stilton\"", field, extractor); pattern2.addConstraint(constraint2); rule2.addPattern(pattern2); rule2.setConsequence( new Consequence() { private static final long serialVersionUID = 510l; public void evaluate( final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {} public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {} public void writeExternal(ObjectOutput out) throws IOException {} public String getName() { return "default"; } }); pkg = new KnowledgePackageImpl("org.drools.test1"); pkg.addRule(rule1); pkg.addRule(rule2); }