Search in sources :

Example 1 with Neighbor

use of org.drools.mvel.compiler.Neighbor 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 2 with Neighbor

use of org.drools.mvel.compiler.Neighbor 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)2 Neighbor (org.drools.mvel.compiler.Neighbor)2 Test (org.junit.Test)2 KieBase (org.kie.api.KieBase)2 KieSession (org.kie.api.runtime.KieSession)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 DebugAgendaEventListener (org.drools.core.event.DebugAgendaEventListener)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1