Search in sources :

Example 66 with Cheese

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

the class ExecutionFlowControlTest method testInsertRetractNoloop.

@Test
public void testInsertRetractNoloop() throws Exception {
    // read in the source
    KieBase kbase = loadKnowledgeBase("test_Insert_Retract_Noloop.drl");
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new Cheese("stilton", 15));
    ksession.fireAllRules();
    assertEquals(0, ksession.getObjects().size());
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 67 with Cheese

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

the class ExecutionFlowControlTest method testNoLoopWithModify.

@Test
public void testNoLoopWithModify() throws Exception {
    KieBase kbase = loadKnowledgeBase("no-loop_with_modify.drl");
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Cheese brie = new Cheese("brie", 12);
    ksession.insert(brie);
    ksession.fireAllRules();
    assertEquals("Should not loop  and thus size should be 1", 1, list.size());
    assertEquals(50, brie.getPrice());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 68 with Cheese

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

the class FireAllRulesCommandTest method zeroRulesFiredTest.

@Test
public void zeroRulesFiredTest() {
    String str = "";
    str += "package org.drools.compiler.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("not cheese"));
    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(0, 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.compiler.Cheese) Test(org.junit.Test)

Example 69 with Cheese

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

the class FireAllRulesCommandTest method fiveRulesFiredTest.

@Test
public void fiveRulesFiredTest() {
    String str = "";
    str += "package org.drools.compiler.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.compiler.Cheese) Test(org.junit.Test)

Example 70 with Cheese

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

the class FireAllRulesCommandTest method oneRuleFiredTest.

@Test
public void oneRuleFiredTest() {
    String str = "";
    str += "package org.drools.compiler.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.newFireAllRules("num-rules-fired"));
    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) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Aggregations

Cheese (org.drools.compiler.Cheese)222 Test (org.junit.Test)204 KieSession (org.kie.api.runtime.KieSession)195 ArrayList (java.util.ArrayList)152 KieBase (org.kie.api.KieBase)138 List (java.util.List)100 Person (org.drools.compiler.Person)92 FactHandle (org.kie.api.runtime.rule.FactHandle)53 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)36 KiePackage (org.kie.api.definition.KiePackage)22 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)21 InternalFactHandle (org.drools.core.common.InternalFactHandle)20 Cheesery (org.drools.compiler.Cheesery)14 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)14 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)13 HashMap (java.util.HashMap)10 Command (org.kie.api.command.Command)8 ExecutionResults (org.kie.api.runtime.ExecutionResults)8 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)7