CompositeRuleEvaluator { public List<AlarmMessage> evaluate(List<CompositeAlarmRule> compositeAlarmRules, List<AlarmMessage> alarmMessages) { final List<AlarmMessage> compositeRuleMessages = new ArrayList<>(); ImmutableListMultimap<String, AlarmMessage> messageMap = Multimaps.index(alarmMessages, alarmMessage -> Joiner.on(Const.ID_CONNECTOR).useForNull(Const.EMPTY_STRING).join(alarmMessage.getId0(), alarmMessage.getId1())); for (CompositeAlarmRule compositeAlarmRule : compositeAlarmRules) { String expr = compositeAlarmRule.getExpression(); Set<String> dependencyRules = expression.analysisInputs(expr); Map<String, Object> dataContext = new HashMap<>(); messageMap.asMap().forEach((key, alarmMessageList) -> { dependencyRules.forEach(ruleName -> dataContext.put(ruleName, false)); alarmMessageList.forEach(alarmMessage -> { if (dependencyRules.contains(alarmMessage.getRuleName())) { dataContext.put(alarmMessage.getRuleName(), true); } }); Object matched = expression.eval(expr, dataContext); if (matched instanceof Boolean && (Boolean) matched) { AlarmMessage headMsg = alarmMessageList.iterator().next(); AlarmMessage message = new AlarmMessage(); message.setOnlyAsCondition(false); message.setScopeId(headMsg.getScopeId()); message.setScope(headMsg.getScope()); message.setName(headMsg.getName()); message.setId0(headMsg.getId0()); message.setId1(headMsg.getId1()); message.setStartTime(System.currentTimeMillis()); message.setRuleName(compositeAlarmRule.getAlarmRuleName()); String alarmMessage = formatMessage(message, compositeAlarmRule.getMessage(), compositeAlarmRule.getExpression()); message.setAlarmMessage(alarmMessage); compositeRuleMessages.add(message); } }); } return compositeRuleMessages; } CompositeRuleEvaluator(Expression expression); List<AlarmMessage> evaluate(List<CompositeAlarmRule> compositeAlarmRules, List<AlarmMessage> alarmMessages);  }
@Test public void testEvaluateMessageWithAndOp() { List<CompositeAlarmRule> compositeAlarmRules = new ArrayList<>(); CompositeAlarmRule compositeAlarmRule = new CompositeAlarmRule("dummy", "a_rule && b_rule", "composite rule {name},{id} triggered!"); compositeAlarmRules.add(compositeAlarmRule); List<AlarmMessage> alarmMessages = getAlarmMessages(); List<AlarmMessage> compositeMsgs = ruleEvaluate.evaluate(compositeAlarmRules, alarmMessages); assertThat(compositeMsgs.size(), is(1)); assertThat(compositeMsgs.get(0).getAlarmMessage(), is("composite rule demo service,id0 triggered!")); assertThat(compositeMsgs.get(0).getRuleName(), is("dummy")); assertThat(compositeMsgs.get(0).getId0(), is("id0")); assertThat(compositeMsgs.get(0).getId1(), is("id1")); assertThat(compositeMsgs.get(0).isOnlyAsCondition(), is(false)); }
@Test public void testEvaluateMessageWithFormatMessage() { List<CompositeAlarmRule> compositeAlarmRules = new ArrayList<>(); CompositeAlarmRule compositeAlarmRule = new CompositeAlarmRule("dummy", "a_rule && b_rule", "composite rule {name} triggered!"); compositeAlarmRules.add(compositeAlarmRule); List<AlarmMessage> alarmMessages = getAlarmMessages(); List<AlarmMessage> compositeMsgs = ruleEvaluate.evaluate(compositeAlarmRules, alarmMessages); assertThat(compositeMsgs.size(), is(1)); assertThat(compositeMsgs.get(0).getAlarmMessage(), is("composite rule demo service triggered!")); assertThat(compositeMsgs.get(0).getRuleName(), is("dummy")); assertThat(compositeMsgs.get(0).getId0(), is("id0")); assertThat(compositeMsgs.get(0).getId1(), is("id1")); assertThat(compositeMsgs.get(0).isOnlyAsCondition(), is(false)); }
@Test public void testEvaluateMessageWithNotExistsRule() { List<CompositeAlarmRule> compositeAlarmRules = new ArrayList<>(); CompositeAlarmRule compositeAlarmRule = new CompositeAlarmRule("dummy", "a_rule && not_exist_rule", "composite rule triggered!"); compositeAlarmRules.add(compositeAlarmRule); List<AlarmMessage> alarmMessages = getAlarmMessages(); List<AlarmMessage> compositeMsgs = ruleEvaluate.evaluate(compositeAlarmRules, alarmMessages); assertThat(compositeMsgs.size(), is(0)); }
@Test public void testEvaluateMessageWithException() { List<CompositeAlarmRule> compositeAlarmRules = new ArrayList<>(); CompositeAlarmRule compositeAlarmRule = new CompositeAlarmRule("dummy", "a_rule + b_rule", "composite rule triggered!"); compositeAlarmRules.add(compositeAlarmRule); List<AlarmMessage> alarmMessages = getAlarmMessages(); List<AlarmMessage> compositeMsgs = ruleEvaluate.evaluate(compositeAlarmRules, alarmMessages); assertThat(compositeMsgs.size(), is(0)); }
@Test public void testEvaluateMessageWithOrOp() { List<CompositeAlarmRule> compositeAlarmRules = new ArrayList<>(); CompositeAlarmRule compositeAlarmRule = new CompositeAlarmRule("dummy", "a_rule || b_rule", "composite rule triggered!"); compositeAlarmRules.add(compositeAlarmRule); List<AlarmMessage> alarmMessages = getAlarmMessages(); alarmMessages.remove(0); List<AlarmMessage> compositeMsgs = ruleEvaluate.evaluate(compositeAlarmRules, alarmMessages); assertThat(compositeMsgs.size(), is(1)); assertThat(compositeMsgs.get(0).getAlarmMessage(), is("composite rule triggered!")); assertThat(compositeMsgs.get(0).getRuleName(), is("dummy")); assertThat(compositeMsgs.get(0).getId0(), is("id0")); assertThat(compositeMsgs.get(0).getId1(), is("id1")); assertThat(compositeMsgs.get(0).isOnlyAsCondition(), is(false)); }
@Test public void testEvaluateMessageWithParenthesisAndOp() { List<CompositeAlarmRule> compositeAlarmRules = new ArrayList<>(); CompositeAlarmRule compositeAlarmRule = new CompositeAlarmRule("dummy", "(a_rule || b_rule) && c_rule", "composite rule triggered!"); compositeAlarmRules.add(compositeAlarmRule); List<AlarmMessage> alarmMessages = getAlarmMessages(); alarmMessages.remove(alarmMessages.size() - 1); List<AlarmMessage> compositeMsgs = ruleEvaluate.evaluate(compositeAlarmRules, alarmMessages); assertThat(compositeMsgs.size(), is(0)); }
@Test public void testEvaluateMessageWithParenthesisAndOrOp() { List<CompositeAlarmRule> compositeAlarmRules = new ArrayList<>(); CompositeAlarmRule compositeAlarmRule = new CompositeAlarmRule("dummy", "(a_rule && b_rule) || c_rule", "composite rule triggered!"); compositeAlarmRules.add(compositeAlarmRule); List<AlarmMessage> alarmMessages = getAlarmMessages(); List<AlarmMessage> compositeMsgs = ruleEvaluate.evaluate(compositeAlarmRules, alarmMessages); assertThat(compositeMsgs.size(), is(1)); assertThat(compositeMsgs.get(0).getAlarmMessage(), is("composite rule triggered!")); assertThat(compositeMsgs.get(0).getRuleName(), is("dummy")); assertThat(compositeMsgs.get(0).getId0(), is("id0")); assertThat(compositeMsgs.get(0).getId1(), is("id1")); assertThat(compositeMsgs.get(0).isOnlyAsCondition(), is(false)); }