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  
31  import java.lang.reflect.Proxy;
32  import java.util.ArrayList;
33  import java.util.Collections;
34  import java.util.List;
35  
36  import org.omwg.ontology.Relation;
37  import org.omwg.ontology.RelationInstance;
38  import org.omwg.ontology.Value;
39  import org.wsmo.common.Identifier;
40  import org.wsmo.common.exception.InvalidModelException;
41  import org.wsmo.common.exception.SynchronisationException;
42  
43  import com.ontotext.wsmo4j.factory.IDReference;
44  
45  public class RelationInstanceImpl extends OntologyElementImpl
46          implements RelationInstance {
47  
48      private Relation memberOf;
49  
50      private Value[] parameterValues;
51      
52      public RelationInstanceImpl(Identifier thisID, Relation rel)
53              throws SynchronisationException, InvalidModelException {
54          super(thisID);
55          _setRelation(rel);
56      }
57      
58    
59      /**
60       * Sets the relation this relation instance is
61       * an instance of.
62       * @param concept The Relation this relation instance
63       * is an instance of.
64       * @throws org.wsmo.common.exception.SynchronisationException
65       * @throws org.wsmo.common.exception.InvalidModelException
66       * @see #getRelation()
67       */
68      public void setRelation(Relation relation)
69              throws SynchronisationException, InvalidModelException {
70          _setRelation(relation);
71      }
72  
73      /**
74       * Returns the relation this relation instance
75       * is an instance of.
76       * @return The relation this relation instance
77       * is an instance of.
78       * @throws org.wsmo.common.exception.SynchronisationException
79       * @see #setRelation(org.omwg.ontology.Relation)
80       */    
81      public Relation getRelation() throws SynchronisationException {
82          return memberOf;
83      }
84  
85      /**
86       * Returns a set of ParameterValues.
87       * @return A set of ParameterValues
88       * for all parameters set via <code>setParameterValue</code>.
89       */
90      public List <Value> listParameterValues() {
91          if (parameterValues == null) {
92              return Collections.unmodifiableList(new ArrayList <Value> ()); // immutable
93          }
94          ArrayList <Value> result = new ArrayList <Value>(parameterValues.length);
95          for(int i = 0; i < parameterValues.length; i++) {
96              result.add(parameterValues[i]);
97          }
98          return result;
99      }
100 
101     /**
102      * Returns the value of the specified paramter.
103      * @param parameter The parameter of interest.
104      * @return The value of the specified parameter.
105      * @throws org.wsmo.common.exception.SynchronisationException
106      * @see #setParameterValue(org.omwg.ontology.Parameter, org.omwg.ontology.Value)
107      */
108     public Value getParameterValue(byte pos)
109             throws SynchronisationException, InvalidModelException {
110         if (memberOf == null
111                 || parameterValues == null) {
112             throw new InvalidModelException();
113         }
114         if (pos < 0 
115                 || pos >= parameterValues.length) {
116             throw new IllegalArgumentException();
117         }
118         return parameterValues[pos];
119     }
120 
121     /**
122      * Sets the value of the specified Parameter to be the specified
123      * ParameterValue object. If this method is called more than once for a
124      * certain parameter position, only the parameter value added on the last
125      * call is preserved - all the rest are discarded.
126      * Null values are allowed for removal purposes. 
127      * 
128      * @param pos
129      *            The index of the parameter of interest.
130      * @param value
131      *            The ParameterValue object to set the specified Parameter's
132      *            value to.
133      * @throws org.wsmo.common.exception.SynchronisationException
134      * @throws org.wsmo.common.exception.InvalidModelException
135      */
136     public void setParameterValue(byte pos, Value value)
137             throws SynchronisationException, InvalidModelException {
138         if (memberOf == null 
139                 || parameterValues == null) {
140             throw new InvalidModelException();
141         }
142         
143         //if relation is not resolved, don't assume anything about arity but extend it like
144         //requested.
145         if (memberOf instanceof Proxy){
146             if(!((IDReference)Proxy.getInvocationHandler(memberOf)).isResolved() &&
147                     parameterValues.length<=pos){
148                 correctValueHolderLength(pos+1);
149             }
150         }
151         int size = 0;
152         if (memberOf.listParameters()!=null){
153             size = memberOf.listParameters().size();
154         }
155         if (pos < 0
156                || (pos >= parameterValues.length)) {
157             throw new IllegalArgumentException("Parameter position must be within arity ("+
158                     size+") of relation ("+
159                     memberOf.getIdentifier()+ ")");
160         }
161         
162         parameterValues[pos] = value;
163     }
164 
165     public boolean equals(Object object) {
166         if (object == null
167                 || false == object instanceof RelationInstance) {
168             return false;
169         }
170         return super.equals(object);
171     }
172     
173     /**
174      * Ensures that the values holder size is the same as the Relation
175      * parameters count.
176      * 
177      * @param newParamCount
178      */
179     private void correctValueHolderLength(int newParamCount) {
180         Value[] newHolder = new Value[newParamCount];
181         System.arraycopy(parameterValues, 
182                          0, 
183                          newHolder, 
184                          0, 
185                          Math.min(parameterValues.length, 
186                                   newParamCount));
187         parameterValues = newHolder;
188     }
189     
190     /**
191      * Sets the relation this relation instance is
192      * an instance of.
193      * @param concept The Relation this relation instance
194      * is an instance of.
195      * @throws org.wsmo.common.exception.SynchronisationException
196      * @throws org.wsmo.common.exception.InvalidModelException
197      * @see #getRelation()
198      */
199     private void _setRelation(Relation relation)
200             throws SynchronisationException, InvalidModelException {
201         if (relation == null) {
202             throw new InvalidModelException();
203         }
204         int newParamCount = relation.listParameters().size();
205         if (false == relation.equals(memberOf)) {
206             parameterValues = new Value[newParamCount]; // new relation - new param values
207         }
208         else {
209             if (parameterValues.length != newParamCount) {
210                 correctValueHolderLength(newParamCount);
211             }
212         }
213         memberOf = relation;
214         if (false == memberOf.listRelationInstances().contains(this)) {
215             memberOf.addRelationInstance(this);
216         }
217     }
218 
219 }
220 
221 /*
222  * $Log$
223  * Revision 1.29  2007/04/02 12:19:08  morcen
224  * Generics support added to wsmo-api, wsmo4j and wsmo-test
225  *
226  * Revision 1.28  2007/04/02 12:13:21  morcen
227  * Generics support added to wsmo-api, wsmo4j and wsmo-test
228  *
229  * Revision 1.27  2007/02/22 15:34:06  alex_simov
230  * unnecessary check removed from equals()
231  *
232  * Revision 1.26  2006/03/23 16:12:13  nathaliest
233  * moving the anon Id check to the validator
234  *
235  * Revision 1.25  2006/02/13 11:49:58  nathaliest
236  * changed anonId-check error String
237  *
238  * Revision 1.24  2006/02/13 09:21:21  nathaliest
239  * added AnonIds Check
240  *
241  * Revision 1.23  2006/02/08 15:20:45  holgerlausen
242  * added more specific error message
243  *
244  * Revision 1.22  2005/11/29 16:42:38  holgerlausen
245  * if the relation of an instance is not know no error is neccessary on setParameter(i)... (relation arity is then just assumed)
246  *
247  * Revision 1.21  2005/09/13 08:59:39  vassil_momtchev
248  * constructor accept (Identifier, Relation) now
249  *
250  * Revision 1.20  2005/06/27 09:04:09  alex_simov
251  * Object param substituted by Value
252  *
253  * Revision 1.19  2005/06/22 09:16:06  alex_simov
254  * Simplified equals() method. Identity strongly relies on identifier string
255  *
256  * Revision 1.18  2005/06/03 13:02:12  alex_simov
257  * fix
258  *
259  * Revision 1.17  2005/06/01 12:09:33  marin_dimitrov
260  * v0.4.0
261  *
262  * Revision 1.2  2005/05/17 12:43:32  alex
263  * immutable Collections.EMPTY_LIST instead of a new empty list
264  *
265  * Revision 1.1  2005/05/12 14:56:41  alex
266  * initial commit
267  *
268  */