Search in sources :

Example 71 with Cheese

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

the class FireAllRulesCommandTest method infiniteLoopTerminatesAtMaxTest.

@Test
public void infiniteLoopTerminatesAtMaxTest() {
    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 += " 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.compiler.Cheese) Test(org.junit.Test)

Example 72 with Cheese

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

the class FireAllRulesCommandTest method oneRuleFiredWithDefinedMaxTest.

@Test
public void oneRuleFiredWithDefinedMaxTest() {
    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")));
    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.compiler.Cheese) Test(org.junit.Test)

Example 73 with Cheese

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

the class FirstOrderLogicTest method testOr.

@Test
public void testOr() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_OrNesting.drl");
    KieSession workingMemory = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    workingMemory.setGlobal("results", list);
    final Cheese cheddar = new Cheese("cheddar", 7);
    final Cheese provolone = new Cheese("provolone", 5);
    final Cheese brie = new Cheese("brie", 15);
    final Person mark = new Person("mark", "stilton");
    FactHandle ch = (FactHandle) workingMemory.insert(cheddar);
    FactHandle ph = (FactHandle) workingMemory.insert(provolone);
    FactHandle bh = (FactHandle) workingMemory.insert(brie);
    FactHandle markh = (FactHandle) workingMemory.insert(mark);
    workingMemory.fireAllRules();
    assertEquals(1, list.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 74 with Cheese

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

the class FirstOrderLogicTest method testExistsWithBinding.

@Test
public void testExistsWithBinding() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_ExistsWithBindings.drl");
    KieSession wm = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    wm.setGlobal("results", list);
    final Cheese c = new Cheese("stilton", 10);
    final Person p = new Person("Mark", "stilton");
    wm.insert(c);
    wm.insert(p);
    wm.fireAllRules();
    assertTrue(list.contains(c.getType()));
    assertEquals(1, list.size());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 75 with Cheese

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

the class FirstOrderLogicTest method testCollect.

@Test
public void testCollect() throws Exception {
    List results = new ArrayList();
    KieBase kbase = loadKnowledgeBase("test_Collect.drl");
    KieSession wm = createKnowledgeSession(kbase);
    wm.setGlobal("results", results);
    wm.insert(new Cheese("stilton", 10));
    wm.insert(new Cheese("stilton", 7));
    wm.insert(new Cheese("stilton", 8));
    wm.insert(new Cheese("brie", 5));
    wm.insert(new Cheese("provolone", 150));
    wm = SerializationHelper.getSerialisedStatefulKnowledgeSession(wm, true);
    results = (List) wm.getGlobal("results");
    wm.insert(new Cheese("provolone", 20));
    wm.insert(new Person("Bob", "stilton"));
    wm.insert(new Person("Mark", "provolone"));
    wm = SerializationHelper.getSerialisedStatefulKnowledgeSession(wm, true);
    results = (List) wm.getGlobal("results");
    wm.fireAllRules();
    wm = SerializationHelper.getSerialisedStatefulKnowledgeSession(wm, true);
    results = (List) wm.getGlobal("results");
    assertEquals(1, results.size());
    assertEquals(3, ((Collection) results.get(0)).size());
    assertEquals(ArrayList.class.getName(), results.get(0).getClass().getName());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) 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