Go to the documentation of this file.00001
00002 #include <algorithm>
00003 #include "Function.h"
00004
00005 Function::Function()
00006 {
00007 }
00008 void Function::setVNameCurr(string soa) {
00009 vnameCurr = soa;
00010 }
00011
00012 void Function::setParents(vector<string> p) {
00013 parents = p;
00014 }
00015
00016 string Function::getVNameCurr() const{
00017 return vnameCurr;
00018 }
00019
00020 vector<string> Function::getParents() const{
00021 return parents;
00022 }
00023
00024 void Function::initSparseTables(map<string, StateObsAct*>* mymap) {
00025
00026
00027 vector<string> cIheader, uIheader;
00028 vector<int> numCIValues, numUIValues;
00029 for (unsigned int i = 0; i < parents.size(); i++) {
00030 cIheader.push_back(parents[i]);
00031 StateObsAct* soa = (*mymap)[parents[i]];
00032 if (soa != NULL)
00033 numCIValues.push_back(soa->getValueEnum().size());
00034 else
00035 numCIValues.push_back(1);
00036 }
00037 uIheader.push_back(vnameCurr);
00038 StateObsAct* soa = (*mymap)[vnameCurr];
00039 numUIValues.push_back(soa->getValueEnum().size());
00040
00041 sparseT = SharedPointer<SparseTable>(new SparseTable(cIheader, uIheader, numCIValues, numUIValues));
00042 }
00043
00044
00045 void Function::fillTables(map<string, StateObsAct*>& mymap,
00046 const vector<string>& insttokens, const vector<double>& probttokensdouble) {
00047 bool simple = true;
00048 bool seestar = false;
00049
00050
00051
00052 for (unsigned int i = 0; i < insttokens.size() && !seestar; i++) {
00053
00054 if (insttokens[i] == "*" || insttokens[i] == "-") {
00055 seestar = true;
00056 simple = false;
00057 vector<string> allCombinations;
00058 if (i != insttokens.size() - 1) {
00059 if (mymap.find(parents[i]) != mymap.end()) {
00060 allCombinations = mymap[parents[i]]->getValueEnum();
00061 } else {
00062 cerr<<"Couldn't find mapping for:"<<parents[i]<<endl;
00063 cerr<<"Please check XML file again."<<endl;
00064 exit(-1);
00065 }
00066 } else {
00067 if (mymap.find(vnameCurr) != mymap.end()) {
00068 allCombinations = mymap[vnameCurr]->getValueEnum();
00069 } else {
00070 cerr<<"Couldn't find mapping for:"<<vnameCurr<<endl;
00071 cerr<<"Please check XML file again."<<endl;
00072 exit(-1);
00073 }
00074 }
00075
00076
00077 for (unsigned int j = 0; j < allCombinations.size(); j++) {
00078
00079
00080 vector<string> temp = insttokens;
00081 string s(allCombinations[j]);
00082 temp.erase(temp.begin() + i);
00083 temp.insert(temp.begin() + i, s);
00084
00085
00086
00087 if (insttokens[i] == "-") {
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 vector<double> st = splitTable(probttokensdouble, allCombinations.size(), j);
00101 fillTables(mymap, temp, st);
00102 } else {
00103 fillTables(mymap, temp, probttokensdouble);
00104 }
00105 }
00106 }
00107 }
00108
00109 if (probttokensdouble.size() > 1)
00110 simple = false;
00111
00112
00113 if (simple) {
00114
00115
00116
00117 string myself = insttokens.back();
00118
00119
00120 vector<string> new_insttokens;
00121 new_insttokens.resize(insttokens.size() - 1);
00122 copy(insttokens.begin(), insttokens.end()-1, new_insttokens.begin());
00123
00124 simpleSparseInsert(mymap, new_insttokens, myself, probttokensdouble.front());
00125 }
00126 }
00127
00128 vector<double> Function::splitTable(const vector<double>& probttokensdouble, int size,
00129 int position) {
00130 vector<double> st;
00131
00132 int factor = probttokensdouble.size() / size;
00133
00134 for (int i = position * factor; i < position * factor + factor; i++) {
00135 st.push_back(probttokensdouble[i]);
00136 }
00137 return st;
00138 }
00139
00140 void Function::simpleSparseInsert(map<string, StateObsAct*>& mymap, const vector<
00141 string>& insttokens, const string& myself, double prob) {
00142
00143 SparseEntry st;
00144 int position = 0;
00145
00146 vector<int> ci;
00147 for (unsigned int i = 0; i < insttokens.size(); i++) {
00148 position = mymap[parents[i]]->getPosition(insttokens[i]);
00149 ci.push_back(position);
00150 }
00151
00152 UniqueIndex ui;
00153 ui.index = mymap[vnameCurr]->getPosition(myself);
00154 ui.value = prob;
00155 st.uniqueIndex.push_back(ui);
00156
00157 sparseT->add(ci, st);
00158 }
00159
00160 bool Function::checkNoMissingEntries(map<string, StateObsAct*> mymap, string& info) {
00161 stringstream ssinfo;
00162 vector<int> commonIndex;
00163 if (!sparseT->checkNoMissingEntries(commonIndex))
00164 {
00165 ssinfo << " Transition not specified for instance ";
00166 for(int i=0;i<commonIndex.size();i++){
00167 if(getParents()[i]!="null"){
00168 StateObsAct* soa = mymap[getParents()[i]];
00169 ssinfo << soa->getValueEnum()[commonIndex[i]] << " ";
00170 }else{
00171 ssinfo << "null" << endl;
00172 }
00173 }
00174 ssinfo << endl;
00175 info = ssinfo.str();
00176 return false;
00177 }
00178 else
00179 return true;
00180 }
00181
00182
00183 std::ostream& Function::write(std::ostream& out) {
00184 out << "\nvnameCurr :" << vnameCurr << endl;
00185 out << "parents: " << endl;
00186
00187 for (unsigned int i = 0; i < parents.size(); i++) {
00188 out << parents[i] << ",";
00189 }
00190 sparseT->write(out);
00191 return out;
00192 }