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 |
0
| public ConceptImpl(Identifier thisID, WsmoFactoryImpl factory) {
|
54 |
0
| super(thisID);
|
55 |
0
| superConcepts = new LinkedHashSet <Concept> ();
|
56 |
0
| subConcepts = new LinkedHashSet <Concept> ();
|
57 |
0
| attributes = new LinkedHashMap <Identifier, Attribute> ();
|
58 |
0
| instances = new LinkedHashSet <Instance> ();
|
59 |
| } |
60 |
| |
61 |
0
| public Set <Concept> listSuperConcepts() throws SynchronisationException {
|
62 |
0
| return Collections.unmodifiableSet(superConcepts);
|
63 |
| } |
64 |
| |
65 |
0
| public void addSuperConcept(Concept superConcept)
|
66 |
| throws SynchronisationException, InvalidModelException { |
67 |
0
| if (superConcept == null) {
|
68 |
0
| throw new IllegalArgumentException();
|
69 |
| } |
70 |
0
| if (checkInheritanceCycles(superConcept)) {
|
71 |
0
| throw new InvalidModelException(ERROR_CYCLE);
|
72 |
| } |
73 |
0
| superConcepts.add(superConcept);
|
74 |
| |
75 |
0
| if (false == superConcept.listSubConcepts().contains(this)) {
|
76 |
0
| superConcept.addSubConcept(this);
|
77 |
| } |
78 |
| } |
79 |
| |
80 |
0
| public void removeSuperConcept(Concept superConcept)
|
81 |
| throws SynchronisationException, InvalidModelException { |
82 |
0
| if (superConcept == null) {
|
83 |
0
| throw new IllegalArgumentException();
|
84 |
| } |
85 |
0
| superConcepts.remove(superConcept);
|
86 |
| |
87 |
0
| if (superConcept.listSubConcepts().contains(this)) {
|
88 |
0
| superConcept.removeSubConcept(this);
|
89 |
| } |
90 |
| } |
91 |
| |
92 |
0
| public Set <Concept> listSubConcepts() throws SynchronisationException {
|
93 |
0
| return Collections.unmodifiableSet(subConcepts);
|
94 |
| } |
95 |
| |
96 |
0
| public void addSubConcept(Concept subConcept)
|
97 |
| throws SynchronisationException, InvalidModelException { |
98 |
0
| if (subConcept == null) {
|
99 |
0
| throw new IllegalArgumentException();
|
100 |
| } |
101 |
0
| subConcepts.add(subConcept);
|
102 |
| |
103 |
0
| if (false == subConcept.listSuperConcepts().contains(this)) {
|
104 |
0
| subConcept.addSuperConcept(this);
|
105 |
| } |
106 |
| } |
107 |
| |
108 |
0
| public void removeSubConcept(Concept subConcept)
|
109 |
| throws SynchronisationException, InvalidModelException { |
110 |
0
| if (subConcept == null) {
|
111 |
0
| throw new IllegalArgumentException();
|
112 |
| } |
113 |
0
| subConcepts.remove(subConcept);
|
114 |
| |
115 |
0
| if (subConcept.listSuperConcepts().contains(this)) {
|
116 |
0
| subConcept.removeSuperConcept(this);
|
117 |
| } |
118 |
| } |
119 |
| |
120 |
0
| public Set <Instance> listInstances() throws SynchronisationException {
|
121 |
0
| return Collections.unmodifiableSet(instances);
|
122 |
| } |
123 |
| |
124 |
0
| public void addInstance(Instance instance)
|
125 |
| throws SynchronisationException, InvalidModelException { |
126 |
0
| if (instance == null) {
|
127 |
0
| throw new IllegalArgumentException();
|
128 |
| } |
129 |
0
| instances.add(instance);
|
130 |
| |
131 |
0
| if (false == instance.listConcepts().contains(this)) {
|
132 |
0
| instance.addConcept(this);
|
133 |
| } |
134 |
| } |
135 |
| |
136 |
0
| public void removeInstance(Instance instance)
|
137 |
| throws SynchronisationException, InvalidModelException { |
138 |
0
| if (instance == null) {
|
139 |
0
| throw new IllegalArgumentException();
|
140 |
| } |
141 |
0
| instances.remove(instance);
|
142 |
| |
143 |
0
| if (instance.listConcepts().contains(this)) {
|
144 |
0
| instance.removeConcept(this);
|
145 |
| } |
146 |
| } |
147 |
| |
148 |
0
| public Set <Attribute> listAttributes() throws SynchronisationException {
|
149 |
0
| return new LinkedHashSet <Attribute> (attributes.values());
|
150 |
| } |
151 |
| |
152 |
0
| public Set <Attribute> findAttributes(Identifier id)
|
153 |
| throws SynchronisationException { |
154 |
0
| if (id == null) {
|
155 |
0
| throw new IllegalArgumentException();
|
156 |
| } |
157 |
| |
158 |
0
| Set <Attribute> attrs = new HashSet <Attribute> ();
|
159 |
0
| Attribute att = attributes.get(id);
|
160 |
0
| if (att != null) {
|
161 |
0
| attrs.add(att);
|
162 |
| } |
163 |
| |
164 |
0
| for (Iterator i = superConcepts.iterator(); i.hasNext();) {
|
165 |
0
| attrs.addAll(((Concept) i.next()).findAttributes(id));
|
166 |
| } |
167 |
| |
168 |
0
| return attrs;
|
169 |
| } |
170 |
| |
171 |
0
| public Attribute createAttribute(Identifier id) throws InvalidModelException{
|
172 |
0
| Attribute attribute = attributes.get(id);
|
173 |
0
| if (attribute == null) {
|
174 |
0
| attribute = new AttributeImpl(id, this);
|
175 |
0
| attributes.put(id, attribute);
|
176 |
| } |
177 |
0
| return attribute;
|
178 |
| } |
179 |
| |
180 |
0
| public void removeAttribute(Identifier identifier) {
|
181 |
0
| if (identifier == null) {
|
182 |
0
| throw new IllegalArgumentException();
|
183 |
| } |
184 |
0
| attributes.remove(identifier);
|
185 |
| } |
186 |
| |
187 |
0
| public void removeAttribute(Attribute attribute)
|
188 |
| throws SynchronisationException, InvalidModelException { |
189 |
0
| if (attribute == null) {
|
190 |
0
| throw new IllegalArgumentException();
|
191 |
| } |
192 |
0
| AttributeImpl attr = (AttributeImpl) attributes.remove(attribute.getIdentifier());
|
193 |
0
| if (attr != null && attr.getConcept() != null)
|
194 |
0
| attr.removeFromConcept();
|
195 |
| } |
196 |
| |
197 |
0
| public boolean equals(Object object) {
|
198 |
0
| if (object == this) {
|
199 |
0
| return true;
|
200 |
| } |
201 |
0
| if (object == null
|
202 |
| || false == object instanceof Concept) { |
203 |
0
| return false;
|
204 |
| } |
205 |
0
| return super.equals(object);
|
206 |
| } |
207 |
| |
208 |
0
| private boolean checkInheritanceCycles(Concept concept) {
|
209 |
0
| if (concept.equals(this))
|
210 |
0
| return true;
|
211 |
| |
212 |
0
| for (Iterator i = concept.listSuperConcepts().iterator(); i.hasNext();) {
|
213 |
0
| Concept superConcept = (Concept) i.next();
|
214 |
0
| if (checkInheritanceCycles(superConcept))
|
215 |
0
| return true;
|
216 |
| } |
217 |
| |
218 |
0
| 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 |
| |