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