00001 #include "GridWise2.h"
00002 using namespace adevs;
00003 using namespace std;
00004
00005 const int GridFriendly2Network::sensor_trip = 0;
00006 const int GridFriendly2Network::load_change = 1;
00007 const int GridFriendly2Network::base_load = 2;
00008
00009 GridFriendly2Network::GridFriendly2Network(ElectricalData* data):
00010 Network<PortValue<BasicEvent*> >()
00011 {
00012
00013 for (unsigned i = 0; i < data->getNodeCount(); i++)
00014 {
00015 GridFriendly2Load* load = new GridFriendly2Load(i,data);
00016 loads[load] = load;
00017 load->setParent(this);
00018 }
00019 }
00020
00021 void GridFriendly2Network::getSensors(list<Sensor*>& sensors)
00022 {
00023
00024 map<GridFriendly2Load*,GridFriendly2Load*>::iterator iter = loads.begin();
00025 for (; iter != loads.end(); iter++)
00026 {
00027 GridFriendly2Sensor *up, *down;
00028 GridFriendly2Sensor::makeSensors((*iter).first,&up,&down);
00029 sensors.push_back(up);
00030 sensors.push_back(down);
00031 }
00032 }
00033
00034 void GridFriendly2Network::route(const PortValue<BasicEvent*>& x,
00035 Devs<PortValue<BasicEvent*> >* model,
00036 Bag<Event<adevs::PortValue<BasicEvent*> > >& r)
00037 {
00038
00039 if (model == this && x.port == sensor_trip)
00040 {
00041 SensorEvent* sensor_event = dynamic_cast<SensorEvent*>(x.value);
00042 PortValue<BasicEvent*> pv(GridFriendly2Load::sensor_input,sensor_event);
00043 Event<PortValue<BasicEvent*> > e(sensor_event->getCause()->getDeviceModel(),pv);
00044 r.insert(e);
00045 }
00046
00047 else if (model == this && x.port == base_load)
00048 {
00049
00050 LoadEvent* load_event = dynamic_cast<LoadEvent*>(x.value);
00051 map<GridFriendly2Load*,GridFriendly2Load*>::iterator iter = loads.begin();
00052 for (; iter != loads.end(); iter++)
00053 {
00054 if ((*iter).first->getBusID() == load_event->getBusID())
00055 {
00056 PortValue<BasicEvent*> pv(GridFriendly2Load::base_load,load_event);
00057 Event<PortValue<BasicEvent*> > e((*iter).first,pv);
00058 r.insert(e);
00059 return;
00060 }
00061 }
00062 }
00063
00064 else
00065 {
00066 PortValue<BasicEvent*> pv(load_change,x.value);
00067 Event<PortValue<BasicEvent*> > e(this,pv);
00068 r.insert(e);
00069 }
00070 }
00071
00072 void GridFriendly2Network::getComponents(Set<Devs<PortValue<BasicEvent*> >* >& c)
00073 {
00074
00075 map<GridFriendly2Load*,GridFriendly2Load*>::iterator iter = loads.begin();
00076 for (; iter != loads.end(); iter++)
00077 {
00078 c.insert((*iter).first);
00079 }
00080
00081 }
00082
00083 GridFriendly2Network::~GridFriendly2Network()
00084 {
00085
00086 map<GridFriendly2Load*,GridFriendly2Load*>::iterator iter = loads.begin();
00087 for (; iter != loads.end(); iter++)
00088 {
00089 delete (*iter).first;
00090 }
00091 }
00092