Coverage Report - com.sdmetrics.model.Model - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
ElementFilters
100%
23/23
100%
16/16
3,091
Model
100%
37/37
100%
24/24
3,091
 
 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.model;
 23  
 
 24  
 import java.util.ArrayList;
 25  
 import java.util.HashMap;
 26  
 import java.util.Iterator;
 27  
 import java.util.List;
 28  
 
 29  
 import com.sdmetrics.math.MappedCollectionsIterator;
 30  
 
 31  
 /**
 32  
  * Container for the model elements of the UML design to be analyzed. Provides
 33  
  * access to the model elements and element filtering based on qualified names.
 34  
  */
 35  
 public class Model implements Iterable<ModelElement> {
 36  
         /** Metamodel on which the elements of this model are based. */
 37  
         private final MetaModel metaModel;
 38  
 
 39  
         /**
 40  
          * Data structure to hold all model elements. Elements of each type are
 41  
          * stored in a random access list.
 42  
          */
 43  
         private final HashMap<MetaModelElement, ArrayList<ModelElement>> elementsByType;
 44  
 
 45  
         /**
 46  
          * Data structure to hold the model elements that have been accepted for
 47  
          * output as per the element filter settings.
 48  
          */
 49  
         private HashMap<MetaModelElement, ArrayList<ModelElement>> acceptedElementsByType;
 50  
 
 51  
         /**
 52  
          * Creates a new, empty model.
 53  
          * 
 54  
          * @param metaModel Metamodel that defines the element types and attributes.
 55  
          */
 56  149
         public Model(MetaModel metaModel) {
 57  149
                 this.metaModel = metaModel;
 58  298
                 elementsByType = new HashMap<MetaModelElement, ArrayList<ModelElement>>(
 59  149
                                 metaModel.getNumberOfTypes());
 60  2193
                 for (MetaModelElement type : metaModel)
 61  1895
                         elementsByType.put(type, new ArrayList<ModelElement>());
 62  149
         }
 63  
 
 64  
         /**
 65  
          * Applies filter settings to the elements of this model.
 66  
          * 
 67  
          * @param filterStrings The list of element filters to apply. Can be
 68  
          *        <code>null</code> or empty to disable filtering.
 69  
          * @param acceptMatchingElements Set to <code>true</code> to accept elements
 70  
          *        matching at least on of the element filters. Set to
 71  
          *        <code>false</code> to accept only elements that matching none of
 72  
          *        the element filters.
 73  
          * @param ignoreRelationsToRejectedElements Set to <code>true</code> to
 74  
          *        ignore links to rejected elements for metrics calculation, set to
 75  
          *        <code>false</code> to include links to rejected elements for
 76  
          *        metrics calculation.
 77  
          */
 78  
         public void setFilter(String[] filterStrings,
 79  
                         boolean acceptMatchingElements,
 80  
                         boolean ignoreRelationsToRejectedElements) {
 81  
 
 82  11
                 if (filterStrings == null || filterStrings.length == 0) {
 83  
                         // disable filtering; mark
 84  30
                         for (ModelElement elem : this)
 85  26
                                 elem.setLinksIgnored(false);
 86  2
                         acceptedElementsByType = null; // means no filter is set
 87  2
                         return;
 88  
                 }
 89  
 
 90  9
                 ElementFilters elementFilters = new ElementFilters(filterStrings);
 91  18
                 acceptedElementsByType = new HashMap<MetaModelElement, ArrayList<ModelElement>>(
 92  9
                                 metaModel.getNumberOfTypes());
 93  
 
 94  
                 // iterate over all elements and determine filter status
 95  71
                 for (MetaModelElement type : metaModel) {
 96  53
                         ArrayList<ModelElement> acceptedElementList = new ArrayList<ModelElement>();
 97  53
                         acceptedElementsByType.put(type, acceptedElementList);
 98  
 
 99  280
                         for (ModelElement elem : elementsByType.get(type)) {
 100  
 
 101  
                                 boolean elementAccepted;
 102  174
                                 if (elementFilters.matches(elem))
 103  49
                                         elementAccepted = acceptMatchingElements;
 104  
                                 else
 105  125
                                         elementAccepted = !acceptMatchingElements;
 106  
 
 107  174
                                 if (elementAccepted)
 108  46
                                         acceptedElementList.add(elem);
 109  
 
 110  348
                                 elem.setLinksIgnored(ignoreRelationsToRejectedElements
 111  148
                                                 && !elementAccepted);
 112  
                         }
 113  
                 }
 114  9
         }
 115  
 
 116  
         /**
 117  
          * Returns the list of all elements of a given type. For example, a list of
 118  
          * all classes, all packages, etc. This method ignores filter settings, and
 119  
          * always returns all model elements.
 120  
          * 
 121  
          * @param type The type ID of the elements to return.
 122  
          * @return A random access list of all elements of the specified type.
 123  
          */
 124  
         public List<ModelElement> getElements(MetaModelElement type) {
 125  5008
                 return elementsByType.get(type);
 126  
         }
 127  
 
 128  
         /**
 129  
          * Returns the list of accepted elements of a given type. If the element
 130  
          * filter is active, this method only returns the elements that should
 131  
          * appear in the output data tables, as per the filter settings.
 132  
          * 
 133  
          * @param type The type of the elements to return.
 134  
          * @return A random access list of all accepted elements of the specified
 135  
          *         type.
 136  
          */
 137  
         public List<ModelElement> getAcceptedElements(MetaModelElement type) {
 138  22
                 if (acceptedElementsByType == null)
 139  
                         // filters not set or disabled => return all elements.
 140  14
                         return elementsByType.get(type);
 141  8
                 return acceptedElementsByType.get(type);
 142  
         }
 143  
 
 144  
         /**
 145  
          * Returns an iterator over all model elements of the model, ignoring any
 146  
          * filters settings.
 147  
          * 
 148  
          * @return Iterator over all model elements of all types.
 149  
          */
 150  
         @Override
 151  
         public Iterator<ModelElement> iterator() {
 152  3
                 return new MappedCollectionsIterator<ModelElement>(elementsByType);
 153  
         }
 154  
 
 155  
         /**
 156  
          * Retrieves the metamodel on which this model is based.
 157  
          * 
 158  
          * @return This model's metamodel.
 159  
          */
 160  
         public MetaModel getMetaModel() {
 161  404
                 return metaModel;
 162  
         }
 163  
 
 164  
         /**
 165  
          * Adds a model element to this model.
 166  
          * 
 167  
          * @param el The model element to add.
 168  
          */
 169  
         void addElement(ModelElement el) {
 170  13698
                 elementsByType.get(el.getType()).add(el);
 171  13698
         }
 172  
 
 173  
         /**
 174  
          * Removes a model element from this model.
 175  
          * @param el The model element to remove.
 176  
          */
 177  
         void removeElement(ModelElement el) {
 178  3
                 elementsByType.get(el.getType()).remove(el);
 179  3
         }
 180  
 }
 181  
 
 182  
 /**
 183  
  * Processes the element filter matching.
 184  
  */
 185  
 class ElementFilters {
 186  
         /** Stores the tokenized element filter strings. */
 187  
         private final String[][] filters;
 188  
         /** Name fragments of a model element to test. */
 189  9
         private final ArrayList<String> nameFragments = new ArrayList<String>();
 190  
 
 191  
         /**
 192  
          * @param filterStrings The list of applicable element filter strings.
 193  
          */
 194  9
         ElementFilters(String[] filterStrings) {
 195  
                 // tokenize the list of filter strings
 196  9
                 filters = new String[filterStrings.length][];
 197  24
                 for (int i = 0; i < filters.length; i++)
 198  15
                         filters[i] = filterStrings[i].split("\\.");
 199  9
         }
 200  
 
 201  
         /**
 202  
          * Tests if a model element matches one of the filter strings.
 203  
          * 
 204  
          * @param element the element to test
 205  
          * @return <code>true</code> if the model element matches at least one of
 206  
          *         the filter strings of this filter, <code>false</code> if it
 207  
          *         matches none of the filter strings.
 208  
          */
 209  
         boolean matches(ModelElement element) {
 210  
                 // collect the fragments of the qualified name of the model
 211  174
                 ModelElement me = element;
 212  174
                 nameFragments.clear();
 213  953
                 while (me != null) {
 214  605
                         nameFragments.add(me.getName());
 215  605
                         me = me.getOwner();
 216  
                 }
 217  
 
 218  
                 // check if any of the filter strings gives a match
 219  354
                 for (String[] filter : filters) {
 220  229
                         if (matches(filter))
 221  49
                                 return true;
 222  
                 }
 223  125
                 return false;
 224  
         }
 225  
 
 226  
         private boolean matches(String[] filter) {
 227  229
                 int fragmentCount = nameFragments.size();
 228  229
                 if (fragmentCount < filter.length) // element not "deep" enough
 229  99
                         return false;
 230  437
                 for (int i = 0; i < filter.length; i++) {
 231  388
                         if (!"#".equals(filter[i]))
 232  197
                                 if (!filter[i].equals(nameFragments.get(fragmentCount - i - 1)))
 233  81
                                         return false;
 234  
                 }
 235  49
                 return true;
 236  
         }
 237  
 }