Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CrNameConfusion |
|
| 4.0;4 |
1 | /* $Id: CrNameConfusion.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-2007 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 javax.swing.Icon; | |
47 | ||
48 | import org.argouml.cognitive.Critic; | |
49 | import org.argouml.cognitive.Designer; | |
50 | import org.argouml.cognitive.ListSet; | |
51 | import org.argouml.cognitive.ToDoItem; | |
52 | import org.argouml.cognitive.critics.Wizard; | |
53 | import org.argouml.model.Model; | |
54 | import org.argouml.uml.cognitive.UMLDecision; | |
55 | import org.argouml.uml.cognitive.UMLToDoItem; | |
56 | ||
57 | /** | |
58 | * Well-formedness rule [1] for Namespace. See section 2.5.3.26 of | |
59 | * UML 1.4 spec. Rule [2] is checked by CrAssocNameConflict. | |
60 | */ | |
61 | public class CrNameConfusion extends CrUML { | |
62 | ||
63 | /** | |
64 | * The serialVersionUID | |
65 | */ | |
66 | private static final long serialVersionUID = -6659510145586121263L; | |
67 | ||
68 | /** | |
69 | * The constructor. | |
70 | */ | |
71 | 900 | public CrNameConfusion() { |
72 | 900 | setupHeadAndDesc(); |
73 | 900 | addSupportedDecision(UMLDecision.NAMING); |
74 | 900 | setKnowledgeTypes(Critic.KT_PRESENTATION); |
75 | 900 | setKnowledgeTypes(Critic.KT_SYNTAX); |
76 | 900 | addTrigger("name"); |
77 | 900 | } |
78 | ||
79 | /* | |
80 | * @see org.argouml.uml.cognitive.critics.CrUML#predicate2( | |
81 | * java.lang.Object, org.argouml.cognitive.Designer) | |
82 | */ | |
83 | public boolean predicate2(Object dm, Designer dsgr) { | |
84 | 571 | if (!(Model.getFacade().isAModelElement(dm)) |
85 | || Model.getFacade().isAAssociation(dm) | |
86 | // UML 1.4 spec is ambiguous - English says no Association or | |
87 | // Generalization, but OCL only includes Association | |
88 | // || Model.getFacade().isAGeneralization(dm) | |
89 | ) { | |
90 | 0 | return NO_PROBLEM; |
91 | } | |
92 | 571 | Object me = dm; |
93 | 571 | ListSet offs = computeOffenders(me); |
94 | 571 | if (offs.size() > 1) { |
95 | 0 | return PROBLEM_FOUND; |
96 | } | |
97 | 571 | return NO_PROBLEM; |
98 | } | |
99 | ||
100 | /** | |
101 | * @param dm the given modelelement | |
102 | * @return the ListSet of offenders | |
103 | */ | |
104 | public ListSet computeOffenders(Object dm) { | |
105 | 571 | Object ns = Model.getFacade().getNamespace(dm); |
106 | 571 | ListSet res = new ListSet(dm); |
107 | 571 | String n = Model.getFacade().getName(dm); |
108 | 571 | if (n == null || n.equals("")) { |
109 | 0 | return res; |
110 | } | |
111 | 571 | String dmNameStr = n; |
112 | 571 | if (dmNameStr == null || dmNameStr.length() == 0) { |
113 | 0 | return res; |
114 | } | |
115 | 571 | String stripped2 = strip(dmNameStr); |
116 | 571 | if (ns == null) { |
117 | 571 | return res; |
118 | } | |
119 | 0 | Collection oes = Model.getFacade().getOwnedElements(ns); |
120 | 0 | if (oes == null) { |
121 | 0 | return res; |
122 | } | |
123 | 0 | Iterator elems = oes.iterator(); |
124 | 0 | while (elems.hasNext()) { |
125 | 0 | Object me2 = elems.next(); |
126 | 0 | if (me2 == dm || Model.getFacade().isAAssociation(me2)) { |
127 | 0 | continue; |
128 | } | |
129 | 0 | String meName = Model.getFacade().getName(me2); |
130 | 0 | if (meName == null || meName.equals("")) { |
131 | 0 | continue; |
132 | } | |
133 | 0 | String compareName = meName; |
134 | 0 | if (confusable(stripped2, strip(compareName)) |
135 | && !dmNameStr.equals(compareName)) { | |
136 | 0 | res.add(me2); |
137 | } | |
138 | 0 | } |
139 | 0 | return res; |
140 | } | |
141 | ||
142 | /* | |
143 | * @see org.argouml.cognitive.critics.Critic#toDoItem( java.lang.Object, | |
144 | * org.argouml.cognitive.Designer) | |
145 | */ | |
146 | @Override | |
147 | public ToDoItem toDoItem(Object dm, Designer dsgr) { | |
148 | 0 | Object me = dm; |
149 | 0 | ListSet offs = computeOffenders(me); |
150 | 0 | return new UMLToDoItem(this, offs, dsgr); |
151 | } | |
152 | ||
153 | /* | |
154 | * @see org.argouml.cognitive.Poster#stillValid( | |
155 | * org.argouml.cognitive.ToDoItem, org.argouml.cognitive.Designer) | |
156 | */ | |
157 | @Override | |
158 | public boolean stillValid(ToDoItem i, Designer dsgr) { | |
159 | 0 | if (!isActive()) { |
160 | 0 | return false; |
161 | } | |
162 | 0 | ListSet offs = i.getOffenders(); |
163 | 0 | Object dm = offs.get(0); |
164 | 0 | if (!predicate(dm, dsgr)) { |
165 | 0 | return false; |
166 | } | |
167 | 0 | ListSet newOffs = computeOffenders(dm); |
168 | 0 | boolean res = offs.equals(newOffs); |
169 | 0 | return res; |
170 | } | |
171 | ||
172 | /** | |
173 | * @param stripped1 given string 1 | |
174 | * @param stripped2 given string 2 | |
175 | * @return true if the both given strings are confusingly similar | |
176 | */ | |
177 | public boolean confusable(String stripped1, String stripped2) { | |
178 | 0 | int countDiffs = countDiffs(stripped1, stripped2); |
179 | 0 | return countDiffs <= 1; |
180 | } | |
181 | ||
182 | /** | |
183 | * @param s1 given string 1 | |
184 | * @param s2 given string 2 | |
185 | * @return positive int, representing the number of different chars, or | |
186 | * if the lengths differ more than 2, this length difference | |
187 | */ | |
188 | public int countDiffs(String s1, String s2) { | |
189 | 0 | int len = Math.min(s1.length(), s2.length()); |
190 | 0 | int count = Math.abs(s1.length() - s2.length()); |
191 | 0 | if (count > 2) { |
192 | 0 | return count; |
193 | } | |
194 | 0 | for (int i = 0; i < len; i++) { |
195 | 0 | if (s1.charAt(i) != s2.charAt(i)) { |
196 | 0 | count++; |
197 | } | |
198 | } | |
199 | 0 | return count; |
200 | } | |
201 | ||
202 | /** | |
203 | * @param s the given string | |
204 | * @return the string s with all non-letters/digits stripped off | |
205 | */ | |
206 | public String strip(String s) { | |
207 | 571 | StringBuffer res = new StringBuffer(s.length()); |
208 | 571 | int len = s.length(); |
209 | 2284 | for (int i = 0; i < len; i++) { |
210 | 1713 | char c = s.charAt(i); |
211 | 1713 | if (Character.isLetterOrDigit(c)) { |
212 | 1713 | res.append(Character.toLowerCase(c)); |
213 | 0 | } else if (c == ']' && i > 1 && s.charAt(i - 1) == '[') { |
214 | 0 | res.append("[]"); |
215 | } | |
216 | } | |
217 | 571 | return res.toString(); |
218 | } | |
219 | ||
220 | /* | |
221 | * @see org.argouml.cognitive.Poster#getClarifier() | |
222 | */ | |
223 | @Override | |
224 | public Icon getClarifier() { | |
225 | 0 | return ClClassName.getTheInstance(); |
226 | } | |
227 | ||
228 | ||
229 | /* | |
230 | * @see org.argouml.cognitive.critics.Critic#initWizard( | |
231 | * org.argouml.cognitive.ui.Wizard) | |
232 | */ | |
233 | @Override | |
234 | public void initWizard(Wizard w) { | |
235 | 0 | if (w instanceof WizManyNames) { |
236 | 0 | ToDoItem item = (ToDoItem) w.getToDoItem(); |
237 | 0 | ((WizManyNames) w).setModelElements(item.getOffenders()); |
238 | } | |
239 | 0 | } |
240 | ||
241 | /* | |
242 | * @see org.argouml.cognitive.critics.Critic#getWizardClass(org.argouml.cognitive.ToDoItem) | |
243 | */ | |
244 | public Class getWizardClass(ToDoItem item) { | |
245 | 0 | return WizManyNames.class; |
246 | } | |
247 | ||
248 | /* | |
249 | * @see org.argouml.uml.cognitive.critics.CrUML#getCriticizedDesignMaterials() | |
250 | */ | |
251 | public Set<Object> getCriticizedDesignMaterials() { | |
252 | 900 | Set<Object> ret = new HashSet<Object>(); |
253 | 900 | ret.add(Model.getMetaTypes().getClassifier()); |
254 | 900 | ret.add(Model.getMetaTypes().getState()); |
255 | 900 | return ret; |
256 | } | |
257 | ||
258 | } |