Coverage Report - com.sdmetrics.metrics.RuleProcedureSet - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleProcedureSet
100%
30/30
100%
12/12
8
 
 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  
 import java.util.Iterator;
 26  
 
 27  
 import com.sdmetrics.math.ExpressionNode;
 28  
 import com.sdmetrics.model.MetaModel;
 29  
 import com.sdmetrics.model.ModelElement;
 30  
 
 31  
 /**
 32  
  * Checks rules based on set procedures.
 33  
  * <p>
 34  
  * Every set procedure can also be used to define a rule: the set is evaluated
 35  
  * for the model element to check, and any elements it contains are reported as
 36  
  * violation.
 37  
  */
 38  4
 public class RuleProcedureSet extends RuleProcedure {
 39  
 
 40  
         @Override
 41  
         public void checkRule(ModelElement element, Rule rule)
 42  
                         throws SDMetricsException {
 43  
 
 44  
                 // Check the precondition of the rule
 45  8
                 ProcedureAttributes attributes = rule.getAttributes();
 46  8
                 ExpressionNode precondition = attributes.getExpression("precondition");
 47  8
                 if (precondition != null)
 48  2
                         if (!evalBooleanExpression(element, precondition, null))
 49  1
                                 return;
 50  
 
 51  
                 // calculate the set of the rule
 52  
                 Collection<?> set;
 53  7
                 if ("valueset".equals(rule.getProcedureName())) {
 54  8
                         set = evalSetExpression(element,
 55  4
                                         attributes.getRequiredExpression("set"), null);
 56  4
                 } else {
 57  6
                         Set proxy = new Set("proxy", getMetaModel().getType(
 58  3
                                         MetaModel.BASE_ELEMENT));
 59  3
                         proxy.setMultiSet(true);
 60  3
                         proxy.setAttributes(attributes);
 61  3
                         proxy.setProcedureName(rule.getProcedureName());
 62  3
                         set = getMetricsEngine().computeSet(element, proxy);
 63  
                 }
 64  
 
 65  7
                 Iterator<?> it = MetricTools.getFlatIterator(set);
 66  14
                 int minCount = getMinExpressionValue(element, attributes, "mincnt",
 67  7
                                 null);
 68  
                 // report any elements in the set that occur at least "mincnt" times
 69  23
                 while (it.hasNext()) {
 70  9
                         Object setElem = it.next();
 71  9
                         int multiplicity = MetricTools.elementCount(set, setElem);
 72  9
                         if (multiplicity >= minCount) {
 73  8
                                 Object value = null;
 74  8
                                 if (setElem instanceof ModelElement)
 75  8
                                         value = getRuleValue((ModelElement) setElem, attributes,
 76  8
                                                         null);
 77  
                                 else
 78  4
                                         value = String.valueOf(setElem);
 79  
 
 80  8
                                 reportViolation(element, rule, value);
 81  
                         }
 82  
                 }
 83  7
         }
 84  
 }