001    package net.sf.cpsolver.ifs.extension;
002    
003    import net.sf.cpsolver.ifs.model.*;
004    import net.sf.cpsolver.ifs.solver.*;
005    import net.sf.cpsolver.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     * @version
018     * IFS 1.1 (Iterative Forward Search)<br>
019     * Copyright (C) 2006 Tomáš Müller<br>
020     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
021     * Lazenska 391, 76314 Zlin, Czech Republic<br>
022     * <br>
023     * This library is free software; you can redistribute it and/or
024     * modify it under the terms of the GNU Lesser General Public
025     * License as published by the Free Software Foundation; either
026     * version 2.1 of the License, or (at your option) any later version.
027     * <br><br>
028     * This library is distributed in the hope that it will be useful,
029     * but WITHOUT ANY WARRANTY; without even the implied warranty of
030     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
031     * Lesser General Public License for more details.
032     * <br><br>
033     * You should have received a copy of the GNU Lesser General Public
034     * License along with this library; if not, write to the Free Software
035     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
036     */
037    public class Extension implements ModelListener {
038        private Model iModel = null;
039        private Solver iSolver = null;
040        private DataProperties iProperties = null;
041        
042        /** Constructor
043         * @param solver IFS solver
044         * @param properties input configuration
045         */
046        public Extension(Solver solver, DataProperties properties) {
047            iSolver = solver;
048            iProperties = properties;
049        }
050        
051        /** Registration of a model. This is called by the solver before start. */
052        public void register(Model model) {
053            iModel = model;
054            iModel.addModelListener(this);
055        }
056        
057        /** Unregistration of a model. This is called by the solver when extension is removed. */
058        public void unregister(Model model) {
059            iModel.removeModelListener(this);
060            iModel = null;
061        }
062        
063        /** Returns true if there is a model registered to this extension, i.e., when extension is registered. */
064        public boolean isRegistered() {
065            return iModel != null;
066        }
067        
068        /** Returns the model */
069        public Model getModel() {
070            return iModel;
071        }
072        
073        /** Returns the solver */
074        public Solver getSolver() {
075            return iSolver;
076        }
077        
078        /** Returns input configuration */
079        public DataProperties getProperties() {
080            return iProperties;
081        }
082        
083        /** Called after a value is assigned to a variable */
084        public void afterAssigned(long iteration, Value value) {
085        }
086        /** Called after a value is unassigned from a variable */
087        public void afterUnassigned(long iteration, Value value) {
088        }
089        /** Called before a value is assigned to a variable */
090        public void beforeAssigned(long iteration, Value value) {
091        }
092        /** Called after a value is unassigned from a variable */
093        public void beforeUnassigned(long iteration, Value value) {
094        }
095        /** Called when a constraint is added to the model */
096        public void constraintAdded(Constraint constraint) {
097        }
098        /** Called when a constraint is removed from the model */
099        public void constraintRemoved(Constraint constraint) {
100        }
101        /** Called when a variable is added to the model */
102        public void variableAdded(Variable variable) {
103        }
104        /** Called when a variable is removed from the model */
105        public void variableRemoved(Variable variable) {
106        }
107        
108        /** Initialization -- called before the solver is started */
109        public boolean init(Solver solver) {
110            return true;
111        }
112        
113        /** Should return true when {@link Value#setExtra(Object)}, {@link Value#getExtra()} are used by the extension */
114        public boolean useValueExtra() {
115            return false;
116        }
117        
118        /** Should return true when {@link Variable#setExtra(Object)}, {@link Variable#getExtra()} are used by the extension */
119        public boolean useVariableExtra() {
120            return false;
121        }
122    }