summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/parameterization/MergedParameterization.java
blob: 8bba1c2a1fb739ed428bf6da6b1375b17a4cc00e (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
package de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization;

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

 Copyright (C) 2012
 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.Collection;
import java.util.Vector;

import de.lmu.ifi.dbs.elki.utilities.ClassGenericsUtil;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.InternalParameterizationErrors;
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.constraints.GlobalParameterConstraint;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter;
import de.lmu.ifi.dbs.elki.utilities.pairs.Pair;

/**
 * This configuration can be "rewound" to allow the same values to be consumed
 * multiple times, by different classes. This is used in best-effort
 * parameterization when some instances might not apply given the actual data,
 * e.g. in visualization classes.
 * 
 * @author Erich Schubert
 */
// TODO: Can we merge MergedParameterization and TrackParameters into one?
public class MergedParameterization implements Parameterization {
  /**
   * The parameterization we get the new values from.
   */
  final private Parameterization inner;

  /**
   * Parameters we used before, but have rewound
   */
  final private ListParameterization current;

  /**
   * Parameters to rewind.
   */
  final private java.util.Vector<Pair<OptionID, Object>> used;

  /**
   * Constructor.
   * 
   * @param child Child parameterization to wrap.
   */
  public MergedParameterization(Parameterization child) {
    super();
    this.inner = child;
    this.current = new ListParameterization();
    this.used = new java.util.Vector<Pair<OptionID, Object>>();
  }

  /**
   * Constructor for descending
   * 
   * @param inner Child parameterization to use.
   * @param current Current parameterization to re-used
   * @param used Used parameters list.
   */
  private MergedParameterization(Parameterization inner, ListParameterization current, Vector<Pair<OptionID, Object>> used) {
    super();
    this.inner = inner;
    this.current = current;
    this.used = used;
  }

  /**
   * Rewind the configuration to the initial situation
   */
  public void rewind() {
    synchronized(used) {
      for(Pair<OptionID, Object> pair : used) {
        current.addParameter(pair.first, pair.second);
      }
      used.removeAllElements();
    }
  }

  @Override
  public boolean setValueForOption(Parameter<?, ?> opt) throws ParameterException {
    try {
      if(current.setValueForOption(opt)) {
        used.add(new Pair<OptionID, Object>(opt.getOptionID(), opt.getValue()));
        return true;
      }
    }
    catch(ParameterException e) {
      current.reportError(e);
    }
    if(inner.setValueForOption(opt)) {
      used.add(new Pair<OptionID, Object>(opt.getOptionID(), opt.getValue()));
      return true;
    }
    used.add(new Pair<OptionID, Object>(opt.getOptionID(), opt.getDefaultValue()));
    return false;
  }

  @Override
  public Parameterization descend(Object option) {
    // We should descend into current, too - but the API doesn't give us a
    // ListParameterization then!
    return new MergedParameterization(inner.descend(option), current, used);
  }

  @Override
  public Collection<ParameterException> getErrors() {
    return current.getErrors();
  }
  
  @Override
  public boolean hasErrors() {
    return current.hasErrors();
  }

  @Override
  public void reportError(ParameterException e) {
    inner.reportError(e);
  }

  @Override
  public boolean grab(Parameter<?, ?> opt) {
    try {
      if (setValueForOption(opt)) {
        return true;
      }
      // Try default value instead.
      if (opt.tryDefaultValue()) {
        return true;
      }
      // No value available.
      return false;
    }
    catch(ParameterException e) {
      reportError(e);
      return false;
    }
  }

  @Override
  public boolean hasUnusedParameters() {
    return inner.hasUnusedParameters();
  }

  @Override
  public boolean checkConstraint(GlobalParameterConstraint constraint) {
    // TODO: does checkConstraint work here reliably?
    return inner.checkConstraint(constraint);
  }

  @Override
  public <C> C tryInstantiate(Class<C> r, Class<?> c) {
    try {
      return ClassGenericsUtil.tryInstantiate(r, c, this);
    }
    catch(Exception e) {
      reportError(new InternalParameterizationErrors("Error instantiating internal class: "+c.getName(), e));
      return null;
    }
  }

  @Override
  public <C> C tryInstantiate(Class<C> c) {
    try {
      return ClassGenericsUtil.tryInstantiate(c, c, this);
    }
    catch(Exception e) {
      reportError(new InternalParameterizationErrors("Error instantiating internal class: "+c.getName(), e));
      return null;
    }
  }
}