/* * 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 IvlToken :: IvlToken () { } IvlToken :: ~IvlToken () { } IvlState :: IvlState (const char* name) : Name (name), Transitions () { } IvlState :: ~IvlState () { IvlListIterOf t = Transitions; while (++t) delete *t; } IvlTransition* IvlState :: CreateTransition (const char* name, IvlState* s, IvlActivation* a, IvlAction* aa) { IvlTransition* t = new IvlTransition (name, this, s, a, aa); Transitions.Append (t); return t; } void IvlState :: Activate (IvlBehaviour* b, IvlToken* tk) { IvlListIterOf t = Transitions; while (++t) { IvlBehaviourReaction* r = new IvlBehaviourReaction (b, *t); (*t)->Activate (r, tk); b->AddReaction (r); } } IvlTransition :: IvlTransition (const char* name, IvlState* from, IvlState* to, IvlActivation* a, IvlAction* aa) : Name (name), From (from), To (to), Activation (a), Action (aa) { } IvlTransition :: ~IvlTransition () { } void IvlTransition :: Activate (IvlBaseReaction* r, IvlToken* t) { (*Activation) (r, t); } IvlState* IvlTransition :: Fire (IvlToken* tk, IvlEvent* ev) { (*Action) (tk, ev); return To; } IvlBehaviourModel :: IvlBehaviourModel () : Initial (0), States () { } IvlBehaviourModel :: ~IvlBehaviourModel () { IvlListIterOf s = States; while (++s) delete *s; } IvlState* IvlBehaviourModel :: CreateState (const char* name) { IvlState* s = new IvlState (name); States.Append (s); return s; } IvlBehaviour :: IvlBehaviour (IvlBehaviourModel& m, IvlToken* tk) : TheModel (&m), CurState (m.GetInitial ()), CurReactions (), CurToken (tk) { CurState->Activate (this, CurToken); } void IvlBehaviour :: Fire (IvlTransition* t, IvlEvent* ev) { /* check consistency */ if (CurState != t->GetOrigin ()) { fprintf (stderr, "incoherent state in behaviour\n"); abort (); } /* disactivate */ IvlBaseReaction* r; while (r = CurReactions.RemoveFirst ()) delete r; /* fire transition */ CurState = t->Fire (CurToken, ev); /* activate new state */ CurState->Activate (this, CurToken); } IvlBehaviourReaction :: IvlBehaviourReaction (IvlBehaviour* b, IvlTransition* t) : TheBehaviour (b), TheTransition (t) { } IvlBehaviourReaction :: ~IvlBehaviourReaction () { } void IvlBehaviourReaction :: Manage (IvlEvent& ev) { TheBehaviour->Fire (TheTransition, &ev); }