1 package org.andromda.core.uml14; 2 3 import java.util.Collection; 4 import java.util.Collections; 5 import java.util.Iterator; 6 import java.util.Vector; 7 8 import org.andromda.core.common.ScriptHelper; 9 import org.andromda.core.common.StringUtilsHelper; 10 import org.omg.uml.foundation.core.Abstraction; 11 import org.omg.uml.foundation.core.AssociationEnd; 12 import org.omg.uml.foundation.core.Attribute; 13 import org.omg.uml.foundation.core.Classifier; 14 import org.omg.uml.foundation.core.Dependency; 15 import org.omg.uml.foundation.core.GeneralizableElement; 16 import org.omg.uml.foundation.core.Generalization; 17 import org.omg.uml.foundation.core.ModelElement; 18 import org.omg.uml.foundation.core.Operation; 19 import org.omg.uml.foundation.core.StructuralFeature; 20 import org.omg.uml.foundation.core.TaggedValue; 21 22 /*** 23 * Extends the UMLDefaultHelper with a set of operations that are useful 24 * for exploring the static parts of UML v1.4 based object models. 25 * 26 *@author Anthony Mowers 27 */ 28 public class UMLStaticHelper extends UMLDefaultHelper implements ScriptHelper { 29 30 /*** 31 * Returns the name of a model element fully qualified by the 32 * name of the package that contains it. If the model element 33 * is a primitive type it will return the primitive type itself. 34 * 35 *@param object model element 36 *@return fully qualifed name 37 */ 38 public String getFullyQualifiedName(Object object) { 39 if ((object == null) || !(object instanceof ModelElement)) { 40 return null; 41 } 42 43 ModelElement modelElement = (ModelElement) object; 44 45 String fullName = modelElement.getName(); 46 47 if (StringUtilsHelper.isPrimitiveType(fullName)) { 48 return fullName; 49 } 50 51 String packageName = getPackageName(modelElement); 52 fullName = 53 "".equals(packageName) ? fullName : packageName + "." + fullName; 54 55 return fullName; 56 } 57 58 /*** 59 * Returns the collection of taggedValues for a given modelElement 60 * 61 * @param object model element 62 * @return Collection of org.omg.uml.foundation.core.TaggedValue 63 * 64 */ 65 public Collection getTaggedValues(Object object) { 66 if ((object == null) || !(object instanceof ModelElement)) { 67 return Collections.EMPTY_LIST; 68 } 69 70 ModelElement modelElement = (ModelElement) object; 71 72 return modelElement.getTaggedValue(); 73 } 74 75 /*** 76 * Searches a collection of tag values for one with a particular 77 * name 78 * 79 * @param Collection of taggedValues 80 * @param tagName name of tag for which to search 81 * 82 * @return value of tag, null if tag not found 83 */ 84 public String findTagValue(Collection taggedValues, String tagName) { 85 for (Iterator i = taggedValues.iterator(); i.hasNext();) { 86 TaggedValue taggedValue = (TaggedValue) i.next(); 87 String tgvName = getName(taggedValue); 88 89 if (tagName.equals(tgvName)) { 90 Iterator it = taggedValue.getDataValue().iterator(); 91 if (it.hasNext()) { 92 return it.next().toString(); 93 } 94 return null; 95 } 96 } 97 98 return null; 99 } 100 101 /*** 102 * Searches for and returns the value of a given tag on 103 * the specified model element. 104 * 105 * @param modelElement model element 106 * @param tagName name of the tag 107 * @return String value of tag, <b>null</b> if tag not found 108 * 109 */ 110 public String findTagValue(ModelElement modelElement, String tagName) { 111 return findTagValue(getTaggedValues(modelElement), tagName); 112 } 113 114 /*** 115 * Returns Association information from the perspective of 116 * a particular end of the association. 117 * 118 * <p>The returned object can answers information about 119 * whether the assocation is one2many, many2one, ... 120 * </p> 121 * 122 * @param object assocation end 123 * @return DirectionalAssociationEnd directional association data 124 * 125 */ 126 public DirectionalAssociationEnd getAssociationData(Object object) { 127 if ((object == null) || !(object instanceof AssociationEnd)) { 128 return null; 129 } 130 131 AssociationEnd ae = (AssociationEnd) object; 132 133 return new DirectionalAssociationEnd(ae); 134 } 135 136 /*** 137 * Searches the given class feature (operation or attribute) for 138 * the specified tag. 139 * 140 * <p>If the follow boolean is set to true then the search will 141 * continue from the class feature to the class itself and then 142 * up the class hiearchy.</p> 143 * 144 * @param feature attribute or operation object 145 * @param tagName name of the tag to search for 146 * @param follow <b>true</b> if search should follow inheritance 147 * hierarchy 148 * @return String value of tag, <b>null</b> if tag not found 149 * 150 */ 151 public String findTagValue( 152 StructuralFeature feature, 153 String tagName, 154 boolean follow) { 155 if (feature == null) 156 return null; 157 158 String value = findTagValue(feature, tagName); 159 ModelElement element = feature.getType(); 160 while ((value == null) && (element != null)) { 161 value = findTagValue(element, tagName); 162 element = getGeneralization(element); 163 } 164 165 return value; 166 } 167 168 /*** 169 * Returns the collection of dependencies for a given model element. 170 * 171 * <p>Abstraction/Interface implements dependencies will not be 172 * included in this collection.</b> 173 * 174 *@param object model element 175 *@return Collection of org.omg.uml.foundation.core.Dependency 176 */ 177 public Collection getDependencies(Object object) { 178 if ((object == null) || !(object instanceof ModelElement)) { 179 return Collections.EMPTY_LIST; 180 } 181 182 ModelElement modelElement = (ModelElement) object; 183 184 Collection clientDependencies = 185 model.getCore().getAClientClientDependency().getClientDependency( 186 modelElement); 187 188 return new FilteredCollection(clientDependencies) { 189 protected boolean accept(Object object) { 190 return (object instanceof Dependency) 191 && !(object instanceof Abstraction); 192 } 193 }; 194 } 195 196 /*** 197 * Gets the attributes of the specified Classifier object. 198 * 199 *@param object Classifier object 200 *@return Collection of org.omg.uml.foundation.core.Attribute 201 */ 202 public Collection getAttributes(Object object) { 203 if ((object == null) || !(object instanceof Classifier)) { 204 return Collections.EMPTY_LIST; 205 } 206 207 Classifier classifier = (Classifier) object; 208 Collection features = new FilteredCollection(classifier.getFeature()) { 209 protected boolean accept(Object object) { 210 return object instanceof Attribute; 211 } 212 }; 213 214 return features; 215 } 216 217 /*** 218 * Gets the operations of the specified Classifier object. 219 * 220 *@param object Classifier object 221 *@return Collection of org.omg.uml.foundation.core.Operation 222 */ 223 public Collection getOperations(Object object) { 224 if ((object == null) || !(object instanceof Classifier)) { 225 return Collections.EMPTY_LIST; 226 } 227 228 Classifier classifier = (Classifier) object; 229 Collection features = new FilteredCollection(classifier.getFeature()) { 230 protected boolean accept(Object object) { 231 return object instanceof Operation; 232 } 233 }; 234 235 return features; 236 } 237 238 /*** 239 * Gets the assocation ends that are attached to the specified 240 * Classifier object. 241 * 242 *@param object Classifier object 243 *@return Collection of org.omg.uml.foundation.core.AssociationEnd 244 */ 245 public Collection getAssociationEnds(Object object) { 246 if ((object == null) || !(object instanceof Classifier)) { 247 return Collections.EMPTY_LIST; 248 } 249 250 Classifier classifier = (Classifier) object; 251 return model.getCore().getAParticipantAssociation().getAssociation( 252 classifier); 253 } 254 255 /*** 256 * Returns the generalization/superclass for the given model generalizable 257 * model element (i.e. Class). 258 * 259 * @param object model element 260 * @return GeneralizableElement super class 261 */ 262 public GeneralizableElement getGeneralization(Object object) { 263 if ((object == null) || !(object instanceof GeneralizableElement)) { 264 return null; 265 } 266 267 GeneralizableElement element = (GeneralizableElement) object; 268 Iterator i = element.getGeneralization().iterator(); 269 270 if (i.hasNext()) { 271 Generalization generalization = (Generalization) i.next(); 272 return generalization.getParent(); 273 } 274 275 return null; 276 } 277 278 /*** 279 * Returns the collection of interfaces implemented by the given 280 * Classifier object. 281 * 282 * @param object Class 283 * @return Collection of Interfaces 284 */ 285 public Collection getAbstractions(Object object) { 286 if ((object == null) || !(object instanceof Classifier)) { 287 return Collections.EMPTY_LIST; 288 } 289 290 ModelElement modelElement = (ModelElement) object; 291 292 Collection clientDependencies = 293 model.getCore().getAClientClientDependency().getClientDependency( 294 modelElement); 295 296 return new FilteredCollection(clientDependencies) { 297 public boolean add(Object object) { 298 Abstraction abstraction = (Abstraction) object; 299 return super.add(abstraction.getSupplier().iterator().next()); 300 } 301 302 protected boolean accept(Object object) { 303 return object instanceof Abstraction; 304 } 305 }; 306 } 307 308 /*** 309 * Filters a collection of objects so that the collection 310 * contains only those objects that pass the 'accept' test. 311 * 312 * <p>It is useful for filtering the results of a query.</p> 313 * 314 *@author Anthony Mowers 315 */ 316 private abstract static class FilteredCollection extends Vector { 317 /*** 318 * Constructor for the FilterCollection object 319 * 320 *@param c Description of the Parameter 321 */ 322 public FilteredCollection(Collection c) { 323 for (Iterator i = c.iterator(); i.hasNext();) { 324 Object object = i.next(); 325 if (accept(object)) { 326 add(object); 327 } 328 } 329 } 330 331 /*** 332 * Description of the Method 333 * 334 *@param object Description of the Parameter 335 *@return Description of the Return Value 336 */ 337 protected abstract boolean accept(Object object); 338 } 339 340 }