001package org.cpsolver.ifs.extension;
002
003import org.cpsolver.ifs.assignment.Assignment;
004import org.cpsolver.ifs.model.Constraint;
005import org.cpsolver.ifs.model.Model;
006import org.cpsolver.ifs.model.ModelListener;
007import org.cpsolver.ifs.model.Value;
008import org.cpsolver.ifs.model.Variable;
009import org.cpsolver.ifs.solver.Solver;
010import org.cpsolver.ifs.util.DataProperties;
011
012/**
013 * Generic extension of IFS solver. <br>
014 * <br>
015 * All extensions should extend this class. <br>
016 * 
017 * @author  Tomáš Müller
018 * @version IFS 1.3 (Iterative Forward Search)<br>
019 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
020 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
021 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
022 * <br>
023 *          This library is free software; you can redistribute it and/or modify
024 *          it under the terms of the GNU Lesser General Public License as
025 *          published by the Free Software Foundation; either version 3 of the
026 *          License, or (at your option) any later version. <br>
027 * <br>
028 *          This library is distributed in the hope that it will be useful, but
029 *          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. <br>
032 * <br>
033 *          You should have received a copy of the GNU Lesser General Public
034 *          License along with this library; if not see
035 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
036 * @param <V> Variable
037 * @param <T> Value
038 */
039public class Extension<V extends Variable<V, T>, T extends Value<V, T>> implements ModelListener<V, T> {
040    private Model<V, T> iModel = null;
041    private Solver<V, T> iSolver = null;
042    private DataProperties iProperties = null;
043
044    /**
045     * Constructor
046     * 
047     * @param solver
048     *            IFS solver
049     * @param properties
050     *            input configuration
051     */
052    public Extension(Solver<V, T> solver, DataProperties properties) {
053        iSolver = solver;
054        iProperties = properties;
055    }
056
057    /** Registration of a model. This is called by the solver before start. 
058     * @param model problem model
059     **/
060    public void register(Model<V, T> model) {
061        iModel = model;
062        iModel.addModelListener(this);
063    }
064
065    /**
066     * Unregistration of a model. This is called by the solver when extension is
067     * removed.
068     * @param model problem model
069     */
070    public void unregister(Model<V, T> model) {
071        iModel.removeModelListener(this);
072        iModel = null;
073    }
074
075    /**
076     * Returns true if there is a model registered to this extension, i.e., when
077     * extension is registered.
078     * @return true if registered with a solver
079     */
080    public boolean isRegistered() {
081        return iModel != null;
082    }
083
084    /** Returns the model
085     * @return problem model 
086     **/
087    public Model<V, T> getModel() {
088        return iModel;
089    }
090
091    /** Returns the solver 
092     * @return current solver
093     **/
094    public Solver<V, T> getSolver() {
095        return iSolver;
096    }
097
098    /** Returns input configuration
099     * @return solver configuration
100     **/
101    public DataProperties getProperties() {
102        return iProperties;
103    }
104
105    /** Called after a value is assigned to a variable */
106    @Override
107    public void afterAssigned(Assignment<V, T> assignment, long iteration, T value) {
108    }
109
110    /** Called after a value is unassigned from a variable */
111    @Override
112    public void afterUnassigned(Assignment<V, T> assignment, long iteration, T value) {
113    }
114
115    /** Called before a value is assigned to a variable */
116    @Override
117    public void beforeAssigned(Assignment<V, T> assignment, long iteration, T value) {
118    }
119
120    /** Called after a value is unassigned from a variable */
121    @Override
122    public void beforeUnassigned(Assignment<V, T> assignment, long iteration, T value) {
123    }
124
125    /** Called when a constraint is added to the model */
126    @Override
127    public void constraintAdded(Constraint<V, T> constraint) {
128    }
129
130    /** Called when a constraint is removed from the model */
131    @Override
132    public void constraintRemoved(Constraint<V, T> constraint) {
133    }
134
135    /** Called when a variable is added to the model */
136    @Override
137    public void variableAdded(V variable) {
138    }
139
140    /** Called when a variable is removed from the model */
141    @Override
142    public void variableRemoved(V variable) {
143    }
144
145    /** Initialization -- called before the solver is started */
146    @Override
147    public boolean init(Solver<V, T> solver) {
148        return true;
149    }
150}