Skip to content

Commit ead1ab7

Browse files
msarcevGrgoMariani
authored andcommitted
Refactor for consistency and readability (use PEP8 style) (#2)
* Refactor for consistency and readability (use PEP8 style) * Fix some issues
1 parent 9d0a3ff commit ead1ab7

File tree

18 files changed

+135
-135
lines changed

18 files changed

+135
-135
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def ON_START(*args, **kwargs):
4949
def HELLO(*args, **kwargs):
5050
print("Hello!")
5151

52-
FSM.OPERATION.start_fsm() # prints "FSM start"
53-
FSM.OPERATION.run_fsm("is_anyone_there") # prints "Hello!"
52+
FSM.OPERATION.start() # prints "FSM start"
53+
FSM.OPERATION.run("is_anyone_there") # prints "Hello!"
5454
```
5555

5656
To learn more please check [the examples at the bottom of the page](#additional-links).

automatabpp/automatabpp.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
__all__ = ["BEHAVIOUR", "EXECUTION", "OPERATION", "INTERFACE", "COMPARISONS"]
2-
31
import logging
4-
automata_bpp_logger = logging.getLogger(__name__)
2+
from functools import wraps
3+
4+
LOGGER = logging.getLogger(__name__)
5+
6+
from .commandqueue.commandqueue import CommandQueue
7+
from .comparisons.comparisons import COMPARISONS
8+
from .constants import START_COMMAND, STOP_COMMAND, GRAPHS_DIRECTORY
9+
from .machines.machines import Machines
10+
from .xml.xmlread import read_graphml
11+
12+
__all__ = ["BEHAVIOUR", "EXECUTION", "OPERATION", "INTERFACE", "COMPARISONS", "LOGGER"]
513

6-
from . machines.machines import Machines
7-
from . xml.xmlread import read_graphml
8-
from . commandqueue.commandqueue import CommandQueue
9-
from . constants import GRAPHS_DIRECTORY, START_COMMAND_NAME, STOP_COMMAND_NAME
10-
from . comparisons.comparisons import COMPARISONS
1114

12-
from functools import wraps
1315

1416

1517
class BEHAVIOUR:
@@ -24,42 +26,42 @@ def load_behaviour_from_graph(graph_file_path: str, machine_name: str):
2426
if graph_file_path[-8:] == ".graphml":
2527
read_graphml("{}/{}".format(GRAPHS_DIRECTORY, graph_file_path), machine_name)
2628
else:
27-
automata_bpp_logger.warning("Unknown format for reading the graph file: {}".format(graph_file_path))
29+
LOGGER.warning("Unknown format for reading the graph file: {}".format(graph_file_path))
2830

2931

3032
class EXECUTION:
3133

3234
@staticmethod
3335
def state(func):
34-
current_machine = Machines().GetCurrentDefinedMachine()
35-
if current_machine is not None:
36-
current_machine.SetExecuteStateFunction(func.__name__, func)
36+
machine = Machines().this_machine()
37+
if machine is not None:
38+
machine.set_state_function(func.__name__, func)
3739
return func
3840

3941

4042
class OPERATION:
4143

4244
@staticmethod
43-
def start_fsm():
44-
Machines().ExecuteCommand(START_COMMAND_NAME)
45+
def start():
46+
Machines().execute_command(START_COMMAND)
4547

4648
@staticmethod
47-
def stop_fsm():
48-
Machines().ExecuteCommand(STOP_COMMAND_NAME)
49-
Machines().ReturnToStart()
50-
CommandQueue().EmptyAllCommands()
49+
def stop():
50+
Machines().execute_command(STOP_COMMAND)
51+
Machines().reset_machines()
52+
CommandQueue().clear_all()
5153

5254
@staticmethod
53-
def reset_fsm():
54-
OPERATION.stop_fsm()
55-
OPERATION.start_fsm()
55+
def reset():
56+
OPERATION.stop()
57+
OPERATION.start()
5658

5759
@staticmethod
58-
def run_fsm(cmd=None):
60+
def run(cmd=None):
5961
if cmd is None:
60-
Machines().ExecuteAllCommands()
62+
Machines().execute_all()
6163
else:
62-
Machines().ExecuteCommand(cmd)
64+
Machines().execute_command(cmd)
6365

6466

6567
class INTERFACE:
@@ -71,7 +73,9 @@ def wrapper_out(func):
7173
def wrapper_in(*args, **kwargs):
7274
result = func(*args, **kwargs)
7375
if lambda_func(result):
74-
Machines().ExecuteCommand(command)
76+
Machines().execute_command(command)
7577
return result
78+
7679
return wrapper_in
80+
7781
return wrapper_out
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
from automatabpp.automatabpp import automata_bpp_logger
1+
from automatabpp.automatabpp import LOGGER
22
from automatabpp.metaclasses.singleton import Singleton
33

44

55
class CommandQueue(object, metaclass=Singleton):
66
SEPARATOR = "\n"
77

88
def __init__(self):
9-
self.__list_of_cmds_to_execute = []
9+
self.__commands = []
1010

1111
def __len__(self):
12-
return len(self.__list_of_cmds_to_execute)
12+
return len(self.__commands)
1313

14-
def GetNextCommand(self):
14+
def get_next(self):
1515
if len(self) > 0:
16-
return self.__list_of_cmds_to_execute.pop(0)
16+
return self.__commands.pop(0)
1717
return None
1818

19-
def PushCommandsToQueue(self, commands: str):
19+
def push_commands(self, commands: str):
2020
for cmd in commands.split(CommandQueue.SEPARATOR):
2121
if len(cmd) > 0:
22-
automata_bpp_logger.debug("++++++> {} pushed to CommandQueue <++++++".format(cmd))
23-
self.__list_of_cmds_to_execute.append(cmd)
24-
25-
def EmptyAllCommands(self):
26-
self.__list_of_cmds_to_execute.clear()
22+
LOGGER.debug("++++++> {} pushed to CommandQueue <++++++".format(cmd))
23+
self.__commands.append(cmd)
2724

25+
def clear_all(self):
26+
self.__commands.clear()

automatabpp/constants/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
GRAPHS_DIRECTORY = "graphs"
22

3-
START_STATE_NAME = "_START_"
4-
START_COMMAND_NAME = "_start_"
5-
STOP_COMMAND_NAME = "_stop_"
3+
START_STATE = "_START_"
4+
START_COMMAND = "_start_"
5+
STOP_COMMAND = "_stop_"

automatabpp/machine/machine.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
1-
from automatabpp.automatabpp import automata_bpp_logger
1+
from automatabpp.automatabpp import LOGGER
2+
from automatabpp.constants import START_STATE
23
from automatabpp.state.state import State
3-
from automatabpp.constants import START_STATE_NAME
44

55

66
class Machine(object):
77

88
def __init__(self, machine_name: str):
99
self.machine_name = machine_name
1010
self.states = dict()
11-
self.curr_state = self.GetStateWithName(START_STATE_NAME)
11+
self.curr_state = self.get_state_by_name(START_STATE)
1212

13-
def GetActiveState(self):
14-
return self.curr_state
15-
16-
def GetStateWithName(self, state_name: str):
13+
def get_state_by_name(self, state_name: str):
1714
if state_name in self.states.keys():
1815
return self.states[state_name]
1916
self.states[state_name] = State(state_name, self.machine_name)
2017
return self.states[state_name]
2118

22-
def AddTransition(self, st_before: str, command: str, st_after: str):
23-
self.GetStateWithName(st_before).SetTransition(command, st_after)
19+
def add_transition(self, state_before: str, command: str, state_after: str):
20+
self.get_state_by_name(state_before).set_transition(command, state_after)
2421

25-
def SetExecuteStateFunction(self, state_name: str, callback: callable):
26-
self.GetStateWithName(state_name).SetExecuteStateFunction(callback)
22+
def set_state_function(self, state_name: str, callback: callable):
23+
self.get_state_by_name(state_name).set_callback(callback)
2724

28-
def ExecuteTransition(self, command: str):
29-
st_after_name = self.curr_state.GetNextStateWithTransition(command)
25+
def transition(self, command: str):
26+
st_after_name = self.curr_state.transition_to_next(command)
3027
if st_after_name is not None:
31-
automata_bpp_logger.debug("{}: state change {}->{}".format(self.machine_name, self.curr_state.GetName(), st_after_name))
32-
self.curr_state = self.GetStateWithName(st_after_name)
33-
self.curr_state.ExecuteState(command)
28+
LOGGER.debug("{}: state change {}->{}".format(self.machine_name, self.curr_state.get_name(), st_after_name))
29+
self.curr_state = self.get_state_by_name(st_after_name)
30+
self.curr_state.execute_state(command)
3431
return True
3532
return False
3633

37-
def ReturnToStart(self):
38-
self.curr_state = self.GetStateWithName(START_STATE_NAME)
34+
def reset_to_start(self):
35+
self.curr_state = self.get_state_by_name(START_STATE)

automatabpp/machines/machines.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,39 @@
1-
from automatabpp.automatabpp import automata_bpp_logger
2-
from automatabpp.metaclasses.singleton import Singleton
3-
from automatabpp.machine.machine import Machine
1+
from automatabpp.automatabpp import LOGGER
42
from automatabpp.commandqueue.commandqueue import CommandQueue
3+
from automatabpp.machine.machine import Machine
4+
from automatabpp.metaclasses.singleton import Singleton
55

66

77
class Machines(object, metaclass=Singleton):
88

99
def __init__(self):
10-
self.__list_of_machines = []
11-
self.curr_machine_to_define = None
10+
self.machines = []
11+
self.machine = None
1212

13-
def AddNewMachine(self, machine_name: str):
14-
self.curr_machine_to_define = Machine(machine_name)
15-
self.__list_of_machines.append(self.curr_machine_to_define)
16-
return self.curr_machine_to_define
13+
def add_new_machine(self, machine_name: str):
14+
self.machine = Machine(machine_name)
15+
self.machines.append(self.machine)
16+
return self.machine
1717

18-
def ExecuteNextCommand(self): # Some patterns could use this
19-
next_command = CommandQueue().GetNextCommand()
18+
def execute_next(self): # Some patterns could use this
19+
next_command = CommandQueue().get_next()
2020
if next_command is not None:
21-
self.ExecuteCommand(next_command)
21+
self.execute_command(next_command)
2222
return True
2323
return False
2424

25-
def ExecuteAllCommands(self): # Should be used as default
25+
def execute_all(self): # Should be used as default
2626
while len(CommandQueue()) > 0:
27-
self.ExecuteNextCommand()
28-
29-
def ExecuteCommand(self, command: str): # Not recommended if states can execute commands
30-
automata_bpp_logger.debug("Executing command [{}]".format(command))
31-
for machine in self.__list_of_machines:
32-
machine.ExecuteTransition(command)
27+
self.execute_next()
3328

34-
def _AddTransitionToCurrDefined(self, st_before: str, command: str, st_after: str):
35-
self.curr_machine_to_define.AddTransition(st_before, command, st_after)
29+
def execute_command(self, command: str): # Not recommended if states can execute commands
30+
LOGGER.debug("Executing command [{}]".format(command))
31+
for machine in self.machines:
32+
machine.transition(command)
3633

37-
def GetCurrentDefinedMachine(self):
38-
return self.curr_machine_to_define
34+
def this_machine(self):
35+
return self.machine
3936

40-
def ReturnToStart(self):
41-
for machine in self.__list_of_machines:
42-
machine.ReturnToStart()
37+
def reset_machines(self):
38+
for machine in self.machines:
39+
machine.reset_to_start()

automatabpp/state/state.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
from automatabpp.automatabpp import automata_bpp_logger
1+
from automatabpp.automatabpp import LOGGER
22
from automatabpp.commandqueue.commandqueue import CommandQueue
33

44

55
class State(object):
66

77
def __not_defined(self, **_):
8-
automata_bpp_logger.debug("Machine:State [{}:{}] Execution not defined".format(self.machine_name, self.state_name))
8+
LOGGER.debug("Machine:State [{}:{}] Execution not defined".format(self.machine_name, self.name))
99

1010
def __init__(self, state_name: str, machine_name: str):
1111
self.machine_name = machine_name
12-
self.state_name = state_name
12+
self.name = state_name
1313
self.callback = self.__not_defined
1414
self.transitions = dict()
15-
self.after_execution_commands = list()
15+
self.post_commands = list()
1616

17-
def GetName(self):
18-
return self.state_name
17+
def get_name(self):
18+
return self.name
1919

20-
def SetExecuteStateFunction(self, callback: callable):
20+
def set_callback(self, callback: callable):
2121
self.callback = callback
2222

23-
def SetTransition(self, command: str, next_state_name):
23+
def set_transition(self, command: str, next_state_name):
2424
self.transitions[command] = next_state_name
2525

26-
def AddCommandToCallAfterExecution(self, cmd: str):
26+
def add_post_command(self, cmd: str):
2727
if len(cmd) > 0:
28-
self.after_execution_commands.append(cmd)
28+
self.post_commands.append(cmd)
2929

30-
def ExecuteState(self, command):
30+
def execute_state(self, command):
3131
self.callback(command=command)
32-
for cmd in self.after_execution_commands:
33-
CommandQueue().PushCommandsToQueue(cmd)
32+
for cmd in self.post_commands:
33+
CommandQueue().push_commands(cmd)
3434

35-
def GetNextStateWithTransition(self, command):
35+
def transition_to_next(self, command):
3636
if command in self.transitions.keys():
3737
return self.transitions[command]
3838
return None

automatabpp/xml/xmlread.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import xml.etree.ElementTree as ET
2-
from automatabpp.machines.machines import Machines
2+
33
from automatabpp.commandqueue.commandqueue import CommandQueue
4+
from automatabpp.machines.machines import Machines
45

56

67
def read_graphml(graphml_path: str, machine_name: str):
@@ -40,11 +41,11 @@ def __init__(self, _edge):
4041
edges = [GraphEdge(edge) for edge in root.findall("ns0:graph/ns0:edge", read_graphml.ns)]
4142
nodes = [GraphNode(node) for node in root.findall("ns0:graph/ns0:node", read_graphml.ns)]
4243
node_map = dict()
43-
new_machine = Machines().AddNewMachine(machine_name)
44+
new_machine = Machines().add_new_machine(machine_name)
4445
for node in nodes:
4546
node_map[node.id] = node.name
4647
for transition in node.transitions_after.split(CommandQueue.SEPARATOR):
47-
new_machine.GetStateWithName(node.name).AddCommandToCallAfterExecution(transition)
48+
new_machine.get_state_by_name(node.name).add_post_command(transition)
4849
for edge in edges:
4950
for transition_name in edge.name.split():
50-
new_machine.AddTransition(node_map[edge.source], transition_name, node_map[edge.target])
51+
new_machine.add_transition(node_map[edge.source], transition_name, node_map[edge.target])

docs/FSM.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ Once we `import automatabpp` into our python script we can use the following 4 c
1212

1313
Operation is a class that only executes the global operations on the FSMs.
1414
```python
15-
OPERATION.start_fsm() # Starts the FSMs by sending the '_start_' command to all the machines
15+
OPERATION.start() # Starts the FSMs by sending the '_start_' command to all the machines
1616

17-
OPERATION.stop_fsm() # Stops all the FSMs by sending the '_stop_' command to all the machines,
17+
OPERATION.stop() # Stops all the FSMs by sending the '_stop_' command to all the machines,
1818
# reseting them to the default '_START_' state and emptying the machine commands stack.
1919

20-
OPERATION.reset_fsm() # Stops and starts the FSMs
20+
OPERATION.reset() # Stops and starts the FSMs
2121

22-
OPERATION.run_fsm() # Runs all the commands in the CommandQueue until the queue is empty.
22+
OPERATION.run() # Runs all the commands in the CommandQueue until the queue is empty.
2323

24-
OPERATION.run_fsm(cmd) # Runs only the cmd on all machines now without running the rest of the queue.
24+
OPERATION.run(cmd) # Runs only the cmd on all machines now without running the rest of the queue.
2525
```
2626

2727
### BEHAVIOUR

0 commit comments

Comments
 (0)