Commit 2cdbf6c7 authored by Daniel Le Berre's avatar Daniel Le Berre
Browse files

fixed sonar violation in sat package

parent 028184de
Pipeline #20129 passed with stages
in 44 minutes and 57 seconds
......@@ -2,8 +2,6 @@ package org.sat4j.sat;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Dictionary;
import java.util.Hashtable;
......@@ -89,9 +87,11 @@ public class CleanCommandComponent extends CommandComponent {
@Override
public void createPanel() {
this.setBorder(new CompoundBorder(new TitledBorder(null,
this.getName(), TitledBorder.LEFT, TitledBorder.TOP),
DetailedCommandPanel.BORDER5));
this.setBorder(
new CompoundBorder(
new TitledBorder(null, this.getName(),
TitledBorder.LEFT, TitledBorder.TOP),
DetailedCommandPanel.BORDER5));
this.setLayout(new BorderLayout());
......@@ -102,7 +102,7 @@ public class CleanCommandComponent extends CommandComponent {
this.cleanSlider.setPaintTicks(true);
// Create the label table
Dictionary<Integer, JLabel> cleanValuesTable = new Hashtable<Integer, JLabel>();
Dictionary<Integer, JLabel> cleanValuesTable = new Hashtable<>();
cleanValuesTable.put(0, this.clean5000Label);
cleanValuesTable.put(1, this.clean10000Label);
cleanValuesTable.put(2, this.clean20000Label);
......@@ -139,18 +139,14 @@ public class CleanCommandComponent extends CommandComponent {
this.cleanAndEvaluationApplyButton = new JButton("Apply changes");
this.cleanAndEvaluationApplyButton
.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
hasChangedCleaningValue();
}
});
.addActionListener( e-> hasChangedCleaningValue());
JPanel tmpPanel5 = new JPanel();
tmpPanel5.add(this.cleanAndEvaluationApplyButton);
JPanel tmpPanel = new JPanel();
tmpPanel.setBorder(new CompoundBorder(new TitledBorder(null, "",
TitledBorder.LEFT, TitledBorder.TOP),
tmpPanel.setBorder(new CompoundBorder(
new TitledBorder(null, "", TitledBorder.LEFT, TitledBorder.TOP),
DetailedCommandPanel.BORDER5));
tmpPanel.setLayout(new BorderLayout());
......@@ -165,11 +161,7 @@ public class CleanCommandComponent extends CommandComponent {
this.cleanButton = new JButton(CLEAN);
this.cleanButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
hasClickedOnClean();
}
});
this.cleanButton.addActionListener(e -> hasClickedOnClean());
tmpPanel2.add(this.manualCleanLabel);
tmpPanel2.add(this.cleanButton);
......@@ -178,11 +170,8 @@ public class CleanCommandComponent extends CommandComponent {
this.cleanUseOriginalStrategyCB = new JCheckBox(USE_ORIGINAL_STRATEGY);
this.cleanUseOriginalStrategyCB.setSelected(true);
this.cleanUseOriginalStrategyCB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
hasClickedOnUseOriginalStrategy();
}
});
this.cleanUseOriginalStrategyCB
.addActionListener(e -> hasClickedOnUseOriginalStrategy());
tmpPanel3.add(this.cleanUseOriginalStrategyCB);
......@@ -211,14 +200,14 @@ public class CleanCommandComponent extends CommandComponent {
int nbConflicts = CLEAN_VALUES[this.cleanSlider.getValue()];
this.controller.setNbClausesAtWhichWeShouldClean(nbConflicts);
if (this.activityRadio.isSelected()) {
this.controller
.setLearnedDeletionStrategyTypeToSolver(LearnedConstraintsEvaluationType.ACTIVITY);
this.controller.setLearnedDeletionStrategyTypeToSolver(
LearnedConstraintsEvaluationType.ACTIVITY);
} else if (this.lbdRadio.isSelected()) {
this.controller
.setLearnedDeletionStrategyTypeToSolver(LearnedConstraintsEvaluationType.LBD);
this.controller.setLearnedDeletionStrategyTypeToSolver(
LearnedConstraintsEvaluationType.LBD);
} else if (this.lbd2Radio.isSelected()) {
this.controller
.setLearnedDeletionStrategyTypeToSolver(LearnedConstraintsEvaluationType.LBD2);
this.controller.setLearnedDeletionStrategyTypeToSolver(
LearnedConstraintsEvaluationType.LBD2);
}
}
......@@ -229,8 +218,8 @@ public class CleanCommandComponent extends CommandComponent {
this.controller
.setUseTelecomStrategyAsLearnedConstraintsDeletionStrategy();
this.controller
.setLearnedDeletionStrategyTypeToSolver(LearnedConstraintsEvaluationType.ACTIVITY);
this.controller.setLearnedDeletionStrategyTypeToSolver(
LearnedConstraintsEvaluationType.ACTIVITY);
this.activityRadio.setSelected(true);
setCleanPanelOriginalStrategyEnabled(false);
......
......@@ -9,7 +9,7 @@ public abstract class CommandComponent extends JPanel {
*/
private static final long serialVersionUID = 1L;
public CommandComponent() {
protected CommandComponent() {
super();
}
......
package org.sat4j.sat;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
......@@ -36,9 +34,11 @@ public class HotSolverCommandComponent extends CommandComponent {
}
public void createHotSolverPanel() {
this.setBorder(new CompoundBorder(new TitledBorder(null,
this.getName(), TitledBorder.LEFT, TitledBorder.TOP),
DetailedCommandPanel.BORDER5));
this.setBorder(
new CompoundBorder(
new TitledBorder(null, this.getName(),
TitledBorder.LEFT, TitledBorder.TOP),
DetailedCommandPanel.BORDER5));
this.setLayout(new BorderLayout());
......@@ -51,13 +51,10 @@ public class HotSolverCommandComponent extends CommandComponent {
tmpPanel.add(this.applyHotSolver);
this.add(tmpPanel, BorderLayout.SOUTH);
this.applyHotSolver.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
HotSolverCommandComponent.this.controller
.setKeepSolverHot(HotSolverCommandComponent.this.keepSolverHotCB
.isSelected());
}
});
this.applyHotSolver.addActionListener(
e -> HotSolverCommandComponent.this.controller.setKeepSolverHot(
HotSolverCommandComponent.this.keepSolverHotCB
.isSelected()));
}
public void setKeepSolverHotPanelEnabled(boolean enabled) {
......
......@@ -7,11 +7,11 @@ import java.net.URL;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.sat4j.minisat.core.ConflictTimer;
import org.sat4j.minisat.core.LearnedConstraintsDeletionStrategy;
import org.sat4j.minisat.learning.MiniSATLearning;
......@@ -40,7 +40,6 @@ import org.sat4j.pb.constraints.pb.PostProcessToCard;
import org.sat4j.pb.constraints.pb.PostProcessToClause;
import org.sat4j.pb.constraints.pb.PreProcessReduceConflict;
import org.sat4j.pb.constraints.pb.SkipStrategy;
import org.sat4j.pb.core.PBDataStructureFactory;
import org.sat4j.pb.core.PBSolverCP;
import org.sat4j.pb.lcds.PBActivityLCDS;
import org.sat4j.pb.lcds.PBGlucoseLCDS;
......@@ -152,24 +151,14 @@ public class KTHLauncher {
log("SAT4J PB solver for KTH experiments");
URL url = KTHLauncher.class.getResource("/sat4j.version");
if (url != null) {
BufferedReader in = null;
try {
in = new BufferedReader(
new InputStreamReader(url.openStream()));
try (BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()))) {
log("version " + in.readLine()); //$NON-NLS-1$
} catch (IOException e) {
log("c ERROR: " + e.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
log("c ERROR: " + e.getMessage());
}
}
}
}
CommandLineParser parser = new PosixParser();
CommandLineParser parser = new DefaultParser();
Options options = createCLIOptions();
if (args.length == 0) {
HelpFormatter formatter = new HelpFormatter();
......@@ -192,14 +181,14 @@ public class KTHLauncher {
cpsolver);
} else if ("inproc".equals(value)) {
InprocCardConstrLearningSolver solver = new InprocCardConstrLearningSolver(
new MiniSATLearning<PBDataStructureFactory>(),
new MiniSATLearning<>(),
new PBMaxClauseCardConstrDataStructure(),
new VarOrderHeap(), true, SkipStrategy.NO_SKIP);
solver.setDetectCardFromAllConstraintsInCflAnalysis(true);
cpsolver = solver;
} else if ("lazy".equals(value)) {
cpsolver = new InprocCardConstrLearningSolver(
new MiniSATLearning<PBDataStructureFactory>(),
new MiniSATLearning<>(),
new PBMaxClauseCardConstrDataStructure(),
new VarOrderHeap(), true, SkipStrategy.NO_SKIP);
} else {
......@@ -539,7 +528,7 @@ public class KTHLauncher {
if (line.hasOption("dot-output")) {
String dotfilename = line.getOptionValue("dot-output");
if (dotfilename != null) {
DotSearchTracing<String> dotTracing = new DotSearchTracing<String>(
DotSearchTracing<String> dotTracing = new DotSearchTracing<>(
dotfilename);
cpsolver.setSearchListener(dotTracing);
dotTracing.setMapping(reader.getMapping());
......
......@@ -29,30 +29,8 @@
*******************************************************************************/
package org.sat4j.sat;
/**
* This class is used to launch the SAT solvers from the command line. It is
* compliant with the SAT competition (www.satcompetition.org) I/O format. The
* launcher is to be used as follows:
*
* <pre>
* [solvername] filename [key=value]*
* </pre>
*
* If no solver name is given, then the default solver of the solver factory is
* used (@see org.sat4j.core.ASolverFactory#defaultSolver()).
*
* This class is no longer used since 2.3.3 because it cannot launch maxsat problems.
*
* @see Launcher
*
* @author sroussel
*
*/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.cli.CommandLine;
......@@ -63,7 +41,6 @@ import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.sat4j.AbstractLauncher;
import org.sat4j.ExitCode;
import org.sat4j.ILauncherMode;
import org.sat4j.OutputPrefix;
import org.sat4j.core.VecInt;
import org.sat4j.minisat.core.ICDCL;
......@@ -133,7 +110,7 @@ public class Lanceur extends AbstractLauncher implements ILogAble {
*/
@SuppressWarnings({ "nls", "unchecked" })
@Override
protected ICDCL configureSolver(String[] args) {
protected ICDCL<?> configureSolver(String[] args) {
Options options = createCLIOptions();
try {
......@@ -154,13 +131,13 @@ public class Lanceur extends AbstractLauncher implements ILogAble {
Class<?> clazz = Class
.forName("org.sat4j." + framework + ".SolverFactory"); //$NON-NLS-1$ //$NON-NLS-2$
Class<?>[] params = {};
Method m = clazz.getMethod("instance", params); //$NON-NLS-1$
clazz.getMethod("instance", params); //$NON-NLS-1$
} catch (Exception e) { // DLB Findbugs warning ok
log("Wrong framework: " + framework
+ ". Using minisat instead.");
}
ICDCL asolver = Solvers.configureSolver(args, this);
ICDCL<?> asolver = Solvers.configureSolver(args, this);
this.launchRemoteControl = cmd.hasOption("remote");
......@@ -414,15 +391,11 @@ public class Lanceur extends AbstractLauncher implements ILogAble {
frame.activateTracing(this.modeTracing);
frame.setOptimisationMode(this.isModeOptimization);
}
} catch (FileNotFoundException e) {
System.err.println("FATAL " + e.getLocalizedMessage());
} catch (IOException e) {
} catch (IOException | ParseFormatException e) {
System.err.println("FATAL " + e.getLocalizedMessage());
} catch (ContradictionException e) {
this.setExitCode(ExitCode.UNSATISFIABLE);
log("(trivial inconsistency)"); //$NON-NLS-1$
} catch (ParseFormatException e) {
System.err.println("FATAL " + e.getLocalizedMessage());
}
}
......
......@@ -67,7 +67,7 @@ public class LaunchRemoteControl {
break;
}
RemoteControlFrame frame = new RemoteControlFrame(filename, ramdisk);
new RemoteControlFrame(filename, ramdisk);
}
......
......@@ -29,15 +29,14 @@
*******************************************************************************/
package org.sat4j.sat;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.sat4j.AbstractLauncher;
import org.sat4j.DecisionMode;
import org.sat4j.ExitCode;
......@@ -56,7 +55,6 @@ import org.sat4j.reader.Reader;
import org.sat4j.specs.ContradictionException;
import org.sat4j.specs.ILogAble;
import org.sat4j.specs.ISolver;
import org.sat4j.specs.TimeoutException;
import org.sat4j.tools.ConflictDepthTracing;
import org.sat4j.tools.ConflictLevelTracing;
import org.sat4j.tools.DecisionTracing;
......@@ -152,7 +150,7 @@ public class Launcher extends AbstractLauncher implements ILogAble {
Options options = Solvers.createCLIOptions();
try {
CommandLine cmd = new PosixParser().parse(options, args);
CommandLine cmd = new DefaultParser().parse(options, args);
this.isModeOptimization = cmd.hasOption("opt");
......@@ -195,14 +193,10 @@ public class Launcher extends AbstractLauncher implements ILogAble {
setSilent(true);
}
if (cmd.hasOption("k")) {
Integer myk = Integer.valueOf(cmd.getOptionValue("k"));
}
if (cmd.hasOption("r")) {
this.modeTracing = true;
if (!cmd.hasOption("remote")) {
asolver.setSearchListener(new MultiTracing(
asolver.setSearchListener(new MultiTracing<>(
new ConflictLevelTracing(
new FileBasedVisualizationTool(
this.filename + "-conflict-level"),
......@@ -249,7 +243,7 @@ public class Launcher extends AbstractLauncher implements ILogAble {
case CNF_MAXSAT:
case WCNF_MAXSAT:
setLauncherMode(OptimizationMode.instance());
asolver = new WeightedMaxSatDecorator((IPBCDCLSolver) asolver,
asolver = new WeightedMaxSatDecorator((IPBCDCLSolver<?>) asolver,
equivalence);
if (cmd.hasOption("lo")) {
this.problem = new ConstraintRelaxingPseudoOptDecorator(
......@@ -329,15 +323,11 @@ public class Launcher extends AbstractLauncher implements ILogAble {
frame.activateTracing(this.modeTracing);
frame.setOptimisationMode(this.isModeOptimization);
}
} catch (FileNotFoundException e) {
System.err.println("FATAL " + e.getLocalizedMessage());
} catch (IOException e) {
} catch (IOException | ParseFormatException e) {
System.err.println("FATAL " + e.getLocalizedMessage());
} catch (ContradictionException e) {
getLauncherMode().setExitCode(ExitCode.UNSATISFIABLE);
log("(trivial inconsistency)"); //$NON-NLS-1$
} catch (ParseFormatException e) {
System.err.println("FATAL " + e.getLocalizedMessage());
}
}
......
......@@ -2,8 +2,6 @@ package org.sat4j.sat;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
......@@ -26,7 +24,7 @@ public class PhaseCommandComponent extends CommandComponent {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = Logger.getLogger("org.sat4j.sat");
private String currentPhaseSelectionStrategy;
private JComboBox phaseList;
......@@ -56,9 +54,11 @@ public class PhaseCommandComponent extends CommandComponent {
public void createPhasePanel() {
this.setBorder(new CompoundBorder(new TitledBorder(null,
this.getName(), TitledBorder.LEFT, TitledBorder.TOP),
DetailedCommandPanel.BORDER5));
this.setBorder(
new CompoundBorder(
new TitledBorder(null, this.getName(),
TitledBorder.LEFT, TitledBorder.TOP),
DetailedCommandPanel.BORDER5));
this.setLayout(new BorderLayout());
......@@ -75,11 +75,7 @@ public class PhaseCommandComponent extends CommandComponent {
this.phaseApplyButton = new JButton(PHASE_APPLY);
this.phaseApplyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
hasClickedOnApplyPhase();
}
});
this.phaseApplyButton.addActionListener(e -> hasClickedOnApplyPhase());
JPanel tmpPanel2 = new JPanel();
tmpPanel2.add(this.phaseApplyButton);
......@@ -93,8 +89,9 @@ public class PhaseCommandComponent extends CommandComponent {
this.currentPhaseSelectionStrategy = phaseName;
IPhaseSelectionStrategy phase = null;
try {
phase = (IPhaseSelectionStrategy) Class.forName(
PHASE_PATH_SAT + "." + phaseName).newInstance();
phase = (IPhaseSelectionStrategy) Class
.forName(PHASE_PATH_SAT + "." + phaseName).getConstructor()
.newInstance();
phase.init(this.solverController.getNVar() + 1);
this.solverController.setPhaseSelectionStrategy(phase);
......@@ -104,6 +101,10 @@ public class PhaseCommandComponent extends CommandComponent {
LOGGER.log(Level.INFO, "Invalid Phase Access error", e);
} catch (InstantiationException e) {
LOGGER.log(Level.INFO, "Invalid Phase Instanciation error", e);
} catch (ReflectiveOperationException e) {
LOGGER.log(Level.INFO, "Reflective Operation error", e);
} catch (SecurityException e) {
LOGGER.log(Level.INFO, "Security error", e);
}
}
......@@ -117,7 +118,7 @@ public class PhaseCommandComponent extends CommandComponent {
public List<String> getListOfPhaseStrategies() {
List<String> resultRTSI = RTSI.find(PHASE_STRATEGY_CLASS);
List<String> finalResult = new ArrayList<String>();
List<String> finalResult = new ArrayList<>();
for (String s : resultRTSI) {
if (!s.contains("Remote")) {
......
......@@ -33,7 +33,6 @@
package org.sat4j.sat;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.net.JarURLConnection;
......@@ -70,8 +69,8 @@ public class RTSI {
}
public static List<String> find(String tosubclassname, boolean fullname) {
alreadySeenPckges = new ArrayList<String>();
Set<String> v = new HashSet<String>();
alreadySeenPckges = new ArrayList<>();
Set<String> v = new HashSet<>();
Set<String> tmp;
try {
Class<?> tosubclass = Class.forName(tosubclassname);
......@@ -85,12 +84,12 @@ public class RTSI {
} catch (ClassNotFoundException ex) {
System.err.println("Class " + tosubclassname + " not found!");
}
return new ArrayList<String>(v);
return new ArrayList<>(v);
}
public static Set<String> find(String pckname, String tosubclassname,
boolean fullname) {
Set<String> v = new HashSet<String>();
Set<String> v = new HashSet<>();
try {
Class<?> tosubclass = Class.forName(tosubclassname);
v = find(pckname, tosubclass, fullname);
......@@ -103,7 +102,7 @@ public class RTSI {
public static Set<String> find(String pckgname, Class<?> tosubclass,
boolean fullname) {
if (alreadySeenPckges.contains(pckgname)) {
return new HashSet<String>();
return new HashSet<>();
} else {
alreadySeenPckges.add(pckgname);
return findnames(pckgname, tosubclass, fullname);
......@@ -128,7 +127,7 @@ public class RTSI {
public static Set<String> findnames(String pckgname, Class<?> tosubclass,
boolean fullname) {
Set<String> v = new HashSet<String>();
Set<String> v = new HashSet<>();
// Code from JWhich
// ======
......@@ -179,12 +178,9 @@ public class RTSI {
}
}
File[] dirs = directory.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return new File(dir.getAbsolutePath() + "/" + name)
.isDirectory();
}
});
File[] dirs = directory.listFiles(
(dir, n) -> new File(dir.getAbsolutePath() + "/" + n)
.isDirectory());
Set<String> tmp;
for (File dir : dirs) {
String newName = pckgname + "." + dir.getName();
......@@ -224,8 +220,8 @@ public class RTSI {
if (fullname) {
v.add(classname);
} else {
v.add(classname.substring(classname
.lastIndexOf('.') + 1));
v.add(classname.substring(
classname.lastIndexOf('.') + 1));
}
}
} catch (NoClassDefFoundError cnfex) {
......@@ -246,11 +242,13 @@ public class RTSI {
System.out.println(find(tosubclassname));
}
public static void displayResultOfFind(String pckname, String tosubclassname) {
public static void displayResultOfFind(String pckname,
String tosubclassname) {
System.out.println(find(pckname, tosubclassname));
}
public static void displayResultOfFind(String pckgname, Class<?> tosubclass) {
public static void displayResultOfFind(String pckgname,
Class<?> tosubclass) {
System.out.println(findnames(pckgname, tosubclass));
}
......
......@@ -2,8 +2,6 @@ package org.sat4j.sat;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
......@@ -29,7 +27,8 @@ public class RandomWalkCommandComponent extends CommandComponent {
private static final String RW_LABEL = "Probabilty : ";
private static final String RW_APPLY = "Apply";
public RandomWalkCommandComponent(String name, SolverController controller) {
public RandomWalkCommandComponent(String name,
SolverController controller) {
this.controller = controller;
this.setName(name);