00001 #include "stdafx.h"
00002 #include <stdlib.h>
00003 #define COLOR "0x710071"
00004 #define ICON "focusstate.bmp"
00005 #define EVTCOL "eventsel.bmp"
00006 #define TRANSITCOL "0xFF0000"
00007
00008 #include "BONComponent.h"
00009
00010
00011 HWND GMEHandle::hwnd;
00012 CBuilder * GMEHandle::bldr;
00013
00014 void CComponent::InvokeEx(CBuilder &builder,CBuilderObject *focus, CBuilderObjectList &selected, long param)
00015 {
00016 builder.BeginTransaction();
00017 GMEHandle::bldr = &builder;
00018
00019
00020 CBuilderFolder *fldr = builder.GetRootFolder();
00021
00022
00023
00024
00025
00026
00027 GMEHandle::hwnd = AfxGetApp()->GetMainWnd()->GetSafeHwnd();
00028
00029
00030 CSequence inp(builder);
00031 const CBuilderEventList *inpseq = inp.GetSequence();
00032
00033 if(inpseq!=NULL && !inpseq->IsEmpty())
00034 {
00035 CState StateMachine(builder);
00036 CBuilderState *fsm = NULL;
00037 StateMachine.GetRootState(&fsm);
00038
00039 POSITION pos = inpseq->GetHeadPosition();
00040
00041 if(fsm != NULL)
00042 {
00043 AfxMessageBox("Ready to Start Simulation");
00044 CBuilderState *currstate = fsm->GetActiveSubstate();
00045
00046 builder.CommitTransaction();
00047 builder.BeginTransaction();
00048 RedrawWindow(GMEHandle::hwnd, 0, 0, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
00049
00050
00051
00052 while(pos)
00053 {
00054 CBuilderEvent *evt = inpseq->GetNext(pos);
00055
00056 int delay=0;
00057
00058 delay = evt->GetDelay() * 1000;
00059 if(delay>0)
00060 Sleep(delay);
00061 else
00062 AfxMessageBox("Next Event: - " + evt->GetName());
00063
00064
00065 evt->Highlight(EVTCOL, GMEHandle::bldr, GMEHandle::hwnd);
00066
00067 CBuilderState *nextstate = currstate->TransitOnEvent(evt);
00068
00069 if(nextstate != NULL)
00070 currstate = nextstate;
00071 else
00072 currstate = currstate;
00073
00074
00075 builder.CommitTransaction();
00076 builder.BeginTransaction();
00077 RedrawWindow(GMEHandle::hwnd, 0, 0, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
00078
00079 }
00080
00081 AfxMessageBox("Simulation Over. Final State:- " + currstate->GetName());
00082
00083 CBuilderState *final = currstate;
00084 while(final != NULL)
00085 {
00086 CString col = "color";
00087 final->SetRegistryValue(col, currstate->GetOriginalColor());
00088 CString ico = "icon";
00089 final->SetRegistryValue(ico, currstate->GetOriginalIcon());
00090
00091 final = (CBuilderState *)final->GetParent();
00092 }
00093 }
00094 }
00095
00096 builder.CommitTransaction();
00097
00098 }
00099
00100
00101
00102 IMPLEMENT_CUSTOMATOM(CBuilderEvent, CBuilderAtom, "Events")
00103
00104 void CBuilderEvent::Initialize()
00105 {
00106 CBuilderAtom::Initialize();
00107 }
00108
00109 CBuilderEvent::~CBuilderEvent()
00110 {
00111
00112 }
00113
00114 int CBuilderEvent::GetDelay()
00115 {
00116 this->GetAttribute("Delay", delay);
00117 return delay;
00118 }
00119
00120 CString CBuilderEvent::GetOriginalIcon()
00121 {
00122 return orgicon;
00123 }
00124
00125 void CBuilderEvent::SaveOriginalIcon()
00126 {
00127 CString ico = "icon";
00128 CString val = "";
00129 this->GetRegistryValue(ico, val);
00130 orgicon = val;
00131 }
00132
00133 void CBuilderEvent::Highlight(CString eval, CBuilder *bldr, HWND handle)
00134 {
00135 CString ico = "icon";
00136
00137 this->SaveOriginalIcon();
00138 this->SetRegistryValue(ico, eval);
00139
00140 bldr->CommitTransaction();
00141 bldr->BeginTransaction();
00142 RedrawWindow(handle, 0, 0, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
00143
00144 Sleep(300);
00145
00146 eval = this->GetOriginalIcon();
00147 this->SetRegistryValue(ico, eval);
00148
00149 bldr->CommitTransaction();
00150 bldr->BeginTransaction();
00151 RedrawWindow(handle, 0, 0, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
00152 }
00153
00154
00155
00156 CSequence::CSequence(CBuilder &builder)
00157 {
00158 root = builder.GetRootFolder();
00159 events = NULL;
00160 }
00161
00162 CSequence::~CSequence()
00163 {
00164
00165 }
00166
00167
00168 bool CSequence::GetEvents()
00169 {
00170 const CBuilderModelList *rmodels = root->GetRootModels();
00171 POSITION pos = rmodels->GetHeadPosition();
00172
00173 bool found = false;
00174 CBuilderModel *container;
00175
00176 while(pos)
00177 {
00178 container = rmodels->GetNext(pos);
00179
00180 if(container->GetKindName()=="InputSequence")
00181 {
00182 found = true;
00183 break;
00184 }
00185 }
00186
00187 if(found)
00188 {
00189 const CBuilderAtomList *allevents = container->GetAtoms("Events");
00190 events = (CBuilderEventList *)allevents;
00191 return true;
00192 }
00193 else
00194 {
00195 AfxMessageBox("No Events/Sequence Defined - Cannot Continue");
00196 return false;
00197 }
00198 }
00199
00200
00201 CBuilderEventList * CSequence::GetSequence()
00202 {
00203 CBuilderEventList *sequence = new CBuilderEventList;
00204
00205 if(GetEvents())
00206 {
00207 if(!events->IsEmpty())
00208 {
00209 CBuilderEvent *start=NULL;
00210
00211 int seqcount = SingleSequence(events, &start);
00212
00213 if(seqcount != 1)
00214 {
00215 AfxMessageBox("Please Specify Exactly a Single Sequence");
00216 }
00217
00218 else
00219 {
00220 if(start!=NULL)
00221 {
00222 sequence->AddTail(start);
00223 TraverseSequence(start, sequence);
00224 }
00225 }
00226 }
00227 else
00228 AfxMessageBox("No Valid Events");
00229 }
00230 return sequence;
00231 }
00232
00233
00234 int CSequence::SingleSequence(const CBuilderEventList *inp, CBuilderEvent **startevent)
00235 {
00236 POSITION pos = inp->GetHeadPosition();
00237
00238
00239 int count=0;
00240
00241 while(pos)
00242 {
00243 CBuilderEvent *fevt = inp->GetNext(pos);
00244
00245 CBuilderObjectList temp;
00246 fevt->GetInConnectedObjects("Sequence", temp);
00247
00248 if(temp.GetCount()==0)
00249 {
00250 count++;
00251 *startevent = fevt;
00252 }
00253 }
00254 return count;
00255 }
00256
00257
00258 void CSequence::TraverseSequence(CBuilderEvent *node, CBuilderEventList *seq)
00259 {
00260 CBuilderObjectList temp;
00261 CBuilderEvent *tempnode;
00262
00263 tempnode = node;
00264
00265 while(tempnode->GetOutConnectedObjects("Sequence", temp))
00266 {
00267 if (temp.IsEmpty())
00268 break;
00269
00270 POSITION pos = temp.GetHeadPosition();
00271
00272 CBuilderEvent *destnode = (CBuilderEvent *)temp.GetNext(pos);
00273
00274 seq->AddTail(destnode);
00275 tempnode = destnode;
00276 temp.RemoveAll();
00277 }
00278 }
00279
00280
00281
00282 IMPLEMENT_CUSTOMMODEL(CBuilderState, CBuilderModel, "State")
00283
00284 void CBuilderState::Initialize()
00285 {
00286 this->activesubstate = NULL;
00287 CBuilderModel::Initialize();
00288 }
00289
00290 CBuilderState::~CBuilderState()
00291 {
00292
00293 }
00294
00295 bool CBuilderState::GetChildStartState(CBuilderState **start)
00296 {
00297 const CBuilderModelList *modlist = this->GetModels();
00298 if(modlist==NULL || modlist->IsEmpty())
00299 {
00300 return false;
00301 }
00302 else
00303 {
00304 POSITION pos = modlist->GetHeadPosition();
00305
00306 while(pos)
00307 {
00308 CBuilderState *state = (CBuilderState *)modlist->GetNext(pos);
00309 if(state->GetIsStartState())
00310 {
00311 *start = state;
00312 return true;
00313 }
00314 }
00315 return false;
00316 }
00317 }
00318
00319
00320
00321 CBuilderState * CBuilderState::GetActiveSubstate()
00322 {
00323 CString col = "color";
00324 CString val = COLOR;
00325 this->SaveOriginalColor();
00326 this->SetRegistryValue(col, val);
00327
00328 CString ico = "icon";
00329 val = ICON;
00330 this->SaveOriginalIcon();
00331 this->SetRegistryValue(ico, val);
00332
00333 CBuilderState *start=NULL;
00334 if(this->GetChildStartState(&start))
00335 {
00336
00337 this->activesubstate = start->GetActiveSubstate();
00338 }
00339
00340 if(activesubstate != NULL)
00341 return activesubstate;
00342 else
00343 return this;
00344 }
00345
00346
00347 CBuilderState * CBuilderState::TransitOnEvent(CBuilderEvent *evt)
00348 {
00349
00350 CBuilderState *nstate = NULL;
00351 if(this->GetNextState(evt, &nstate))
00352 {
00353
00354 CString col = "color";
00355 CString val = this->GetOriginalColor();
00356 this->SetRegistryValue(col, val);
00357
00358 CString ico = "icon";
00359 val = this->GetOriginalIcon();
00360 this->SetRegistryValue(ico, val);
00361
00362 CBuilderState *nextactivestate=NULL;
00363
00364 nextactivestate = nstate->GetActiveSubstate();
00365 return nextactivestate;
00366 }
00367 else
00368 {
00369
00370 CBuilderState *par = (CBuilderState *)this->GetParent();
00371 if(par != NULL)
00372 {
00373 nstate = par->TransitOnEvent(evt);
00374 if(nstate != NULL)
00375 {
00376 CString col = "color";
00377 CString val = this->GetOriginalColor();
00378 this->SetRegistryValue(col, val);
00379
00380 CString ico = "icon";
00381 val = this->GetOriginalIcon();
00382 this->SetRegistryValue(ico, val);
00383
00384 return nstate;
00385 }
00386 }
00387 return NULL;
00388 }
00389 }
00390
00391
00392 bool CBuilderState::GetNextState(CBuilderEvent *event, CBuilderState **nextstate)
00393 {
00394 const CBuilderConnectionList *connlist = this->GetOutConnections("Transition");
00395
00396 if(connlist!=NULL)
00397 {
00398 POSITION pos = connlist->GetHeadPosition();
00399 while(pos)
00400 {
00401 CBuilderTransition *conn = (CBuilderTransition *)connlist->GetNext(pos);
00402
00403 if(conn->GetEvent() == event->GetName())
00404 {
00405 conn->Highlight(TRANSITCOL, GMEHandle::bldr, GMEHandle::hwnd);
00406
00407 *nextstate = (CBuilderState *)conn->GetDestination();
00408 (*nextstate)->lastincomingtransition = conn;
00409 return true;
00410 }
00411 }
00412 }
00413 return false;
00414 }
00415
00416 CString CBuilderState::GetOriginalColor()
00417 {
00418 return orgcol;
00419 }
00420
00421 void CBuilderState::SaveOriginalColor()
00422 {
00423 CString col = "color";
00424 CString val = "";
00425 this->GetRegistryValue(col, val);
00426 orgcol = val;
00427 }
00428
00429 CString CBuilderState::GetOriginalIcon()
00430 {
00431 return orgicon;
00432 }
00433
00434 void CBuilderState::SaveOriginalIcon()
00435 {
00436 CString ico = "icon";
00437 CString val = "";
00438 this->GetRegistryValue(ico, val);
00439 orgicon = val;
00440 }
00441
00442 bool CBuilderState::GetIsStartState()
00443 {
00444 this->GetAttribute("StartState", isstart);
00445 return isstart;
00446 }
00447
00448
00449
00450
00451 CState::CState(CBuilder &builder)
00452 {
00453 root = builder.GetRootFolder();
00454 }
00455
00456 CState::~CState()
00457 {
00458
00459 }
00460
00461 bool CState::GetRootState(CBuilderState **rootstate)
00462 {
00463 const CBuilderModelList *rmodels = root->GetRootModels();
00464 POSITION pos = rmodels->GetHeadPosition();
00465
00466 bool found = false;
00467 CBuilderModel *stcontainer;
00468
00469 while(pos)
00470 {
00471 stcontainer = rmodels->GetNext(pos);
00472
00473 if(stcontainer->GetKindName()=="State")
00474 {
00475 *rootstate = (CBuilderState *)stcontainer;
00476 found = true;
00477 break;
00478 }
00479 }
00480
00481 if(!found)
00482 {
00483 AfxMessageBox("No State Machine Defined");
00484 return false;
00485 }
00486 return true;
00487 }
00488
00489
00490
00491
00492 IMPLEMENT_CUSTOMCONNECTION(CBuilderTransition, CBuilderConnection, "Transition")
00493
00494 void CBuilderTransition::Initialize()
00495 {
00496 CBuilderConnection::Initialize();
00497 }
00498
00499 CBuilderTransition::~CBuilderTransition()
00500 {
00501
00502 }
00503
00504 CString CBuilderTransition::GetEvent()
00505 {
00506 this->GetAttribute("Event", event);
00507 return event;
00508 }
00509
00510
00511 CString CBuilderTransition::GetOriginalColor()
00512 {
00513 return orgcol;
00514 }
00515
00516 void CBuilderTransition::SaveOriginalColor()
00517 {
00518 CString col = "color";
00519 CString val = "";
00520 this->GetRegistryValue(col, val);
00521 orgcol = val;
00522 }
00523
00524 void CBuilderTransition::Highlight(CString tval, CBuilder *bldr, HWND handle)
00525 {
00526 CString col = "color";
00527
00528 this->SaveOriginalColor();
00529 this->SetRegistryValue(col, tval);
00530
00531 bldr->CommitTransaction();
00532 bldr->BeginTransaction();
00533 RedrawWindow(handle, 0, 0, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
00534
00535 Sleep(250);
00536
00537 tval = this->GetOriginalColor();
00538 this->SetRegistryValue(col, tval);
00539
00540 bldr->CommitTransaction();
00541 bldr->BeginTransaction();
00542 RedrawWindow(handle, 0, 0, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
00543 }