@Override public boolean match(WrappedBooking wrappedBooking) { switch (rule.getSimilarityMatchType()) { case IBAN: return StringUtils.equalsIgnoreCase(rule.getExpression(), wrappedBooking.getBankConnection()); case REFERENCE_NAME: if (wrappedBooking.getReferenceName() != null) { Double result = JARO_WINKLER.apply(rule.getExpression(), wrappedBooking.getReferenceName().toLowerCase()); if (result >= MIN_DISTANCE) { log.debug("similarity expression {} compared with {} resulted in score {}", rule.getExpression(), wrappedBooking.getReferenceName().toLowerCase(), result); } return result >= MIN_DISTANCE; } return false; case PURPOSE: Double result = JARO_WINKLER.apply(rule.getExpression(), normalize(wrappedBooking.getPurpose())); if (result >= MIN_DISTANCE) { log.debug("similarity expression {} compared with {} resulted in score {}", rule.getExpression(), normalize(wrappedBooking.getPurpose()), result); } return result >= MIN_DISTANCE; } throw new IllegalArgumentException("missing match type"); }
public static ExpressionMatcher createExpressionMatcher(Rule rule) { ExpressionParser.ExpressionContext ruleContext = null; if (StringUtils.isNotBlank(rule.getExpression())) { try { CharStream input = CharStreams.fromReader(new StringReader(rule.getExpression())); ExpressionLexer lexer = new ExpressionLexer(input); lexer.removeErrorListeners(); lexer.addErrorListener(new ConsoleErrorListener() { @Override public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { log.warn("line " + line + ":" + charPositionInLine + " " + msg); log.warn(input.toString()); } }); ExpressionParser parser = new ExpressionParser(new CommonTokenStream(lexer)); ruleContext = parser.expression(); } catch (IOException e) { throw new RuntimeException(e); } } return new ExpressionMatcher(ruleContext, rule); }
public ExpressionMatcher(ExpressionParser.ExpressionContext ctx, Rule definition) { this.rule = definition; if (ctx == null) { matcher = new CreditorIdMatcher(definition.getCreditorId()); } else { if (ctx.expression().size() == 1) { isNegative = isChildNegative(ctx); matcher = new ExpressionMatcher(ctx.expression(0), definition); } else if (ctx.expression().size() == 2 && ctx.operator() != null) { matcher = new ConnectedRule(definition, ctx); } else if (ctx.statement() != null) { matcher = new Statement(definition.getCreditorId(), ctx.statement()); } } if (matcher == null) { throw new IllegalArgumentException("Invalid matcher expression: " + definition.getExpression()); } }