Expression { public String getDrl() { return drl; } Expression(Collection<FullTrigger> activeTriggers);  Expression(String expiration, Collection<FullTrigger> activeTriggers); String getDrl(); @Override String toString();  }
@Test public void marketingScenarioDsl() { Trigger trigger = new Trigger(TEST_TENANT, "marketing-scenario","Marketing Scenario"); String expression = "event:groupBy(context.accountId):window(time,10s):having(count > 2)"; ExternalCondition condition = new ExternalCondition(trigger.getId(), FIRING, DATA_ID, ALERTER_ID, expression); Expression exp = new Expression(Arrays.asList(new FullTrigger(trigger, null, Arrays.asList(condition)))); String drl = exp.getDrl(); startSession(drl); clock.setStartupTime(1); long now = clock.getCurrentTime(); Event e1 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E1 - Buy Book"); e1.addContext("duration", "1000"); e1.addContext("accountId", "user1"); Event e6 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E6 - Buy Book"); e6.addContext("duration", "1000"); e6.addContext("accountId", "user2"); insert(e1, e6); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e2 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E2 - Buy Music"); e2.addContext("duration", "2000"); e2.addContext("accountId", "user1"); insert(e2); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e3 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E3 - Buy Groceries"); e3.addContext("duration", "1500"); e3.addContext("accountId", "user1"); insert(e3); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e4 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E4 - Buy VideoGames"); e4.addContext("duration", "3000"); e4.addContext("accountId", "user1"); insert(e4); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e5 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E5 - Buy VideoGames"); e5.addContext("duration", "3000"); e5.addContext("accountId", "user1"); insert(e5); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e7 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E7 - Buy Music"); e7.addContext("duration", "2000"); e7.addContext("accountId", "user2"); insert(e7); now = clock.advanceTime(6000, TimeUnit.MILLISECONDS); Event e8 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E8 - Buy Groceries"); e8.addContext("duration", "1500"); e8.addContext("accountId", "user2"); insert(e8); stopSession(); Assert.assertEquals(4, results.size()); results.stream().forEach(e -> { Assert.assertEquals("user1", e.getContext().get("accountId")); }); }
@Test public void fraudScenarioDsl() { Trigger trigger = new Trigger(TEST_TENANT, "fraud-scenario", "Fraud Scenario"); String expression = "event:groupBy(tags.accountId):window(time,10s):having(count > 1, count.tags.location > 1)"; ExternalCondition condition = new ExternalCondition(trigger.getId(), FIRING, DATA_ID, ALERTER_ID, expression); Expression exp = new Expression(Arrays.asList(new FullTrigger(trigger, null, Arrays.asList(condition)))); String drl = exp.getDrl(); startSession(drl); clock.setStartupTime(1); long now = clock.getCurrentTime(); Event e1 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "Buy Book"); e1.addTag("duration", "1000"); e1.addTag("accountId", "user1"); e1.addTag("location", "ip1"); Event e6 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "Buy Book"); e6.addTag("duration", "1000"); e6.addTag("accountId", "user2"); e6.addTag("location", "ip3"); Event e11 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "Buy Book"); e11.addTag("duration", "1000"); e11.addTag("accountId", "user3"); e11.addTag("location", "ip10"); insert(e1, e6, e11); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e2 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "Buy Music"); e2.addTag("duration", "2000"); e2.addTag("accountId", "user1"); e2.addTag("location", "ip1"); Event e12 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "Buy Music"); e12.addTag("duration", "2000"); e12.addTag("accountId", "user3"); e12.addTag("location", "ip10"); insert(e2, e12); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e3 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "Buy Groceries"); e3.addTag("duration", "1500"); e3.addTag("accountId", "user1"); e3.addTag("location", "ip1"); Event e13 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "Buy Groceries"); e13.addTag("duration", "1500"); e13.addTag("accountId", "user3"); e13.addTag("location", "ip10"); insert(e3, e13); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e4 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "Buy VideoGames"); e4.addTag("duration", "3000"); e4.addTag("accountId", "user1"); e4.addTag("location", "ip2"); Event e14 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "Buy VideoGames"); e14.addTag("duration", "3000"); e14.addTag("accountId", "user3"); e14.addTag("location", "ip10"); insert(e4, e14); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e5 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "Buy VideoGames"); e5.addTag("duration", "3000"); e5.addTag("accountId", "user1"); e5.addTag("location", "ip1"); Event e15 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "Buy VideoGames"); e15.addTag("duration", "3000"); e15.addTag("accountId", "user3"); e15.addTag("location", "ip10"); insert(e5, e15); now = clock.advanceTime(11000, TimeUnit.MILLISECONDS); Event e7 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "Buy Music"); e7.addTag("duration", "2000"); e7.addTag("accountId", "user2"); e7.addTag("location", "ip4"); insert(e7); now = clock.advanceTime(5000, TimeUnit.MILLISECONDS); Event e8 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "Buy Groceries"); e8.addTag("duration", "1500"); e8.addTag("accountId", "user2"); e8.addTag("location", "ip5"); insert(e8); stopSession(); Assert.assertEquals(3, results.size()); results.stream().forEach(e -> { String accountId = e.getContext().get("accountId"); Assert.assertTrue(accountId.equals("user1") || accountId.equals("user2")); }); }
@Test public void customerRetentionScenarioDsl() { Trigger trigger = new Trigger(TEST_TENANT, "customer-retention-scenario", "Customer Retention Scenario"); String expression = "event:groupBy(tags.traceId):" + "filter((category == \"Credit Check\" && text == \"Exceptionally Good\") || " + "(category == \"Stock Check\" && text == \"Out of Stock\")):" + "having(count > 1, count.tags.accountId == 1)"; ExternalCondition condition = new ExternalCondition(trigger.getId(), FIRING, DATA_ID, ALERTER_ID, expression); Expression exp = new Expression(Arrays.asList(new FullTrigger(trigger, null, Arrays.asList(condition)))); String drl = exp.getDrl(); startSession(drl); clock.setStartupTime(1); long now = clock.getCurrentTime(); Event e1 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "Credit Check", "Exceptionally Good"); e1.addTag("duration", "1000"); e1.addTag("traceId", "trace1"); e1.addTag("accountId", "user1"); Event e11 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "Credit Check", "Exceptionally Good"); e11.addTag("duration", "1000"); e11.addTag("traceId", "trace4"); e11.addTag("accountId", "user2"); insert(e1, e11); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e2 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "Stock Check", "Out of Stock"); e2.addTag("duration", "2000"); e2.addTag("traceId", "trace1"); e2.addTag("accountId", "user1"); Event e12 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "Stock Check", "Out of Stock"); e12.addTag("duration", "2000"); e12.addTag("traceId", "trace4"); e12.addTag("accountId", "user2"); insert(e2, e12); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e3 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "Credit Check", "Good"); e3.addTag("duration", "1500"); e3.addTag("traceId", "trace2"); e3.addTag("accountId", "user1"); Event e13 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "Credit Check", "Good"); e13.addTag("duration", "1500"); e13.addTag("traceId", "trace5"); e13.addTag("accountId", "user2"); insert(e3, e13); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e4 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "Stock Check", "Out of Stock"); e4.addTag("duration", "2000"); e4.addTag("traceId", "trace2"); e4.addTag("accountId", "user1"); Event e14 = new Event(TEST_TENANT, uuid(), now + 3, DATA_ID, "Stock Check", "Out of Stock"); e14.addTag("duration", "2000"); e14.addTag("traceId", "trace5"); e14.addTag("accountId", "user2"); insert(e4, e14); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e5 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "Credit Check", "Exceptionally Good"); e5.addTag("duration", "1500"); e5.addTag("traceId", "trace3"); e5.addTag("accountId", "user1"); Event e15 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "Credit Check", "Exceptionally Good"); e15.addTag("duration", "1500"); e15.addTag("traceId", "trace6"); e15.addTag("accountId", "user2"); insert(e5, e15); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event e6 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "Stock Check", "Available"); e6.addTag("duration", "2000"); e6.addTag("traceId", "trace3"); e6.addTag("accountId", "user1"); Event e16 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "Stock Check", "Available"); e16.addTag("duration", "2000"); e16.addTag("traceId", "trace6"); e16.addTag("accountId", "user2"); insert(e6, e16); stopSession(); Assert.assertEquals(2, results.size()); results.stream().forEach(e -> { String traceId = e.getContext().get("traceId"); Assert.assertTrue(traceId.equals("trace1") || traceId.equals("trace4")); }); }
@Test public void combinedScenarios() { List<FullTrigger> activeTriggers = new ArrayList<>(); Trigger trigger = new Trigger(TEST_TENANT, "marketing-scenario","Marketing Scenario"); String expression = "event:groupBy(tags.accountId):window(time,10s):having(count > 2)"; ExternalCondition condition = new ExternalCondition(trigger.getId(), FIRING, DATA_ID, ALERTER_ID, expression); activeTriggers.add(new FullTrigger(trigger, null, Arrays.asList(condition))); trigger = new Trigger(TEST_TENANT, "fraud-scenario", "Fraud Scenario"); expression = "event:groupBy(tags.accountId):window(time,10s):having(count > 1, count.tags.location > 1)"; condition = new ExternalCondition(trigger.getId(), FIRING, DATA_ID, ALERTER_ID, expression); activeTriggers.add(new FullTrigger(trigger, null, Arrays.asList(condition))); trigger = new Trigger(TEST_TENANT, "customer-retention-scenario", "Customer Retention Scenario"); expression = "event:groupBy(tags.traceId):" + "filter((category == \"Credit Check\" && text == \"Exceptionally Good\") || " + "(category == \"Stock Check\" && text == \"Out of Stock\")):" + "having(count > 1, count.tags.accountId == 1)"; condition = new ExternalCondition(trigger.getId(), FIRING, DATA_ID, ALERTER_ID, expression); activeTriggers.add(new FullTrigger(trigger, null, Arrays.asList(condition))); Expression exp = new Expression(activeTriggers); String drl = exp.getDrl(); startSession(drl); clock.setStartupTime(1); long now = clock.getCurrentTime(); Event a1 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "A1 - Buy Book"); a1.addContext("duration", "1000"); a1.addContext("accountId", "user1"); Event a6 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "A6 - Buy Book"); a6.addContext("duration", "1000"); a6.addContext("accountId", "user2"); Event b1 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "B1 - Buy Book"); b1.addTag("duration", "1000"); b1.addTag("accountId", "user1"); b1.addTag("location", "ip1"); Event b6 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "B6 - Buy Book"); b6.addTag("duration", "1000"); b6.addTag("accountId", "user2"); b6.addTag("location", "ip3"); Event b11 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "B11 - Buy Book"); b11.addTag("duration", "1000"); b11.addTag("accountId", "user3"); b11.addTag("location", "ip10"); Event e1 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E1 - Buy Book"); e1.addTag("duration", "1000"); e1.addTag("accountId", "user1"); e1.addTag("location", "ip1"); Event e6 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E6 - Buy Book"); e6.addTag("duration", "1000"); e6.addTag("accountId", "user2"); e6.addTag("location", "ip3"); Event e11 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E11 - Buy Book"); e11.addTag("duration", "1000"); e11.addTag("accountId", "user3"); e11.addTag("location", "ip10"); insert(a1, a6, b1, b6, b11, e1, e6, e11); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event a2 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "A2 - Buy Music"); a2.addContext("duration", "2000"); a2.addContext("accountId", "user1"); Event b2 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "B2 - Buy Music"); b2.addTag("duration", "2000"); b2.addTag("accountId", "user1"); b2.addTag("location", "ip1"); Event b12 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "B12 - Buy Music"); b12.addTag("duration", "2000"); b12.addTag("accountId", "user3"); b12.addTag("location", "ip10"); Event e2 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E2 - Buy Music"); e2.addTag("duration", "2000"); e2.addTag("accountId", "user1"); e2.addTag("location", "ip1"); Event e12 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E12 - Buy Music"); e12.addTag("duration", "2000"); e12.addTag("accountId", "user3"); e12.addTag("location", "ip10"); insert(a2, b2, b12, e2, e12); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event a3 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "A3 - Buy Groceries"); a3.addContext("duration", "1500"); a3.addContext("accountId", "user1"); Event b3 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "B3 - Buy Groceries"); b3.addTag("duration", "1500"); b3.addTag("accountId", "user1"); b3.addTag("location", "ip1"); Event b13 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "B13 - Buy Groceries"); b13.addTag("duration", "1500"); b13.addTag("accountId", "user3"); b13.addTag("location", "ip10"); Event e3 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E3 - Buy Groceries"); e3.addTag("duration", "1500"); e3.addTag("accountId", "user1"); e3.addTag("location", "ip1"); Event e13 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E13 - Buy Groceries"); e13.addTag("duration", "1500"); e13.addTag("accountId", "user3"); e13.addTag("location", "ip10"); insert(a3, b3, b13, e3, e13); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event a4 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "A4 - Buy VideoGames"); a4.addContext("duration", "3000"); a4.addContext("accountId", "user1"); Event b4 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "B4 - Buy VideoGames"); b4.addTag("duration", "3000"); b4.addTag("accountId", "user1"); b4.addTag("location", "ip2"); Event b14 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "B14 - Buy VideoGames"); b14.addTag("duration", "3000"); b14.addTag("accountId", "user3"); b14.addTag("location", "ip10"); Event e4 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E4 - Buy VideoGames"); e4.addTag("duration", "3000"); e4.addTag("accountId", "user1"); e4.addTag("location", "ip2"); Event e14 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E14 - Buy VideoGames"); e14.addTag("duration", "3000"); e14.addTag("accountId", "user3"); e14.addTag("location", "ip10"); insert(a4, b4, b14, e4, e14); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event a5 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "A5 - Buy VideoGames"); a5.addContext("duration", "3000"); a5.addContext("accountId", "user1"); Event b5 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "B5 - Buy VideoGames"); b5.addTag("duration", "3000"); b5.addTag("accountId", "user1"); b5.addTag("location", "ip1"); Event b15 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "B15 - Buy VideoGames"); b15.addTag("duration", "3000"); b15.addTag("accountId", "user3"); b15.addTag("location", "ip10"); Event e5 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E5 - Buy VideoGames"); e5.addTag("duration", "3000"); e5.addTag("accountId", "user1"); e5.addTag("location", "ip1"); Event e15 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E15 - Buy VideoGames"); e15.addTag("duration", "3000"); e15.addTag("accountId", "user3"); e15.addTag("location", "ip10"); insert(a5, b5, b15, e5, e15); now = clock.advanceTime(1000, TimeUnit.MILLISECONDS); Event a7 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "A7 - Buy Music"); a7.addContext("duration", "2000"); a7.addContext("accountId", "user2"); insert(a7); now = clock.advanceTime(6000, TimeUnit.MILLISECONDS); Event a8 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "A8 - Buy Groceries"); a8.addContext("duration", "1500"); a8.addContext("accountId", "user2"); insert(a8); now = clock.advanceTime(4000, TimeUnit.MILLISECONDS); Event b7 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "B7 - Buy Music"); b7.addTag("duration", "2000"); b7.addTag("accountId", "user2"); b7.addTag("location", "ip4"); Event e7 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E7 - Buy Music"); e7.addTag("duration", "2000"); e7.addTag("accountId", "user2"); e7.addTag("location", "ip4"); insert(b7, e7); now = clock.advanceTime(5000, TimeUnit.MILLISECONDS); Event b8 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "B8 - Buy Groceries"); b8.addTag("duration", "1500"); b8.addTag("accountId", "user2"); b8.addTag("location", "ip5"); Event e8 = new Event(TEST_TENANT, uuid(), now, DATA_ID, "TraceCompletion", "E8 - Buy Groceries"); e8.addTag("duration", "1500"); e8.addTag("accountId", "user2"); e8.addTag("location", "ip5"); insert(b8, e8); stopSession(); Assert.assertEquals(15, results.size()); results.stream().forEach(e -> { }); }