Search in sources :

Example 6 with CollectionValue

use of org.kie.kogito.tracing.typedvalue.CollectionValue 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 7 with CollectionValue

use of org.kie.kogito.tracing.typedvalue.CollectionValue 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

Test (org.junit.jupiter.api.Test)7 CollectionValue (org.kie.kogito.tracing.typedvalue.CollectionValue)7 UnitValue (org.kie.kogito.tracing.typedvalue.UnitValue)7 CounterfactualSearchDomainUnitValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue)6 NamedTypedValue (org.kie.kogito.explainability.api.NamedTypedValue)6 StructureValue (org.kie.kogito.tracing.typedvalue.StructureValue)5 List (java.util.List)4 IntNode (com.fasterxml.jackson.databind.node.IntNode)3 TextNode (com.fasterxml.jackson.databind.node.TextNode)3 ArrayList (java.util.ArrayList)3 Collections.emptyList (java.util.Collections.emptyList)3 Feature (org.kie.kogito.explainability.model.Feature)3 TypedValue (org.kie.kogito.tracing.typedvalue.TypedValue)3 DoubleNode (com.fasterxml.jackson.databind.node.DoubleNode)2 CounterfactualExplainabilityRequest (org.kie.kogito.explainability.api.CounterfactualExplainabilityRequest)2 CounterfactualSearchDomainCollectionValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainCollectionValue)2 Output (org.kie.kogito.explainability.model.Output)2 Collections (java.util.Collections)1 Map (java.util.Map)1 Optional (java.util.Optional)1