Skip to content

Commit

Permalink
GPT example doc
Browse files Browse the repository at this point in the history
  • Loading branch information
OscarFdezS committed Jun 26, 2024
1 parent 7ad49b9 commit b9055be
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 54 deletions.
12 changes: 6 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ tcp = "xdevs.plugins.output_handlers.tcp:TCPOutputHandler"
mqtt = "xdevs.plugins.output_handlers.mqtt:MQTTOutputHandler"

[project.entry-points."xdevs.components"]
generator = "xdevs.examples.gpt.gpt:Generator"
transducer = "xdevs.examples.gpt.gpt:Transducer"
processor = "xdevs.examples.gpt.gpt:Processor"
gpt = "xdevs.examples.gpt.gpt:Gpt"
ef = "xdevs.examples.gpt.efp:Ef"
efp = "xdevs.examples.gpt.efp:Efp"
generator = "xdevs.examples.gpt.models:Generator"
transducer = "xdevs.examples.gpt.models:Transducer"
processor = "xdevs.examples.gpt.models:Processor"
gpt = "xdevs.examples.gpt.models:Gpt"
ef = "xdevs.examples.gpt.models:Ef"
efp = "xdevs.examples.gpt.models:Efp"

[project.entry-points."xdevs.wrappers"]
pypdevs = "xdevs.plugins.wrappers.pypdevs:PyPDEVSWrapper"
Expand Down
93 changes: 93 additions & 0 deletions xdevs/examples/gpt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# GPT examples

This folder stores several examples in order to illustrate some of the possibilities offered by the repository.


## `xDEVS.py` Virtual Simulation

A virtual simulation is carried out only taking into account the virtual environment.

### Example:


```bash
$ cd xdevs/examples/gpt
$ python3 gpt_v_sim.py
```

## `xDEVS` Real-Time Simulation

This section aims to show a collection of examples based on the methodology followed in this repository for achieving the wall-clock behaviour.

### Examples:

1. #### No handlers real-time simulation

```bash
$ cd xdevs/examples/gpt
$ python3 gpt_rt_sim.py
```
2. #### Input handler real-time simulation

```bash
$ cd xdevs/examples/gpt
$ python3 gpt_rt_ih_sim.py
```
However, in order to be able to completely understand the system you must execute a TCP client to send the input events to the model.
The following code, show a basic example of a TCP client that sends an input event to the model.
It is important to keep in mind that the expect message of the TCP Input handler is `Port_name,message` as it is specified in `xdevs.plugins.input_handlers.tcp`
```bash
import socket

HOST = 'LocalHost'
PORT = 4321

c = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # AF_INET: IPv4, SOCK_STREAM: TCP

c.connect((HOST,PORT))

c.sendall('ih_in,TCP'.encode()) # The data is 'port_name,msg'
```

3. #### Output handler real-time simulation

```bash
$ cd xdevs/examples/gpt
$ python3 gpt_rt_oh_sim.py
```
In order to capture the outgoing events of the model, you must execute a TCP server to receive the output events.
A basic TCP server is shown below:
```bash
import socket

HOST = 'LocalHost'
PORT = 4321

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # AF_INET: IPv4, SOCK_STREAM: TCP

s.bind((HOST,PORT))
s.listen()

s_con, add = s.accept()
print(f'Connected to: {add}')
while True:
try:
data = s_con.recv(1024) # 1024 is the buffer size
data = data.decode()
if not data:
print(f'Client disconnected: {add}')
break
print(f'data received is: {data}')
except ConnectionResetError:
print(f'Client closed unexpectedly: {add}')
break
````

4. #### Input and Output handlers real-time simulation

````bash
$ cd xdevs/examples/gpt
$ python3 gpt_rt_ih_oh_sim.py
````
If you want to test the system properly, you will have to execute the server TCP first, then run the example and later
execute the client TCP to send the input events to the model.
44 changes: 3 additions & 41 deletions xdevs/examples/gpt/efp.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,9 @@
from xdevs.examples.gpt.gpt import Generator, Transducer, Job, Processor
from xdevs.models import Coupled, Port


class Ef(Coupled):
def __init__(self, name: str, gen_t: float, obs_t: float):
super().__init__(name)

gen = Generator('generator', gen_t)
trans = Transducer('transducer', obs_t)

self.add_component(gen)
self.add_component(trans)

self.p_in_ef = Port(Job, name='p_in_ef')
self.p_out_ef = Port(Job, name='p_out_ef')

self.add_in_port(self.p_in_ef)
self.add_out_port(self.p_out_ef)

self.add_coupling(gen.o_job, trans.i_arrived)
self.add_coupling(gen.o_job, self.p_out_ef)
self.add_coupling(trans.o_out, gen.i_stop)
self.add_coupling(self.p_in_ef, trans.i_solved)


class Efp(Coupled):
def __init__(self, name: str, gen_t: float, proc_t: float, obs_t: float):
super().__init__(name)

ef = Ef('ef', gen_t, obs_t)
proc = Processor('processor', proc_t)

self.add_component(ef)
self.add_component(proc)

self.add_coupling(ef.p_out_ef, proc.i_in)
self.add_coupling(proc.o_out, ef.p_in_ef)

from xdevs.sim import Coordinator
from xdevs.examples.gpt.models import Efp

if __name__ == '__main__':
from xdevs.sim import Coordinator

efp = Efp('efp', 3, 5, 100)
coord = Coordinator(efp)
coord.initialize()
coord.simulate_iters()
coord.simulate()
18 changes: 18 additions & 0 deletions xdevs/examples/gpt/gpt_rt_ih_oh_sim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from xdevs.rt import RealTimeCoordinator, RealTimeManager
from xdevs.examples.gpt.models import GptIHOH, Job

if __name__ == '__main__':
gpt = GptIHOH("gpt", 20, 1, 100)
manager = RealTimeManager(max_jitter=0.02,time_scale=1,event_window=.05)

# The InputHandler under study will be a TCP one
# msg_parser: How must the arrived messages adapt to the system, in this case they are converted into Jobs named
# after the receiving message
msg_parser = {"ih_in" : lambda x : Job(str(x))}
# We pass the identifier and the required arguments for the TCP handler
manager.add_input_handler("tcp", port=4321, msg_parsers=msg_parser)

manager.add_output_handler('tcp', port=1234)

coord = RealTimeCoordinator(gpt, manager)
coord.simulate_rt()
16 changes: 16 additions & 0 deletions xdevs/examples/gpt/gpt_rt_ih_sim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from xdevs.rt import RealTimeCoordinator, RealTimeManager
from xdevs.examples.gpt.models import GptIHOH, Job

if __name__ == '__main__':
gpt = GptIHOH("gpt", 20, 1, 100)
manager = RealTimeManager(max_jitter=0.02,time_scale=1,event_window=.05)

# The InputHandler under study will be a TCP one
# msg_parser: How must the arrived messages adapt to the system, in this case they are converted into Jobs named
# after the receiving message
msg_parser = {"ih_in" : lambda x : Job(str(x))}
# We pass the identifier and the required arguments for the TCP handler
manager.add_input_handler("tcp", port=4321, msg_parsers=msg_parser)

coord = RealTimeCoordinator(gpt, manager)
coord.simulate_rt()
12 changes: 12 additions & 0 deletions xdevs/examples/gpt/gpt_rt_oh_sim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from xdevs.rt import RealTimeCoordinator, RealTimeManager
from xdevs.examples.gpt.models import GptIHOH, Job

if __name__ == '__main__':
gpt = GptIHOH("gpt", 5, 3, 100)
manager = RealTimeManager(max_jitter=0.02,time_scale=1,event_window=.05)

manager.add_output_handler('tcp',port=4321)

coord = RealTimeCoordinator(gpt, manager)
coord.simulate_rt()

8 changes: 8 additions & 0 deletions xdevs/examples/gpt/gpt_rt_sim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from xdevs.rt import RealTimeCoordinator, RealTimeManager
from xdevs.examples.gpt.models import Gpt

if __name__ == '__main__':
gpt = Gpt("gpt", 2, 7, 100)
manager = RealTimeManager(0.02,1,0.05)
coordinator = RealTimeCoordinator(gpt, manager)
coordinator.simulate_rt(100)
10 changes: 10 additions & 0 deletions xdevs/examples/gpt/gpt_v_sim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from xdevs.sim import Coordinator
from xdevs.examples.gpt.models import Gpt

if __name__ == '__main__':

gpt = Gpt("gpt", 3, 5, 100)
coord = Coordinator(gpt)
coord.initialize()
coord.simulate()

78 changes: 71 additions & 7 deletions xdevs/examples/gpt/gpt.py → xdevs/examples/gpt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def __init__(self, name: str):
"""
self.name: str = name
self.time: float = 0

def __str__(self):
return self.name

class Generator(Atomic):
def __init__(self, name: str, gen_t: float):
Expand Down Expand Up @@ -171,7 +172,40 @@ def lambdaf(self):
if self.phase == PHASE_DONE:
self.o_out.add(True)

class Ef(Coupled):
def __init__(self, name: str, gen_t: float, obs_t: float):
super().__init__(name)

gen = Generator('generator', gen_t)
trans = Transducer('transducer', obs_t)

self.add_component(gen)
self.add_component(trans)

self.p_in_ef = Port(Job, name='p_in_ef')
self.p_out_ef = Port(Job, name='p_out_ef')

self.add_in_port(self.p_in_ef)
self.add_out_port(self.p_out_ef)

self.add_coupling(gen.o_job, trans.i_arrived)
self.add_coupling(gen.o_job, self.p_out_ef)
self.add_coupling(trans.o_out, gen.i_stop)
self.add_coupling(self.p_in_ef, trans.i_solved)


class Efp(Coupled):
def __init__(self, name: str, gen_t: float, proc_t: float, obs_t: float):
super().__init__(name)

ef = Ef('ef', gen_t, obs_t)
proc = Processor('processor', proc_t)

self.add_component(ef)
self.add_component(proc)

self.add_coupling(ef.p_out_ef, proc.i_in)
self.add_coupling(proc.o_out, ef.p_in_ef)
class Gpt(Coupled):
def __init__(self, name: str, gen_t: float, proc_t: float, obs_t: float):
super().__init__(name)
Expand All @@ -189,11 +223,41 @@ def __init__(self, name: str, gen_t: float, proc_t: float, obs_t: float):
self.add_coupling(proc.o_out, trans.i_solved)
self.add_coupling(trans.o_out, gen.i_stop)

class GptIHOH(Coupled):

# Adaptation of the GPT DEVS model for injecting events via a new input port and for ejection of events via a new
# output port.

def __init__(self, name: str, gen_t: float, proc_t: float, obs_t: float):
super().__init__(name)

gen = Generator('generator', gen_t)
proc = Processor('processor', proc_t)
trans = Transducer('transducer', obs_t)

# New input handler port
self.ih_in = Port(Job, name='ih_in')
self.add_in_port(self.ih_in)

# New output handler port
self.oh_out = Port(Job, name='ih_in')
self.add_out_port(self.oh_out)

self.add_component(gen)
self.add_component(proc)
self.add_component(trans)

# New coupling for the input handler
self.add_coupling(self.ih_in, proc.i_in)

# New coupling for the output handler
self.add_coupling(proc.o_out, self.oh_out)

self.add_coupling(gen.o_job, proc.i_in)
self.add_coupling(gen.o_job, trans.i_arrived)
self.add_coupling(proc.o_out, trans.i_solved)
self.add_coupling(trans.o_out, gen.i_stop)



if __name__ == '__main__':
from xdevs.sim import Coordinator

gpt = Gpt("gpt", 3, 5, 100)
coord = Coordinator(gpt)
coord.initialize()
coord.simulate_time()

0 comments on commit b9055be

Please sign in to comment.