[1146] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
| 15 | package de.ugoe.cs.autoquest.tasktrees.treeimpl;
|
---|
| 16 |
|
---|
[1423] | 17 | import java.util.ArrayList; |
---|
| 18 | import java.util.HashMap; |
---|
| 19 | |
---|
[1146] | 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
|
---|
| 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo;
|
---|
[1423] | 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.TaskMetric; |
---|
[1146] | 23 |
|
---|
| 24 | /**
|
---|
[1216] | 25 | * <p> |
---|
| 26 | * this is the default implementation of the interface {@link ITaskInfo}. It |
---|
| 27 | * does not do anything fancy except implementing the interface. |
---|
| 28 | * </p> |
---|
| 29 | * |
---|
| 30 | * @author Patrick Harms |
---|
[1146] | 31 | */
|
---|
| 32 | public class TaskInfo implements ITaskInfo {
|
---|
| 33 |
|
---|
[1216] | 34 | /** |
---|
| 35 | * <p> |
---|
| 36 | * the task to which the infos belong |
---|
| 37 | * </p> |
---|
| 38 | */
|
---|
[1423] | 39 | private ITask task; |
---|
| 40 | |
---|
[1216] | 41 | /** |
---|
| 42 | * <p> |
---|
[1423] | 43 | * all available measures for the task |
---|
[1287] | 44 | * </p> |
---|
| 45 | */ |
---|
[1423] | 46 | private ArrayList<Measure> measures = new ArrayList<Measure>();
|
---|
| 47 |
|
---|
[1287] | 48 | /** |
---|
| 49 | * <p> |
---|
[1216] | 50 | * initialized the task infos with the task to which they belong. |
---|
| 51 | * </p> |
---|
| 52 | *
|
---|
[1146] | 53 | * @param task
|
---|
| 54 | */
|
---|
| 55 | TaskInfo(ITask task) {
|
---|
| 56 | this.task = task;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[1216] | 59 | /* (non-Javadoc)
|
---|
| 60 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getTask()
|
---|
[1146] | 61 | */
|
---|
| 62 | @Override
|
---|
| 63 | public ITask getTask() {
|
---|
| 64 | return task;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[1287] | 67 | /* (non-Javadoc) |
---|
[1423] | 68 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getMeasures() |
---|
[1287] | 69 | */ |
---|
| 70 | @Override |
---|
[1423] | 71 | public IMeasure[] getMeasures() { |
---|
| 72 | measures.trimToSize(); |
---|
| 73 | return measures.toArray(new IMeasure[measures.size()]); |
---|
[1287] | 74 | } |
---|
| 75 | |
---|
[1423] | 76 | /* (non-Javadoc) |
---|
| 77 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getMeasureValue(java.lang.String) |
---|
| 78 | */ |
---|
| 79 | @Override |
---|
| 80 | public int getMeasureValue(TaskMetric metric) { |
---|
| 81 | Measure measure = getMeasure(metric); |
---|
| 82 | |
---|
| 83 | if (measure == null) { |
---|
| 84 | throw new IllegalArgumentException("unknown metric " + metric); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | return measure.getValue(); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /* (non-Javadoc) |
---|
| 91 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getMeasureValue(java.lang.String, de.ugoe.cs.autoquest.tasktrees.treeifc.ITask) |
---|
| 92 | */ |
---|
| 93 | @Override |
---|
| 94 | public int getMeasureValue(TaskMetric metric, ITask context) { |
---|
| 95 | Measure measure = getMeasure(metric); |
---|
| 96 | |
---|
| 97 | if (measure == null) { |
---|
| 98 | throw new IllegalArgumentException("unknown metric " + metric); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | return measure.getValue(context); |
---|
| 102 | } |
---|
| 103 | |
---|
[1216] | 104 | /* (non-Javadoc)
|
---|
[1146] | 105 | * @see java.lang.Object#toString()
|
---|
| 106 | */
|
---|
| 107 | @Override
|
---|
| 108 | public synchronized String toString() {
|
---|
[1287] | 109 | return "TaskInfo(" + task + ")";
|
---|
[1146] | 110 | }
|
---|
[1287] | 111 | |
---|
| 112 | /** |
---|
| 113 | * <p> |
---|
[1423] | 114 | * must be called to indicate that a new new measures for the provided metric are about to |
---|
| 115 | * be calculated and added. |
---|
[1287] | 116 | * </p> |
---|
[1423] | 117 | * |
---|
| 118 | * @param metric the metric for which measures are about to be provided |
---|
[1287] | 119 | */ |
---|
[1423] | 120 | void addMeasure(TaskMetric metric) { |
---|
| 121 | Measure measure = getMeasure(metric); |
---|
| 122 | |
---|
| 123 | if (measure != null) { |
---|
| 124 | throw new IllegalArgumentException("measure for metric " + metric + " already exists."); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | measure = new Measure(metric); |
---|
| 128 | measures.add(measure); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * <p> |
---|
| 133 | * sets a specific value for a measure of a specific metric in the provided context of the task |
---|
| 134 | * </p> |
---|
| 135 | * |
---|
| 136 | * @param metric the metric to which the value belongs |
---|
| 137 | * @param context the context of the task in which the measure was recorded |
---|
| 138 | * @param value the value of the measure |
---|
| 139 | */ |
---|
| 140 | void setCount(TaskMetric metric, ITask context, int value) { |
---|
| 141 | Measure measure = getMeasure(metric); |
---|
| 142 | |
---|
| 143 | if (measure == null) { |
---|
| 144 | throw new IllegalArgumentException("unknown metric. Please create a measure " + |
---|
| 145 | "for the metric before using it."); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | measure.set(context, value); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * <p> |
---|
| 153 | * increases a specific value for a measure of a specific metric in the provided context of the |
---|
| 154 | * task |
---|
| 155 | * </p> |
---|
| 156 | * |
---|
| 157 | * @param metric the metric to which the value belongs |
---|
| 158 | * @param context the context of the task in which the measure was recorded |
---|
| 159 | * @param increment the increment to be added to the value of the measure |
---|
| 160 | */ |
---|
| 161 | void increaseCount(TaskMetric metric, ITask context, int increment) { |
---|
| 162 | Measure measure = getMeasure(metric); |
---|
| 163 | |
---|
| 164 | if (measure == null) { |
---|
| 165 | throw new IllegalArgumentException("unknown metric. Please create a measure " + |
---|
| 166 | "for the metric before using it."); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | measure.increase(context, increment); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /** |
---|
| 173 | * <p> |
---|
| 174 | * convenience method to internally determine the measure for a specific metric |
---|
| 175 | * </p> |
---|
| 176 | */ |
---|
| 177 | private Measure getMeasure(TaskMetric metric) { |
---|
| 178 | for (Measure candidate : measures) { |
---|
| 179 | if (candidate.getMetric().equals(metric)) { |
---|
| 180 | return candidate; |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | return null; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /** |
---|
| 188 | * <p> |
---|
| 189 | * implementation for the measure interface of the task info interface. Does nothing fancy |
---|
| 190 | * except implementing the interface |
---|
| 191 | * </p> |
---|
| 192 | * |
---|
| 193 | * @author Patrick Harms |
---|
| 194 | */ |
---|
| 195 | private static class Measure implements IMeasure { |
---|
| 196 | |
---|
| 197 | /** |
---|
| 198 | * <p> |
---|
| 199 | * the metric to which the measure belongs |
---|
| 200 | * </p> |
---|
| 201 | */ |
---|
| 202 | private TaskMetric metric; |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | * <p> |
---|
| 206 | * the observed values for the difference contexts of the task |
---|
| 207 | * </p> |
---|
| 208 | */ |
---|
| 209 | private HashMap<ITask, Integer> values; |
---|
| 210 | |
---|
| 211 | /** |
---|
| 212 | * <p> |
---|
| 213 | * the context free value of the measure independent of the task context |
---|
| 214 | * </p> |
---|
| 215 | */ |
---|
| 216 | private int contextFreeValue = 0; |
---|
| 217 | |
---|
| 218 | /** |
---|
| 219 | * <p> |
---|
| 220 | * initializes the measure with a specific metric |
---|
| 221 | * </p> |
---|
| 222 | */ |
---|
| 223 | private Measure(TaskMetric metric) { |
---|
| 224 | super(); |
---|
| 225 | this.metric = metric; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | /* (non-Javadoc) |
---|
| 229 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo.IMeasure#getMetric() |
---|
| 230 | */ |
---|
| 231 | @Override |
---|
| 232 | public TaskMetric getMetric() { |
---|
| 233 | return metric; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | /* (non-Javadoc) |
---|
| 237 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo.IMeasure#getValue() |
---|
| 238 | */ |
---|
| 239 | @Override |
---|
| 240 | public int getValue() { |
---|
| 241 | return contextFreeValue; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | /* (non-Javadoc) |
---|
| 245 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo.IMeasure#getValue(de.ugoe.cs.autoquest.tasktrees.treeifc.ITask) |
---|
| 246 | */ |
---|
| 247 | @Override |
---|
| 248 | public int getValue(ITask context) { |
---|
| 249 | if ((context != null) && (values != null)) { |
---|
| 250 | Integer currentValue = values.get(context); |
---|
| 251 | |
---|
| 252 | if (currentValue != null) { |
---|
| 253 | return currentValue; |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | return Integer.MIN_VALUE; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | /** |
---|
| 261 | * <p> |
---|
| 262 | * sets the value of the measure context free as well as specific to the provided context |
---|
| 263 | * </p> |
---|
| 264 | */ |
---|
| 265 | private void set(ITask context, int value) { |
---|
| 266 | contextFreeValue = value; |
---|
| 267 | |
---|
| 268 | if (context != null) { |
---|
| 269 | if (values == null) { |
---|
| 270 | values = new HashMap<ITask, Integer>(); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | values.put(context, value); |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /** |
---|
| 278 | * <p> |
---|
| 279 | * increases the value of the measure context free as well as specific to the provided |
---|
| 280 | * context according to the provided increment |
---|
| 281 | * </p> |
---|
| 282 | */ |
---|
| 283 | private void increase(ITask context, int increment) { |
---|
| 284 | contextFreeValue += increment; |
---|
| 285 | |
---|
| 286 | if (context != null) { |
---|
| 287 | if (values == null) { |
---|
| 288 | values = new HashMap<ITask, Integer>(); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | Integer currentValue = values.get(context); |
---|
| 292 | |
---|
| 293 | if (currentValue == null) { |
---|
| 294 | currentValue = 0; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | values.put(context, currentValue + increment); |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | } |
---|
| 302 | |
---|
[1146] | 303 | }
|
---|