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.termination.TerminationCondition; 008 009/** 010 * Abstract problem loader class. 011 * 012 * @version IFS 1.3 (Iterative Forward Search)<br> 013 * Copyright (C) 2006 - 2016 Tomáš Müller<br> 014 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 015 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 016 * <br> 017 * This library is free software; you can redistribute it and/or modify 018 * it under the terms of the GNU Lesser General Public License as 019 * published by the Free Software Foundation; either version 3 of the 020 * License, or (at your option) any later version. <br> 021 * <br> 022 * This library is distributed in the hope that it will be useful, but 023 * WITHOUT ANY WARRANTY; without even the implied warranty of 024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 025 * Lesser General Public License for more details. <br> 026 * <br> 027 * You should have received a copy of the GNU Lesser General Public 028 * License along with this library; if not see 029 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 030 */ 031public abstract class ProblemLoader<V extends Variable<V, T>, T extends Value<V, T>, M extends Model<V,T>> implements Runnable { 032 private M iModel = null; 033 private Assignment<V, T> iAssignment = null; 034 private Callback iCallback = null; 035 private TerminationCondition<V, T> iTermination = null; 036 037 /** 038 * Constructor 039 * 040 * @param model 041 * an empty instance of timetable model 042 * @param assignment current assignment 043 */ 044 public ProblemLoader(M model, Assignment<V, T> assignment) { 045 iModel = model; 046 iAssignment = assignment; 047 } 048 049 /** 050 * Returns provided model. 051 * 052 * @return provided model 053 */ 054 public M getModel() { 055 return iModel; 056 } 057 058 /** 059 * Returns provided assignment 060 * @return provided assignment 061 */ 062 public Assignment<V, T> getAssignment() { 063 return iAssignment; 064 } 065 066 /** 067 * Load the model. 068 * @throws Exception thrown when the load fails 069 */ 070 public abstract void load() throws Exception; 071 072 /** 073 * Sets callback class 074 * 075 * @param callback 076 * method {@link Callback#execute()} is executed when load is 077 * done 078 */ 079 public void setCallback(Callback callback) { 080 iCallback = callback; 081 } 082 083 /** 084 * Provide termination condition so that the save process can be stopped if needed (optional). 085 */ 086 public void setTerminationCondition(TerminationCondition<V, T> termination) { 087 iTermination = termination; 088 } 089 090 /** 091 * Return termination condition so that the save process can be stopped if needed. 092 */ 093 public TerminationCondition<V, T> getTerminationCondition() { 094 return iTermination; 095 } 096 097 @Override 098 public void run() { 099 try { 100 load(); 101 } catch (Exception e) { 102 org.apache.logging.log4j.LogManager.getLogger(this.getClass()).error(e.getMessage(), e); 103 } finally { 104 if (iCallback != null) 105 iCallback.execute(); 106 } 107 } 108 109}