Search in sources :

Example 1 with OrderItem

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

the class ContainsTest method testNotContainsOperator.

@Test
public void testNotContainsOperator() {
    // JBRULES-2404: "not contains" operator doesn't work on nested fields
    final String str = "package org.drools.compiler\n" + "rule NotContains\n" + "when\n" + "    $oi : OrderItem( )\n" + "    $o  : Order( items.values() not contains $oi )" + "then\n" + "end";
    final KieBase kbase = loadKnowledgeBaseFromString(str);
    final KieSession ksession = createKnowledgeSession(kbase);
    final Order order1 = new Order(1, "XYZ");
    final Order order2 = new Order(2, "ABC");
    final OrderItem item11 = new OrderItem(order1, 1);
    order1.addItem(item11);
    final OrderItem item21 = new OrderItem(order2, 1);
    order2.addItem(item21);
    ksession.insert(order1);
    ksession.insert(item11);
    // should not fire, as item11 is contained in order1.items
    int rules = ksession.fireAllRules();
    assertEquals(0, rules);
    // should fire as item21 is not contained in order1.items
    ksession.insert(item21);
    rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : Order(org.drools.compiler.Order) KieBase(org.kie.api.KieBase) OrderItem(org.drools.compiler.OrderItem) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 2 with OrderItem

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

the class EvalRewriteTest method testEvalRewriteWithSpecialOperators.

@Test
public void testEvalRewriteWithSpecialOperators() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_EvalRewriteWithSpecialOperators.drl"));
    final KieSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("results", list);
    final Order order1 = new Order(10, "Bob");
    final OrderItem item11 = new OrderItem(order1, 1);
    final OrderItem item12 = new OrderItem(order1, 2);
    order1.addItem(item11);
    order1.addItem(item12);
    final Order order2 = new Order(11, "Bob");
    final OrderItem item21 = new OrderItem(order2, 1);
    final OrderItem item22 = new OrderItem(order2, 2);
    order2.addItem(item21);
    order2.addItem(item22);
    final Order order3 = new Order(12, "Bob");
    final OrderItem item31 = new OrderItem(order3, 1);
    final OrderItem item32 = new OrderItem(order3, 2);
    final OrderItem item33 = new OrderItem(order3, 3);
    order3.addItem(item31);
    order3.addItem(item32);
    order3.addItem(item33);
    final Order order4 = new Order(13, "Bob");
    final OrderItem item41 = new OrderItem(order4, 1);
    final OrderItem item42 = new OrderItem(order4, 2);
    order4.addItem(item41);
    order4.addItem(item42);
    final Order order5 = new Order(14, "Mark");
    final OrderItem item51 = new OrderItem(order5, 1);
    final OrderItem item52 = new OrderItem(order5, 2);
    order5.addItem(item51);
    order5.addItem(item52);
    ksession.insert(order1);
    ksession.insert(item11);
    ksession.insert(item12);
    ksession.insert(order2);
    ksession.insert(item21);
    ksession.insert(item22);
    ksession.insert(order3);
    ksession.insert(item31);
    ksession.insert(item32);
    ksession.insert(item33);
    ksession.insert(order4);
    ksession.insert(item41);
    ksession.insert(item42);
    ksession.insert(order5);
    ksession.insert(item51);
    ksession.insert(item52);
    ksession.fireAllRules();
    assertEquals(9, list.size());
    int index = 0;
    assertEquals(item11, list.get(index++));
    assertEquals(item12, list.get(index++));
    assertEquals(item21, list.get(index++));
    assertEquals(item22, list.get(index++));
    assertEquals(item31, list.get(index++));
    assertEquals(item33, list.get(index++));
    assertEquals(item41, list.get(index++));
    assertEquals(order5, list.get(index++));
    assertEquals(order5, 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 3 with OrderItem

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

the class EvalRewriteTest method testEvalRewriteMatches.

@Test
public void testEvalRewriteMatches() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_EvalRewriteMatches.drl"));
    final KieSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("results", list);
    final Order order1 = new Order(14, "Mark");
    final OrderItem item11 = new OrderItem(order1, 1);
    final OrderItem item12 = new OrderItem(order1, 2);
    order1.addItem(item11);
    order1.addItem(item12);
    ksession.insert(order1);
    ksession.insert(item11);
    ksession.insert(item12);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.contains(item11));
    assertTrue(list.contains(item12));
}
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 4 with OrderItem

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

the class FromTest method testFromNestedAccessors.

@Test
public void testFromNestedAccessors() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_FromNestedAccessors.drl"));
    final KieSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("results", list);
    final Order order1 = new Order(11, "Bob");
    final OrderItem item11 = new OrderItem(order1, 1);
    final OrderItem item12 = new OrderItem(order1, 2);
    order1.addItem(item11);
    order1.addItem(item12);
    ksession.insert(order1);
    ksession.insert(item11);
    ksession.insert(item12);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertSame(order1.getStatus(), list.get(0));
}
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 5 with OrderItem

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

the class QueryTest2 method testEvalRewrite.

@Test
public void testEvalRewrite() throws Exception {
    String str = "" + "package org.drools.compiler;\n" + "global java.util.List results;\n" + "rule \"eval rewrite\"\n" + "    when\n" + "        $o1 : OrderItem( order.number == 11, $seq : seq == 1 )\n" + // "        $o2 : OrderItem( order.number == $o1.order.number, seq != $seq )\n" +
    "        $o2 : Order( items[(Integer) 1] == $o1 ) \n" + "    then\n" + "        System.out.println( $o1 + \":\" + $o2 );\n" + "end        \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    final Order order1 = new Order(11, "Bob");
    final OrderItem item11 = new OrderItem(order1, 1);
    final OrderItem item12 = new OrderItem(order1, 2);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.insert(order1);
    ksession.insert(item11);
    ksession.insert(item12);
    ksession.fireAllRules();
}
Also used : Order(org.drools.compiler.Order) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) OrderItem(org.drools.compiler.OrderItem) 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