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 #include "util.h"
00067 #include "mrf.h"
00068 #include "superclause.h"
00069 #include "superpred.h"
00070
00071
00072 bool SuperPred::isFirstAccessForStaticVars_ = true;
00073 Array<IntArrayToSuperPredMap*>* SuperPred::constantsToSuperPredArr_ = NULL;
00074 Array<Array<SuperPred*>*>* SuperPred::superPredsArr_ = NULL;
00075
00076
00077 void createSuperPreds(Array<Array<SuperClause*>*>* const & superClausesArr,
00078 Domain* const & domain)
00079 {
00080 IntHashArray seenPredIds;
00081 Array<SuperClause *> * superClauses;
00082
00083 int domainPredCnt = domain->getNumPredicates();
00084 Array<IntArrayToPredicateConstantsInfoMap*>* pconstantsToPCInfoArr =
00085 new Array<IntArrayToPredicateConstantsInfoMap*>();
00086 pconstantsToPCInfoArr->growToSize(domainPredCnt);
00087 IntArrayToPredicateConstantsInfoMap * pconstantsToPCInfo;
00088 IntArrayToPredicateConstantsInfoMap::iterator iaToPCInfoItr;
00089
00090
00091 for (int i = 0; i < domainPredCnt; i++)
00092 {
00093 (*pconstantsToPCInfoArr)[i] = new IntArrayToPredicateConstantsInfoMap();
00094 }
00095
00096 Array<int> *pconstants;
00097 Clause* clause;
00098 Predicate *pred;
00099 SuperClause *superClause;
00100 PredicateConstantsInfo *pcInfo;
00101 ClauseCounter *clauseCounter;
00102 int superClauseCnt;
00103
00104 int prevCnt = 0;
00105
00106 for (int arrIndex = 0; arrIndex < superClausesArr->size(); arrIndex++)
00107 {
00108 superClauses = (*superClausesArr)[arrIndex];
00109 superClauseCnt = superClauses->size();
00110
00111 for (int scindex = 0; scindex < superClauseCnt; scindex++)
00112 {
00113 superClause = (*superClauses)[scindex];
00114 clause = superClause->getClause();
00115 int predCnt = clause->getNumPredicates();
00116
00117
00118 int numTuples = superClause->getNumTuples();
00119 for (int tindex = 0; tindex < numTuples; tindex++)
00120 {
00121
00122 double tcnt = superClause->getTupleCount(tindex);
00123
00124
00125 int implicitCnt;
00126 for (int pindex = 0; pindex < predCnt; pindex++)
00127 {
00128 pred = clause->getPredicate(pindex);
00129 pconstants = superClause->getPredicateConstants(tindex, pred);
00130 implicitCnt = superClause->getImplicitCountJoiningWithPred(tindex,
00131 pred);
00132 int predId = domain->getPredicateId(pred->getName());
00133 seenPredIds.append(predId);
00134 pconstantsToPCInfo = (*pconstantsToPCInfoArr)[predId];
00135 iaToPCInfoItr = pconstantsToPCInfo->find(pconstants);
00136 if (iaToPCInfoItr == pconstantsToPCInfo->end())
00137 {
00138 int superPredId = SuperPred::getSuperPredId(pconstants, predId);
00139 clauseCounter = new ClauseCounter();
00140 pcInfo = new PredicateConstantsInfo(clauseCounter, superPredId);
00141 (*pconstantsToPCInfo)[pconstants] = pcInfo;
00142 }
00143 else
00144 {
00145
00146 delete pconstants;
00147 pcInfo = iaToPCInfoItr->second;
00148 clauseCounter = pcInfo->getClauseCounter();
00149 }
00150 clauseCounter->incrementCount(scindex + prevCnt, pindex, predCnt,
00151 tcnt * implicitCnt);
00152 }
00153 }
00154 }
00155 prevCnt += superClauseCnt;
00156 }
00157
00158
00159 ClauseCounterToSuperPredMap * clauseCounterToSuperPred;
00160 ClauseCounterToSuperPredMap::iterator ocToSpItr;
00161 SuperPred *superPred;
00162
00163 int parentSuperPredId;
00164
00165
00166 SuperPred::clear(domainPredCnt);
00167
00168 for (int predId = 0; predId < domainPredCnt; predId++)
00169 {
00170
00171
00172 if (seenPredIds.find(predId) < 0)
00173 continue;
00174
00175 clauseCounterToSuperPred = new ClauseCounterToSuperPredMap();
00176 pconstantsToPCInfo = (*pconstantsToPCInfoArr)[predId];
00177
00178 for (iaToPCInfoItr = pconstantsToPCInfo->begin();
00179 iaToPCInfoItr != pconstantsToPCInfo->end();
00180 iaToPCInfoItr++)
00181 {
00182 pconstants = iaToPCInfoItr->first;
00183 pcInfo = iaToPCInfoItr->second;
00184 parentSuperPredId = pcInfo->getSuperPredId();
00185 clauseCounter = pcInfo->getClauseCounter();
00186 ocToSpItr = clauseCounterToSuperPred->find(clauseCounter);
00187 if (ocToSpItr == clauseCounterToSuperPred->end())
00188 {
00189 superPred = new SuperPred(predId, clauseCounter, parentSuperPredId);
00190 (*clauseCounterToSuperPred)[clauseCounter] = superPred;
00191 }
00192 else
00193 {
00194 delete clauseCounter;
00195 superPred = ocToSpItr->second;
00196 }
00197 superPred->addConstantTuple(pconstants, predId);
00198 }
00199 clauseCounterToSuperPred->clear();
00200 delete clauseCounterToSuperPred;
00201 }
00202
00203
00204 for (int predId = 0; predId < domainPredCnt; predId++)
00205 {
00206 pconstantsToPCInfo = (*pconstantsToPCInfoArr)[predId];
00207 pconstantsToPCInfo->clear();
00208 delete pconstantsToPCInfo;
00209 }
00210 delete pconstantsToPCInfoArr;
00211 }
00212