source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/treeimpl/TaskInfo.java @ 1423

Last change on this file since 1423 was 1423, checked in by pharms, 10 years ago
  • added possibility to calculate metrics for tasks and added the first metrics
File size: 8.8 KB
Line 
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
15package de.ugoe.cs.autoquest.tasktrees.treeimpl;
16
17import java.util.ArrayList;
18import java.util.HashMap;
19
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.TaskMetric;
23
24/**
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
31 */
32public class TaskInfo implements ITaskInfo {
33   
34    /**
35     * <p>
36     * default serial version UID
37     * </p>
38     */
39    private static final long serialVersionUID = 1L;
40   
41    /**
42     * <p>
43     * the task to which the infos belong
44     * </p>
45     */
46    private ITask task;
47   
48    /**
49     * <p>
50     * all available measures for the task
51     * </p>
52     */
53    private ArrayList<Measure> measures = new ArrayList<Measure>();
54
55    /**
56     * <p>
57     * initialized the task infos with the task to which they belong.
58     * </p>
59     *
60     * @param task
61     */
62    TaskInfo(ITask task) {
63        this.task = task;
64    }
65
66    /* (non-Javadoc)
67     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getTask()
68     */
69    @Override
70    public ITask getTask() {
71        return task;
72    }
73
74    /* (non-Javadoc)
75     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getMeasures()
76     */
77    @Override
78    public IMeasure[] getMeasures() {
79        measures.trimToSize();
80        return measures.toArray(new IMeasure[measures.size()]);
81    }
82
83    /* (non-Javadoc)
84     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getMeasureValue(java.lang.String)
85     */
86    @Override
87    public int getMeasureValue(TaskMetric metric) {
88        Measure measure = getMeasure(metric);
89       
90        if (measure == null) {
91            throw new IllegalArgumentException("unknown metric " + metric);
92        }
93       
94        return measure.getValue();
95    }
96
97    /* (non-Javadoc)
98     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getMeasureValue(java.lang.String, de.ugoe.cs.autoquest.tasktrees.treeifc.ITask)
99     */
100    @Override
101    public int getMeasureValue(TaskMetric metric, ITask context) {
102        Measure measure = getMeasure(metric);
103       
104        if (measure == null) {
105            throw new IllegalArgumentException("unknown metric " + metric);
106        }
107       
108        return measure.getValue(context);
109    }
110
111    /* (non-Javadoc)
112     * @see java.lang.Object#toString()
113     */
114    @Override
115    public synchronized String toString() {
116        return "TaskInfo(" + task + ")";
117    }
118
119    /**
120     * <p>
121     * must be called to indicate that a new new measures for the provided metric are about to
122     * be calculated and added.
123     * </p>
124     *
125     * @param metric the metric for which measures are about to be provided
126     */
127    void addMeasure(TaskMetric metric) {
128        Measure measure = getMeasure(metric);
129       
130        if (measure != null) {
131            throw new IllegalArgumentException("measure for metric " + metric + " already exists.");
132        }
133       
134        measure = new Measure(metric);
135        measures.add(measure);
136    }
137
138    /**
139     * <p>
140     * sets a specific value for a measure of a specific metric in the provided context of the task
141     * </p>
142     *
143     * @param metric  the metric to which the value belongs
144     * @param context the context of the task in which the measure was recorded
145     * @param value   the value of the measure
146     */
147    void setCount(TaskMetric metric, ITask context, int value) {
148        Measure measure = getMeasure(metric);
149       
150        if (measure == null) {
151            throw new IllegalArgumentException("unknown metric. Please create a measure " +
152                                               "for the metric before using it.");
153        }
154       
155        measure.set(context, value);
156    }
157
158    /**
159     * <p>
160     * increases a specific value for a measure of a specific metric in the provided context of the
161     * task
162     * </p>
163     *
164     * @param metric    the metric to which the value belongs
165     * @param context   the context of the task in which the measure was recorded
166     * @param increment the increment to be added to the value of the measure
167     */
168    void increaseCount(TaskMetric metric, ITask context, int increment) {
169        Measure measure = getMeasure(metric);
170       
171        if (measure == null) {
172            throw new IllegalArgumentException("unknown metric. Please create a measure " +
173                                               "for the metric before using it.");
174        }
175       
176        measure.increase(context, increment);
177    }
178
179    /**
180     * <p>
181     * convenience method to internally determine the measure for a specific metric
182     * </p>
183     */
184    private Measure getMeasure(TaskMetric metric) {
185        for (Measure candidate : measures) {
186            if (candidate.getMetric().equals(metric)) {
187                return candidate;
188            }
189        }
190       
191        return null;
192    }
193
194    /**
195     * <p>
196     * implementation for the measure interface of the task info interface. Does nothing fancy
197     * except implementing the interface
198     * </p>
199     *
200     * @author Patrick Harms
201     */
202    private static class Measure implements IMeasure {
203
204        /**
205         * <p>
206         * the metric to which the measure belongs
207         * </p>
208         */
209        private TaskMetric metric;
210       
211        /**
212         * <p>
213         * the observed values for the difference contexts of the task
214         * </p>
215         */
216        private HashMap<ITask, Integer> values;
217       
218        /**
219         * <p>
220         * the context free value of the measure independent of the task context
221         * </p>
222         */
223        private int contextFreeValue = 0;
224       
225        /**
226         * <p>
227         * initializes the measure with a specific metric
228         * </p>
229         */
230        private Measure(TaskMetric metric) {
231            super();
232            this.metric = metric;
233        }
234
235        /* (non-Javadoc)
236         * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo.IMeasure#getMetric()
237         */
238        @Override
239        public TaskMetric getMetric() {
240            return metric;
241        }
242
243        /* (non-Javadoc)
244         * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo.IMeasure#getValue()
245         */
246        @Override
247        public int getValue() {
248            return contextFreeValue;
249        }
250
251        /* (non-Javadoc)
252         * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo.IMeasure#getValue(de.ugoe.cs.autoquest.tasktrees.treeifc.ITask)
253         */
254        @Override
255        public int getValue(ITask context) {
256            if ((context != null) && (values != null)) {
257                Integer currentValue = values.get(context);
258               
259                if (currentValue != null) {
260                    return currentValue;
261                }
262            }
263           
264            return Integer.MIN_VALUE;
265        }
266
267        /**
268         * <p>
269         * sets the value of the measure context free as well as specific to the provided context
270         * </p>
271         */
272        private void set(ITask context, int value) {
273            contextFreeValue = value;
274           
275            if (context != null) {
276                if (values == null) {
277                    values = new HashMap<ITask, Integer>();
278                }
279               
280                values.put(context, value);
281            }
282        }
283
284        /**
285         * <p>
286         * increases the value of the measure context free as well as specific to the provided
287         * context according to the provided increment
288         * </p>
289         */
290        private void increase(ITask context, int increment) {
291            contextFreeValue += increment;
292           
293            if (context != null) {
294                if (values == null) {
295                    values = new HashMap<ITask, Integer>();
296                }
297               
298                Integer currentValue = values.get(context);
299               
300                if (currentValue == null) {
301                    currentValue = 0;
302                }
303               
304                values.put(context, currentValue + increment);
305            }
306        }
307
308    }
309
310}
Note: See TracBrowser for help on using the repository browser.