Coverage Report - com.sdmetrics.metrics.MetricProcedureConnectedComponents - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
MetricProcedureConnectedComponents
95%
23/24
100%
4/4
1,667
 
 1  0
 /*
 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.ModelElement;
 28  
 
 29  
 /**
 30  
  * Calculates a "connectedcomponents" metric procedure.
 31  
  */
 32  5
 public class MetricProcedureConnectedComponents extends MetricProcedure {
 33  
 
 34  
         /**
 35  
          * @return Number of connected components as defined by the metric.
 36  
          */
 37  
         @Override
 38  
         public Integer calculate(ModelElement element, Metric metric)
 39  
                         throws SDMetricsException {
 40  6
                 return Integer.valueOf(connectedComponents(element, metric)
 41  3
                                 .getConnectedComponentCount());
 42  
         }
 43  
 
 44  
         /*
 45  
          * Performs the actual calculation of the metric.
 46  
          */
 47  
         private StronglyConnectedComponents<ModelElement> connectedComponents(
 48  
                         ModelElement element, Metric metric) throws SDMetricsException {
 49  
 
 50  4
                 ProcedureAttributes attributes = metric.getAttributes();
 51  4
                 Variables vars = new Variables(element);
 52  
 
 53  4
                 int minNodes = 1; // minimum size of connected components to report
 54  4
                 ExpressionNode minnExpr = attributes.getExpression("minnodes");
 55  4
                 if (minnExpr != null)
 56  2
                         minNodes = ((Number) evalExpression(element, minnExpr, vars))
 57  1
                                         .intValue();
 58  
 
 59  8
                 Collection<ModelElement> nodeSet = evalElementSetExpression(element,
 60  4
                                 attributes.getRequiredExpression("set"), vars);
 61  4
                 ExpressionNode neighborExpr = attributes.getRequiredExpression("nodes");
 62  8
                 StronglyConnectedComponents.Graph<ModelElement> graph = new ElementGraph(
 63  4
                                 getMetricsEngine(), nodeSet, neighborExpr, vars);
 64  
 
 65  4
                 boolean undirected = attributes.getBooleanValue("undirected", false);
 66  4
                 if (undirected)
 67  1
                         graph = new ChangeDirectionsAdapter<ModelElement>(graph, true);
 68  
 
 69  4
                 StronglyConnectedComponents<ModelElement> scc = new StronglyConnectedComponents<ModelElement>();
 70  4
                 scc.calculateConnectedComponents(graph, minNodes, false);
 71  4
                 return scc;
 72  
         }
 73  
 
 74  
         /**
 75  
          * Calculates the connected components for a model element.
 76  
          * 
 77  
          * @param engine The metrics engine to calculate the connected components
 78  
          * @param element Model element for which to calculate the connected
 79  
          *        components
 80  
          * @param metric Metric that defines the connected components
 81  
          * @return The connected components for the specified model element and
 82  
          *         metric.
 83  
          * @throws SDMetricsException Problem during the calculation
 84  
          */
 85  
         public StronglyConnectedComponents<ModelElement> getConnectedComponents(
 86  
                         MetricsEngine engine, ModelElement element, Metric metric)
 87  
                         throws SDMetricsException {
 88  1
                 setMetricsEngine(engine);
 89  1
                 return connectedComponents(element, metric);
 90  
         }
 91  
 }