Coverage Report - com.sdmetrics.metrics.AbstractProcedure - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractProcedure
100%
36/36
92%
13/14
1,714
 
 1  
 /*
 2  
  * SDMetrics Open Core for UML design measurement
 3  
  * Copyright (c) Juergen Wuest
 4  
  * To contact the author, see <http://www.sdmetrics.com/Contact.html>.
 5  
  * 
 6  
  * This file is part of the SDMetrics Open Core.
 7  
  * 
 8  
  * SDMetrics Open Core is free software: you can redistribute it and/or modify
 9  
  * it under the terms of the GNU Affero General Public License as
 10  
  * published by the Free Software Foundation, either version 3 of the
 11  
  * License, or (at your option) any later version.
 12  
     
 13  
  * SDMetrics Open Core is distributed in the hope that it will be useful,
 14  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  
  * GNU Affero General Public License for more details.
 17  
  *
 18  
  * You should have received a copy of the GNU Affero General Public License
 19  
  * along with SDMetrics Open Core.  If not, see <http://www.gnu.org/licenses/>.
 20  
  *
 21  
  */
 22  
 package com.sdmetrics.metrics;
 23  
 
 24  
 import java.util.Collection;
 25  
 
 26  
 import com.sdmetrics.math.ExpressionNode;
 27  
 import com.sdmetrics.model.MetaModel;
 28  
 import com.sdmetrics.model.MetaModelElement;
 29  
 import com.sdmetrics.model.Model;
 30  
 import com.sdmetrics.model.ModelElement;
 31  
 
 32  
 /**
 33  
  * Base class for metric, set, and rule procedures as well as scalar, boolean,
 34  
  * and set operations.
 35  
  */
 36  1187
 public class AbstractProcedure {
 37  
 
 38  
         /** The name of the procedure in the metric definition file. */
 39  
         private String name;
 40  
 
 41  
         /** The metrics engine for calculation of the procedure. */
 42  
         private MetricsEngine metricsEngine;
 43  
 
 44  
         // Methods for use by specializations
 45  
 
 46  
         /**
 47  
          * Gets the metrics engine to use for calculations.
 48  
          * 
 49  
          * @return Metrics engine for this procedure
 50  
          */
 51  
         protected MetricsEngine getMetricsEngine() {
 52  535
                 return metricsEngine;
 53  
         }
 54  
 
 55  
         /**
 56  
          * Gets the element set specified by a "relation" or "relset" attribute.
 57  
          * 
 58  
          * @param element Element for which to retrieve the set.
 59  
          * @param attributes Attributes of the procedure definition.
 60  
          * @param vars Variables for the evaluation of expressions
 61  
          * @return The set specified by the "relation" or "relset" attribute.
 62  
          * @throws SDMetricsException Neither "relation" nor "relset" attribute was
 63  
          *         specified.
 64  
          */
 65  
         @SuppressWarnings("unchecked")
 66  
         protected Collection<ModelElement> getRelationOrSet(ModelElement element,
 67  
                         ProcedureAttributes attributes, Variables vars)
 68  
                         throws SDMetricsException {
 69  
 
 70  
                 // Check "relation attribute"
 71  332
                 String relation = attributes.getStringValue("relation");
 72  330
                 if (relation != null)
 73  326
                         return element.getRelations(relation);
 74  
 
 75  
                 // Check "relset" attribute
 76  4
                 ExpressionNode setExpr = attributes.getExpression("relset");
 77  4
                 if (setExpr == null)
 78  2
                         throw new SDMetricsException(element, null,
 79  1
                                         "Neither 'relation' nor 'relset' attribute specified.");
 80  6
                 return (Collection<ModelElement>) metricsEngine.evalSetExpression(
 81  3
                                 element, setExpr, vars);
 82  
         }
 83  
 
 84  
         /**
 85  
          * Creates a new filter attribute processing object.
 86  
          * 
 87  
          * @param attributes Attributes of the procedure definition
 88  
          * @return Filter attribute processor for this procedure
 89  
          * @throws SDMetricsException One of the filter attributes contains illegal
 90  
          *         values
 91  
          */
 92  
         protected FilterAttributeProcessor getFilterAttributeProcessor(
 93  
                         ProcedureAttributes attributes) throws SDMetricsException {
 94  236
                 return new FilterAttributeProcessor(getMetricsEngine(), attributes);
 95  
         }
 96  
 
 97  
         /**
 98  
          * Gets the model for which this calculation takes place.
 99  
          * 
 100  
          * @return The model for this calculation.
 101  
          */
 102  
         protected Model getModel() {
 103  6
                 return metricsEngine.getModel();
 104  
         }
 105  
 
 106  
         /**
 107  
          * Gets the metamodel for which this calculation takes place.
 108  
          * 
 109  
          * @return The metamodel for this calculation
 110  
          */
 111  
         protected MetaModel getMetaModel() {
 112  11
                 return metricsEngine.getMetaModel();
 113  
         }
 114  
 
 115  
         /**
 116  
          * Calculates the value of a metric expression for a model element.
 117  
          * 
 118  
          * @param element The model element to evaluate the expression for.
 119  
          * @param node Root node of the expression to evaluate
 120  
          * @param vars Variables for the expression evaluation
 121  
          * @return The resulting metric value of the expression
 122  
          * @throws SDMetricsException a problem occurred during the evaluation
 123  
          */
 124  
         protected Object evalExpression(ModelElement element, ExpressionNode node,
 125  
                         Variables vars) throws SDMetricsException {
 126  3100
                 return metricsEngine.evalExpression(element, node, vars);
 127  
         }
 128  
 
 129  
         /**
 130  
          * Calculates the value of a boolean expression for a model element.
 131  
          * 
 132  
          * @param element The model element to evaluate the expression for.
 133  
          * @param node Root node of the expression to evaluate
 134  
          * @param vars Variables for the expression evaluation
 135  
          * @return The resulting boolean value of the expression
 136  
          * @throws SDMetricsException a problem occurred during the evaluation
 137  
          */
 138  
         protected boolean evalBooleanExpression(ModelElement element,
 139  
                         ExpressionNode node, Variables vars) throws SDMetricsException {
 140  362
                 return metricsEngine.evalBooleanExpression(element, node, vars);
 141  
         }
 142  
 
 143  
         /**
 144  
          * Calculates a set expression for a model element.
 145  
          * 
 146  
          * @param element The model element to evaluate the expression for.
 147  
          * @param node Root node of the expression to evaluate
 148  
          * @param vars Variables for the expression evaluation
 149  
          * @return the result set of the evaluation
 150  
          * @throws SDMetricsException a problem occurred during the evaluation
 151  
          */
 152  
         protected Collection<?> evalSetExpression(ModelElement element,
 153  
                         ExpressionNode node, Variables vars) throws SDMetricsException {
 154  48
                 return metricsEngine.evalSetExpression(element, node, vars);
 155  
         }
 156  
 
 157  
         /**
 158  
          * Calculates a set expression that returns a set of model elements.
 159  
          * 
 160  
          * @param element The model element to evaluate the expression for.
 161  
          * @param node Root node of the expression to evaluate
 162  
          * @param vars Variables for the expression evaluation
 163  
          * @return the resulting element set
 164  
          * @throws SDMetricsException a problem occurred during the evaluation
 165  
          */
 166  
         @SuppressWarnings("unchecked")
 167  
         protected Collection<ModelElement> evalElementSetExpression(
 168  
                         ModelElement element, ExpressionNode node, Variables vars)
 169  
                         throws SDMetricsException {
 170  102
                 return (Collection<ModelElement>) metricsEngine.evalSetExpression(
 171  51
                                 element, node, vars);
 172  
         }
 173  
 
 174  
         /**
 175  
          * Checks if a metric or set can be applied to a related element. Used for
 176  
          * recursive or nesting metrics/sets. If the metric or set is not
 177  
          * inheritable, the related element must be of the same type as the element
 178  
          * for which the metric is being calculated. If the metric or set is
 179  
          * inheritable, the related element must be an ancestor or descendant for
 180  
          * which the metric is also available.
 181  
          * 
 182  
          * @param element The element for which the metric/set/rule is calculated.
 183  
          * @param metric The metric, set, or rule to be checked.
 184  
          * @param candidate The related element to check.
 185  
          * @return <code>true</code> if the metric/set/rule can be applied to the
 186  
          *         candidate element, else <code>false</code>
 187  
          * @since 2.3
 188  
          */
 189  
         protected boolean isCompatible(ModelElement element, MetricEntry metric,
 190  
                         ModelElement candidate) {
 191  123
                 MetaModelElement elType = element.getType();
 192  123
                 MetaModelElement candType = candidate.getType();
 193  123
                 if (elType == candType)
 194  60
                         return true;
 195  
 
 196  63
                 if (metric.isInheritable()) {
 197  57
                         return (candType.specializes(elType) || elType
 198  53
                                         .specializes(candType))
 199  16
                                         && metricsEngine.getMetricStore().getMetric(candType,
 200  8
                                                         metric.getName()) != null;
 201  
                 }
 202  10
                 return false;
 203  
         }
 204  
 
 205  
         // Package internal house keeping methods
 206  
 
 207  
         /**
 208  
          * Sets the name of this procedure from the metric definition file.
 209  
          * 
 210  
          * @param name The name of the procedure.
 211  
          */
 212  
         void setName(String name) {
 213  1158
                 this.name = name;
 214  1158
         }
 215  
 
 216  
         /**
 217  
          * Gets the name of this procedure.
 218  
          * 
 219  
          * @return name The name of the procedure.
 220  
          * 
 221  
          */
 222  
         String getName() {
 223  3035
                 return name;
 224  
         }
 225  
 
 226  
         /**
 227  
          * Sets the metrics engine for this procedure.
 228  
          * 
 229  
          * @param engine The metrics engine
 230  
          */
 231  
         void setMetricsEngine(MetricsEngine engine) {
 232  2159
                 this.metricsEngine = engine;
 233  2159
         }
 234  
 
 235  
         /**
 236  
          * Clears the procedure for reuse.
 237  
          * <p>
 238  
          * Deletes the reference to the metrics engine.
 239  
          */
 240  
         void clear() {
 241  3030
                 metricsEngine = null;
 242  3030
         }
 243  
 }