Search in sources :

Example 16 with SpongeEngine

use of org.openksavi.sponge.engine.SpongeEngine in project sponge by softelnet.

the class CoreRulesTest method testRulesEventPattern.

@Test
public void testRulesEventPattern() {
    SpongeEngine engine = DefaultSpongeEngine.builder().knowledgeBase(TestUtils.DEFAULT_KB, "examples/core/rules_event_pattern.py").build();
    engine.startup();
    try {
        CorrelationEventsLog eventsLog = engine.getOperations().getVariable(CorrelationEventsLog.class, CorrelationEventsLog.VARIABLE_NAME);
        await().atMost(20, TimeUnit.SECONDS).until(() -> eventsLog.getAllEvents("NameRule").size() >= 1 && eventsLog.getAllEvents("PatternRule").size() >= 3);
        assertEquals(1, eventsLog.getAllEvents("NameRule").size());
        assertEquals(3, eventsLog.getAllEvents("PatternRule").size());
        TestUtils.assertEventSequences(eventsLog, "NameRule", "a1", new String[][] { { "a1", "b1" } });
        TestUtils.assertEventSequences(eventsLog, "PatternRule", "a1", new String[][] { { "a1", "b1" }, { "a1", "b2" } });
        TestUtils.assertEventSequences(eventsLog, "PatternRule", "a2", new String[][] { { "a2", "b2" } });
        assertFalse(engine.isError());
    } finally {
        engine.shutdown();
    }
}
Also used : CorrelationEventsLog(org.openksavi.sponge.test.util.CorrelationEventsLog) DefaultSpongeEngine(org.openksavi.sponge.core.engine.DefaultSpongeEngine) SpongeEngine(org.openksavi.sponge.engine.SpongeEngine) Test(org.junit.Test)

Example 17 with SpongeEngine

use of org.openksavi.sponge.engine.SpongeEngine in project sponge by softelnet.

the class EngineBuilderTest method testEngineBuilder.

@Test
public void testEngineBuilder() throws InterruptedException {
    SpongeEngine engine = createAndStartupEngine();
    try {
        await().pollDelay(3, TimeUnit.SECONDS).atMost(30, TimeUnit.SECONDS).until(() -> getEvents(engine, "e1").size() >= 1 && getEvents(engine, "e1e2-first").size() >= 2 && getEvents(engine, "e1e2-last").size() >= 2 && getEvents(engine, "e1e2-all").size() >= 7);
        assertEquals(2, getEvents(engine, "e1").size());
        assertEquals(2, getEvents(engine, "e1e2-first").size());
        assertEquals(2, getEvents(engine, "e1e2-last").size());
        assertEquals(7, getEvents(engine, "e1e2-all").size());
        assertEquals(0, getEvents(engine, "e3").size());
        assertEquals(2, getEvents(engine, "e1e2-first").get(0).<Number>get("mark"));
        assertEquals(4, getEvents(engine, "e1e2-first").get(1).<Number>get("mark"));
        assertEquals(6, getEvents(engine, "e1e2-last").get(0).<Number>get("mark"));
        assertEquals(6, getEvents(engine, "e1e2-last").get(1).<Number>get("mark"));
        assertEquals(2, getEvents(engine, "e1e2-all").get(0).<Number>get("mark"));
        assertEquals(4, getEvents(engine, "e1e2-all").get(1).<Number>get("mark"));
        assertEquals(4, getEvents(engine, "e1e2-all").get(2).<Number>get("mark"));
        assertEquals(5, getEvents(engine, "e1e2-all").get(3).<Number>get("mark"));
        assertEquals(5, getEvents(engine, "e1e2-all").get(4).<Number>get("mark"));
        assertEquals(6, getEvents(engine, "e1e2-all").get(5).<Number>get("mark"));
        assertEquals(6, getEvents(engine, "e1e2-all").get(6).<Number>get("mark"));
    } finally {
        engine.shutdown();
    }
    if (engine.isError()) {
        throw SpongeUtils.wrapException(engine.getError());
    }
}
Also used : DefaultSpongeEngine(org.openksavi.sponge.core.engine.DefaultSpongeEngine) SpongeEngine(org.openksavi.sponge.engine.SpongeEngine) Test(org.junit.Test)

Example 18 with SpongeEngine

use of org.openksavi.sponge.engine.SpongeEngine in project sponge by softelnet.

the class MpdEventsMain method run.

public void run() {
    SpongeEngine engine = DefaultSpongeEngine.builder().config("examples/mpd/mpd_events.xml").build();
    try {
        engine.startup();
        SpongeUtils.registerShutdownHook(engine);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : DefaultSpongeEngine(org.openksavi.sponge.core.engine.DefaultSpongeEngine) SpongeEngine(org.openksavi.sponge.engine.SpongeEngine)

Example 19 with SpongeEngine

use of org.openksavi.sponge.engine.SpongeEngine in project sponge by softelnet.

the class ActionsTestTemplate method testActions.

public static void testActions(KnowledgeBaseType type) {
    SpongeEngine engine = ScriptTestUtils.startWithKnowledgeBase(type, "actions");
    try {
        await().atMost(30, TimeUnit.SECONDS).until(() -> engine.getOperations().getVariable("scriptActionResult") != null);
        await().atMost(30, TimeUnit.SECONDS).until(() -> engine.getOperations().getVariable("javaActionResult") != null);
        Object[] scriptResult = (Object[]) engine.getOperations().getVariable("scriptActionResult");
        assertEquals(2, scriptResult.length);
        // Note, that different scripting engines may map numbers to different types.
        assertEquals(1, ((Number) scriptResult[0]).intValue());
        assertEquals("test", scriptResult[1]);
        Object[] javaResult = (Object[]) engine.getOperations().getVariable("javaActionResult");
        assertEquals(2, javaResult.length);
        // Note, that different scripting engines may map numbers to different types.
        assertEquals(2, ((Number) javaResult[0]).intValue());
        assertEquals("TEST", javaResult[1]);
        assertFalse(engine.isError());
    } finally {
        engine.shutdown();
    }
}
Also used : SpongeEngine(org.openksavi.sponge.engine.SpongeEngine)

Example 20 with SpongeEngine

use of org.openksavi.sponge.engine.SpongeEngine in project sponge by softelnet.

the class EventsTestTemplate method testRemovingEvent.

public static void testRemovingEvent(KnowledgeBaseType type) {
    SpongeEngine engine = ScriptTestUtils.startWithKnowledgeBase(type, "events_removing");
    try {
        await().pollDelay(3, TimeUnit.SECONDS).atMost(30, TimeUnit.SECONDS).until(() -> engine.getOperations().getVariable(Number.class, "eventCounter").intValue() == engine.getOperations().getVariable(Number.class, "allowNumber").intValue());
        TimeUnit.SECONDS.sleep(2);
        assertEquals(engine.getOperations().getVariable(Number.class, "allowNumber").intValue(), engine.getOperations().getVariable(Number.class, "eventCounter").intValue());
        assertFalse(engine.isError());
    } catch (InterruptedException ie) {
        throw new SpongeException(ie);
    } finally {
        engine.shutdown();
    }
}
Also used : SpongeException(org.openksavi.sponge.SpongeException) SpongeEngine(org.openksavi.sponge.engine.SpongeEngine)

Aggregations

SpongeEngine (org.openksavi.sponge.engine.SpongeEngine)91 DefaultSpongeEngine (org.openksavi.sponge.core.engine.DefaultSpongeEngine)55 Test (org.junit.Test)49 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 StandaloneEngineMain (org.openksavi.sponge.standalone.StandaloneEngineMain)6 SpringSpongeEngine (org.openksavi.sponge.spring.SpringSpongeEngine)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)5 CamelContext (org.apache.camel.CamelContext)4 SpongeException (org.openksavi.sponge.SpongeException)4 CorrelationEventsLog (org.openksavi.sponge.test.util.CorrelationEventsLog)4 LinkedHashMap (java.util.LinkedHashMap)3 ProducerTemplate (org.apache.camel.ProducerTemplate)3 RouteBuilder (org.apache.camel.builder.RouteBuilder)3 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)3 SimpleRegistry (org.apache.camel.impl.SimpleRegistry)3 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 IOException (java.io.IOException)1 Reader (java.io.Reader)1 BigInteger (java.math.BigInteger)1 List (java.util.List)1