Coverage Report - com.sdmetrics.metrics.SetProcedureCompare - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
SetProcedureCompare
100%
44/44
100%
22/22
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  
 
 26  
 import com.sdmetrics.math.ExpressionNode;
 27  
 import com.sdmetrics.math.ExpressionParser;
 28  
 import com.sdmetrics.model.MetaModelElement;
 29  
 import com.sdmetrics.model.ModelElement;
 30  
 
 31  
 /**
 32  
  * Calculates the deprecated "compare" set procedure.
 33  
  * <p>
 34  
  * Element comparisons are now just a special case of projections, using the
 35  
  * principal element in the condition expression.
 36  
  */
 37  
 
 38  11
 public class SetProcedureCompare extends SetProcedure {
 39  
 
 40  3
         private final static ExpressionNode SELF_NODE = new ExpressionNode("_self");
 41  6
         private final static ExpressionNode TERM_EQUALS_WITH_NODE = new ExpressionParser()
 42  6
                         .parseExpression("term=with");
 43  
 
 44  
         @Override
 45  
         public Collection<?> calculate(ModelElement e, Set m)
 46  
                         throws SDMetricsException {
 47  12
                 return setCompare(e, m, false);
 48  
         }
 49  
 
 50  
         /**
 51  
          * Calculate a set defined by the "compare" procedure.
 52  
          * 
 53  
          * @param element The model element to calculate the set for.
 54  
          * @param set The set definition.
 55  
          * @param quitOnFirstMatch If <code>true</code>, only the first matching
 56  
          *        element will be returned, else all matching elements will be
 57  
          *        returned.
 58  
          * @return Set with the elements in the projection.
 59  
          * @throws SDMetricsException An error occurred during calculation of the
 60  
          *         set.
 61  
          */
 62  
         @SuppressWarnings("unchecked")
 63  
         Collection<?> setCompare(ModelElement element, Set set,
 64  
                         boolean quitOnFirstMatch) throws SDMetricsException {
 65  15
                 ProcedureAttributes attributes = set.getAttributes();
 66  15
                 Variables vars = new Variables(element);
 67  
 
 68  
                 // evaluate term expression
 69  15
                 ExpressionNode term = attributes.getExpression("term");
 70  19
                 Object termValue = (term == null) ? element : evalExpression(element,
 71  4
                                 term, vars);
 72  15
                 vars.setVariable("term", termValue);
 73  
 
 74  
                 // get elements to compare via target or set attribute
 75  
                 Collection<ModelElement> comparisonSet;
 76  15
                 String targetElement = attributes.getStringValue("target");
 77  15
                 if (targetElement == null) {
 78  11
                         ExpressionNode setExpression = attributes.getExpression("set");
 79  11
                         if (setExpression == null)
 80  2
                                 throw new SDMetricsException(element, set,
 81  1
                                                 "Either the 'target' or the 'set' attribute must be specified.");
 82  20
                         comparisonSet = evalElementSetExpression(element, setExpression,
 83  10
                                         vars);
 84  10
                 } else {
 85  4
                         MetaModelElement targetType = getMetaModel().getType(targetElement);
 86  4
                         if (targetType == null)
 87  2
                                 throw new SDMetricsException(element, set,
 88  2
                                                 "Unknown target model element type '" + targetElement
 89  1
                                                                 + "'.");
 90  3
                         comparisonSet = getModel().getAcceptedElements(targetType);
 91  
                 }
 92  
 
 93  13
                 ExpressionNode withExpression = attributes.getExpression("with");
 94  13
                 ExpressionNode compareExpression = attributes.getExpression("comp");
 95  13
                 if (withExpression == null)
 96  9
                         withExpression = SELF_NODE;
 97  13
                 if (compareExpression == null)
 98  1
                         compareExpression = TERM_EQUALS_WITH_NODE;
 99  
 
 100  
                 @SuppressWarnings("rawtypes")
 101  13
                 Collection result = MetricTools.createHashSet(set.isMultiSet());
 102  
 
 103  13
                 boolean excludeSelf = attributes.getBooleanValue("exclude_self", true);
 104  13
                 FilterAttributeProcessor fap = getFilterAttributeProcessor(attributes);
 105  132
                 for (ModelElement elem : fap.validIteration(comparisonSet, vars)) {
 106  107
                         if (!excludeSelf || elem != element) {
 107  105
                                 Object withValue = evalExpression(elem, withExpression, vars);
 108  105
                                 vars.setVariable("with", withValue);
 109  105
                                 if (evalBooleanExpression(element, compareExpression, vars)) {
 110  19
                                         result.add(elem);
 111  19
                                         if (quitOnFirstMatch)
 112  1
                                                 return result;
 113  
                                 }
 114  
                         }
 115  
                 }
 116  12
                 return result;
 117  
         }
 118  
 }