use of org.kie.kogito.tracing.typedvalue.TypedValue in project kogito-runtimes by kiegroup.
the class EventUtilsTest method testTypedVariableFromJsonNode.
@Test
void testTypedVariableFromJsonNode() throws JsonProcessingException {
ObjectReader reader = new ObjectMapper().reader();
TypedValue value = EventUtils.typedValueFromJsonNode(null, null);
assertNotNull(value);
assertSame(TypedValue.Kind.UNIT, value.getKind());
assertEquals(BuiltInType.UNKNOWN.getName(), value.getType());
value = EventUtils.typedValueFromJsonNode(reader.readTree("true"), null);
assertNotNull(value);
assertSame(TypedValue.Kind.UNIT, value.getKind());
assertEquals(BuiltInType.BOOLEAN.getName(), value.getType());
value = EventUtils.typedValueFromJsonNode(reader.readTree("12"), null);
assertNotNull(value);
assertSame(TypedValue.Kind.UNIT, value.getKind());
assertEquals(BuiltInType.NUMBER.getName(), value.getType());
value = EventUtils.typedValueFromJsonNode(reader.readTree("\"test\""), null);
assertNotNull(value);
assertSame(TypedValue.Kind.UNIT, value.getKind());
assertEquals(BuiltInType.STRING.getName(), value.getType());
value = EventUtils.typedValueFromJsonNode(reader.readTree("[1,2,3]"), null);
assertNotNull(value);
assertSame(TypedValue.Kind.COLLECTION, value.getKind());
assertEquals(BuiltInType.LIST.getName(), value.getType());
value = EventUtils.typedValueFromJsonNode(reader.readTree("{\"name\": \"John\", \"age\": 45, \"married\": true}"), null);
assertNotNull(value);
assertSame(TypedValue.Kind.STRUCTURE, value.getKind());
assertEquals(BuiltInType.UNKNOWN.getName(), value.getType());
}
use of org.kie.kogito.tracing.typedvalue.TypedValue in project kogito-runtimes by kiegroup.
the class DefaultAggregator method traceOutputFrom.
private static TraceOutputValue traceOutputFrom(EvaluateDecisionResult decisionResult, DMNModel model, Map<String, Object> context) {
DMNType type = Optional.ofNullable(model).map(m -> m.getDecisionById(decisionResult.getDecisionId())).map(DecisionNode::getResultType).orElse(null);
// cast to DMNBaseNode here is required to have access to getDependencies method
Map<String, DMNType> decisionInputTypes = Optional.ofNullable(model).map(m -> m.getDecisionById(decisionResult.getDecisionId())).filter(DMNBaseNode.class::isInstance).map(DMNBaseNode.class::cast).map(DMNBaseNode::getDependencies).map(deps -> deps.values().stream().map(DMNNode::getId).collect(Collectors.toList())).map(ids -> ids.stream().map(id -> typeAndNameOf(id, model)).filter(Objects::nonNull).collect(Collectors.toMap(Pair::getRight, Pair::getLeft))).orElseGet(HashMap::new);
Map<String, TypedValue> decisionInputs = decisionInputTypes.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> EventUtils.typedValueFrom(e.getValue(), context.get(e.getKey()))));
return new TraceOutputValue(decisionResult.getDecisionId(), decisionResult.getDecisionName(), decisionResult.getEvaluationStatus().name(), EventUtils.typedValueFrom(type, decisionResult.getResult()), decisionInputs, decisionResult.getMessages());
}
use of org.kie.kogito.tracing.typedvalue.TypedValue in project kogito-apps by kiegroup.
the class RemotePredictionProvider method toPredictionOutput.
protected PredictionOutput toPredictionOutput(JsonObject mainObj) {
if (mainObj == null || !mainObj.containsKey("result")) {
LOG.error("Malformed json {}", mainObj);
return null;
}
List<Output> resultOutputs = toOutputList(mainObj.getJsonObject("result"));
List<String> resultOutputNames = resultOutputs.stream().map(Output::getName).collect(toList());
Map<String, TypedValue> mappedOutputs = predictionOutputs.stream().collect(Collectors.toMap(HasNameValue::getName, HasNameValue::getValue));
// It's possible that some outputs are missing in the response from the prediction service
// (e.g. when the generated perturbed inputs don't make sense and a decision is skipped).
// The explainer, however, may throw exceptions if it can't find all the inputs that were
// specified in the execution request.
// Here we take the outputs received from the prediction service and we fill (only if needed)
// the missing ones with Output objects containing "null" values of type UNDEFINED, to make
// the explainer happy.
List<Output> outputs = Stream.concat(resultOutputs.stream().filter(output -> mappedOutputs.containsKey(output.getName())), mappedOutputs.keySet().stream().filter(key -> !resultOutputNames.contains(key)).map(key -> new Output(key, Type.UNDEFINED, new Value(null), 1d))).collect(toList());
return new PredictionOutput(outputs);
}
use of org.kie.kogito.tracing.typedvalue.TypedValue in project kogito-apps by kiegroup.
the class ConversionUtilsTest method toFeatureTypedValue.
@Test
void toFeatureTypedValue() {
Feature name = ConversionUtils.toFeature("name", new UnitValue("number", new DoubleNode(10d)));
assertNotNull(name);
assertEquals("name", name.getName());
assertEquals(Type.NUMBER, name.getType());
assertEquals(10d, name.getValue().getUnderlyingObject());
assertTrue(name.isConstrained());
assertTrue(name.getDomain().isEmpty());
Feature name1 = ConversionUtils.toFeature("name1", new StructureValue("complex", singletonMap("key", new UnitValue("string1", new TextNode("stringValue")))));
assertNotNull(name1);
assertTrue(name1.isConstrained());
assertTrue(name1.getDomain().isEmpty());
assertEquals("name1", name1.getName());
assertEquals(Type.COMPOSITE, name1.getType());
assertTrue(name1.getValue().getUnderlyingObject() instanceof List);
@SuppressWarnings("unchecked") List<Feature> features = (List<Feature>) name1.getValue().getUnderlyingObject();
assertEquals(1, features.size());
assertEquals(Type.TEXT, features.get(0).getType());
assertEquals("stringValue", features.get(0).getValue().getUnderlyingObject());
List<TypedValue> values = List.of(new UnitValue("number", new DoubleNode(0d)), new UnitValue("number", new DoubleNode(1d)));
Feature collectionFeature = ConversionUtils.toFeature("name", new CollectionValue("list", values));
assertNotNull(collectionFeature);
assertEquals("name", collectionFeature.getName());
assertEquals(Type.COMPOSITE, collectionFeature.getType());
assertTrue(collectionFeature.getValue().getUnderlyingObject() instanceof List);
@SuppressWarnings("unchecked") List<Feature> objects = (List<Feature>) collectionFeature.getValue().getUnderlyingObject();
assertEquals(2, objects.size());
for (Feature f : objects) {
assertNotNull(f);
assertNotNull(f.getName());
assertNotNull(f.getType());
assertEquals(Type.NUMBER, f.getType());
assertNotNull(f.getValue());
}
}
use of org.kie.kogito.tracing.typedvalue.TypedValue in project kogito-apps by kiegroup.
the class ConversionUtilsTest method testNestedCollection.
@Test
void testNestedCollection() {
Collection<TypedValue> depthTwoOne = new ArrayList<>(2);
depthTwoOne.add(new StructureValue("complex", singletonMap("key", new UnitValue("string1", new TextNode("value one")))));
depthTwoOne.add(new StructureValue("complex", singletonMap("key", new UnitValue("string1", new TextNode("value two")))));
Collection<TypedValue> depthTwoTwo = new ArrayList<>(2);
depthTwoTwo.add(new StructureValue("complex", singletonMap("key", new UnitValue("string1", new TextNode("value three")))));
depthTwoTwo.add(new StructureValue("complex", singletonMap("key", new UnitValue("string1", new TextNode("value four")))));
CollectionValue depthOneLeft = new CollectionValue("list", depthTwoOne);
CollectionValue depthOneRight = new CollectionValue("list", depthTwoTwo);
Collection<TypedValue> depthOne = new ArrayList<>(2);
depthOne.add(depthOneLeft);
depthOne.add(depthOneRight);
CollectionValue value = new CollectionValue("list", depthOne);
Feature collectionFeature = ConversionUtils.toFeature("name", value);
assertNotNull(collectionFeature);
assertEquals("name", collectionFeature.getName());
assertEquals(Type.COMPOSITE, collectionFeature.getType());
assertTrue(collectionFeature.getValue().getUnderlyingObject() instanceof List);
@SuppressWarnings("unchecked") List<Feature> deepFeatures = (List<Feature>) collectionFeature.getValue().getUnderlyingObject();
assertEquals(2, deepFeatures.size());
for (Feature f : deepFeatures) {
assertNotNull(f);
assertNotNull(f.getName());
assertNotNull(f.getType());
assertEquals(Type.COMPOSITE, f.getType());
assertNotNull(f.getValue());
List<Feature> nestedOneValues = (List<Feature>) f.getValue().getUnderlyingObject();
for (Feature nestedOneValue : nestedOneValues) {
assertNotNull(nestedOneValue);
assertNotNull(nestedOneValue.getName());
assertNotNull(nestedOneValue.getType());
assertEquals(Type.COMPOSITE, nestedOneValue.getType());
assertNotNull(nestedOneValue.getValue());
List<Feature> nestedTwoValues = (List<Feature>) nestedOneValue.getValue().getUnderlyingObject();
for (Feature nestedTwoValue : nestedTwoValues) {
assertNotNull(nestedTwoValue);
assertNotNull(nestedTwoValue.getName());
assertNotNull(nestedTwoValue.getType());
assertEquals(Type.TEXT, nestedTwoValue.getType());
assertNotNull(nestedTwoValue.getValue());
assertTrue(nestedTwoValue.getValue().asString().contains("value"));
}
}
}
}
Aggregations