Search in sources :

Example 1 with Pet

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

the class ConsequenceTest method testMVELConsequenceWithoutSemiColon1.

@Test
public void testMVELConsequenceWithoutSemiColon1() throws Exception {
    String drl = "";
    drl += "package test\n";
    drl += "import org.drools.compiler.Person\n";
    drl += "import org.drools.compiler.Pet\n";
    drl += "rule test dialect 'mvel'\n";
    drl += "when\n";
    drl += "    $person:Person()\n";
    drl += "    $pet:Pet()\n";
    drl += "then\n";
    drl += "    delete($person) // some comment\n";
    drl += "    delete($pet) // another comment\n";
    drl += "end\n";
    final KieBase kbase = loadKnowledgeBaseFromString(drl);
    final KieSession ksession = createKnowledgeSession(kbase);
    // create working memory mock listener
    final RuleRuntimeEventListener wml = Mockito.mock(RuleRuntimeEventListener.class);
    ksession.addEventListener(wml);
    final FactHandle personFH = ksession.insert(new Person("Toni"));
    final FactHandle petFH = ksession.insert(new Pet("Toni"));
    final int fired = ksession.fireAllRules();
    assertEquals(1, fired);
    // capture the arguments and check that the retracts happened
    final ArgumentCaptor<ObjectDeletedEvent> retracts = ArgumentCaptor.forClass(ObjectDeletedEvent.class);
    verify(wml, times(2)).objectDeleted(retracts.capture());
    final List<ObjectDeletedEvent> values = retracts.getAllValues();
    assertThat(values.get(0).getFactHandle(), is(personFH));
    assertThat(values.get(1).getFactHandle(), is(petFH));
}
Also used : ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Pet(org.drools.compiler.Pet) Test(org.junit.Test)

Example 2 with Pet

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

the class TimerAndCalendarTest method testIntervalTimerExpressionWithOr.

@Test(timeout = 10000)
public void testIntervalTimerExpressionWithOr() 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 timer (expr: f1.field2, f1.field2; repeat-limit=3)\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";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    KieBase kbase = loadKnowledgeBaseFromString(text);
    KieSession ksession = createKnowledgeSession(kbase, conf);
    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
    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(250);
    FactA fact3 = new FactA();
    fact3.setField1("f2");
    fact3.setField2(1000);
    ksession.insert(fact1);
    ksession.insert(fact3);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(fact1, list.get(0));
    timeService.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(fact1, list.get(1));
    timeService.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    // did not change, repeat-limit kicked in
    assertEquals(2, list.size());
    timeService.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertEquals(fact3, list.get(2));
    timeService.advanceTime(1000, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(4, list.size());
    assertEquals(fact3, list.get(3));
    timeService.advanceTime(1000, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    // did not change, repeat-limit kicked in
    assertEquals(4, list.size());
}
Also used : KieBase(org.kie.api.KieBase) Foo(org.drools.compiler.Foo) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) FactA(org.drools.compiler.FactA) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) Pet(org.drools.compiler.Pet) Test(org.junit.Test)

Example 3 with Pet

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

the class ExecutionFlowControlTest method testSalienceExpressionWithOr.

@Test
public void testSalienceExpressionWithOr() 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)\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 = loadKnowledgeBaseFromString(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();
    System.out.println(list);
    assertEquals(3, list.size());
    assertEquals(fact2, list.get(0));
    assertEquals(fact3, list.get(1));
    assertEquals(fact1, list.get(2));
}
Also used : KieBase(org.kie.api.KieBase) Foo(org.drools.compiler.Foo) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) FactA(org.drools.compiler.FactA) Pet(org.drools.compiler.Pet) Test(org.junit.Test)

Example 4 with Pet

use of org.drools.compiler.Pet 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 = loadKnowledgeBaseFromString(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.compiler.Foo) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) FactA(org.drools.compiler.FactA) Pet(org.drools.compiler.Pet) Test(org.junit.Test)

Example 5 with Pet

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

the class MapConstraintTest method testAccessingMapValues.

@Test
public void testAccessingMapValues() throws Exception {
    String rule = "";
    rule += "package org.drools.compiler;\n";
    rule += "import org.drools.compiler.Pet;\n";
    rule += "rule \"Test Rule\"\n";
    rule += "  when\n";
    rule += "    $pet: Pet()\n";
    rule += "    Pet( \n";
    rule += "      ownerName == $pet.attributes[\"key\"] \n";
    rule += "    )\n";
    rule += "  then\n";
    rule += "    System.out.println(\"hi pet\");\n";
    rule += "end";
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(rule));
    final KieSession session = createKnowledgeSession(kbase);
    assertNotNull(session);
    final Pet pet1 = new Pet("Toni");
    pet1.getAttributes().put("key", "value");
    final Pet pet2 = new Pet("Toni");
    session.insert(pet1);
    session.insert(pet2);
    session.fireAllRules();
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Pet(org.drools.compiler.Pet) Test(org.junit.Test)

Aggregations

Pet (org.drools.compiler.Pet)8 Test (org.junit.Test)8 KieBase (org.kie.api.KieBase)8 KieSession (org.kie.api.runtime.KieSession)8 ArrayList (java.util.ArrayList)6 List (java.util.List)5 Person (org.drools.compiler.Person)4 FactA (org.drools.compiler.FactA)3 Foo (org.drools.compiler.Foo)3 Arrays.asList (java.util.Arrays.asList)1 Date (java.util.Date)1 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)1 ObjectDeletedEvent (org.kie.api.event.rule.ObjectDeletedEvent)1 RuleRuntimeEventListener (org.kie.api.event.rule.RuleRuntimeEventListener)1 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1