EmbASP-Python
handler.py
1 from abc import ABCMeta
2 
3 
4 class Handler(object):
5  """A collection of InputProgram and OptionDescriptor.
6 
7  The subclasses have to implement start_async(Callback, List, List)
8  and start_sync(List, List) methods.
9  """
10  __metaclass__ = ABCMeta
11 
12  def __init__(self):
13  self._programs = dict() # Is where InputProgram elements are stored.
14  self._options = dict() # Is where OptionDescriptor elements are stored
15 
16  def add_option(self, o):
17  """Add a new element inside _options dict.
18 
19  The o parameter is the new OptionDescriptor instance. The method
20  return the id associated whit the added OptionDescriptor
21  instance.
22  """
23  last_index = len(self._options)
24  current_value = last_index
25  self._options[last_index] = o
26  return current_value
27 
28  def add_program(self, program):
29  """Add a new element inside _programs dict.
30 
31  The program param is the InputProgram instance added to the
32  collection. The method return the id associated whit the added
33  InputProgram instance.
34  """
35  last_index = len(self._programs)
36  current_value = last_index
37  self._programs[last_index] = program
38  return current_value
39 
40  def _collect_options(self, option_index):
41  """Return a list of options in _options dict, according to set of
42  indexes given.
43 
44  If option_index is empty, the method return a list of all
45  options.
46  """
47  input_option = list()
48  if not option_index:
49  for k in self._options.keys():
50  input_option.append(self._options.get(k))
51  else:
52  for index in option_index:
53  input_option.append(self._options.get(index))
54  return input_option
55 
56  def _collect_programs(self, program_index):
57  """Return a list of programs in _programs dict, according to set of
58  indexes given.
59 
60  If program_index is empty, the method return a list of all
61  program.
62  """
63  input_programs = list()
64  if not program_index:
65  for k in self._programs.keys():
66  input_programs.append(self._programs.get(k))
67  else:
68  for index in program_index:
69  input_programs.append(self._programs.get(index))
70  return input_programs
71 
72  def get_input_program(self, key):
73  """Returns the specified InputProgram element.
74 
75  The parameter key represents the id. The method return the
76  InputProgram element associated with the given key.
77  """
78  return self._programs.get(key)
79 
80  def get_option_descriptor(self, key):
81  """Returns the specified OptionDescriptor element.
82 
83  The parameter key represents the id. The method return the
84  OptionDescriptor element associated with the given key.
85  """
86  return self._options.get(key)
87 
88  def remove_all(self):
89  """Removes all the elements from _programs and _options.
90 
91  Both the collections will be empty after this method returns.
92  """
93  self._options.clear()
94  self._programs.clear()
95 
96  def remove_option_from_id(self, option_id):
97  """Removes the element associated with the given id from _options
98  dict.
99 
100  option_id represents the id associated with an element.
101  """
102  self._options.pop(option_id)
103 
105  """Removes every occurrence of a specified OptionDescriptor element
106  from _options dict.
107 
108  the parameter o represents the element to be removed.
109  The method return true if one or more elements are removed,
110  false otherwise
111  """
112  result = False
113  for k in self._options:
114  if self._options.get(k) == o:
115  self._options.pop(k)
116  result = True
117  return result
118 
120  """Removes every occurrence of a specified InputProgram element from
121  _programs dict.
122 
123  The parameter p represents the element to be removed.
124  The method returns true if one or more elements are removed,
125  false otherwise
126  """
127  result = False
128  for k in self._programs:
129  if self._programs.get(k) == p:
130  self._programs.pop(k)
131  result = True
132  return result
133 
134  def remove_program_from_id(self, program_id):
135  """Removes the element associated with the given id from _programs}
136  dict.
137 
138  The parameter program_id represents the id associated with an
139  element
140  """
141  self._programs.pop(program_id)
142 
143  def start_async(self, c, program_index=None, option_index=None):
144  """This method have to be implemented by subclasses to execute solver
145  in an asynchronous way, if no parameters are given, the entire sets of
146  programs and option are used."""
147  pass
148 
149  def start_sync(self, program_index=None, option_index=None):
150  """This method have to be implemented by subclasses to execute solver
151  in a synchronous way, if no parameters are given, the entire sets of
152  programs and option are used."""
153  return None
base.handler.Handler._options
_options
Definition: handler.py:14
base.handler.Handler
Definition: handler.py:4
base.handler.Handler.remove_program_from_value
def remove_program_from_value(self, p)
Definition: handler.py:119
base.handler.Handler.remove_option_from_value
def remove_option_from_value(self, o)
Definition: handler.py:104
base.handler.Handler.remove_all
def remove_all(self)
Definition: handler.py:88
base.handler.Handler.add_option
def add_option(self, o)
Definition: handler.py:16
base.handler.Handler.add_program
def add_program(self, program)
Definition: handler.py:28
base.handler.Handler.start_sync
def start_sync(self, program_index=None, option_index=None)
Definition: handler.py:149
base.handler.Handler.start_async
def start_async(self, c, program_index=None, option_index=None)
Definition: handler.py:143
base.handler.Handler.get_input_program
def get_input_program(self, key)
Definition: handler.py:72
base.handler.Handler.remove_option_from_id
def remove_option_from_id(self, option_id)
Definition: handler.py:96
base.handler.Handler.remove_program_from_id
def remove_program_from_id(self, program_id)
Definition: handler.py:134
base.handler.Handler.get_option_descriptor
def get_option_descriptor(self, key)
Definition: handler.py:80
base.handler.Handler._programs
_programs
Definition: handler.py:13