summaryrefslogtreecommitdiff
path: root/dnn/Behaviour.cc
diff options
context:
space:
mode:
Diffstat (limited to 'dnn/Behaviour.cc')
-rwxr-xr-xdnn/Behaviour.cc151
1 files changed, 151 insertions, 0 deletions
diff --git a/dnn/Behaviour.cc b/dnn/Behaviour.cc
new file mode 100755
index 0000000..98a5b8d
--- /dev/null
+++ b/dnn/Behaviour.cc
@@ -0,0 +1,151 @@
+/*
+ * DNN - Data News Network
+ *
+ * by Stephane Chatty
+ *
+ * Copyright 1993-1996
+ * Centre d'Etudes de la Navigation Aerienne (CENA)
+ *
+ * Behaviours.
+ *
+ * $Id$
+ * $CurLog$
+ */
+
+#include "Behaviour.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+DnnToken :: DnnToken ()
+{
+}
+
+DnnToken :: ~DnnToken ()
+{
+}
+
+DnnState :: DnnState (const char* name)
+: Name (name),
+ Transitions ()
+{
+}
+
+DnnState :: ~DnnState ()
+{
+ CcuListIterOf <DnnTransition> t = Transitions;
+ while (++t)
+ delete *t;
+}
+
+DnnTransition*
+DnnState :: CreateTransition (const char* name, DnnState* s, DnnActivation* a, DnnAction* aa)
+{
+ DnnTransition* t = new DnnTransition (name, this, s, a, aa);
+ Transitions.Append (t);
+ return t;
+}
+
+void
+DnnState :: Activate (DnnBehaviour* b, DnnToken* tk)
+{
+ CcuListIterOf <DnnTransition> t = Transitions;
+ while (++t) {
+ DnnBehaviourReaction* r = new DnnBehaviourReaction (b, *t);
+ (*t)->Activate (r, tk);
+ b->AddReaction (r);
+ }
+}
+
+DnnTransition :: DnnTransition (const char* name, DnnState* from, DnnState* to, DnnActivation* a, DnnAction* aa)
+: Name (name),
+ From (from),
+ To (to),
+ Activation (a),
+ Action (aa)
+{
+}
+
+DnnTransition :: ~DnnTransition ()
+{
+}
+
+void
+DnnTransition :: Activate (DnnBaseReaction* r, DnnToken* t)
+{
+ (*Activation) (r, t);
+}
+
+DnnState*
+DnnTransition :: Fire (DnnToken* tk, DnnEvent* ev)
+{
+ (*Action) (tk, ev);
+ return To;
+}
+
+DnnBehaviourModel :: DnnBehaviourModel ()
+: Initial (0),
+ States ()
+{
+}
+
+DnnBehaviourModel :: ~DnnBehaviourModel ()
+{
+ CcuListIterOf <DnnState> s = States;
+ while (++s)
+ delete *s;
+}
+
+DnnState*
+DnnBehaviourModel :: CreateState (const char* name)
+{
+ DnnState* s = new DnnState (name);
+ States.Append (s);
+ return s;
+}
+
+DnnBehaviour :: DnnBehaviour (DnnBehaviourModel& m, DnnToken* tk)
+: TheModel (&m),
+ CurState (m.GetInitial ()),
+ CurReactions (),
+ CurToken (tk)
+{
+ CurState->Activate (this, CurToken);
+}
+
+void
+DnnBehaviour :: Fire (DnnTransition* t, DnnEvent* ev)
+{
+ /* check consistency */
+ if (CurState != t->GetOrigin ()) {
+ fprintf (stderr, "incoherent state in behaviour\n");
+ abort ();
+ }
+
+ /* disactivate */
+ DnnBaseReaction* r;
+ while (r = CurReactions.RemoveFirst ())
+ delete r;
+
+ /* fire transition */
+ CurState = t->Fire (CurToken, ev);
+
+ /* activate new state */
+ CurState->Activate (this, CurToken);
+}
+
+DnnBehaviourReaction :: DnnBehaviourReaction (DnnBehaviour* b, DnnTransition* t)
+ : TheBehaviour (b),
+ TheTransition (t)
+{
+}
+
+DnnBehaviourReaction :: ~DnnBehaviourReaction ()
+{
+}
+
+void
+DnnBehaviourReaction :: Manage (DnnEvent& ev)
+{
+ TheBehaviour->Fire (TheTransition, &ev);
+}