Add classes for cli parameters of each algorithm

parent 3349413f
package de.monticore.lang.monticar.generator.middleware.cli; package de.monticore.lang.monticar.generator.middleware.cli;
import java.util.Optional;
import java.util.Set; import java.util.Set;
public class CliParameters { public class CliParameters {
...@@ -12,17 +13,19 @@ public class CliParameters { ...@@ -12,17 +13,19 @@ public class CliParameters {
private Set<String> generators; private Set<String> generators;
private String emadlBackend; private String emadlBackend;
private Boolean writeTagFile; private Boolean writeTagFile;
private ClustringParameters clustringParameters;
public CliParameters() { public CliParameters() {
} }
public CliParameters(String modelsDir, String outputDir, String rootModel, Set<String> generators, String emadlBackend, Boolean writeTagFile) { public CliParameters(String modelsDir, String outputDir, String rootModel, Set<String> generators, String emadlBackend, Boolean writeTagFile, ClustringParameters clustringParameters) {
this.modelsDir = modelsDir; this.modelsDir = modelsDir;
this.outputDir = outputDir; this.outputDir = outputDir;
this.rootModel = rootModel; this.rootModel = rootModel;
this.generators = generators; this.generators = generators;
this.emadlBackend = emadlBackend == null ? DEFAULT_EMADL_BACKEND : emadlBackend; this.emadlBackend = emadlBackend == null ? DEFAULT_EMADL_BACKEND : emadlBackend;
this.writeTagFile = writeTagFile == null ? DEFAULT_WRITE_TAG_FILE : writeTagFile; this.writeTagFile = writeTagFile == null ? DEFAULT_WRITE_TAG_FILE : writeTagFile;
this.clustringParameters = clustringParameters;
} }
public String getModelsDir() { public String getModelsDir() {
...@@ -48,4 +51,9 @@ public class CliParameters { ...@@ -48,4 +51,9 @@ public class CliParameters {
public boolean getWriteTagFile() { public boolean getWriteTagFile() {
return writeTagFile; return writeTagFile;
} }
public Optional<ClustringParameters> getClustringParameters() {
return Optional.ofNullable(clustringParameters);
}
} }
package de.monticore.lang.monticar.generator.middleware.cli;
import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.lang.reflect.Type;
public class CliParametersLoader {
private CliParametersLoader() {
}
public static CliParameters loadCliParameters(String filePath) throws FileNotFoundException {
JsonReader jsonReader = new JsonReader(new FileReader(filePath));
Gson gson = new GsonBuilder().registerTypeAdapter(AlgorithmCliParameters.class, new AlgorithmParametersInterfaceAdapter()).create();
return gson.fromJson(jsonReader, CliParameters.class);
}
static final class AlgorithmParametersInterfaceAdapter implements JsonSerializer<AlgorithmCliParameters>, JsonDeserializer<AlgorithmCliParameters> {
public JsonElement serialize(AlgorithmCliParameters object, Type interfaceType, JsonSerializationContext context) {
return context.serialize(object);
}
public AlgorithmCliParameters deserialize(JsonElement elem, Type interfaceType, JsonDeserializationContext context) throws JsonParseException {
final Type actualType = typeForName(((JsonObject)elem).get("name"));
return context.deserialize(elem, actualType);
}
private Type typeForName(final JsonElement typeElem){
String algoName = typeElem.getAsString().toLowerCase();
switch (algoName){
case "spectralclustering": return SpectralClusteringCliParameters.class;
case "dbscan": return DBScanCliParameters.class;
case "affinitypropagation": return AffinityPropagationCliParameters.class;
case "markov": return MarkovCliParameters.class;
default: return UnknownAlgorithmCliParameters.class;
}
}
}
}
package de.monticore.lang.monticar.generator.middleware.cli;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.AlgorithmCliParameters;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class ClustringParameters {
private Integer numberOfClusters;
private ResultChoosingStrategy chooseBy = ResultChoosingStrategy.bestWithFittingN;
private List<AlgorithmCliParameters> algorithmParameters = new ArrayList<>();
public ClustringParameters() {
}
public Optional<Integer> getNumberOfClusters() {
return Optional.ofNullable(numberOfClusters);
}
public ResultChoosingStrategy getChooseBy() {
return chooseBy;
}
public List<AlgorithmCliParameters> getAlgorithmParameters() {
return algorithmParameters;
}
}
package de.monticore.lang.monticar.generator.middleware.cli; package de.monticore.lang.monticar.generator.middleware.cli;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol; import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.ros.RosToEmamTagSchema; import de.monticore.lang.embeddedmontiarc.tagging.middleware.ros.RosToEmamTagSchema;
import de.monticore.lang.monticar.emadl.generator.EMADLAbstractSymtab; import de.monticore.lang.monticar.emadl.generator.EMADLAbstractSymtab;
...@@ -16,7 +15,6 @@ import de.monticore.lang.tagging._symboltable.TaggingResolver; ...@@ -16,7 +15,6 @@ import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.se_rwth.commons.logging.Log; import de.se_rwth.commons.logging.Log;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
...@@ -59,8 +57,7 @@ public final class DistributedTargetGeneratorCli { ...@@ -59,8 +57,7 @@ public final class DistributedTargetGeneratorCli {
String filePath = expandHomeDir(args[0]); String filePath = expandHomeDir(args[0]);
try { try {
JsonReader jsonReader = new JsonReader(new FileReader(filePath)); parameters = CliParametersLoader.loadCliParameters(filePath);
parameters = gson.fromJson(jsonReader, CliParameters.class);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Log.error("0x49E6A: Can not find specified config file: " + args[0] + "!"); Log.error("0x49E6A: Can not find specified config file: " + args[0] + "!");
} }
......
package de.monticore.lang.monticar.generator.middleware.cli;
public enum ResultChoosingStrategy {
bestWithFittingN,
bestOverall;
}
package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
public class AffinityPropagationCliParameters extends AlgorithmCliParameters{
public AffinityPropagationCliParameters() {
}
}
package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
public class AlgorithmCliParameters {
public static final String TYPE_SPECTRAL_CLUSTERING = "SpectralClustering";
public static final String TYPE_UNKOWN = "Unkown";
public static final String TYPE_DBSCAN = "DBScan";
public static final String TYPE_MARKOV = "Markov";
protected String name;
public AlgorithmCliParameters() {
}
public String getName() {
return name;
}
}
package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
public class DBScanCliParameters extends AlgorithmCliParameters {
public DBScanCliParameters() {
}
@Override
public String getName() {
return TYPE_DBSCAN;
}
}
package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
public class MarkovCliParameters extends AlgorithmCliParameters {
public MarkovCliParameters() {
}
@Override
public String getName() {
return TYPE_MARKOV;
}
}
package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
public class SpectralClusteringCliParameters extends AlgorithmCliParameters {
public SpectralClusteringCliParameters() {
}
@Override
public String getName() {
return TYPE_SPECTRAL_CLUSTERING;
}
}
package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
public class UnknownAlgorithmCliParameters extends AlgorithmCliParameters {
public UnknownAlgorithmCliParameters() {
}
@Override
public String getName() {
return TYPE_UNKOWN;
}
}
package de.monticore.lang.monticar.generator.middleware;
import de.monticore.lang.monticar.generator.middleware.cli.CliParametersLoader;
import de.monticore.lang.monticar.generator.middleware.cli.CliParameters;
import de.monticore.lang.monticar.generator.middleware.cli.ClustringParameters;
import de.monticore.lang.monticar.generator.middleware.cli.ResultChoosingStrategy;
import de.monticore.lang.monticar.generator.middleware.cli.algorithms.*;
import org.junit.Test;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class ParameterLoadingTest {
private static String BASE_DIR = "src/test/resources/config/parameterTest/";
private CliParameters loadCliParameters(String fileName) throws FileNotFoundException {
return CliParametersLoader.loadCliParameters(BASE_DIR + fileName + ".json");
}
@Test
public void testClusterParamsDefaultValues() throws FileNotFoundException {
CliParameters params = loadCliParameters("clusterParamsDefaultValues");
ClustringParameters clustringParameters = params.getClustringParameters().get();
assertFalse(clustringParameters.getNumberOfClusters().isPresent());
assertEquals(ResultChoosingStrategy.bestWithFittingN, clustringParameters.getChooseBy());
assertTrue(clustringParameters.getAlgorithmParameters().isEmpty());
}
@Test
public void testClusterParamsNoAlgo() throws FileNotFoundException {
CliParameters params = loadCliParameters("clusterParamsNoAlgo");
ClustringParameters clustringParameters = params.getClustringParameters().get();
assertEquals(3, (long) clustringParameters.getNumberOfClusters().get());
assertEquals(ResultChoosingStrategy.bestOverall, clustringParameters.getChooseBy());
assertTrue(clustringParameters.getAlgorithmParameters().isEmpty());
}
@Test
public void testClusterParamsAllAlgos() throws FileNotFoundException {
CliParameters params = loadCliParameters("clusterParamsAllAlgos");
ClustringParameters clustringParameters = params.getClustringParameters().get();
List<AlgorithmCliParameters> algorithmParameters = clustringParameters.getAlgorithmParameters();
assertEquals(4, algorithmParameters.size());
Map<String, AlgorithmCliParameters> nameToParams = new HashMap<>();
algorithmParameters.forEach(p -> nameToParams.put(p.getName(), p));
assertTrue(nameToParams.get("SpectralClustering") instanceof SpectralClusteringCliParameters);
assertTrue(nameToParams.get("DBScan") instanceof DBScanCliParameters);
assertTrue(nameToParams.get("Markov") instanceof MarkovCliParameters);
assertTrue(nameToParams.get("AffinityPropagation") instanceof AffinityPropagationCliParameters);
}
@Test
public void testClusterParamsInvalidAlgos() throws FileNotFoundException {
CliParameters params = loadCliParameters("clusterParamsInvalidAlgos");
ClustringParameters clustringParameters = params.getClustringParameters().get();
List<AlgorithmCliParameters> algorithmParameters = clustringParameters.getAlgorithmParameters();
assertEquals(2, algorithmParameters.size());
assertTrue(algorithmParameters.get(0) instanceof UnknownAlgorithmCliParameters);
assertTrue(algorithmParameters.get(1) instanceof UnknownAlgorithmCliParameters);
}
}
{
"modelsDir": "src/test/resources/",
"outputDir": "target/cliTest/validConfigFile",
"rootModel": "tests.a.addComp",
"generators": ["cpp","roscpp"],
"clustringParameters":{
"algorithmParameters":[
{
"name":"SpectralClustering"
},
{
"name":"DBScan"
},
{
"name":"Markov"
},
{
"name":"AffinityPropagation"
}
]
}
}
\ No newline at end of file
{
"modelsDir": "src/test/resources/",
"outputDir": "target/cliTest/validConfigFile",
"rootModel": "tests.a.addComp",
"generators": ["cpp","roscpp"],
"clustringParameters":{
}
}
\ No newline at end of file
{
"modelsDir": "src/test/resources/",
"outputDir": "target/cliTest/validConfigFile",
"rootModel": "tests.a.addComp",
"generators": ["cpp","roscpp"],
"clustringParameters":{
"algorithmParameters":[
{
"name":"Invalid"
},
{
"name":"Unknown"
}
]
}
}
\ No newline at end of file
{
"modelsDir": "src/test/resources/",
"outputDir": "target/cliTest/validConfigFile",
"rootModel": "tests.a.addComp",
"generators": ["cpp","roscpp"],
"clustringParameters":{
"numberOfClusters":3,
"chooseBy":"bestOverall"
}
}
\ 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