Aufgrund eines Versionsupdates wird GitLab am 17.12. zwischen 9:00 und 9:30 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to a version upgrade, GitLab won't be accessible at 17.12. between 9 and 9:30 a.m.

Add field name aware parsing for CliParameters

parent dbf5b788
Pipeline #109076 canceled with stages
package de.monticore.lang.monticar.generator.middleware.cli;
import alice.tuprolog.Int;
import com.google.gson.*;
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.se_rwth.commons.logging.Log;
......@@ -11,6 +9,7 @@ import java.io.FileNotFoundException;
import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CliParametersLoader {
......@@ -20,9 +19,7 @@ public class CliParametersLoader {
public static CliParameters loadCliParameters(String filePath) throws FileNotFoundException {
JsonReader jsonReader = new JsonReader(new FileReader(filePath));
JsonDeserializer<DynamicParameter> deserializer = new JsonDeserializer<DynamicParameter>() {
@Override
public DynamicParameter deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
JsonDeserializer<DynamicParameter> deserializer = (jsonElement, type, jsonDeserializationContext) -> {
if(jsonElement.isJsonObject()){
JsonObject jsonObj = (JsonObject) jsonElement;
Double min = jsonObj.has("min") ? jsonObj.get("min").getAsDouble() : null;
......@@ -40,21 +37,33 @@ public class CliParametersLoader {
}else{
return new ListParameter(jsonElement.getAsDouble());
}
}
};
Gson gson = new GsonBuilder()
Gson delegateGson = new GsonBuilder()
.registerTypeAdapter(DynamicAlgorithmCliParameters.class, new AlgorithmParametersInterfaceAdapter())
.registerTypeAdapter(DynamicParameter.class, deserializer)
.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);
}
static final class AlgorithmParametersInterfaceAdapter implements JsonSerializer<DynamicAlgorithmCliParameters>, JsonDeserializer<DynamicAlgorithmCliParameters> {
public JsonElement serialize(DynamicAlgorithmCliParameters object, Type interfaceType, JsonSerializationContext context) {
return context.serialize(object);
}
static final class AlgorithmParametersInterfaceAdapter implements JsonDeserializer<DynamicAlgorithmCliParameters> {
public DynamicAlgorithmCliParameters deserialize(JsonElement elem, Type interfaceType, JsonDeserializationContext context) throws JsonParseException {
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
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.DynamicSpectralClusteringCliParameters;
import de.se_rwth.commons.logging.Log;
import org.junit.Test;
import java.io.FileNotFoundException;
......@@ -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 @@
"name":"SpectralClustering",
"numberOfClusters":10
},
{
"name":"SpectralClustering",
"numberOfClusters":457
},
{
"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