Implement bottom-up recursion through tree
This commit is contained in:
parent
1e389f4d65
commit
20eb60e737
@ -36,6 +36,12 @@ class Voter(EmbeddedObject):
|
|||||||
|
|
||||||
class Question(EmbeddedObject):
|
class Question(EmbeddedObject):
|
||||||
prompt = StringField()
|
prompt = StringField()
|
||||||
|
|
||||||
|
def compute_result(self):
|
||||||
|
...
|
||||||
|
|
||||||
|
class Result(EmbeddedObject):
|
||||||
|
pass
|
||||||
|
|
||||||
class ApprovalQuestion(Question):
|
class ApprovalQuestion(Question):
|
||||||
choices = ListField(StringField())
|
choices = ListField(StringField())
|
||||||
@ -43,9 +49,13 @@ class ApprovalQuestion(Question):
|
|||||||
class ApprovalAnswer(Answer):
|
class ApprovalAnswer(Answer):
|
||||||
choices = ListField(IntField())
|
choices = ListField(IntField())
|
||||||
|
|
||||||
|
class ApprovalResult(Result):
|
||||||
|
choices = ListField(IntField())
|
||||||
|
|
||||||
class Election(TopLevelObject):
|
class Election(TopLevelObject):
|
||||||
_id = UUIDField()
|
_id = UUIDField()
|
||||||
workflow = EmbeddedObjectField(Workflow) # Once saved, we don't care what kind of workflow it is
|
workflow = EmbeddedObjectField(Workflow) # Once saved, we don't care what kind of workflow it is
|
||||||
name = StringField()
|
name = StringField()
|
||||||
voters = EmbeddedObjectListField(hashed=False)
|
voters = EmbeddedObjectListField(hashed=False)
|
||||||
questions = EmbeddedObjectListField()
|
questions = EmbeddedObjectListField()
|
||||||
|
results = EmbeddedObjectListField(hashed=False)
|
||||||
|
@ -25,17 +25,30 @@ class ElectionTestCase(TestCase):
|
|||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
client.drop_database('test')
|
client.drop_database('test')
|
||||||
|
|
||||||
|
def exit_task_assert(self, election, task, next_task):
|
||||||
|
self.assertEqual(election.workflow.get_task(task).status, WorkflowTask.Status.READY)
|
||||||
|
self.assertEqual(election.workflow.get_task(next_task).status, WorkflowTask.Status.NOT_READY)
|
||||||
|
election.workflow.get_task(task).exit()
|
||||||
|
self.assertEqual(election.workflow.get_task(task).status, WorkflowTask.Status.EXITED)
|
||||||
|
self.assertEqual(election.workflow.get_task(next_task).status, WorkflowTask.Status.READY)
|
||||||
|
|
||||||
def test_run_election(self):
|
def test_run_election(self):
|
||||||
# Set up election
|
# Set up election
|
||||||
election = Election()
|
election = Election()
|
||||||
election.workflow = WorkflowBase(election)
|
election.workflow = WorkflowBase()
|
||||||
|
|
||||||
|
# Check _instance
|
||||||
|
self.assertEqual(election.workflow._instance, (election, 'workflow'))
|
||||||
|
|
||||||
self.assertEqual(election.workflow.get_task('eos.base.workflow.TaskConfigureElection').status, WorkflowTask.Status.READY)
|
self.assertEqual(election.workflow.get_task('eos.base.workflow.TaskConfigureElection').status, WorkflowTask.Status.READY)
|
||||||
|
|
||||||
election.name = 'Test Election'
|
election.name = 'Test Election'
|
||||||
|
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
election.voters.append(Voter())
|
voter = Voter()
|
||||||
|
election.voters.append(voter)
|
||||||
|
# Check _instance
|
||||||
|
self.assertEqual(voter._instance, (election.voters, None))
|
||||||
|
|
||||||
question = ApprovalQuestion(prompt='President', choices=['John Smith', 'Joe Bloggs', 'John Q. Public'])
|
question = ApprovalQuestion(prompt='President', choices=['John Smith', 'Joe Bloggs', 'John Q. Public'])
|
||||||
election.questions.append(question)
|
election.questions.append(question)
|
||||||
@ -49,12 +62,7 @@ class ElectionTestCase(TestCase):
|
|||||||
self.assertEqual(db[Election._name].find_one(), election.serialise())
|
self.assertEqual(db[Election._name].find_one(), election.serialise())
|
||||||
|
|
||||||
# Freeze election
|
# Freeze election
|
||||||
self.assertEqual(election.workflow.get_task('eos.base.workflow.TaskConfigureElection').status, WorkflowTask.Status.READY)
|
self.exit_task_assert(election, 'eos.base.workflow.TaskConfigureElection', 'eos.base.workflow.TaskOpenVoting')
|
||||||
self.assertEqual(election.workflow.get_task('eos.base.workflow.TaskOpenVoting').status, WorkflowTask.Status.NOT_READY)
|
|
||||||
election.workflow.get_task('eos.base.workflow.TaskConfigureElection').exit()
|
|
||||||
self.assertEqual(election.workflow.get_task('eos.base.workflow.TaskConfigureElection').status, WorkflowTask.Status.EXITED)
|
|
||||||
self.assertEqual(election.workflow.get_task('eos.base.workflow.TaskOpenVoting').status, WorkflowTask.Status.READY)
|
|
||||||
|
|
||||||
election.save()
|
election.save()
|
||||||
|
|
||||||
# Cast ballots
|
# Cast ballots
|
||||||
@ -69,3 +77,11 @@ class ElectionTestCase(TestCase):
|
|||||||
election.voters[i].ballots.append(ballot)
|
election.voters[i].ballots.append(ballot)
|
||||||
|
|
||||||
election.save()
|
election.save()
|
||||||
|
|
||||||
|
# Close voting
|
||||||
|
self.exit_task_assert(election, 'eos.base.workflow.TaskOpenVoting', 'eos.base.workflow.TaskCloseVoting')
|
||||||
|
election.save()
|
||||||
|
|
||||||
|
# Compute result
|
||||||
|
for i in range(2):
|
||||||
|
result = election.questions[i].compute_result()
|
||||||
|
@ -29,13 +29,13 @@ class WorkflowTask(EmbeddedObject):
|
|||||||
|
|
||||||
status = IntField()
|
status = IntField()
|
||||||
|
|
||||||
def __init__(self, workflow=None, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def post_init(self):
|
||||||
|
super().post_init()
|
||||||
|
|
||||||
self.workflow = workflow
|
self.workflow = self.recurse_parents(Workflow)
|
||||||
|
|
||||||
if self.workflow is None:
|
|
||||||
self.workflow = self._instance
|
|
||||||
|
|
||||||
self.status = WorkflowTask.Status.READY if self.are_dependencies_met() else WorkflowTask.Status.NOT_READY
|
self.status = WorkflowTask.Status.READY if self.are_dependencies_met() else WorkflowTask.Status.NOT_READY
|
||||||
|
|
||||||
@ -79,9 +79,8 @@ class Workflow(EmbeddedObject):
|
|||||||
'abstract': True
|
'abstract': True
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, election=None, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.election = election if election else self._instance
|
|
||||||
|
|
||||||
def get_tasks(self, descriptor):
|
def get_tasks(self, descriptor):
|
||||||
yield from (task for task in self.tasks if task.satisfies(descriptor))
|
yield from (task for task in self.tasks if task.satisfies(descriptor))
|
||||||
@ -103,12 +102,16 @@ class TaskConfigureElection(WorkflowTask):
|
|||||||
class TaskOpenVoting(WorkflowTask):
|
class TaskOpenVoting(WorkflowTask):
|
||||||
depends_on = ['eos.base.workflow.TaskConfigureElection']
|
depends_on = ['eos.base.workflow.TaskConfigureElection']
|
||||||
|
|
||||||
|
class TaskCloseVoting(WorkflowTask):
|
||||||
|
depends_on = ['eos.base.workflow.TaskOpenVoting']
|
||||||
|
|
||||||
# Concrete workflows
|
# Concrete workflows
|
||||||
# ==================
|
# ==================
|
||||||
|
|
||||||
class WorkflowBase(Workflow):
|
class WorkflowBase(Workflow):
|
||||||
def __init__(self, election=None, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(election, *args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
self.tasks.append(TaskConfigureElection(self))
|
self.tasks.append(TaskConfigureElection())
|
||||||
self.tasks.append(TaskOpenVoting(self))
|
self.tasks.append(TaskOpenVoting())
|
||||||
|
self.tasks.append(TaskCloseVoting())
|
||||||
|
@ -58,7 +58,7 @@ class EmbeddedObjectField(Field):
|
|||||||
|
|
||||||
class ListField(Field):
|
class ListField(Field):
|
||||||
def __init__(self, element_field=None, *args, **kwargs):
|
def __init__(self, element_field=None, *args, **kwargs):
|
||||||
super().__init__(default=[], *args, **kwargs)
|
super().__init__(default=EosList, *args, **kwargs)
|
||||||
self.element_field = element_field
|
self.element_field = element_field
|
||||||
|
|
||||||
def serialise(self, value):
|
def serialise(self, value):
|
||||||
@ -69,7 +69,7 @@ class ListField(Field):
|
|||||||
|
|
||||||
class EmbeddedObjectListField(Field):
|
class EmbeddedObjectListField(Field):
|
||||||
def __init__(self, object_type=None, *args, **kwargs):
|
def __init__(self, object_type=None, *args, **kwargs):
|
||||||
super().__init__(default=[], *args, **kwargs)
|
super().__init__(default=EosList, *args, **kwargs)
|
||||||
self.object_type = object_type
|
self.object_type = object_type
|
||||||
|
|
||||||
def serialise(self, value):
|
def serialise(self, value):
|
||||||
@ -102,6 +102,20 @@ class EosObjectType(type):
|
|||||||
class EosObject(metaclass=EosObjectType):
|
class EosObject(metaclass=EosObjectType):
|
||||||
objects = {}
|
objects = {}
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._instance = (None, None)
|
||||||
|
self._inited = False
|
||||||
|
|
||||||
|
def post_init(self):
|
||||||
|
self._inited = True
|
||||||
|
|
||||||
|
def recurse_parents(self, cls):
|
||||||
|
if isinstance(self, cls):
|
||||||
|
return self
|
||||||
|
if self._instance[0]:
|
||||||
|
return self._instance[0].recurse_parents(cls)
|
||||||
|
return None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def serialise_and_wrap(value, object_type=None):
|
def serialise_and_wrap(value, object_type=None):
|
||||||
if object_type:
|
if object_type:
|
||||||
@ -114,6 +128,14 @@ class EosObject(metaclass=EosObjectType):
|
|||||||
return object_type.deserialise(value)
|
return object_type.deserialise(value)
|
||||||
return EosObject.objects[value['type']].deserialise(value['value'])
|
return EosObject.objects[value['type']].deserialise(value['value'])
|
||||||
|
|
||||||
|
class EosList(EosObject, list):
|
||||||
|
def append(self, value):
|
||||||
|
if isinstance(value, EosObject):
|
||||||
|
value._instance = (self, None)
|
||||||
|
if not value._inited:
|
||||||
|
value.post_init()
|
||||||
|
return super().append(value)
|
||||||
|
|
||||||
class DocumentObjectType(EosObjectType):
|
class DocumentObjectType(EosObjectType):
|
||||||
def __new__(meta, name, bases, attrs):
|
def __new__(meta, name, bases, attrs):
|
||||||
cls = EosObjectType.__new__(meta, name, bases, attrs)
|
cls = EosObjectType.__new__(meta, name, bases, attrs)
|
||||||
@ -123,14 +145,35 @@ class DocumentObjectType(EosObjectType):
|
|||||||
for attr in list(dir(cls)):
|
for attr in list(dir(cls)):
|
||||||
val = getattr(cls, attr)
|
val = getattr(cls, attr)
|
||||||
if isinstance(val, Field):
|
if isinstance(val, Field):
|
||||||
|
val._instance = (cls, name)
|
||||||
fields[attr] = val
|
fields[attr] = val
|
||||||
delattr(cls, attr)
|
delattr(cls, attr)
|
||||||
cls._fields = fields
|
cls._fields = fields
|
||||||
|
|
||||||
|
# Make properties
|
||||||
|
def make_property(name, field):
|
||||||
|
def field_getter(self):
|
||||||
|
return self._field_values[name]
|
||||||
|
def field_setter(self, value):
|
||||||
|
if isinstance(value, EosObject):
|
||||||
|
value._instance = (self, name)
|
||||||
|
if not value._inited:
|
||||||
|
value.post_init()
|
||||||
|
|
||||||
|
self._field_values[name] = value
|
||||||
|
return property(field_getter, field_setter)
|
||||||
|
|
||||||
|
for attr, val in fields.items():
|
||||||
|
setattr(cls, attr, make_property(attr, val))
|
||||||
|
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
class DocumentObject(metaclass=DocumentObjectType):
|
class DocumentObject(EosObject, metaclass=DocumentObjectType):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self._field_values = {}
|
||||||
|
|
||||||
for attr, val in self._fields.items():
|
for attr, val in self._fields.items():
|
||||||
if attr in kwargs:
|
if attr in kwargs:
|
||||||
setattr(self, attr, kwargs[attr])
|
setattr(self, attr, kwargs[attr])
|
||||||
|
Loading…
Reference in New Issue
Block a user