use of org.kie.kogito.tracing.typedvalue.StructureValue in project kogito-apps by kiegroup.
the class CounterfactualExplainerServiceHandlerTest method testGetPredictionWithStructuredOutputModel.
@Test
public void testGetPredictionWithStructuredOutputModel() {
CounterfactualExplainabilityRequest request = new CounterfactualExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, COUNTERFACTUAL_ID, Collections.emptyList(), List.of(new NamedTypedValue("input1", new StructureValue("number", Map.of("input2b", new UnitValue("number", new IntNode(55)))))), Collections.emptyList(), MAX_RUNNING_TIME_SECONDS);
assertThrows(IllegalArgumentException.class, () -> handler.getPrediction(request));
}
use of org.kie.kogito.tracing.typedvalue.StructureValue in project kogito-apps by kiegroup.
the class LimeExplainerServiceHandlerTest method testGetPredictionWithNonEmptyDefinition.
@Test
@SuppressWarnings("unchecked")
public void testGetPredictionWithNonEmptyDefinition() {
LIMEExplainabilityRequest request = new LIMEExplainabilityRequest(EXECUTION_ID, SERVICE_URL, MODEL_IDENTIFIER, List.of(new NamedTypedValue("input1", new UnitValue("number", "number", new IntNode(20))), new NamedTypedValue("input2", new StructureValue("number", Map.of("input2b", new UnitValue("number", new IntNode(55))))), new NamedTypedValue("input3", new CollectionValue("number", List.of(new UnitValue("number", new IntNode(100)))))), List.of(new NamedTypedValue("output1", new UnitValue("number", "number", new IntNode(20))), new NamedTypedValue("output2", new StructureValue("number", Map.of("output2b", new UnitValue("number", new IntNode(55))))), new NamedTypedValue("output3", new CollectionValue("number", List.of(new UnitValue("number", new IntNode(100)))))));
Prediction prediction = handler.getPrediction(request);
// Inputs
assertEquals(3, prediction.getInput().getFeatures().size());
Optional<Feature> oInput1 = prediction.getInput().getFeatures().stream().filter(f -> f.getName().equals("input1")).findFirst();
assertTrue(oInput1.isPresent());
Feature input1 = oInput1.get();
assertEquals(Type.NUMBER, input1.getType());
assertEquals(20, input1.getValue().asNumber());
Optional<Feature> oInput2 = prediction.getInput().getFeatures().stream().filter(f -> f.getName().equals("input2")).findFirst();
assertTrue(oInput2.isPresent());
Feature input2 = oInput2.get();
assertEquals(Type.COMPOSITE, input2.getType());
assertTrue(input2.getValue().getUnderlyingObject() instanceof List);
List<Feature> input2Object = (List<Feature>) input2.getValue().getUnderlyingObject();
assertEquals(1, input2Object.size());
Optional<Feature> oInput2Child = input2Object.stream().filter(f -> f.getName().equals("input2b")).findFirst();
assertTrue(oInput2Child.isPresent());
Feature input2Child = oInput2Child.get();
assertEquals(Type.NUMBER, input2Child.getType());
assertEquals(55, input2Child.getValue().asNumber());
Optional<Feature> oInput3 = prediction.getInput().getFeatures().stream().filter(f -> f.getName().equals("input3")).findFirst();
assertTrue(oInput3.isPresent());
Feature input3 = oInput3.get();
assertEquals(Type.COMPOSITE, input3.getType());
assertTrue(input3.getValue().getUnderlyingObject() instanceof List);
List<Feature> input3Object = (List<Feature>) input3.getValue().getUnderlyingObject();
assertEquals(1, input3Object.size());
Feature input3Child = input3Object.get(0);
assertEquals(Type.NUMBER, input3Child.getType());
assertEquals(100, input3Child.getValue().asNumber());
// Outputs
assertEquals(3, prediction.getOutput().getOutputs().size());
Optional<Output> oOutput1 = prediction.getOutput().getOutputs().stream().filter(o -> o.getName().equals("output1")).findFirst();
assertTrue(oOutput1.isPresent());
Output output1 = oOutput1.get();
assertEquals(Type.NUMBER, output1.getType());
assertEquals(20, output1.getValue().asNumber());
Optional<Output> oOutput2 = prediction.getOutput().getOutputs().stream().filter(o -> o.getName().equals("output2")).findFirst();
assertTrue(oOutput2.isPresent());
Output output2 = oOutput2.get();
assertEquals(Type.COMPOSITE, input2.getType());
assertTrue(output2.getValue().getUnderlyingObject() instanceof List);
List<Output> output2Object = (List<Output>) output2.getValue().getUnderlyingObject();
assertEquals(1, output2Object.size());
Optional<Output> oOutput2Child = output2Object.stream().filter(f -> f.getName().equals("output2b")).findFirst();
assertTrue(oOutput2Child.isPresent());
Output output2Child = oOutput2Child.get();
assertEquals(Type.NUMBER, output2Child.getType());
assertEquals(55, output2Child.getValue().asNumber());
Optional<Output> oOutput3 = prediction.getOutput().getOutputs().stream().filter(o -> o.getName().equals("output3")).findFirst();
assertTrue(oOutput3.isPresent());
Output output3 = oOutput3.get();
assertEquals(Type.COMPOSITE, output3.getType());
assertTrue(output3.getValue().getUnderlyingObject() instanceof List);
List<Output> output3Object = (List<Output>) output3.getValue().getUnderlyingObject();
assertEquals(1, output3Object.size());
Output output3Child = output3Object.get(0);
assertEquals(Type.NUMBER, output3Child.getType());
assertEquals(100, output3Child.getValue().asNumber());
}
use of org.kie.kogito.tracing.typedvalue.StructureValue 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.StructureValue 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