Search in sources :

Example 1 with CategoricalFeatureDomain

use of org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain in project kogito-apps by kiegroup.

the class CounterfactualEntityFactory method from.

public static CounterfactualEntity from(Feature feature, FeatureDistribution featureDistribution) {
    CounterfactualEntity entity = null;
    validateFeature(feature);
    final Type type = feature.getType();
    final FeatureDomain featureDomain = feature.getDomain();
    final boolean isConstrained = feature.isConstrained();
    final Object valueObject = feature.getValue().getUnderlyingObject();
    if (type == Type.NUMBER) {
        if (valueObject instanceof Double) {
            if (isConstrained) {
                entity = FixedDoubleEntity.from(feature);
            } else {
                entity = DoubleEntity.from(feature, featureDomain.getLowerBound(), featureDomain.getUpperBound(), featureDistribution, isConstrained);
            }
        } else if (valueObject instanceof Long) {
            if (isConstrained) {
                entity = FixedLongEntity.from(feature);
            } else {
                entity = LongEntity.from(feature, featureDomain.getLowerBound().intValue(), featureDomain.getUpperBound().intValue(), featureDistribution, isConstrained);
            }
        } else if (valueObject instanceof Integer) {
            if (isConstrained) {
                entity = FixedIntegerEntity.from(feature);
            } else {
                entity = IntegerEntity.from(feature, featureDomain.getLowerBound().intValue(), featureDomain.getUpperBound().intValue(), featureDistribution, isConstrained);
            }
        }
    } else if (feature.getType() == Type.BOOLEAN) {
        if (isConstrained) {
            entity = FixedBooleanEntity.from(feature);
        } else {
            entity = BooleanEntity.from(feature, isConstrained);
        }
    } else if (feature.getType() == Type.TEXT) {
        if (isConstrained) {
            entity = FixedTextEntity.from(feature);
        } else {
            throw new IllegalArgumentException("Unsupported feature type: " + feature.getType());
        }
    } else if (feature.getType() == Type.BINARY) {
        if (isConstrained) {
            entity = FixedBinaryEntity.from(feature);
        } else {
            entity = BinaryEntity.from(feature, ((BinaryFeatureDomain) featureDomain).getCategories(), isConstrained);
        }
    } else if (feature.getType() == Type.URI) {
        if (isConstrained) {
            entity = FixedURIEntity.from(feature);
        } else {
            entity = URIEntity.from(feature, ((URIFeatureDomain) featureDomain).getCategories(), isConstrained);
        }
    } else if (feature.getType() == Type.TIME) {
        if (isConstrained) {
            entity = FixedTimeEntity.from(feature);
        } else {
            final LocalTime lowerBound = LocalTime.MIN.plusSeconds(featureDomain.getLowerBound().longValue());
            final LocalTime upperBound = LocalTime.MIN.plusSeconds(featureDomain.getUpperBound().longValue());
            entity = TimeEntity.from(feature, lowerBound, upperBound, isConstrained);
        }
    } else if (feature.getType() == Type.DURATION) {
        if (isConstrained) {
            entity = FixedDurationEntity.from(feature);
        } else {
            DurationFeatureDomain domain = (DurationFeatureDomain) featureDomain;
            entity = DurationEntity.from(feature, Duration.of(domain.getLowerBound().longValue(), domain.getUnit()), Duration.of(domain.getUpperBound().longValue(), domain.getUnit()), featureDistribution, isConstrained);
        }
    } else if (feature.getType() == Type.VECTOR) {
        if (isConstrained) {
            entity = FixedVectorEntity.from(feature);
        } else {
            throw new IllegalArgumentException("Unsupported feature type: " + feature.getType());
        }
    } else if (feature.getType() == Type.COMPOSITE) {
        if (isConstrained) {
            entity = FixedCompositeEntity.from(feature);
        } else {
            throw new IllegalArgumentException("Unsupported feature type: " + feature.getType());
        }
    } else if (feature.getType() == Type.CURRENCY) {
        if (isConstrained) {
            entity = FixedCurrencyEntity.from(feature);
        } else {
            entity = CurrencyEntity.from(feature, ((CurrencyFeatureDomain) featureDomain).getCategories(), isConstrained);
        }
    } else if (feature.getType() == Type.CATEGORICAL) {
        if (isConstrained) {
            entity = FixedCategoricalEntity.from(feature);
        } else {
            entity = CategoricalEntity.from(feature, ((CategoricalFeatureDomain) featureDomain).getCategories(), isConstrained);
        }
    } else if (feature.getType() == Type.UNDEFINED) {
        if (isConstrained) {
            entity = FixedObjectEntity.from(feature);
        } else {
            entity = ObjectEntity.from(feature, ((ObjectFeatureDomain) featureDomain).getCategories(), isConstrained);
        }
    } else {
        throw new IllegalArgumentException("Unsupported feature type: " + feature.getType());
    }
    return entity;
}
Also used : LocalTime(java.time.LocalTime) URIFeatureDomain(org.kie.kogito.explainability.model.domain.URIFeatureDomain) DurationFeatureDomain(org.kie.kogito.explainability.model.domain.DurationFeatureDomain) ObjectFeatureDomain(org.kie.kogito.explainability.model.domain.ObjectFeatureDomain) URIFeatureDomain(org.kie.kogito.explainability.model.domain.URIFeatureDomain) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) DurationFeatureDomain(org.kie.kogito.explainability.model.domain.DurationFeatureDomain) BinaryFeatureDomain(org.kie.kogito.explainability.model.domain.BinaryFeatureDomain) CurrencyFeatureDomain(org.kie.kogito.explainability.model.domain.CurrencyFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) Type(org.kie.kogito.explainability.model.Type)

Example 2 with CategoricalFeatureDomain

use of org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain in project kogito-apps by kiegroup.

the class ConversionUtilsTest method testToFeatureDomain_UnitCategoricalString.

@Test
void testToFeatureDomain_UnitCategoricalString() {
    FeatureDomain featureDomain = ConversionUtils.toFeatureDomain(new CounterfactualSearchDomainUnitValue("string", "string", true, new CounterfactualDomainCategorical(List.of(TextNode.valueOf("Black"), TextNode.valueOf("White")))));
    assertTrue(featureDomain instanceof CategoricalFeatureDomain);
    CategoricalFeatureDomain categoricalFeatureDomain = (CategoricalFeatureDomain) featureDomain;
    assertEquals(2, categoricalFeatureDomain.getCategories().size());
    assertTrue(categoricalFeatureDomain.getCategories().containsAll(List.of("White", "Black")));
    assertNull(categoricalFeatureDomain.getLowerBound());
    assertNull(categoricalFeatureDomain.getUpperBound());
}
Also used : EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) CounterfactualSearchDomainUnitValue(org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue) CounterfactualDomainCategorical(org.kie.kogito.explainability.api.CounterfactualDomainCategorical) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) Test(org.junit.jupiter.api.Test)

Aggregations

CategoricalFeatureDomain (org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain)2 FeatureDomain (org.kie.kogito.explainability.model.domain.FeatureDomain)2 LocalTime (java.time.LocalTime)1 Test (org.junit.jupiter.api.Test)1 CounterfactualDomainCategorical (org.kie.kogito.explainability.api.CounterfactualDomainCategorical)1 CounterfactualSearchDomainUnitValue (org.kie.kogito.explainability.api.CounterfactualSearchDomainUnitValue)1 Type (org.kie.kogito.explainability.model.Type)1 BinaryFeatureDomain (org.kie.kogito.explainability.model.domain.BinaryFeatureDomain)1 CurrencyFeatureDomain (org.kie.kogito.explainability.model.domain.CurrencyFeatureDomain)1 DurationFeatureDomain (org.kie.kogito.explainability.model.domain.DurationFeatureDomain)1 EmptyFeatureDomain (org.kie.kogito.explainability.model.domain.EmptyFeatureDomain)1 NumericalFeatureDomain (org.kie.kogito.explainability.model.domain.NumericalFeatureDomain)1 ObjectFeatureDomain (org.kie.kogito.explainability.model.domain.ObjectFeatureDomain)1 URIFeatureDomain (org.kie.kogito.explainability.model.domain.URIFeatureDomain)1