@Before public void setUp() { this.ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase(); pkg = new org.drools.rule.Package("org.droos.test"); pkg.setClassFieldAccessorCache( new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader())); JavaDialectRuntimeData data = new JavaDialectRuntimeData(); data.onAdd(pkg.getDialectRuntimeRegistry(), ruleBase.getRootClassLoader()); pkg.getDialectRuntimeRegistry().setDialectData("java", data); // we need to add one rule to the package because the previous deadlock was encountered // while removing rules from a package when said package is removed from the rulebase rule = new Rule("Test"); rule.setDialect("java"); rule.setConsequence( new Consequence() { public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {} public String getName() { return "default"; } }); pkg.addRule(rule); ruleBase.addPackage(pkg); }
@Test public void testAddPackage() throws Exception { final org.drools.rule.Package pkg1 = new org.drools.rule.Package("org.droos.test"); pkg1.setClassFieldAccessorCache( new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader())); pkg1.addGlobal("global1", Object.class); pkg1.addGlobal("global2", Object.class); final org.drools.rule.Package pkg2 = new org.drools.rule.Package("org.droos.test"); pkg2.setClassFieldAccessorCache( new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader())); pkg2.addGlobal("global1", Object.class); pkg2.addGlobal("global3", Object.class); final org.drools.rule.Package pkg3 = new org.drools.rule.Package("org.droos.test2"); pkg3.setClassFieldAccessorCache( new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader())); pkg3.addGlobal("global3", Object.class); pkg3.addGlobal("global4", Object.class); this.ruleBase.addPackage(pkg1); // one package assertLength(1, this.ruleBase.getPackages()); // two globals assertLength(2, this.ruleBase.getGlobals().values()); // two globals in the package also assertLength(2, this.ruleBase.getPackages()[0].getGlobals().values()); this.ruleBase.addPackage(pkg2); // packages merged, so still 1 package assertLength(1, this.ruleBase.getPackages()); // globals merged, so 3 globals total assertLength(3, this.ruleBase.getGlobals().values()); // three globals in the package also assertLength(3, this.ruleBase.getPackages()[0].getGlobals().values()); this.ruleBase.addPackage(pkg3); // new package, so now we have 2 package assertLength(2, this.ruleBase.getPackages()); // globals partially merged, so 4 globals total assertLength(4, this.ruleBase.getGlobals().values()); // two globals in the package final org.drools.rule.Package[] pkgs = this.ruleBase.getPackages(); for (int i = 0; i < pkgs.length; i++) { if (pkgs[i].getName().equals(pkg3.getName())) { assertLength(2, pkgs[i].getGlobals().values()); } } }
@Test public void testRemovePackage() throws Exception { final org.drools.rule.Package pkg1 = new org.drools.rule.Package("org.droos.test"); pkg1.setClassFieldAccessorCache( new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader())); pkg1.addGlobal("global1", Object.class); pkg1.addGlobal("global2", Object.class); final org.drools.rule.Package pkg2 = new org.drools.rule.Package("org.droos.test"); pkg2.setClassFieldAccessorCache( new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader())); pkg2.addGlobal("global1", Object.class); pkg2.addGlobal("global3", Object.class); final org.drools.rule.Package pkg3 = new org.drools.rule.Package("org.droos.test2"); pkg3.setClassFieldAccessorCache( new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader())); pkg3.addGlobal("global3", Object.class); pkg3.addGlobal("global4", Object.class); this.ruleBase.addPackage(pkg1); this.ruleBase.addPackage(pkg2); this.ruleBase.addPackage(pkg3); this.ruleBase.removePackage(pkg1.getName()); // packages were partially merged when adding, so removal // shall left only package 3 behind assertLength(1, this.ruleBase.getPackages()); assertLength(2, this.ruleBase.getGlobals().values()); this.ruleBase.removePackage(pkg3.getName()); assertLength(0, this.ruleBase.getPackages()); assertLength(0, this.ruleBase.getGlobals().values()); }