1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| package org.deri.wsmo4j.validator; |
17 |
| |
18 |
| import java.util.*; |
19 |
| import java.util.Map.Entry; |
20 |
| |
21 |
| import org.omwg.ontology.*; |
22 |
| import org.wsmo.common.*; |
23 |
| import org.wsmo.validator.ValidationError; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| public class AnonIdCollectHelper { |
42 |
| |
43 |
| private final static String ERROR_ANON_IDS = "An object with an unnumbered " + |
44 |
| "anonymous identifier cannot be referenced. To reference this object, name it."; |
45 |
| |
46 |
| private List <ValidationError> errors = null; |
47 |
| |
48 |
| private HashMap <Concept, Integer> concepts = null; |
49 |
| |
50 |
| private HashMap <Relation, Integer> relations = null; |
51 |
| |
52 |
| private HashMap <Instance, Integer> instances = null; |
53 |
| |
54 |
0
| public AnonIdCollectHelper(List <ValidationError> errors) {
|
55 |
0
| this.errors = errors;
|
56 |
0
| concepts = new HashMap <Concept, Integer> ();
|
57 |
0
| relations = new HashMap <Relation, Integer> ();
|
58 |
0
| instances = new HashMap <Instance, Integer> ();
|
59 |
| } |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
0
| private void visitConcept(Concept concept){
|
68 |
0
| boolean checked = false;
|
69 |
0
| if (concept.getIdentifier() instanceof UnnumberedAnonymousID) {
|
70 |
0
| addToConcepts(concept);
|
71 |
0
| if (concept.listSuperConcepts().size() > 0) {
|
72 |
0
| if (checked) {
|
73 |
0
| checked = false;
|
74 |
0
| addToConcepts(concept);
|
75 |
| } |
76 |
| else { |
77 |
0
| checked = true;
|
78 |
| } |
79 |
| } |
80 |
0
| if (concept.listAttributes().size() > 0) {
|
81 |
0
| if (checked) {
|
82 |
0
| checked = false;
|
83 |
0
| addToConcepts(concept);
|
84 |
| } |
85 |
| else { |
86 |
0
| checked = true;
|
87 |
| } |
88 |
| } |
89 |
0
| if (concept.listInstances().size() > 0) {
|
90 |
0
| for (int i=0; i<concept.listInstances().size(); i++) {
|
91 |
0
| if (checked) {
|
92 |
0
| checked = false;
|
93 |
0
| addToConcepts(concept);
|
94 |
| } |
95 |
| else { |
96 |
0
| checked = true;
|
97 |
| } |
98 |
| } |
99 |
| } |
100 |
0
| if (concept.listNFPValues().size() > 0) {
|
101 |
0
| for (int i=0; i<concept.listNFPValues().size(); i++) {
|
102 |
0
| if (checked) {
|
103 |
0
| checked = false;
|
104 |
0
| addToConcepts(concept);
|
105 |
| } |
106 |
| else { |
107 |
0
| checked = true;
|
108 |
| } |
109 |
| } |
110 |
| } |
111 |
| } |
112 |
0
| if (concept.listSuperConcepts().size() > 0) {
|
113 |
0
| Iterator it = concept.listSuperConcepts().iterator();
|
114 |
0
| while (it.hasNext()) {
|
115 |
0
| Concept tmp = (Concept) it.next();
|
116 |
0
| if (tmp.getIdentifier() instanceof UnnumberedAnonymousID) {
|
117 |
0
| addToConcepts(tmp, concept);
|
118 |
| } |
119 |
| } |
120 |
| } |
121 |
0
| if (concept.listAttributes().size() > 0) {
|
122 |
0
| Iterator it = concept.listAttributes().iterator();
|
123 |
0
| while (it.hasNext()) {
|
124 |
0
| Attribute a = (Attribute) it.next();
|
125 |
0
| if (a.listTypes().size() > 0) {
|
126 |
0
| Iterator it2 = a.listTypes().iterator();
|
127 |
0
| while (it2.hasNext()) {
|
128 |
0
| Type t = (Type) it2.next();
|
129 |
0
| if (t instanceof Concept &&
|
130 |
| ((Concept) t).getIdentifier() instanceof UnnumberedAnonymousID) { |
131 |
0
| addToConcepts((Concept) t, a);
|
132 |
| } |
133 |
| } |
134 |
| } |
135 |
| } |
136 |
| } |
137 |
| } |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
0
| private void visitInstance(Instance instance) {
|
146 |
0
| boolean checked = false;
|
147 |
0
| if (instance.getIdentifier() instanceof UnnumberedAnonymousID) {
|
148 |
0
| addToInstances(instance);
|
149 |
0
| if (instance.listAttributeValues().size() > 0) {
|
150 |
0
| if (checked) {
|
151 |
0
| checked = false;
|
152 |
0
| addToInstances(instance);
|
153 |
| } |
154 |
| else { |
155 |
0
| checked = true;
|
156 |
| } |
157 |
| } |
158 |
0
| if (instance.listConcepts().size() > 0) {
|
159 |
0
| for (int i=0; i<instance.listConcepts().size(); i++) {
|
160 |
0
| if (checked) {
|
161 |
0
| checked = false;
|
162 |
0
| addToInstances(instance);
|
163 |
| } |
164 |
| else { |
165 |
0
| checked = true;
|
166 |
| } |
167 |
| } |
168 |
| } |
169 |
0
| if (instance.listNFPValues().size() > 0) {
|
170 |
0
| for (int i=0; i<instance.listNFPValues().size(); i++) {
|
171 |
0
| if (checked) {
|
172 |
0
| checked = false;
|
173 |
0
| addToInstances(instance);
|
174 |
| } |
175 |
| else { |
176 |
0
| checked = true;
|
177 |
| } |
178 |
| } |
179 |
| } |
180 |
| } |
181 |
0
| if (instance.listAttributeValues().size() > 0) {
|
182 |
0
| Iterator it = instance.listAttributeValues().entrySet().iterator();
|
183 |
0
| while (it.hasNext()) {
|
184 |
0
| Set set = (Set) ((Entry) it.next()).getValue();
|
185 |
0
| Iterator it2 = set.iterator();
|
186 |
0
| while (it2.hasNext()) {
|
187 |
0
| Object o = it2.next();
|
188 |
0
| if (o instanceof Instance &&
|
189 |
| ((Instance) o).getIdentifier() instanceof UnnumberedAnonymousID) { |
190 |
0
| addToInstances((Instance) o, instance);
|
191 |
| } |
192 |
| } |
193 |
| } |
194 |
| } |
195 |
| } |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
0
| private void visitRelation(Relation relation) {
|
205 |
0
| if (relation.getIdentifier() instanceof UnnumberedAnonymousID) {
|
206 |
0
| addToRelations(relation);
|
207 |
0
| if (relation.listSuperRelations().size() > 0) {
|
208 |
0
| addToRelations(relation);
|
209 |
| } |
210 |
0
| if (relation.listParameters().size() > 0) {
|
211 |
0
| Iterator it = relation.listParameters().iterator();
|
212 |
0
| while (it.hasNext()) {
|
213 |
0
| Parameter p = (Parameter) it.next();
|
214 |
0
| if (p.listTypes().size() > 0) {
|
215 |
0
| addToRelations(relation);
|
216 |
| } |
217 |
| } |
218 |
| } |
219 |
0
| if (relation.listNFPValues().size() > 0) {
|
220 |
0
| for (int i=0; i<relation.listNFPValues().size(); i++) {
|
221 |
0
| addToRelations(relation);
|
222 |
| } |
223 |
| } |
224 |
| } |
225 |
0
| if (relation.listSuperRelations().size() > 0) {
|
226 |
0
| Iterator it = relation.listSuperRelations().iterator();
|
227 |
0
| while(it.hasNext()) {
|
228 |
0
| Relation tmp = (Relation) it.next();
|
229 |
0
| if (tmp.getIdentifier() instanceof UnnumberedAnonymousID) {
|
230 |
0
| addToRelations(tmp, relation);
|
231 |
| } |
232 |
| } |
233 |
| } |
234 |
0
| if (relation.listParameters().size() > 0) {
|
235 |
0
| Iterator it = relation.listParameters().iterator();
|
236 |
0
| while (it.hasNext()) {
|
237 |
0
| Parameter p = (Parameter) it.next();
|
238 |
0
| if (p.listTypes().size() > 0) {
|
239 |
0
| Iterator it2 = p.listTypes().iterator();
|
240 |
0
| while (it2.hasNext()) {
|
241 |
0
| Type t = (Type) it2.next();
|
242 |
0
| if (t instanceof Concept &&
|
243 |
| ((Concept) t).getIdentifier() instanceof UnnumberedAnonymousID) { |
244 |
0
| addToConcepts((Concept) t, relation);
|
245 |
| |
246 |
| |
247 |
0
| if (relation.listSuperRelations().size() > 0 ||
|
248 |
| relation.listSubRelations().size() > 0) { |
249 |
0
| addError(relation, ValidationError.ANON_ID_ERR + ":\n" +
|
250 |
| ERROR_ANON_IDS); |
251 |
| } |
252 |
| } |
253 |
| } |
254 |
| } |
255 |
| } |
256 |
| } |
257 |
| } |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
0
| private void visitRelationInstance(RelationInstance relationInstance) {
|
265 |
0
| if (relationInstance.getRelation().getIdentifier() instanceof UnnumberedAnonymousID) {
|
266 |
0
| addError(relationInstance, ValidationError.ANON_ID_ERR + ":\n" +
|
267 |
| "A relationInstance cannot reference an anonymous relation"); |
268 |
| } |
269 |
0
| if (relationInstance.listParameterValues().size() > 0) {
|
270 |
0
| Iterator it = relationInstance.listParameterValues().iterator();
|
271 |
0
| while (it.hasNext()) {
|
272 |
0
| Value v = (Value) it.next();
|
273 |
0
| if (v instanceof Instance &&
|
274 |
| ((Instance) v).getIdentifier() instanceof UnnumberedAnonymousID) { |
275 |
0
| addToInstances((Instance) v, relationInstance);
|
276 |
| } |
277 |
| } |
278 |
| } |
279 |
| } |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
0
| private void addToConcepts(Concept concept) {
|
285 |
0
| addToConcepts(concept, concept);
|
286 |
| } |
287 |
| |
288 |
| |
289 |
| |
290 |
| |
291 |
| |
292 |
| |
293 |
| |
294 |
| |
295 |
| |
296 |
0
| private void addToConcepts(Concept concept, Entity entity) {
|
297 |
0
| if (concepts.containsKey(concept)) {
|
298 |
0
| concepts.put(concept, new Integer(concepts.get(concept).intValue() + 1));
|
299 |
0
| addError(entity, ValidationError.ANON_ID_ERR + ":\n" +
|
300 |
| ERROR_ANON_IDS); |
301 |
| } |
302 |
| else { |
303 |
0
| concepts.put(concept, new Integer(1));
|
304 |
| } |
305 |
| } |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
0
| private void addToInstances(Instance instance) {
|
311 |
0
| addToInstances(instance, instance);
|
312 |
| } |
313 |
| |
314 |
| |
315 |
| |
316 |
| |
317 |
| |
318 |
| |
319 |
| |
320 |
| |
321 |
| |
322 |
0
| private void addToInstances(Instance instance, Entity entity) {
|
323 |
0
| if (instances.containsKey(instance)) {
|
324 |
0
| instances.put(instance, new Integer(instances.get(instance).intValue() + 1));
|
325 |
0
| addError(entity, ValidationError.ANON_ID_ERR + ":\n" +
|
326 |
| ERROR_ANON_IDS); |
327 |
| } |
328 |
| else { |
329 |
0
| instances.put(instance, new Integer(1));
|
330 |
| } |
331 |
| } |
332 |
| |
333 |
| |
334 |
| |
335 |
| |
336 |
0
| private void addToRelations(Relation relation) {
|
337 |
0
| addToRelations(relation, relation);
|
338 |
| } |
339 |
| |
340 |
| |
341 |
| |
342 |
| |
343 |
| |
344 |
| |
345 |
| |
346 |
| |
347 |
| |
348 |
0
| private void addToRelations(Relation relation, Entity entity) {
|
349 |
0
| if (relations.containsKey(relation)) {
|
350 |
0
| relations.put(relation, new Integer(relations.get(relation).intValue() + 1));
|
351 |
0
| addError(entity, ValidationError.ANON_ID_ERR + ":\n" +
|
352 |
| ERROR_ANON_IDS); |
353 |
| } |
354 |
| else { |
355 |
0
| relations.put(relation, new Integer(1));
|
356 |
| } |
357 |
| } |
358 |
| |
359 |
| |
360 |
| |
361 |
| |
362 |
| |
363 |
0
| public void checkAnonIds(Ontology ontology) {
|
364 |
0
| Iterator i = ontology.listConcepts().iterator();
|
365 |
0
| while (i.hasNext()) {
|
366 |
0
| Concept concept = (Concept) i.next();
|
367 |
0
| visitConcept(concept);
|
368 |
| } |
369 |
0
| i = ontology.listInstances().iterator();
|
370 |
0
| while (i.hasNext()) {
|
371 |
0
| Instance instance = (Instance) i.next();
|
372 |
0
| visitInstance(instance);
|
373 |
| } |
374 |
| |
375 |
0
| i = ontology.listRelations().iterator();
|
376 |
0
| while (i.hasNext()) {
|
377 |
0
| Relation relation = (Relation) i.next();
|
378 |
0
| visitRelation(relation);
|
379 |
| } |
380 |
| |
381 |
0
| i = ontology.listRelationInstances().iterator();
|
382 |
0
| while (i.hasNext()) {
|
383 |
0
| RelationInstance relationInstance = (RelationInstance) i.next();
|
384 |
0
| visitRelationInstance(relationInstance);
|
385 |
| } |
386 |
| } |
387 |
| |
388 |
0
| private void addError(Entity entity, String reason) {
|
389 |
0
| errors.add(new ValidationErrorImpl(entity, reason, WSML.WSML_FULL));
|
390 |
| } |
391 |
| |
392 |
| } |
393 |
| |
394 |
| |
395 |
| |
396 |
| |
397 |
| |
398 |
| |
399 |
| |
400 |
| |
401 |
| |
402 |
| |
403 |
| |
404 |
| |
405 |
| |
406 |
| |
407 |
| |
408 |
| |
409 |
| |
410 |
| |
411 |
| |