Search in sources :

Example 31 with Cheese

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

the class UpdateTest method doModifyTest.

private void doModifyTest(final String drlResource) throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, drlResource);
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("results", list);
    final Person bob = new Person("Bob");
    bob.setStatus("hungry");
    final Cheese c = new Cheese();
    ksession.insert(bob);
    ksession.insert(c);
    ksession.fireAllRules();
    assertEquals(10, c.getPrice());
    assertEquals("fine", bob.getStatus());
}
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.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person)

Example 32 with Cheese

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

the class UpdateTest method noDormantCheckOnModifies.

@Test
public void noDormantCheckOnModifies() throws Exception {
    // Test case for BZ 862325
    final String str = "package org.drools.mvel.compiler;\n" + " rule R1\n" + "    salience 10\n" + "    when\n" + "        $c : Cheese( price == 10 ) \n" + "        $p : Person( ) \n" + "    then \n" + "        modify($c) { setPrice( 5 ) }\n" + "        modify($p) { setAge( 20 ) }\n" + "end\n" + "rule R2\n" + "    when\n" + "        $p : Person( )" + "    then \n" + "        // noop\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    final org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
    ksession.addEventListener(ael);
    ksession.insert(new Person("Bob", 19));
    ksession.insert(new Cheese("brie", 10));
    ksession.fireAllRules();
    // both rules should fire exactly once
    verify(ael, times(2)).afterMatchFired(any(org.kie.api.event.rule.AfterMatchFiredEvent.class));
    // no cancellations should have happened
    verify(ael, never()).matchCancelled(any(org.kie.api.event.rule.MatchCancelledEvent.class));
}
Also used : Cheese(org.drools.mvel.compiler.Cheese) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 33 with Cheese

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

the class UpdateTest method testInvalidModify2.

@Test
public void testInvalidModify2() throws Exception {
    String str = "";
    str += "package org.drools.compiler \n";
    str += "import " + Cheese.class.getName() + "\n";
    str += "global java.util.List list \n";
    str += "rule rule1 \n";
    str += "    no-loop \n";
    str += "when \n";
    str += "    $i : Cheese() \n";
    str += "then \n";
    str += "    modify( $i ) { setType( \"stilton\" ); setType( \"stilton\" );}; ";
    str += "    list.add( $i ); \n";
    str += "end \n";
    KieBuilder kieBuilder = KieUtil.getKieBuilderFromDrls(kieBaseTestConfiguration, false, str);
    List<Message> errors = kieBuilder.getResults().getMessages(Message.Level.ERROR);
    assertFalse("Should have an error", errors.isEmpty());
}
Also used : Message(org.kie.api.builder.Message) Cheese(org.drools.mvel.compiler.Cheese) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 34 with Cheese

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

the class RuleRuntimeEventTest method testEventModel.

@Test
public void testEventModel() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_EventModel.drl");
    KieSession wm = kbase.newKieSession();
    final RuleRuntimeEventListener wmel = mock(RuleRuntimeEventListener.class);
    wm.addEventListener(wmel);
    final Cheese stilton = new Cheese("stilton", 15);
    final FactHandle stiltonHandle = wm.insert(stilton);
    final ArgumentCaptor<ObjectInsertedEvent> oic = ArgumentCaptor.forClass(org.kie.api.event.rule.ObjectInsertedEvent.class);
    verify(wmel).objectInserted(oic.capture());
    assertSame(stiltonHandle, oic.getValue().getFactHandle());
    wm.update(stiltonHandle, stilton);
    final ArgumentCaptor<org.kie.api.event.rule.ObjectUpdatedEvent> ouc = ArgumentCaptor.forClass(org.kie.api.event.rule.ObjectUpdatedEvent.class);
    verify(wmel).objectUpdated(ouc.capture());
    assertSame(stiltonHandle, ouc.getValue().getFactHandle());
    wm.delete(stiltonHandle);
    final ArgumentCaptor<ObjectDeletedEvent> orc = ArgumentCaptor.forClass(ObjectDeletedEvent.class);
    verify(wmel).objectDeleted(orc.capture());
    assertSame(stiltonHandle, orc.getValue().getFactHandle());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) Cheese(org.drools.mvel.compiler.Cheese) ObjectInsertedEvent(org.kie.api.event.rule.ObjectInsertedEvent) ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 35 with Cheese

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

the class StatefulSessionTest method testGetFactHandleIdentityBehavior.

@Test
public void testGetFactHandleIdentityBehavior() throws Exception {
    KieBaseTestConfiguration identityConfig = TestParametersUtil.getIdentityInstanceOf(kieBaseTestConfiguration);
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", identityConfig);
    KieSession ksession = kbase.newKieSession();
    final CheeseEqual cheese = new CheeseEqual("stilton", 10);
    ksession.insert(cheese);
    final FactHandle fh1 = ksession.getFactHandle(new Cheese("stilton", 10));
    assertNull(fh1);
    final FactHandle fh2 = ksession.getFactHandle(cheese);
    assertNotNull(fh2);
}
Also used : CheeseEqual(org.drools.mvel.compiler.CheeseEqual) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) Test(org.junit.Test)

Aggregations

Cheese (org.drools.mvel.compiler.Cheese)136 Test (org.junit.Test)129 KieSession (org.kie.api.runtime.KieSession)112 KieBase (org.kie.api.KieBase)96 ArrayList (java.util.ArrayList)90 List (java.util.List)65 Person (org.drools.mvel.compiler.Person)47 FactHandle (org.kie.api.runtime.rule.FactHandle)29 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)24 KiePackage (org.kie.api.definition.KiePackage)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)20 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)20 IteratorToList (org.drools.mvel.integrationtests.IteratorToList)14 Command (org.kie.api.command.Command)10 ExecutionResults (org.kie.api.runtime.ExecutionResults)10 PackageDescr (org.drools.drl.ast.descr.PackageDescr)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)6 Cheesery (org.drools.mvel.compiler.Cheesery)6 ClassObjectFilter (org.drools.core.ClassObjectFilter)5 ClassObjectType (org.drools.core.base.ClassObjectType)5