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

Last change on this file since 1428 was 1428, checked in by pharms, 10 years ago
  • removed find bugs warning
File size: 8.7 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     * the task to which the infos belong
37     * </p>
38     */
39    private ITask task;
40   
41    /**
42     * <p>
43     * all available measures for the task
44     * </p>
45     */
46    private ArrayList<Measure> measures = new ArrayList<Measure>();
47
48    /**
49     * <p>
50     * initialized the task infos with the task to which they belong.
51     * </p>
52     *
53     * @param task
54     */
55    TaskInfo(ITask task) {
56        this.task = task;
57    }
58
59    /* (non-Javadoc)
60     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getTask()
61     */
62    @Override
63    public ITask getTask() {
64        return task;
65    }
66
67    /* (non-Javadoc)
68     * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getMeasures()
69     */
70    @Override
71    public IMeasure[] getMeasures() {
72        measures.trimToSize();
73        return measures.toArray(new IMeasure[measures.size()]);
74    }
75
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
104    /* (non-Javadoc)
105     * @see java.lang.Object#toString()
106     */
107    @Override
108    public synchronized String toString() {
109        return "TaskInfo(" + task + ")";
110    }
111
112    /**
113     * <p>
114     * must be called to indicate that a new new measures for the provided metric are about to
115     * be calculated and added.
116     * </p>
117     *
118     * @param metric the metric for which measures are about to be provided
119     */
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
303}
Note: See TracBrowser for help on using the repository browser.