protected LogicalClause(String key, Object value) throws JSONException { operatorName = key.trim(); operator = LogicalOperator.parse(operatorName); children = new ArrayList<>(); ApptentiveLog.v(INTERACTIONS, " + LogicalClause of type \"%s\"", operatorName); if (value instanceof JSONArray) { JSONArray jsonArray = (JSONArray) value; for (int i = 0; i < jsonArray.length(); i++) { JSONObject child = (JSONObject) jsonArray.get(i); children.add(ClauseParser.parse(null, child)); } } else if (value instanceof JSONObject) { JSONObject jsonObject = (JSONObject) value; Iterator<String> it = jsonObject.keys(); while (it.hasNext()) { String currentKey = it.next(); if (!jsonObject.isNull(currentKey)) { children.add(ClauseParser.parse(currentKey, jsonObject.get(currentKey))); } else { children.add(ClauseParser.parse(currentKey, null)); } } } else { ApptentiveLog.w(INTERACTIONS, "Unrecognized LogicalClause: %s", value.toString()); } }
public Comparable getValue(String query) { Object rawValue = doGetValue(query); return (Comparable) ClauseParser.parseValue(rawValue); }
public static Clause parse(String json) throws JSONException { ApptentiveLog.v(INTERACTIONS, "+ Parsing Interaction Criteria."); if (json == null) { ApptentiveLog.e(INTERACTIONS, "+ Interaction Criteria is null."); return null; } JSONObject root = new JSONObject(json); Clause ret = ClauseParser.parse(null, root); ApptentiveLog.v(INTERACTIONS, "+ Finished parsing Interaction Criteria."); return ret; }
private List<ConditionalTest> getConditions(JSONObject conditionObject) { List<ConditionalTest> conditionalTests = new ArrayList<ConditionalTest>(); Iterator<String> operators = conditionObject.keys(); while (operators.hasNext()) { String operator = operators.next(); Object value = null; if (!conditionObject.isNull(operator)) { value = ClauseParser.parseValue(conditionObject.opt(operator)); } conditionalTests.add(new ConditionalTest(ConditionalOperator.parse(operator), value)); } return conditionalTests; }
public boolean isMet(FieldManager fieldManager, boolean verbose) { try { Clause rootClause = ClauseParser.parse(json); boolean ret = false; if (rootClause != null) { IndentPrinter printer = verbose ? new IndentBufferedPrinter() : IndentPrinter.NULL; ret = rootClause.evaluate(fieldManager, printer); if (verbose) { ApptentiveLog.i(INTERACTIONS, "Criteria evaluated => %b", ret); ApptentiveLog.d(INTERACTIONS, "Criteria evaluation details:\n%s", printer); } } else { if (verbose) { ApptentiveLog.i(INTERACTIONS, "Criteria could not be evaluated: no clause found"); } } return ret; } catch (Exception e) { ApptentiveLog.e(INTERACTIONS, e, "Exception while evaluating interaction criteria"); logException(e); } return false; } }
public ConditionalClause(String field, Object inputValue) { this.fieldName = field.trim(); ApptentiveLog.v(INTERACTIONS, " + ConditionalClause for query: \"%s\"", fieldName); if (inputValue instanceof JSONObject && !isComplexType((JSONObject) inputValue)) { conditionalTests = getConditions((JSONObject) inputValue); } else { conditionalTests = new ArrayList<>(); conditionalTests.add(new ConditionalTest(ConditionalOperator.$eq, ClauseParser.parseValue(inputValue))); } }
@Test public void predicateParsing() throws JSONException { String json = loadTextAssetAsString(TEST_DATA_DIR + "testPredicateParsing.json"); try { Clause criteria = ClauseParser.parse(json); assertNotNull("Criteria was null, but it shouldn't be.", criteria); } catch (JSONException e) { ApptentiveLog.e(e, "Error parsing test JSON."); assertNotNull(e); } } }