-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ad49b9
commit b9055be
Showing
9 changed files
with
237 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters