24 | | AimsunDS DS; |
25 | | shared_ptr<logger> L = UI::build <logger>( F, "logger" ); |
| 22 | // SYSTEM TO CONTROL |
| 23 | AimsunDS Ds; //hard-coded so far |
| 24 | |
| 25 | // AGENTS |
| 26 | Array<shared_ptr<Participant> > Ags; |
| 27 | UI::get ( Ags,Cfg, "agents" ); |
| 28 | |
| 29 | // LOGGER |
| 30 | shared_ptr<logger> L = UI::build <logger>( Cfg, "logger" ); |
| 31 | if (!L) {L=new stdlog();} // DEFAULT LOGGER <== poor choice, use better ones |
| 32 | |
| 33 | Config MsgStore; |
| 34 | MsgStore.setAutoConvert(true); |
| 35 | Setting& Queue = MsgStore.getRoot().add("queue", Setting::TypeList); |
| 36 | |
| 37 | // REGISTER ACTIVE OBJECTS IN LOGGER |
| 38 | Ds.log_register ( *L, "DS" ); |
| 39 | for ( int i=0; i<Ags.length(); i++ ) { |
| 40 | Ags ( i )->log_register ( *L,Ags(i)->_name() ); // estimate |
| 41 | Ags ( i )->ds_register(Ds); // allows agents to update their datalinks |
| 42 | } |
| 43 | L->init(); |
| 44 | |
| 45 | vec glob_dt(Ds._drv()._dsize() ); |
| 46 | vec glob_ut(Ds._urv()._dsize() ); |
| 47 | for ( int tK=0;tK<Ds.max_length();tK++ ) { |
| 48 | Ds.log_write ( ); // write stuff to |
| 49 | Ds.getdata(glob_dt); |
| 50 | |
| 51 | for ( int i=0; i<Ags.length(); i++ ) { |
| 52 | Ags(i) -> adapt(glob_dt); |
| 53 | } |
| 54 | for ( int i=0; i<Ags.length(); i++ ) { |
| 55 | Ags(i) -> broadcast(Queue); |
| 56 | } |
| 57 | // parse message queue |
| 58 | for ( int m=Queue.getLength()-1; m>=0; m-- ) { // go backwards - last mesages are discarded |
| 59 | for ( int i=0; i<Ags.length(); i++ ) { |
| 60 | Setting& msg=Queue[m]; |
| 61 | string m_to=msg["to"]; |
| 62 | if (m_to==Ags(i)->_name()) { |
| 63 | Ags(i)->receive(msg); |
| 64 | Queue.remove(m); |
| 65 | break; |
| 66 | // message delivered; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | if (Queue.getLength()>0){bdm_error("undelivered messages - probably unknown neighbours");} |
| 71 | |
| 72 | for ( int i=0; i<Ags.length(); i++ ) { |
| 73 | Ags(i) -> act(glob_ut); |
| 74 | } |
| 75 | |
| 76 | L->step(); |
| 77 | Ds.write(glob_ut); |
| 78 | Ds.step(); // simulator step |
| 79 | |
| 80 | for ( int i=0; i<Ags.length(); i++ ) { |
| 81 | Ags(i) -> step(); |
| 82 | } |
| 83 | |
| 84 | } |