Search in sources :

Example 1 with Cell

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

the class MarshallingTest method testMultiRuleMultiJoinNodePatternsWithHalt.

@Test
public void testMultiRuleMultiJoinNodePatternsWithHalt() throws Exception {
    String rule1 = "package org.drools.compiler.test;\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule \"Rule 1\"\n" + "when\n" + "    $c : Cheese( ) \n" + "    $p : Person( cheese == $c ) \n" + "then\n" + "    list.add( $p );\n" + "end";
    String rule2 = "package org.drools.compiler.test;\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\n" + "import " + Cell.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule \"Rule 2\"\n" + "when\n" + "    $c : Cheese( ) \n" + "    $p : Person( cheese == $c ) \n" + "    $x : Cell( value == $p.age ) \n" + "then\n" + "    list.add( $x );\n" + "end";
    String rule3 = "package org.drools.compiler.test;\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + FactA.class.getCanonicalName() + "\n" + "import " + FactB.class.getCanonicalName() + "\n" + "import " + FactC.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule \"Rule 3\"\n" + "when\n" + "    $a : FactA( field2 > 10 ) \n" + "    $b : FactB( f2 >= $a.field2 ) \n" + "    $p : Person( name == \"darth vadar\" ) \n" + "    $c : FactC( f2 >= $b.f2 ) \n" + "then\n" + "    list.add( $c );\n" + "    drools.halt();\n" + "end";
    KieBase kBase = loadKnowledgeBaseFromString(rule1, rule2, rule3);
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    Cheese stilton = new Cheese("stilton", 25);
    Cheese brie = new Cheese("brie", 49);
    Person bobba = new Person("bobba fet", 30);
    bobba.setCheese(stilton);
    Person vadar = new Person("darth vadar", 38);
    Person c3po = new Person("c3p0", 17);
    c3po.setCheese(stilton);
    Person r2d2 = new Person("r2d2", 58);
    r2d2.setCheese(brie);
    session.insert(stilton);
    session.insert(bobba);
    session.insert(vadar);
    session.insert(brie);
    session.insert(c3po);
    session.insert(r2d2);
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    list = (List) session.getGlobal("list");
    assertEquals(3, list.size());
    assertTrue(list.contains(r2d2));
    assertTrue(list.contains(c3po));
    assertTrue(list.contains(bobba));
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    Cell cell30 = new Cell(30);
    session.insert(cell30);
    Cell cell58 = new Cell(58);
    session.insert(cell58);
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(5, list.size());
    assertTrue(list.contains(cell30));
    assertTrue(list.contains(cell58));
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.insert(new FactA(15));
    session.insert(new FactB(20));
    FactC factC27 = new FactC(27);
    session.insert(factC27);
    FactC factC52 = new FactC(52);
    session.insert(factC52);
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    session.fireAllRules();
    assertEquals(7, list.size());
    assertTrue(list.contains(factC52));
    assertTrue(list.contains(factC27));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) Cell(org.drools.mvel.compiler.Cell) FactA(org.drools.mvel.compiler.FactA) FactB(org.drools.mvel.compiler.FactB) FactC(org.drools.mvel.compiler.FactC) Test(org.junit.Test)

Example 2 with Cell

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

the class PropertyReactivityBlockerTest method testUpdateRewrittenWithCorrectBitMaskAndCorrectClass.

@Test()
public void testUpdateRewrittenWithCorrectBitMaskAndCorrectClass() {
    String drl = "import " + Cell.class.getCanonicalName() + ";\n" + "import " + Neighbor.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + "    Neighbor( $n : neighbor ) \n" + "then\n" + "    modify( $n ) {\n" + "        setValue( $n.getValue() + 1 )\n" + "    }\n" + "end\n" + "rule C when\n" + "    $c: Cell( value > 0 ) \n" + "then\n" + "   list.add(\"C\"); \n" + "end\n";
    /* The RHS was wrongly rewritten as:
          { org.kie.api.runtime.rule.FactHandle $n__Handle2__ = drools.getFactHandle($n);
            $n.setValue( $n.getValue() + 1 ); 
            drools.update( $n__Handle2__, org.drools.core.util.bitmask.EmptyBitMask.get(), org.drools.mvel.compiler.Neighbor.class ); }
        instead of:
          { org.kie.api.runtime.rule.FactHandle $n__Handle2__ = drools.getFactHandle($n);
            $n.setValue( $n.getValue() + 1 ); 
            drools.update( $n__Handle2__, new org.drools.core.util.bitmask.LongBitMask(16L), org.drools.mvel.compiler.Cell.class ); }
         */
    // making the default explicit:
    Map<String, String> kieModuleConfigurationProperties = new HashMap<>();
    kieModuleConfigurationProperties.put("drools.propertySpecific", "ALWAYS");
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, kieModuleConfigurationProperties, drl);
    final KieSession ksession = kbase.newKieSession();
    System.out.println(drl);
    ReteDumper.dumpRete(ksession);
    ksession.addEventListener(new DebugAgendaEventListener());
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    Cell c0 = new Cell(0);
    Cell c1 = new Cell(0);
    Neighbor n = new Neighbor(c0, c1);
    System.out.println("c0: " + c0);
    System.out.println("c1: " + c1);
    System.out.println("n:" + n);
    ksession.insert(c0);
    ksession.insert(c1);
    ksession.insert(n);
    int x = ksession.fireAllRules();
    System.out.println("from outside:");
    System.out.println("c0: " + c0);
    System.out.println("c1: " + c1);
    System.out.println("n:" + n);
    assertEquals(1, list.size());
    assertEquals("C", list.get(0));
}
Also used : DebugAgendaEventListener(org.drools.core.event.DebugAgendaEventListener) HashMap(java.util.HashMap) KieBase(org.kie.api.KieBase) Neighbor(org.drools.mvel.compiler.Neighbor) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cell(org.drools.mvel.compiler.Cell) Test(org.junit.Test)

Example 3 with Cell

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

the class ExecutionFlowControlTest method testLockOnActiveWithModify2.

@Test
public void testLockOnActiveWithModify2() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(this.getClass(), kieBaseTestConfiguration, "test_LockOnActiveWithModify.drl");
    KieSession ksession = kbase.newKieSession();
    // populating working memory
    final int size = 3;
    Cell[][] cells = new Cell[size][];
    FactHandle[][] handles = new FactHandle[size][];
    for (int row = 0; row < size; row++) {
        cells[row] = new Cell[size];
        handles[row] = new FactHandle[size];
        for (int col = 0; col < size; col++) {
            cells[row][col] = new Cell(Cell.DEAD, row, col);
            handles[row][col] = (FactHandle) ksession.insert(cells[row][col]);
            if (row >= 1 && col >= 1) {
                // northwest
                ksession.insert(new Neighbor(cells[row - 1][col - 1], cells[row][col]));
                ksession.insert(new Neighbor(cells[row][col], cells[row - 1][col - 1]));
            }
            if (row >= 1) {
                // north
                ksession.insert(new Neighbor(cells[row - 1][col], cells[row][col]));
                ksession.insert(new Neighbor(cells[row][col], cells[row - 1][col]));
            }
            if (row >= 1 && col < (size - 1)) {
                // northeast
                ksession.insert(new Neighbor(cells[row - 1][col + 1], cells[row][col]));
                ksession.insert(new Neighbor(cells[row][col], cells[row - 1][col + 1]));
            }
            if (col >= 1) {
                // west
                ksession.insert(new Neighbor(cells[row][col - 1], cells[row][col]));
                ksession.insert(new Neighbor(cells[row][col], cells[row][col - 1]));
            }
        }
    }
    ksession.getAgenda().getAgendaGroup("calculate").clear();
    // now, start playing
    int fired = ksession.fireAllRules(100);
    assertEquals(0, fired);
    ksession.getAgenda().getAgendaGroup("calculate").setFocus();
    fired = ksession.fireAllRules(100);
    // logger.writeToDisk();
    assertEquals(0, fired);
    assertEquals("MAIN", ((InternalAgenda) ksession.getAgenda()).getFocusName());
    // on the fifth day God created the birds and sea creatures
    cells[0][0].setState(Cell.LIVE);
    ksession.update(handles[0][0], cells[0][0]);
    ksession.getAgenda().getAgendaGroup("birth").setFocus();
    ksession.getAgenda().getAgendaGroup("calculate").setFocus();
    fired = ksession.fireAllRules(100);
    // logger.writeToDisk();
    int[][] expected = new int[][] { { 0, 1, 0 }, { 1, 1, 0 }, { 0, 0, 0 } };
    assertEqualsMatrix(size, cells, expected);
    assertEquals("MAIN", ((InternalAgenda) ksession.getAgenda()).getFocusName());
    // on the sixth day God created the animals that walk over the land and
    // the Man
    cells[1][1].setState(Cell.LIVE);
    ksession.update(handles[1][1], cells[1][1]);
    ksession.getAgenda().getAgendaGroup("calculate").setFocus();
    ksession.fireAllRules(100);
    // logger.writeToDisk();
    expected = new int[][] { { 1, 2, 1 }, { 2, 1, 1 }, { 1, 1, 1 } };
    assertEqualsMatrix(size, cells, expected);
    assertEquals("MAIN", ((InternalAgenda) ksession.getAgenda()).getFocusName());
    ksession.getAgenda().getAgendaGroup("birth").setFocus();
    ksession.fireAllRules(100);
    expected = new int[][] { { 1, 2, 1 }, { 2, 1, 1 }, { 1, 1, 1 } };
    assertEqualsMatrix(size, cells, expected);
    assertEquals("MAIN", ((InternalAgenda) ksession.getAgenda()).getFocusName());
    System.out.println("--------");
    ksession.getAgenda().getAgendaGroup("calculate").setFocus();
    ksession.fireAllRules(100);
    // logger.writeToDisk();
    // printMatrix( cells );
    expected = new int[][] { { 3, 3, 2 }, { 3, 3, 2 }, { 2, 2, 1 } };
    assertEqualsMatrix(size, cells, expected);
    assertEquals("MAIN", ((InternalAgenda) ksession.getAgenda()).getFocusName());
    System.out.println("--------");
    // on the seventh day, while God rested, man start killing them all
    cells[0][0].setState(Cell.DEAD);
    ksession.update(handles[0][0], cells[0][0]);
    ksession.getAgenda().getAgendaGroup("calculate").setFocus();
    ksession.fireAllRules(100);
    expected = new int[][] { { 3, 2, 2 }, { 2, 2, 2 }, { 2, 2, 1 } };
    assertEqualsMatrix(size, cells, expected);
    assertEquals("MAIN", ((InternalAgenda) ksession.getAgenda()).getFocusName());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Neighbor(org.drools.mvel.compiler.Neighbor) KieSession(org.kie.api.runtime.KieSession) Cell(org.drools.mvel.compiler.Cell) Test(org.junit.Test)

Aggregations

Cell (org.drools.mvel.compiler.Cell)3 Test (org.junit.Test)3 KieBase (org.kie.api.KieBase)3 KieSession (org.kie.api.runtime.KieSession)3 ArrayList (java.util.ArrayList)2 Neighbor (org.drools.mvel.compiler.Neighbor)2 HashMap (java.util.HashMap)1 List (java.util.List)1 DebugAgendaEventListener (org.drools.core.event.DebugAgendaEventListener)1 Cheese (org.drools.mvel.compiler.Cheese)1 FactA (org.drools.mvel.compiler.FactA)1 FactB (org.drools.mvel.compiler.FactB)1 FactC (org.drools.mvel.compiler.FactC)1 Person (org.drools.mvel.compiler.Person)1 IteratorToList (org.drools.mvel.integrationtests.IteratorToList)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1