CriteriaMatcher implements Serializable { public static boolean evaluate(Criteria criteria, Map<Tuple2<String, String>, String> observedValues) { for (Map.Entry<Field, String> crit : criteria.getStringMatches().entrySet()) { if (!checkField(crit.getValue(), getObservedValues(criteria, crit.getKey(), observedValues))) { return false; } } if (criteria.getKeywords().size() > 0) { Set<String> observed = getObservedValues(criteria, Field.KEYWORD, observedValues); if (observed.size() > 0) { observed = cleanValues(observed); for (String criteriaKeyword : criteria.getKeywords()) { for (String value : observed) { if (value.contains(criteriaKeyword)) { return true; } } } } } else { return true; } return false; }  static Set<String> getObservedValues(Criteria criteria, Field field, Map<Tuple2<String, String>, String> observed); static boolean checkField(String value, Set<String> observedValues); static boolean evaluate(Criteria criteria, Map<Tuple2<String, String>, String> observedValues);  }
@Ignore @Test public void testCriteriaMatcher() throws ConfigurationException { Map<Tuple2<String, String>, String> observedValues = new HashMap<>(); Criteria criteria = new Criteria().addKeyword("room"); criteria.useSpaceTokens(false); criteria.setCaseSensitive(true); criteria.addMatch(Field.USER, "twitterUser"); criteria.addMatch(Field.LOCATION, "USA"); criteria.addMatch(Field.LANGUAGE, "en"); criteria.addMatch(Field.KEYWORD, "room"); criteria.addMatch(Field.JSON, "{\"source\": \"TWITTER\""); SchemaAdapter schema = new SchemaAdapter(); schema.loadJson("datasift-twitter-schema.json"); criteria.setSchema(schema); observedValues.clear(); observedValues.put(new Tuple2<String, String>("twitter.user.name", "twitter"), "twitterUser"); assertTrue(CriteriaMatcher.evaluate(criteria, observedValues)); observedValues.clear(); observedValues.put(new Tuple2<String, String>("twitter.user.name", "twitter"), "otherUser"); assertFalse(CriteriaMatcher.evaluate(criteria, observedValues)); observedValues.clear(); observedValues.put(new Tuple2<String, String>("pip.location", "twitter"), "USA"); assertTrue(CriteriaMatcher.evaluate(criteria, observedValues)); observedValues.clear(); observedValues.put(new Tuple2<String, String>("pip.location.country", "twitter"), "USA"); assertTrue(CriteriaMatcher.evaluate(criteria, observedValues)); observedValues.clear(); observedValues.put(new Tuple2<String, String>("pip.location", "twitter"), "Mexico"); assertFalse(CriteriaMatcher.evaluate(criteria, observedValues)); observedValues.clear(); observedValues.put(new Tuple2<String, String>("pip.location.country", "twitter"), "Canada"); assertFalse(CriteriaMatcher.evaluate(criteria, observedValues)); observedValues.clear(); observedValues.put(new Tuple2<String, String>("twitter.lang", "twitter"), "en"); assertTrue(CriteriaMatcher.evaluate(criteria, observedValues)); observedValues.clear(); observedValues.put(new Tuple2<String, String>("language.tag", "twitter"), "en"); assertTrue(CriteriaMatcher.evaluate(criteria, observedValues)); observedValues.clear(); observedValues.put(new Tuple2<String, String>("twitter.lang", "twitter"), "spanish"); assertFalse(CriteriaMatcher.evaluate(criteria, observedValues)); observedValues.clear(); observedValues.put(new Tuple2<String, String>("language.tag", "twitter"), "spanish"); assertFalse(CriteriaMatcher.evaluate(criteria, observedValues)); observedValues.clear(); observedValues.put(new Tuple2<String, String>("interaction.content", "twitter"), "Bianca_Hdz15 .That room brings back memories"); assertTrue(CriteriaMatcher.evaluate(criteria, observedValues)); observedValues.clear(); observedValues.put(new Tuple2<String, String>("interaction.content", "twitter"), "This is not a string I am looking for"); assertFalse(CriteriaMatcher.evaluate(criteria, observedValues)); observedValues.clear(); observedValues.put(new Tuple2<String, String>("data", "cognition.esjson"), "{\"source\": \"TWITTER\""); assertTrue(CriteriaMatcher.evaluate(criteria, observedValues)); observedValues.clear(); observedValues.put(new Tuple2<String, String>("data", "cognition.esjson"), "{\"source\": \"FACEBOOK\"}"); assertFalse(CriteriaMatcher.evaluate(criteria, observedValues)); criteria.setCaseSensitive(false); observedValues.clear(); observedValues.put(new Tuple2<String, String>("data", "cognition.esjson"), "{\"source\": \"Twitter\""); assertTrue(CriteriaMatcher.evaluate(criteria, observedValues)); criteria.setCaseSensitive(true); assertFalse(CriteriaMatcher.evaluate(criteria, observedValues)); criteria.useSpaceTokens(true); criteria.addKeyword("here"); observedValues.clear(); observedValues.put(new Tuple2<String, String>("interaction.content", "twitter"), "There are many ways to skin a cat"); assertFalse(CriteriaMatcher.evaluate(criteria, observedValues)); criteria.useSpaceTokens(false); assertTrue(CriteriaMatcher.evaluate(criteria, observedValues)); }