@Test public void requireThatJsonFormatCanBeEvaluated() throws IOException { String evaluateJson = String.join("\n", "{", " \"expression\": \"t1 * t2\",", " \"arguments\": [", " {", " \"name\": \"t1\",", " \"type\": \"tensor(x[])\",", " \"value\": \"{{x:0}:1,{x:1}:2}\"", " },", " {", " \"name\": \"t2\",", " \"type\": \"tensor(x[])\",", " \"value\": \"{{x:0}:2,{x:1}:3}\"", " }", " ]", "}"); String expectedJson = String.join("\n", "{", " \"type\": \"tensor(x[])\",", " \"value\": {", " \"literal\": \"tensor(x[]):{0:2.0,1:6.0}\",", " \"cells\": [{\"address\":{\"x\":\"0\"},\"value\":2.0},{\"address\":{\"x\":\"1\"},\"value\":6.0}]", " }", "}"); String resultJson = ExpressionEvaluator.evaluate(evaluateJson); ObjectMapper m = new ObjectMapper(); JsonNode result = m.readTree(resultJson); JsonNode expected = m.readTree(expectedJson); assertEquals(expected.get("type").asText(), result.get("type").asText()); assertEquals(expected.get("value").get("literal").asText(), result.get("value").get("literal").asText()); }
public static String evaluate(String json) throws IOException { ObjectMapper m = new ObjectMapper(); JsonNode root = m.readTree(json); if (root == null) { return error("Could not parse json to evaluate"); } if (!root.has("expression")) { return error("Required field missing: expression"); } String expression = root.get("expression").asText(); try { MapContext context = new MapContext(); if (root.has("arguments")) { JsonNode arguments = root.get("arguments"); if (!arguments.isArray()) { return error("Invalid JSON format: arguments must be an array"); } for (JsonNode argument : arguments) { if (!argument.isObject()) { return error("Invalid JSON format: argument must be an object"); } if (!argument.has("name")) { return error("Invalid JSON format: argument must have a name"); } if (!argument.has("type")) { return error("Invalid JSON format: argument must have a type"); } if (!argument.has("value")) { return error("Invalid JSON format: argument must have a value"); } String name = argument.get("name").asText(); String type = argument.get("type").asText(); Value value; if ("string".equalsIgnoreCase(type)) { value = new StringValue(argument.get("value").asText()); } else if ("double".equalsIgnoreCase(type)) { value = new DoubleValue(argument.get("value").asDouble()); } else if ("boolean".equalsIgnoreCase(type)) { value = new BooleanValue(argument.get("value").asBoolean()); } else if (type.toLowerCase().startsWith("tensor(")) { value = new TensorValue(Tensor.from(type + ":" + argument.get("value").asText())); } else { return error("Unknown argument type: " + type); } context.put(name, value); } } Value value = evaluate(expression, context); String valueType = valueType(value); if (valueType.equals("unknown")) { return error("Evaluation of ranking expression returned unknown value type"); } return String.join("\n", "{", " \"type\": \"" + valueType + "\",", " \"value\": " + valueValue(value), "}"); } catch (ParseException e) { return error("Could not parse expression: " + expression); } catch (IllegalArgumentException e) { return error(e.getMessage()); } }
ExpressionEvaluator { public static String evaluate(String json) throws IOException { ObjectMapper m = new ObjectMapper(); JsonNode root = m.readTree(json); if (root == null) { return error("Could not parse json to evaluate"); } if (!root.has("expression")) { return error("Required field missing: expression"); } String expression = root.get("expression").asText(); try { MapContext context = new MapContext(); if (root.has("arguments")) { JsonNode arguments = root.get("arguments"); if (!arguments.isArray()) { return error("Invalid JSON format: arguments must be an array"); } for (JsonNode argument : arguments) { if (!argument.isObject()) { return error("Invalid JSON format: argument must be an object"); } if (!argument.has("name")) { return error("Invalid JSON format: argument must have a name"); } if (!argument.has("type")) { return error("Invalid JSON format: argument must have a type"); } if (!argument.has("value")) { return error("Invalid JSON format: argument must have a value"); } String name = argument.get("name").asText(); String type = argument.get("type").asText(); Value value; if ("string".equalsIgnoreCase(type)) { value = new StringValue(argument.get("value").asText()); } else if ("double".equalsIgnoreCase(type)) { value = new DoubleValue(argument.get("value").asDouble()); } else if ("boolean".equalsIgnoreCase(type)) { value = new BooleanValue(argument.get("value").asBoolean()); } else if (type.toLowerCase().startsWith("tensor(")) { value = new TensorValue(Tensor.from(type + ":" + argument.get("value").asText())); } else { return error("Unknown argument type: " + type); } context.put(name, value); } } Value value = evaluate(expression, context); String valueType = valueType(value); if (valueType.equals("unknown")) { return error("Evaluation of ranking expression returned unknown value type"); } return String.join("\n", "{", " \"type\": \"" + valueType + "\",", " \"value\": " + valueValue(value), "}"); } catch (ParseException e) { return error("Could not parse expression: " + expression); } catch (IllegalArgumentException e) { return error(e.getMessage()); } } }
ExpressionEvaluator { public static String evaluate(String json) throws IOException { ObjectMapper m = new ObjectMapper(); JsonNode root = m.readTree(json); if (root == null) { return error("Could not parse json to evaluate"); } if (!root.has("expression")) { return error("Required field missing: expression"); } String expression = root.get("expression").asText(); try { MapContext context = new MapContext(); if (root.has("arguments")) { JsonNode arguments = root.get("arguments"); if (!arguments.isArray()) { return error("Invalid JSON format: arguments must be an array"); } for (JsonNode argument : arguments) { if (!argument.isObject()) { return error("Invalid JSON format: argument must be an object"); } if (!argument.has("name")) { return error("Invalid JSON format: argument must have a name"); } if (!argument.has("type")) { return error("Invalid JSON format: argument must have a type"); } if (!argument.has("value")) { return error("Invalid JSON format: argument must have a value"); } String name = argument.get("name").asText(); String type = argument.get("type").asText(); Value value; if ("string".equalsIgnoreCase(type)) { value = new StringValue(argument.get("value").asText()); } else if ("double".equalsIgnoreCase(type)) { value = new DoubleValue(argument.get("value").asDouble()); } else if ("boolean".equalsIgnoreCase(type)) { value = new BooleanValue(argument.get("value").asBoolean()); } else if (type.toLowerCase().startsWith("tensor(")) { value = new TensorValue(Tensor.from(type + ":" + argument.get("value").asText())); } else { return error("Unknown argument type: " + type); } context.put(name, value); } } Value value = evaluate(expression, context); String valueType = valueType(value); if (valueType.equals("unknown")) { return error("Evaluation of ranking expression returned unknown value type"); } return String.join("\n", "{", " \"type\": \"" + valueType + "\",", " \"value\": " + valueValue(value), "}"); } catch (ParseException e) { return error("Could not parse expression: " + expression); } catch (IllegalArgumentException e) { return error(e.getMessage()); } }  }
ExpressionEvaluator { public static String evaluate(String json) throws IOException { ObjectMapper m = new ObjectMapper(); JsonNode root = m.readTree(json); if (root == null) { return error("Could not parse json to evaluate"); } if (!root.has("expression")) { return error("Required field missing: expression"); } String expression = root.get("expression").asText(); try { MapContext context = new MapContext(); if (root.has("arguments")) { JsonNode arguments = root.get("arguments"); if (!arguments.isArray()) { return error("Invalid JSON format: arguments must be an array"); } for (JsonNode argument : arguments) { if (!argument.isObject()) { return error("Invalid JSON format: argument must be an object"); } if (!argument.has("name")) { return error("Invalid JSON format: argument must have a name"); } if (!argument.has("type")) { return error("Invalid JSON format: argument must have a type"); } if (!argument.has("value")) { return error("Invalid JSON format: argument must have a value"); } String name = argument.get("name").asText(); String type = argument.get("type").asText(); Value value; if ("string".equalsIgnoreCase(type)) { value = new StringValue(argument.get("value").asText()); } else if ("double".equalsIgnoreCase(type)) { value = new DoubleValue(argument.get("value").asDouble()); } else if ("boolean".equalsIgnoreCase(type)) { value = new BooleanValue(argument.get("value").asBoolean()); } else if (type.toLowerCase().startsWith("tensor(")) { value = new TensorValue(Tensor.from(type + ":" + argument.get("value").asText())); } else { return error("Unknown argument type: " + type); } context.put(name, value); } } Value value = evaluate(expression, context); String valueType = valueType(value); if (valueType.equals("unknown")) { return error("Evaluation of ranking expression returned unknown value type"); } return String.join("\n", "{", " \"type\": \"" + valueType + "\",", " \"value\": " + valueValue(value), "}"); } catch (ParseException e) { return error("Could not parse expression: " + expression); } catch (IllegalArgumentException e) { return error(e.getMessage()); } }  static String evaluate(String json); static Value evaluate(String expressionString, Context context); }
ExpressionEvaluator { public static String evaluate(String json) throws IOException { ObjectMapper m = new ObjectMapper(); JsonNode root = m.readTree(json); if (root == null) { return error("Could not parse json to evaluate"); } if (!root.has("expression")) { return error("Required field missing: expression"); } String expression = root.get("expression").asText(); try { MapContext context = new MapContext(); if (root.has("arguments")) { JsonNode arguments = root.get("arguments"); if (!arguments.isArray()) { return error("Invalid JSON format: arguments must be an array"); } for (JsonNode argument : arguments) { if (!argument.isObject()) { return error("Invalid JSON format: argument must be an object"); } if (!argument.has("name")) { return error("Invalid JSON format: argument must have a name"); } if (!argument.has("type")) { return error("Invalid JSON format: argument must have a type"); } if (!argument.has("value")) { return error("Invalid JSON format: argument must have a value"); } String name = argument.get("name").asText(); String type = argument.get("type").asText(); Value value; if ("string".equalsIgnoreCase(type)) { value = new StringValue(argument.get("value").asText()); } else if ("double".equalsIgnoreCase(type)) { value = new DoubleValue(argument.get("value").asDouble()); } else if ("boolean".equalsIgnoreCase(type)) { value = new BooleanValue(argument.get("value").asBoolean()); } else if (type.toLowerCase().startsWith("tensor(")) { value = new TensorValue(Tensor.from(type + ":" + argument.get("value").asText())); } else { return error("Unknown argument type: " + type); } context.put(name, value); } } Value value = evaluate(expression, context); String valueType = valueType(value); if (valueType.equals("unknown")) { return error("Evaluation of ranking expression returned unknown value type"); } return String.join("\n", "{", " \"type\": \"" + valueType + "\",", " \"value\": " + valueValue(value), "}"); } catch (ParseException e) { return error("Could not parse expression: " + expression); } catch (IllegalArgumentException e) { return error(e.getMessage()); } }  static String evaluate(String json); static Value evaluate(String expressionString, Context context);  }