3.6 Examples
The folder examples of the runtime has four example models outside the lamp domain. Each folder has a metamodel, a model, one state machine, event definitions in EVENTS.kmeta and a README.md with the steps to run it. The lamp itself is in the folder examples/lamp of the IDE, and a room with spare bulbs whose state machines objects get at run time, as 3.1.13 State machine management and lifecycle describes, in the folder examples/spares of the IDE.
| Folder | What it shows | Object of the state machine | Events |
|---|---|---|---|
single_robot_example | A cleaning robot that drives around a room and steers away from obstacles and walls: nested states, NOT LOOPING, EVAL conditions, SETOBJ on a knows relation, WITH ... AS, macros with numpy, events raised by the state machine | turtle | start in domain public. EVENTS.kmeta also defines setSpeed and teleport, which the state machine does not use |
delivery_drone_example | A delivery drone driven by events with payloads: conditions on payload fields, $(event->...) in ENTRY blocks, IF IN SCOPE, payload values copied into the model | scout | assignDelivery, setCruiseSpeed, recall in domain control |
autonomous_car_example | A car that tests three subsystems in parallel before it drives: SPLIT into three branches and JOIN back | av-1 | powerOn in domain ignition |
drone_swarm_example | A swarm that grows, reshapes and shrinks its fleet: APPENDOBJ, SETOBJ, DROPOBJ, SETLIST, DROPLIST, WITH ... AS | alpha-swarm | deploy, droneDown, recall in domain swarm |
| Folder | Files |
|---|---|
single_robot_example | cleaning_robot.kmeta, cleaning_robot.kmodel, cleaning_robot.kstates, EVENTS.kmeta |
delivery_drone_example | delivery_drone.kmeta, delivery_drone.kmodel, delivery_drone.kstates, EVENTS.kmeta |
autonomous_car_example | autonomous_car.kmeta, autonomous_car.kmodel, autonomous_car.kstates, EVENTS.kmeta |
drone_swarm_example | drone_swarm.kmeta, drone_swarm.kmodel, drone_swarm.kstates, EVENTS.kmeta |
All four use EVAL blocks or macros, so run them with Python at hand. The macros of the robot import numpy. The Docker image of 3.3.3 Running in Docker has both.
To run an example:
- Start the runtime as 3.3 Running the runtime describes.
-
In the folder of the example, create an environment and upload the files. For the car:
ENV=$(curl -s -X POST http://127.0.0.1:8080/createEnvironment) curl -X PUT "http://127.0.0.1:8080/setMetamodel?envKey=$ENV" --data-binary @autonomous_car.kmeta curl -X PUT "http://127.0.0.1:8080/setModel?envKey=$ENV" --data-binary @autonomous_car.kmodel curl -X PUT "http://127.0.0.1:8080/setEventDefinitions?envKey=$ENV" --data-binary @EVENTS.kmeta curl -X PUT "http://127.0.0.1:8080/setStateMachine?envKey=$ENV&attachedTo=av-1" --data-binary @autonomous_car.kstates curl -X POST "http://127.0.0.1:8080/setTickDelay?envKey=$ENV&milliseconds=300" -
Register a WebSocket client and an observer of the object of the state machine:
KEY=$(curl -s -X POST "http://127.0.0.1:8080/registerClientForWebSocket?clientId=viewer&envKey=$ENV") curl -X POST "http://127.0.0.1:8080/registerObjectObserver?envKey=$ENV&clientId=viewer&objectId=av-1" -
Run and start the environment:
curl -X POST "http://127.0.0.1:8080/runEnvironment?envKey=$ENV" curl -X POST "http://127.0.0.1:8080/startEnvironment?envKey=$ENV" -
Connect the client to
ws://127.0.0.1:8080/ws, sendviewer:<ENV>:<KEY>as the first frame and then the event that starts the example:{"environmentKey": "ignition", "messageType": "powerOn", "payload": ""}
The client gets objectChanged for the car each time an action block changes it. The README of each example names its events and payloads. The connection and the messages are in 3.5 WebSocket.