org.chocosolver.solver.variables.view.IntBoolView Maven / Gradle / Ivy
Show all versions of choco-solver Show documentation
/*
* This file is part of choco-solver, http://choco-solver.org/
*
* Copyright (c) 2019, IMT Atlantique. All rights reserved.
*
* Licensed under the BSD 4-clause license.
*
* See LICENSE file in the project root for full license information.
*/
package org.chocosolver.solver.variables.view;
import org.chocosolver.solver.ICause;
import org.chocosolver.solver.exception.ContradictionException;
import org.chocosolver.solver.variables.BoolVar;
import org.chocosolver.solver.variables.IntVar;
import org.chocosolver.solver.variables.Variable;
import org.chocosolver.solver.variables.delta.IEnumDelta;
import org.chocosolver.solver.variables.delta.IIntDeltaMonitor;
import org.chocosolver.solver.variables.delta.NoDelta;
import org.chocosolver.solver.variables.delta.OneValueDelta;
import org.chocosolver.solver.variables.delta.monitor.OneValueDeltaMonitor;
import org.chocosolver.solver.variables.impl.AbstractVariable;
import org.chocosolver.solver.variables.impl.scheduler.BoolEvtScheduler;
import org.chocosolver.util.iterators.DisposableRangeBoundIterator;
import org.chocosolver.util.iterators.DisposableRangeIterator;
import org.chocosolver.util.iterators.DisposableValueBoundIterator;
import org.chocosolver.util.iterators.DisposableValueIterator;
import org.chocosolver.util.iterators.EvtScheduler;
import org.chocosolver.util.objects.setDataStructures.iterable.IntIterableSet;
/**
* An abstract class for boolean views over {@link org.chocosolver.solver.variables.IntVar}.
*
* @see EqView
* @see LeqView
*
* Project: choco-solver.
* @author Charles Prud'homme
* @since 04/12/2018.
*/
public abstract class IntBoolView extends IntView implements BoolVar{
/**
* A constant value
*/
public final int cste;
/**
* Associate boolean variable expressing not(this)
*/
private BoolVar not;
/**
* For boolean expression purpose
*/
private boolean isNot = false;
/**
* To iterate over removed values
*/
IEnumDelta delta = NoDelta.singleton;
/**
* Set to true if this variable reacts is associated with at least one propagator which
* reacts on value removal
*/
boolean reactOnRemoval = false;
/**
* A view based on var and a constant
*
* @param var an integer variable
* @param cste an int
*/
IntBoolView(final IntVar var, String op, final int cste) {
super("(" + var.getName() + op + cste + ")", var);
this.cste = cste;
}
@Override
public final boolean setToTrue(ICause cause) throws ContradictionException {
return instantiateTo(1, cause);
}
@Override
public final boolean setToFalse(ICause cause) throws ContradictionException {
return instantiateTo(0, cause);
}
@Override
public final boolean removeValue(int value, ICause cause) throws ContradictionException {
assert cause != null;
if (value == kFALSE)
return instantiateTo(kTRUE, cause);
else if (value == kTRUE)
return instantiateTo(kFALSE, cause);
return false;
}
@Override
public final boolean removeValues(IntIterableSet values, ICause cause) throws ContradictionException {
boolean hasChanged = false;
if (values.contains(kFALSE)) {
hasChanged = instantiateTo(kTRUE, cause);
}
if (values.contains(kTRUE)) {
hasChanged = instantiateTo(kFALSE, cause);
}
return hasChanged;
}
@Override
public final boolean removeAllValuesBut(IntIterableSet values, ICause cause) throws ContradictionException {
boolean hasChanged = false;
if (!values.contains(kFALSE)) {
hasChanged = instantiateTo(kTRUE, cause);
}
if (!values.contains(kTRUE)) {
hasChanged = instantiateTo(kFALSE, cause);
}
return hasChanged;
}
@Override
public final boolean removeInterval(int from, int to, ICause cause) throws ContradictionException {
boolean hasChanged = false;
if (from <= to && from <= 1 && to >= 0) {
if (from == kTRUE) {
hasChanged = instantiateTo(kFALSE, cause);
} else if (to == kFALSE) {
hasChanged = instantiateTo(kTRUE, cause);
} else {
model.getSolver().getEventObserver().instantiateTo(this, 2, cause, kFALSE, kTRUE);
this.contradiction(cause, AbstractVariable.MSG_EMPTY);
}
}
return hasChanged;
}
@Override
public final boolean updateLowerBound(int value, ICause cause) throws ContradictionException {
assert cause != null;
return value > kFALSE && instantiateTo(value, cause);
}
@Override
public final boolean updateUpperBound(int value, ICause cause) throws ContradictionException {
assert cause != null;
return value < kTRUE && instantiateTo(value, cause);
}
@Override
public final boolean updateBounds(int lb, int ub, ICause cause) throws ContradictionException {
boolean hasChanged = false;
if (lb > kTRUE || ub < kFALSE) {
model.getSolver().getEventObserver().instantiateTo(this, 2, cause, kFALSE, kTRUE);
this.contradiction(cause, MSG_EMPTY);
} else {
if (lb == kTRUE) {
hasChanged = instantiateTo(kTRUE, cause);
} else if (ub == kFALSE) {
hasChanged = instantiateTo(kFALSE, cause);
}
}
return hasChanged;
}
@Override
public final int getTypeAndKind() {
return Variable.VIEW | Variable.BOOL;
}
@Override
public final int getValue() {
return getLB();
}
@Override
protected final EvtScheduler createScheduler() {
return new BoolEvtScheduler();
}
@Override
public final String toString() {
if (isInstantiated()) {
return this.name + " = " + this.getValue();
} else {
return this.name + " = " + "[0,1]";
}
}
@Override
public final DisposableValueIterator getValueIterator(boolean bottomUp) {
if (_viterator == null || _viterator.isNotReusable()) {
_viterator = new DisposableValueBoundIterator(this);
}
if (bottomUp) {
_viterator.bottomUpInit();
} else {
_viterator.topDownInit();
}
return _viterator;
}
@Override
public final DisposableRangeIterator getRangeIterator(boolean bottomUp) {
if (_riterator == null || _riterator.isNotReusable()) {
_riterator = new DisposableRangeBoundIterator(this);
}
if (bottomUp) {
_riterator.bottomUpInit();
} else {
_riterator.topDownInit();
}
return _riterator;
}
@Override
public final void createDelta() {
if (!reactOnRemoval) {
delta = new OneValueDelta(model.getEnvironment());
reactOnRemoval = true;
}
}
@SuppressWarnings("unchecked")
@Override
public final IIntDeltaMonitor monitorDelta(ICause propagator) {
createDelta();
return new OneValueDeltaMonitor(delta, propagator);
}
@Override
public final void _setNot(BoolVar neg) {
this.not = neg;
}
@Override
public final BoolVar not() {
if (!hasNot()) {
not = model.boolNotView(this);
not._setNot(this);
}
return not;
}
@Override
public final boolean hasNot() {
return not != null;
}
@Override
public final boolean isLit() {
return true;
}
@Override
public final boolean isNot() {
return isNot;
}
@Override
public final void setNot(boolean isNot) {
this.isNot = isNot;
}
}