aima.core.search.csp.DomainRestoreInfo Maven / Gradle / Ivy
package aima.core.search.csp;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import aima.core.util.datastructure.Pair;
/**
* Provides informations which might be useful for a caller of a constraint
* propagation algorithm. It maintains old domains for variables and provides
* means to restore the initial state of the CSP (before domain reduction
* started). Additionally, a flag indicates whether an empty domain has been
* found during propagation.
*
* @author Ruediger Lunde
*
*/
public class DomainRestoreInfo {
private List> savedDomains;
private HashSet affectedVariables;
private boolean emptyDomainObserved;
public DomainRestoreInfo() {
savedDomains = new ArrayList>();
affectedVariables = new HashSet();
}
public void clear() {
savedDomains.clear();
affectedVariables.clear();
}
public boolean isEmpty() {
return savedDomains.isEmpty();
}
/**
* Stores the specified domain for the specified variable if a domain has
* not yet been stored for the variable.
*/
public void storeDomainFor(Variable var, Domain domain) {
if (!affectedVariables.contains(var)) {
savedDomains.add(new Pair(var, domain));
affectedVariables.add(var);
}
}
public void setEmptyDomainFound(boolean b) {
emptyDomainObserved = b;
}
/**
* Can be called after all domain information has been collected to reduce
* storage consumption.
*
* @return this object, after removing one hashtable.
*/
public DomainRestoreInfo compactify() {
affectedVariables = null;
return this;
}
public boolean isEmptyDomainFound() {
return emptyDomainObserved;
}
public List> getSavedDomains() {
return savedDomains;
}
public void restoreDomains(CSP csp) {
for (Pair pair : getSavedDomains())
csp.setDomain(pair.getFirst(), pair.getSecond());
}
public String toString() {
StringBuffer result = new StringBuffer();
for (Pair pair : savedDomains)
result.append(pair.getFirst() + "=" + pair.getSecond() + " ");
if (emptyDomainObserved)
result.append("!");
return result.toString();
}
}