|
| 1 | +**External Transition (`extTransition`):** |
| 2 | + - Executed when the model receives a message via its input ports. Only applicable to models that handle incoming data, such as the default model. |
| 3 | + - Use `self.peek(self.IPorts[0], *args) to retrieve a message from a port. Change the index to peek at a specific port. |
| 4 | + *Example : retrieve a message for every ports :* |
| 5 | + for port in self.IPorts: |
| 6 | + msg = self.peek(port, *args) |
| 7 | + |
| 8 | + - `Message` is a class with the following variables : `value` which is the content of the message, and `time` representing the simulation time it was sent. |
| 9 | + - Based on the message content, the model can take actions such as changing state, executing specific logic, or remaining in the current state. |
| 10 | + - Use `self.holdIn()` to set the new state and delay until the next transition as needed |
| 11 | + - the line **`return self.getState()`** is required |
| 12 | + |
| 13 | + |
| 14 | +**Handle and change state: ** |
| 15 | + - Use `self.getState()` to retrieve the model’s current state at any point in the code. This can help conditionally control actions based on the model’s state. |
| 16 | + - Use `self.phaseIs()` to test if the model's current state name is equal to the provided string. Example : `if self.phaseIs("State_Name"):`. |
| 17 | + - Use `self.getStatus()` to get the model's current state name. Use this if only the current state name is needed. |
| 18 | + |
| 19 | +**Example: the model has at least one input port** |
| 20 | +def extTransition(self, *args): |
| 21 | + ''' DEVS external transition function (only for models that receive data) ''' |
| 22 | + for port in self.IPorts: |
| 23 | + msg = self.peek(port, *args) |
| 24 | + if msg: |
| 25 | + # State change or action logic based on the message |
| 26 | + current_state = self.getState() # Example usage of getState() |
| 27 | + # Customize based on intended behavior |
| 28 | + |
| 29 | + return self.getState() |
| 30 | + |
| 31 | + |
| 32 | +**Example: The model doesn't have any input ports** |
| 33 | +def extTransition(self, *args): |
| 34 | + ''' DEVS external transition function (only for models that receive data) ''' |
| 35 | + pass |
| 36 | + |
| 37 | + |
| 38 | +## Steps |
| 39 | +1. Use the number of input ports to identify if the function is necessary |
| 40 | +2. Use the 'parameters' field to add the eventually needed parameters. |
| 41 | +3. Use the 'description' and 'input_ports' fields to infer if you need to peek at every ports or only a specific one. |
| 42 | +4. Use the information in the 'states' array when using `self.holdIn()`. If the 'time' field is a string, assume that a class variable has been created. For example : `self.holdIn("state",self.variable_name)`. |
| 43 | +*Exception:* if the time is marked as 'infinity', use it directly, as a constant, without the quotes. |
| 44 | +Example : |
| 45 | +"states": [ |
| 46 | + { "name": "IDLE", "time": "INFINITY"}, |
| 47 | + { "name": "SEND", "time": "duration"}, |
| 48 | + { "name": "GENERATE", "time": 0} |
| 49 | + ], |
| 50 | +Where : |
| 51 | +- INFINITY represents the DEVSimPy constant |
| 52 | +- period, or any other string, represents a class variable |
| 53 | +- 0, or any other number, directly represent the state duration |
| 54 | +is used : |
| 55 | +- `self.holdIn("IDLE",INFINITY)` |
| 56 | +- `self.holdIn("SEND",self.duration)` |
| 57 | +- `self.holdIn("GENERATE",0)` |
| 58 | +5. Except told otherwise, there is no need to check the state before changing it. |
| 59 | +6. Only use the json's data value, the keys and structure must not appear in the code. |
| 60 | +7. Assume that every variable in 'model_properties' has been initialized and can be used directly, with the provided type and value. |
| 61 | +8. the function self.peek() should always be present if the function is needed. |
0 commit comments