Ignore:
Timestamp:
02/28/14 15:09:12 (10 years ago)
Author:
pharms
Message:
  • added possibility to calculate metrics for tasks and added the first metrics
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/treeimpl/TaskInfo.java

    r1287 r1423  
    1515package de.ugoe.cs.autoquest.tasktrees.treeimpl; 
    1616 
     17import java.util.ArrayList; 
     18import java.util.HashMap; 
     19 
    1720import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; 
    1821import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo; 
     22import de.ugoe.cs.autoquest.tasktrees.treeifc.TaskMetric; 
    1923 
    2024/** 
     
    4145     */ 
    4246    private ITask task; 
    43  
    44     /** 
    45      * <p> 
    46      * the number of occurrences of this task 
    47      * </p> 
    48      */ 
    49     private int count; 
     47     
     48    /** 
     49     * <p> 
     50     * all available measures for the task 
     51     * </p> 
     52     */ 
     53    private ArrayList<Measure> measures = new ArrayList<Measure>(); 
    5054 
    5155    /** 
     
    6973 
    7074    /* (non-Javadoc) 
    71      * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInfo#getCount() 
    72      */ 
    73     @Override 
    74     public int getCount() { 
    75         return count; 
     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); 
    76109    } 
    77110 
     
    86119    /** 
    87120     * <p> 
    88      * used to increase the counter of occurrences of this task by one 
    89      * </p> 
    90      */ 
    91     void increaseCount() { 
    92         count++; 
    93     } 
     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 
    94310} 
Note: See TracChangeset for help on using the changeset viewer.