00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #ifndef LEARNWTS_H_NOV_23_2005
00067 #define LEARNWTS_H_NOV_23_2005
00068
00069 #include <sys/time.h>
00070 #include "util.h"
00071 #include "timer.h"
00072 #include "fol.h"
00073 #include "mln.h"
00074 #include "indextranslator.h"
00075
00076
00077 extern const char* ZZ_TMP_FILE_POSTFIX;
00078 const bool DOMAINS_SHARE_DATA_STRUCT = true;
00079
00087 void extractFileNames(const char* const & namesStr, Array<string>& namesArray)
00088 {
00089 if (namesStr == NULL) return;
00090 string s(namesStr);
00091 s = Util::trim(s);
00092 if (s.length() == 0) return;
00093 s.append(",");
00094 string::size_type cur = 0;
00095 string::size_type comma;
00096 string name;
00097 while (true)
00098 {
00099 comma = s.find(",", cur);
00100 if (comma == string::npos) return;
00101 name = s.substr(cur, comma-cur);
00102 namesArray.append(name);
00103 cur = comma+1;
00104 }
00105 }
00106
00107 void createDomainAndMLN(Array<Domain*>& domains, Array<MLN*>& mlns,
00108 const string& inMLNFile, ostringstream& constFiles,
00109 ostringstream& dbFiles,
00110 const StringHashArray* const & nonEvidPredNames,
00111 const bool& addUnitClauses, const double& priorMean,
00112 const bool& checkPlusTypes, const bool& mwsLazy,
00113 const bool& allPredsExceptQueriesAreCW,
00114 const StringHashArray* const & owPredNames)
00115 {
00116 string::size_type bslash = inMLNFile.rfind("/");
00117 string tmp = (bslash == string::npos) ?
00118 inMLNFile:inMLNFile.substr(bslash+1,inMLNFile.length()-bslash-1);
00119 char tmpInMLN[100];
00120 sprintf(tmpInMLN, "%s%s", tmp.c_str(), ZZ_TMP_FILE_POSTFIX);
00121
00122 ofstream out(tmpInMLN);
00123 ifstream in(inMLNFile.c_str());
00124 if (!out.good()) { cout<<"ERROR: failed to open "<<tmpInMLN <<endl; exit(-1);}
00125 if (!in.good()) { cout<<"ERROR: failed to open "<<inMLNFile<<endl; exit(-1);}
00126
00127 string buffer;
00128 while(getline(in, buffer)) out << buffer << endl;
00129 in.close();
00130
00131 out << constFiles.str() << endl
00132 << dbFiles.str() << endl;
00133 out.close();
00134
00135
00136 Domain* domain = new Domain;
00137 MLN* mln = new MLN();
00138
00139
00140
00141 bool warnAboutDupGndPreds = true;
00142 bool mustHaveWtOrFullStop = false;
00143 bool flipWtsOfFlippedClause = false;
00144 Domain* domain0 = (checkPlusTypes) ? domains[0] : NULL;
00145
00146 bool ok = runYYParser(mln, domain, tmpInMLN, allPredsExceptQueriesAreCW,
00147 owPredNames, nonEvidPredNames, addUnitClauses,
00148 warnAboutDupGndPreds, priorMean, mustHaveWtOrFullStop,
00149 domain0, mwsLazy, flipWtsOfFlippedClause);
00150
00151 if (!ok) { unlink(tmpInMLN); exit(-1); }
00152 domains.append(domain);
00153 mlns.append(mln);
00154 unlink(tmpInMLN);
00155 }
00156
00157
00158 void createDomainsAndMLNs(Array<Domain*>& domains, Array<MLN*>& mlns,
00159 const bool& multipleDatabases,
00160 const string& inMLNFile,
00161 const Array<string>& constFilesArr,
00162 const Array<string>& dbFilesArr,
00163 const StringHashArray* const & nonEvidPredNames,
00164 const bool& addUnitClauses, const double& priorMean,
00165 const bool& mwsLazy,
00166 const bool& allPredsExceptQueriesAreCW,
00167 const StringHashArray* const & owPredNames)
00168 {
00169 if (!multipleDatabases)
00170 {
00171 ostringstream constFilesStream, dbFilesStream;
00172 for (int i = 0; i < constFilesArr.size(); i++)
00173 constFilesStream << "#include \"" << constFilesArr[i] << "\"" << endl;
00174 for (int i = 0; i < dbFilesArr.size(); i++)
00175 dbFilesStream << "#include \"" << dbFilesArr[i] << "\"" << endl;
00176 createDomainAndMLN(domains, mlns, inMLNFile, constFilesStream,
00177 dbFilesStream, nonEvidPredNames,
00178 addUnitClauses, priorMean, false, mwsLazy,
00179 allPredsExceptQueriesAreCW, owPredNames);
00180 }
00181 else
00182 {
00183 for (int i = 0; i < dbFilesArr.size(); i++)
00184 {
00185 cout << "parsing MLN and creating domain " << i << "..." << endl;
00186 ostringstream constFilesStream, dbFilesStream;
00187 if (constFilesArr.size() > 0)
00188 constFilesStream << "#include \"" << constFilesArr[i] << "\"" << endl;
00189 dbFilesStream << "#include \"" << dbFilesArr[i] << "\"" << endl;
00190
00191 bool checkPlusTypes = (i > 0);
00192
00193 createDomainAndMLN(domains, mlns, inMLNFile, constFilesStream,
00194 dbFilesStream, nonEvidPredNames,
00195 addUnitClauses, priorMean, checkPlusTypes, mwsLazy,
00196 allPredsExceptQueriesAreCW, owPredNames);
00197
00198
00199 if (DOMAINS_SHARE_DATA_STRUCT && i > 0)
00200 {
00201 const ClauseHashArray* carr = mlns[i]->getClauses();
00202 for (int j = 0; j < carr->size(); j++)
00203 {
00204 Clause* c = (*carr)[j];
00205 for (int k = 0; k < c->getNumPredicates(); k++)
00206 {
00207 Predicate* p = c->getPredicate(k);
00208 const PredicateTemplate* t
00209 = domains[0]->getPredicateTemplate(p->getName());
00210 assert(t);
00211 p->setTemplate((PredicateTemplate*)t);
00212 }
00213 }
00214
00215 ((Domain*)domains[i])->replaceTypeDualMap((
00216 DualMap*)domains[0]->getTypeDualMap());
00217 ((Domain*)domains[i])->replaceStrToPredTemplateMapAndPredDualMap(
00218 (StrToPredTemplateMap*) domains[0]->getStrToPredTemplateMap(),
00219 (DualMap*) domains[0]->getPredDualMap());
00220 ((Domain*)domains[i])->replaceStrToFuncTemplateMapAndFuncDualMap(
00221 (StrToFuncTemplateMap*) domains[0]->getStrToFuncTemplateMap(),
00222 (DualMap*) domains[0]->getFuncDualMap());
00223 ((Domain*)domains[i])->replaceEqualPredTemplate(
00224 (PredicateTemplate*)domains[0]->getEqualPredTemplate());
00225 ((Domain*)domains[i])->replaceFuncSet(
00226 (FunctionSet*)domains[0]->getFuncSet());
00227 }
00228
00229 }
00230 }
00231
00232
00233
00234
00235
00236
00237 }
00238
00239 void assignWtsAndOutputMLN(ostream& out, Array<MLN*>& mlns,
00240 Array<Domain*>& domains, const Array<double>& wts,
00241 IndexTranslator* const& indexTrans)
00242 {
00243
00244
00245 double* wwts = (double*) wts.getItems();
00246 indexTrans->assignNonTiedClauseWtsToMLNs(++wwts);
00247
00248
00249
00250 out << "//predicate declarations" << endl;
00251 domains[0]->printPredicateTemplates(out);
00252 out << endl;
00253
00254
00255 out << "//function declarations" << endl;
00256 domains[0]->printFunctionTemplates(out);
00257 out << endl;
00258
00259 mlns[0]->printMLNNonExistFormulas(out, domains[0]);
00260
00261 const ClauseHashArray* clauseOrdering = indexTrans->getClauseOrdering();
00262 const StringHashArray* exFormOrdering = indexTrans->getExistFormulaOrdering();
00263 for (int i = 0; i < exFormOrdering->size(); i++)
00264 {
00265
00266 out.width(0); out << "// "; out.width(6);
00267 out << wts[1+clauseOrdering->size()+i] << " " <<(*exFormOrdering)[i]<<endl;
00268 out << wts[1+clauseOrdering->size()+i] << " " <<(*exFormOrdering)[i]<<endl;
00269 out << endl;
00270 }
00271 }
00272
00273
00274 void assignWtsAndOutputMLN(ostream& out, Array<MLN*>& mlns,
00275 Array<Domain*>& domains, const Array<double>& wts)
00276 {
00277
00278 for (int i = 0; i < mlns.size(); i++)
00279 {
00280 MLN* mln = mlns[i];
00281 const ClauseHashArray* clauses = mln->getClauses();
00282 for (int i = 0; i < clauses->size(); i++)
00283 (*clauses)[i]->setWt(wts[i+1]);
00284 }
00285
00286
00287 out << "//predicate declarations" << endl;
00288 domains[0]->printPredicateTemplates(out);
00289 out << endl;
00290
00291
00292 out << "//function declarations" << endl;
00293 domains[0]->printFunctionTemplates(out);
00294 out << endl;
00295 mlns[0]->printMLN(out, domains[0]);
00296 }
00297
00298
00299 void deleteDomains(Array<Domain*>& domains)
00300 {
00301 for (int i = 0; i < domains.size(); i++)
00302 {
00303 if (DOMAINS_SHARE_DATA_STRUCT && i > 0)
00304 {
00305 ((Domain*)domains[i])->setTypeDualMap(NULL);
00306 ((Domain*)domains[i])->setStrToPredTemplateMapAndPredDualMap(NULL, NULL);
00307 ((Domain*)domains[i])->setStrToFuncTemplateMapAndFuncDualMap(NULL, NULL);
00308 ((Domain*)domains[i])->setEqualPredTemplate(NULL);
00309 ((Domain*)domains[i])->setFuncSet(NULL);
00310 }
00311 delete domains[i];
00312 }
00313 }
00314
00315
00316 #endif