Skip to content

Commit

Permalink
fixed sonarqube quality issues
Browse files Browse the repository at this point in the history
  • Loading branch information
koalamitice committed Aug 12, 2024
1 parent a93d142 commit 5f63835
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 162 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.kit.provideq.toolbox.maxcut;

import edu.kit.provideq.toolbox.ResourceProvider;
import edu.kit.provideq.toolbox.exception.MissingExampleException;
import edu.kit.provideq.toolbox.maxcut.solvers.CirqMaxCutSolver;
import edu.kit.provideq.toolbox.maxcut.solvers.GamsMaxCutSolver;
import edu.kit.provideq.toolbox.maxcut.solvers.QiskitMaxCutSolver;
Expand Down Expand Up @@ -53,7 +54,7 @@ private Set<Problem<String, String>> loadExampleProblems(ResourceProvider resour
problem.setInput(resourceProvider.readStream(problemInputStream));
return Set.of(problem);
} catch (IOException e) {
throw new RuntimeException("Could not load example problems", e);
throw new MissingExampleException("Could not load example problems", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public BooleanState(String name, String title, boolean state) {
}

public boolean getState() {
return state;
return state;
}

public void setState(boolean state) {
this.state = state;
this.state = state;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,37 @@

@JsonDeserialize(using = MetaSolverSettingDeserializer.class)
public abstract class MetaSolverSetting {
public String name;
private String name;
private MetaSolverSettingType type;
private String title;
public MetaSolverSettingType type;

public MetaSolverSetting() {
protected MetaSolverSetting(String name, String title, MetaSolverSettingType type) {
this.setName(name);
this.setType(type);
this.setTitle(title);
}

protected MetaSolverSetting(String name, String title, MetaSolverSettingType type) {
public String getTitle() {
return title;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public MetaSolverSettingType getType() {
return type;
}

public void setType(MetaSolverSettingType type) {
this.type = type;
this.title = title;
}

public String getTitle() {
return title;
public void setTitle(String title) {
this.title = title;
}
}
21 changes: 19 additions & 2 deletions src/main/java/edu/kit/provideq/toolbox/meta/setting/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import javax.annotation.Nullable;

public class Select<T> extends MetaSolverSetting {
public List<T> options;
private List<T> options;
@Nullable
public T selectedOption;
private T selectedOption;

public Select(String name, String title, List<T> options) {
this(name, title, options, null);
Expand All @@ -15,7 +15,24 @@ public Select(String name, String title, List<T> options) {
public Select(String name, String title, List<T> options, T selectedOption) {
super(name, title, MetaSolverSettingType.SELECT);

this.setOptions(options);
this.setSelectedOption(selectedOption);
}

public List<T> getOptions() {
return options;
}

public void setOptions(List<T> options) {
this.options = options;
}

@Nullable
public T getSelectedOption() {
return selectedOption;
}

public void setSelectedOption(@Nullable T selectedOption) {
this.selectedOption = selectedOption;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package edu.kit.provideq.toolbox.process;

import edu.kit.provideq.toolbox.meta.ProblemType;
import java.util.Optional;
import java.util.UUID;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
* Process runner with input & output post-processing specifically for invoking GAMS.
*/
Expand Down Expand Up @@ -55,14 +54,6 @@ public GamsProcessRunner(String directory, String scriptFileName, String... argu
addProblemFilePathToProcessCommand("--INPUT=\"%s\"");
}

@Override
public ProcessResult<String> run(ProblemType<?, ?> problemType, UUID solutionId, String problemData) {
var result = super.run(problemType, solutionId, problemData);

var obfuscatedOutput = obfuscateGamsLicense(result.output().get());
return new ProcessResult<>(result.success(), Optional.of(obfuscatedOutput), Optional.empty());
}

/**
* Removes GAMS' license output from an output log.
*/
Expand Down Expand Up @@ -90,4 +81,13 @@ private static String obfuscateGamsLicense(String output) {
private static String obfuscateLine(String line) {
return Strings.repeat("*", line.length());
}

@Override
public ProcessResult<String> run(ProblemType<?, ?> problemType, UUID solutionId,
String problemData) {
var result = super.run(problemType, solutionId, problemData);

var obfuscatedOutput = obfuscateGamsLicense(result.output().orElse("no license found"));
return new ProcessResult<>(result.success(), Optional.of(obfuscatedOutput), Optional.empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public <T> ProcessResult<T> run(ProblemType<?, ?> problemType, UUID solutionId,
.getProblemDirectory(problemType, solutionId)
.getAbsolutePath();
} catch (IOException e) {
return new ProcessResult<T>(
return new ProcessResult<>(
false,
Optional.empty(),
Optional.of(
Expand All @@ -202,7 +202,7 @@ public <T> ProcessResult<T> run(ProblemType<?, ?> problemType, UUID solutionId,
try {
Files.writeString(problemFilePath, problemData);
} catch (IOException e) {
return new ProcessResult<T>(
return new ProcessResult<>(
false,
Optional.empty(),
Optional.of("Error: The problem data couldn't be written to %s:%n%s".formatted(
Expand Down Expand Up @@ -244,7 +244,7 @@ public <T> ProcessResult<T> run(ProblemType<?, ?> problemType, UUID solutionId,

processExitCode = process.waitFor();
} catch (IOException | InterruptedException e) {
return new ProcessResult<T>(
return new ProcessResult<>(
false,
Optional.empty(),
Optional.of(
Expand All @@ -256,7 +256,7 @@ public <T> ProcessResult<T> run(ProblemType<?, ?> problemType, UUID solutionId,

// Return prematurely if the process failed
if (processExitCode != 0) {
return new ProcessResult<T>(
return new ProcessResult<>(
false,
Optional.empty(),
Optional.of(
Expand All @@ -268,7 +268,7 @@ public <T> ProcessResult<T> run(ProblemType<?, ?> problemType, UUID solutionId,
reader.read(solutionFile, problemFilePath, Path.of(problemDirectoryPath));

if (!result.success()) {
return new ProcessResult<T>(
return new ProcessResult<>(
result.success(),
result.output(),
result.errorOutput().isPresent() ? Optional.of(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.kit.provideq.toolbox.process;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.kit.provideq.toolbox.qubo;

import edu.kit.provideq.toolbox.ResourceProvider;
import edu.kit.provideq.toolbox.exception.MissingExampleException;
import edu.kit.provideq.toolbox.meta.Problem;
import edu.kit.provideq.toolbox.meta.ProblemManager;
import edu.kit.provideq.toolbox.meta.ProblemType;
Expand Down Expand Up @@ -55,7 +56,7 @@ private Set<Problem<String, String>> loadExampleProblems(ResourceProvider resour
problem.setInput(resourceProvider.readStream(problemInputStream));
return Set.of(problem);
} catch (IOException e) {
throw new RuntimeException("Could not load example problems", e);
throw new MissingExampleException("Could not load example problems", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
@Component
public class DwaveQuboSolver extends QuboSolver {
private static final String METHOD_SETTING_NAME = "method";
private static final String API_TOKEN_SETTING_NAME = "dwave-token";
private final String quboScriptPath;
private final ApplicationContext context;

Expand All @@ -34,37 +32,20 @@ public String getName() {
return "(D-Wave) Annealing QUBO Solver";
}

/*
@Override
public List<MetaSolverSetting> getSettings() {
return List.of(
new Select<String>(METHOD_SETTING_NAME, "DWave Annealing Method", List.of("sim", "hybrid", "qbsolv", "direct"), "sim"),
new Text(API_TOKEN_SETTING_NAME, "DWave API Token (required for non-sim methods)")
);
}*/

@Override
public Mono<Solution<String>> solve(
String input,
SubRoutineResolver subRoutineResolver
) {
/*
String dwaveAnnealingMethod = settings.stream()
.filter(setting -> setting.name.equals(METHOD_SETTING_NAME))
.map(setting -> ((Select<String>) setting))
.findFirst()
.map(setting -> setting.selectedOption)
.orElse("sim");

Optional<String> dwaveToken = settings.stream()
.filter(setting -> setting.name.equals(API_TOKEN_SETTING_NAME))
.map(setting -> ((Text) setting))
.findFirst()
.map(setting -> setting.text);
*/ //TODO: Add Setting again (currently not part of our model)
// there is currently no field where a token can be added by the user
// this field is kept because it was used in Lucas implementation and will be added back later
Optional<String> dwaveToken = Optional.empty();

String dwaveAnnealingMethod = "sim"; //TODO: remove this again
Optional<String> dwaveToken = Optional.empty(); //TODO: remove this again
// this field is only relevant when a dwaveToken is added
// (a token is needed to access the d-wave hardware)
// options are: sim, hybrid, absolv, direct
String dwaveAnnealingMethod = "sim";

var solution = new Solution<String>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.kit.provideq.toolbox.sat;

import edu.kit.provideq.toolbox.ResourceProvider;
import edu.kit.provideq.toolbox.exception.MissingExampleException;
import edu.kit.provideq.toolbox.format.cnf.dimacs.DimacsCnfSolution;
import edu.kit.provideq.toolbox.meta.Problem;
import edu.kit.provideq.toolbox.meta.ProblemManager;
Expand Down Expand Up @@ -51,7 +52,7 @@ private Set<Problem<String, DimacsCnfSolution>> loadExampleProblems(
problem.setInput(resourceProvider.readStream(problemInputStream));
return Set.of(problem);
} catch (IOException e) {
throw new RuntimeException("Could not load example problems", e);
throw new MissingExampleException("Could not load example problems", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import edu.kit.provideq.toolbox.tsp.solvers.LkhTspSolver;
import edu.kit.provideq.toolbox.tsp.solvers.QuboTspSolver;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -41,7 +40,7 @@ ProblemManager<String, String> getTspManager(

private Set<Problem<String, String>> loadExampleProblems(ResourceProvider provider) {
try {
String[] problemNames = new String[]{
String[] problemNames = new String[] {
"att48.tsp", "SmallSampleTSP.tsp", "VerySmallSampleTSP.tsp"
};

Expand Down
Loading

0 comments on commit 5f63835

Please sign in to comment.