001 package ifs.extension;
002
003 import ifs.model.*;
004 import ifs.solver.*;
005 import ifs.util.*;
006
007 /**
008 * Generic extension of IFS solver.
009 * <br><br>
010 * All extensions should extend this class.
011 * <br><br>
012 * An extension may use extra information associated with a variable or a value
013 * (see {@link Variable#setExtra(Object)}, {@link Variable#getExtra()}, {@link Value#setExtra(Object)}, {@link Value#getExtra()})
014 * but there can be only one extension using these extra objects used during the search.
015 * For instance, {@link MacPropagation} is using these extra objects to memorize explanations.
016 *
017 * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
018 * @version 1.0
019 */
020 public class Extension implements ModelListener {
021 private Model iModel = null;
022 private Solver iSolver = null;
023 private DataProperties iProperties = null;
024
025 /** Constructor
026 * @param solver IFS solver
027 * @param properties input configuration
028 */
029 public Extension(Solver solver, DataProperties properties) {
030 iSolver = solver;
031 iProperties = properties;
032 }
033
034 /** Registration of a model. This is called by the solver before start. */
035 public void register(Model model) {
036 iModel = model;
037 iModel.addModelListener(this);
038 }
039
040 /** Unregistration of a model. This is called by the solver when extension is removed. */
041 public void unregister(Model model) {
042 iModel.removeModelListener(this);
043 iModel = null;
044 }
045
046 /** Returns true if there is a model registered to this extension, i.e., when extension is registered. */
047 public boolean isRegistered() {
048 return iModel != null;
049 }
050
051 /** Returns the model */
052 public Model getModel() {
053 return iModel;
054 }
055
056 /** Returns the solver */
057 public Solver getSolver() {
058 return iSolver;
059 }
060
061 /** Returns input configuration */
062 public DataProperties getProperties() {
063 return iProperties;
064 }
065
066 /** Called after a value is assigned to a variable */
067 public void afterAssigned(long iteration, Value value) {
068 }
069 /** Called after a value is unassigned from a variable */
070 public void afterUnassigned(long iteration, Value value) {
071 }
072 /** Called before a value is assigned to a variable */
073 public void beforeAssigned(long iteration, Value value) {
074 }
075 /** Called after a value is unassigned from a variable */
076 public void beforeUnassigned(long iteration, Value value) {
077 }
078 /** Called when a constraint is added to the model */
079 public void constraintAdded(Constraint constraint) {
080 }
081 /** Called when a constraint is removed from the model */
082 public void constraintRemoved(Constraint constraint) {
083 }
084 /** Called when a variable is added to the model */
085 public void variableAdded(Variable variable) {
086 }
087 /** Called when a variable is removed from the model */
088 public void variableRemoved(Variable variable) {
089 }
090
091 /** Put some information from the extension to the solution info table*/
092 public void getInfo(java.util.Hashtable anInfo) {
093 }
094
095 /** Initialization -- called before the solver is started */
096 public boolean init(Solver solver) {
097 return true;
098 }
099
100 /** Should return true when {@link Value#setExtra(Object)}, {@link Value#getExtra()} are used by the extension */
101 public boolean useValueExtra() {
102 return false;
103 }
104
105 /** Should return true when {@link Variable#setExtra(Object)}, {@link Variable#getExtra()} are used by the extension */
106 public boolean useVariableExtra() {
107 return false;
108 }
109 }