Search in sources :

Example 6 with OrderItem

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

the class FirstOrderLogicTest method testCollectWithContainsOperators.

@Test
public void testCollectWithContainsOperators() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CollectContainsOperator.drl");
    KieSession workingMemory = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    workingMemory.setGlobal("results", list);
    final Order order1 = new Order(1, "bob");
    final OrderItem item11 = new OrderItem(order1, 1);
    final OrderItem item12 = new OrderItem(order1, 2);
    final Order order2 = new Order(2, "mark");
    final OrderItem item21 = new OrderItem(order2, 1);
    final OrderItem item22 = new OrderItem(order2, 2);
    workingMemory.insert(order1);
    workingMemory.insert(item11);
    workingMemory.insert(item12);
    workingMemory.insert(order2);
    workingMemory.insert(item21);
    workingMemory.insert(item22);
    workingMemory.fireAllRules();
    int index = 0;
    assertEquals(8, list.size());
    assertSame(order1, list.get(index++));
    assertSame(item11, list.get(index++));
    assertSame(order2, list.get(index++));
    assertSame(item21, list.get(index++));
    assertSame(order1, list.get(index++));
    assertSame(item11, list.get(index++));
    assertSame(order2, list.get(index++));
    assertSame(item21, list.get(index++));
}
Also used : Order(org.drools.compiler.Order) KieBase(org.kie.api.KieBase) OrderItem(org.drools.compiler.OrderItem) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with OrderItem

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

the class DynamicRulesTest method testDynamicRuleRemovalsSubNetwork.

@Test(timeout = 10000)
public void testDynamicRuleRemovalsSubNetwork() throws Exception {
    Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicRulesWithSubnetwork1.drl", "test_DynamicRulesWithSubnetwork.drl"));
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
    kbase.addPackages(kpkgs);
    kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicRulesWithSubnetwork2.drl"));
    kbase.addPackages(kpkgs);
    KieSession session = createKnowledgeSession(kbase);
    final List<?> list = new ArrayList<Object>();
    session.setGlobal("results", list);
    Order order = new Order();
    OrderItem item1 = new OrderItem(order, 1, "Adventure Guide Brazil", OrderItem.TYPE_BOOK, 24);
    order.addItem(item1);
    FactHandle item1Fh = session.insert(item1);
    OrderItem item2 = new OrderItem(order, 2, "Prehistoric Britain", OrderItem.TYPE_BOOK, 15);
    order.addItem(item2);
    FactHandle item2Fh = session.insert(item2);
    OrderItem item3 = new OrderItem(order, 3, "Holiday Music", OrderItem.TYPE_CD, 9);
    order.addItem(item3);
    FactHandle item3Fh = session.insert(item3);
    OrderItem item4 = new OrderItem(order, 4, "Very Best of Mick Jagger", OrderItem.TYPE_CD, 11);
    order.addItem(item4);
    FactHandle item4Fh = session.insert(item4);
    session.insert(order);
    session.fireAllRules();
    assertEquals(11, list.size());
    kbase.removeRule("org.drools.compiler", "Apply Discount on all books");
    list.clear();
    session.update(item1Fh, item1);
    session.update(item2Fh, item2);
    session.update(item3Fh, item3);
    session.update(item4Fh, item4);
    session.fireAllRules();
    assertEquals(10, list.size());
    kbase.removeRule("org.drools.compiler", "like book");
    list.clear();
    session.update(item1Fh, item1);
    session.update(item2Fh, item2);
    session.update(item3Fh, item3);
    session.update(item4Fh, item4);
    session.fireAllRules();
    assertEquals(8, list.size());
    final OrderItem item5 = new OrderItem(order, 5, "Sinatra : Vegas", OrderItem.TYPE_CD, 5);
    FactHandle item5Fh = session.insert(item5);
    session.fireAllRules();
    assertEquals(10, list.size());
    kbase.removeKiePackage("org.drools.compiler");
    list.clear();
    session.update(item1Fh, item1);
    session.update(item2Fh, item2);
    session.update(item3Fh, item3);
    session.update(item4Fh, item4);
    session.update(item5Fh, item5);
    session.fireAllRules();
    assertEquals(0, list.size());
}
Also used : Order(org.drools.compiler.Order) KiePackage(org.kie.api.definition.KiePackage) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) OrderItem(org.drools.compiler.OrderItem) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 8 with OrderItem

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

the class AccumulateTest method testAccumulateMVELWithModify.

@Test(timeout = 10000)
public void testAccumulateMVELWithModify() throws Exception {
    // read in the source
    KieSession wm = getKieSessionFromResources("test_AccumulateMVELwithModify.drl");
    final List<Number> results = new ArrayList<Number>();
    wm.setGlobal("results", results);
    Order order = new Order(1, "Bob");
    OrderItem item1 = new OrderItem(order, 1, "maquilage", 1, 10);
    OrderItem item2 = new OrderItem(order, 2, "perfume", 1, 5);
    order.addItem(item1);
    order.addItem(item2);
    wm.insert(order);
    wm.insert(item1);
    wm.insert(item2);
    wm.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(15, results.get(0).intValue());
    assertEquals(15.0, order.getTotal(), 0.0);
}
Also used : Order(org.drools.compiler.Order) OrderItem(org.drools.compiler.OrderItem) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 9 with OrderItem

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

the class FirstOrderLogicTest method testCollectWithMemberOfOperators.

@Test
public void testCollectWithMemberOfOperators() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CollectMemberOfOperator.drl");
    KieSession workingMemory = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    workingMemory.setGlobal("results", list);
    final Order order1 = new Order(1, "bob");
    final OrderItem item11 = new OrderItem(order1, 1);
    final OrderItem item12 = new OrderItem(order1, 2);
    final Order order2 = new Order(2, "mark");
    final OrderItem item21 = new OrderItem(order2, 1);
    final OrderItem item22 = new OrderItem(order2, 2);
    workingMemory.insert(order1);
    workingMemory.insert(item11);
    workingMemory.insert(item12);
    workingMemory.insert(order2);
    workingMemory.insert(item21);
    workingMemory.insert(item22);
    workingMemory.fireAllRules();
    int index = 0;
    assertEquals(8, list.size());
    assertSame(order1, list.get(index++));
    assertSame(item11, list.get(index++));
    assertSame(order2, list.get(index++));
    assertSame(item21, list.get(index++));
    assertSame(order1, list.get(index++));
    assertSame(item11, list.get(index++));
    assertSame(order2, list.get(index++));
    assertSame(item21, list.get(index++));
}
Also used : Order(org.drools.compiler.Order) KieBase(org.kie.api.KieBase) OrderItem(org.drools.compiler.OrderItem) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 10 with OrderItem

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

the class DynamicRulesTest method testRemovePackageSubNetwork.

@Test(timeout = 10000)
public void testRemovePackageSubNetwork() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_DynamicRulesWithSubnetwork.drl");
    String packageName = kbase.getKiePackages().iterator().next().getName();
    KieSession workingMemory = createKnowledgeSession(kbase);
    List<?> results = new ArrayList<Object>();
    workingMemory.setGlobal("results", results);
    Order order = new Order();
    OrderItem item1 = new OrderItem(order, 1, "Adventure Guide Brazil", OrderItem.TYPE_BOOK, 24);
    OrderItem item2 = new OrderItem(order, 2, "Prehistoric Britain", OrderItem.TYPE_BOOK, 15);
    OrderItem item3 = new OrderItem(order, 3, "Holiday Music", OrderItem.TYPE_CD, 9);
    OrderItem item4 = new OrderItem(order, 4, "Very Best of Mick Jagger", OrderItem.TYPE_CD, 11);
    OrderItem item5 = new OrderItem(order, 5, "The Master and Margarita", OrderItem.TYPE_BOOK, 29);
    order.addItem(item1);
    order.addItem(item2);
    order.addItem(item3);
    order.addItem(item4);
    order.addItem(item5);
    workingMemory.insert(order);
    workingMemory.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(0)).size());
    results.clear();
    InternalKnowledgeBase ruleBaseWM = (InternalKnowledgeBase) workingMemory.getKieBase();
    ruleBaseWM.removeKiePackage(packageName);
    Collection<KiePackage> kpkgs = loadKnowledgePackages("test_DynamicRulesWithSubnetwork.drl");
    ruleBaseWM.addPackages(SerializationHelper.serializeObject(kpkgs));
    workingMemory.fireAllRules();
    results = (List) workingMemory.getGlobal("results");
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(0)).size());
    results.clear();
    ruleBaseWM.removeKiePackage(packageName);
    ruleBaseWM.addPackages(SerializationHelper.serializeObject(kpkgs));
    workingMemory.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(0)).size());
    results.clear();
    ruleBaseWM.removeKiePackage(packageName);
    ruleBaseWM.addPackages(SerializationHelper.serializeObject(kpkgs));
    workingMemory.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(0)).size());
    results.clear();
}
Also used : Order(org.drools.compiler.Order) KiePackage(org.kie.api.definition.KiePackage) KieBase(org.kie.api.KieBase) OrderItem(org.drools.compiler.OrderItem) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

Order (org.drools.compiler.Order)13 OrderItem (org.drools.compiler.OrderItem)13 Test (org.junit.Test)13 KieSession (org.kie.api.runtime.KieSession)13 ArrayList (java.util.ArrayList)10 KieBase (org.kie.api.KieBase)10 List (java.util.List)7 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 KiePackage (org.kie.api.definition.KiePackage)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1