Search in sources :

Example 6 with TypedValue

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());
}
Also used : ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue) Test(org.junit.jupiter.api.Test)

Example 7 with TypedValue

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());
}
Also used : TraceEventType(org.kie.kogito.tracing.event.trace.TraceEventType) TraceExecutionStepType(org.kie.kogito.tracing.event.trace.TraceExecutionStepType) Arrays(java.util.Arrays) DMNType(org.kie.dmn.api.core.DMNType) CloudEvent(io.cloudevents.CloudEvent) AFTER_EVALUATE_DECISION_SERVICE(org.kie.kogito.tracing.decision.event.evaluate.EvaluateEventType.AFTER_EVALUATE_DECISION_SERVICE) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Deque(java.util.Deque) Function(java.util.function.Function) TraceExecutionStep(org.kie.kogito.tracing.event.trace.TraceExecutionStep) ArrayList(java.util.ArrayList) DecisionServiceNodeImpl(org.kie.dmn.core.ast.DecisionServiceNodeImpl) DMNModel(org.kie.dmn.api.core.DMNModel) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNBaseNode(org.kie.dmn.core.ast.DMNBaseNode) Map(java.util.Map) EvaluateDecisionResult(org.kie.kogito.tracing.decision.event.evaluate.EvaluateDecisionResult) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) InternalMessageType(org.kie.kogito.tracing.decision.message.InternalMessageType) Message(org.kie.kogito.tracing.event.message.Message) EvaluateEventType(org.kie.kogito.tracing.decision.event.evaluate.EvaluateEventType) TraceEvent(org.kie.kogito.tracing.event.trace.TraceEvent) ConfigBean(org.kie.kogito.conf.ConfigBean) Logger(org.slf4j.Logger) DMNNode(org.kie.dmn.api.core.ast.DMNNode) EvaluateEvent(org.kie.kogito.tracing.decision.event.evaluate.EvaluateEvent) TraceInputValue(org.kie.kogito.tracing.event.trace.TraceInputValue) TraceOutputValue(org.kie.kogito.tracing.event.trace.TraceOutputValue) Collection(java.util.Collection) EventUtils(org.kie.kogito.tracing.decision.event.EventUtils) Collectors(java.util.stream.Collectors) Pair(org.kie.dmn.feel.util.Pair) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue) Objects(java.util.Objects) List(java.util.List) TraceHeader(org.kie.kogito.tracing.event.trace.TraceHeader) Stream(java.util.stream.Stream) CloudEventUtils(org.kie.kogito.event.cloudevents.utils.CloudEventUtils) BEFORE_EVALUATE_DECISION_SERVICE(org.kie.kogito.tracing.decision.event.evaluate.EvaluateEventType.BEFORE_EVALUATE_DECISION_SERVICE) Optional(java.util.Optional) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) TraceOutputValue(org.kie.kogito.tracing.event.trace.TraceOutputValue) DMNBaseNode(org.kie.dmn.core.ast.DMNBaseNode) HashMap(java.util.HashMap) DMNNode(org.kie.dmn.api.core.ast.DMNNode) HashMap(java.util.HashMap) Map(java.util.Map) DMNType(org.kie.dmn.api.core.DMNType) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue)

Example 8 with TypedValue

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);
}
Also used : WebClientOptions(io.vertx.ext.web.client.WebClientOptions) Feature(org.kie.kogito.explainability.model.Feature) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Value(org.kie.kogito.explainability.model.Value) Map(java.util.Map) HasNameValue(org.kie.kogito.explainability.api.HasNameValue) JsonObject(io.vertx.core.json.JsonObject) URI(java.net.URI) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) PredictInput(org.kie.kogito.explainability.models.PredictInput) Logger(org.slf4j.Logger) Executor(java.util.concurrent.Executor) Collection(java.util.Collection) ThreadContext(org.eclipse.microprofile.context.ThreadContext) ConversionUtils.toOutputList(org.kie.kogito.explainability.ConversionUtils.toOutputList) Collectors(java.util.stream.Collectors) Type(org.kie.kogito.explainability.model.Type) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue) Objects(java.util.Objects) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Output(org.kie.kogito.explainability.model.Output) Vertx(io.vertx.mutiny.core.Vertx) WebClient(io.vertx.mutiny.ext.web.client.WebClient) ModelIdentifier(org.kie.kogito.explainability.api.ModelIdentifier) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) Value(org.kie.kogito.explainability.model.Value) HasNameValue(org.kie.kogito.explainability.api.HasNameValue) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue)

Example 9 with TypedValue

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());
    }
}
Also used : CollectionValue(org.kie.kogito.tracing.typedvalue.CollectionValue) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) StructureValue(org.kie.kogito.tracing.typedvalue.StructureValue) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Feature(org.kie.kogito.explainability.model.Feature) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue) Test(org.junit.jupiter.api.Test)

Example 10 with TypedValue

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"));
            }
        }
    }
}
Also used : CollectionValue(org.kie.kogito.tracing.typedvalue.CollectionValue) ArrayList(java.util.ArrayList) UnitValue(org.kie.kogito.tracing.typedvalue.UnitValue) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) StructureValue(org.kie.kogito.tracing.typedvalue.StructureValue) TextNode(com.fasterxml.jackson.databind.node.TextNode) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Feature(org.kie.kogito.explainability.model.Feature) NamedTypedValue(org.kie.kogito.explainability.api.NamedTypedValue) TypedValue(org.kie.kogito.tracing.typedvalue.TypedValue) Test(org.junit.jupiter.api.Test)

Aggregations

TypedValue (org.kie.kogito.tracing.typedvalue.TypedValue)15 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)9 ArrayList (java.util.ArrayList)8 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)8 List (java.util.List)7 Test (org.junit.jupiter.api.Test)6 CollectionValue (org.kie.kogito.tracing.typedvalue.CollectionValue)6 TextNode (com.fasterxml.jackson.databind.node.TextNode)4 Map (java.util.Map)4 CounterfactualSearchDomainValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainValue)4 HasNameValue (org.kie.kogito.explainability.api.HasNameValue)4 Feature (org.kie.kogito.explainability.model.Feature)4 Type (org.kie.kogito.explainability.model.Type)4 Value (org.kie.kogito.explainability.model.Value)4 DoubleNode (com.fasterxml.jackson.databind.node.DoubleNode)3 Collection (java.util.Collection)3 Collections.emptyList (java.util.Collections.emptyList)3 Objects (java.util.Objects)3 Collectors (java.util.stream.Collectors)3 Output (org.kie.kogito.explainability.model.Output)3