Search in sources :

Example 81 with FactHandle

use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.

the class ExtendsTest method testDeclareInheritance.

@Test
public void testDeclareInheritance() throws Exception {
    String s1 = "package org.drools;\n" + "import org.drools.compiler.integrationtests.ExtendsTest.*;\n" + "\n" + "declare A \n" + " @role( event )" + " @typesafe( false )\n" + "end\n" + "" + "declare C @role( event ) @typesafe( false ) end \n" + "" + "rule R \n" + "when " + "   $x : C( this.x == 1 ) \n" + "then\n" + "   System.out.println( $x ); \n" + "end\n" + "";
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
    }
    assertFalse(kBuilder.hasErrors());
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
    KieSession knowledgeSession = knowledgeBase.newKieSession();
    FactHandle h = knowledgeSession.insert(new X());
    assertTrue(((InternalFactHandle) h).isEvent());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 82 with FactHandle

use of org.kie.api.runtime.rule.FactHandle 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 83 with FactHandle

use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.

the class FirstOrderLogicTest method testCollectModifyAlphaRestriction.

@Test
public void testCollectModifyAlphaRestriction() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CollectAlphaRestriction.drl");
    KieSession wm = createKnowledgeSession(kbase);
    final List results = new ArrayList();
    wm.setGlobal("results", results);
    final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 2), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 16), new Cheese("provolone", 8) };
    final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
    for (int i = 0; i < cheese.length; i++) {
        cheeseHandles[i] = (FactHandle) wm.insert(cheese[i]);
    }
    // ---------------- 1st scenario
    int fireCount = 0;
    wm.fireAllRules();
    assertEquals(++fireCount, results.size());
    assertEquals(3, ((Collection) results.get(fireCount - 1)).size());
    assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
    // ---------------- 2nd scenario
    final int index = 1;
    cheese[index].setType("brie");
    wm.update(cheeseHandles[index], cheese[index]);
    wm.fireAllRules();
    assertEquals(++fireCount, results.size());
    assertEquals(2, ((Collection) results.get(fireCount - 1)).size());
    assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
    // ---------------- 3rd scenario
    wm.retract(cheeseHandles[2]);
    wm.fireAllRules();
    assertEquals(++fireCount, results.size());
    assertEquals(1, ((Collection) results.get(fireCount - 1)).size());
    assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
}
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) Test(org.junit.Test)

Example 84 with FactHandle

use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.

the class FirstOrderLogicTest method testCollectModify.

@Test
public void testCollectModify() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_Collect.drl");
    KieSession wm = createKnowledgeSession(kbase);
    List results = new ArrayList();
    wm.setGlobal("results", results);
    final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 2), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 16), new Cheese("provolone", 8) };
    final Person bob = new Person("Bob", "stilton");
    final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
    for (int i = 0; i < cheese.length; i++) {
        cheeseHandles[i] = (FactHandle) wm.insert(cheese[i]);
    }
    final FactHandle bobHandle = (FactHandle) wm.insert(bob);
    // ---------------- 1st scenario
    int fireCount = 0;
    wm.fireAllRules();
    assertEquals(++fireCount, results.size());
    assertEquals(3, ((Collection) results.get(fireCount - 1)).size());
    assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
    // ---------------- 2nd scenario
    final int index = 1;
    cheese[index].setPrice(9);
    wm.update(cheeseHandles[index], cheese[index]);
    wm.fireAllRules();
    assertEquals(++fireCount, results.size());
    assertEquals(3, ((Collection) results.get(fireCount - 1)).size());
    assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
    // ---------------- 3rd scenario
    bob.setLikes("brie");
    wm.update(bobHandle, bob);
    wm.fireAllRules();
    assertEquals(fireCount, results.size());
    // ---------------- 4th scenario
    wm.retract(cheeseHandles[3]);
    wm.fireAllRules();
    // should not have fired as per constraint
    assertEquals(fireCount, results.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 85 with FactHandle

use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.

the class FirstOrderLogicTest method testForallSinglePattern.

@Test
public void testForallSinglePattern() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_ForallSinglePattern.drl");
    KieSession workingMemory = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    workingMemory.setGlobal("results", list);
    int fired = 0;
    // no cheeses, so should fire
    workingMemory.fireAllRules();
    assertEquals(++fired, list.size());
    // only stilton, so should not fire again
    FactHandle stilton1 = (FactHandle) workingMemory.insert(new Cheese("stilton", 10));
    workingMemory.fireAllRules();
    assertEquals(fired, list.size());
    // only stilton, so should not fire again
    FactHandle stilton2 = (FactHandle) workingMemory.insert(new Cheese("stilton", 11));
    workingMemory.fireAllRules();
    assertEquals(fired, list.size());
    // still only stilton, so should not fire
    workingMemory.retract(stilton1);
    workingMemory.fireAllRules();
    assertEquals(fired, list.size());
    // there is a brie, so should not fire
    FactHandle brie = (FactHandle) workingMemory.insert(new Cheese("brie", 10));
    workingMemory.fireAllRules();
    assertEquals(fired, list.size());
    // no brie anymore, so should fire
    workingMemory.retract(brie);
    workingMemory.fireAllRules();
    assertEquals(++fired, list.size());
    // no more cheese, but since it already fired, should not fire again
    workingMemory.retract(stilton2);
    workingMemory.fireAllRules();
    assertEquals(fired, 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) Test(org.junit.Test)

Aggregations

FactHandle (org.kie.api.runtime.rule.FactHandle)650 Test (org.junit.Test)547 KieSession (org.kie.api.runtime.KieSession)506 KieBase (org.kie.api.KieBase)304 ArrayList (java.util.ArrayList)298 InternalFactHandle (org.drools.core.common.InternalFactHandle)192 List (java.util.List)165 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)119 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)66 KieHelper (org.kie.internal.utils.KieHelper)50 Person (org.drools.modelcompiler.domain.Person)46 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)43 HashMap (java.util.HashMap)41 Person (org.drools.compiler.Person)41 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)39 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)39 Person (org.drools.mvel.compiler.Person)38 Ignore (org.junit.Ignore)36 Match (org.kie.api.runtime.rule.Match)36 Collection (java.util.Collection)34