| 30 | |
| 31 | |
| 32 | class Planner(FirstClassObjectInterface, BaseObject): |
| 33 | |
| 34 | schema = PlannerSchema() |
| 35 | display_schema = PlannerSchema(exclude=['module', 'ignore_enforcement_modules']) |
| 36 | |
| 37 | @property |
| 38 | def unique(self): |
| 39 | return self.hash(self.name) |
| 40 | |
| 41 | def __init__(self, name='', planner_id='', module='', params=None, stopping_conditions=None, description=None, |
| 42 | ignore_enforcement_modules=(), allow_repeatable_abilities=False, plugin=''): |
| 43 | super().__init__() |
| 44 | self.name = name |
| 45 | self.planner_id = planner_id if planner_id else str(uuid.uuid4()) |
| 46 | self.module = module |
| 47 | self.params = params if params else {} |
| 48 | self.description = description |
| 49 | self.stopping_conditions = self._set_stopping_conditions(stopping_conditions) |
| 50 | self.ignore_enforcement_modules = ignore_enforcement_modules |
| 51 | self.allow_repeatable_abilities = allow_repeatable_abilities |
| 52 | self.plugin = plugin |
| 53 | |
| 54 | def store(self, ram): |
| 55 | existing = self.retrieve(ram['planners'], self.unique) |
| 56 | if not existing: |
| 57 | ram['planners'].append(self) |
| 58 | return self.retrieve(ram['planners'], self.unique) |
| 59 | else: |
| 60 | existing.update('stopping_conditions', self.stopping_conditions) |
| 61 | existing.update('params', self.params) |
| 62 | existing.update('plugin', self.plugin) |
| 63 | existing.update('description', self.description) |
| 64 | existing.update('allow_repeatable_abilities', self.allow_repeatable_abilities) |
| 65 | return existing |
| 66 | |
| 67 | async def which_plugin(self): |
| 68 | return self.plugin |
| 69 | |
| 70 | @staticmethod |
| 71 | def _set_stopping_conditions(conditions): |
| 72 | if conditions: |
| 73 | return [Fact.load(dict(trait=trait, value=value)) for sc in conditions for trait, value in sc.items()] |
| 74 | return [] |