ParameterList.java
001 /*
002  *  ParameterList.java
003  *
004  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
005  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006  *
007  *  This file is part of GATE (see http://gate.ac.uk/), and is free
008  *  software, licenced under the GNU Library General Public License,
009  *  Version 2, June 1991 (in the distribution as file licence.html,
010  *  and also available at http://gate.ac.uk/gate/licence.html).
011  *
012  *  Hamish Cunningham, 15/Nov/2000
013  *
014  *  $Id: ParameterList.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015  */
016 
017 package gate.creole;
018 
019 import java.io.Serializable;
020 import java.util.*;
021 
022 import gate.Factory;
023 import gate.FeatureMap;
024 import gate.util.Out;
025 import gate.util.Strings;
026 
027 /** Models resource parameters lists as described in their
028   <TT>creole.xml</TT> metadata. Parameters are stored as lists
029   * of disjunctions (<TT>OR'd</TT> sets in the metadata).
030   */
031 public class ParameterList implements Serializable
032 {
033   /** Debug flag */
034   private static final boolean DEBUG = false;
035 
036   /** The runtime parameters */
037   protected List<List<Parameter>> runtimeParameters = 
038     new ArrayList<List<Parameter>>();
039 
040   /** Get the list of runtime parameters
041     * (as a list of parameter disjunctions).
042     */
043   public List<List<Parameter>> getRuntimeParameters() {
044     return runtimeParameters;
045   // getRuntimeParameters()
046 
047   /** The initialisation time parameters */
048   protected List<List<Parameter>> initimeParameters = 
049       new ArrayList<List<Parameter>>();
050 
051   /** Get the list of initialisation-time parameters
052     * (as a list of parameter disjunctions).
053     */
054   public List<List<Parameter>> getInitimeParameters() {
055     return initimeParameters;
056   // getInitimeParameters()
057 
058   /** Add a parameter disjunction.
059     * It is assumed that the parameters in a disjunction are all runtime
060     * or all init-time, never a mix of the two.
061     @exception NoSuchElementException disjunction has no more elements.
062     */
063   public boolean add(List<Parameter> disjunction) {
064     boolean status = false;
065     Iterator<Parameter> iter = disjunction.iterator();
066     Parameter param = iter.next();
067 
068     if(param.isRuntime()) {
069       status = runtimeParameters.add(disjunction);
070     else {
071       status = initimeParameters.add(disjunction);
072     }
073 
074     return status;
075   // add(param)
076 
077   /** Add all the members of a parameter list (as individual disjunctions) */
078   public boolean addAll(List<Parameter> c) {
079     boolean status = false;
080     Iterator<Parameter> iter = c.iterator();
081     while(iter.hasNext()) {
082       List<Parameter> disj = new ArrayList<Parameter>();
083       Parameter param = iter.next();
084       disj.add(param);
085       status = add(disj);
086     }
087 
088     return status;
089   // addAll(Collection)
090 
091   /** Get default runtime parameter value set.
092     * Calls <TT>getDefaults(List)</TT>.
093     @see #getDefaults(List)
094     */
095   public FeatureMap getRuntimeDefaults() throws ParameterException {
096     return getDefaults(runtimeParameters);
097   // getRuntimeDefaults()
098 
099   /** Get default initime parameter value set.
100     * Calls <TT>getDefaults(List)</TT>.
101     @see #getDefaults(List)
102     */
103   public FeatureMap getInitimeDefaults() throws ParameterException {
104     return getDefaults(initimeParameters);
105   // getInitimeDefaults()
106 
107   /** Get default parameter value set. Where more than one default
108     * is possible amongst disjunctive parameters, only the first will be set.
109     * To check if the default set is comprehensive,
110     * use <TT>isFullyDefaulted()</TT>.
111     @see #isFullyDefaulted()
112     */
113   public FeatureMap getDefaults(List<List<Parameter>> parametersthrows ParameterException {
114     FeatureMap defaults = Factory.newFeatureMap();
115 
116     // each element of the parameters list is a list of (disjunctive) params
117     Iterator<List<Parameter>> disjIter = parameters.iterator();
118 
119     // for each parameter disjunction in parameters
120     disjIterLoop:
121     while(disjIter.hasNext()) {
122       boolean optional = false// were any of this disj optional?
123 
124       // get an iterator for this disjunction of parameters
125       List<Parameter> paramDisj = disjIter.next();
126       Iterator<Parameter> paramsIter = paramDisj.iterator();
127 
128       // for each parameter in the disjunction
129       while(paramsIter.hasNext()) {
130         Parameter param = paramsIter.next();
131         if(DEBUGOut.prln("Examining " + param);
132         if(!optional)
133           optional = param.isOptional();
134 
135         // try and find a default value
136         Object defaultValue = param.calculateDefaultValue();
137 
138         // no default found
139         if(defaultValue == null) {
140           // if none of this disj were optional, and we're the last, then
141           // we've got at least one non-optional param unset
142           if(!optional && !paramsIter.hasNext()) {
143             fullyDefaulted = false;
144           }
145 
146         // valid default found - set it and continue with the next disj
147         else {
148           defaults.put(param.getName(), defaultValue);
149           continue disjIterLoop;
150         }
151       // paramsIter
152 
153     // disjIter
154 
155     return defaults;
156   // getDefaults()
157 
158 
159 // this stuff is for if we decide to do more thorough checking
160 // of parameterisation in Factory.createResource... but at present
161 // the gui checks pretty well so...
162 //
163 //  /** Analyse a parameter-value feature map and return a list
164 //    * of init time parameter disjunctions that are unset.
165 //    */
166 //  public List getUnsetInitimeParameters(FeatureMap paramValues) {
167 //    List unsetDisj = new ArrayList();
168 //
169 //    // for each init disj,
170 //    //   for each param,
171 //    //     if there's no value in paramValues
172 //    //       add this disj to rtnValue,
173 //    //       continue disjLoop
174 //  } // getUnsetInitimeParameters(paramValues)
175 //
176 //  /** Analyse a parameter-value feature map and return a list
177 //    * of runtime parameter disjunctions that are unset.
178 //    */
179 //  public List getUnsetRuntimeParameters(FeatureMap paramValues) {
180 //  } // getUnsetRuntimeParameters(paramValues)
181 //
182 //  public List getUnsetInitimeParameters(
183 //    FeatureMap paramValues, boolean includeOptional
184 //  ) {
185 //  } // getUnsetInitimeParameters(paramValues, includeOptional)
186 //
187 //  public List getUnsetRuntimeParameters(
188 //    FeatureMap paramValues, boolean includeOptional
189 //  ) {
190 //  } // getUnsetRuntimeParameters(paramValues, includeOptional)
191 //
192 
193   /** Status of the last run of <TT>getDefaults(List)</TT>. */
194   protected boolean fullyDefaulted = false;
195 
196   /** Get the status of the last run of <TT>getDefaults(List)</TT>.
197     * If the last run managed to find a default for all parameters
198     * that are part of a disjunction of which none are optional, then
199     * this status is true; else it is false.
200     @see #getDefaults(List)
201     */
202   public boolean isFullyDefaulted() { return fullyDefaulted; }
203 
204 
205   /** String representation */
206   public String toString() {
207     StringBuffer s = new StringBuffer(Strings.getNl() "  ParameterList:");
208 
209     Iterator<List<Parameter>> iter = getRuntimeParameters().iterator();
210     if(iter.hasNext()) s.append(Strings.getNl() "  runtime params=");
211     while(iter.hasNext()) {
212       s.append(Strings.getNl() "    ");
213       List<Parameter> paramDisj = iter.next();
214       Iterator<Parameter> iter2 = paramDisj.iterator();
215 
216       while(iter2.hasNext())
217         s.appenditer2.next() + Strings.getNl() "    " );
218     }
219 
220     iter = getInitimeParameters().iterator();
221     if(iter.hasNext()) s.append(Strings.getNl() "  initime params=");
222     while(iter.hasNext()) {
223       s.append(Strings.getNl() "    ");
224       List<Parameter> paramDisj = iter.next();
225       Iterator<Parameter> iter2 = paramDisj.iterator();
226 
227       while(iter2.hasNext())
228         s.appenditer2.next() + Strings.getNl() "    " );
229     }
230 
231     return s.toString();
232   // toString()
233 
234 // class ParameterList
235