summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/utilities/optionhandling/constraints/ParameterFlagGlobalConstraint.java
blob: ab77240a6ef927179d7ac047f3505c4c2d7b5181 (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
package de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints;

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

 Copyright (C) 2013
 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.List;

import de.lmu.ifi.dbs.elki.utilities.optionhandling.ParameterException;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.UnusedParameterException;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Flag;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.AbstractParameter;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter;

/**
 * Global parameter constraint describing the dependency of a parameter (
 * {@link AbstractParameter}) on a given flag ({@link Flag}). Depending on the
 * status of the flag the parameter is tested for keeping its constraints or
 * not.
 * 
 * @author Steffi Wanka
 * 
 * @apiviz.uses de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Flag
 * @apiviz.uses 
 *              de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.Parameter
 * 
 * @param <S> Parameter type
 */
public class ParameterFlagGlobalConstraint<S> implements GlobalParameterConstraint {
  /**
   * Parameter possibly to be checked.
   */
  private Parameter<? extends S> param;

  /**
   * Flag the checking of the parameter constraints is dependent on.
   */
  private Flag flag;

  /**
   * Indicates at which status of the flag the parameter is to be checked.
   */
  private boolean flagConstraint;

  /**
   * List of parameter constraints.
   */
  private List<? extends ParameterConstraint<? super S>> cons;

  /**
   * Constructs a global parameter constraint specifying that the testing of the
   * parameter given for keeping the parameter constraints given is dependent on
   * the status of the flag given.
   * 
   * @param p parameter possibly to be checked
   * @param c a list of parameter constraints, if the value is null, the
   *        parameter is just tested if it is set.
   * @param f flag controlling the checking of the parameter constraints
   * @param flagConstraint indicates at which status of the flag the parameter
   *        is to be checked
   */
  public ParameterFlagGlobalConstraint(Parameter<? extends S> p, List<? extends ParameterConstraint<? super S>> c, Flag f, boolean flagConstraint) {
    param = p;
    flag = f;
    this.flagConstraint = flagConstraint;
    cons = c;
  }

  /**
   * Checks the parameter for its parameter constraints dependent on the status
   * of the given flag. If a parameter constraint is breached a parameter
   * exception is thrown.
   * 
   */
  @Override
  public void test() throws ParameterException {
    // only check constraints of param if flag is set
    if(flagConstraint == flag.getValue().booleanValue()) {
      if(cons != null) {
        for(ParameterConstraint<? super S> c : cons) {
          c.test(param.getValue());
        }
      }
      else {
        if(!param.isDefined()) {
          throw new UnusedParameterException("Value of parameter " + param.getName() + " is not optional.");
        }
      }
    }
  }

  @Override
  public String getDescription() {
    StringBuilder description = new StringBuilder();
    if(flagConstraint) {
      description.append("If ").append(flag.getName());
      description.append(" is set, the following constraints for parameter ");
      description.append(param.getName()).append(" have to be fullfilled: ");
      if(cons != null) {
        for(int i = 0; i < cons.size(); i++) {
          ParameterConstraint<? super S> c = cons.get(i);
          if(i > 0) {
            description.append(", ");
          }
          description.append(c.getDescription(param.getName()));
        }
      }
      else {
        description.append(param.getName()).append(" must be set.");
      }
    }
    return description.toString();
  }
}