View Javadoc

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  
20  package com.ontotext.wsmo4j.ontology;
21  
22  /**
23   * <p>Title: WSMO4J</p>
24   * <p>Description: WSMO API and a Reference Implementation</p>
25   * <p>Copyright:  Copyright (c) 2004-2005</p>
26   * <p>Company: OntoText Lab. / SIRMA </p>
27   * @author not attributable
28   * @version 1.0
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          // establishing the inverse connection
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          // removing the inverse connection
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         // establishing the inverse connection
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         // removing the inverse connection
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         // establishing the inverse connection
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         // removing the inverse connection
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; // instance match
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  * $Log$
224  * Revision 1.30  2007/04/02 12:13:21  morcen
225  * Generics support added to wsmo-api, wsmo4j and wsmo-test
226  *
227  * Revision 1.29  2006/11/17 10:52:41  vassil_momtchev
228  * conceptImpl now uses attributeImpl, when attribute is removed to reset the domain of the attribute
229  *
230  * Revision 1.28  2006/03/23 16:12:13  nathaliest
231  * moving the anon Id check to the validator
232  *
233  * Revision 1.27  2006/02/16 14:34:57  nathaliest
234  * added removeAttribute(Identifier) method
235  *
236  * Revision 1.26  2006/02/13 22:49:23  nathaliest
237  * - changed concept.createAttribute() and Parameter.addType to throw InvalidModelException.
238  * - small change at check AnonIds in ConceptImpl
239  *
240  * Revision 1.25  2006/02/13 11:49:58  nathaliest
241  * changed anonId-check error String
242  *
243  * Revision 1.24  2006/02/13 09:21:21  nathaliest
244  * added AnonIds Check
245  *
246  * Revision 1.23  2006/02/10 14:33:04  vassil_momtchev
247  * attributes are now stricly local to the scope of a concept; findAttributes implemented to search all superconcept attributes
248  *
249  * Revision 1.22  2005/09/15 14:59:41  vassil_momtchev
250  * cycles in the inheritance hierarchy are not allowed - checkInheritanceCycles method implemented; imports organized;
251  *
252  * Revision 1.21  2005/09/07 14:33:01  vassil_momtchev
253  * createAttribute did not add the created attribute to the internal map of attributes
254  *
255  * Revision 1.20  2005/09/01 14:55:19  vassil_momtchev
256  * createAttribute(IRI) replaced addAttribute(Attribute)
257  *
258  * Revision 1.19  2005/07/04 14:25:04  marin_dimitrov
259  * createAttribute-->addAttribute
260  *
261  * Revision 1.18  2005/06/22 09:16:05  alex_simov
262  * Simplified equals() method. Identity strongly relies on identifier string
263  *
264  * Revision 1.17  2005/06/01 12:09:33  marin_dimitrov
265  * v0.4.0
266  *
267  * Revision 1.4  2005/05/31 14:41:30  damian
268  * createAttribute changed
269  *
270  * Revision 1.3  2005/05/17 12:04:57  alex
271  * Collections.unmodifiableSet() used instead of new set construction
272  *
273  * Revision 1.2  2005/05/12 11:14:05  alex
274  * createAttribute() instantiated Attribute instead of  ConceptAttribute
275  *
276  * Revision 1.1  2005/05/11 14:19:53  alex
277  * initial commit
278  *
279  */