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  package com.ontotext.wsmo4j.ontology;
20  
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.common.*;
38  
39  
40  public class OntologyImpl
41      extends TopEntityImpl
42      implements Ontology {
43  
44      private LinkedHashMap <Identifier, Concept> concepts;
45  
46      private LinkedHashMap <Identifier, Relation> relations;
47  
48      private LinkedHashMap <Identifier, Instance> instances;
49  
50      private LinkedHashMap <Identifier, Axiom> axioms;
51  
52      private LinkedHashMap <Identifier, RelationInstance> relationInstances;
53  
54      public OntologyImpl(IRI thisID) {
55          super(thisID);
56  
57          concepts = new LinkedHashMap <Identifier, Concept> ();
58          relations = new LinkedHashMap <Identifier, Relation> ();
59          instances = new LinkedHashMap <Identifier, Instance> ();
60          axioms = new LinkedHashMap <Identifier, Axiom> ();
61          relationInstances = new LinkedHashMap <Identifier, RelationInstance> ();
62      }
63  
64      public Set <Concept> listConcepts()
65          throws SynchronisationException {
66          return new LinkedHashSet <Concept> (concepts.values());
67      }
68  
69      public void addConcept(Concept concept)
70          throws SynchronisationException, InvalidModelException {
71          if (concept == null) {
72              throw new IllegalArgumentException();
73          }
74          concepts.put(concept.getIdentifier(), concept);
75          if (false == this.equals(concept.getOntology())) {
76              concept.setOntology(this);
77          }
78      }
79  
80      public void removeConcept(Concept concept)
81          throws SynchronisationException, InvalidModelException {
82          if (concept == null) {
83              throw new IllegalArgumentException();
84          }
85          concepts.remove(concept.getIdentifier());
86          concept.setOntology(null);
87      }
88  
89      public void removeConcept(Identifier conceptID)
90          throws SynchronisationException, InvalidModelException {
91          if (conceptID == null) {
92              throw new IllegalArgumentException();
93          }
94          Concept concept = concepts.get(conceptID);
95          if (concept != null) {
96              concept.setOntology(null);
97          }
98          concepts.remove(conceptID);
99      }
100 
101     public Set <Relation> listRelations()
102         throws SynchronisationException {
103         return new LinkedHashSet <Relation> (relations.values());
104     }
105 
106     public void addRelation(Relation relation)
107         throws SynchronisationException, InvalidModelException {
108         if (relation == null) {
109             throw new IllegalArgumentException();
110         }
111         relations.put(relation.getIdentifier(), relation);
112         if (false == this.equals(relation.getOntology())) {
113             relation.setOntology(this);
114         }
115     }
116 
117     public void removeRelation(Relation relation)
118         throws SynchronisationException, InvalidModelException {
119         if (relation == null) {
120             throw new IllegalArgumentException();
121         }
122         relations.remove(relation.getIdentifier());
123         relation.setOntology(null);
124     }
125 
126     public void removeRelation(Identifier relationID)
127         throws SynchronisationException, InvalidModelException {
128         if (relationID == null) {
129             throw new IllegalArgumentException();
130         }
131         Relation relation = relations.get(relationID);
132         if (relation != null) {
133             relation.setOntology(null);
134         }
135         relations.remove(relationID);
136     }
137 
138     public Set <Instance> listInstances()
139         throws SynchronisationException {
140         return new LinkedHashSet <Instance> (instances.values());
141     }
142 
143     public void addInstance(Instance instance)
144         throws SynchronisationException, InvalidModelException {
145         if (instance == null) {
146             throw new IllegalArgumentException();
147         }
148         instances.put(instance.getIdentifier(), instance);
149         if (false == this.equals(instance.getOntology())) {
150             instance.setOntology(this);
151         }
152     }
153 
154     public void removeInstance(Instance instance)
155         throws SynchronisationException, InvalidModelException {
156         if (instance == null) {
157             throw new IllegalArgumentException();
158         }
159         instance.setOntology(null);
160         instances.remove(instance.getIdentifier());
161     }
162 
163     public void removeInstance(Identifier instanceID)
164         throws SynchronisationException, InvalidModelException {
165         if (instanceID == null) {
166             throw new IllegalArgumentException();
167         }
168         Instance instance = instances.get(instanceID);
169         if (instance != null) {
170             instance.setOntology(null);
171         }
172         instances.remove(instanceID);
173     }
174 
175     public Set <Axiom> listAxioms()
176         throws SynchronisationException {
177         return new LinkedHashSet <Axiom> (axioms.values());
178     }
179 
180     public void addAxiom(Axiom axiom)
181         throws SynchronisationException, InvalidModelException {
182         if (axiom == null) {
183             throw new IllegalArgumentException();
184         }
185         axioms.put(axiom.getIdentifier(), axiom);
186         if (false == this.equals(axiom.getOntology())) {
187             axiom.setOntology(this);
188         }
189     }
190 
191     public void removeAxiom(Axiom axiom)
192         throws SynchronisationException, InvalidModelException {
193         if (axiom == null) {
194             throw new IllegalArgumentException();
195         }
196         axioms.remove(axiom.getIdentifier());
197         axiom.setOntology(null);
198     }
199 
200     public void removeAxiom(Identifier axiomID)
201         throws SynchronisationException, InvalidModelException {
202         if (axiomID == null) {
203             throw new IllegalArgumentException();
204         }
205         Axiom axiom = axioms.get(axiomID);
206         if (axiom != null) {
207             axiom.setOntology(null);
208         }
209         axioms.remove(axiomID);
210     }
211 
212     public Set <RelationInstance> listRelationInstances()
213         throws SynchronisationException {
214         return new LinkedHashSet <RelationInstance> (relationInstances.values());
215     }
216 
217     public void addRelationInstance(RelationInstance instance)
218         throws SynchronisationException, InvalidModelException {
219         if (instance == null) {
220             throw new IllegalArgumentException();
221         }
222         relationInstances.put(instance.getIdentifier(), instance);
223         if (false == this.equals(instance.getOntology())) {
224             instance.setOntology(this);
225         }
226     }
227 
228     public void removeRelationInstance(RelationInstance instance)
229         throws SynchronisationException, InvalidModelException {
230         if (instance == null) {
231             throw new IllegalArgumentException();
232         }
233         relationInstances.remove(instance.getIdentifier());
234         instance.setOntology(null);
235     }
236 
237     public void removeRelationInstance(Identifier instanceID)
238         throws SynchronisationException, InvalidModelException {
239         if (instanceID == null) {
240             throw new IllegalArgumentException();
241         }
242         RelationInstance instance = relationInstances.get(instanceID);
243         if (instance != null) {
244             instance.setOntology(null);
245         }
246 
247         relationInstances.remove(instanceID);
248     }
249 
250     public Concept findConcept(Identifier id)
251         throws SynchronisationException {
252 
253         if (null == id) {
254             throw new IllegalArgumentException();
255         }
256         return concepts.get(id);
257     }
258 
259     public Relation findRelation(Identifier id)
260         throws SynchronisationException {
261 
262         if (null == id) {
263             throw new IllegalArgumentException();
264         }
265         return relations.get(id);
266     }
267 
268     public Instance findInstance(Identifier id)
269         throws SynchronisationException {
270 
271         if (null == id) {
272             throw new IllegalArgumentException();
273         }
274 
275         return instances.get(id);
276     }
277 
278     public Axiom findAxiom(Identifier id)
279         throws SynchronisationException {
280 
281         if (null == id) {
282             throw new IllegalArgumentException();
283         }
284         return axioms.get(id);
285     }
286 
287     public RelationInstance findRelationInstance(Identifier id)
288         throws SynchronisationException {
289 
290         if (null == id) {
291             throw new IllegalArgumentException();
292         }
293         return relationInstances.get(id);
294     }
295 
296     public Set <Entity> findEntity(Identifier id)
297         throws SynchronisationException {
298 
299         if (null == id) {
300             throw new IllegalArgumentException();
301         }
302         //iterate all collections
303         Set <Entity> entities = new LinkedHashSet <Entity> ();
304         Entity entity = findAxiom(id);
305         if (entity != null)
306             entities.add(entity);
307         entity = findConcept(id);
308         if (entity != null)
309             entities.add(entity);
310         entity = findInstance(id);
311         if (entity != null)
312             entities.add(entity);
313         entity = findRelation(id);
314         if (entity != null)
315             entities.add(entity);
316         entity = findRelationInstance(id);
317         if (entity != null)
318             entities.add(entity);
319 
320         //no need to clone/copy since this is a temporary set
321         return entities;
322     }
323 
324     public boolean equals(Object object) {
325         if (object == this) {
326             return true;
327         }
328         if (object == null
329             || false == object instanceof Ontology) {
330             return false;
331         }
332         return super.equals(object);
333     }
334 
335 }
336 /*
337  * $Log$
338  * Revision 1.27  2007/04/02 12:13:21  morcen
339  * Generics support added to wsmo-api, wsmo4j and wsmo-test
340  *
341  * Revision 1.26  2005/10/18 10:36:04  vassil_momtchev
342  * bug fix; findEntity(Identifier) returned Set with null values
343  *
344  * Revision 1.25  2005/10/18 09:08:34  marin_dimitrov
345  * findEntity resturns Set (was Entity[])
346  *
347  * Revision 1.24  2005/10/18 08:46:47  vassil_momtchev
348  * Entity findEntity(Identifier) changed to Entity[] findEntity(Identifier)
349  *
350  * Revision 1.23  2005/06/22 09:16:05  alex_simov
351  * Simplified equals() method. Identity strongly relies on identifier string
352  *
353  * Revision 1.22  2005/06/07 07:30:58  alex_simov
354  * owner ontology not set/cleared for addXXX() and removeXXX() methods
355  *
356  * Revision 1.21  2005/06/01 12:09:33  marin_dimitrov
357  * v0.4.0
358  *
359  * Revision 1.2  2005/05/19 14:05:43  marin
360  * constructor accepts an IRI (was: Identifier)
361  *
362  * Revision 1.1  2005/05/12 15:47:15  alex
363  * initial commit
364  *
365  */