00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 #include "AlgObj.h"
00022 #include "DataObj.h"
00023 #include "SourceObj.h"
00024 
00025 
00026 
00027 
00028 AlgObj::AlgObj()
00029 :AlgObjIF(),
00030  _inputs(0),
00031  _nInputs(0)
00032 {
00033 
00034 }
00035 
00036 AlgObj::AlgObj(unsigned int numInputs)
00037 :AlgObjIF(),
00038  _inputs(0),
00039  _nInputs(0)
00040 {
00041    setNumInputs(numInputs);
00042 }
00043 
00044 
00045 AlgObj::~AlgObj()
00046 {
00047    if(_inputs)
00048       delete[] _inputs;
00049 }
00050 
00051 
00052 
00053 
00054 
00055 
00056 
00057 
00058 
00059 
00060 void AlgObj::execute()
00061 {
00062    
00063 
00064 
00065 
00066    if(!checkInputs())
00067    {
00068       derr("execute(), inputs not ready!!");
00069    }
00070 
00071    
00072 
00073    
00074    
00075    
00076    
00077    
00078    if(inputsModified()) _modified = true;
00079 
00080    if(_modified)
00081    {
00082       
00083       execDef();
00084       _modified = false;
00085    }
00086 }
00087 
00088 
00089 
00090 
00091 void AlgObj::force()
00092 {
00093    if(!checkInputs())
00094    {
00095       derr("force(), inputs not ready, deffering execution");
00096       _modified = true;
00097       return;
00098    }
00099    execDef();
00100    _modified = false;
00101    _forward = false;
00102 }
00103 
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111 
00112 
00113 void AlgObj::update_(DataObjSP d)
00114 {
00115 
00116    if(!_forward) return;
00117    _modified = true;
00118    if(!checkInputs())
00119    {
00120       
00121       return;
00122    }
00123    execute();
00124 
00125    _forward = false;
00126 }
00127 
00128 
00129 
00130 
00131 
00132 
00133 
00134 
00135 
00136 
00137 bool AlgObj::inputsModified()
00138 {
00139    for(int i=0; i<_nInputs; ++i)
00140    {
00141       if(!_inputs[i].isNull())
00142       {  
00143          if(_inputs[i]->isModified())
00144          {
00145             derr("inputs modified()");
00146             return true;
00147          }
00148       }
00149    }
00150    return false;
00151 }
00152 
00153 
00154 
00155 
00156 bool AlgObj::checkInputs()
00157 {
00158   
00159    for(int i=0; i<_nInputs; ++i) 
00160    {
00161       
00162       if(!_inputs[i].isNull()) 
00163       {
00164          
00165          if(!_inputs[i]->request_()) 
00166          {
00167             return false;  
00168          }
00169       }
00170       else 
00171       {
00172          derr("request(), null DataObj encountered");
00173       }
00174    }
00175 
00176    return true;
00177 }
00178 
00179 
00180 
00181 
00182 void AlgObj::addInput(DataObjSP d)
00183 {
00184    if(d.isNull())
00185    {
00186       derr("addInput(), attempting to add NULL input");
00187       return;
00188    }
00189    setInputN(_nInputs, d);
00190 }
00191 
00192 
00193 
00194 
00195 void AlgObj::delInput(DataObjSP d)
00196 {
00197    if(!isInput(d))
00198    {
00199       derr("delInput(), DataObject is not input");
00200       return;
00201    }
00202    
00203    DataObjSP *objNew = new DataObjSP[_nInputs - 1];
00204    int idx = 0;
00205    for(int i=0; i<_nInputs; ++i)
00206    {
00207       if(d != _inputs[i])
00208       {
00209          objNew[idx] = _inputs[i];
00210          ++idx;
00211       }
00212    }
00213 
00214    d->delConsumer(SimModObjSP(this));
00215 
00216    delete[] _inputs;
00217    _inputs = objNew;
00218    --_nInputs;
00219 }
00220 
00221 
00222 
00223 
00224 bool AlgObj::isInput(const DataObjSP d) const
00225 {
00226    if(d.isNull()) return false;
00227    for(int i=0; i<_nInputs; ++i)
00228    {
00229       if(d == _inputs[i]) return true;
00230    }
00231    return false;
00232 }
00233 
00234 
00235 
00236 
00237 void AlgObj::setNumInputs(int nInputs)
00238 {
00239    if(nInputs == _nInputs) 
00240    {
00241       return;
00242    }
00243 
00244    DataObjSP *inputsNew = new DataObjSP[nInputs];
00245    
00246 
00247    for(int i=0; i<nInputs && i<_nInputs; ++i)
00248    {
00249       inputsNew[i] = _inputs[i];
00250    }
00251 
00252 
00253    delete[] _inputs;
00254 
00255 
00256    _inputs = inputsNew;
00257    _nInputs = nInputs;
00258 
00259 }
00260 
00261 
00262 
00263 
00264 void AlgObj::setInputN(int inNum, DataObjSP d)
00265 {
00266    if(d.isNull())
00267    {
00268       derr("setInputN(), attempting to set a null input!!");
00269       return;
00270    }
00271    if(inNum >= _nInputs)
00272    {
00273       setNumInputs(_nInputs + 1);
00274    }
00275 
00276    _inputs[inNum] = d;
00277    d->addConsumer(SimModObjSP(this));
00278 }
00279 
00280 
00281 
00282 
00283 DataObjIF *AlgObj::getInputN(int inNum)
00284 {
00285    if(inNum >= _nInputs)
00286    {
00287       derr("getInputN(), index number greater than number of inputs");
00288       return 0;
00289    }
00290    return _inputs[inNum].getPtr();
00291 }
00292 
00293