/* * 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 #include DnnToken :: DnnToken () { } DnnToken :: ~DnnToken () { } DnnState :: DnnState (const char* name) : Name (name), Transitions () { } DnnState :: ~DnnState () { CcuListIterOf 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 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 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); }