Search in sources :

Example 41 with Message

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

the class KieLoggersTest method testKieConsoleLoggerStateless.

@Test
public void testKieConsoleLoggerStateless() throws Exception {
    String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + "    when\n" + "        m : Message( myMessage : message )\n" + "    then\n" + "end";
    // get the resource
    Resource dt = ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("org/drools/integrationtests/hello.drl");
    // create the builder
    StatelessKieSession ksession = getStatelessKieSession(dt);
    KieRuntimeLogger logger = KieServices.Factory.get().getLoggers().newConsoleLogger(ksession);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    ksession.execute(new Message("Hello World"));
    verify(ael).afterMatchFired(any(AfterMatchFiredEvent.class));
    logger.close();
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) Message(org.drools.compiler.Message) Resource(org.kie.api.io.Resource) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 42 with Message

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

the class KieLoggersTest method testDeclarativeKieConsoleLoggerStateless.

@Test
public void testDeclarativeKieConsoleLoggerStateless() throws Exception {
    String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + "    when\n" + "        m : Message( myMessage : message )\n" + "    then\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    kproj.newKieBaseModel("KBase1").newKieSessionModel("KSession1").setType(KieSessionModel.KieSessionType.STATELESS).setConsoleLogger("logger");
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(kproj.toXML());
    kfs.write("src/main/resources/KBase1/rule.drl", drl);
    KieModule kieModule = ks.newKieBuilder(kfs).buildAll().getKieModule();
    KieContainer kieContainer = ks.newKieContainer(kieModule.getReleaseId());
    StatelessKieSession ksession = kieContainer.newStatelessKieSession("KSession1");
    ksession.execute(new Message("Hello World"));
    KieRuntimeLogger logger = ksession.getLogger();
    assertNotNull(logger);
    logger.close();
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieModuleModel(org.kie.api.builder.model.KieModuleModel) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieServices(org.kie.api.KieServices) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 43 with Message

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

the class KieRuntimeTest method testKieRuntimeAccess.

@Test
public void testKieRuntimeAccess() throws IOException, ClassNotFoundException {
    String str = "";
    str += "package org.drools.compiler.test\n";
    str += "import " + Message.class.getName() + "\n";
    str += "rule \"Hello World\"\n";
    str += "when\n";
    str += "    Message( )\n";
    str += "then\n";
    str += "    System.out.println( drools.getKieRuntime() );\n";
    str += "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    kbase = SerializationHelper.serializeObject(kbase);
    final KieSession ksession = createKnowledgeSession(kbase);
    ksession.insert(new Message("help"));
    ksession.fireAllRules();
    ksession.dispose();
}
Also used : Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 44 with Message

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

the class FirstOrderLogicTest method testOrs.

@Test
public void testOrs() throws Exception {
    String str = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getName() + "\n" + "rule X\n" + "    when\n" + "        Message( message == 'test' )\n" + "        Message( !fired ) or eval( !false )\n" + "    then\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.insert(new Message("test"));
    int rules = ksession.fireAllRules();
    assertEquals(2, rules);
    ksession.dispose();
}
Also used : Message(org.drools.compiler.Message) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) SpecialString(org.drools.compiler.SpecialString) Test(org.junit.Test)

Example 45 with Message

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

the class HelloWorldTest method testHelloWorldDebug.

@Test
public void testHelloWorldDebug() throws Exception {
    final Set<String> knownVariables = new HashSet<String>();
    MVELRuntime.resetDebugger();
    MVELDebugHandler.setDebugMode(true);
    MVELRuntime.setThreadDebugger(new Debugger() {

        public int onBreak(Frame frame) {
            System.out.println("onBreak");
            for (String var : frame.getFactory().getKnownVariables()) {
                knownVariables.add(var);
            }
            return 0;
        }
    });
    String source = "org.drools.integrationtests.Rule_Hello_World";
    MVELRuntime.registerBreakpoint(source, 1);
    // 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/testHelloWorldDebug");
    // go !
    Message message = new Message();
    message.setMessage("Hello World");
    message.setStatus(Message.HELLO);
    ksession.insert(message);
    ksession.fireAllRules();
    logger.close();
    assertEquals(6, knownVariables.size());
    assertTrue(knownVariables.contains("drools"));
    assertTrue(knownVariables.contains("myMessage"));
    assertTrue(knownVariables.contains("rule"));
    assertTrue(knownVariables.contains("kcontext"));
    assertTrue(knownVariables.contains("this"));
    assertTrue(knownVariables.contains("m"));
    assertTrue(knownVariables.contains("myMessage"));
}
Also used : Debugger(org.mvel2.debug.Debugger) Frame(org.mvel2.debug.Frame) 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) HashSet(java.util.HashSet) 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