00001 /* 00002 * (c) copyright 2008, Technische Universitaet Graz and Technische Universitaet Wien 00003 * 00004 * This file is part of jdiagengine. 00005 * 00006 * jdiagengine is free software: you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation, either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * jdiagengine is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * You should have received a copy of the GNU General Public License 00016 * along with jdiagengine. If not, see <http://www.gnu.org/licenses/>. 00017 * 00018 * Authors: Joerg Weber, Franz Wotawa 00019 * Contact: jweber@ist.tugraz.at (preferred), or fwotawa@ist.tugraz.at 00020 * 00021 */ 00022 00023 00024 package dfengine; 00025 00026 import java.util.*; 00027 00028 import utils.*; 00029 00030 public class RepairCandidates { 00031 00032 // Map from Integer to ArrayList of RepairCandidate 00033 protected TreeMap items = new TreeMap(); 00034 00035 protected int size = 0; 00036 00037 00038 protected Collection getCandidatesOfSize(int size) { 00039 Object o = items.get(new Integer(size)); 00040 if (o == null) return null; 00041 else return (Collection)o; 00042 } 00043 00044 /* 00045 * If a repair candidate exists which has exactly the same components as ma: 00046 * return this candidate. Otherwise, return null. 00047 */ 00048 public RepairCandidate findCandidateFor(ModeAssignment ma) { 00049 Collection candList = getCandidatesOfSize(ma.size()); 00050 if (candList == null) return null; 00051 else { 00052 Iterator itCand = candList.iterator(); 00053 while (itCand.hasNext()) { 00054 RepairCandidate rc = (RepairCandidate)itCand.next(); 00055 assert(rc.size() == ma.size()); 00056 if (rc.equalComponents(ma)) return rc; 00057 } 00058 } 00059 00060 return null; 00061 } 00062 00063 public Iterator iterator() { 00064 return new RepairCandidatesIterator(); 00065 } 00066 00067 public int size() { 00068 return size; 00069 } 00070 00071 public String toString() { 00072 StringBuffer result = new StringBuffer(); 00073 00074 result.append("number of repair candidates: " + size); 00075 result.append("\n********************************\n"); 00076 00077 Iterator itCands = iterator(); 00078 while (itCands.hasNext()) { 00079 RepairCandidate cand = (RepairCandidate)itCands.next(); 00080 result.append(cand.toString()); 00081 result.append("\n-----------------------------------------------\n"); 00082 } 00083 00084 return result.toString(); 00085 } 00086 00087 public void add(ModeAssignment ma) { 00088 00089 // precond 00090 assert(findCandidateFor(ma) == null); 00091 00092 Collection cands = getCandidatesOfSize(ma.size()); 00093 if (cands == null) { 00094 cands = new ArrayList(); 00095 items.put(new Integer(ma.size()), cands); 00096 } 00097 RepairCandidate rc = new RepairCandidate(ma); 00098 cands.add(rc); 00099 00100 ++size; 00101 00102 // postcond 00103 assert((findCandidateFor(ma)).equalComponents(ma)); 00104 } 00105 00106 00107 // --------------------------------------------------------------- 00108 00109 /* 00110 * Iterates through all RepairCandidate's. 00111 */ 00112 class RepairCandidatesIterator implements Iterator { 00113 00114 Iterator itValues; 00115 00116 Iterator itColl; 00117 00118 RepairCandidate nextCand = null; 00119 00120 00121 public RepairCandidatesIterator() { 00122 itValues = items.values().iterator(); 00123 moveToNext(); 00124 } 00125 00126 protected void moveToNext() { 00127 if ((itColl == null) || (!itColl.hasNext())) { 00128 if (itValues.hasNext()) { 00129 Collection cands = (Collection)itValues.next(); 00130 itColl = cands.iterator(); 00131 assert(itColl.hasNext()); // there must be no empty collections here! 00132 nextCand = (RepairCandidate)itColl.next(); 00133 } else { 00134 nextCand = null; 00135 } 00136 } else { 00137 nextCand = (RepairCandidate)itColl.next(); 00138 } 00139 } 00140 00141 public boolean hasNext() { 00142 return (nextCand != null); 00143 } 00144 00145 public Object next() { 00146 RepairCandidate result = nextCand; 00147 moveToNext(); 00148 return result; 00149 } 00150 00151 public void remove() { 00152 throw new UnsupportedOperationException(); 00153 } 00154 00155 } 00156 00157 }