Search in sources :

Example 1 with State

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

the class FirstOrderLogicTest method testForall2.

@Test
public void testForall2() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_Forall2.drl");
    KieSession ksession = kbase.newKieSession();
    final List<String> list = new ArrayList<String>();
    ksession.setGlobal("results", list);
    final State state = new State("SP");
    ksession.insert(state);
    final Person bob = new Person("Bob");
    bob.setStatus(state.getState());
    bob.setAlive(true);
    ksession.insert(bob);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    final State qc = new State("QC");
    ksession.insert(qc);
    final Person john = new Person("John");
    john.setStatus(qc.getState());
    john.setAlive(false);
    ksession.insert(john);
    ksession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : KieBase(org.kie.api.KieBase) State(org.drools.mvel.compiler.State) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) SpecialString(org.drools.mvel.compiler.SpecialString) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 2 with State

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

the class FirstOrderLogicTest method testForall.

@Test
public void testForall() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_Forall.drl");
    KieSession workingMemory = kbase.newKieSession();
    final List list = new ArrayList();
    workingMemory.setGlobal("results", list);
    final State state = new State("SP");
    workingMemory.insert(state);
    final Person bob = new Person("Bob");
    bob.setStatus(state.getState());
    bob.setLikes("stilton");
    workingMemory.insert(bob);
    workingMemory.fireAllRules();
    assertEquals(0, list.size());
    workingMemory.insert(new Cheese(bob.getLikes(), 10));
    workingMemory.fireAllRules();
    assertEquals(1, list.size());
}
Also used : KieBase(org.kie.api.KieBase) State(org.drools.mvel.compiler.State) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 3 with State

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

the class PropertyReactivityTest method testPropertyChangeSupportNewAPI.

@Test
public void testPropertyChangeSupportNewAPI() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_PropertyChangeTypeDecl.drl");
    KieSession session = kbase.newKieSession();
    final List list = new ArrayList();
    session.setGlobal("list", list);
    final State state = new State("initial");
    session.insert(state);
    session.fireAllRules();
    assertEquals(1, ((List) session.getGlobal("list")).size());
    state.setFlag(true);
    assertEquals(1, ((List) session.getGlobal("list")).size());
    session.fireAllRules();
    // due to property reactivity the rule shouldn't fire again
    assertEquals(1, ((List) session.getGlobal("list")).size());
    state.setState("finished");
    session.dispose();
    // checks that the session removed itself from the bean listeners list
    assertEquals(0, state.getPropertyChangeListeners().length);
}
Also used : KieBase(org.kie.api.KieBase) State(org.drools.mvel.compiler.State) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)3 State (org.drools.mvel.compiler.State)3 Test (org.junit.Test)3 KieBase (org.kie.api.KieBase)3 KieSession (org.kie.api.runtime.KieSession)3 List (java.util.List)2 Person (org.drools.mvel.compiler.Person)2 Cheese (org.drools.mvel.compiler.Cheese)1 SpecialString (org.drools.mvel.compiler.SpecialString)1