001package org.cpsolver.ifs.util;
002
003import org.apache.log4j.Logger;
004import org.cpsolver.ifs.assignment.Assignment;
005import org.cpsolver.ifs.model.Model;
006import org.cpsolver.ifs.model.Value;
007import org.cpsolver.ifs.model.Variable;
008import org.cpsolver.ifs.solution.Solution;
009import org.cpsolver.ifs.solver.Solver;
010
011/**
012 * Abstract problem saver class.
013 * 
014 * @version IFS 1.3 (Iterative Forward Search)<br>
015 *          Copyright (C) 2006 - 2016 Tomas Muller<br>
016 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
017 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
018 * <br>
019 *          This library is free software; you can redistribute it and/or modify
020 *          it under the terms of the GNU Lesser General Public License as
021 *          published by the Free Software Foundation; either version 3 of the
022 *          License, or (at your option) any later version. <br>
023 * <br>
024 *          This library is distributed in the hope that it will be useful, but
025 *          WITHOUT ANY WARRANTY; without even the implied warranty of
026 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
027 *          Lesser General Public License for more details. <br>
028 * <br>
029 *          You should have received a copy of the GNU Lesser General Public
030 *          License along with this library; if not see
031 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
032 */
033public abstract class ProblemSaver<V extends Variable<V, T>, T extends Value<V, T>, M extends Model<V,T>> implements Runnable {
034    private Solver<V, T> iSolver = null;
035    private Callback iCallback = null;
036
037    /**
038     * Constructor
039     * @param solver current solver
040     */
041    public ProblemSaver(Solver<V, T> solver) {
042        iSolver = solver;
043    }
044
045    /** Solver 
046     * @return current solver
047     **/
048    public Solver<V, T> getSolver() {
049        return iSolver;
050    }
051    
052    /** Solution to be saved 
053     * @return current solution
054     **/
055    protected Solution<V, T> getSolution() {
056        return iSolver.currentSolution();
057    }
058
059    /** Model of the solution 
060     * @return problem model
061     **/
062    @SuppressWarnings("unchecked")
063    public M getModel() {
064        return (M)iSolver.currentSolution().getModel();
065    }
066
067    /** Current assignment 
068     * @return current assignment
069     **/
070    public Assignment<V, T> getAssignment() {
071        return getSolution().getAssignment();
072    }
073
074    /** Save the solution 
075     * @throws Exception thrown when save fails
076     **/
077    public abstract void save() throws Exception;
078
079    /**
080     * Sets callback class
081     * 
082     * @param callback
083     *            method {@link Callback#execute()} is executed when save is
084     *            done
085     */
086    public void setCallback(Callback callback) {
087        iCallback = callback;
088    }
089
090    @Override
091    public void run() {
092        try {
093            save();
094        } catch (Exception e) {
095            Logger.getLogger(this.getClass()).error(e.getMessage(), e);
096        } finally {
097            if (iCallback != null)
098                iCallback.execute();
099        }
100    }
101}