Skip to content

Commit

Permalink
Calling getValue() on an uninstantiated variable throws an IllegalS…
Browse files Browse the repository at this point in the history
…tateException (#957)

+ fix warnings
  • Loading branch information
cprudhom authored Oct 26, 2022
1 parent ae01ac8 commit d424fa6
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,12 @@ default boolean updateUpperBound(long value, ICause cause) throws ContradictionE
boolean isInstantiatedTo(int value);

/**
* Retrieves the current value of the variable if instantiated, otherwier the lower bound.
* Retrieves the current value of the variable if instantiated
*
* @return the current value (or lower bound if not yet instantiated).
* @return the current value
* @throws IllegalStateException when the variable is not instantiated
*/
int getValue();
int getValue() throws IllegalStateException;

/**
* Retrieves the lower bound of the variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,14 +589,12 @@ public boolean contains(int aValue) {
return false;
}

/**
* Retrieves the current value of the variable if instantiated, otherwier the lower bound.
*
* @return the current value (or lower bound if not yet instantiated).
*/
@Override
public int getValue() {
assert isInstantiated() : name + " not instantiated";
public int getValue() throws IllegalStateException{
if(!isInstantiated()){
throw new IllegalStateException("getValue() can be only called on instantiated variable. " +
name + " is not instantiated");
}
return getLB();
}

Expand Down Expand Up @@ -766,7 +764,7 @@ public int getTypeAndKind() {
}

@Override
protected EvtScheduler createScheduler() {
protected EvtScheduler<IntEventType> createScheduler() {
return new IntEvtScheduler();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public final class BitsetIntVarImpl extends AbstractVariable implements IntVar {
/**
* Signed Literal
*/
protected SignedLiteral.Set literal;
private SignedLiteral.Set literal;

/**
* Create an enumerated IntVar based on a bitset
Expand Down Expand Up @@ -564,14 +564,12 @@ public boolean contains(int aValue) {
return LB.get() <= aValue && aValue <= UB.get() && this.VALUES.get(aValue);
}

/**
* Retrieves the current value of the variable if instantiated, otherwier the lower bound.
*
* @return the current value (or lower bound if not yet instantiated).
*/
@Override
public int getValue() {
assert isInstantiated() : name + " not instantiated";
public int getValue() throws IllegalStateException {
if (!isInstantiated()) {
throw new IllegalStateException("getValue() can be only called on instantiated variable. " +
name + " is not instantiated");
}
return getLB();
}

Expand Down Expand Up @@ -691,7 +689,6 @@ public void createDelta() {
}
}

@SuppressWarnings("unchecked")
@Override
public IIntDeltaMonitor monitorDelta(ICause propagator) {
createDelta();
Expand All @@ -706,7 +703,7 @@ public int getTypeAndKind() {
}

@Override
protected EvtScheduler createScheduler() {
protected EvtScheduler<IntEventType> createScheduler() {
return new IntEvtScheduler();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,12 @@ public boolean contains(int aValue) {
return aValue == kFALSE || aValue == kTRUE;
}

/**
* Retrieves the current value of the variable if instantiated, otherwier the lower bound.
*
* @return the current value (or lower bound if not yet instantiated).
*/
@Override
public int getValue() {
assert isInstantiated() : name + " not instantiated";
public int getValue() throws IllegalStateException{
if (!isInstantiated()) {
throw new IllegalStateException("getValue() can be only called on instantiated variable. " +
name + " is not instantiated");
}
return getLB();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.chocosolver.solver.variables.delta.IIntDeltaMonitor;
import org.chocosolver.solver.variables.delta.NoDelta;
import org.chocosolver.solver.variables.events.IEventType;
import org.chocosolver.solver.variables.events.IntEventType;
import org.chocosolver.solver.variables.impl.scheduler.IntEvtScheduler;
import org.chocosolver.solver.variables.impl.siglit.SignedLiteral;
import org.chocosolver.solver.variables.view.IView;
Expand Down Expand Up @@ -173,7 +174,7 @@ public boolean isInstantiatedTo(int value) {
}

@Override
public int getValue() {
public int getValue() throws IllegalStateException{
return constante;
}

Expand Down Expand Up @@ -249,19 +250,18 @@ public boolean isInstantiated() {
}

@Override//void (a constant receives no event)
public void addMonitor(IVariableMonitor monitor) {
public void addMonitor(IVariableMonitor<?> monitor) {
}

@Override//void (a constant receives no event)
public void removeMonitor(IVariableMonitor monitor) {
public void removeMonitor(IVariableMonitor<?> monitor) {
}

@Override//void (a constant receives no event)
public void subscribeView(IView view, int idx) {
public void subscribeView(IView<?> view, int idx) {
}


@SuppressWarnings("unchecked")
@Override
public IIntDeltaMonitor monitorDelta(ICause propagator) {
return IIntDeltaMonitor.Default.NONE;
Expand All @@ -276,11 +276,11 @@ public void notifyPropagators(IEventType event, ICause cause) throws Contradicti
}

@Override//void (a constant receives no event)
public void notifyMonitors(IEventType event) throws ContradictionException {
public void notifyMonitors(IEventType event) {
}

@Override//void (a constant receives no event)
public void notifyViews(IEventType event, ICause cause) throws ContradictionException {
public void notifyViews(IEventType event, ICause cause) {
}

@Override
Expand All @@ -294,7 +294,7 @@ public int getTypeAndKind() {
}

@Override
protected EvtScheduler createScheduler() {
protected EvtScheduler<IntEventType> createScheduler() {
return new IntEvtScheduler();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public final class IntervalIntVarImpl extends AbstractVariable implements IntVar
/**
* Signed Literal
*/
protected SignedLiteral.Set literal;
private SignedLiteral.Set literal;

/**
* Create a bounded domain IntVar : [min,max]
Expand Down Expand Up @@ -373,14 +373,12 @@ public boolean contains(int aValue) {
return ((aValue >= LB.get()) && (aValue <= UB.get()));
}

/**
* Retrieves the current value of the variable if instantiated, otherwier the lower bound.
*
* @return the current value (or lower bound if not yet instantiated).
*/
@Override
public int getValue() {
assert isInstantiated() : name + " not instantiated";
public int getValue() throws IllegalStateException{
if(!isInstantiated()) {
throw new IllegalStateException("getValue() can be only called on instantiated variable. " +
name + " is not instantiated");
}
return getLB();
}

Expand Down Expand Up @@ -489,7 +487,6 @@ public void createDelta() {
}
}

@SuppressWarnings("unchecked")
@Override
public IIntDeltaMonitor monitorDelta(ICause propagator) {
createDelta();
Expand All @@ -503,7 +500,7 @@ public int getTypeAndKind() {
}

@Override
protected EvtScheduler createScheduler() {
protected EvtScheduler<IntEventType> createScheduler() {
return new IntEvtScheduler();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.chocosolver.solver.variables.delta.*;
import org.chocosolver.solver.variables.delta.monitor.OneValueDeltaMonitor;
import org.chocosolver.solver.variables.events.IEventType;
import org.chocosolver.solver.variables.events.IntEventType;
import org.chocosolver.solver.variables.impl.AbstractVariable;
import org.chocosolver.solver.variables.impl.scheduler.BoolEvtScheduler;
import org.chocosolver.solver.variables.view.bool.BoolEqView;
Expand Down Expand Up @@ -173,12 +174,16 @@ public final int getTypeAndKind() {
}

@Override
public final int getValue() {
public final int getValue() throws IllegalStateException{
if(!isInstantiated()) {
throw new IllegalStateException("getValue() can be only called on instantiated variable. " +
name + " is not instantiated");
}
return getLB();
}

@Override
protected final EvtScheduler createScheduler() {
protected final EvtScheduler<IntEventType> createScheduler() {
return new BoolEvtScheduler();
}

Expand Down Expand Up @@ -230,7 +235,6 @@ public IDelta getDelta() {
return delta;
}

@SuppressWarnings("unchecked")
@Override
public final IIntDeltaMonitor monitorDelta(ICause propagator) {
createDelta();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ public boolean isInstantiatedTo(int value) {
}

@Override
public int getValue() {
public int getValue() throws IllegalStateException{
if(!isInstantiated()){
throw new IllegalStateException("getValue() can be only called on instantiated variable. " +
name + " is not instantiated");
}
int v = var.getValue();
return 1 - v;
}
Expand Down Expand Up @@ -224,7 +228,7 @@ protected int transform(int value) {
}

@Override
protected EvtScheduler createScheduler() {
protected EvtScheduler<IntEventType> createScheduler() {
return new BoolEvtScheduler();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

/**
* Boolean view b over a set variable S:
*
* With v an integer, b = true iff S contains v.
*
* @author Dimitri Justeau-Allaire
Expand Down Expand Up @@ -215,7 +214,11 @@ public final int getTypeAndKind() {
}

@Override
public final int getValue() {
public final int getValue() throws IllegalStateException{
if (!isInstantiated()) {
throw new IllegalStateException("getValue() can be only called on instantiated variable. " +
name + " is not instantiated");
}
return getLB();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ public boolean isInstantiatedTo(int value) {
}

@Override
public int getValue() {
public int getValue() throws IllegalStateException{
if(!isInstantiated()) {
throw new IllegalStateException("getValue() can be only called on instantiated variable. " +
name + " is not instantiated");
}
return -var.getValue();
}

Expand Down Expand Up @@ -176,7 +180,7 @@ public int previousValueOut(int v) {
}

@Override
protected EvtScheduler createScheduler() {
protected EvtScheduler<IntEventType> createScheduler() {
return new IntEvtScheduler();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ public boolean isInstantiatedTo(int value) {
}

@Override
public int getValue() {
public int getValue() throws IllegalStateException{
if (!isInstantiated()) {
throw new IllegalStateException("getValue() can be only called on instantiated variable. " +
name + " is not instantiated");
}
return var.getValue() + cste;
}

Expand Down Expand Up @@ -145,7 +149,7 @@ public int previousValueOut(int v) {
}

@Override
protected EvtScheduler createScheduler() {
protected EvtScheduler<IntEventType> createScheduler() {
return new IntEvtScheduler();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ public boolean isInstantiatedTo(int value) {
}

@Override
public int getValue() {
public int getValue() throws IllegalStateException{
if (!isInstantiated()) {
throw new IllegalStateException("getValue() can be only called on instantiated variable. " +
name + " is not instantiated");
}
return var.getValue() * cste;
}

Expand Down

0 comments on commit d424fa6

Please sign in to comment.