Search in sources :

Example 1 with Cell

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

the class CellTest method testCell.

@Test
public void testCell() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("evalmodify.drl"));
    final Environment env = EnvironmentFactory.newEnvironment();
    env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
    KieSession session = createKieSession(kbase, null, env);
    final Cell cell1 = new Cell(9);
    final Cell cell = new Cell(0);
    session.insert(cell1);
    session.insert(cell);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    assertEquals(9, cell.getValue());
}
Also used : IdentityPlaceholderResolverStrategy(org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy) KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) Cell(org.drools.compiler.Cell) Test(org.junit.Test)

Example 2 with Cell

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

the class ExecutionFlowControlTest method testLockOnActiveWithModify2.

@Test
public void testLockOnActiveWithModify2() throws Exception {
    KieBase kbase = loadKnowledgeBase("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.compiler.Neighbor) KieSession(org.kie.api.runtime.KieSession) Cell(org.drools.compiler.Cell) Test(org.junit.Test)

Example 3 with Cell

use of org.drools.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.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.compiler.Cell.class ); }
         */
    // making the default explicit:
    KieSession ksession = new KieHelper(PropertySpecificOption.ALWAYS).addContent(drl, ResourceType.DRL).build().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) Neighbor(org.drools.compiler.Neighbor) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Cell(org.drools.compiler.Cell) Test(org.junit.Test)

Example 4 with Cell

use of org.drools.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";
    rule1 += "import org.drools.compiler.Person\n";
    rule1 += "import org.drools.compiler.Cheese\n";
    rule1 += "global java.util.List list\n";
    rule1 += "rule \"Rule 1\"\n";
    rule1 += "when\n";
    rule1 += "    $c : Cheese( ) \n";
    rule1 += "    $p : Person( cheese == $c ) \n";
    rule1 += "then\n";
    rule1 += "    list.add( $p );\n";
    rule1 += "end";
    String rule2 = "package org.drools.compiler.test;\n";
    rule2 += "import org.drools.compiler.Person\n";
    rule2 += "import org.drools.compiler.Cheese\n";
    rule2 += "import org.drools.compiler.Cell\n";
    rule2 += "global java.util.List list\n";
    rule2 += "rule \"Rule 2\"\n";
    rule2 += "when\n";
    rule2 += "    $c : Cheese( ) \n";
    rule2 += "    $p : Person( cheese == $c ) \n";
    rule2 += "    $x : Cell( value == $p.age ) \n";
    rule2 += "then\n";
    rule2 += "    list.add( $x );\n";
    rule2 += "end";
    String rule3 = "package org.drools.compiler.test;\n";
    rule3 += "import org.drools.compiler.FactA\n";
    rule3 += "import org.drools.compiler.FactB\n";
    rule3 += "import org.drools.compiler.FactC\n";
    rule3 += "import org.drools.compiler.Person\n";
    rule3 += "global java.util.List list\n";
    rule3 += "rule \"Rule 3\"\n";
    rule3 += "when\n";
    rule3 += "    $a : FactA( field2 > 10 ) \n";
    rule3 += "    $b : FactB( f2 >= $a.field2 ) \n";
    rule3 += "    $p : Person( name == \"darth vadar\" ) \n";
    rule3 += "    $c : FactC( f2 >= $b.f2 ) \n";
    rule3 += "then\n";
    rule3 += "    list.add( $c );\n";
    rule3 += "    drools.halt();\n";
    rule3 += "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 = SerializationHelper.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 = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    Cell cell30 = new Cell(30);
    session.insert(cell30);
    Cell cell58 = new Cell(58);
    session.insert(cell58);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    assertEquals(5, list.size());
    assertTrue(list.contains(cell30));
    assertTrue(list.contains(cell58));
    session = SerializationHelper.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 = SerializationHelper.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) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Cell(org.drools.compiler.Cell) FactA(org.drools.compiler.FactA) FactB(org.drools.compiler.FactB) FactC(org.drools.compiler.FactC) Test(org.junit.Test)

Aggregations

Cell (org.drools.compiler.Cell)4 Test (org.junit.Test)4 KieSession (org.kie.api.runtime.KieSession)4 KieBase (org.kie.api.KieBase)3 ArrayList (java.util.ArrayList)2 Neighbor (org.drools.compiler.Neighbor)2 List (java.util.List)1 Cheese (org.drools.compiler.Cheese)1 FactA (org.drools.compiler.FactA)1 FactB (org.drools.compiler.FactB)1 FactC (org.drools.compiler.FactC)1 Person (org.drools.compiler.Person)1 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)1 DebugAgendaEventListener (org.drools.core.event.DebugAgendaEventListener)1 IdentityPlaceholderResolverStrategy (org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy)1 Environment (org.kie.api.runtime.Environment)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1 KieHelper (org.kie.internal.utils.KieHelper)1