PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/umltuowl/src/main/java/org/cdlflex/mdd/umltuowl/metamodel/MetaPackage.java

https://bitbucket.org/agruenwald/cdlflex-mdd
Java | 275 lines | 190 code | 27 blank | 58 comment | 35 complexity | 76e7d9e516cfaf40c231dcd5dae7463a MD5 | raw file
  1. package org.cdlflex.mdd.umltuowl.metamodel;
  2. import java.util.Iterator;
  3. import java.util.LinkedHashSet;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.Set;
  7. /**
  8. * @author Andreas Gruenwald <a.gruenw@gmail.com> Represents a single UML package an is transformed into a single
  9. * ontology. Packages are defined differently in UML tools: In Visual Paradigm a package usually is presented in
  10. * a new class diagram. Therefore, one has to differ between default UML packages and UML tool constructs.
  11. * Packages therefore not always might be supported.
  12. */
  13. public class MetaPackage extends MetaElement {
  14. private static final long serialVersionUID = 1L;
  15. private String name;
  16. private Set<MetaClass> classes;
  17. /**
  18. * Create the package. Usually, each package is transformed into a single OWL Ontology, but perhaps they also may be
  19. * merged into a single one.
  20. *
  21. * @param name the package name - must be unique. case sensitive.
  22. */
  23. public MetaPackage(String name) {
  24. super();
  25. this.classes = this.initSet();
  26. this.name = name;
  27. }
  28. /**
  29. * Creates a new meta package without name. This constructor must be avoided and should only be used for automated
  30. * processing, such as serialization.
  31. */
  32. public MetaPackage() {
  33. this("");
  34. }
  35. /**
  36. * @return returns a set of all classes in the package, including interfaces and abstract classes.
  37. */
  38. public Set<MetaClass> getClasses() {
  39. return classes;
  40. }
  41. /**
  42. * @param classes set meta model classes or add one/multiple classes/ interfaces/abstract classes to the meta
  43. * package.
  44. */
  45. public void setClasses(Set<MetaClass> classes) {
  46. this.classes = classes;
  47. }
  48. /**
  49. * find a meta class by its identifier (internal) an return it.
  50. *
  51. * @param classId unique class id (!= class name).
  52. * @return the meta class with the given class id or {@code null}, if no meta class matched.
  53. */
  54. public MetaClass findClassById(String classId) {
  55. Iterator<MetaClass> it = this.classes.iterator();
  56. while (it.hasNext()) {
  57. MetaClass c = it.next();
  58. if (c.getId().equals(classId)) {
  59. return c;
  60. }
  61. }
  62. return null;
  63. }
  64. /**
  65. * find a meta class by its name and return it.
  66. *
  67. * @param className unique class name
  68. * @return the meta class with the given class name or {@code null}, if no meta class matched.
  69. */
  70. public MetaClass findClassByName(String className) {
  71. Iterator<MetaClass> it = this.classes.iterator();
  72. while (it.hasNext()) {
  73. MetaClass c = it.next();
  74. if (c.getName().equalsIgnoreCase(className)) {
  75. return c;
  76. }
  77. }
  78. return null;
  79. }
  80. /**
  81. * find a meta class by its name and return it. In some cases it can be, that a class names occurs multiple time,
  82. * e.g. in ArgoUML all classes (from different drawings) are merged into a single XMI file.
  83. *
  84. * @param className unique class name
  85. * @return a list of the meta classes found with the given class name or an empty list, if if no meta classes
  86. * matched.
  87. */
  88. public List<MetaClass> findMultipleClassesByName(String className) {
  89. List<MetaClass> result = new LinkedList<MetaClass>();
  90. Iterator<MetaClass> it = this.classes.iterator();
  91. while (it.hasNext()) {
  92. MetaClass c = it.next();
  93. if (c.getName().equals(className)) {
  94. result.add(c);
  95. }
  96. }
  97. return result;
  98. }
  99. /**
  100. * @return return a list of all classes sorted (interfaces,abstracts,concrete classes).
  101. */
  102. public List<MetaClass> findAllClassesSortedByAbstract() {
  103. List<MetaClass> interfaces = new LinkedList<MetaClass>();
  104. List<MetaClass> abstracts = new LinkedList<MetaClass>();
  105. List<MetaClass> concretes = new LinkedList<MetaClass>();
  106. for (MetaClass metaClass : this.classes) {
  107. if (metaClass.isAbstractClass()) {
  108. abstracts.add(metaClass);
  109. } else if (metaClass.isInterfaceClass()) {
  110. interfaces.add(metaClass);
  111. } else {
  112. concretes.add(metaClass);
  113. }
  114. }
  115. List<MetaClass> list = new LinkedList<MetaClass>();
  116. list.addAll(interfaces);
  117. list.addAll(abstracts);
  118. list.addAll(concretes);
  119. return list;
  120. }
  121. public int getSizeAssociations() {
  122. int size = 0;
  123. int bi = 0;
  124. int uni = 0;
  125. Iterator<MetaClass> it = this.getClasses().iterator();
  126. while (it.hasNext()) {
  127. MetaClass c = it.next();
  128. for (MetaAssociation a : c.getAssociations()) {
  129. if (a.getInverseAssociation() == null) {
  130. bi++;
  131. } else {
  132. uni++;
  133. }
  134. }
  135. }
  136. size = bi + uni / 2;
  137. return size;
  138. }
  139. public int getSizeAttributes() {
  140. int size = 0;
  141. Iterator<MetaClass> it = this.getClasses().iterator();
  142. while (it.hasNext()) {
  143. MetaClass c = it.next();
  144. size += c.getAttributes().size();
  145. }
  146. return size;
  147. }
  148. public int getSizeGeneralizations() {
  149. Iterator<MetaClass> it = this.getClasses().iterator();
  150. int size = 0;
  151. while (it.hasNext()) {
  152. MetaClass c = it.next();
  153. size += c.getSuperclasses().size();
  154. }
  155. return size;
  156. }
  157. /**
  158. * Modify the name of a package.
  159. *
  160. * @param name The new name of the package, must not be null. Can be used for instance to update a package name
  161. * according to Java package conventions, etc.
  162. */
  163. public void setName(String name) {
  164. this.name = name;
  165. }
  166. /**
  167. * @return package name.
  168. */
  169. @Override
  170. public String getName() {
  171. return name;
  172. }
  173. /**
  174. * @return whole model is returned as a string containing package, class and attribute information.
  175. */
  176. @Override
  177. public String toString() {
  178. String s = "";
  179. Iterator<MetaComment> itComments = this.getComment().iterator();
  180. while (itComments.hasNext()) {
  181. MetaComment c = itComments.next();
  182. s += " Comment: " + c.getText() + "\n";
  183. }
  184. Iterator<MetaClass> it = this.classes.iterator();
  185. while (it.hasNext()) {
  186. MetaClass clazz = it.next();
  187. String attrs = "";
  188. if (clazz.isAbstractClass() || clazz.isInterfaceClass()) {
  189. attrs = clazz.isAbstractClass() ? "abstract" : "";
  190. if (!attrs.isEmpty()) {
  191. attrs = attrs + (clazz.isInterfaceClass() ? ",interface" : "");
  192. } else {
  193. attrs = attrs + (clazz.isInterfaceClass() ? "interface" : "");
  194. }
  195. attrs = " (" + attrs + ")";
  196. }
  197. s += "Class " + clazz + attrs + "\n";
  198. Iterator<MetaClass> itSubclasses = clazz.getSubclasses().iterator();
  199. while (itSubclasses.hasNext()) {
  200. MetaClass subclass = itSubclasses.next();
  201. s += "==> subclass " + subclass + "\n";
  202. }
  203. Iterator<MetaClass> itSuperclasses = clazz.getSuperclasses().iterator();
  204. while (itSuperclasses.hasNext()) {
  205. MetaClass superclass = itSuperclasses.next();
  206. s += "==> superclass " + superclass + "\n";
  207. }
  208. itComments = clazz.getComment().iterator();
  209. while (itComments.hasNext()) {
  210. MetaComment c = itComments.next();
  211. s += " Comment: " + c.getText() + "\n";
  212. }
  213. Iterator<MetaAttribute> itAttributes = clazz.getAttributes().iterator();
  214. while (itAttributes.hasNext()) {
  215. MetaAttribute attr = itAttributes.next();
  216. s += " " + attr;
  217. }
  218. Iterator<MetaAssociation> itAssociation = clazz.getAssociations().iterator();
  219. while (itAssociation.hasNext()) {
  220. MetaAssociation ass = itAssociation.next();
  221. s += " + " + ass + "\n";
  222. }
  223. }
  224. return s;
  225. }
  226. @Override
  227. public boolean equals(Object o) {
  228. MetaPackage p1 = (MetaPackage) o;
  229. if (p1.getName().isEmpty() && this.getName().isEmpty()) {
  230. return super.equals(o);
  231. } else if (p1.getName().equals(this.getName())) {
  232. return true;
  233. } else {
  234. return false;
  235. }
  236. }
  237. public void sort() {
  238. Set<MetaClass> classes = initSet();
  239. classes.addAll(this.classes);
  240. this.classes = classes;
  241. for (MetaClass mc : this.classes) {
  242. mc.sort();
  243. }
  244. }
  245. private Set<MetaClass> initSet() {
  246. return new LinkedHashSet<>();
  247. }
  248. }