Commit bd5b5434 authored by Daniel Le Berre's avatar Daniel Le Berre
Browse files

more sonarqube violations fixes for sat4j core

parent b1ff349b
Pipeline #20108 passed with stages
in 44 minutes and 11 seconds
......@@ -146,7 +146,7 @@ public class QuadraticPrimeImplicantStrategy implements PrimeImplicantStrategy {
}
solver.cancelUntil(0);
var implicant = new int[prime.length - removed - 1];
int index = 0;
var index = 0;
for (int i : prime) {
if (i != 0) {
implicant[index++] = i;
......
......@@ -130,7 +130,7 @@ public class SolverStats implements Serializable {
}
public Map<String, Number> toMap() {
Map<String, Number> map = new HashMap<String, Number>();
Map<String, Number> map = new HashMap<>();
Class<?> clazz = this.getClass();
do {
for (Field f : clazz.getDeclaredFields()) {
......@@ -141,9 +141,7 @@ public class SolverStats implements Serializable {
&& value instanceof Number) {
map.put(f.getName(), (Number) value);
}
} catch (IllegalArgumentException e) {
// ignores silently
} catch (IllegalAccessException e) {
} catch (IllegalArgumentException | IllegalAccessException e) {
// ignores silently
}
}
......
......@@ -83,11 +83,11 @@ public class WatcherBasedPrimeImplicantStrategy
}
this.prime = new int[solver.voc.nVars() + 1];
int p;
for (int i = 0; i < this.prime.length; i++) {
for (var i = 0; i < this.prime.length; i++) {
this.prime[i] = 0;
}
// unit clauses need to be handled specifically
for (int i = 0; i < solver.trail.size(); i++) {
for (var i = 0; i < solver.trail.size(); i++) {
isMandatory(solver.trail.get(i));
}
for (int d : solver.fullmodel) {
......@@ -100,9 +100,9 @@ public class WatcherBasedPrimeImplicantStrategy
reduceClausesContainingTheNegationOfPI(solver, toInternal(d));
}
int removed = 0;
int posremoved = 0;
int propagated = 0;
var removed = 0;
var posremoved = 0;
var propagated = 0;
for (int d : fullModel(solver)) {
if (this.prime[Math.abs(d)] != 0) {
// d has been propagated
......@@ -118,8 +118,8 @@ public class WatcherBasedPrimeImplicantStrategy
}
}
solver.cancelUntil(0);
int[] implicant = new int[propagated];
int index = 0;
var implicant = new int[propagated];
var index = 0;
for (int i : this.prime) {
if (i != 0) {
implicant[index++] = i;
......@@ -146,7 +146,7 @@ public class WatcherBasedPrimeImplicantStrategy
lwatched.clear();
solver.voc.watches(p).moveTo(lwatched);
final int size = lwatched.size();
for (int i = 0; i < size; i++) {
for (var i = 0; i < size; i++) {
solver.stats.incInspects();
lwatched.get(i).propagatePI(this, p);
}
......
......@@ -87,8 +87,8 @@ public final class ActiveLearning<D extends DataStructureFactory>
*/
@Override
protected boolean learningCondition(Constr clause) {
int nbactivevars = 0;
for (int i = 0; i < clause.size(); i++) {
var nbactivevars = 0;
for (var i = 0; i < clause.size(); i++) {
if (this.order.varActivity(clause.get(i)) > 1) {
nbactivevars++;
}
......
......@@ -56,9 +56,9 @@ public abstract class LimitedLearning<D extends DataStructureFactory>
private SolverStats stats;
public LimitedLearning() {
this.none = new NoLearningButHeuristics<D>();
this.all = new MiniSATLearning<D>();
protected LimitedLearning() {
this.none = new NoLearningButHeuristics<>();
this.all = new MiniSATLearning<>();
}
public void setSolver(Solver<D> s) {
......
......@@ -45,10 +45,12 @@ public final class PhaseCachingAutoEraseStrategy
*/
private static final long serialVersionUID = 1L;
@Override
public void assignLiteral(int p) {
this.phase[var(p)] = p;
}
@Override
public void updateVar(int p) {
this.phase[var(p)] = p;
}
......@@ -57,7 +59,4 @@ public final class PhaseCachingAutoEraseStrategy
public String toString() {
return "Phase caching with auto forget feature";
}
public void updateVarAtDecisionLevel(int q) {
}
}
......@@ -43,8 +43,8 @@ public final class PositiveLiteralSelectionStrategy
*/
private static final long serialVersionUID = 1L;
public int select(int var) {
return posLit(var);
public int select(int variable) {
return posLit(variable);
}
@Override
......
......@@ -32,8 +32,7 @@ package org.sat4j.minisat.orders;
import org.sat4j.annotations.Feature;
/**
* @author leberre TODO To change the template for this generated type comment
* go to Window - Preferences - Java - Code Style - Code Templates
* @author leberre
*/
@Feature(value = "varheuristics", parent = "expert")
public final class PureOrder extends VarOrderHeap {
......
......@@ -60,11 +60,11 @@ public final class RandomLiteralSelectionStrategy
.getProperty("NONDETERMINISTIC") == null ? new Random(123456789)
: new Random();
public int select(int var) {
public int select(int variable) {
if (RAND.nextBoolean()) {
return posLit(var);
return posLit(variable);
}
return negLit(var);
return negLit(variable);
}
@Override
......
......@@ -95,12 +95,12 @@ public class RandomWalkDecorator implements IOrder, Serializable {
public int select() {
if (RAND.nextDouble() < this.p) {
int var, lit, max;
int variable, lit, max;
while (!this.decorated.heap.empty()) {
max = this.decorated.heap.size();
var = this.decorated.heap.get(RAND.nextInt(max) + 1);
lit = getPhaseSelectionStrategy().select(var);
variable = this.decorated.heap.get(RAND.nextInt(max) + 1);
lit = getPhaseSelectionStrategy().select(variable);
if (this.voc.isUnassigned(lit)) {
this.nbRandomWalks++;
return lit;
......
......@@ -56,7 +56,7 @@ public final class SolutionPhaseSelectionStrategy
public void init(int nlength) {
if (this.phase == null || this.phase.length < nlength) {
this.phase = new int[nlength];
for (int i = 1; i < nlength; i++) {
for (var i = 1; i < nlength; i++) {
this.phase[i] = negLit(i);
}
}
......
......@@ -52,7 +52,7 @@ public class TabuListDecorator implements IOrder {
private ILits voc;
private int lastVar = -1;
private final LinkedList<Integer> tabuList = new LinkedList<Integer>();
private final LinkedList<Integer> tabuList = new LinkedList<>();
public TabuListDecorator(VarOrderHeap order) {
this(order, 10);
......@@ -84,14 +84,14 @@ public class TabuListDecorator implements IOrder {
public int select() {
int lit = this.decorated.select();
if (lit == ILits.UNDEFINED) {
int var;
int variable;
do {
if (this.tabuList.isEmpty()) {
return ILits.UNDEFINED;
}
var = this.tabuList.removeFirst();
} while (!this.voc.isUnassigned(var << 1));
return getPhaseSelectionStrategy().select(var);
variable = this.tabuList.removeFirst();
} while (!this.voc.isUnassigned(variable << 1));
return getPhaseSelectionStrategy().select(variable);
}
this.lastVar = lit >> 1;
return lit;
......@@ -112,8 +112,8 @@ public class TabuListDecorator implements IOrder {
public void undo(int x) {
if (this.tabuList.size() == this.tabuSize) {
int var = this.tabuList.removeFirst();
this.decorated.undo(var);
int variable = this.tabuList.removeFirst();
this.decorated.undo(variable);
}
if (x == this.lastVar) {
this.tabuList.add(x);
......
......@@ -110,10 +110,10 @@ public class VarOrderHeap implements IOrder, Serializable {
*/
public int select() {
while (!this.heap.empty()) {
int var = this.heap.getmin();
int next = this.phaseStrategy.select(var);
int variable = this.heap.getmin();
int next = this.phaseStrategy.select(variable);
if (this.lits.isUnassigned(next)) {
if (this.activity[var] < 0.0001) {
if (this.activity[variable] < 0.0001) {
this.nullchoice++;
}
return next;
......@@ -155,16 +155,16 @@ public class VarOrderHeap implements IOrder, Serializable {
@Override
public void updateVar(int p, double value) {
int var = var(p);
updateActivity(var, value);
int variable = var(p);
updateActivity(variable, value);
this.phaseStrategy.updateVar(p);
if (this.heap.inHeap(var)) {
this.heap.increase(var);
if (this.heap.inHeap(variable)) {
this.heap.increase(variable);
}
}
protected void updateActivity(final int var, double inc) {
if ((this.activity[var] += (inc * varInc)) > VAR_RESCALE_BOUND) {
protected void updateActivity(final int variable, double inc) {
if ((this.activity[variable] += (inc * varInc)) > VAR_RESCALE_BOUND) {
varRescaleActivity();
}
}
......@@ -180,7 +180,7 @@ public class VarOrderHeap implements IOrder, Serializable {
*
*/
private void varRescaleActivity() {
for (int i = 1; i < this.activity.length; i++) {
for (var i = 1; i < this.activity.length; i++) {
this.activity[i] *= VAR_RESCALE_FACTOR;
}
this.varInc *= VAR_RESCALE_FACTOR;
......@@ -194,8 +194,8 @@ public class VarOrderHeap implements IOrder, Serializable {
*
*/
public int numberOfInterestingVariables() {
int cpt = 0;
for (int i = 1; i < this.activity.length; i++) {
var cpt = 0;
for (var i = 1; i < this.activity.length; i++) {
if (this.activity[i] > 1.0) {
cpt++;
}
......@@ -220,7 +220,7 @@ public class VarOrderHeap implements IOrder, Serializable {
this.activity[0] = -1;
this.heap = createHeap(this.activity);
this.heap.setBounds(nlength);
for (int i = 1; i < nlength; i++) {
for (var i = 1; i < nlength; i++) {
assert i > 0;
assert i <= this.lits.nVars() : "" + this.lits.nVars() + "/" + i; //$NON-NLS-1$ //$NON-NLS-2$
this.activity[i] = 0.0;
......
......@@ -69,6 +69,7 @@ public class EMARestarts implements RestartStrategy {
public void newConflict() {
}
@Override
public void init(SearchParams params, SolverStats stats) {
this.stats = stats;
reset();
......
......@@ -86,6 +86,7 @@ public class Glucose21Restarts implements RestartStrategy {
}
}
@Override
public void init(SearchParams params, SolverStats stats) {
this.stats = stats;
reset();
......
......@@ -47,7 +47,8 @@ public class ModelIteratorToSATAdapter extends ModelIterator {
private int[] lastModel = null;
private final SolutionFoundListener sfl;
public ModelIteratorToSATAdapter(ISolver solver, SolutionFoundListener sfl) {
public ModelIteratorToSATAdapter(ISolver solver,
SolutionFoundListener sfl) {
this(solver, Long.MAX_VALUE, sfl);
}
......@@ -59,7 +60,7 @@ public class ModelIteratorToSATAdapter extends ModelIterator {
@Override
public boolean isSatisfiable() throws TimeoutException {
boolean isSat = false;
var isSat = false;
while (super.isSatisfiable()) {
isSat = true;
lastModel = super.model();
......@@ -71,7 +72,7 @@ public class ModelIteratorToSATAdapter extends ModelIterator {
@Override
public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
boolean isSat = false;
var isSat = false;
while (super.isSatisfiable(assumps)) {
isSat = true;
lastModel = super.model();
......
......@@ -56,7 +56,7 @@ public class MultiTracing<T extends ISolverService>
*/
private static final long serialVersionUID = 1L;
private final Collection<SearchListener<T>> listeners = new ArrayList<SearchListener<T>>();
private final Collection<SearchListener<T>> listeners = new ArrayList<>();
public MultiTracing(SearchListener<T>... listeners) {
this.listeners.addAll(Arrays.asList(listeners));
......
......@@ -58,7 +58,7 @@ public class NegationDecorator<T extends ISolver>
*/
private static final long serialVersionUID = 1L;
private final Collection<Integer> addedVars = new ArrayList<Integer>();
private final Collection<Integer> addedVars = new ArrayList<>();
public NegationDecorator(T decorated) {
super(decorated);
......@@ -70,7 +70,7 @@ public class NegationDecorator<T extends ISolver>
int newVar = createNewVar(literals);
IVecInt clause = new VecInt(2);
clause.push(newVar);
ConstrGroup group = new ConstrGroup();
var group = new ConstrGroup();
for (IteratorInt it = literals.iterator(); it.hasNext();) {
clause.push(-it.next());
group.add(super.addClause(clause));
......@@ -102,8 +102,8 @@ public class NegationDecorator<T extends ISolver>
public boolean isSatisfiable(IVecInt assumps, boolean global)
throws TimeoutException {
IVecInt vars = new VecInt();
for (int var : getAddedVars()) {
vars.push(var);
for (int variable : getAddedVars()) {
vars.push(variable);
}
try {
IConstr constr = super.addClause(vars);
......
......@@ -49,8 +49,6 @@ public class OptToSatAdapter extends SolverDecorator<ISolver> {
private final IVecInt assumps = new VecInt();
private long begin;
private final SolutionFoundListener sfl;
public OptToSatAdapter(IOptimizationProblem problem) {
......@@ -84,7 +82,7 @@ public class OptToSatAdapter extends SolverDecorator<ISolver> {
public boolean isSatisfiable(IVecInt myAssumps) throws TimeoutException {
this.assumps.clear();
myAssumps.copyTo(this.assumps);
this.begin = System.currentTimeMillis();
long begin = System.currentTimeMillis();
if (this.problem.hasNoObjectiveFunction()) {
return this.problem.isSatisfiable(myAssumps);
}
......@@ -99,7 +97,7 @@ public class OptToSatAdapter extends SolverDecorator<ISolver> {
System.out.println(getLogPrefix()
+ "Current objective function value: "
+ this.problem.getObjectiveValue() + "("
+ (System.currentTimeMillis() - this.begin) / 1000.0
+ (System.currentTimeMillis() - begin) / 1000.0
+ "s)");
}
} while (this.problem.admitABetterSolution(myAssumps));
......@@ -108,8 +106,7 @@ public class OptToSatAdapter extends SolverDecorator<ISolver> {
} catch (TimeoutException e) {
if (isVerbose()) {
System.out.println(getLogPrefix() + "Solver timed out after "
+ (System.currentTimeMillis() - this.begin) / 1000.0
+ "s)");
+ (System.currentTimeMillis() - begin) / 1000.0 + "s)");
}
} catch (ContradictionException ce) {
expireTimeout();
......@@ -124,8 +121,8 @@ public class OptToSatAdapter extends SolverDecorator<ISolver> {
}
@Override
public boolean model(int var) {
return this.problem.model(var);
public boolean model(int variable) {
return this.problem.model(variable);
}
@Override
......
......@@ -16,6 +16,10 @@ import org.sat4j.specs.TimeoutException;
*/
public class ProblemDecorator<T extends IProblem> implements IProblem {
/**
*
*/
private static final long serialVersionUID = 1L;
private final T decorated;
public ProblemDecorator(T decorated) {
......@@ -23,8 +27,8 @@ public class ProblemDecorator<T extends IProblem> implements IProblem {
}
@Override
public boolean model(int var) {
return decorated.model(var);
public boolean model(int variable) {
return decorated.model(variable);
}
@Override
......@@ -90,6 +94,7 @@ public class ProblemDecorator<T extends IProblem> implements IProblem {
}
@Override
@Deprecated
public void printInfos(PrintWriter out, String prefix) {
decorated.printInfos(out, prefix);
}
......
Supports Markdown
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