Add field name aware parsing for CliParameters

parent dbf5b788
Pipeline #109076 canceled with stages
package de.monticore.lang.monticar.generator.middleware.cli; package de.monticore.lang.monticar.generator.middleware.cli;
import alice.tuprolog.Int;
import com.google.gson.*; import com.google.gson.*;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.*;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.*; import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.*;
import de.se_rwth.commons.logging.Log; import de.se_rwth.commons.logging.Log;
...@@ -11,6 +9,7 @@ import java.io.FileNotFoundException; ...@@ -11,6 +9,7 @@ import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public class CliParametersLoader { public class CliParametersLoader {
...@@ -20,41 +19,51 @@ public class CliParametersLoader { ...@@ -20,41 +19,51 @@ public class CliParametersLoader {
public static CliParameters loadCliParameters(String filePath) throws FileNotFoundException { public static CliParameters loadCliParameters(String filePath) throws FileNotFoundException {
JsonReader jsonReader = new JsonReader(new FileReader(filePath)); JsonReader jsonReader = new JsonReader(new FileReader(filePath));
JsonDeserializer<DynamicParameter> deserializer = new JsonDeserializer<DynamicParameter>() { JsonDeserializer<DynamicParameter> deserializer = (jsonElement, type, jsonDeserializationContext) -> {
@Override if(jsonElement.isJsonObject()){
public DynamicParameter deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonObject jsonObj = (JsonObject) jsonElement;
if(jsonElement.isJsonObject()){ Double min = jsonObj.has("min") ? jsonObj.get("min").getAsDouble() : null;
JsonObject jsonObj = (JsonObject) jsonElement; Double max = jsonObj.has("max") ? jsonObj.get("max").getAsDouble() : null;
Double min = jsonObj.has("min") ? jsonObj.get("min").getAsDouble() : null; Double step = jsonObj.has("step") ? jsonObj.get("step").getAsDouble() : null;
Double max = jsonObj.has("max") ? jsonObj.get("max").getAsDouble() : null; Integer count = jsonObj.has("count") ? jsonObj.get("count").getAsInt() : null;
Double step = jsonObj.has("step") ? jsonObj.get("step").getAsDouble() : null; return new GeneratorParamter(min,max,step,count);
Integer count = jsonObj.has("count") ? jsonObj.get("count").getAsInt() : null; }else if(jsonElement.isJsonArray()) {
return new GeneratorParamter(min,max,step,count); JsonArray jArray = (JsonArray) jsonElement;
}else if(jsonElement.isJsonArray()) { List<Double> values = new ArrayList<>();
JsonArray jArray = (JsonArray) jsonElement; for(int i = 0; i < jArray.size(); i++){
List<Double> values = new ArrayList<>(); values.add(jArray.get(i).getAsDouble());
for(int i = 0; i < jArray.size(); i++){
values.add(jArray.get(i).getAsDouble());
}
return new ListParameter(values);
}else{
return new ListParameter(jsonElement.getAsDouble());
} }
return new ListParameter(values);
}else{
return new ListParameter(jsonElement.getAsDouble());
} }
}; };
Gson gson = new GsonBuilder() Gson delegateGson = new GsonBuilder()
.registerTypeAdapter(DynamicAlgorithmCliParameters.class, new AlgorithmParametersInterfaceAdapter()) .registerTypeAdapter(DynamicAlgorithmCliParameters.class, new AlgorithmParametersInterfaceAdapter())
.registerTypeAdapter(DynamicParameter.class, deserializer) .registerTypeAdapter(DynamicParameter.class, deserializer)
.create(); .create();
JsonDeserializer<DynamicSpectralClusteringCliParameters> desCliParameters = new StrictJsonDeserializer<>(Arrays.asList("modelsDir","outputDir","rootModel","generators","emadlBackend","writeTagFile","clusteringParameters"), delegateGson);
JsonDeserializer<ClusteringParameters> desClustering = new StrictJsonDeserializer<>(Arrays.asList("numberOfClusters","flatten","flattenLevel","chooseBy","algorithmParameters"), delegateGson);
JsonDeserializer<DynamicSpectralClusteringCliParameters> desSpectral = new StrictJsonDeserializer<>(Arrays.asList("numberOfClusters","l","sigma"), delegateGson);
JsonDeserializer<DynamicSpectralClusteringCliParameters> desMarkov = new StrictJsonDeserializer<>(Arrays.asList("max_residual","gamma_exp","loop_gain","zero_max"), delegateGson);
JsonDeserializer<DynamicSpectralClusteringCliParameters> desDBScan = new StrictJsonDeserializer<>(Arrays.asList("min_pts","radius"), delegateGson);
Gson gson = new GsonBuilder()
.registerTypeAdapter(DynamicSpectralClusteringCliParameters.class, desSpectral)
.registerTypeAdapter(DynamicMarkovCliParameters.class, desMarkov)
.registerTypeAdapter(DynamicDBScanCliParameters.class, desDBScan)
.registerTypeAdapter(CliParameters.class, desCliParameters)
.registerTypeAdapter(ClusteringParameters.class, desClustering)
.create();
return gson.fromJson(jsonReader, CliParameters.class); return gson.fromJson(jsonReader, CliParameters.class);
} }
static final class AlgorithmParametersInterfaceAdapter implements JsonSerializer<DynamicAlgorithmCliParameters>, JsonDeserializer<DynamicAlgorithmCliParameters> { static final class AlgorithmParametersInterfaceAdapter implements JsonDeserializer<DynamicAlgorithmCliParameters> {
public JsonElement serialize(DynamicAlgorithmCliParameters object, Type interfaceType, JsonSerializationContext context) {
return context.serialize(object);
}
public DynamicAlgorithmCliParameters deserialize(JsonElement elem, Type interfaceType, JsonDeserializationContext context) throws JsonParseException { public DynamicAlgorithmCliParameters deserialize(JsonElement elem, Type interfaceType, JsonDeserializationContext context) throws JsonParseException {
final Type actualType = typeForName(((JsonObject)elem).get("name")); final Type actualType = typeForName(((JsonObject)elem).get("name"));
......
package de.monticore.lang.monticar.generator.middleware.cli;
import com.google.gson.*;
import de.se_rwth.commons.logging.Log;
import java.lang.reflect.Type;
import java.util.List;
public class StrictJsonDeserializer<T> implements JsonDeserializer<T> {
private final List<String> fieldNames;
private final Gson delegate;
public StrictJsonDeserializer(List<String> fieldNames, Gson delegate) {
this.fieldNames = fieldNames;
this.delegate = delegate;
}
@Override
public T deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
if (!jsonElement.isJsonObject()) {
Log.error("Expecting json object!");
} else {
JsonObject jsonObj = jsonElement.getAsJsonObject();
for (String s : jsonObj.keySet()) {
if (!fieldNames.contains(s)) {
Log.error("0x9DB6E: " + s + " is not a valid field name! Valid options: " + fieldNames);
}
}
}
return delegate.fromJson(jsonElement, type);
}
}
...@@ -7,6 +7,7 @@ import de.monticore.lang.monticar.generator.middleware.cli.ResultChoosingStrateg ...@@ -7,6 +7,7 @@ import de.monticore.lang.monticar.generator.middleware.cli.ResultChoosingStrateg
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.*; import de.monticore.lang.monticar.generator.middleware.cli.algorithms.*;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.DynamicAlgorithmCliParameters; import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.DynamicAlgorithmCliParameters;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.DynamicSpectralClusteringCliParameters; import de.monticore.lang.monticar.generator.middleware.cli.algorithms.dynamic.DynamicSpectralClusteringCliParameters;
import de.se_rwth.commons.logging.Log;
import org.junit.Test; import org.junit.Test;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
...@@ -170,5 +171,19 @@ public class ParameterLoadingTest { ...@@ -170,5 +171,19 @@ public class ParameterLoadingTest {
} }
} }
@Test
public void testInvalidFieldName() throws FileNotFoundException {
Log.enableFailQuick(false);
Log.getFindings().clear();
loadCliParameters("invalidFieldName");
assertEquals(1, Log.getErrorCount());
assertTrue(Log.getFindings().get(0).getMsg().contains("0x9DB6E"));
Log.getFindings().clear();
Log.enableFailQuick(true);
}
} }
...@@ -15,6 +15,10 @@ ...@@ -15,6 +15,10 @@
"name":"SpectralClustering", "name":"SpectralClustering",
"numberOfClusters":10 "numberOfClusters":10
}, },
{
"name":"SpectralClustering",
"numberOfClusters":457
},
{ {
"name":"Markov" "name":"Markov"
}, },
......
{
"mmodelsDir": "src/test/resources/pacman/",
"outputDir": "target/cliTest/allAlgos/",
"rootModel": "de.rwth.pacman.heithoff2.controller",
"generators": ["cpp","roscpp"]
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment