    001package org.cpsolver.ifs.util;
    002
    003import org.cpsolver.ifs.assignment.Assignment;
    004import org.cpsolver.ifs.model.Model;
    005import org.cpsolver.ifs.model.Value;
    006import org.cpsolver.ifs.model.Variable;
    007import org.cpsolver.ifs.solution.Solution;
    008import org.cpsolver.ifs.solver.Solver;
    009import org.cpsolver.ifs.termination.TerminationCondition;
    010
    011/**
    012 * Abstract problem saver class.
    013 *
    014 * @version IFS 1.3 (Iterative Forward Search)<br>
    015 *          Copyright (C) 2006 - 2016 Tomáš Müller<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    private TerminationCondition<V, T> iTermination = null;
    037
    038    /**
    039     * Constructor
    040     * @param solver current solver
    041     */
    042    public ProblemSaver(Solver<V, T> solver) {
    043        iSolver = solver;
    044    }
    045
    046    /** Solver
    047     * @return current solver
    048     **/
    049    public Solver<V, T> getSolver() {
    050        return iSolver;
    051    }
    052
    053    /** Solution to be saved
    054     * @return current solution
    055     **/
    056    protected Solution<V, T> getSolution() {
    057        return iSolver.currentSolution();
    058    }
    059
    060    /** Model of the solution
    061     * @return problem model
    062     **/
    063    @SuppressWarnings("unchecked")
    064    public M getModel() {
    065        return (M)iSolver.currentSolution().getModel();
    066    }
    067
    068    /** Current assignment
    069     * @return current assignment
    070     **/
    071    public Assignment<V, T> getAssignment() {
    072        return getSolution().getAssignment();
    073    }
    074
    075    /** Save the solution
    076     * @throws Exception thrown when save fails
    077     **/
    078    public abstract void save() throws Exception;
    079
    080    /**
    081     * Sets callback class
    082     *
    083     * @param callback
    084     *            method {@link Callback#execute()} is executed when save is
    085     *            done
    086     */
    087    public void setCallback(Callback callback) {
    088        iCallback = callback;
    089    }
    090
    091    /**
    092     * Provide termination condition so that the save process can be stopped if needed (optional).
    093     */
    094    public void setTerminationCondition(TerminationCondition<V, T> termination) {
    095        iTermination = termination;
    096    }
    097
    098    /**
    099     * Return termination condition so that the save process can be stopped if needed.
    100     */
    101    public TerminationCondition<V, T> getTerminationCondition() {
    102        return iTermination;
    103    }
    104
    105    @Override
    106    public void run() {
    107        try {
    108            save();
    109        } catch (Exception e) {
    110            org.apache.logging.log4j.LogManager.getLogger(this.getClass()).error(e.getMessage(), e);
    111        } finally {
    112            if (iCallback != null)
    113                iCallback.execute();
    114        }
    115    }
    116}