summaryrefslogtreecommitdiff
path: root/tools/dfagen/examples
diff options
context:
space:
mode:
Diffstat (limited to 'tools/dfagen/examples')
-rw-r--r--tools/dfagen/examples/Makefile40
-rw-r--r--tools/dfagen/examples/ex1.conf5
-rw-r--r--tools/dfagen/examples/ex1.fsm21
-rw-r--r--tools/dfagen/examples/ex1_cb.c.patch41
-rw-r--r--tools/dfagen/examples/ex2.conf5
-rw-r--r--tools/dfagen/examples/ex2.fsm40
-rw-r--r--tools/dfagen/examples/ex2_cb.c.patch67
-rw-r--r--tools/dfagen/examples/ex2_robot.c39
-rw-r--r--tools/dfagen/examples/ex2_robot.h18
9 files changed, 276 insertions, 0 deletions
diff --git a/tools/dfagen/examples/Makefile b/tools/dfagen/examples/Makefile
new file mode 100644
index 00000000..b78f1121
--- /dev/null
+++ b/tools/dfagen/examples/Makefile
@@ -0,0 +1,40 @@
+CFLAGS = -O2 -Wall
+
+all: ex1 ex2 ex1.png ex2.png
+
+ex1: ex1.o ex1_cb.o
+
+ex1.c: ex1.fsm ex1.conf
+ python ../dfagen.py -o c -d ex1.fsm -c ex1.conf -p ex1
+
+ex1.h ex1_cb_skel.c ex1_cb.h: ex1.c
+
+ex1_cb.c: ex1_cb_skel.c ex1_cb.c.patch
+ cp $< $@
+ patch $@ ex1_cb.c.patch
+
+ex1.o: ex1_cb.h ex1.h
+ex1_cb.o: ex1_cb.h ex1.h
+
+ex2: ex2.o ex2_cb.o ex2_robot.o
+
+ex2.c: ex2.fsm ex2.conf
+ python ../dfagen.py -o c -d ex2.fsm -c ex2.conf -p ex2
+
+ex2.h ex2_cb_skel.c ex2_cb.h: ex2.c
+
+ex2_cb.c: ex2_cb_skel.c ex2_cb.c.patch
+ cp $< $@
+ patch $@ ex2_cb.c.patch
+
+%.dot: %.fsm
+ python ../dfagen.py -o dot -d $< -p $(@:%.dot=%)
+
+%.png: %.dot
+ dot -Tpng $< -o $@
+
+clean:
+ rm -f ex1 ex1.o ex1_cb.o ex1.c ex1.h ex1_cb_skel.c ex1_cb.h ex1_cb.c
+ rm -f ex1.dot ex1.png
+ rm -f ex2 ex2.o ex2_cb.o ex2_robot.o ex2.c ex2.h ex2_cb_skel.c ex2_cb.h ex2_cb.c
+ rm -f ex2.dot ex2.png
diff --git a/tools/dfagen/examples/ex1.conf b/tools/dfagen/examples/ex1.conf
new file mode 100644
index 00000000..40a7f419
--- /dev/null
+++ b/tools/dfagen/examples/ex1.conf
@@ -0,0 +1,5 @@
+[user]
+type = door_t
+type-forward-decl = typedef struct door_t door_t;
+type-decl = struct door_t { ex1_state_t fsm; };
+field = fsm
diff --git a/tools/dfagen/examples/ex1.fsm b/tools/dfagen/examples/ex1.fsm
new file mode 100644
index 00000000..3a8f083b
--- /dev/null
+++ b/tools/dfagen/examples/ex1.fsm
@@ -0,0 +1,21 @@
+# First FSM example.
+Example 1
+ A door which can be open or closed.
+
+States:
+ OPEN
+ The door is open
+ CLOSED
+ The door is clossed
+
+Events:
+ open
+ close
+
+OPEN:
+ close -> CLOSED
+ Close the door
+
+CLOSED:
+ open -> OPEN
+ Open the door
diff --git a/tools/dfagen/examples/ex1_cb.c.patch b/tools/dfagen/examples/ex1_cb.c.patch
new file mode 100644
index 00000000..bc4cc924
--- /dev/null
+++ b/tools/dfagen/examples/ex1_cb.c.patch
@@ -0,0 +1,41 @@
+--- ex1_cb_skel.c 2008-01-06 18:00:55.000000000 +0100
++++ ex1_cb.c 2008-01-06 18:02:10.000000000 +0100
+@@ -7,6 +7,10 @@
+ */
+ #include "ex1_cb.h"
+
++#include <stdio.h>
++
++struct door_t { ex1_state_t fsm; };
++
+ /*
+ * OPEN =close=>
+ * => CLOSED
+@@ -15,6 +19,7 @@
+ ex1_branch_t
+ ex1__OPEN__close (door_t *user)
+ {
++ printf ("close the door\n");
+ return ex1_next (OPEN, close);
+ }
+
+@@ -26,7 +31,18 @@
+ ex1_branch_t
+ ex1__CLOSED__open (door_t *user)
+ {
++ printf ("open the door\n");
+ return ex1_next (CLOSED, open);
+ }
+
+-
++int
++main (void)
++{
++ door_t door;
++ ex1_init (&door);
++ ex1_handle_event (&door, EX1_EVENT_close);
++ ex1_handle_event (&door, EX1_EVENT_open);
++ printf ("now, will crash:\n");
++ ex1_handle_event (&door, EX1_EVENT_open);
++ return 0;
++}
diff --git a/tools/dfagen/examples/ex2.conf b/tools/dfagen/examples/ex2.conf
new file mode 100644
index 00000000..0e20b765
--- /dev/null
+++ b/tools/dfagen/examples/ex2.conf
@@ -0,0 +1,5 @@
+[user]
+type = robot_t
+type-forward-decl = typedef struct robot_t robot_t;
+type-decl = #include "ex2_robot.h"
+field = fsm
diff --git a/tools/dfagen/examples/ex2.fsm b/tools/dfagen/examples/ex2.fsm
new file mode 100644
index 00000000..e290c05c
--- /dev/null
+++ b/tools/dfagen/examples/ex2.fsm
@@ -0,0 +1,40 @@
+# Second FSM example.
+Example 2
+ A barman robot.
+
+States:
+ IDLE
+ waiting for a command
+ DROPPING_ICE
+ FILLING_GLASS
+
+Events:
+ command
+ ice dropped
+ glass filled
+ replace bottle
+
+IDLE:
+ command: with ice -> DROPPING_ICE
+ open the ice door
+ command: without ice -> FILLING_GLASS
+ start filling
+ command: empty bottle -> .
+ display "empty bottle, please replace it"
+ replace bottle -> .
+ reset glass counter
+
+DROPPING_ICE:
+ ice dropped -> FILLING_GLASS
+ close the ice door
+ start filling
+
+FILLING_GLASS:
+ glass filled -> IDLE
+ stop filling
+
+DROPPING_ICE, FILLING_GLASS:
+ command -> .
+ ignored
+ replace bottle -> .
+ ignored
diff --git a/tools/dfagen/examples/ex2_cb.c.patch b/tools/dfagen/examples/ex2_cb.c.patch
new file mode 100644
index 00000000..f1402431
--- /dev/null
+++ b/tools/dfagen/examples/ex2_cb.c.patch
@@ -0,0 +1,67 @@
+--- ex2_cb_skel.c 2008-01-06 18:02:50.000000000 +0100
++++ ex2_cb.c 2008-01-06 18:02:50.000000000 +0100
+@@ -6,6 +6,9 @@
+ * A barman robot.
+ */
+ #include "ex2_cb.h"
++#include "ex2_robot.h"
++
++#include <stdio.h>
+
+ /*
+ * FILLING_GLASS =command=>
+@@ -37,6 +40,7 @@
+ ex2_branch_t
+ ex2__FILLING_GLASS__glass_filled (robot_t *user)
+ {
++ puts ("stop filling");
+ return ex2_next (FILLING_GLASS, glass_filled);
+ }
+
+@@ -48,6 +52,8 @@
+ ex2_branch_t
+ ex2__IDLE__replace_bottle (robot_t *user)
+ {
++ puts ("reset glass counter");
++ user->bottle = 3;
+ return ex2_next (IDLE, replace_bottle);
+ }
+
+@@ -63,9 +69,25 @@
+ ex2_branch_t
+ ex2__IDLE__command (robot_t *user)
+ {
+- return ex2_next_branch (IDLE, command, empty_bottle);
+- return ex2_next_branch (IDLE, command, without_ice);
+- return ex2_next_branch (IDLE, command, with_ice);
++ if (user->bottle)
++ {
++ user->bottle--;
++ if (user->ice)
++ {
++ puts ("open the ice door");
++ return ex2_next_branch (IDLE, command, with_ice);
++ }
++ else
++ {
++ puts ("start filling");
++ return ex2_next_branch (IDLE, command, without_ice);
++ }
++ }
++ else
++ {
++ puts ("empty bottle, please replace it");
++ return ex2_next_branch (IDLE, command, empty_bottle);
++ }
+ }
+
+ /*
+@@ -99,6 +121,8 @@
+ ex2_branch_t
+ ex2__DROPPING_ICE__ice_dropped (robot_t *user)
+ {
++ puts ("close the ice door");
++ puts ("start filling");
+ return ex2_next (DROPPING_ICE, ice_dropped);
+ }
+
diff --git a/tools/dfagen/examples/ex2_robot.c b/tools/dfagen/examples/ex2_robot.c
new file mode 100644
index 00000000..93281f1a
--- /dev/null
+++ b/tools/dfagen/examples/ex2_robot.c
@@ -0,0 +1,39 @@
+/* Example 2 extra code. */
+#include "ex2_robot.h"
+
+#include <stdio.h>
+
+int
+main (void)
+{
+ robot_t robot;
+ ex2_init (&robot);
+ robot.bottle = 3;
+ puts ("A glass:");
+ robot.ice = 1;
+ ex2_handle_event (&robot, EX2_EVENT_command);
+ ex2_handle_event (&robot, EX2_EVENT_ice_dropped);
+ ex2_handle_event (&robot, EX2_EVENT_glass_filled);
+ puts ("Another glass:");
+ robot.ice = 0;
+ ex2_handle_event (&robot, EX2_EVENT_command);
+ ex2_handle_event (&robot, EX2_EVENT_glass_filled);
+ puts ("Yet another glass:");
+ robot.ice = 0;
+ ex2_handle_event (&robot, EX2_EVENT_command);
+ ex2_handle_event (&robot, EX2_EVENT_glass_filled);
+ puts ("There is no more liquid:");
+ robot.ice = 0;
+ ex2_handle_event (&robot, EX2_EVENT_command);
+ puts ("Replace bootle:");
+ ex2_handle_event (&robot, EX2_EVENT_replace_bottle);
+ puts ("Another glass:");
+ robot.ice = 1;
+ ex2_handle_event (&robot, EX2_EVENT_command);
+ puts ("Commands are ignore while the robot is fonctionning:");
+ ex2_handle_event (&robot, EX2_EVENT_command);
+ ex2_handle_event (&robot, EX2_EVENT_ice_dropped);
+ ex2_handle_event (&robot, EX2_EVENT_command);
+ ex2_handle_event (&robot, EX2_EVENT_glass_filled);
+ return 0;
+}
diff --git a/tools/dfagen/examples/ex2_robot.h b/tools/dfagen/examples/ex2_robot.h
new file mode 100644
index 00000000..0ceb7949
--- /dev/null
+++ b/tools/dfagen/examples/ex2_robot.h
@@ -0,0 +1,18 @@
+#ifndef ex2_robot_h
+#define ex2_robot_h
+/* Example 2 type definition. */
+
+#include "ex2.h"
+
+/* Robot structure. */
+struct robot_t
+{
+ /* The generated FSM. */
+ ex2_state_t fsm;
+ /* True for command with ice. */
+ int ice;
+ /* Number of glasses in the bottle. */
+ int bottle;
+};
+
+#endif /* ex2_robot_h */