Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CrNoAssociations |
|
| 12.75;12.75 |
1 | /* $Id: CrNoAssociations.java 17849 2010-01-12 19:50:34Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * maurelio1234 | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2006 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.uml.cognitive.critics; | |
40 | ||
41 | import java.util.Collection; | |
42 | import java.util.HashSet; | |
43 | import java.util.Iterator; | |
44 | import java.util.Set; | |
45 | ||
46 | import org.argouml.cognitive.Critic; | |
47 | import org.argouml.cognitive.Designer; | |
48 | import org.argouml.model.Model; | |
49 | import org.argouml.uml.cognitive.UMLDecision; | |
50 | ||
51 | /** | |
52 | * A critic to detect when a classifier might require | |
53 | * associations. It checks for inherited associations as well and | |
54 | * keeps silent if it finds any. For usecases it checks the | |
55 | * extend/include relationships as well. | |
56 | * If the classifier has dependencies defined, then the critic | |
57 | * remains silent (see issue 1129). | |
58 | */ | |
59 | public class CrNoAssociations extends CrUML { | |
60 | ||
61 | /** | |
62 | * Constructor. | |
63 | */ | |
64 | 900 | public CrNoAssociations() { |
65 | 900 | setupHeadAndDesc(); |
66 | 900 | addSupportedDecision(UMLDecision.RELATIONSHIPS); |
67 | 900 | setKnowledgeTypes(Critic.KT_COMPLETENESS); |
68 | 900 | addTrigger("associationEnd"); |
69 | 900 | } |
70 | ||
71 | /** | |
72 | * Decide whether the given design material causes a problem. | |
73 | * | |
74 | * @param dm the object to criticize | |
75 | * the designer who decides the design process | |
76 | * @param dsgr the designer | |
77 | * @return <CODE>PROBLEM_FOUND</CODE> if there is a problem, | |
78 | * otherwise <CODE>NO_PROBLEM</CODE> | |
79 | */ | |
80 | @Override | |
81 | public boolean predicate2(Object dm, Designer dsgr) { | |
82 | 0 | if (!(Model.getFacade().isAClassifier(dm))) |
83 | 0 | return NO_PROBLEM; |
84 | 0 | if (!(Model.getFacade().isPrimaryObject(dm))) |
85 | 0 | return NO_PROBLEM; |
86 | ||
87 | // If the classifier does not have a name, | |
88 | // then no problem - the model is not finished anyhow. | |
89 | 0 | if ((Model.getFacade().getName(dm) == null) |
90 | || ("".equals(Model.getFacade().getName(dm)))) { | |
91 | 0 | return NO_PROBLEM; |
92 | } | |
93 | ||
94 | // Abstract elements do not necessarily require associations | |
95 | 0 | if (Model.getFacade().isAGeneralizableElement(dm) |
96 | && Model.getFacade().isAbstract(dm)) { | |
97 | 0 | return NO_PROBLEM; |
98 | } | |
99 | ||
100 | // Types can probably have associations, but we should not nag at them | |
101 | // not having any. | |
102 | // utility is a namespace collection - also not strictly required | |
103 | // to have associations. | |
104 | 0 | if (Model.getFacade().isType(dm)) |
105 | 0 | return NO_PROBLEM; |
106 | 0 | if (Model.getFacade().isUtility(dm)) |
107 | 0 | return NO_PROBLEM; |
108 | ||
109 | // See issue 1129: If the classifier has dependencies, | |
110 | // then mostly there is no problem. | |
111 | 0 | if (Model.getFacade().getClientDependencies(dm).size() > 0) |
112 | 0 | return NO_PROBLEM; |
113 | 0 | if (Model.getFacade().getSupplierDependencies(dm).size() > 0) |
114 | 0 | return NO_PROBLEM; |
115 | ||
116 | // special cases for use cases | |
117 | // Extending use cases and use case that are being included are | |
118 | // not required to have associations. | |
119 | 0 | if (Model.getFacade().isAUseCase(dm)) { |
120 | 0 | Object usecase = dm; |
121 | 0 | Collection includes = Model.getFacade().getIncludes(usecase); |
122 | 0 | if (includes != null && includes.size() >= 1) { |
123 | 0 | return NO_PROBLEM; |
124 | } | |
125 | 0 | Collection extend = Model.getFacade().getExtends(usecase); |
126 | 0 | if (extend != null && extend.size() >= 1) { |
127 | 0 | return NO_PROBLEM; |
128 | } | |
129 | } | |
130 | ||
131 | ||
132 | ||
133 | //TODO: different critic or special message for classes | |
134 | //that inherit all ops but define none of their own. | |
135 | ||
136 | 0 | if (findAssociation(dm, 0)) |
137 | 0 | return NO_PROBLEM; |
138 | 0 | return PROBLEM_FOUND; |
139 | } | |
140 | ||
141 | /** | |
142 | * @param dm The classifier to examine. | |
143 | * @param depth Number of levels searched. | |
144 | * @return true if an association can be found in this classifier | |
145 | * or in any of its generalizations. | |
146 | */ | |
147 | private boolean findAssociation(Object dm, int depth) { | |
148 | 0 | if (Model.getFacade().getAssociationEnds(dm).iterator().hasNext()) |
149 | 0 | return true; |
150 | ||
151 | 0 | if (depth > 50) |
152 | 0 | return false; |
153 | ||
154 | 0 | Iterator iter = Model.getFacade().getGeneralizations(dm).iterator(); |
155 | ||
156 | 0 | while (iter.hasNext()) { |
157 | 0 | Object parent = Model.getFacade().getGeneral(iter.next()); |
158 | ||
159 | 0 | if (parent == dm) |
160 | 0 | continue; |
161 | ||
162 | 0 | if (Model.getFacade().isAClassifier(parent)) |
163 | 0 | if (findAssociation(parent, depth + 1)) |
164 | 0 | return true; |
165 | 0 | } |
166 | ||
167 | 0 | if (Model.getFacade().isAUseCase(dm)) { |
168 | // for use cases we need to check for extend/includes | |
169 | // actors cannot have them, so no need to check | |
170 | 0 | Iterator iter2 = Model.getFacade().getExtends(dm).iterator(); |
171 | 0 | while (iter2.hasNext()) { |
172 | 0 | Object parent = Model.getFacade().getExtension(iter2.next()); |
173 | ||
174 | 0 | if (parent == dm) |
175 | 0 | continue; |
176 | ||
177 | 0 | if (Model.getFacade().isAClassifier(parent)) |
178 | 0 | if (findAssociation(parent, depth + 1)) |
179 | 0 | return true; |
180 | 0 | } |
181 | ||
182 | 0 | Iterator iter3 = Model.getFacade().getIncludes(dm).iterator(); |
183 | 0 | while (iter3.hasNext()) { |
184 | 0 | Object parent = Model.getFacade().getBase(iter3.next()); |
185 | ||
186 | 0 | if (parent == dm) |
187 | 0 | continue; |
188 | ||
189 | 0 | if (Model.getFacade().isAClassifier(parent)) |
190 | 0 | if (findAssociation(parent, depth + 1)) |
191 | 0 | return true; |
192 | 0 | } |
193 | } | |
194 | 0 | return false; |
195 | } | |
196 | ||
197 | /* | |
198 | * @see org.argouml.uml.cognitive.critics.CrUML#getCriticizedDesignMaterials() | |
199 | */ | |
200 | public Set<Object> getCriticizedDesignMaterials() { | |
201 | 900 | Set<Object> ret = new HashSet<Object>(); |
202 | 900 | ret.add(Model.getMetaTypes().getUMLClass()); |
203 | 900 | ret.add(Model.getMetaTypes().getActor()); |
204 | 900 | ret.add(Model.getMetaTypes().getUseCase()); |
205 | 900 | return ret; |
206 | } | |
207 | ||
208 | } |