Search in sources :

Example 46 with Message

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

the class KnowledgeContextTest method testKnowledgeContext.

private void testKnowledgeContext(final String drlResourceName) {
    final KieBase kbase = loadKnowledgeBase(drlResourceName);
    final KieSession ksession = createKnowledgeSession(kbase);
    final List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert(new Message());
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("Hello World", list.get(0));
}
Also used : Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession)

Example 47 with Message

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

the class DynamicRuleLoadTest method updateToVersion.

public void updateToVersion() {
    KieServices ks = KieServices.Factory.get();
    // Create a new jar for version 1.1.0
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    KieModule km = createAndDeployJar(ks, releaseId2, drl1, drl2_2);
    // try to update the container to version 1.1.0
    kieContainer.updateToVersion(releaseId2);
    // create and use a new session
    ksession.insert(new Message("Hello World"));
}
Also used : Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule)

Example 48 with Message

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

the class ConstraintsTest method testExpressionConstraints1.

@Test
public void testExpressionConstraints1() {
    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" + "                $folderType : getDefaultFolderType(),\n" + "                FolderType.INBOX == $folderType,\n" + "                $folderType == FolderType.INBOX,\n" + "                $mailForFolder2 : getMailTypeForFolderType(getDefaultFolderType()),\n" + "                FolderType.SENT != getDefaultFolderType(), \n" + "                getDefaultFolderType() != FolderType.SENT, \n" + "                getMailTypeForFolderType($folderType) == MailType.WORK,\n" + "                1 > 0\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) Test(org.junit.Test)

Example 49 with Message

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

the class ConstraintsTest method testExpressionConstraints3.

@Test
public void testExpressionConstraints3() {
    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" + "                $type1 : FolderType.INBOX,\n" + "                $type2 : org.drools.compiler.Mailbox$FolderType.INBOX,\n" + "                $work1 : getFolder(null),\n" + "                $work2 : getFolder(org.drools.compiler.Mailbox$FolderType.INBOX),\n" + "                $work3 : getFolder(FolderType.INBOX),\n" + "                getFolder($type1) != null,\n" + "                getFolder($type1).size() > 0,\n" + "                ! getFolder($type1).isEmpty(),\n" + "                $work6 : folders,\n" + "                $work7 : folders.size,\n" + "                //folders.containsKey(FolderType.INBOX),\n" + "                folders.containsKey(org.drools.compiler.Mailbox$FolderType.INBOX),\n" + "                folders.containsKey($type2),\n" + "                !folders.isEmpty,\n" + "                getFolder(FolderType.INBOX) != null,\n" + "                //$v1 : folders[FolderType.INBOX], \n" + "                //$v2 : folders[com.sample.Mailbox.FolderType.INBOX],\n" + "                //$v3 : folders[com.sample.Mailbox$FolderType.INBOX],\n" + "                //folders[$type1]!=null,\n" + "                //folders.get(FolderType.INBOX)!=null, // sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class\n" + "                //folders.isEmpty(),  // SAME ERROR\n" + "                0 < 1\n" + "        )\n" + "    then\n" + "end";
    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) Test(org.junit.Test)

Example 50 with Message

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

the class IncrementalCompilationTest method testIncrementalCompilationWithRemovedError.

@Test
public void testIncrementalCompilationWithRemovedError() throws Exception {
    String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    String drl2_1 = "package org.drools.compiler\n" + "rule R2_1 when\n" + "   $m : Message( nonExistentField == \"Hello World\" )\n" + "then\n" + "end\n";
    String drl2_2 = "package org.drools.compiler\n" + "rule R2_2 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl1).write("src/main/resources/r2.drl", drl2_1);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(1, kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR).size());
    kfs.write("src/main/resources/r2.drl", drl2_2);
    IncrementalResults results = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
    assertEquals(0, results.getAddedMessages().size());
    assertEquals(1, results.getRemovedMessages().size());
    KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    KieSession ksession = kieContainer.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(2, ksession.fireAllRules());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) KieContainer(org.kie.api.runtime.KieContainer) 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