Skip to content

Commit 5afc163

Browse files
committed
add comparisons to automatabpp
Decided to add comparisons within the module itself
1 parent 94d2aaa commit 5afc163

File tree

11 files changed

+41
-21
lines changed

11 files changed

+41
-21
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ I also recommend using **Python 3.4+** although I haven't even tried it yet on t
8888
### Directories
8989

9090
* **./automatabpp** - our module source code. This directory contains the python code for this project.
91-
* **./comparisons** - a small module which offers some lambda comparison functions you can use with this project.
9291
* **./graphs** - yEd graphs are stored in this directory by default
9392
* **./README** - I've really took my time to write all of this so I would appreciate you checking it out
9493

README/FSM.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ I won't cover too much how the code is written but rather how to use it.
44

55
Once we `import automatabpp` into our python script we can use the following 4 classes.
66

7-
| [OPERATION](#operation) | [BEHAVIOUR](#behaviour) | [EXECUTION](#execution) | [INTERFACE](#interface) |
8-
| --- | --- | --- | --- |
7+
| [OPERATION](#operation) | [BEHAVIOUR](#behaviour) | [EXECUTION](#execution) | [INTERFACE](#interface) | [COMPARISONS](#comparisons) |
8+
| --- | --- | --- | --- | --- |
99

1010

1111
### OPERATION
@@ -45,6 +45,21 @@ INTERFACE.run_command_if_lambda_on_result_true(lambda_function, command)
4545
# When the function decorated with this function is called, the command will be run if the lambda on result is True
4646
```
4747

48+
### COMPARISONS
49+
A helper class with lots of comparison `lambda` type functions. Can only return `True` or `False` .
50+
```python
51+
COMPARISONS.less_than(num)
52+
COMPARISONS.between(num1, num2)
53+
COMPARISONS.more_than(num)
54+
COMPARISONS.equal(num)
55+
COMPARISONS.not_equal(num)
56+
COMPARISONS.contains(string)
57+
COMPARISONS.one_of(items)
58+
COMPARISONS.none_of(items)
59+
COMPARISONS.always_true()
60+
COMPARISONS.on_result_true()
61+
COMPARISONS.on_result_false()
62+
```
4863

4964
| [Back to Main][prev] | ----- | [Tutorial][next] |
5065
| --- | --- | --- |

README/examples/example4.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ _embedded/fan.graphml_
5656

5757
<img src="../images/examples/example4/machine_fan.png" width="640" height="140" />
5858

59-
These graphs will only serve as an endpoint for our execution functions. One thing to notice is that you can only turn on the device once. There is no possibility of turning the device again while it is already turned on.
59+
These graphs will only serve as an endpoint for our execution functions. One thing to notice is that you can only turn on the device once. There is no possibility of turning the device on again while it is already in that state.
6060
<br>Since the default state for LED light is to always be on we will set it to be on while starting.
6161

6262
Now let's draw the behaviour of our components.
@@ -87,7 +87,6 @@ You can check the code in the [example4.py][pycode] script.
8787
- First let's take care of importing everything we will need:
8888
```python
8989
from automatabpp import *
90-
from comparisons import COMPARISONS # a helper class for comparison lambda functions
9190
import math, time # we'll use this to simulate our readings
9291
```
9392

@@ -166,9 +165,15 @@ You can check the code in the [example4.py][pycode] script.
166165
temp = round(simulate_temperature_reading(i), 2) # simulate reading the temperature sensor
167166
print("\t\t|\t{}V\t|\t{}°C".format(volt, temp))
168167
OPERATION.run_fsm() # run commands left in the CommandQueue
169-
time.sleep(1)
168+
time.sleep(2)
170169
```
171170

171+
Run the script and check how our simulated device behaves. Is the behaviour the same as in the requirements?
172+
173+
```console
174+
user@computer:~$ python example4.py
175+
```
176+
172177
The rest of the code doesn't ever need to care about the behaviour of our components. The only thing that we need to do is read the sensor from time to time and run the commands left in the CommandQueue and the automatabpp will take care of everything else.
173178
<br>Our code has just gotten a lot simpler after this and we can take care of whichever other things we want to do in the application.
174179

README/other.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ Just keeping a list of things that I could add to the module.
1111
* add encryption support
1212
* add compression support
1313
* add more graph file extensions support
14+
* add more comparisons
1415

1516
[Back to Main](../README.md)

automatabpp/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
__all__ = ["logger", "BEHAVIOUR", "EXECUTION", "OPERATION", "INTERFACE"]
1+
__all__ = ["BEHAVIOUR", "EXECUTION", "OPERATION", "INTERFACE", "COMPARISONS"]
22

33
import logging
4-
logger = logging.getLogger(__name__)
4+
automata_bpp_logger = logging.getLogger(__name__)
55

66
from . machines.machines import Machines
77
from . xml.xmlread import read_graphml
88
from . commandqueue.commandqueue import CommandQueue
99
from . constants import GRAPHS_DIRECTORY, START_COMMAND_NAME, STOP_COMMAND_NAME
10+
from . comparisons.comparisons import COMPARISONS
1011

1112
from functools import wraps
1213

@@ -25,7 +26,7 @@ def load_behaviour_from_graph(graph_file_path: str, machine_name: str):
2526
if graph_file_path[-8:] == ".graphml":
2627
read_graphml("{}/{}".format(GRAPHS_DIRECTORY, graph_file_path), machine_name)
2728
else:
28-
logger.warning("Unknown format for reading the graph file: {}".format(graph_file_path))
29+
automata_bpp_logger.warning("Unknown format for reading the graph file: {}".format(graph_file_path))
2930

3031

3132
class EXECUTION:

automatabpp/commandqueue/commandqueue.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from automatabpp import logger
1+
from automatabpp import automata_bpp_logger
22
from automatabpp.metaclasses.singleton import Singleton
33

44

@@ -19,7 +19,7 @@ def GetNextCommand(self):
1919
def PushCommandsToQueue(self, commands: str):
2020
for cmd in commands.split(CommandQueue.SEPARATOR):
2121
if len(cmd) > 0:
22-
logger.debug("++++++> {} pushed to CommandQueue <++++++".format(cmd))
22+
automata_bpp_logger.debug("++++++> {} pushed to CommandQueue <++++++".format(cmd))
2323
self.__list_of_cmds_to_execute.append(cmd)
2424

2525
def EmptyAllCommands(self):

comparisons/__init__.py renamed to automatabpp/comparisons/comparisons.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ def contains(string: str):
2525
return lambda x: x.count(string) > 0
2626

2727
@staticmethod
28-
def one_of(items: str):
28+
def one_of(items):
2929
return lambda x: x in items
3030

3131
@staticmethod
32-
def none_of(items: str):
32+
def none_of(items):
3333
return lambda x: x not in items
3434

3535
@staticmethod

automatabpp/machine/machine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from automatabpp import logger
1+
from automatabpp import automata_bpp_logger
22
from automatabpp.state.state import State
33
from automatabpp.constants import START_STATE_NAME
44

@@ -28,7 +28,7 @@ def SetExecuteStateFunction(self, state_name: str, callback: callable):
2828
def ExecuteTransition(self, command: str):
2929
st_after_name = self.curr_state.GetNextStateWithTransition(command)
3030
if st_after_name is not None:
31-
logger.debug("{}: state change {}->{}".format(self.machine_name, self.curr_state.GetName(), st_after_name))
31+
automata_bpp_logger.debug("{}: state change {}->{}".format(self.machine_name, self.curr_state.GetName(), st_after_name))
3232
self.curr_state = self.GetStateWithName(st_after_name)
3333
self.curr_state.ExecuteState(command)
3434
return True

automatabpp/machines/machines.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from automatabpp import logger
1+
from automatabpp import automata_bpp_logger
22
from automatabpp.metaclasses.singleton import Singleton
33
from automatabpp.machine.machine import Machine
44
from automatabpp.commandqueue.commandqueue import CommandQueue
@@ -27,7 +27,7 @@ def ExecuteAllCommands(self): # Should be used as default
2727
self.ExecuteNextCommand()
2828

2929
def ExecuteCommand(self, command: str): # Not recommended if states can execute commands
30-
logger.debug("Executing command [{}]".format(command))
30+
automata_bpp_logger.debug("Executing command [{}]".format(command))
3131
for machine in self.__list_of_machines:
3232
machine.ExecuteTransition(command)
3333

automatabpp/state/state.py

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

44

55
class State(object):
66

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

1010
def __init__(self, state_name: str, machine_name: str):
1111
self.machine_name = machine_name

0 commit comments

Comments
 (0)