Coverage Report - com.sdmetrics.metrics.RuleProcedure - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleProcedure
100%
20/20
100%
6/6
1,333
 
 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.LinkedList;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 
 28  
 import com.sdmetrics.math.ExpressionNode;
 29  
 import com.sdmetrics.model.ModelElement;
 30  
 
 31  
 /**
 32  
  * Base class for all rule procedures.
 33  
  */
 34  163
 public abstract class RuleProcedure extends AbstractProcedure {
 35  
 
 36  
         /** Rule engine of the procedure. */
 37  
         private RuleEngine ruleEngine;
 38  
 
 39  
         private List<RuleViolation> violations;
 40  
 
 41  
         /**
 42  
          * Checks the rule for a model element.
 43  
          * 
 44  
          * @param element The element to check.
 45  
          * @param rule The rule to check.
 46  
          * @throws SDMetricsException Problem while checking the rule
 47  
          */
 48  
         public abstract void checkRule(ModelElement element, Rule rule)
 49  
                         throws SDMetricsException;
 50  
 
 51  
         /**
 52  
          * Sets the rule engine for the procedure.
 53  
          * 
 54  
          * @param engine The rule engine.
 55  
          */
 56  
         void setRuleEngine(RuleEngine engine) {
 57  26
                 setMetricsEngine(engine.getMetricsEngine());
 58  26
                 this.ruleEngine = engine;
 59  26
         }
 60  
 
 61  
         /**
 62  
          * Gets the list of rule violations reported by the procedure.
 63  
          * 
 64  
          * @return List of detected rule violations
 65  
          */
 66  
         List<RuleViolation> getViolations() {
 67  21
                 return violations;
 68  
         }
 69  
 
 70  
         @Override
 71  
         void clear() {
 72  172
                 super.clear();
 73  172
                 ruleEngine = null;
 74  172
                 violations = null;
 75  172
         }
 76  
 
 77  
         /**
 78  
          * Gets the rule engine for this rule procedure.
 79  
          * 
 80  
          * @return The rule engine of the procedure.
 81  
          */
 82  
         protected RuleEngine getRuleEngine() {
 83  2
                 return ruleEngine;
 84  
         }
 85  
 
 86  
         /**
 87  
          * Reports a rule violation.
 88  
          * 
 89  
          * @param element The violating element.
 90  
          * @param rule The violated rule.
 91  
          * @param value The value of the rule violation.
 92  
          */
 93  
         protected void reportViolation(ModelElement element, Rule rule, Object value) {
 94  16
                 if (violations == null) {
 95  14
                         violations = new LinkedList<RuleViolation>();
 96  
                 }
 97  16
                 violations.add(new RuleViolation(element, rule, value));
 98  16
         }
 99  
 
 100  
         /**
 101  
          * Gets the cache to store data for reuse for subsequent checks of other
 102  
          * model elements.
 103  
          * 
 104  
          * @return value cache for rule procedures
 105  
          */
 106  
         protected Map<Object, Object> getValuesCache() {
 107  8
                 return ruleEngine.getValuesCache();
 108  
         }
 109  
 
 110  
         /**
 111  
          * Evaluates the "value" expression of a rule.
 112  
          * 
 113  
          * @param element Model element for which the rule is evaluated
 114  
          * @param attributes Attributes of the calculation procedure definition of
 115  
          *        the rule.
 116  
          * @param vars Variables for the evaluation of the expression
 117  
          * @return Result of the "value" expression of the rule, or
 118  
          *         <code>null</code> if none was specified.
 119  
          * @throws SDMetricsException An error occurred evaluating the "value"
 120  
          *         expression.
 121  
          */
 122  
         protected Object getRuleValue(ModelElement element,
 123  
                         ProcedureAttributes attributes, Variables vars)
 124  
                         throws SDMetricsException {
 125  8
                 ExpressionNode value = attributes.getExpression("value");
 126  8
                 return (value == null) ? null : evalExpression(element, value, vars);
 127  
         }
 128  
 
 129  
         /**
 130  
          * Evaluates the "minnodes" or "mincnt" expression of a rule.
 131  
          * 
 132  
          * @param element Model element for which the rule is evaluated
 133  
          * @param attributes Attributes of the calculation procedure definition of
 134  
          *        the rule.
 135  
          * @param attrName Name of the attribute ("minnodes" or "mincnt").
 136  
          * @param vars Variables for the evaluation of the expression
 137  
          * @return Value of the "min" expression, or 1 if none was specified.
 138  
          * @throws SDMetricsException An error occurred evaluating the expression.
 139  
          */
 140  
         protected int getMinExpressionValue(ModelElement element,
 141  
                         ProcedureAttributes attributes, String attrName, Variables vars)
 142  
                         throws SDMetricsException {
 143  11
                 ExpressionNode node = attributes.getExpression(attrName);
 144  25
                 return (node == null) ? 1 : ((Number) evalExpression(element, node,
 145  6
                                 vars)).intValue();
 146  
         }
 147  
 
 148  
 }