Search in sources :

Example 11 with Cheesery

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

the class MVELTest method testMVELRewrite.

@Test
public void testMVELRewrite() throws Exception {
    // read in the source
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_MVELrewrite.drl"));
    KieSession ksession = createKnowledgeSession(kbase);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    final List results = new ArrayList();
    ksession.setGlobal("results", results);
    final Cheese brie = new Cheese("brie", 2);
    final Cheese stilton = new Cheese("stilton", 2);
    final Cheesery cheesery = new Cheesery();
    cheesery.addCheese(brie);
    cheesery.addCheese(stilton);
    ksession.insert(cheesery);
    ksession.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(cheesery, results.get(0));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Cheesery(org.drools.compiler.Cheesery) Test(org.junit.Test)

Example 12 with Cheesery

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

the class ImportsTest method testImportStaticClass.

@Test
public void testImportStaticClass() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_StaticField.drl"));
    KieSession session = createKnowledgeSession(kbase);
    // will test serialisation of int and typesafe enums tests
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    final List list = new ArrayList();
    session.setGlobal("list", list);
    final Cheesery cheesery1 = new Cheesery();
    cheesery1.setStatus(Cheesery.SELLING_CHEESE);
    cheesery1.setMaturity(Cheesery.Maturity.OLD);
    session.insert(cheesery1);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    final Cheesery cheesery2 = new Cheesery();
    cheesery2.setStatus(Cheesery.MAKING_CHEESE);
    cheesery2.setMaturity(Cheesery.Maturity.YOUNG);
    session.insert(cheesery2);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(cheesery1, list.get(0));
    assertEquals(cheesery2, list.get(1));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheesery(org.drools.compiler.Cheesery) Test(org.junit.Test)

Example 13 with Cheesery

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

the class ContainsTest method testContainsCheese.

@Test
public void testContainsCheese() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_ContainsCheese.drl"));
    final KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Cheese stilton = new Cheese("stilton", 12);
    ksession.insert(stilton);
    final Cheese brie = new Cheese("brie", 10);
    ksession.insert(brie);
    final Cheesery cheesery = new Cheesery();
    cheesery.getCheeses().add(stilton);
    ksession.insert(cheesery);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(stilton, list.get(0));
    assertEquals(brie, list.get(1));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Cheesery(org.drools.compiler.Cheesery) Test(org.junit.Test)

Example 14 with Cheesery

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

the class FromTest method testFromDeclarationWithWorkingMemoryLogger.

/**
 * JBRULES-1415 Certain uses of from causes NullPointerException in WorkingMemoryLogger
 */
@Test
public void testFromDeclarationWithWorkingMemoryLogger() throws Exception {
    String rule = "package org.drools.compiler.test;\n";
    rule += "import org.drools.compiler.Cheesery\n";
    rule += "import org.drools.compiler.Cheese\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Test Rule\"\n";
    rule += "when\n";
    rule += "    $cheesery : Cheesery()\n";
    rule += "    Cheese( $type : type) from $cheesery.cheeses\n";
    rule += "then\n";
    rule += "    list.add( $type );\n";
    rule += "end";
    final KieBase kbase = loadKnowledgeBaseFromString(rule);
    final KieSession session = kbase.newKieSession();
    final List list = new ArrayList();
    session.setGlobal("list", list);
    final Cheesery cheesery = new Cheesery();
    cheesery.addCheese(new Cheese("stilton", 22));
    session.insert(cheesery);
    session.fireAllRules();
    assertEquals(1, ((List) session.getGlobal("list")).size());
    assertEquals("stilton", ((List) session.getGlobal("list")).get(0));
}
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) Cheese(org.drools.compiler.Cheese) Cheesery(org.drools.compiler.Cheesery) Test(org.junit.Test)

Example 15 with Cheesery

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

the class FromTest method testBasicFrom.

@Test
public void testBasicFrom() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_From.drl");
    kbase = SerializationHelper.serializeObject(kbase);
    KieSession ksession = createKnowledgeSession(kbase);
    final List list1 = new ArrayList();
    ksession.setGlobal("list1", list1);
    final List list2 = new ArrayList();
    ksession.setGlobal("list2", list2);
    final List list3 = new ArrayList();
    ksession.setGlobal("list3", list3);
    final Cheesery cheesery = new Cheesery();
    final Cheese stilton = new Cheese("stilton", 12);
    final Cheese cheddar = new Cheese("cheddar", 15);
    cheesery.addCheese(stilton);
    cheesery.addCheese(cheddar);
    ksession.setGlobal("cheesery", cheesery);
    ksession.insert(cheesery);
    final Person p = new Person("stilton");
    ksession.insert(p);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    // from using a global
    assertEquals(2, ((List) ksession.getGlobal("list1")).size());
    assertEquals(cheddar, ((List) ksession.getGlobal("list1")).get(0));
    assertEquals(stilton, ((List) ksession.getGlobal("list1")).get(1));
    // from using a declaration
    assertEquals(2, ((List) ksession.getGlobal("list2")).size());
    assertEquals(cheddar, ((List) ksession.getGlobal("list2")).get(0));
    assertEquals(stilton, ((List) ksession.getGlobal("list2")).get(1));
    // from using a declaration
    assertEquals(1, ((List) ksession.getGlobal("list3")).size());
    assertEquals(stilton, ((List) ksession.getGlobal("list3")).get(0));
}
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) Cheese(org.drools.compiler.Cheese) Cheesery(org.drools.compiler.Cheesery) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

Cheesery (org.drools.compiler.Cheesery)16 Test (org.junit.Test)16 KieSession (org.kie.api.runtime.KieSession)16 Cheese (org.drools.compiler.Cheese)14 ArrayList (java.util.ArrayList)13 KieBase (org.kie.api.KieBase)12 List (java.util.List)11 Person (org.drools.compiler.Person)6 FactHandle (org.kie.api.runtime.rule.FactHandle)6 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 Dimension (java.awt.Dimension)2 Worker (org.drools.compiler.Worker)2 SessionInspector (org.drools.core.util.debug.SessionInspector)2 StatefulKnowledgeSessionInfo (org.drools.core.util.debug.StatefulKnowledgeSessionInfo)2 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)2 KiePackage (org.kie.api.definition.KiePackage)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1 KieHelper (org.kie.internal.utils.KieHelper)1