1 /*
2 wsmo4j - a WSMO API and Reference Implementation
3
4 Copyright (c) 2004-2005, OntoText Lab. / SIRMA
5
6 This library is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the Free
8 Software Foundation; either version 2.1 of the License, or (at your option)
9 any later version.
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 details.
14 You should have received a copy of the GNU Lesser General Public License along
15 with this library; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package com.ontotext.wsmo4j.ontology;
20
21 /**
22 * <p>Title: WSMO4J</p>
23 * <p>Description: WSMO API and a Reference Implementation</p>
24 * <p>Copyright: Copyright (c) 2004-2005</p>
25 * <p>Company: OntoText Lab. / SIRMA </p>
26 * @author not attributable
27 * @version 1.0
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
38 public class RelationImpl extends OntologyElementImpl
39 implements Relation {
40
41 private LinkedHashSet <Relation> superRelations;
42
43 private LinkedHashSet <Relation> subRelations;
44
45 private LinkedHashSet <RelationInstance> relInstances;
46
47 private final static String ERROR_CYCLE = "Cycle in relation hierarchy detected!";
48
49 private List <Parameter> parameters;
50
51 public RelationImpl(Identifier thisID) {
52 super(thisID);
53 superRelations = new LinkedHashSet <Relation> ();
54 subRelations = new LinkedHashSet <Relation> ();
55 relInstances = new LinkedHashSet <RelationInstance> ();
56 parameters = new LinkedList <Parameter> ();
57 }
58
59 public Set <Relation> listSuperRelations() throws SynchronisationException {
60 return Collections.unmodifiableSet(superRelations);
61 }
62
63 public void addSuperRelation(Relation relation)
64 throws SynchronisationException, InvalidModelException {
65 if (relation == null) {
66 throw new InvalidModelException();
67 }
68 if (checkInheritanceCycles(relation)) {
69 throw new InvalidModelException(ERROR_CYCLE);
70 }
71 superRelations.add(relation);
72 // establishing the reverse connection
73 if (false == relation.listSubRelations().contains(this)) {
74 relation.addSubRelation(this);
75 }
76 }
77
78 public void removeSuperRelation(Relation relation)
79 throws SynchronisationException, InvalidModelException {
80 if (relation == null) {
81 throw new InvalidModelException();
82 }
83 superRelations.remove(relation);
84 // removing the reverse connection
85 if (relation.listSubRelations().contains(this)) {
86 relation.removeSubRelation(this);
87 }
88 }
89
90 public Set <RelationInstance> listRelationInstances() throws SynchronisationException {
91 return Collections.unmodifiableSet(relInstances);
92 }
93
94 public void addRelationInstance(RelationInstance instance)
95 throws SynchronisationException, InvalidModelException {
96 if (instance == null) {
97 throw new InvalidModelException();
98 }
99 relInstances.add(instance);
100 if (instance.getRelation() != this) {
101 instance.setRelation(this);
102 }
103 }
104
105 public void removeRelationInstance(RelationInstance instance)
106 throws SynchronisationException, InvalidModelException {
107 if (instance == null) {
108 throw new InvalidModelException();
109 }
110 relInstances.remove(instance);
111 }
112
113 public Set <Relation> listSubRelations() throws SynchronisationException {
114 return Collections.unmodifiableSet(subRelations);
115 }
116
117 public void addSubRelation(Relation relation)
118 throws SynchronisationException, InvalidModelException {
119 if (relation == null) {
120 throw new InvalidModelException();
121 }
122 subRelations.add(relation);
123 // establishing the reverse connection
124 if (false == relation.listSuperRelations().contains(this)) {
125 relation.addSuperRelation(this);
126 }
127 }
128
129 public void removeSubRelation(Relation relation)
130 throws SynchronisationException, InvalidModelException {
131 if (relation == null) {
132 throw new InvalidModelException();
133 }
134 subRelations.remove(relation);
135 // removing the reverse connection
136 if (relation.listSuperRelations().contains(this)) {
137 relation.removeSuperRelation(this);
138 }
139 }
140
141 public List <Parameter> listParameters() throws SynchronisationException {
142 return Collections.unmodifiableList(parameters);
143 }
144
145 /**
146 * The positioning of the parameters is zero-based, i.e. the first parameter
147 * is at position 0. The initial order of parameters creation must be
148 * succesive, starting from the 0th position. Any other order raises an
149 * InvalidModelException. If this method is called more than once for a
150 * certain position, only the parameter created on the last call is
151 * preserved - all the rest are discarded.
152 *
153 * @param pos
154 * The position of the new Parameter for this Relation
155 * @throws org.wsmo.common.exception.InvalidModelException
156 * @return The newly created Parameter
157 */
158 public Parameter createParameter(byte pos)
159 throws SynchronisationException, InvalidModelException {
160 if (pos < 0 || pos > parameters.size()) {
161 throw new InvalidModelException();
162 }
163 Parameter param = new ParameterImpl(this);
164 if (pos == parameters.size()) {
165 parameters.add(param);
166 }
167 else {
168 parameters.set(pos, param);
169 }
170 return param;
171 }
172
173 /**
174 * retrieve the parameter at a given position
175 * Note: the parameter must already exist (e.g. a call to createParameter() must precede this call)
176 * @param pos The position of the parameter in interest
177 * @return a reference to the Parameter
178 * @throws org.wsmo.common.exception.SynchronisationException
179 * @see #createParameter(byte pos)
180 */
181 public Parameter getParameter(byte pos)
182 throws SynchronisationException {
183 if (pos < 0
184 || pos > parameters.size()-1) {
185 throw new IllegalArgumentException();
186 }
187 return parameters.get(pos);
188 }
189
190 /**
191 * The positioning of the parameters is zero-based, i.e. the first parameter
192 * is at position 0. The removal of parameters can only be performed from the
193 * end of the list. Trying to remove a parameter followed by another parameter
194 * will raise an exception.
195 * @param param The parameter to be removed
196 * @throws java.lang.IllegalArgumentException
197 */
198
199 public void removeParameter(Parameter param)
200 throws SynchronisationException, InvalidModelException {
201 if (parameters.size() == 0
202 || false == parameters.get(parameters.size()-1).equals(param)) {
203 throw new InvalidModelException();
204 }
205 parameters.remove(param);
206 }
207
208 /**
209 * The positioning of the parameters is zero-based, i.e. the first parameter
210 * is at position 0. The removal of parameters can only be performed from the
211 * end of the list. Trying to remove a parameter followed by another parameter
212 * will raise an exception.
213 * @param pos The position of the parameter to be removed
214 * @throws java.lang.IllegalArgumentException
215 */
216
217 public void removeParameter(byte pos)
218 throws SynchronisationException, InvalidModelException {
219 if (pos < 0 || parameters.size() != pos+1) {
220 throw new InvalidModelException();
221 }
222 parameters.remove(pos);
223 }
224
225 public boolean equals(Object object) {
226 if (object == this) {
227 return true; // instance match
228 }
229 if (object == null
230 || false == object instanceof Relation) {
231 return false;
232 }
233 return super.equals(object);
234 }
235
236 private boolean checkInheritanceCycles(Relation relation) {
237 if (relation.equals(this))
238 return true;
239
240 for (Iterator i = relation.listSuperRelations().iterator(); i.hasNext();) {
241 Relation superRelation = (Relation) i.next();
242 if (checkInheritanceCycles(superRelation))
243 return true;
244 }
245
246 return false;
247 }
248
249 }
250
251 /*
252 * $Log$
253 * Revision 1.21 2007/04/02 12:13:21 morcen
254 * Generics support added to wsmo-api, wsmo4j and wsmo-test
255 *
256 * Revision 1.20 2006/03/23 16:12:13 nathaliest
257 * moving the anon Id check to the validator
258 *
259 * Revision 1.19 2006/02/27 10:22:58 nathaliest
260 * added anonId check at relation.addXXXRelation(Relation)
261 *
262 * Revision 1.18 2005/09/15 14:59:41 vassil_momtchev
263 * cycles in the inheritance hierarchy are not allowed - checkInheritanceCycles method implemented; imports organized;
264 *
265 * Revision 1.17 2005/06/25 13:16:50 damyan_rm
266 * fixed getParameter() to avoid throwing Exception in case that index is correct
267 *
268 * Revision 1.16 2005/06/22 09:16:06 alex_simov
269 * Simplified equals() method. Identity strongly relies on identifier string
270 *
271 * Revision 1.15 2005/06/03 13:02:12 alex_simov
272 * fix
273 *
274 * Revision 1.14 2005/06/01 12:09:33 marin_dimitrov
275 * v0.4.0
276 *
277 * Revision 1.3 2005/05/17 12:04:57 alex
278 * Collections.unmodifiableSet() used instead of new set construction
279 *
280 * Revision 1.2 2005/05/16 08:49:31 alex
281 * implementation method getParameter(byte) added
282 *
283 * Revision 1.1 2005/05/12 09:20:13 alex
284 * initial commit
285 *
286 */