Search in sources :

Example 36 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class ExceptionTest method testReturnValueException.

@Test
public void testReturnValueException() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_ReturnValueException.drl"));
    final KieSession ksession = kbase.newKieSession();
    final Cheese brie = new Cheese("brie", 12);
    try {
        ksession.insert(brie);
        ksession.fireAllRules();
        fail("Should throw an Exception from the ReturnValue");
    } catch (final Exception e) {
        Throwable root = e;
        while (root.getCause() != null) {
            root = root.getCause();
        }
        root.getMessage().contains("this should throw an exception");
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 37 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class ImportsTest method testImportFunctions.

@Test
public void testImportFunctions() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_ImportFunctions.drl"));
    KieSession session = createKnowledgeSession(kbase);
    final Cheese cheese = new Cheese("stilton", 15);
    session.insert(cheese);
    List list = new ArrayList();
    session.setGlobal("list", list);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    final int fired = session.fireAllRules();
    list = (List) session.getGlobal("list");
    assertEquals(4, fired);
    assertEquals(4, list.size());
    assertEquals("rule1", list.get(0));
    assertEquals("rule2", list.get(1));
    assertEquals("rule3", list.get(2));
    assertEquals("rule4", list.get(3));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 38 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class ImportsTest method testImportConflict.

@Test
public void testImportConflict() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_ImportConflict.drl"));
    createKnowledgeSession(kbase);
}
Also used : KieBase(org.kie.api.KieBase) Test(org.junit.Test)

Example 39 with KieBase

use of org.kie.api.KieBase 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 40 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class NestingTest method testNesting.

@Test
public void testNesting() throws Exception {
    final Person p = new Person();
    p.setName("Michael");
    final Address add1 = new Address();
    add1.setStreet("High");
    final Address add2 = new Address();
    add2.setStreet("Low");
    final List l = new ArrayList();
    l.add(add1);
    l.add(add2);
    p.setAddresses(l);
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final PackageDescr desc = parser.parse(new InputStreamReader(getClass().getResourceAsStream("nested_fields.drl")));
    final List packageAttrs = desc.getAttributes();
    assertEquals(1, desc.getRules().size());
    assertEquals(1, packageAttrs.size());
    final RuleDescr rule = desc.getRules().get(0);
    final Map<String, AttributeDescr> ruleAttrs = rule.getAttributes();
    assertEquals(1, ruleAttrs.size());
    assertEquals("mvel", ruleAttrs.get("dialect").getValue());
    assertEquals("dialect", ruleAttrs.get("dialect").getName());
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(desc));
    KieSession session = createKnowledgeSession(kbase);
    session.insert(p);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
}
Also used : Address(org.drools.compiler.Address) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Person(org.drools.compiler.Person) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) Test(org.junit.Test)

Aggregations

KieBase (org.kie.api.KieBase)1272 Test (org.junit.Test)1191 KieSession (org.kie.api.runtime.KieSession)1011 ArrayList (java.util.ArrayList)592 List (java.util.List)392 Person (org.drools.compiler.Person)214 FactHandle (org.kie.api.runtime.rule.FactHandle)176 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)168 KieHelper (org.kie.internal.utils.KieHelper)156 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)154 Cheese (org.drools.compiler.Cheese)139 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)99 Arrays.asList (java.util.Arrays.asList)87 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)86 QueryResults (org.kie.api.runtime.rule.QueryResults)78 KieServices (org.kie.api.KieServices)74 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)67 Model (org.drools.model.Model)64 Rule (org.drools.model.Rule)64 ModelImpl (org.drools.model.impl.ModelImpl)64