def __init__(self, input=None): """Create InputParser object.""" self._original_sections = None self._filename = None self._sections = None if input is not None: if isinstance(input, dict): self._sections = input elif isinstance(input, str): self._filename = input else: raise AlgorithmError("Invalid parser input type.") self._section_order = [ InputParser.PROBLEM, InputParser.INPUT, InputParser.ALGORITHM ] for pluggable_type in local_pluggables_types(): if pluggable_type != InputParser.ALGORITHM: self._section_order.append(pluggable_type) self._section_order.extend([InputParser.BACKEND, InputParser._UNKNOWN]) problems_dict = OrderedDict() for algo_name in local_algorithms(): problems = InputParser.get_algorithm_problems(algo_name) for problem in problems: problems_dict[problem] = None problems_enum = {'enum': list(problems_dict.keys())} jsonfile = os.path.join(os.path.dirname(__file__), 'input_schema.json') with open(jsonfile) as json_file: self._schema = json.load(json_file) self._schema['definitions'][InputParser.PROBLEM]['properties'][ InputParser.NAME]['oneOf'] = [problems_enum] self._original_schema = copy.deepcopy(self._schema)
def _update_algorithm_problem(self): problem_name = self.get_section_property(JSONSchema.PROBLEM, JSONSchema.NAME) if problem_name is None: problem_name = self.get_property_default_value( JSONSchema.PROBLEM, JSONSchema.NAME) if problem_name is None: raise AquaChemistryError( "No algorithm 'problem' section found on input.") algo_name = self.get_section_property(JSONSchema.ALGORITHM, JSONSchema.NAME) if algo_name is not None and problem_name in InputParser.get_algorithm_problems( algo_name): return for algo_name in local_algorithms(): if problem_name in self.get_algorithm_problems(algo_name): # set to the first algorithm to solve the problem self.set_section_property(JSONSchema.ALGORITHM, JSONSchema.NAME, algo_name) return # no algorithm solve this problem, remove section self.delete_section(JSONSchema.ALGORITHM)
def populate_problem_names(self): """Populate enum list of problem names""" problems_dict = OrderedDict() for algo_name in local_algorithms(): problems = JSONSchema.get_algorithm_problems(algo_name) for problem in problems: problems_dict[problem] = None problems_enum = {'enum': list(problems_dict.keys())} self._schema['properties'][JSONSchema.PROBLEM]['properties'][ JSONSchema.NAME]['oneOf'] = [problems_enum]