| 1 | package de.uka.ipd.sdq.dsexplore.opt4j.genotype; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | import java.util.Iterator; |
| 5 | import java.util.List; |
| 6 | import java.util.ListIterator; |
| 7 | |
| 8 | import org.eclipse.emf.ecore.util.EcoreUtil; |
| 9 | import org.opt4j.core.problem.Genotype; |
| 10 | import org.opt4j.genotype.ListGenotype; |
| 11 | |
| 12 | import de.uka.ipd.sdq.pcm.designdecision.Candidate; |
| 13 | import de.uka.ipd.sdq.pcm.designdecision.Choice; |
| 14 | import de.uka.ipd.sdq.pcm.designdecision.DecisionSpace; |
| 15 | import de.uka.ipd.sdq.pcm.designdecision.DegreeOfFreedomInstance; |
| 16 | import de.uka.ipd.sdq.pcm.designdecision.designdecisionFactory; |
| 17 | |
| 18 | public class DesignDecisionGenotype implements ListGenotype<Choice> { |
| 19 | |
| 20 | private Candidate choices; |
| 21 | |
| 22 | /* |
| 23 | * Internal static class to disallow any changes of the individual counter except increasing it. |
| 24 | */ |
| 25 | static class IndividualID { |
| 26 | private static long individualIDCounter = 0; |
| 27 | protected static long incID(){ |
| 28 | return individualIDCounter++; |
| 29 | } |
| 30 | } |
| 31 | private long numericId = IndividualID.incID(); |
| 32 | |
| 33 | public DesignDecisionGenotype() { |
| 34 | this.choices = designdecisionFactory.eINSTANCE.createCandidate(); |
| 35 | } |
| 36 | |
| 37 | public DesignDecisionGenotype(Candidate emfCandidate){ |
| 38 | this.choices = emfCandidate; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * create new genotype. Sort choice in the order of the passed Problem. |
| 43 | * @param emfCandidate |
| 44 | */ |
| 45 | public DesignDecisionGenotype(Candidate emfCandidate, DecisionSpace problem, DesignDecisionGenotype initialCandidateGenotype){ |
| 46 | // sort by designdecision order (time doesnot matter so much as this is only done at optimisation startup) |
| 47 | this.choices = designdecisionFactory.eINSTANCE.createCandidate(); |
| 48 | for (DegreeOfFreedomInstance dof : problem.getDegreesOfFreedom()) { |
| 49 | boolean foundChoice = false; |
| 50 | for (Choice choice : emfCandidate.getChoices()) { |
| 51 | if (choice.getDegreeOfFreedomInstance() == dof){ |
| 52 | this.choices.getChoices().add(choice); |
| 53 | foundChoice = true; |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | if (!foundChoice){ |
| 58 | // if not found take from initial genotype |
| 59 | for (Choice choice : initialCandidateGenotype.getEMFCandidate().getChoices()) { |
| 60 | if (EcoreUtil.equals(choice.getDegreeOfFreedomInstance(),dof)){ |
| 61 | this.choices.getChoices().add((Choice)EcoreUtil.copy(choice)); |
| 62 | foundChoice = true; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private DesignDecisionGenotype(List<Choice> internalList) { |
| 71 | this.choices = designdecisionFactory.eINSTANCE.createCandidate(); |
| 72 | this.choices.getChoices().addAll(internalList); |
| 73 | } |
| 74 | |
| 75 | @SuppressWarnings("unchecked") |
| 76 | @Override |
| 77 | public <G extends Genotype> G newInstance() { |
| 78 | return (G) new DesignDecisionGenotype(); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public int size() { |
| 83 | return this.choices.getChoices().size(); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public boolean add(Choice e) { |
| 88 | return this.choices.getChoices().add(e); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void add(int index, Choice element) { |
| 93 | this.choices.getChoices().add(index, element); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean addAll(Collection<? extends Choice> c) { |
| 98 | return this.choices.getChoices().addAll(c); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean addAll(int index, Collection<? extends Choice> c) { |
| 103 | return this.choices.getChoices().addAll(index, c); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void clear() { |
| 108 | this.clear(); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public boolean contains(Object o) { |
| 113 | return this.choices.getChoices().contains(o); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public boolean containsAll(Collection<?> c) { |
| 118 | return this.choices.getChoices().containsAll(c); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Choice get(int index) { |
| 123 | return this.choices.getChoices().get(index); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public int indexOf(Object o) { |
| 128 | return this.choices.getChoices().indexOf(o); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public boolean isEmpty() { |
| 133 | return this.choices.getChoices().isEmpty(); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Iterator<Choice> iterator() { |
| 138 | return new InternalIterator(this.choices.getChoices(),0); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public int lastIndexOf(Object o) { |
| 143 | return this.choices.getChoices().lastIndexOf(o); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public ListIterator<Choice> listIterator() { |
| 148 | return new InternalIterator(this.choices.getChoices(),0); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public ListIterator<Choice> listIterator(int index) { |
| 153 | return new InternalIterator(this.choices.getChoices(), index); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public boolean remove(Object o) { |
| 158 | return this.choices.getChoices().remove(o); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Choice remove(int index) { |
| 163 | return this.choices.getChoices().remove(index); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public boolean removeAll(Collection<?> c) { |
| 168 | return this.choices.getChoices().removeAll(c); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public boolean retainAll(Collection<?> c) { |
| 173 | return this.choices.getChoices().retainAll(c); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public Choice set(int index, Choice element) { |
| 178 | return this.choices.getChoices().set(index, element); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public List<Choice> subList(int fromIndex, int toIndex) { |
| 183 | return new DesignDecisionGenotype(choices.getChoices().subList(fromIndex, toIndex)); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public Object[] toArray() { |
| 188 | return this.choices.getChoices().toArray(); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public <T> T[] toArray(T[] a) { |
| 193 | return this.choices.getChoices().toArray(a); |
| 194 | } |
| 195 | |
| 196 | protected List<Choice> getInternalList(){ |
| 197 | return this.choices.getChoices(); |
| 198 | } |
| 199 | |
| 200 | public Candidate getEMFCandidate(){ |
| 201 | return this.choices; |
| 202 | } |
| 203 | |
| 204 | public long getNumericID() { |
| 205 | return this.numericId; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * This allows us to intercept uses of the iterator when needed. |
| 214 | * @author martens |
| 215 | * |
| 216 | */ |
| 217 | class InternalIterator implements ListIterator<Choice>{ |
| 218 | |
| 219 | ListIterator<Choice> decoratedIterator; |
| 220 | |
| 221 | public InternalIterator(List<Choice> internalList, int index){ |
| 222 | this.decoratedIterator = internalList.listIterator(index); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public boolean hasNext() { |
| 227 | return decoratedIterator.hasNext(); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public Choice next() { |
| 232 | return decoratedIterator.next(); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public void remove() { |
| 237 | decoratedIterator.remove(); |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void add(Choice e) { |
| 242 | this.decoratedIterator.add(e); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public boolean hasPrevious() { |
| 247 | return this.decoratedIterator.hasPrevious(); |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public int nextIndex() { |
| 252 | return this.decoratedIterator.nextIndex(); |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public Choice previous() { |
| 257 | return this.decoratedIterator.previous(); |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public int previousIndex() { |
| 262 | return this.decoratedIterator.previousIndex(); |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void set(Choice e) { |
| 267 | this.decoratedIterator.set(e); |
| 268 | } |
| 269 | |
| 270 | } |