use of org.kie.pmml.api.models.Interval in project drools by kiegroup.
the class KiePMMLModelFactoryUtils method getObjectCreationExprFromInterval.
static ObjectCreationExpr getObjectCreationExprFromInterval(Interval source) {
ObjectCreationExpr toReturn = new ObjectCreationExpr();
toReturn.setType(Interval.class.getCanonicalName());
NodeList<Expression> arguments = new NodeList<>();
if (source.getLeftMargin() != null) {
arguments.add(new NameExpr(source.getLeftMargin().toString()));
} else {
arguments.add(new NullLiteralExpr());
}
if (source.getRightMargin() != null) {
arguments.add(new NameExpr(source.getRightMargin().toString()));
} else {
arguments.add(new NullLiteralExpr());
}
toReturn.setArguments(arguments);
return toReturn;
}
use of org.kie.pmml.api.models.Interval in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method createIntervalsExpression.
@Test
public void createIntervalsExpression() {
List<Interval> intervals = IntStream.range(0, 3).mapToObj(i -> {
int leftMargin = new Random().nextInt(40);
int rightMargin = leftMargin + 13;
return new Interval(leftMargin, rightMargin);
}).collect(Collectors.toList());
Expression retrieved = KiePMMLModelFactoryUtils.createIntervalsExpression(intervals);
assertNotNull(retrieved);
assertTrue(retrieved instanceof MethodCallExpr);
MethodCallExpr mtdExp = (MethodCallExpr) retrieved;
String expected = "java.util.Arrays";
assertEquals(expected, mtdExp.getScope().get().asNameExpr().toString());
expected = "asList";
assertEquals(expected, mtdExp.getName().asString());
NodeList<Expression> arguments = mtdExp.getArguments();
assertEquals(intervals.size(), arguments.size());
arguments.forEach(argument -> {
assertTrue(argument instanceof ObjectCreationExpr);
ObjectCreationExpr objCrt = (ObjectCreationExpr) argument;
assertEquals(Interval.class.getCanonicalName(), objCrt.getType().asString());
Optional<Interval> intervalOpt = intervals.stream().filter(interval -> String.valueOf(interval.getLeftMargin()).equals(objCrt.getArgument(0).asNameExpr().toString()) && String.valueOf(interval.getRightMargin()).equals(objCrt.getArgument(1).asNameExpr().toString())).findFirst();
assertTrue(intervalOpt.isPresent());
});
}
use of org.kie.pmml.api.models.Interval in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method getObjectCreationExprFromInterval.
@Test
public void getObjectCreationExprFromInterval() {
Interval interval = new Interval(null, -14);
ObjectCreationExpr retrieved = KiePMMLModelFactoryUtils.getObjectCreationExprFromInterval(interval);
assertNotNull(retrieved);
assertEquals(Interval.class.getCanonicalName(), retrieved.getType().asString());
NodeList<Expression> arguments = retrieved.getArguments();
assertEquals(2, arguments.size());
assertTrue(arguments.get(0) instanceof NullLiteralExpr);
assertEquals(String.valueOf(interval.getRightMargin()), arguments.get(1).asNameExpr().toString());
interval = new Interval(-13, 10);
retrieved = KiePMMLModelFactoryUtils.getObjectCreationExprFromInterval(interval);
assertNotNull(retrieved);
assertEquals(Interval.class.getCanonicalName(), retrieved.getType().asString());
arguments = retrieved.getArguments();
assertEquals(2, arguments.size());
assertEquals(String.valueOf(interval.getLeftMargin()), arguments.get(0).asNameExpr().toString());
assertEquals(String.valueOf(interval.getRightMargin()), arguments.get(1).asNameExpr().toString());
interval = new Interval(-13, null);
retrieved = KiePMMLModelFactoryUtils.getObjectCreationExprFromInterval(interval);
assertNotNull(retrieved);
assertEquals(Interval.class.getCanonicalName(), retrieved.getType().asString());
arguments = retrieved.getArguments();
assertEquals(2, arguments.size());
assertEquals(String.valueOf(interval.getLeftMargin()), arguments.get(0).asNameExpr().toString());
assertTrue(arguments.get(1) instanceof NullLiteralExpr);
}
use of org.kie.pmml.api.models.Interval in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method commonVerifyMiningFieldsObjectCreation.
private void commonVerifyMiningFieldsObjectCreation(List<Expression> toVerify, List<MiningField> miningFields) {
toVerify.forEach(expression -> {
assertTrue(expression instanceof ObjectCreationExpr);
ObjectCreationExpr objCrt = (ObjectCreationExpr) expression;
assertEquals(MiningField.class.getCanonicalName(), objCrt.getType().asString());
Optional<MiningField> miningFieldOpt = miningFields.stream().filter(miningField -> miningField.getName().equals(objCrt.getArgument(0).asStringLiteralExpr().asString())).findFirst();
assertTrue(miningFieldOpt.isPresent());
MiningField miningField = miningFieldOpt.get();
assertEquals(MiningField.class.getCanonicalName(), objCrt.getType().asString());
String expected = miningField.getUsageType() != null ? FIELD_USAGE_TYPE.class.getCanonicalName() + "." + miningField.getUsageType() : "null";
assertEquals(expected, objCrt.getArgument(1).toString());
expected = miningField.getOpType() != null ? OP_TYPE.class.getCanonicalName() + "." + miningField.getOpType() : "null";
assertEquals(expected, objCrt.getArgument(2).toString());
expected = miningField.getDataType() != null ? DATA_TYPE.class.getCanonicalName() + "." + miningField.getDataType() : "null";
assertEquals(expected, objCrt.getArgument(3).toString());
expected = miningField.getMissingValueTreatmentMethod() != null ? MISSING_VALUE_TREATMENT_METHOD.class.getCanonicalName() + "." + miningField.getMissingValueTreatmentMethod() : "null";
assertEquals(expected, objCrt.getArgument(4).toString());
expected = miningField.getInvalidValueTreatmentMethod() != null ? INVALID_VALUE_TREATMENT_METHOD.class.getCanonicalName() + "." + miningField.getInvalidValueTreatmentMethod() : "null";
assertEquals(expected, objCrt.getArgument(5).toString());
expected = miningField.getMissingValueReplacement() != null ? miningField.getMissingValueReplacement() : "null";
assertEquals(expected, objCrt.getArgument(6).asStringLiteralExpr().asString());
expected = miningField.getInvalidValueReplacement() != null ? miningField.getInvalidValueReplacement() : "null";
assertEquals(expected, objCrt.getArgument(7).asStringLiteralExpr().asString());
MethodCallExpr allowedValuesMethod = objCrt.getArgument(8).asMethodCallExpr();
IntStream.range(0, 3).forEach(i -> {
String exp = miningField.getAllowedValues().get(i);
assertEquals(exp, allowedValuesMethod.getArgument(i).asStringLiteralExpr().asString());
});
MethodCallExpr intervalsMethod = objCrt.getArgument(9).asMethodCallExpr();
IntStream.range(0, 3).forEach(i -> {
Interval interval = miningField.getIntervals().get(i);
ObjectCreationExpr objectCreationExpr = intervalsMethod.getArgument(i).asObjectCreationExpr();
String exp = interval.getLeftMargin().toString();
assertEquals(exp, objectCreationExpr.getArgument(0).asNameExpr().toString());
exp = interval.getRightMargin().toString();
assertEquals(exp, objectCreationExpr.getArgument(1).asNameExpr().toString());
});
});
}
use of org.kie.pmml.api.models.Interval in project drools by kiegroup.
the class BooleanFunctionsTest method getIsValidValueWrongSizeInput.
@Test(expected = IllegalArgumentException.class)
public void getIsValidValueWrongSizeInput() {
final Object[] input = { 34, 34 };
MiningField referredByFieldRef = getReferredByFieldRef(null, null, Arrays.asList(new Interval(20, 29), new Interval(30, 40), new Interval(41, 50)));
BooleanFunctions.IS_VALID.getValue(input, referredByFieldRef);
}
Aggregations