summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameters/VectorListParameter.java
blob: 9fb9c6f14554358220aca12f49c1e3ee97754b8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2011
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.UnspecifiedParameterException;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.WrongParameterValueException;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.ParameterConstraint;

/**
 * Parameter class for a parameter specifying a list of vectors.
 * 
 * @author Steffi Wanka
 * @author Erich Schubert
 */
public class VectorListParameter extends ListParameter<List<Double>> {
  /**
   * Constructs a vector list parameter with the given name and description.
   * 
   * @param optionID Option ID
   * @param constraints Constraint
   * @param defaultValue Default value
   */
  public VectorListParameter(OptionID optionID, List<ParameterConstraint<List<List<Double>>>> constraints, List<List<Double>> defaultValue) {
    super(optionID, constraints, defaultValue);
  }

  /**
   * Constructs a vector list parameter with the given name and description.
   * 
   * @param optionID Option ID
   * @param constraints Constraints
   * @param optional Optional flag
   */
  public VectorListParameter(OptionID optionID, List<ParameterConstraint<List<List<Double>>>> constraints, boolean optional) {
    super(optionID, constraints, optional);
  }

  /**
   * Constructs a vector list parameter with the given name and description.
   * 
   * @param optionID Option ID
   * @param constraints Constraints
   */
  // Indiscernible from optionID, defaults
  /*
   * public VectorListParameter(OptionID optionID, List<ParameterConstraint<?
   * super List<List<Double>>>> constraints) { super(optionID, constraints); }
   */

  /**
   * Constructs a vector list parameter with the given name and description.
   * 
   * @param optionID Option ID
   * @param constraint Constraint
   * @param defaultValue Default value
   */
  public VectorListParameter(OptionID optionID, ParameterConstraint<List<List<Double>>> constraint, List<List<Double>> defaultValue) {
    super(optionID, constraint, defaultValue);
  }

  /**
   * Constructs a vector list parameter with the given name and description.
   * 
   * @param optionID Option ID
   * @param constraint Constraint
   * @param optional Optional flag
   */
  public VectorListParameter(OptionID optionID, ParameterConstraint<List<List<Double>>> constraint, boolean optional) {
    super(optionID, constraint, optional);
  }

  /**
   * Constructs a vector list parameter with the given name and description.
   * 
   * @param optionID Option ID
   * @param constraint Constraint
   */
  public VectorListParameter(OptionID optionID, ParameterConstraint<List<List<Double>>> constraint) {
    super(optionID, constraint);
  }

  /**
   * Constructs a vector list parameter with the given name and description.
   * 
   * @param optionID Option ID
   * @param defaultValue Default value
   */
  // Indiscernible from optionID, constraints
  /*
   * public VectorListParameter(OptionID optionID, List<List<Double>>
   * defaultValue) { super(optionID, defaultValue); }
   */

  /**
   * Constructs a vector list parameter with the given name and description.
   * 
   * @param optionID Option ID
   * @param optional Optional flag
   */
  public VectorListParameter(OptionID optionID, boolean optional) {
    super(optionID, optional);
  }

  /**
   * Constructs a vector list parameter with the given name and description.
   * 
   * @param optionID Option ID
   */
  public VectorListParameter(OptionID optionID) {
    super(optionID);
  }

  /** {@inheritDoc} */
  @Override
  public String getValueAsString() {
    StringBuffer buf = new StringBuffer();
    List<List<Double>> val = getValue();
    Iterator<List<Double>> valiter = val.iterator();
    while(valiter.hasNext()) {
      List<Double> vec = valiter.next();
      Iterator<Double> veciter = vec.iterator();
      while(veciter.hasNext()) {
        buf.append(Double.toString(veciter.next()));
        if (veciter.hasNext()) {
          buf.append(LIST_SEP);
        }
      }
      // Append separation character
      if (valiter.hasNext()) {
        buf.append(VECTOR_SEP);
      }
    }
    return buf.toString();
  }

  /** {@inheritDoc} */
  @SuppressWarnings("unchecked")
  @Override
  protected List<List<Double>> parseValue(Object obj) throws ParameterException {
    try {
      List<?> l = List.class.cast(obj);
      // do extra validation:
      for(Object o : l) {
        List<?> v = List.class.cast(o);
        for(Object c : v) {
          if(!(c instanceof Double)) {
            throw new WrongParameterValueException("Wrong parameter format for parameter \"" + getName() + "\". Given list contains objects of different type!");
          }
        }
      }
      // TODO: can we use reflection to get extra checks?
      // TODO: Should we copy the list and vectors?
      return (List<List<Double>>) l;
    }
    catch(ClassCastException e) {
      // continue with other attempts.
    }
    if(obj instanceof String) {
      String[] vectors = VECTOR_SPLIT.split((String) obj);
      if(vectors.length == 0) {
        throw new UnspecifiedParameterException("Wrong parameter format! Given list of vectors for parameter \"" + getName() + "\" is empty!");
      }
      ArrayList<List<Double>> vecs = new ArrayList<List<Double>>();

      for(String vector : vectors) {
        String[] coordinates = SPLIT.split(vector);
        ArrayList<Double> vectorCoord = new ArrayList<Double>();
        for(String coordinate : coordinates) {
          try {
            Double.parseDouble(coordinate);
            vectorCoord.add(Double.parseDouble(coordinate));
          }
          catch(NumberFormatException e) {
            throw new WrongParameterValueException("Wrong parameter format! Coordinates of vector \"" + vector + "\" are not valid!");
          }
        }
        vecs.add(vectorCoord);
      }
      return vecs;
    }
    throw new WrongParameterValueException("Wrong parameter format! Parameter \"" + getName() + "\" requires a list of Double values!");
  }

  /**
   * Returns an array containing the individual vector sizes of this vector list
   * parameter.
   * 
   * @return the individual vector sizes
   */
  // unused?
  /*
   * public int[] vectorSizes() {
   * 
   * int[] sizes = new int[getListSize()];
   * 
   * int i = 0; for(List<?> vecs : value) { sizes[i] = vecs.size(); i++; }
   * 
   * return sizes; }
   */

  /**
   * Returns a string representation of the parameter's type.
   * 
   * @return 
   *         &quot;&lt;double_11,...,double_1n:...:double_m1,...,double_mn&gt;&quot
   *         ;
   */
  @Override
  public String getSyntax() {
    return "<double_11,...,double_1n:...:double_m1,...,double_mn>";
  }
}