Validate the parameters consistency based on the restrictions. This method validates the internal consistency using the pre-defined list of restrictions. A restriction is defined as a string which specifies a binary operation. The supported binary operations are {'==', '!=', '<', '<=',
(self)
| 220 | return params_dict |
| 221 | |
| 222 | def validate(self): |
| 223 | """Validate the parameters consistency based on the restrictions. |
| 224 | |
| 225 | This method validates the internal consistency using the pre-defined list of |
| 226 | restrictions. A restriction is defined as a string which specifies a binary |
| 227 | operation. The supported binary operations are {'==', '!=', '<', '<=', '>', |
| 228 | '>='}. Note that the meaning of these operators are consistent with the |
| 229 | underlying Python immplementation. Users should make sure the define |
| 230 | restrictions on their type make sense. |
| 231 | |
| 232 | For example, for a ParamsDict like the following |
| 233 | ``` |
| 234 | a: |
| 235 | a1: 1 |
| 236 | a2: 2 |
| 237 | b: |
| 238 | bb: |
| 239 | bb1: 10 |
| 240 | bb2: 20 |
| 241 | ccc: |
| 242 | a1: 1 |
| 243 | a3: 3 |
| 244 | ``` |
| 245 | one can define two restrictions like this |
| 246 | ['a.a1 == b.ccc.a1', 'a.a2 <= b.bb.bb2'] |
| 247 | |
| 248 | What it enforces are: |
| 249 | - a.a1 = 1 == b.ccc.a1 = 1 |
| 250 | - a.a2 = 2 <= b.bb.bb2 = 20 |
| 251 | |
| 252 | Raises: |
| 253 | KeyError: if any of the following happens |
| 254 | (1) any of parameters in any of restrictions is not defined in |
| 255 | ParamsDict, |
| 256 | (2) any inconsistency violating the restriction is found. |
| 257 | ValueError: if the restriction defined in the string is not supported. |
| 258 | """ |
| 259 | |
| 260 | def _get_kv(dotted_string, params_dict): |
| 261 | """Get keys and values indicated by dotted_string.""" |
| 262 | if _CONST_VALUE_RE.match(dotted_string) is not None: |
| 263 | const_str = dotted_string |
| 264 | if const_str == 'None': |
| 265 | constant = None |
| 266 | else: |
| 267 | constant = float(const_str) |
| 268 | return None, constant |
| 269 | else: |
| 270 | tokenized_params = dotted_string.split('.') |
| 271 | v = params_dict |
| 272 | for t in tokenized_params: |
| 273 | v = v[t] |
| 274 | return tokenized_params[-1], v |
| 275 | |
| 276 | def _get_kvs(tokens, params_dict): |
| 277 | if len(tokens) != 2: |
| 278 | raise ValueError('Only support binary relation in restriction.') |
| 279 | stripped_tokens = [t.strip() for t in tokens] |