Added parsing of all algorithm parameters

parent beaac86d
Pipeline #102094 passed with stages
in 15 minutes and 34 seconds
package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringAlgorithm;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.AffinityPropagationAlgorithm;
import java.util.ArrayList;
import java.util.List;
public class AffinityPropagationCliParameters extends AlgorithmCliParameters{
public AffinityPropagationCliParameters() {
}
}
@Override
public ClusteringAlgorithm asClustringAlgorithm() {
return new AffinityPropagationAlgorithm();
}
@Override
public List<Object> asAlgorithmArgs(){
return new ArrayList<>();
}
@Override
public boolean isValid() {
return true;
}
}
\ No newline at end of file
package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
public class AlgorithmCliParameters {
import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringAlgorithm;
import java.util.List;
public abstract class AlgorithmCliParameters {
public static final String TYPE_SPECTRAL_CLUSTERING = "SpectralClustering";
public static final String TYPE_UNKOWN = "Unkown";
public static final String TYPE_DBSCAN = "DBScan";
......@@ -13,4 +17,10 @@ public class AlgorithmCliParameters {
public String getName() {
return name;
}
public abstract ClusteringAlgorithm asClustringAlgorithm();
public abstract List<Object> asAlgorithmArgs();
public abstract boolean isValid();
}
package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringAlgorithm;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.DBSCANClusteringAlgorithm;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.DBSCANClusteringBuilder;
import de.se_rwth.commons.logging.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class DBScanCliParameters extends AlgorithmCliParameters {
private Integer min_pts;
private Double radius;
public DBScanCliParameters() {
}
......@@ -9,4 +21,38 @@ public class DBScanCliParameters extends AlgorithmCliParameters {
public String getName() {
return TYPE_DBSCAN;
}
@Override
public ClusteringAlgorithm asClustringAlgorithm() {
return new DBSCANClusteringAlgorithm();
}
@Override
public List<Object> asAlgorithmArgs() {
List<Object> res = new ArrayList<>();
if(!isValid()){
Log.error("DBScanCliParameters: The min_pts or radius parameters are mandatory but at least one is unset!");
return res;
}
res.add(DBSCANClusteringBuilder.DBSCANParameters.DBSCAN_MIN_PTS);
res.add(min_pts);
res.add(DBSCANClusteringBuilder.DBSCANParameters.DBSCAN_RADIUS);
res.add(radius);
return res;
}
@Override
public boolean isValid() {
return min_pts != null && radius != null;
}
public Optional<Integer> getMinPts() {
return Optional.ofNullable(min_pts);
}
public Optional<Double> getRadius() {
return Optional.ofNullable(radius);
}
}
package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringAlgorithm;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.MarkovClusteringAlgorithm;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.MarkovClusteringBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class MarkovCliParameters extends AlgorithmCliParameters {
private Double max_residual;
private Double gamma_exp;
private Double loop_gain;
private Double zero_max;
public MarkovCliParameters() {
}
......@@ -9,4 +21,52 @@ public class MarkovCliParameters extends AlgorithmCliParameters {
public String getName() {
return TYPE_MARKOV;
}
@Override
public ClusteringAlgorithm asClustringAlgorithm() {
return new MarkovClusteringAlgorithm();
}
@Override
public List<Object> asAlgorithmArgs() {
List<Object> res = new ArrayList<>();
if(max_residual != null){
res.add(MarkovClusteringBuilder.MarkovParameters.MARKOV_MAX_RESIDUAL);
res.add(max_residual);
}
if(gamma_exp != null){
res.add(MarkovClusteringBuilder.MarkovParameters.MARKOV_GAMMA_EXP);
res.add(gamma_exp);
}
if(loop_gain != null){
res.add(MarkovClusteringBuilder.MarkovParameters.MARKOV_LOOP_GAIN);
res.add(loop_gain);
}
if(zero_max != null){
res.add(MarkovClusteringBuilder.MarkovParameters.MARKOV_ZERO_MAX);
res.add(zero_max);
}
return res;
}
@Override
public boolean isValid() {
return true;
}
public Optional<Double> getMaxResidual() {
return Optional.ofNullable(max_residual);
}
public Optional<Double> getGammaExp() {
return Optional.ofNullable(gamma_exp);
}
public Optional<Double> getLoopGain() {
return Optional.ofNullable(loop_gain);
}
public Optional<Double> getZeroMax() {
return Optional.ofNullable(zero_max);
}
}
package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringAlgorithm;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.SpectralClusteringAlgorithm;
import de.monticore.lang.monticar.generator.middleware.clustering.algorithms.SpectralClusteringBuilder;
import de.se_rwth.commons.logging.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class SpectralClusteringCliParameters extends AlgorithmCliParameters {
private Integer numberOfClusters;
private Integer l;
private Double sigma;
public SpectralClusteringCliParameters() {
}
......@@ -9,4 +21,51 @@ public class SpectralClusteringCliParameters extends AlgorithmCliParameters {
public String getName() {
return TYPE_SPECTRAL_CLUSTERING;
}
@Override
public ClusteringAlgorithm asClustringAlgorithm() {
return new SpectralClusteringAlgorithm();
}
@Override
public List<Object> asAlgorithmArgs(){
ArrayList<Object> res = new ArrayList<>();
if(!isValid()){
Log.error("SpectralClusteringCliParameters: The numberOfClusters parameter is mandatory but unset!");
return res;
}
res.add(SpectralClusteringBuilder.SpectralParameters.SPECTRAL_NUM_CLUSTERS);
res.add(numberOfClusters);
if(l != null){
res.add(SpectralClusteringBuilder.SpectralParameters.SPECTRAL_L);
res.add(l);
}
if(sigma != null){
res.add(SpectralClusteringBuilder.SpectralParameters.SPECTRAL_SIGMA);
res.add(sigma);
}
return res;
}
@Override
public boolean isValid() {
return numberOfClusters != null;
}
public Optional<Integer> getNumberOfClusters() {
return Optional.ofNullable(numberOfClusters);
}
public Optional<Integer> getL() {
return Optional.ofNullable(l);
}
public Optional<Double> getSigma() {
return Optional.ofNullable(sigma);
}
}
package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringAlgorithm;
import java.util.List;
public class UnknownAlgorithmCliParameters extends AlgorithmCliParameters {
public UnknownAlgorithmCliParameters() {
......@@ -9,4 +13,19 @@ public class UnknownAlgorithmCliParameters extends AlgorithmCliParameters {
public String getName() {
return TYPE_UNKOWN;
}
@Override
public ClusteringAlgorithm asClustringAlgorithm() {
return null;
}
@Override
public List<Object> asAlgorithmArgs() {
return null;
}
@Override
public boolean isValid() {
return false;
}
}
......@@ -56,9 +56,14 @@ public class SpectralClusteringBuilder {
public SpectralClustering build() {
SpectralClustering sc;
if (this.l != null && this.sigma != null) sc = new SpectralClustering(data, k, l, sigma); else
if (this.sigma != null) sc = new SpectralClustering(data, k, sigma); else
if (this.l != null && this.sigma != null) {
sc = new SpectralClustering(data, k, l, sigma);
} else {
if (this.sigma != null) {
sc = new SpectralClustering(data, k, sigma);
} else
sc = new SpectralClustering(data, k);
}
return sc;
}
......
......@@ -53,10 +53,36 @@ public class ParameterLoadingTest {
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);
SpectralClusteringCliParameters spectralClustering = (SpectralClusteringCliParameters) nameToParams.get("SpectralClustering");
DBScanCliParameters dbScan = (DBScanCliParameters) nameToParams.get("DBScan");
MarkovCliParameters markov = (MarkovCliParameters) nameToParams.get("Markov");
AffinityPropagationCliParameters affinityPropagation = (AffinityPropagationCliParameters) nameToParams.get("AffinityPropagation");
assertTrue(spectralClustering.isValid());
assertTrue(dbScan.isValid());
assertTrue(markov.isValid());
assertTrue(affinityPropagation.isValid());
double delta = 0.000001;
assertEquals(4, (int) spectralClustering.getNumberOfClusters().get());
assertEquals(10, (int) spectralClustering.getL().get());
assertEquals(0.01, (double) spectralClustering.getSigma().get(), delta);
assertEquals(5, (int) dbScan.getMinPts().get());
assertEquals(2.5, (double) dbScan.getRadius().get(), delta);
assertEquals(0.001, markov.getMaxResidual().get(), delta);
assertEquals(2.0, markov.getGammaExp().get(), delta);
assertEquals(0, markov.getLoopGain().get(), delta);
assertEquals(0.002, markov.getZeroMax().get(), delta);
}
@Test
......
......@@ -6,13 +6,22 @@
"clustringParameters":{
"algorithmParameters":[
{
"name":"SpectralClustering"
"name":"SpectralClustering",
"numberOfClusters":4,
"l": 10,
"sigma": 0.01
},
{
"name":"DBScan"
"name":"DBScan",
"min_pts":5,
"radius":2.5
},
{
"name":"Markov"
"name":"Markov",
"max_residual":0.001,
"gamma_exp": 2.0,
"loop_gain": 0.0,
"zero_max": 0.002
},
{
"name":"AffinityPropagation"
......
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