1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.ontotext.wsmo4j.ontology;
21
22
23
24
25
26
27
28
29
30
31 import java.util.*;
32
33 import org.omwg.ontology.*;
34 import org.wsmo.common.*;
35 import org.wsmo.common.exception.*;
36
37 import com.ontotext.wsmo4j.factory.*;
38
39
40 public class ConceptImpl extends OntologyElementImpl
41 implements Concept {
42
43 private LinkedHashSet <Concept> superConcepts;
44
45 private LinkedHashSet <Concept> subConcepts;
46
47 private LinkedHashSet <Instance> instances;
48
49 private LinkedHashMap <Identifier, Attribute> attributes;
50
51 private final static String ERROR_CYCLE = "Cycle in concept hierarchy detected!";
52
53 public ConceptImpl(Identifier thisID, WsmoFactoryImpl factory) {
54 super(thisID);
55 superConcepts = new LinkedHashSet <Concept> ();
56 subConcepts = new LinkedHashSet <Concept> ();
57 attributes = new LinkedHashMap <Identifier, Attribute> ();
58 instances = new LinkedHashSet <Instance> ();
59 }
60
61 public Set <Concept> listSuperConcepts() throws SynchronisationException {
62 return Collections.unmodifiableSet(superConcepts);
63 }
64
65 public void addSuperConcept(Concept superConcept)
66 throws SynchronisationException, InvalidModelException {
67 if (superConcept == null) {
68 throw new IllegalArgumentException();
69 }
70 if (checkInheritanceCycles(superConcept)) {
71 throw new InvalidModelException(ERROR_CYCLE);
72 }
73 superConcepts.add(superConcept);
74
75 if (false == superConcept.listSubConcepts().contains(this)) {
76 superConcept.addSubConcept(this);
77 }
78 }
79
80 public void removeSuperConcept(Concept superConcept)
81 throws SynchronisationException, InvalidModelException {
82 if (superConcept == null) {
83 throw new IllegalArgumentException();
84 }
85 superConcepts.remove(superConcept);
86
87 if (superConcept.listSubConcepts().contains(this)) {
88 superConcept.removeSubConcept(this);
89 }
90 }
91
92 public Set <Concept> listSubConcepts() throws SynchronisationException {
93 return Collections.unmodifiableSet(subConcepts);
94 }
95
96 public void addSubConcept(Concept subConcept)
97 throws SynchronisationException, InvalidModelException {
98 if (subConcept == null) {
99 throw new IllegalArgumentException();
100 }
101 subConcepts.add(subConcept);
102
103 if (false == subConcept.listSuperConcepts().contains(this)) {
104 subConcept.addSuperConcept(this);
105 }
106 }
107
108 public void removeSubConcept(Concept subConcept)
109 throws SynchronisationException, InvalidModelException {
110 if (subConcept == null) {
111 throw new IllegalArgumentException();
112 }
113 subConcepts.remove(subConcept);
114
115 if (subConcept.listSuperConcepts().contains(this)) {
116 subConcept.removeSuperConcept(this);
117 }
118 }
119
120 public Set <Instance> listInstances() throws SynchronisationException {
121 return Collections.unmodifiableSet(instances);
122 }
123
124 public void addInstance(Instance instance)
125 throws SynchronisationException, InvalidModelException {
126 if (instance == null) {
127 throw new IllegalArgumentException();
128 }
129 instances.add(instance);
130
131 if (false == instance.listConcepts().contains(this)) {
132 instance.addConcept(this);
133 }
134 }
135
136 public void removeInstance(Instance instance)
137 throws SynchronisationException, InvalidModelException {
138 if (instance == null) {
139 throw new IllegalArgumentException();
140 }
141 instances.remove(instance);
142
143 if (instance.listConcepts().contains(this)) {
144 instance.removeConcept(this);
145 }
146 }
147
148 public Set <Attribute> listAttributes() throws SynchronisationException {
149 return new LinkedHashSet <Attribute> (attributes.values());
150 }
151
152 public Set <Attribute> findAttributes(Identifier id)
153 throws SynchronisationException {
154 if (id == null) {
155 throw new IllegalArgumentException();
156 }
157
158 Set <Attribute> attrs = new HashSet <Attribute> ();
159 Attribute att = attributes.get(id);
160 if (att != null) {
161 attrs.add(att);
162 }
163
164 for (Iterator i = superConcepts.iterator(); i.hasNext();) {
165 attrs.addAll(((Concept) i.next()).findAttributes(id));
166 }
167
168 return attrs;
169 }
170
171 public Attribute createAttribute(Identifier id) throws InvalidModelException{
172 Attribute attribute = attributes.get(id);
173 if (attribute == null) {
174 attribute = new AttributeImpl(id, this);
175 attributes.put(id, attribute);
176 }
177 return attribute;
178 }
179
180 public void removeAttribute(Identifier identifier) {
181 if (identifier == null) {
182 throw new IllegalArgumentException();
183 }
184 attributes.remove(identifier);
185 }
186
187 public void removeAttribute(Attribute attribute)
188 throws SynchronisationException, InvalidModelException {
189 if (attribute == null) {
190 throw new IllegalArgumentException();
191 }
192 AttributeImpl attr = (AttributeImpl) attributes.remove(attribute.getIdentifier());
193 if (attr != null && attr.getConcept() != null)
194 attr.removeFromConcept();
195 }
196
197 public boolean equals(Object object) {
198 if (object == this) {
199 return true;
200 }
201 if (object == null
202 || false == object instanceof Concept) {
203 return false;
204 }
205 return super.equals(object);
206 }
207
208 private boolean checkInheritanceCycles(Concept concept) {
209 if (concept.equals(this))
210 return true;
211
212 for (Iterator i = concept.listSuperConcepts().iterator(); i.hasNext();) {
213 Concept superConcept = (Concept) i.next();
214 if (checkInheritanceCycles(superConcept))
215 return true;
216 }
217
218 return false;
219 }
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279