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