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   * <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  import java.util.Collections;
31  import java.util.HashSet;
32  import java.util.Iterator;
33  import java.util.LinkedHashMap;
34  import java.util.LinkedHashSet;
35  import java.util.Map;
36  import java.util.Set;
37  
38  import org.omwg.ontology.Attribute;
39  import org.omwg.ontology.Concept;
40  import org.omwg.ontology.Instance;
41  import org.omwg.ontology.Value;
42  import org.wsmo.common.Identifier;
43  import org.wsmo.common.exception.InvalidModelException;
44  import org.wsmo.common.exception.SynchronisationException;
45  
46  
47  public class InstanceImpl extends OntologyElementImpl
48          implements Instance {
49  
50      private LinkedHashSet <Concept> concepts;
51      
52      private LinkedHashMap <Identifier, Set<Value>> attributeValues;
53      
54      public InstanceImpl(Identifier thisID) {
55          super(thisID);
56          concepts = new LinkedHashSet <Concept> ();
57          attributeValues = new LinkedHashMap <Identifier, Set<Value>> ();
58      }
59  
60      /**
61       * Sets the concept this instance is an instance of.
62       * @param concept The concept that this instance
63       * is an instance of.
64       * @throws org.wsmo.common.exception.SynchronisationException
65       * @throws org.wsmo.common.exception.InvalidModelException
66       */
67      public void addConcept(Concept memberOf) throws InvalidModelException {
68          if (memberOf == null) {
69              throw new IllegalArgumentException();
70          }
71          concepts.add(memberOf);
72          if (false == memberOf.listInstances().contains(this)) {
73              memberOf.addInstance(this);
74          }
75      }
76  
77      /**
78       * Removes an axiom from the set of axioms defined
79       * by this ontology.
80       * @param axiom The axiom to be removed.
81       * @throws org.wsmo.common.exception.SynchronisationException
82       * @throws org.wsmo.common.exception.InvalidModelException
83       */
84      public void removeConcept(Concept memberOf) throws InvalidModelException {
85          if (memberOf == null) {
86              throw new IllegalArgumentException();
87          }
88          concepts.remove(memberOf);
89          if (memberOf.listInstances().contains(this)) {
90              memberOf.removeInstance(this);
91          }
92      }
93  
94      /**
95       * Lists the axioms defined by this ontology.
96       * @return The set of axioms defined by this ontology.
97       * @throws org.wsmo.common.exception.SynchronisationException
98       */
99      public Set <Concept> listConcepts() throws SynchronisationException {
100         return Collections.unmodifiableSet(concepts);
101     }
102 
103     /**
104      * Adds a new attribute value to the
105      * list of values associated with the specified
106      * attribute of this instance.
107      * @param value The value to be added.
108      * @param id The attribute's identifier of interest.
109      * @throws org.wsmo.common.exception.SynchronisationException
110      * @throws org.wsmo.common.exception.InvalidModelException
111      */
112     public Set <Value> listAttributeValues(Identifier id)
113             throws SynchronisationException {
114         Set <Value> collection = attributeValues.get(id);
115         if (collection != null) {
116             return Collections.unmodifiableSet(collection);
117         }
118 
119         return Collections.unmodifiableSet(new HashSet <Value> ()); //immutable
120     }
121 
122     /**
123      * Returns a list of values of a specified attribute.
124      * @return A Map of [Attribute, Set] pairs.
125      * @throws org.wsmo.common.exception.SynchronisationException
126      */
127     public synchronized Map <Identifier, Set <Value>> listAttributeValues()
128             throws SynchronisationException {
129         Iterator <Map.Entry <Identifier, Set <Value>>> i = attributeValues.entrySet().iterator();
130         LinkedHashMap <Identifier, Set <Value>> ret = new LinkedHashMap <Identifier, Set <Value>> ();
131         while (i.hasNext()) {
132             Map.Entry <Identifier, Set <Value>> curEntry = i.next();
133             LinkedHashSet <Value> curVal = (LinkedHashSet <Value>) curEntry.getValue();
134             ret.put(curEntry.getKey(), Collections.unmodifiableSet(curVal));
135         }
136         return ret;
137     }
138 
139     /**
140      * Adds a new attribute value to the
141      * list of values associated with the specified
142      * attribute of this instance.
143      * @param value The value to be added.
144      * @param id The attribute's identifier of interest.
145      * @throws org.wsmo.common.exception.SynchronisationException
146      * @throws org.wsmo.common.exception.InvalidModelException
147      */
148     public synchronized void addAttributeValue(Identifier id, Value value)
149             throws SynchronisationException, InvalidModelException {
150         Set <Value> collection = attributeValues.get(id);
151         if (collection == null) {
152             collection = new LinkedHashSet <Value>();
153             attributeValues.put(id, collection);
154         }
155         collection.add(value);
156     }
157 
158     /**
159      * Removes a particular value associated with an attribute within this instance.
160      * @param id The attribute's identifier of interest.
161      * @param value the attribute value to be removed.
162      * @throws org.wsmo.common.exception.SynchronisationException
163      * @throws org.wsmo.common.exception.InvalidModelException
164      * @see #removeAttributeValues(Attribute key)
165      */
166     public synchronized void removeAttributeValue(Identifier id, Value attrVal) 
167             throws SynchronisationException, InvalidModelException {
168         Set <Value> collection = attributeValues.get(id);
169         if (collection != null) {
170             collection.remove(attrVal);
171             if (collection.size() == 0) {
172                 attributeValues.remove(id);
173             }
174         }
175     }
176 
177     /**
178      * clears all the values associated with a particular attribute of this instance.
179      * @param attribute The attribute of interest.
180      * @throws org.wsmo.common.exception.SynchronisationException
181      * @throws org.wsmo.common.exception.InvalidModelException
182      * @see removeAttributeValue(Attribute key, Object value)
183      */
184     public synchronized void removeAttributeValues(Identifier id)
185             throws SynchronisationException, InvalidModelException {
186         attributeValues.remove(id);
187         }
188 
189     public Set <Attribute> findAttributeDefinitions(Identifier id) {
190         Set <Attribute> attrs = new HashSet <Attribute> ();
191         for (Iterator <Concept> i = concepts.iterator(); i.hasNext();) {
192             attrs.addAll(i.next().findAttributes(id));
193         }
194         return attrs;
195     }
196 
197     public boolean equals(Object object) {
198         if (object == null
199                 || false == object instanceof Instance) {
200             return false;
201         }
202         return super.equals(object);
203     }
204     
205 }
206 
207 /*
208  * $Log$
209  * Revision 1.27  2007/04/02 12:19:08  morcen
210  * Generics support added to wsmo-api, wsmo4j and wsmo-test
211  *
212  * Revision 1.26  2007/04/02 12:13:21  morcen
213  * Generics support added to wsmo-api, wsmo4j and wsmo-test
214  *
215  * Revision 1.25  2007/02/22 15:32:48  alex_simov
216  * unnecessary check removed from equals()
217  *
218  * Revision 1.24  2006/03/23 16:12:13  nathaliest
219  * moving the anon Id check to the validator
220  *
221  * Revision 1.23  2006/02/13 11:49:58  nathaliest
222  * changed anonId-check error String
223  *
224  * Revision 1.22  2006/02/13 09:21:21  nathaliest
225  * added AnonIds Check
226  *
227  * Revision 1.21  2006/02/10 14:34:13  vassil_momtchev
228  * instance refere attributes by Identifier; no longer handle to Attribute object is used
229  *
230  * Revision 1.20  2005/06/27 09:04:03  alex_simov
231  * Object param substituted by Value
232  *
233  * Revision 1.19  2005/06/22 09:16:05  alex_simov
234  * Simplified equals() method. Identity strongly relies on identifier string
235  *
236  * Revision 1.18  2005/06/03 13:02:12  alex_simov
237  * fix
238  *
239  * Revision 1.17  2005/06/01 12:09:33  marin_dimitrov
240  * v0.4.0
241  *
242  * Revision 1.3  2005/05/17 12:43:17  alex
243  * immutable Collections.EMPTY_SET instead of a new empty set
244  *
245  * Revision 1.2  2005/05/17 12:04:57  alex
246  * Collections.unmodifiableSet() used instead of new set construction
247  *
248  * Revision 1.1  2005/05/13 08:01:39  alex
249  * initial commit
250  *
251  */