Coverage Report - com.sdmetrics.metrics.RuleProcedureCycle - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
RuleProcedureCycle
100%
32/32
100%
8/8
3
 
 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.HashMap;
 25  
 
 26  
 import com.sdmetrics.model.ModelElement;
 27  
 
 28  
 /**
 29  
  * Checks a "cycle" rule.
 30  
  * <p>
 31  
  * Implementation note on cache usage: this procedure uses the cache of the rule
 32  
  * engine to store the strongly connected components of the graph considered by
 33  
  * the rule. The optimal time to clear the cache is when you are done checking
 34  
  * this rule for all model elements.
 35  
  */
 36  2
 public class RuleProcedureCycle extends RuleProcedure {
 37  
 
 38  
         private final static String CC_CACHE_KEY = "RuleProcedureCycle.connectedComponents";
 39  
 
 40  
         @Override
 41  
         public void checkRule(ModelElement element, Rule rule)
 42  
                         throws SDMetricsException {
 43  
                 // check if connected components have already been calculated for this
 44  
                 // rule
 45  6
                 HashMap<Rule, StronglyConnectedComponents<ModelElement>> ccCache = getConnectedComponentCache();
 46  6
                 StronglyConnectedComponents<ModelElement> scc = ccCache.get(rule);
 47  6
                 if (scc == null) {
 48  
                         // No, so calculate and cache them
 49  2
                         scc = new StronglyConnectedComponents<ModelElement>();
 50  2
                         ProcedureAttributes attrs = rule.getAttributes();
 51  4
                         int minCount = getMinExpressionValue(element, attrs, "minnodes",
 52  2
                                         null);
 53  
 
 54  4
                         ElementGraph graph = new ElementGraph(getMetricsEngine(),
 55  2
                                         getModel().getAcceptedElements(element.getType()),
 56  2
                                         attrs.getRequiredExpression("nodes"), null);
 57  
 
 58  2
                         scc.calculateConnectedComponents(graph, minCount, true);
 59  2
                         ccCache.put(rule, scc);
 60  
                 }
 61  
 
 62  
                 // report if the model element is part of a cycle
 63  6
                 int ccIndex = scc.getIndexFor(element);
 64  6
                 if (ccIndex >= 0) {
 65  5
                         int ccSize = scc.getConnectedComponent(ccIndex).size();
 66  5
                         StringBuilder label = new StringBuilder("cyc# ");
 67  5
                         label.append(ccIndex + 1);
 68  5
                         if (ccSize > 1) {
 69  4
                                 label.append(" (");
 70  4
                                 label.append(ccSize);
 71  4
                                 label.append(" nodes)");
 72  4
                         } else
 73  1
                                 label.append(" (1 node)");
 74  5
                         reportViolation(element, rule, label.toString());
 75  
                 }
 76  6
         }
 77  
 
 78  
         /*
 79  
          * Each cycle rule in the metric definition file calculates the connected
 80  
          * components for one graph, and uses the same graph for all model elements
 81  
          * the rule checks. Therefore, the SCC of the graph are calculated once per
 82  
          * rule, and then stored in the rule engine's cache for the checking of
 83  
          * subsequent elements.
 84  
          */
 85  
         private HashMap<Rule, StronglyConnectedComponents<ModelElement>> getConnectedComponentCache() {
 86  
                 @SuppressWarnings("unchecked")
 87  12
                 HashMap<Rule, StronglyConnectedComponents<ModelElement>> result = (HashMap<Rule, StronglyConnectedComponents<ModelElement>>) getValuesCache()
 88  6
                                 .get(CC_CACHE_KEY);
 89  6
                 if (result == null) {
 90  2
                         result = new HashMap<Rule, StronglyConnectedComponents<ModelElement>>();
 91  2
                         getValuesCache().put(CC_CACHE_KEY, result);
 92  
                 }
 93  6
                 return result;
 94  
         }
 95  
 }