Search in sources :

Example 21 with KieBase

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

the class MultithreadTest method testFireUntilHaltAndDispose.

@Test(timeout = 10000)
public void testFireUntilHaltAndDispose() throws InterruptedException {
    // DROOLS-1103
    final String drl = "rule R no-loop timer( int: 1s )\n" + "when\n" + "    String()\n" + "then\n" + "end";
    final KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    final KieBase kbase = helper.build(EventProcessingOption.STREAM);
    final KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration();
    ksconf.setOption(TimedRuleExecutionOption.YES);
    final KieSession ksession = kbase.newKieSession(ksconf, null);
    new Thread() {

        @Override
        public void run() {
            ksession.fireUntilHalt();
        }
    }.start();
    try {
        Thread.sleep(100);
    } catch (final InterruptedException e) {
    // do nothing
    }
    ksession.insert("xxx");
    try {
        Thread.sleep(100);
    } catch (final InterruptedException e) {
    // do nothing
    }
    ksession.dispose();
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 22 with KieBase

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

the class CommentTest method testCommentDelimiterInString.

@Test
public void testCommentDelimiterInString() throws Exception {
    // JBRULES-3401
    final String str = "rule x\n" + "dialect \"mvel\"\n" + "when\n" + "then\n" + "System.out.println( \"/*\" );\n" + "end\n";
    final KieBase kbase = loadKnowledgeBaseFromString(str);
    kbase.newKieSession();
}
Also used : KieBase(org.kie.api.KieBase) Test(org.junit.Test)

Example 23 with KieBase

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

the class BindTest method testAutomaticBindings.

@Test
public void testAutomaticBindings() throws Exception {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_AutoBindings.drl"));
    final KieSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("results", list);
    final Person bob = new Person("bob", "stilton");
    final Cheese stilton = new Cheese("stilton", 12);
    ksession.insert(bob);
    ksession.insert(stilton);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(bob, list.get(0));
}
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.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 24 with KieBase

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

the class ConsequenceTest method testMVELConsequenceWithMapsAndArrays.

@Test
public void testMVELConsequenceWithMapsAndArrays() throws Exception {
    String rule = "package org.drools.compiler.test;\n";
    rule += "import java.util.ArrayList\n";
    rule += "import java.util.HashMap\n";
    rule += "global java.util.List list\n";
    rule += "rule \"Test Rule\"\n";
    rule += "    dialect \"mvel\"";
    rule += "when\n";
    rule += "then\n";
    rule += "    m = new HashMap();\n";
    rule += "    l = new ArrayList();\n";
    rule += "    l.add(\"first\");\n";
    rule += "    m.put(\"content\", l);\n";
    rule += "    System.out.println(((ArrayList)m[\"content\"])[0]);\n";
    rule += "    list.add(((ArrayList)m[\"content\"])[0]);\n";
    rule += "end";
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(rule));
    KieSession session = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    session.setGlobal("list", list);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    assertEquals(1, ((List) session.getGlobal("list")).size());
    assertEquals("first", ((List) session.getGlobal("list")).get(0));
}
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) Test(org.junit.Test)

Example 25 with KieBase

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

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