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();
}
}
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());
}
}
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);
}
}
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();
}
}
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();
}
}
Aggregations