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  331
                 String relation = attributes.getStringValue("relation");
 72  329
                 if (relation != null) {
 73  325
                         return element.getRelations(relation);
 74  
                 }
 75  
 
 76  
                 // Check "relset" attribute
 77  4
                 ExpressionNode setExpr = attributes.getExpression("relset");
 78  4
                 if (setExpr == null) {
 79  2
                         throw new SDMetricsException(element, null,
 80  1
                                         "Neither 'relation' nor 'relset' attribute specified.");
 81  
                 }
 82  6
                 return (Collection<ModelElement>) metricsEngine.evalSetExpression(
 83  3
                                 element, setExpr, vars);
 84  
         }
 85  
 
 86  
         /**
 87  
          * Creates a new filter attribute processing object.
 88  
          * 
 89  
          * @param attributes Attributes of the procedure definition
 90  
          * @return Filter attribute processor for this procedure
 91  
          * @throws SDMetricsException One of the filter attributes contains illegal
 92  
          *         values
 93  
          */
 94  
         protected FilterAttributeProcessor getFilterAttributeProcessor(
 95  
                         ProcedureAttributes attributes) throws SDMetricsException {
 96  236
                 return new FilterAttributeProcessor(getMetricsEngine(), attributes);
 97  
         }
 98  
 
 99  
         /**
 100  
          * Gets the model for which this calculation takes place.
 101  
          * 
 102  
          * @return The model for this calculation.
 103  
          */
 104  
         protected Model getModel() {
 105  6
                 return metricsEngine.getModel();
 106  
         }
 107  
 
 108  
         /**
 109  
          * Gets the metamodel for which this calculation takes place.
 110  
          * 
 111  
          * @return The metamodel for this calculation
 112  
          */
 113  
         protected MetaModel getMetaModel() {
 114  11
                 return metricsEngine.getMetaModel();
 115  
         }
 116  
 
 117  
         /**
 118  
          * Calculates the value of a metric expression for a model element.
 119  
          * 
 120  
          * @param element The model element to evaluate the expression for.
 121  
          * @param node Root node of the expression to evaluate
 122  
          * @param vars Variables for the expression evaluation
 123  
          * @return The resulting metric value of the expression
 124  
          * @throws SDMetricsException a problem occurred during the evaluation
 125  
          */
 126  
         protected Object evalExpression(ModelElement element, ExpressionNode node,
 127  
                         Variables vars) throws SDMetricsException {
 128  3093
                 return metricsEngine.evalExpression(element, node, vars);
 129  
         }
 130  
 
 131  
         /**
 132  
          * Calculates the value of a boolean expression for a model element.
 133  
          * 
 134  
          * @param element The model element to evaluate the expression for.
 135  
          * @param node Root node of the expression to evaluate
 136  
          * @param vars Variables for the expression evaluation
 137  
          * @return The resulting boolean value of the expression
 138  
          * @throws SDMetricsException a problem occurred during the evaluation
 139  
          */
 140  
         protected boolean evalBooleanExpression(ModelElement element,
 141  
                         ExpressionNode node, Variables vars) throws SDMetricsException {
 142  361
                 return metricsEngine.evalBooleanExpression(element, node, vars);
 143  
         }
 144  
 
 145  
         /**
 146  
          * Calculates a set expression for a model element.
 147  
          * 
 148  
          * @param element The model element to evaluate the expression for.
 149  
          * @param node Root node of the expression to evaluate
 150  
          * @param vars Variables for the expression evaluation
 151  
          * @return the result set of the evaluation
 152  
          * @throws SDMetricsException a problem occurred during the evaluation
 153  
          */
 154  
         protected Collection<?> evalSetExpression(ModelElement element,
 155  
                         ExpressionNode node, Variables vars) throws SDMetricsException {
 156  48
                 return metricsEngine.evalSetExpression(element, node, vars);
 157  
         }
 158  
 
 159  
         /**
 160  
          * Calculates a set expression that returns a set of model elements.
 161  
          * 
 162  
          * @param element The model element to evaluate the expression for.
 163  
          * @param node Root node of the expression to evaluate
 164  
          * @param vars Variables for the expression evaluation
 165  
          * @return the resulting element set
 166  
          * @throws SDMetricsException a problem occurred during the evaluation
 167  
          */
 168  
         @SuppressWarnings("unchecked")
 169  
         protected Collection<ModelElement> evalElementSetExpression(
 170  
                         ModelElement element, ExpressionNode node, Variables vars)
 171  
                         throws SDMetricsException {
 172  102
                 return (Collection<ModelElement>) metricsEngine.evalSetExpression(
 173  51
                                 element, node, vars);
 174  
         }
 175  
 
 176  
         /**
 177  
          * Checks if a metric or set can be applied to a related element. Used for
 178  
          * recursive or nesting metrics/sets. If the metric or set is not
 179  
          * inheritable, the related element must be of the same type as the element
 180  
          * for which the metric is being calculated. If the metric or set is
 181  
          * inheritable, the related element must be an ancestor or descendant for
 182  
          * which the metric is also available.
 183  
          * 
 184  
          * @param element The element for which the metric/set/rule is calculated.
 185  
          * @param metric The metric, set, or rule to be checked.
 186  
          * @param candidate The related element to check.
 187  
          * @return <code>true</code> if the metric/set/rule can be applied to the
 188  
          *         candidate element, else <code>false</code>
 189  
          * @since 2.3
 190  
          */
 191  
         protected boolean isCompatible(ModelElement element, MetricEntry metric,
 192  
                         ModelElement candidate) {
 193  123
                 MetaModelElement elType = element.getType();
 194  123
                 MetaModelElement candType = candidate.getType();
 195  123
                 if (elType == candType) {
 196  60
                         return true;
 197  
                 }
 198  
 
 199  63
                 if (metric.isInheritable()) {
 200  57
                         return (candType.specializes(elType) || elType
 201  53
                                         .specializes(candType))
 202  16
                                         && metricsEngine.getMetricStore().getMetric(candType,
 203  8
                                                         metric.getName()) != null;
 204  
                 }
 205  10
                 return false;
 206  
         }
 207  
 
 208  
         // Package internal house keeping methods
 209  
 
 210  
         /**
 211  
          * Sets the name of this procedure from the metric definition file.
 212  
          * 
 213  
          * @param name The name of the procedure.
 214  
          */
 215  
         void setName(String name) {
 216  1158
                 this.name = name;
 217  1158
         }
 218  
 
 219  
         /**
 220  
          * Gets the name of this procedure.
 221  
          * 
 222  
          * @return name The name of the procedure.
 223  
          * 
 224  
          */
 225  
         String getName() {
 226  3031
                 return name;
 227  
         }
 228  
 
 229  
         /**
 230  
          * Sets the metrics engine for this procedure.
 231  
          * 
 232  
          * @param engine The metrics engine
 233  
          */
 234  
         void setMetricsEngine(MetricsEngine engine) {
 235  2155
                 this.metricsEngine = engine;
 236  2155
         }
 237  
 
 238  
         /**
 239  
          * Clears the procedure for reuse.
 240  
          * <p>
 241  
          * Deletes the reference to the metrics engine.
 242  
          */
 243  
         void clear() {
 244  3026
                 metricsEngine = null;
 245  3026
         }
 246  
 }