Commit 88f87a1d authored by leberre's avatar leberre

Fixed a few sonar violations (final, hidden field, CONSTANT_NAMING)

git-svn-id: svn+ssh://svn.forge.ow2.org/svnroot/sat4j/maven/trunk@1706 ba638df5-4473-46d1-82f8-c3ae2a17a6e1
parent dffb6cb2
......@@ -79,7 +79,7 @@ import org.sat4j.pb.tools.ManyCorePB;
* @author leberre
* @since 2.0
*/
public class SolverFactory extends ASolverFactory<IPBSolver> {
public final class SolverFactory extends ASolverFactory<IPBSolver> {
/**
*
......
......@@ -15,90 +15,90 @@ import org.sat4j.specs.IVec;
import org.sat4j.specs.IVecInt;
public class JSONPBReader extends JSONReader<IPBSolver> {
public static final String WLITERAL = "\\[(-?\\d+),(-?\\d+)\\]";
public static final String WCLAUSE = "(\\[(" + WLITERAL + "(," + WLITERAL
+ ")*)?\\])";
public static final String PB = "(\\[" + WCLAUSE + ",'[=<>]=?',-?\\d+\\])";
public static final String WLITERAL = "\\[(-?\\d+),(-?\\d+)\\]";
public static final String WCLAUSE = "(\\[(" + WLITERAL + "(," + WLITERAL
+ ")*)?\\])";
public static final String PB = "(\\[" + WCLAUSE + ",'[=<>]=?',-?\\d+\\])";
public static final String OBJ = "(\\[('min'|'max')," + WCLAUSE + "\\])";
public static final String OBJ = "(\\[('min'|'max')," + WCLAUSE + "\\])";
public static final Pattern pseudo = Pattern.compile(PB);
public static final Pattern wclause = Pattern.compile(WCLAUSE);
public static final Pattern wliteral = Pattern.compile(WLITERAL);
public static final Pattern obj = Pattern.compile(OBJ);
public static final Pattern pseudo = Pattern.compile(PB);
public static final Pattern wclause = Pattern.compile(WCLAUSE);
public static final Pattern wliteral = Pattern.compile(WLITERAL);
public static final Pattern obj = Pattern.compile(OBJ);
public JSONPBReader(IPBSolver solver) {
super(solver);
}
public JSONPBReader(IPBSolver solver) {
super(solver);
}
@Override
protected void handleNotHandled(String constraint)
throws ParseFormatException, ContradictionException {
if (pseudo.matcher(constraint).matches()) {
handlePB(constraint);
} else if (obj.matcher(constraint).matches()) {
handleObj(constraint);
} else {
throw new UnsupportedOperationException("Wrong formula "
+ constraint);
}
}
@Override
protected void handleNotHandled(String constraint)
throws ParseFormatException, ContradictionException {
if (pseudo.matcher(constraint).matches()) {
handlePB(constraint);
} else if (obj.matcher(constraint).matches()) {
handleObj(constraint);
} else {
throw new UnsupportedOperationException("Wrong formula "
+ constraint);
}
}
private void handleObj(String constraint) {
Matcher matcher = wclause.matcher(constraint);
if (matcher.find()) {
String wclause = matcher.group();
constraint = matcher.replaceFirst("");
matcher = wliteral.matcher(wclause);
IVecInt literals = new VecInt();
String[] pieces = constraint.split(",");
boolean negate = pieces[0].contains("max");
IVec<BigInteger> coefs = new Vec<BigInteger>();
BigInteger coef;
while (matcher.find()) {
literals.push(Integer.valueOf(matcher.group(2)));
coef = new BigInteger(matcher.group(1));
coefs.push(negate ? coef.negate() : coef);
}
solver.setObjectiveFunction(new ObjectiveFunction(literals, coefs));
}
private void handleObj(String constraint) {
Matcher matcher = wclause.matcher(constraint);
if (matcher.find()) {
String weightedLiterals = matcher.group();
constraint = matcher.replaceFirst("");
matcher = wliteral.matcher(weightedLiterals);
IVecInt literals = new VecInt();
String[] pieces = constraint.split(",");
boolean negate = pieces[0].contains("max");
IVec<BigInteger> coefs = new Vec<BigInteger>();
BigInteger coef;
while (matcher.find()) {
literals.push(Integer.valueOf(matcher.group(2)));
coef = new BigInteger(matcher.group(1));
coefs.push(negate ? coef.negate() : coef);
}
solver.setObjectiveFunction(new ObjectiveFunction(literals, coefs));
}
}
}
private void handlePB(String constraint) throws ContradictionException {
Matcher matcher = wclause.matcher(constraint);
if (matcher.find()) {
String wclause = matcher.group();
constraint = matcher.replaceFirst("");
matcher = wliteral.matcher(wclause);
IVecInt literals = new VecInt();
IVecInt coefs = new VecInt();
while (matcher.find()) {
literals.push(Integer.valueOf(matcher.group(2)));
coefs.push(Integer.valueOf(matcher.group(1)));
}
String[] pieces = constraint.split(",");
String comp = pieces[1].substring(1, pieces[1].length() - 1);
int degree = Integer.valueOf(pieces[2].substring(0,
pieces[2].length() - 1));
if ("=".equals(comp) || "==".equals(comp)) {
solver.addExactly(literals, coefs, degree);
} else if ("<=".equals(comp)) {
solver.addAtMost(literals, coefs, degree);
} else if ("<".equals(comp)) {
solver.addAtMost(literals, coefs, degree - 1);
} else if (">=".equals(comp)) {
solver.addAtLeast(literals, coefs, degree);
} else {
assert ">".equals(comp);
solver.addAtLeast(literals, coefs, degree + 1);
}
}
private void handlePB(String constraint) throws ContradictionException {
Matcher matcher = wclause.matcher(constraint);
if (matcher.find()) {
String weightedLiterals = matcher.group();
constraint = matcher.replaceFirst("");
matcher = wliteral.matcher(weightedLiterals);
IVecInt literals = new VecInt();
IVecInt coefs = new VecInt();
while (matcher.find()) {
literals.push(Integer.valueOf(matcher.group(2)));
coefs.push(Integer.valueOf(matcher.group(1)));
}
String[] pieces = constraint.split(",");
String comp = pieces[1].substring(1, pieces[1].length() - 1);
int degree = Integer.valueOf(pieces[2].substring(0,
pieces[2].length() - 1));
if ("=".equals(comp) || "==".equals(comp)) {
solver.addExactly(literals, coefs, degree);
} else if ("<=".equals(comp)) {
solver.addAtMost(literals, coefs, degree);
} else if ("<".equals(comp)) {
solver.addAtMost(literals, coefs, degree - 1);
} else if (">=".equals(comp)) {
solver.addAtLeast(literals, coefs, degree);
} else {
assert ">".equals(comp);
solver.addAtLeast(literals, coefs, degree + 1);
}
}
}
}
@Override
protected String constraintPattern() {
return "(" + CLAUSE + "|" + CARD + "|" + PB + "|" + OBJ + ")";
}
@Override
protected String constraintPattern() {
return "(" + CLAUSE + "|" + CARD + "|" + PB + "|" + OBJ + ")";
}
}
......@@ -29,25 +29,25 @@
*******************************************************************************/
package org.sat4j.pb.tools;
public class StringNegator implements INegator {
public final class StringNegator implements INegator {
public static final INegator instance = new StringNegator();
public static final INegator INSTANCE = new StringNegator();
private StringNegator() {
// no access to constructor
}
private StringNegator() {
// no access to constructor
}
public boolean isNegated(Object thing) {
if (thing instanceof String) {
return ((String) thing).startsWith("-");
}
return false;
}
public boolean isNegated(Object thing) {
if (thing instanceof String) {
return ((String) thing).startsWith("-");
}
return false;
}
public Object unNegate(Object thing) {
if (isNegated(thing)) {
return ((String) thing).substring(1);
}
return thing;
}
public Object unNegate(Object thing) {
if (isNegated(thing)) {
return ((String) thing).substring(1);
}
return thing;
}
}
......@@ -32,77 +32,77 @@ package org.sat4j.pb.tools;
import java.math.BigInteger;
public class WeightedObject<T> implements Comparable<WeightedObject<T>> {
public final class WeightedObject<T> implements Comparable<WeightedObject<T>> {
public final T thing;
private BigInteger weight;
public final T thing;
private BigInteger weight;
private WeightedObject(T thing, BigInteger weight) {
this.thing = thing;
this.weight = weight;
}
private WeightedObject(T thing, BigInteger weight) {
this.thing = thing;
this.weight = weight;
}
public BigInteger getWeight() {
return this.weight;
}
public BigInteger getWeight() {
return this.weight;
}
public void increaseWeight(BigInteger delta) {
this.weight = this.weight.add(delta);
}
public void increaseWeight(BigInteger delta) {
this.weight = this.weight.add(delta);
}
public int compareTo(WeightedObject<T> arg0) {
return this.weight.compareTo(arg0.getWeight());
}
public int compareTo(WeightedObject<T> arg0) {
return this.weight.compareTo(arg0.getWeight());
}
public static <E> WeightedObject<E> newWO(E e, int w) {
return new WeightedObject<E>(e, BigInteger.valueOf(w));
}
public static <E> WeightedObject<E> newWO(E e, int w) {
return new WeightedObject<E>(e, BigInteger.valueOf(w));
}
public static <E> WeightedObject<E> newWO(E e, long w) {
return new WeightedObject<E>(e, BigInteger.valueOf(w));
}
public static <E> WeightedObject<E> newWO(E e, long w) {
return new WeightedObject<E>(e, BigInteger.valueOf(w));
}
public static <E> WeightedObject<E> newWO(E e, BigInteger w) {
return new WeightedObject<E>(e, w);
}
public static <E> WeightedObject<E> newWO(E e, BigInteger w) {
return new WeightedObject<E>(e, w);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ (this.thing == null ? 0 : this.thing.hashCode());
result = prime * result
+ (this.weight == null ? 0 : this.weight.hashCode());
return result;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ (this.thing == null ? 0 : this.thing.hashCode());
result = prime * result
+ (this.weight == null ? 0 : this.weight.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
WeightedObject<?> other = (WeightedObject<?>) obj;
if (this.thing == null) {
if (other.thing != null) {
return false;
}
} else if (!this.thing.equals(other.thing)) {
return false;
}
if (this.weight == null) {
if (other.weight != null) {
return false;
}
} else if (!this.weight.equals(other.weight)) {
return false;
}
return true;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
WeightedObject<?> other = (WeightedObject<?>) obj;
if (this.thing == null) {
if (other.thing != null) {
return false;
}
} else if (!this.thing.equals(other.thing)) {
return false;
}
if (this.weight == null) {
if (other.weight != null) {
return false;
}
} else if (!this.weight.equals(other.weight)) {
return false;
}
return true;
}
}
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