Search in sources :

Example 1 with State

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

the class NestingTest method testNestedConditionalElements.

@Test
public void testNestedConditionalElements() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_NestedConditionalElements.drl"));
    final KieSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("results", list);
    final State state = new State("SP");
    ksession.insert(state);
    final Person bob = new Person("Bob");
    bob.setStatus(state.getState());
    bob.setLikes("stilton");
    ksession.insert(bob);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    ksession.insert(new Cheese(bob.getLikes(), 10));
    ksession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : KieBase(org.kie.api.KieBase) State(org.drools.compiler.State) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 2 with State

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

the class PropertyReactivityTest method testPropertyChangeSupportNewAPI.

@Test
public void testPropertyChangeSupportNewAPI() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_PropertyChangeTypeDecl.drl"));
    final KieSession session = createKnowledgeSession(kbase);
    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();
    assertEquals(2, ((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.compiler.State) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 3 with State

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

the class FirstOrderLogicTest method testForall.

@Test
public void testForall() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_Forall.drl");
    KieSession workingMemory = createKnowledgeSession(kbase);
    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.compiler.State) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 4 with State

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

the class FirstOrderLogicTest method testForall2.

@Test
public void testForall2() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_Forall2.drl");
    KieSession ksession = createKnowledgeSession(kbase);
    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.fireAllRules();
    assertEquals(1, list.size());
}
Also used : KieBase(org.kie.api.KieBase) State(org.drools.compiler.State) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) SpecialString(org.drools.compiler.SpecialString) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

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