Search in sources :

Example 26 with Message

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

the class HelloWorldTest method testHelloWorld.

@Test
public void testHelloWorld() throws Exception {
    // load up the knowledge base
    KieBase kbase = readKnowledgeBase();
    KieSession ksession = createKnowledgeSession(kbase);
    File testTmpDir = new File("target/test-tmp/");
    testTmpDir.mkdirs();
    KieRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "target/test-tmp/testHelloWorld");
    ksession.getAgendaEventListeners().size();
    // go !
    Message message = new Message();
    message.setMessage("Hello World");
    message.setStatus(Message.HELLO);
    ksession.insert(message);
    ksession.fireAllRules();
    logger.close();
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) File(java.io.File) Test(org.junit.Test)

Example 27 with Message

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

the class Misc2Test method testPackagingJarWithTypeDeclarations.

@Test
public void testPackagingJarWithTypeDeclarations() throws Exception {
    // BZ-1054823
    String drl1 = "package org.drools.compiler\n" + "import org.drools.compiler.Message\n" + "declare Message\n" + "   @role (event)\n" + "end\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    byte[] jar = createKJar(ks, releaseId, null, drl1);
    KieModule km = deployJar(ks, jar);
    // Create a session and fire rules
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    ksession.fireAllRules();
}
Also used : Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 28 with Message

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

the class Misc2Test method testNPEOnMutableGlobal.

@Test
public void testNPEOnMutableGlobal() throws Exception {
    // BZ-1019473
    String str = "package org.drools.compiler\n" + "global java.util.List context\n" + "rule B\n" + "  when\n" + "    Message( message == \"b\" )\n" + "    $s : String() from context\n" + "  then\n" + "    System.out.println($s);\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ResourceFactory.newByteArrayResource(str.getBytes()).setTargetPath("org/drools/compiler/rules.drl"));
    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
    kbuilder.buildAll();
    assertEquals(0, kbuilder.getResults().getMessages().size());
    ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).getKieBase();
    KieSession ksession = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
    assertNotNull(ksession);
    List<String> context = new ArrayList<String>();
    ksession.setGlobal("context", context);
    FactHandle b = ksession.insert(new Message("b"));
    ksession.delete(b);
    int fired = ksession.fireAllRules(1);
    assertEquals(0, fired);
    ksession.dispose();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 29 with Message

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

the class DynamicRuleLoadTest method testKJarUpgrade.

@Test
public void testKJarUpgrade() throws Exception {
    // DROOLS-919
    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1, drl2_1);
    // Create a session and fire rules
    kieContainer = ks.newKieContainer(km.getReleaseId());
    ksession = kieContainer.newKieSession();
    ksession.setGlobal("test", this);
    ksession.insert(new Message("Hi Universe"));
    ksession.fireAllRules();
    assertTrue(done);
}
Also used : Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 30 with Message

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

the class ConstraintsTest method testExpressionConstraints2.

@Test
public void testExpressionConstraints2() {
    final String drl = "package org.drools.compiler.integrationtests\n" + "import " + Mailbox.FolderType.class.getCanonicalName() + ";\n" + "import " + Mailbox.class.getCanonicalName() + ";\n" + "rule R1\n" + "    dialect \"mvel\"\n" + "    when\n" + "        $m : Mailbox( \n" + "                $me : owneremail,\n" + "                recentContacts[Mailbox.TEST_EMAIL] != null,\n" + "                recentContacts[\"me@test.com\"] != null,\n" + "                $d1 : recentContacts[Mailbox.TEST_EMAIL],\n" + "                $d2 : recentContacts[\"me@test.com\"],\n" + "                recentContacts.get(owneremail) != null,\n" + "                recentContacts.get($me) != null,\n" + "                recentContacts[$me] != null,\n" + "                $d3: recentContacts.get(owneremail),\n" + "                // Waiting for MVEL fixes for the following 2 scenarios" + "                // recentContacts[owneremail] != null,\n" + "                // recentContacts[getOwneremail()] != null,\n" + "                0 < 1\n" + "        )\n" + "    then\n" + "end\n";
    final KieBase kbase = loadKnowledgeBaseFromString(drl);
    final KieSession ksession = createKnowledgeSession(kbase);
    final Mailbox mbox = new Mailbox(Mailbox.TEST_EMAIL);
    final Message message = new Message();
    message.setMessage("Welcome");
    message.setStatus(Message.HELLO);
    mbox.getFolder(FolderType.INBOX).add(message);
    ksession.insert(mbox);
    ksession.insert(message);
    final int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : Mailbox(org.drools.compiler.Mailbox) Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) FolderType(org.drools.compiler.Mailbox.FolderType) Test(org.junit.Test)

Aggregations

Message (org.drools.compiler.Message)68 Test (org.junit.Test)65 KieSession (org.kie.api.runtime.KieSession)59 KieServices (org.kie.api.KieServices)41 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)36 ReleaseId (org.kie.api.builder.ReleaseId)30 KieContainer (org.kie.api.runtime.KieContainer)25 KieModule (org.kie.api.builder.KieModule)24 KieBase (org.kie.api.KieBase)20 KieFileSystem (org.kie.api.builder.KieFileSystem)19 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)18 ArrayList (java.util.ArrayList)14 KieRuntimeLogger (org.kie.api.logger.KieRuntimeLogger)8 List (java.util.List)7 KieBuilder (org.kie.api.builder.KieBuilder)6 Resource (org.kie.api.io.Resource)6 File (java.io.File)5 DynamicRulesTest (org.drools.compiler.integrationtests.DynamicRulesTest)4 KieModuleModel (org.kie.api.builder.model.KieModuleModel)4 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)4