Search in sources :

Example 6 with FactA

use of org.drools.mvel.compiler.FactA in project drools by kiegroup.

the class MarshallingTest method testMultiRuleMultiJoinNodePatternsWithHalt.

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

Example 7 with FactA

use of org.drools.mvel.compiler.FactA in project drools by kiegroup.

the class MVELTest method testTypeCoercionFloatCompareToDouble.

@Test
public void testTypeCoercionFloatCompareToDouble() {
    String str = "package com.sample\n" + "import " + FactA.class.getCanonicalName() + ";\n" + "rule R1\n" + "dialect \"mvel\"\n" + "when\n" + "  $f : FactA(field3 == 15.1)\n" + "then\n" + "end";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    FactA f = new FactA();
    f.setField3(new Float(15.1f));
    ksession.insert(f);
    assertEquals(1, ksession.fireAllRules());
}
Also used : KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) FactA(org.drools.mvel.compiler.FactA) Test(org.junit.Test)

Example 8 with FactA

use of org.drools.mvel.compiler.FactA in project drools by kiegroup.

the class DynamicRulesTest method testDynamicRules2.

@Test(timeout = 10000)
public void testDynamicRules2() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources("test", getClass(), kieBaseTestConfiguration);
    KieSession session = kbase.newKieSession();
    // Assert some simple facts
    final FactA a = new FactA("hello", new Integer(1), new Float(3.14));
    final FactB b = new FactB("hello", new Integer(2), new Float(6.28));
    session.insert(a);
    session.insert(b);
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicRules2.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) FactA(org.drools.mvel.compiler.FactA) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) FactB(org.drools.mvel.compiler.FactB) Test(org.junit.Test)

Example 9 with FactA

use of org.drools.mvel.compiler.FactA in project drools by kiegroup.

the class ExecutionFlowControlTest method testEnabledExpressionWithOr.

@Test
public void testEnabledExpressionWithOr() throws Exception {
    String text = "package org.kie.test\n" + "global java.util.List list\n" + "import " + FactA.class.getCanonicalName() + "\n" + "import " + Foo.class.getCanonicalName() + "\n" + "import " + Pet.class.getCanonicalName() + "\n" + "rule r1 salience(f1.field2) enabled(f1.field2 >= 20)\n" + "when\n" + "    foo: Foo()\n" + "    ( Pet()  and f1 : FactA( field1 == 'f1') ) or \n" + "    f1 : FactA(field1 == 'f2') \n" + "then\n" + "    list.add( f1 );\n" + "    foo.setId( 'xxx' );\n" + "end\n" + "\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, text);
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new Foo(null, null));
    ksession.insert(new Pet(null));
    FactA fact1 = new FactA();
    fact1.setField1("f1");
    fact1.setField2(10);
    FactA fact2 = new FactA();
    fact2.setField1("f1");
    fact2.setField2(30);
    FactA fact3 = new FactA();
    fact3.setField1("f2");
    fact3.setField2(20);
    ksession.insert(fact1);
    ksession.insert(fact2);
    ksession.insert(fact3);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(fact2, list.get(0));
    assertEquals(fact3, list.get(1));
}
Also used : KieBase(org.kie.api.KieBase) Foo(org.drools.mvel.compiler.Foo) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) FactA(org.drools.mvel.compiler.FactA) Pet(org.drools.mvel.compiler.Pet) Test(org.junit.Test)

Aggregations

FactA (org.drools.mvel.compiler.FactA)9 Test (org.junit.Test)8 KieSession (org.kie.api.runtime.KieSession)8 KieBase (org.kie.api.KieBase)7 FactB (org.drools.mvel.compiler.FactB)6 ArrayList (java.util.ArrayList)4 FactC (org.drools.mvel.compiler.FactC)4 List (java.util.List)3 Foo (org.drools.mvel.compiler.Foo)2 Pet (org.drools.mvel.compiler.Pet)2 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)2 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)2 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)1 Cell (org.drools.mvel.compiler.Cell)1 Cheese (org.drools.mvel.compiler.Cheese)1 Person (org.drools.mvel.compiler.Person)1 IteratorToList (org.drools.mvel.integrationtests.IteratorToList)1 KiePackage (org.kie.api.definition.KiePackage)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1