From f73d0784caa333464905a7e14f7359e4a090a227 Mon Sep 17 00:00:00 2001 From: Artur Meski Date: Wed, 28 Dec 2016 18:32:32 +0100 Subject: [PATCH] working on ECA --- rs/extended_context_automaton.py | 38 +++++++++++++++++++++++++++----- rs_testing.py | 4 ++-- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/rs/extended_context_automaton.py b/rs/extended_context_automaton.py index 27b45de..2b18773 100644 --- a/rs/extended_context_automaton.py +++ b/rs/extended_context_automaton.py @@ -8,8 +8,15 @@ class ExtendedContextAutomaton(ContextAutomaton): def __init__(self, reaction_system): self._actions = [] super(ExtendedContextAutomaton, self).__init__(reaction_system) - - def add_transition(self, src, action, ctx_reaction, dst): + + def add_transition(self, src, actions, ctx_reaction, dst): + """Adds a transition + + src: is the source state name + dst: is the destination state name + actions: is the set of actions with which the transitions is synchronised + ctx_reaction: is the context reaction associated with the transition + """ ctx_reactants, ctx_inhibitors, ctx_products = ctx_reaction @@ -33,29 +40,32 @@ class ExtendedContextAutomaton(ContextAutomaton): src_id = self.get_state_id(src) dst_id = self.get_state_id(dst) - act_id = self.get_action_id(action) + act_ids = self.get_set_of_action_ids(actions) r_ids = self.get_set_of_ids(ctx_reactants) i_ids = self.get_set_of_ids(ctx_inhibitors) p_ids = self.get_set_of_ids(ctx_products) - self._transitions.append((src_id, act_id, (r_ids, i_ids, p_ids), dst_id)) + self._transitions.append((src_id, act_ids, (r_ids, i_ids, p_ids), dst_id)) def show_transitions(self): + """Prints the set of registered transitions""" print(C_MARK_INFO + " Context automaton transitions:") for src_id, act_id, reaction, dst_id in self._transitions: str_transition = self.get_state_name(src_id) + " --( " - str_transition += self.get_action_name(act_id) + " | " + str_transition += "<" + self.get_actions_str(act_id) + "> | " str_transition += "( " + self.rsset2str(reaction[0]) + "," + self.rsset2str(reaction[1]) + "," + self.rsset2str(reaction[2]) + " )" str_transition += " )--> " + self.get_state_name(dst_id) print(" - " + str_transition) def add_action(self, action_name): + """Registers an action""" if action_name not in self._actions: self._actions.append(action_name) else: print("\'%s\' already added. skipping..." % (action_name,)) def get_action_id(self, action_name): + """For an action name returns its id""" try: return self._actions.index(action_name) except ValueError: @@ -64,11 +74,27 @@ class ExtendedContextAutomaton(ContextAutomaton): def get_action_name(self, action_id): return self._actions[action_id] + + def get_set_of_action_ids(self, actions): + """Converts a set of actions into the set of their ids""" + act_ids = set() + for act in actions: + act_ids.add(self.get_action_id(act)) + return act_ids + + def get_actions_str(self, actions): + """Returns the string for the set of action ids given by actions""" + s = "" + for act in actions: + s += self.get_action_name(act) + ", " + s = s[:-2] + return s def show_actions(self): + """Prints all the actions""" print(C_MARK_INFO + " Context automaton actions:") for act in self._actions: - print(" - " + act) + print(" - " + act + " (id=" + str(self.get_action_id(act)) + ")") def show(self): self.show_states() diff --git a/rs_testing.py b/rs_testing.py index d1706d1..6ccd8f5 100644 --- a/rs_testing.py +++ b/rs_testing.py @@ -17,8 +17,8 @@ def test_extended_automaton(): c.add_state("working") c.add_action("act1") c.add_action("act2") - c.add_transition("init", "act1", ([],[],["inc"]), "working") - c.add_transition("working", "act2", ([],[],["inc"]), "working") + c.add_transition("init", ["act1", "act2"], ([],[],["inc"]), "working") + c.add_transition("working", ["act2"], ([],[],["inc"]), "working") rc = ReactionSystemWithAutomaton(r,c)