Coverage Report - com.sdmetrics.metrics.MatrixEngine - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
MatrixEngine
97%
36/37
100%
16/16
3,25
 
 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.ArrayList;
 25  
 import java.util.Collection;
 26  
 import java.util.HashMap;
 27  
 import java.util.List;
 28  
 
 29  
 import com.sdmetrics.math.ExpressionNode;
 30  
 import com.sdmetrics.math.IntegerMatrix;
 31  
 import com.sdmetrics.model.MetaModel;
 32  
 import com.sdmetrics.model.MetaModelElement;
 33  
 import com.sdmetrics.model.Model;
 34  
 import com.sdmetrics.model.ModelElement;
 35  
 
 36  
 /**
 37  
  * Calculates relation matrices.
 38  
  */
 39  
 public class MatrixEngine {
 40  
 
 41  
         /** Metrics engine to use. */
 42  
         private final MetricsEngine engine;
 43  
         /** The model with all available elements. */
 44  
         private final Model model;
 45  
 
 46  
         /** Set to calculate the set definition procedures for the matrices. */
 47  
         private final Set proxy;
 48  
 
 49  
         /**
 50  
          * Creates a new matrix engine.
 51  
          * 
 52  
          * @param me The metrics engine to be used for the calculation.
 53  
          */
 54  2
         public MatrixEngine(MetricsEngine me) {
 55  2
                 this.engine = me;
 56  2
                 model = me.getModel();
 57  
 
 58  4
                 proxy = new Set("dummy", me.getMetaModel().getType(
 59  2
                                 MetaModel.BASE_ELEMENT));
 60  2
                 proxy.setMultiSet(true);
 61  2
         }
 62  
 
 63  
         /**
 64  
          * Returns the metrics engine that this matrix engine uses.
 65  
          * 
 66  
          * @return Metrics engine of the matrix engine.
 67  
          */
 68  
         public MetricsEngine getMetricsEngine() {
 69  0
                 return engine;
 70  
         }
 71  
 
 72  
         /**
 73  
          * Calculates a relation matrix.
 74  
          * 
 75  
          * @param matrix Definition of the matrix to calculate.
 76  
          * @return The matrix
 77  
          * @throws SDMetricsException An error occurred during the calculation of
 78  
          *         the matrix.
 79  
          */
 80  
         public MatrixData calculate(Matrix matrix) throws SDMetricsException {
 81  
                 // create lists of accepted row and column elements
 82  4
                 List<ModelElement> rows = getElementList(matrix.getRowType(),
 83  2
                                 matrix.getRowCondition());
 84  4
                 List<ModelElement> columns = getElementList(matrix.getColumnType(),
 85  2
                                 matrix.getColumnCondition());
 86  
 
 87  
                 // create a mapping from the column elements to their column index
 88  2
                 HashMap<Object, Integer> colIndices = new HashMap<Object, Integer>();
 89  31
                 for (int i = 0; i < columns.size(); i++) {
 90  29
                         colIndices.put(columns.get(i), Integer.valueOf(i));
 91  
                 }
 92  
 
 93  2
                 ProcedureAttributes attributes = matrix.getAttributes();
 94  2
                 proxy.setAttributes(attributes);
 95  
                 // set the procedure name, for backwards compatibility, replace
 96  
                 // deprecated "setoperation" with the current "compoundset"
 97  2
                 String procName = matrix.getProcedureName();
 98  2
                 if ("setoperation".equals(procName))
 99  1
                         procName = "compoundset";
 100  2
                 proxy.setProcedureName(procName);
 101  
 
 102  
                 // Calculate the contents of the matrix
 103  2
                 IntegerMatrix values = new IntegerMatrix();
 104  32
                 for (int row = 0; row < rows.size(); row++) {
 105  30
                         Collection<?> set = engine.computeSet(rows.get(row), proxy);
 106  71
                         for (Object colElement : set) {
 107  11
                                 Integer colIndex = colIndices.get(colElement);
 108  11
                                 if (colIndex != null)
 109  8
                                         values.increment(row, colIndex.intValue());
 110  
                         }
 111  
                 }
 112  
 
 113  2
                 return new MatrixData(matrix, rows, columns, values);
 114  
         }
 115  
 
 116  
         /**
 117  
          * Returns the list of elements of a type that satisfy a given condition.
 118  
          * 
 119  
          * @param type Type of the elements to return
 120  
          * @param condition The condition for the elements to return,
 121  
          *        <code>null</code> to return all elements.
 122  
          * @return List of matching elements
 123  
          * @throws SDMetricsException Error evaluating the condition
 124  
          */
 125  
         private List<ModelElement> getElementList(MetaModelElement type,
 126  
                         ExpressionNode condition) throws SDMetricsException {
 127  
 
 128  4
                 List<ModelElement> candidateElements = model.getAcceptedElements(type);
 129  4
                 if (condition == null)
 130  2
                         return candidateElements;
 131  
 
 132  2
                 List<ModelElement> result = new ArrayList<ModelElement>();
 133  34
                 for (ModelElement elem : candidateElements)
 134  30
                         if (engine.evalBooleanExpression(elem, condition, null))
 135  11
                                 result.add(elem);
 136  2
                 return result;
 137  
         }
 138  
 }