Search in sources :

Example 1 with CorrelationEventsLog

use of org.openksavi.sponge.test.util.CorrelationEventsLog 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 2 with CorrelationEventsLog

use of org.openksavi.sponge.test.util.CorrelationEventsLog in project sponge by softelnet.

the class CoreUnorderedRulesTest method doTestUnorderedRulesEvents.

public static void doTestUnorderedRulesEvents(SpongeEngine engine, long timeout) {
    CorrelationEventsLog eventsLog = engine.getOperations().getVariable(CorrelationEventsLog.class, CorrelationEventsLog.VARIABLE_NAME);
    // Real test event sequence: a1, b1, b2, b3, c1, c2, a2, c3
    Map<String, String[][]> expected = new LinkedHashMap<>();
    // @formatter:off
    expected.put("RuleF", new String[][] { { "a1" }, { "a2" } });
    expected.put("RuleFFF", new String[][] { { "a1", "b1", "c1" }, { "b1", "c1", "a2" }, { "b2", "c1", "a2" }, { "b3", "c1", "a2" } });
    expected.put("RuleFFFDuration", expected.get("RuleFFF"));
    expected.put("RuleFFL", expected.get("RuleFFF"));
    expected.put("RuleFFA", new String[][] { { "a1", "b1", "c1" }, { "b1", "c1", "a2" }, { "b1", "a2", "c3" }, { "b2", "c1", "a2" }, { "b2", "a2", "c3" }, { "b3", "c1", "a2" }, { "b3", "a2", "c3" } });
    expected.put("RuleFLF", new String[][] { { "a1", "b3", "c1" }, { "b1", "c1", "a2" }, { "b2", "c1", "a2" }, { "b3", "c1", "a2" } });
    expected.put("RuleFLL", new String[][] { { "a1", "b3", "c1" }, { "b1", "c1", "a2" }, { "b2", "c1", "a2" }, { "b3", "c1", "a2" } });
    expected.put("RuleFLA", new String[][] { { "a1", "b3", "c1" }, { "b1", "c1", "a2" }, { "b1", "a2", "c3" }, { "b2", "c1", "a2" }, { "b2", "a2", "c3" }, { "b3", "c1", "a2" }, { "b3", "a2", "c3" } });
    expected.put("RuleFAF", new String[][] { { "a1", "b1", "c1" }, { "a1", "b2", "c1" }, { "a1", "b3", "c1" }, { "b1", "c1", "a2" }, { "b2", "c1", "a2" }, { "b3", "c1", "a2" } });
    expected.put("RuleFAL", new String[][] { { "a1", "b1", "c1" }, { "a1", "b2", "c1" }, { "a1", "b3", "c1" }, { "b1", "c1", "a2" }, { "b2", "c1", "a2" }, { "b3", "c1", "a2" } });
    expected.put("RuleFAA", new String[][] { { "a1", "b1", "c1" }, { "a1", "b2", "c1" }, { "a1", "b3", "c1" }, { "b1", "c1", "a2" }, { "b1", "a2", "c3" }, { "b2", "c1", "a2" }, { "b2", "a2", "c3" }, { "b3", "c1", "a2" }, { "b3", "a2", "c3" } });
    expected.put("RuleFFN", new String[][] { { "b1", "c1", null }, { "b2", "c1", null }, { "b3", "c1", null } });
    expected.put("RuleFLN", expected.get("RuleFFN"));
    expected.put("RuleFAN", expected.get("RuleFFN"));
    expected.put("RuleFNF", new String[][] { { "a1", "c1", null }, { "c1", "a2", null }, { "c2", "a2", null }, { "a2", "c3", null } });
    expected.put("RuleFNL", expected.get("RuleFNF"));
    expected.put("RuleFNA", expected.get("RuleFNF"));
    // @formatter:on
    expected.put("RuleFFNReject", new String[][] {});
    expected.put("RuleFLNReject", new String[][] {});
    expected.put("RuleFANReject", new String[][] {});
    expected.put("RuleFNNReject", new String[][] { { "c3", null, null } });
    expected.put("RuleFNFReject", new String[][] { { "c1", "a2", null }, { "c2", "a2", null }, { "a2", "c3", null } });
    expected.put("RuleFNLReject", expected.get("RuleFNFReject"));
    expected.put("RuleFNAReject", expected.get("RuleFNFReject"));
    expected.put("RuleLFNReject", new String[][] {});
    expected.put("RuleLLNReject", new String[][] {});
    expected.put("RuleLANReject", new String[][] {});
    expected.put("RuleLNNReject", new String[][] { { "c3", null, null } });
    expected.put("RuleLNFReject", new String[][] { { "c1", "a2", null }, { "c2", "a2", null }, { "a2", "c3", null } });
    expected.put("RuleLNLReject", expected.get("RuleLNFReject"));
    expected.put("RuleLNAReject", expected.get("RuleLNFReject"));
    expected.put("RuleAFNReject", new String[][] {});
    expected.put("RuleALNReject", new String[][] {});
    expected.put("RuleAANReject", new String[][] {});
    expected.put("RuleANNReject", new String[][] { { "c3", null, null } });
    expected.put("RuleANFReject", new String[][] { { "c1", "a2", null }, { "c2", "a2", null }, { "a2", "c3", null } });
    expected.put("RuleANLReject", expected.get("RuleANFReject"));
    expected.put("RuleANAReject", expected.get("RuleANFReject"));
    expected.put("RuleNFNReject", new String[][] {});
    expected.put("RuleNLNReject", new String[][] {});
    expected.put("RuleNANReject", new String[][] {});
    expected.put("RuleNNNReject", new String[][] {});
    expected.put("RuleNNFReject", new String[][] {});
    expected.put("RuleNNLReject", new String[][] {});
    expected.put("RuleNNAReject", new String[][] {});
    try {
        TimeUnit.SECONDS.sleep(10);
    } catch (InterruptedException e) {
        throw SpongeUtils.wrapException(e);
    }
    expected.forEach((rule, sequences) -> {
        try {
            logger.info("Checking {}...", rule);
            await().atMost(timeout, TimeUnit.SECONDS).until(() -> eventsLog.getEvents(rule, null).size() >= sequences.length);
            TestUtils.assertEventSequences(eventsLog, rule, null, sequences, true);
            logger.info("Rule {} OK; expected sequences: {}", rule, sequences);
        } catch (Exception e) {
            logger.error("Unsuccessful waiting for rule {}; expected sequences: {}", rule, (Object) sequences);
            throw e;
        }
    });
    assertFalse(engine.isError());
}
Also used : CorrelationEventsLog(org.openksavi.sponge.test.util.CorrelationEventsLog) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with CorrelationEventsLog

use of org.openksavi.sponge.test.util.CorrelationEventsLog in project sponge by softelnet.

the class RulesTestTemplate method testRulesNoneModeEvents.

public static void testRulesNoneModeEvents(KnowledgeBaseType type) {
    SpongeEngine engine = ScriptTestUtils.startWithKnowledgeBase(type, "rules_none_mode_events");
    try {
        CorrelationEventsLog eventsLog = engine.getOperations().getVariable(CorrelationEventsLog.class, CorrelationEventsLog.VARIABLE_NAME);
        Map<String, String[][]> expected = new LinkedHashMap<>();
        expected.put("RuleFNNF", new String[][] { { "1", null, null, "5" } });
        expected.put("RuleFNNNL", new String[][] { { "1", null, null, null, "7" } });
        expected.put("RuleFNNNLReject", new String[][] {});
        expected.put("RuleFNFNL", new String[][] { { "1", null, "2", null, "7" } });
        expected.forEach((rule, sequences) -> {
            try {
                await().atMost(180, TimeUnit.SECONDS).until(() -> eventsLog.getEvents(rule, "1").size() >= sequences.length);
            } catch (Exception e) {
                logger.error("Unsuccessful waiting for rule {} sequences {}", rule, (Object) sequences);
                throw e;
            }
        });
        expected.forEach((rule, sequences) -> TestUtils.assertEventSequences(eventsLog, rule, "1", sequences));
        assertFalse(engine.isError());
    } finally {
        engine.shutdown();
    }
}
Also used : CorrelationEventsLog(org.openksavi.sponge.test.util.CorrelationEventsLog) SpongeEngine(org.openksavi.sponge.engine.SpongeEngine) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with CorrelationEventsLog

use of org.openksavi.sponge.test.util.CorrelationEventsLog in project sponge by softelnet.

the class RulesTestTemplate method testRulesNoneModeEventsConditions.

public static void testRulesNoneModeEventsConditions(KnowledgeBaseType type) {
    SpongeEngine engine = ScriptTestUtils.startWithKnowledgeBase(type, "rules_none_mode_events_conditions");
    try {
        CorrelationEventsLog eventsLog = engine.getOperations().getVariable(CorrelationEventsLog.class, CorrelationEventsLog.VARIABLE_NAME);
        Map<String, String[][]> expected = new LinkedHashMap<>();
        expected.put("RuleFNF", new String[][] { { "1", null, "5" } });
        expected.put("RuleFNNFReject", new String[][] {});
        expected.forEach((rule, sequences) -> {
            try {
                await().atMost(180, TimeUnit.SECONDS).until(() -> eventsLog.getEvents(rule, "1").size() >= sequences.length);
            } catch (Exception e) {
                logger.error("Unsuccessful waiting for rule {} sequences {}", rule, (Object) sequences);
                throw e;
            }
        });
        expected.forEach((rule, sequences) -> TestUtils.assertEventSequences(eventsLog, rule, "1", sequences));
        assertFalse(engine.isError());
    } finally {
        engine.shutdown();
    }
}
Also used : CorrelationEventsLog(org.openksavi.sponge.test.util.CorrelationEventsLog) SpongeEngine(org.openksavi.sponge.engine.SpongeEngine) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with CorrelationEventsLog

use of org.openksavi.sponge.test.util.CorrelationEventsLog in project sponge by softelnet.

the class CoreRulesTest method doTestRulesImmediate.

private void doTestRulesImmediate(SpongeEngine engine, Map<String, String[][]> expected) {
    CorrelationEventsLog eventsLog = engine.getOperations().getVariable(CorrelationEventsLog.class, CorrelationEventsLog.VARIABLE_NAME);
    try {
        TimeUnit.SECONDS.sleep(2);
    } catch (InterruptedException e) {
        throw SpongeUtils.wrapException(e);
    }
    expected.forEach((rule, sequences) -> {
        try {
            await().atMost(180, TimeUnit.SECONDS).until(() -> eventsLog.getEvents(rule, "1").size() >= sequences.length);
        } catch (Exception e) {
            logger.error("Unsuccessful waiting for rule {} sequences {}", rule, (Object) sequences);
            throw e;
        }
    });
    expected.forEach((rule, sequences) -> TestUtils.assertEventSequences(eventsLog, rule, "1", sequences));
}
Also used : CorrelationEventsLog(org.openksavi.sponge.test.util.CorrelationEventsLog) SpongeException(org.openksavi.sponge.SpongeException)

Aggregations

CorrelationEventsLog (org.openksavi.sponge.test.util.CorrelationEventsLog)9 LinkedHashMap (java.util.LinkedHashMap)5 SpongeEngine (org.openksavi.sponge.engine.SpongeEngine)4 Test (org.junit.Test)2 DefaultSpongeEngine (org.openksavi.sponge.core.engine.DefaultSpongeEngine)2 SpongeException (org.openksavi.sponge.SpongeException)1