pyjevsim 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. pyjevsim-1.0.0/LICENSE +21 -0
  2. pyjevsim-1.0.0/MANIFEST.in +12 -0
  3. pyjevsim-1.0.0/PKG-INFO +162 -0
  4. pyjevsim-1.0.0/README.md +131 -0
  5. pyjevsim-1.0.0/pyjevsim/__init__.py +41 -0
  6. pyjevsim-1.0.0/pyjevsim/atomic_model.py +39 -0
  7. pyjevsim-1.0.0/pyjevsim/behavior_executor.py +126 -0
  8. pyjevsim-1.0.0/pyjevsim/behavior_model.py +260 -0
  9. pyjevsim-1.0.0/pyjevsim/core_model.py +103 -0
  10. pyjevsim-1.0.0/pyjevsim/default_message_catcher.py +42 -0
  11. pyjevsim-1.0.0/pyjevsim/definition.py +102 -0
  12. pyjevsim-1.0.0/pyjevsim/executor.py +27 -0
  13. pyjevsim-1.0.0/pyjevsim/executor_factory.py +77 -0
  14. pyjevsim-1.0.0/pyjevsim/exgen.py +116 -0
  15. pyjevsim-1.0.0/pyjevsim/message_deliverer.py +44 -0
  16. pyjevsim-1.0.0/pyjevsim/restore_handler.py +116 -0
  17. pyjevsim-1.0.0/pyjevsim/snapshot_condition.py +101 -0
  18. pyjevsim-1.0.0/pyjevsim/snapshot_executor.py +219 -0
  19. pyjevsim-1.0.0/pyjevsim/snapshot_factory.py +63 -0
  20. pyjevsim-1.0.0/pyjevsim/snapshot_manager.py +120 -0
  21. pyjevsim-1.0.0/pyjevsim/structural_executor.py +136 -0
  22. pyjevsim-1.0.0/pyjevsim/structural_model.py +48 -0
  23. pyjevsim-1.0.0/pyjevsim/system_executor.py +648 -0
  24. pyjevsim-1.0.0/pyjevsim/system_message.py +99 -0
  25. pyjevsim-1.0.0/pyjevsim/system_object.py +52 -0
  26. pyjevsim-1.0.0/pyjevsim/termination_manager.py +37 -0
  27. pyjevsim-1.0.0/pyjevsim.egg-info/PKG-INFO +162 -0
  28. pyjevsim-1.0.0/pyjevsim.egg-info/SOURCES.txt +31 -0
  29. pyjevsim-1.0.0/pyjevsim.egg-info/dependency_links.txt +1 -0
  30. pyjevsim-1.0.0/pyjevsim.egg-info/requires.txt +7 -0
  31. pyjevsim-1.0.0/pyjevsim.egg-info/top_level.txt +1 -0
  32. pyjevsim-1.0.0/pyproject.toml +43 -0
  33. pyjevsim-1.0.0/setup.cfg +4 -0
pyjevsim-1.0.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 eventsim
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,12 @@
1
+ include LICENSE
2
+ include README.md
3
+ include pyproject.toml
4
+ recursive-include pyjevsim *.py
5
+ recursive-exclude tests *
6
+ recursive-exclude examples *
7
+ recursive-exclude docs *
8
+ recursive-exclude utils *
9
+ recursive-exclude venv *
10
+ exclude setup.py
11
+ global-exclude *.pyc
12
+ global-exclude __pycache__
@@ -0,0 +1,162 @@
1
+ Metadata-Version: 2.1
2
+ Name: pyjevsim
3
+ Version: 1.0.0
4
+ Summary: A DEVS(Discrete Event System Specification) Modeling & Simulation environment with journaling functionality
5
+ Home-page: https://github.com/eventsim/pyevsim
6
+ Author: Changbeom Choi
7
+ Author-email: Changbeom Choi <me@cbchoi.info>
8
+ License: MIT
9
+ Project-URL: Homepage, https://github.com/eventsim/pyjevsim
10
+ Project-URL: Documentation, https://pyjevsim.readthedocs.io/en/latest/index.html
11
+ Project-URL: Repository, https://github.com/eventsim/pyjevsim
12
+ Project-URL: Issues, https://github.com/eventsim/pyjevsim/issues
13
+ Keywords: DEVS,simulation,discrete-event,modeling
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: License :: OSI Approved :: MIT License
20
+ Classifier: Operating System :: OS Independent
21
+ Classifier: Topic :: Scientific/Engineering
22
+ Classifier: Intended Audience :: Science/Research
23
+ Requires-Python: >=3.10
24
+ Description-Content-Type: text/markdown
25
+ License-File: LICENSE
26
+ Provides-Extra: snapshot
27
+ Requires-Dist: dill>=0.3.6; extra == "snapshot"
28
+ Provides-Extra: dev
29
+ Requires-Dist: pytest>=7.0; extra == "dev"
30
+ Requires-Dist: dill>=0.3.6; extra == "dev"
31
+
32
+ # pyjevsim
33
+ ## Introduction
34
+ pyjevsim is a DEVS(discrete event system specification) environment that provides journaling functionality.
35
+ It provides the ability to snapshot and restore models or simulation engines.
36
+ It's compatible with Python versions 3.10+.
37
+
38
+ For more information, see the documentation. : [pyjevsim](https://pyjevsim.readthedocs.io/en/latest/index.html)
39
+
40
+ ## Installing
41
+ You can install pyjevsim via
42
+ ```
43
+ git clone https://github.com/eventsim/pyjevsim
44
+ ```
45
+
46
+ ## Dependencies
47
+ The only dependency required by pyjevsim is dill ~= 0.3.6 for model serialization and restoration.
48
+ dill is an essential library for serializing models and simulation states and can be installed via.
49
+ ```
50
+ pip install dill
51
+ ```
52
+
53
+ ### Optional Dependencies
54
+ pytest is an optional dependency required for running test cases and example executions.
55
+ You can install pyjevsim via
56
+ ```
57
+ pip install pytest
58
+ ```
59
+
60
+ Additionally, you can install all necessary libraries, including optional dependencies, by running the following command:
61
+ ```
62
+ pip install -r requirements.txt
63
+ ```
64
+
65
+ ## Working with pyjevsim
66
+ Once you have installed the library, you can begin working with it.
67
+
68
+ ### Quick Start
69
+ The docs describe how to configure a simulation via pyjevsim's BehaviorModel and SysExecutor.
70
+ Check out the [documentation](link) to configure your simulation.
71
+
72
+ ### Example
73
+ There is a banksim example that uses pyjevsim's DEVS functionality and journaling features.
74
+ [documentation](link)
75
+
76
+ ## Execution Modes
77
+
78
+ SysExecutor supports three execution modes via `ExecutionType`:
79
+
80
+ | Mode | Description |
81
+ |------|-------------|
82
+ | `V_TIME` | Virtual time — simulation runs as fast as possible |
83
+ | `R_TIME` | Real time — simulation paces itself to wall-clock time |
84
+ | `HLA_TIME` | HLA/RTI-controlled time — time advancement is driven externally |
85
+
86
+ ```python
87
+ from pyjevsim.system_executor import SysExecutor
88
+ from pyjevsim.definition import ExecutionType
89
+
90
+ se = SysExecutor(1, ex_mode=ExecutionType.V_TIME)
91
+ ```
92
+
93
+ ## Multi-threading Support
94
+
95
+ SysExecutor provides thread-safe APIs for multi-threaded simulation environments where external threads inject events while the simulation runs.
96
+
97
+ ### Pause / Resume
98
+
99
+ Pause the simulation to allow external threads to accumulate events, then resume.
100
+
101
+ ```python
102
+ se.pause_sim() # Pauses the simulation loop
103
+ # External threads can safely call insert_external_event() while paused
104
+ se.resume_sim() # Resumes the simulation loop
105
+ ```
106
+
107
+ ### External Event Injection
108
+
109
+ Insert events from external threads into the simulation. Thread-safe via internal synchronization.
110
+
111
+ ```python
112
+ se.insert_external_event("port_name", message, scheduled_time=0)
113
+ ```
114
+
115
+ ### Output Event Callback
116
+
117
+ Register a callback to be notified when output events are generated, avoiding polling.
118
+
119
+ ```python
120
+ se.set_output_event_callback(lambda: print("output ready"))
121
+ events = se.handle_external_output_event()
122
+ ```
123
+
124
+ ## HLA/RTI Integration (HLA_TIME Mode)
125
+
126
+ For HLA/RTI-controlled simulations, use `HLA_TIME` mode with `step()` and `get_next_event_time()`.
127
+
128
+ ```python
129
+ se = SysExecutor(1, ex_mode=ExecutionType.HLA_TIME)
130
+ se.register_entity(model)
131
+ se.init_sim()
132
+
133
+ # RTI-driven loop
134
+ while not se.is_terminated():
135
+ next_time = se.get_next_event_time()
136
+ # ... request time advance from RTI, wait for grant ...
137
+ granted_time = ... # time granted by RTI
138
+ output_events = se.step(granted_time)
139
+ # ... publish output_events to RTI ...
140
+ ```
141
+
142
+ ### `step(granted_time)`
143
+ Executes one simulation step up to the granted time. Returns output events generated during the step.
144
+
145
+ ### `get_next_event_time()`
146
+ Returns the earliest scheduled event time (internal or external). Used to determine the Time Advance Request value for the RTI.
147
+
148
+ ## Graceful Termination
149
+
150
+ ```python
151
+ se.terminate_simulation() # Sets SIMULATION_TERMINATED state
152
+ se.is_terminated() # Returns True if terminated
153
+ ```
154
+
155
+ Signal handlers (SIGTERM, SIGINT) automatically invoke `terminate_simulation()` on all registered SysExecutor instances.
156
+
157
+ ## License
158
+ Author: Changbeom Choi (@cbchoi)
159
+ Copyright (c) 2014-2020 Handong Global University
160
+ Copyright (c) 2021-2024 Hanbat National University
161
+ License: MIT. The full license text is available at:
162
+ - https://github.com/eventsim/pyjevsim/blob/main/LICENSE
@@ -0,0 +1,131 @@
1
+ # pyjevsim
2
+ ## Introduction
3
+ pyjevsim is a DEVS(discrete event system specification) environment that provides journaling functionality.
4
+ It provides the ability to snapshot and restore models or simulation engines.
5
+ It's compatible with Python versions 3.10+.
6
+
7
+ For more information, see the documentation. : [pyjevsim](https://pyjevsim.readthedocs.io/en/latest/index.html)
8
+
9
+ ## Installing
10
+ You can install pyjevsim via
11
+ ```
12
+ git clone https://github.com/eventsim/pyjevsim
13
+ ```
14
+
15
+ ## Dependencies
16
+ The only dependency required by pyjevsim is dill ~= 0.3.6 for model serialization and restoration.
17
+ dill is an essential library for serializing models and simulation states and can be installed via.
18
+ ```
19
+ pip install dill
20
+ ```
21
+
22
+ ### Optional Dependencies
23
+ pytest is an optional dependency required for running test cases and example executions.
24
+ You can install pyjevsim via
25
+ ```
26
+ pip install pytest
27
+ ```
28
+
29
+ Additionally, you can install all necessary libraries, including optional dependencies, by running the following command:
30
+ ```
31
+ pip install -r requirements.txt
32
+ ```
33
+
34
+ ## Working with pyjevsim
35
+ Once you have installed the library, you can begin working with it.
36
+
37
+ ### Quick Start
38
+ The docs describe how to configure a simulation via pyjevsim's BehaviorModel and SysExecutor.
39
+ Check out the [documentation](link) to configure your simulation.
40
+
41
+ ### Example
42
+ There is a banksim example that uses pyjevsim's DEVS functionality and journaling features.
43
+ [documentation](link)
44
+
45
+ ## Execution Modes
46
+
47
+ SysExecutor supports three execution modes via `ExecutionType`:
48
+
49
+ | Mode | Description |
50
+ |------|-------------|
51
+ | `V_TIME` | Virtual time — simulation runs as fast as possible |
52
+ | `R_TIME` | Real time — simulation paces itself to wall-clock time |
53
+ | `HLA_TIME` | HLA/RTI-controlled time — time advancement is driven externally |
54
+
55
+ ```python
56
+ from pyjevsim.system_executor import SysExecutor
57
+ from pyjevsim.definition import ExecutionType
58
+
59
+ se = SysExecutor(1, ex_mode=ExecutionType.V_TIME)
60
+ ```
61
+
62
+ ## Multi-threading Support
63
+
64
+ SysExecutor provides thread-safe APIs for multi-threaded simulation environments where external threads inject events while the simulation runs.
65
+
66
+ ### Pause / Resume
67
+
68
+ Pause the simulation to allow external threads to accumulate events, then resume.
69
+
70
+ ```python
71
+ se.pause_sim() # Pauses the simulation loop
72
+ # External threads can safely call insert_external_event() while paused
73
+ se.resume_sim() # Resumes the simulation loop
74
+ ```
75
+
76
+ ### External Event Injection
77
+
78
+ Insert events from external threads into the simulation. Thread-safe via internal synchronization.
79
+
80
+ ```python
81
+ se.insert_external_event("port_name", message, scheduled_time=0)
82
+ ```
83
+
84
+ ### Output Event Callback
85
+
86
+ Register a callback to be notified when output events are generated, avoiding polling.
87
+
88
+ ```python
89
+ se.set_output_event_callback(lambda: print("output ready"))
90
+ events = se.handle_external_output_event()
91
+ ```
92
+
93
+ ## HLA/RTI Integration (HLA_TIME Mode)
94
+
95
+ For HLA/RTI-controlled simulations, use `HLA_TIME` mode with `step()` and `get_next_event_time()`.
96
+
97
+ ```python
98
+ se = SysExecutor(1, ex_mode=ExecutionType.HLA_TIME)
99
+ se.register_entity(model)
100
+ se.init_sim()
101
+
102
+ # RTI-driven loop
103
+ while not se.is_terminated():
104
+ next_time = se.get_next_event_time()
105
+ # ... request time advance from RTI, wait for grant ...
106
+ granted_time = ... # time granted by RTI
107
+ output_events = se.step(granted_time)
108
+ # ... publish output_events to RTI ...
109
+ ```
110
+
111
+ ### `step(granted_time)`
112
+ Executes one simulation step up to the granted time. Returns output events generated during the step.
113
+
114
+ ### `get_next_event_time()`
115
+ Returns the earliest scheduled event time (internal or external). Used to determine the Time Advance Request value for the RTI.
116
+
117
+ ## Graceful Termination
118
+
119
+ ```python
120
+ se.terminate_simulation() # Sets SIMULATION_TERMINATED state
121
+ se.is_terminated() # Returns True if terminated
122
+ ```
123
+
124
+ Signal handlers (SIGTERM, SIGINT) automatically invoke `terminate_simulation()` on all registered SysExecutor instances.
125
+
126
+ ## License
127
+ Author: Changbeom Choi (@cbchoi)
128
+ Copyright (c) 2014-2020 Handong Global University
129
+ Copyright (c) 2021-2024 Hanbat National University
130
+ License: MIT. The full license text is available at:
131
+ - https://github.com/eventsim/pyjevsim/blob/main/LICENSE
@@ -0,0 +1,41 @@
1
+ """
2
+ Author: Changbeom Choi (@cbchoi)
3
+ Copyright (c) 2014-2020 Handong Global University
4
+ Copyright (c) 2014-2020 Hanbat National University
5
+ License: MIT. The full license text is available at:
6
+ - https://github.com/eventsim/pyjevsim/blob/main/LICENSE
7
+ """
8
+ __author__ = "me@cbchoi.info"
9
+ __all__ = [
10
+ "behavior_model",
11
+ "behavior_executor",
12
+ "core_model",
13
+ "default_message_catcher",
14
+ "definition",
15
+ "executor_factory",
16
+ "executor",
17
+ "structural_model",
18
+ "structural_executor",
19
+ "executor_factory",
20
+ "system_executor",
21
+ "system_message",
22
+ "system_object",
23
+ "termination_manager",
24
+ "snapshot_manager"
25
+ ]
26
+
27
+ from .definition import (
28
+ AttributeType,
29
+ Infinite,
30
+ ModelType,
31
+ ExecutionType,
32
+ SimulationMode,
33
+ SingletonType,
34
+ )
35
+
36
+ from .system_executor import SysExecutor
37
+ from .system_message import SysMessage
38
+ from .behavior_model import BehaviorModel
39
+ from .structural_model import StructuralModel
40
+ from .snapshot_manager import SnapshotManager
41
+ from .restore_handler import RestoreHandler
@@ -0,0 +1,39 @@
1
+ """
2
+ Author: Changbeom Choi (@cbchoi)
3
+ Copyright (c) 2014-2020 Handong Global University
4
+ Copyright (c) 2021-2024 Hanbat National University
5
+ License: MIT. The full license text is available at:
6
+ https://github.com/eventsim/pyjevsim/blob/main/LICENSE
7
+
8
+ This module contains a BehaivorModel object that allows you to implement the Discrete Event System Specification AtomicModel.
9
+ """
10
+
11
+ from abc import abstractmethod
12
+ from collections import OrderedDict
13
+
14
+ from .behavior_model import BehaviorModel
15
+ from .definition import ModelType
16
+
17
+ class AtomicModel(BehaviorModel):
18
+ def __init__(self, _name=""):
19
+ super().__init__(_name)
20
+
21
+ @abstractmethod
22
+ def ext_trans(self, port, msg):
23
+ """Defines the external transition, to be implemented by subclasses"""
24
+ pass
25
+
26
+ @abstractmethod
27
+ def int_trans(self):
28
+ """Defines the internal transition, to be implemented by subclasses"""
29
+ pass
30
+
31
+ @abstractmethod
32
+ def output(self, msg_deliver):
33
+ """Defines the output function, to be implemented by subclasses"""
34
+ pass
35
+
36
+ @abstractmethod
37
+ def time_advance(self):
38
+ """Defines the output function, to be implemented by subclasses"""
39
+ return -1
@@ -0,0 +1,126 @@
1
+ """
2
+ Author: Changbeom Choi (@cbchoi)
3
+ Copyright (c) 2014-2020 Handong Global University
4
+ Copyright (c) 2021-2024 Hanbat National University
5
+ License: MIT. The full license text is available at:
6
+ https://github.com/eventsim/pyjevsim/blob/main/LICENSE
7
+
8
+ This module contains a BehaviorExecutor, an object for executing a BehaviorModel.
9
+ """
10
+
11
+ from .definition import Infinite
12
+ from .executor import Executor
13
+
14
+ class BehaviorExecutor(Executor):
15
+ """
16
+ A decorated form of the BehaviorModel, ready to be executed by the SysExecutor.
17
+ Manages the simulation time of the BehaviorModel and the information in the SysExecutor.
18
+
19
+ Args:
20
+ itime (int or Infinite): Time of instance creation
21
+ dtime (int or Infinite): Time of instance destruction
22
+ ename (str): SysExecutor name
23
+ behavior_model (ModelType.BEHAVIORAL): Behavior Model
24
+ """
25
+ def __init__(self, itime=Infinite, dtime=Infinite, ename="default", behavior_model=None, parent=None):
26
+ super().__init__(itime, dtime, ename, behavior_model, parent)
27
+
28
+ self._next_event_t = 0 # Next event time
29
+ self._cur_state = "" # Current state of the behavior executor
30
+ self.request_time = float("inf") # Request time initialized to infinity
31
+
32
+ self.behavior_model = behavior_model #Behavior Model
33
+ self._cancel_reschedule_f = False #cancel reschedule flag
34
+
35
+ def set_global_time(self, gtime):
36
+ """Sets the global time for the executor and behavior model"""
37
+ self.global_time = gtime
38
+ self.behavior_model.set_global_time(gtime)
39
+
40
+ def get_core_model(self):
41
+ """Returns the core behavior model"""
42
+ return self.behavior_model
43
+
44
+ def __str__(self):
45
+ """Returns a string representation of the executor"""
46
+ return f"[N]:{self.get_name()}, [S]:{self._cur_state}"
47
+
48
+ def get_name(self):
49
+ """Returns the name of the behavior model"""
50
+ return self.behavior_model.get_name()
51
+
52
+ def get_engine_name(self):
53
+ """Returns the name of the engine"""
54
+ return self.engine_name
55
+
56
+ def set_engine_name(self, engine_name):
57
+ """Sets the name of the engine"""
58
+ self.engine_name = engine_name
59
+
60
+ def get_create_time(self):
61
+ """Returns the instance creation time"""
62
+ return self._instance_t
63
+
64
+ def get_destruct_time(self):
65
+ """Returns the destruction time"""
66
+ return self._destruct_t
67
+
68
+ def get_obj_id(self):
69
+ """Returns the object ID of the behavior model"""
70
+ return self.behavior_model.get_obj_id()
71
+
72
+ # State management
73
+ def get_cur_state(self):
74
+ """Returns the current state of the executor"""
75
+ return self._cur_state
76
+
77
+ def init_state(self, state):
78
+ """Initializes the state of the executor"""
79
+ self._cur_state = state
80
+
81
+ # External Transition
82
+ def ext_trans(self, port, msg):
83
+ """Handles external transition based on port and message"""
84
+ if self.behavior_model.get_cancel_flag():
85
+ self._cancel_reschedule_f = True
86
+
87
+ self.behavior_model.ext_trans(port, msg)
88
+
89
+ # Internal Transition
90
+ def int_trans(self):
91
+ """Handles internal transition"""
92
+ self.behavior_model.int_trans()
93
+
94
+ # Output Function
95
+ def output(self, msg_deliver):
96
+ """Executes the output function of the behavior model"""
97
+ return self.behavior_model.output(msg_deliver)
98
+
99
+ # Time Advance Function
100
+ def time_advance(self):
101
+ """Returns the time advance value for the current state"""
102
+ if self.behavior_model._cur_state in self.behavior_model._states:
103
+ return self.behavior_model._states[self.behavior_model._cur_state]
104
+
105
+ return -1
106
+
107
+ def set_req_time(self, global_time):
108
+ """Sets the request time based on the global time and time advance"""
109
+ self.set_global_time(global_time)
110
+ if self.time_advance() == Infinite:
111
+ self._next_event_t = Infinite
112
+ self.request_time = Infinite
113
+ else:
114
+ if self._cancel_reschedule_f:
115
+ self.request_time = min(self._next_event_t, global_time + self.time_advance())
116
+ else:
117
+ self.request_time = global_time + self.time_advance()
118
+
119
+ def get_req_time(self):
120
+ """Returns the request time and resets the cancel flag if necessary"""
121
+ if self._cancel_reschedule_f:
122
+ self._cancel_reschedule_f = False
123
+ self.behavior_model.reset_cancel_flag()
124
+ self._next_event_t = self.request_time
125
+ return self.request_time
126
+