3 InferenceList::InferenceList() {
4 list = new ModelList<Inference*>;
7 int InferenceList::getSize() {
11 void InferenceList::pop_back() {
15 Inference* InferenceList::back() {
19 void InferenceList::push_back(Inference *infer) {
20 list->push_back(infer);
23 void InferenceList::pop_front() {
27 /** We should not call this function too often because we want a nicer
28 * abstraction of the list of inferences. So far, it will only be called in
29 * the functions in InferenceSet */
30 ModelList<Inference*>* InferenceList::getList() {
34 bool InferenceList::applyPatch(Inference *curInfer, Inference *newInfer, Patch *patch) {
35 bool canUpdate = true,
38 for (int i = 0; i < patch->getSize(); i++) {
41 PatchUnit *unit = patch->get(i);
42 newInfer->strengthen(unit->getAct(), unit->getMO(), canUpdate, hasUpdated);
44 // This is not a feasible patch, bail
46 } else if (hasUpdated) {
57 void InferenceList::applyPatch(Inference *curInfer, Patch* patch) {
59 Inference *newInfer = new Inference(curInfer);
60 if (!applyPatch(curInfer, newInfer, patch)) {
63 list->push_back(newInfer);
66 ModelList<Inference*> *newList = new ModelList<Inference*>;
67 for (ModelList<Inference*>::iterator it = list->begin(); it !=
69 Inference *oldInfer = *it;
70 Inference *newInfer = new Inference(oldInfer);
71 if (!applyPatch(curInfer, newInfer, patch)) {
74 newList->push_back(newInfer);
78 for (ModelList<Inference*>::iterator it = list->begin(); it !=
87 void InferenceList::applyPatch(Inference *curInfer, SnapVector<Patch*> *patches) {
89 for (unsigned i = 0; i < patches->size(); i++) {
90 Inference *newInfer = new Inference(curInfer);
91 Patch *patch = (*patches)[i];
92 if (!applyPatch(curInfer, newInfer, patch)) {
95 list->push_back(newInfer);
99 ModelList<Inference*> *newList = new ModelList<Inference*>;
100 for (ModelList<Inference*>::iterator it = list->begin(); it !=
102 Inference *oldInfer = *it;
103 for (unsigned i = 0; i < patches->size(); i++) {
104 Inference *newInfer = new Inference(oldInfer);
105 Patch *patch = (*patches)[i];
106 if (!applyPatch(curInfer, newInfer, patch)) {
109 newList->push_back(newInfer);
113 // Clean the old list
114 for (ModelList<Inference*>::iterator it = list->begin(); it !=
123 /** Append another list to this list */
124 bool InferenceList::append(InferenceList *inferList) {
127 ModelList<Inference*> *l = inferList->list;
128 list->insert(list->end(), l->begin(), l->end());
132 /** Only choose the weakest existing candidates & they must be stronger than the
133 * current inference */
134 void InferenceList::pruneCandidates(Inference *curInfer) {
135 ModelList<Inference*> *newCandidates = new ModelList<Inference*>(),
138 ModelList<Inference*>::iterator it1, it2;
140 for (it1 = candidates->begin(); it1 != candidates->end(); it1++) {
141 Inference *cand = *it1;
142 compVal = cand->compareTo(curInfer);
144 // If as strong as curInfer, bail
148 // Check if the cand is any stronger than those in the newCandidates
149 for (it2 = newCandidates->begin(); it2 != newCandidates->end(); it2++) {
150 Inference *addedInfer = *it2;
151 compVal = addedInfer->compareTo(cand);
152 if (compVal == 0 || compVal == 1) { // Added inference is stronger
154 it2 = newCandidates->erase(it2);
158 // Now push the cand to the list
159 newCandidates->push_back(cand);
162 list = newCandidates;
165 void InferenceList::clearAll() {
169 void InferenceList::clearList() {
173 void InferenceList::clearAll(ModelList<Inference*> *l) {
174 for (ModelList<Inference*>::iterator it = l->begin(); it !=
176 Inference *infer = *it;
182 void InferenceList::clearAll(InferenceList *inferList) {
183 clearAll(inferList->list);
186 void InferenceList::print(InferenceList *inferList, const char *msg) {
187 print(inferList->getList(), msg);
190 void InferenceList::print(ModelList<Inference*> *inferList, const char *msg) {
191 for (ModelList<Inference*>::iterator it = inferList->begin(); it !=
192 inferList->end(); it++) {
193 int idx = distance(inferList->begin(), it);
194 Inference *infer = *it;
195 model_print("%s %d:\n", msg, idx);
201 void InferenceList::print() {
202 print(list, "Inference");
205 void InferenceList::print(const char *msg) {