/************************************************************************** * Modified by: hcpatel 95s522 95solc 1995/05/18 * Added #ifdef DEBUG conditions which binds printf statements. This changes * were implemented by jcaiani in 94f522 project. This printf statements can * be executed, if DEBUG is defined in header file f93olc.h. **************************************************************************/ #include #include #include # if !defined OLC3COMMON_H # include "olc3common.h" # endif /* # if !defined OLC93SU523_H # include "olc93su523.h" # endif */ #include "f93olc.h" # if !defined PROCESSEVENT_H # include "processevent.h" # endif # if !defined EVENTINSTANCE_H # include "eventinstance.h" # endif # if !defined ACTIVECLASS_H # include "activeclass.h" # endif # if !defined ACTIVEINSTANCE_H # include "activeinstance.h" # endif # if !defined STATE_H # include "state.h" # endif static PROTOTYPE(void ProcessEventsInEventQueueOrder, (int howmany)) static PROTOTYPE(void ProcessEventsInActiveInstanceQueueOrder, (int howmany)) static PROTOTYPE(void ProcessEventsInActiveClassQueueOrder, (int howmany)) static PROTOTYPE(void ProcessEventsInRandomOrder, (int howmany)) /*OWEN*/ #ifdef ORIG static PROTOTYPE(void ProcessOneEvent, (hcg_key EIid)) #else PROTOTYPE(void ProcessOneEvent, (hcg_key EIid)) #endif static PROTOTYPE(void DoTransition, (hcg_key STid, hcg_key AIid, hcg_key EIid)) static PROTOTYPE(void EventInstanceReport, (hcg_key EIid)) FUNCTION2( void ProcessEvents, EventProcessingType, how, int, howmany ) { hcg_key EIid; switch (how) { case EventQueueOrder: /* process all events on the event queue */ ProcessEventsInEventQueueOrder(howmany); break; case ActiveInstanceQueueOrder: /* process in Active Instance order */ ProcessEventsInActiveInstanceQueueOrder(howmany); break; case ActiveClassQueueOrder: /* process in Active Class order */ ProcessEventsInActiveClassQueueOrder(howmany); break; case RandomOrder: ProcessEventsInRandomOrder(howmany); break; default: printf("Don't know about that event processing type (%d)\n", how); break; } } static FUNCTION1( void ProcessEventsInEventQueueOrder, int, howmany ) { int i; hcg_key EIid; if (howmany == (int)(ProcessAllEvents)) { howmany = INT_MAX; } else if (howmany < 0) { printf("Can't process a negative number of events."); return; } for(i = 0; i < howmany; i++) { EIid = EventInstanceFirst(); if (EIid == 0) { break; } ProcessOneEvent(EIid); } } static FUNCTION1( void ProcessEventsInActiveInstanceQueueOrder, int, howmany ) { int i; hcg_key AIid; hcg_key EIid; int EventProcessed; if (howmany == (int)(ProcessAllEvents)) { howmany = INT_MAX; } else if (howmany < 0) { printf("Can't process a negative number of events."); return; } for(i = 0; i < howmany; i++) { EventProcessed = 0; AIid = ActiveInstanceFirst(); while(AIid != 0) { EIid = EventInstanceFirst(); while(EIid != 0) { if (EventInstanceGetAIid2(EIid) == AIid) { ProcessOneEvent(EIid); EventProcessed += 1; break; } EIid = EventInstanceNext(EIid); } AIid = ActiveInstanceNext(AIid); } if (EventProcessed == 0) { /* Quit if no more events */ return; } } } static FUNCTION1( void ProcessEventsInActiveClassQueueOrder, int, howmany ) { int i; hcg_key ACid; hcg_key AIid; hcg_key EIid; int EventProcessed; if (howmany == (int)(ProcessAllEvents)) { howmany = INT_MAX; } else if (howmany < 0) { printf("Can't process a negative number of events."); return; } for(i = 0; i < howmany; i++) { EventProcessed = 0; ACid = ActiveClassFirst(); while(ACid != 0) { AIid = ActiveInstanceFirst(); while(AIid != 0) { if (ActiveInstanceGetACid(AIid) == ACid) { EIid = EventInstanceFirst(); while(EIid != 0) { if (EventInstanceGetAIid2(EIid) == AIid) { ProcessOneEvent(EIid); EventProcessed += 1; break; } EIid = EventInstanceNext(EIid); } } AIid = ActiveInstanceNext(AIid); } ACid = ActiveClassNext(ACid); } if (EventProcessed == 0) { /* Quit if no more events */ return; } } } static FUNCTION1( void ProcessEventsInRandomOrder, int, howmany ) { int i; int j; hcg_key curEIid; hcg_key newEIid; hcg_key AIidfrom; hcg_key AIidto; if (howmany == (int)(ProcessAllEvents)) { howmany = INT_MAX; } else if (howmany < 0) { printf("Can't process a negative number of events."); return; } for(i = 0; i < howmany; i++) { /* ** Choose one of the first 20 events at random. Each newEIid has ** a 1/(j+1) chance of being chosen. So the first Event is chosen ** with probability 1. The second is then chosen with probability .5. ** If it isn't chosen, then the first is left as the chosen one so ** if there are two events, the first and second are chosen with ** equal probabilities. Induction will show that this continues. */ newEIid = EventInstanceFirst(); curEIid = newEIid; if (curEIid == 0) { /* no events? */ break; /* nothing to do */ } for(j = 0; i < 20; j++) { newEIid = EventInstanceNext(newEIid); if (newEIid == 0) { break; } if (0 == (rand() / (RAND_MAX /(j+1)))) { curEIid = newEIid; } } /* ** We can't just use this curEIid since there might be an earlier ** which must be processed first. Look for this */ AIidfrom = EventInstanceGetAIid1(curEIid); AIidto = EventInstanceGetAIid2(curEIid); newEIid = EventInstanceFirst(); while (newEIid != curEIid) { if ( (EventInstanceGetAIid1(newEIid) == AIidfrom) && (EventInstanceGetAIid2(newEIid) == AIidto)) { curEIid = newEIid; } else { newEIid = EventInstanceNext(newEIid); } } /* ** Process this event */ ProcessOneEvent(curEIid); } } /*OWEN*/ #ifdef ORIG static FUNCTION1( void ProcessOneEvent, hcg_key, EIid ) #else FUNCTION1( void ProcessOneEvent, hcg_key, EIid ) #endif { hcg_key AIid; hcg_key ETid; hcg_key STid; char CurState[StatesNameMaxLen+1]; KEYBUFFER(STkey); #ifdef DEBUG /* pr_dump will be replaced with new meets_view function in the future. It is commented out because it outputs too much information */ /* pr_dump("JuicePlant", "temp.dat", 1, "a"); */ #endif ETid = EventInstanceGetETid(EIid); AIid = EventInstanceGetAIid2(EIid); ActiveInstanceCopyState(AIid, CurState); /* ** We have the event type and the current state, find a transition ** from this state enable by the event type. If we find one, do it. */ STid = StateFirst(); while (STid != 0) { if (strcmp(CurState, StateGetName(STid)) == 0) { break; } STid = StateNext(STid); } if (STid == 0) { printf("Cannot proccess event with key %x as the destination\n", EIid); printf("active instance with key %x has an invalid current state %s.\n", AIid, CurState); return; } child_loop (ST, TR, TRid1, STid1) { child_loop(TR, EN, ENid, TRid) { if (ENcurr->ETid == ETid) { DoTransition(TRcurr->STid2, AIid, EIid); goto DELETE_EVENT_INSTANCE; } } } DELETE_EVENT_INSTANCE: EventInstanceDelete(EIid); } static FUNCTION3( void DoTransition, hcg_key, STid, hcg_key, AIid, hcg_key, EIid ) { /* ** First, put the active instance into the correct state */ ActiveInstanceSetState(AIid, StateGetName(STid)); /* ** And call the action routine */ (*StateGetActFunc(STid))(EIid); } static FUNCTION1( void EventInstanceReport, hcg_key, EIid ) { hcg_key ETid, AIid1, AIid2, STid; char CurState[StatesNameMaxLen+1]; ETid = EventInstanceGetETid(EIid); AIid1 = EventInstanceGetAIid1(EIid); AIid2 = EventInstanceGetAIid2(EIid); printf("EventInstance ID: %x\n", EIid); printf(" Trigger event - %s: %s\n", EventTypeGetLabel(ETid), EventTypeGetDescrip(ETid)); printf(" From - %s\n", ActiveInstanceGetName(AIid1)); printf(" To - %s\n", ActiveInstanceGetName(AIid2)); ActiveInstanceCopyState(AIid2, CurState); printf(" now in state %s\n", CurState); printf(" Data - %d %d %f %f \"%s\"\n\n", EventInstanceGetInt1(EIid), EventInstanceGetInt2(EIid), (double)EventInstanceGetFlt1(EIid), (double)EventInstanceGetFlt2(EIid), EventInstanceGetText(EIid)); }