Don't skip float constraints for int variables

This is legal AMPL:

    var replicas integer >= 1.0, <= 8;

Therefore, also unify code for generating float and int variables since
it is roughly the same.

Change-Id: I768af08063666e43bf162c9c88e8536c6c6b5f79
This commit is contained in:
Rudi Schlatte 2024-02-23 15:45:53 +01:00
parent 2cdbe451a4
commit 98fd21d563

View File

@ -198,46 +198,29 @@ public class AMPLGenerator {
out.println("# Variables");
for (final JsonNode p : app.getKubevelaVariables().values()) {
ObjectNode param = (ObjectNode) p;
// Even if these variables are sent over as "float", we know they
// have to be treated as integers for kubevela (replicas, memory)
// or SAL (cpu). I.e., paramMeaning overrides paramType.
List<String> integerVariables = List.of("cpu", "memory", "replicas");
List<String> integerMeanings = List.of("cpu", "memory", "replicas");
String paramName = param.get("key").textValue();
String paramPath = param.get("path").textValue();
String paramType = param.get("type").textValue();
String paramMeaning = param.get("meaning").textValue();
// Even if these variables are sent over as "float", we know they
// have to be treated as integers for kubevela (replicas, memory)
// or SAL (cpu). I.e., paramMeaning overrides paramType.
boolean shouldBeInt = paramType.equals("int") || integerMeanings.contains(paramMeaning);
ObjectNode value = (ObjectNode)param.get("value");
if (paramType.equals("int") || integerVariables.contains(paramMeaning)) {
out.format("var %s integer", paramName);
if (value != null) {
String separator = "";
JsonNode lower = value.at("/lower_bound");
JsonNode upper = value.at("/higher_bound");
// TODO: What if the constraints are given as float?
// Round up or down? Emit as floats and hope for the
// best? Leave out illegal constraints?
if (lower.isIntegralNumber()) {
out.format(" >= %s", lower.longValue());
separator = ", ";
}
if (upper.isIntegralNumber()) {
out.format("%s<= %s", separator, upper.longValue());
}
}
out.println(";");
} else if (paramType.equals("float")) {
if (paramType.equals("int") || paramType.equals("float")) {
out.format("var %s", paramName);
if (shouldBeInt) { out.print(" integer"); }
if (value != null) {
String separator = "";
JsonNode lower = value.at("/lower_bound");
JsonNode upper = value.at("/higher_bound");
// `isNumber` because the constraint might be given as integer
if (lower.isNumber()) {
out.format(" >= %s", lower.doubleValue());
separator = ", ";
out.format(" >= %s", lower.numberValue());
separator = ",";
}
if (upper.isNumber()) {
out.format("%s<= %s", separator, upper.doubleValue());
out.format("%s <= %s", separator, upper.numberValue());
}
}
out.println(";");