Search in sources :

Example 41 with Cheese

use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.

the class DynamicRulesTest method testDynamicFunction.

@Ignore("Fails with standard-drl after changing to new API. See DROOLS-6060")
@Test(timeout = 10000)
public void testDynamicFunction() throws Exception {
    // JBRULES-1258 serialising a package breaks function removal -- left the serialisation commented out for now
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicFunction1.drl").getKiePackages();
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources("test", getClass(), kieBaseTestConfiguration);
    kbase.addPackages(kpkgs);
    // kbase = SerializationHelper.serializeObject( kbase );
    KieSession workingMemory = kbase.newKieSession();
    final List<?> list = new ArrayList<Object>();
    workingMemory.setGlobal("list", list);
    final Cheese stilton = new Cheese("stilton", 5);
    workingMemory.insert(stilton);
    workingMemory.fireAllRules();
    assertEquals(new Integer(5), list.get(0));
    // Check a function can be removed from a package.
    // Once removed any efforts to use it should throw an Exception
    kbase.removeFunction("org.drools.mvel.compiler.test", "addFive");
    final Cheese cheddar = new Cheese("cheddar", 5);
    workingMemory.insert(cheddar);
    try {
        workingMemory.fireAllRules();
        fail("Function should have been removed and NoClassDefFoundError thrown from the Consequence");
    } catch (final Throwable e) {
    }
    // Check a new function can be added to replace an old function
    Collection<KiePackage> kpkgs2 = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicFunction2.drl").getKiePackages();
    kbase.addPackages(kpkgs2);
    final Cheese brie = new Cheese("brie", 5);
    workingMemory.insert(brie);
    workingMemory.fireAllRules();
    assertEquals(new Integer(6), list.get(1));
    Collection<KiePackage> kpkgs3 = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicFunction3.drl").getKiePackages();
    kbase.addPackages(kpkgs3);
    final Cheese feta = new Cheese("feta", 5);
    workingMemory.insert(feta);
    workingMemory.fireAllRules();
    assertEquals(new Integer(5), list.get(2));
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 42 with Cheese

use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.

the class DynamicRulesTest method testJBRULES_2206.

@Test(timeout = 10000)
public void testJBRULES_2206() {
    KieBaseConfiguration config = RuleBaseFactory.newKnowledgeBaseConfiguration();
    ((RuleBaseConfiguration) config).setRuleBaseUpdateHandler(null);
    final KieModule kieModule = KieUtil.getKieModuleFromResources(KieUtil.generateReleaseId("test"), kieBaseTestConfiguration);
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.newKieBaseFromReleaseId(kieModule.getReleaseId(), config);
    KieSession session = kbase.newKieSession();
    AgendaEventListener ael = mock(AgendaEventListener.class);
    session.addEventListener(ael);
    for (int i = 0; i < 5; i++) {
        session.insert(new Cheese());
    }
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_JBRULES_2206_1.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    ((InternalAgenda) session.getAgenda()).evaluateEagerList();
    // two matching rules were added, so 2 activations should have been created
    verify(ael, times(2)).matchCreated(any(MatchCreatedEvent.class));
    int fireCount = session.fireAllRules();
    // both should have fired
    assertEquals(2, fireCount);
    kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_JBRULES_2206_2.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    ((InternalAgenda) session.getAgenda()).evaluateEagerList();
    // one rule was overridden and should activate
    verify(ael, times(3)).matchCreated(any(MatchCreatedEvent.class));
    fireCount = session.fireAllRules();
    // that rule should fire again
    assertEquals(1, fireCount);
    session.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) InternalAgenda(org.drools.core.common.InternalAgenda) KiePackage(org.kie.api.definition.KiePackage) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieModule(org.kie.api.builder.KieModule) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 43 with Cheese

use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.

the class FireAllRulesCommandTest method infiniteLoopTerminatesAtMaxTest.

@Test
public void infiniteLoopTerminatesAtMaxTest() {
    String str = "";
    str += "package org.drools.mvel.integrationtests \n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "rule StringRule \n";
    str += " when \n";
    str += " $c : Cheese() \n";
    str += " then \n";
    str += " update($c); \n";
    str += "end \n";
    StatelessKieSession ksession = getSession(str);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert(new Cheese("stilton")));
    FireAllRulesCommand farc = (FireAllRulesCommand) CommandFactory.newFireAllRules(10);
    farc.setOutIdentifier("num-rules-fired");
    commands.add(farc);
    ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
    int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
    assertEquals(10, fired);
}
Also used : Command(org.kie.api.command.Command) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Cheese(org.drools.mvel.compiler.Cheese) Test(org.junit.Test)

Example 44 with Cheese

use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.

the class FireAllRulesCommandTest method fiveRulesFiredTest.

@Test
public void fiveRulesFiredTest() {
    String str = "";
    str += "package org.drools.mvel.integrationtests \n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "rule StringRule \n";
    str += " when \n";
    str += " $c : Cheese() \n";
    str += " then \n";
    str += " System.out.println($c); \n";
    str += "end \n";
    StatelessKieSession ksession = getSession(str);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert(new Cheese("stilton")));
    commands.add(CommandFactory.newInsert(new Cheese("gruyere")));
    commands.add(CommandFactory.newInsert(new Cheese("cheddar")));
    commands.add(CommandFactory.newInsert(new Cheese("stinky")));
    commands.add(CommandFactory.newInsert(new Cheese("limburger")));
    commands.add(CommandFactory.newFireAllRules("num-rules-fired"));
    ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
    int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
    assertEquals(5, fired);
}
Also used : Command(org.kie.api.command.Command) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Cheese(org.drools.mvel.compiler.Cheese) Test(org.junit.Test)

Example 45 with Cheese

use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.

the class FireAllRulesCommandTest method oneRuleFiredWithDefinedMaxTest.

@Test
public void oneRuleFiredWithDefinedMaxTest() {
    String str = "";
    str += "package org.drools.mvel.integrationtests \n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "rule StringRule \n";
    str += " when \n";
    str += " $c : Cheese() \n";
    str += " then \n";
    str += " System.out.println($c); \n";
    str += "end \n";
    StatelessKieSession ksession = getSession(str);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert(new Cheese("stilton")));
    FireAllRulesCommand farc = (FireAllRulesCommand) CommandFactory.newFireAllRules(10);
    farc.setOutIdentifier("num-rules-fired");
    commands.add(farc);
    ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
    int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
    assertEquals(1, fired);
}
Also used : Command(org.kie.api.command.Command) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Cheese(org.drools.mvel.compiler.Cheese) Test(org.junit.Test)

Aggregations

Cheese (org.drools.mvel.compiler.Cheese)136 Test (org.junit.Test)129 KieSession (org.kie.api.runtime.KieSession)112 KieBase (org.kie.api.KieBase)96 ArrayList (java.util.ArrayList)90 List (java.util.List)65 Person (org.drools.mvel.compiler.Person)47 FactHandle (org.kie.api.runtime.rule.FactHandle)29 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)24 KiePackage (org.kie.api.definition.KiePackage)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)20 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)20 IteratorToList (org.drools.mvel.integrationtests.IteratorToList)14 Command (org.kie.api.command.Command)10 ExecutionResults (org.kie.api.runtime.ExecutionResults)10 PackageDescr (org.drools.drl.ast.descr.PackageDescr)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)6 Cheesery (org.drools.mvel.compiler.Cheesery)6 ClassObjectFilter (org.drools.core.ClassObjectFilter)5 ClassObjectType (org.drools.core.base.ClassObjectType)5