16 # pragma warning (disable: 4786)
17 # pragma warning (disable: 4243)
19 # pragma warning (disable: 4355)
67 std::ostringstream str;
69 if (this->
mNot ==
true)
75 ChoiceSetOfSets::const_iterator it = this->
mChoices.begin(), endit = this->
mChoices.end();
79 if (it->second ==
true)
85 ChoiceSet::const_iterator inner = it->first.begin(), innerend = it->first.end();
87 while (inner != innerend)
89 if (inner->second ==
true)
91 str <<
"NOT (" << inner->first->toString() <<
")";
95 str << inner->first->toString();
100 if (inner != innerend)
115 ItemSetOfSets::const_iterator it2 = this->
mAndSets.begin(), endit2 = this->
mAndSets.end();
117 while (it2 != endit2)
119 if (it2->second ==
true)
125 ItemSet::const_iterator inner = it2->first.begin(), innerend = it2->first.end();
127 while (inner != innerend)
129 if (inner->second ==
true)
131 str <<
"NOT (" << inner->first->toString() <<
")";
135 str << inner->first->toString();
140 if (inner != innerend)
204 ChoiceSetOfSets::const_iterator it = this->
mChoices.begin(), endit = this->
mChoices.end();
206 while (it != endit && result ==
true)
210 if ((*it).second ==
true)
219 ChoiceSet::iterator it2 = tmpSet.begin(), endit2 = tmpSet.end();
221 while (it2 != endit2 && result ==
true)
223 if ((*it2).second ==
true)
225 (*it2).first->negate();
228 (*it2).first->simplify();
234 assert(pLogical != NULL);
252 set1[0] = itemSetOfSets;
253 pLogical = &it2->first->getTrueExpression();
254 assert(pLogical != NULL);
272 set1[1] = itemSetOfSets;
273 pLogical =
dynamic_cast<const CNormalLogical*
>(it2->first->getCondition().copy());
275 assert(pLogical != NULL);
293 set1[2] = itemSetOfSets;
295 pLogical = &it2->first->getFalseExpression();
296 assert(pLogical != NULL);
314 set1[3] = itemSetOfSets;
322 set2[0] = itemSetOfSets;
329 set2[1] = itemSetOfSets;
355 ItemSetOfSets::const_iterator tmpIt = itemSetOfSets->begin(), tmpEndit = itemSetOfSets->end();
357 while (tmpIt != tmpEndit)
368 delete itemSetOfSets;
384 if (this->
mNot ==
true)
402 ItemSetOfSets::iterator it2 = this->
mAndSets.begin(), endit2 = this->
mAndSets.end();
407 while (it2 != endit2)
411 assert((*it2).second ==
false);
412 ItemSet::iterator it3 = (*it2).first.begin(), endit3 = (*it2).first.end();
414 while (it3 != endit3)
416 assert((*it3).second ==
false);
417 (*it3).first->simplify();
425 bool eliminate =
false;
426 it3 = (*it2).first.begin();
436 while (it3 != endit3 && eliminate ==
false)
448 ItemSet::iterator it4 = it3, endit4 = (*it2).first.end();
455 while (it4 != endit4 && eliminate ==
false)
457 pItem2 = (*it4).first;
465 if ((*pItem2) == (*pItem1))
478 if (eliminate ==
true)
483 tmpSet.insert(std::make_pair(pLogical,
false));
485 if (tmpAndSets.insert(std::make_pair(tmpSet,
false)).second ==
false)
496 if (tmpAndSets.insert(std::make_pair(tmpSet,
false)).second ==
false)
520 bool eliminate =
false;
523 while (it2 != endit2 && eliminate ==
false)
527 if ((*it2).first.size() == 1)
534 delete pLogicalItem1;
539 ItemSetOfSets::iterator it3 = it2, endit3 = this->
mAndSets.end();
542 while (it3 != endit3 && eliminate ==
false)
544 if ((*it3).first.size() == 1)
546 pLogicalItem2 = (*(*it3).first.begin()).first;
548 if ((*pLogicalItem1) == (*pLogicalItem2))
551 delete pLogicalItem1;
559 delete pLogicalItem1;
565 if (eliminate ==
true)
571 tmpSet.insert(std::make_pair(pLogical,
false));
572 this->
mAndSets.insert(std::make_pair(tmpSet,
false));
586 tmpSet.insert(std::make_pair(pFalseItem,
false));
587 std::pair<ItemSet, bool> falsePair = std::make_pair(tmpSet,
false);
594 ItemSetOfSets::iterator it2 = this->
mAndSets.begin();
595 ItemSetOfSets::iterator endit2 = this->
mAndSets.end();
599 std::pair<CNormalLogicalItem*, bool> truePair = std::make_pair(pTrueItem,
false);
601 while (it2 != endit2)
609 if (tmpSet2.size() > 1)
611 tmpSet2.erase(truePair);
614 if (tmpSet.insert(std::make_pair(tmpSet2, it2->second)).second ==
false)
677 ChoiceSetOfSets::const_iterator it = this->
mChoices.begin(), endit = this->
mChoices.end(), it2 = rhs.
mChoices.begin();
682 if (comp.
isEqual((*it), (*it2)) ==
false)
692 ItemSetOfSets::const_iterator it3 = this->
mAndSets.begin(), endit3 = this->
mAndSets.end(), it4 = rhs.
mAndSets.begin();
695 while (it3 != endit3 && result ==
true)
697 if (comp2.
isEqual((*it3), (*it4)) ==
false)
720 if (this->
mNot ==
false && rhs.
mNot ==
true)
732 ChoiceSetOfSets::const_iterator it = this->
mChoices.begin(), endit = this->
mChoices.end(), it2 = rhs.
mChoices.begin();
737 if (comp((*it), (*it2)) ==
false)
755 ItemSetOfSets::const_iterator it3 = this->
mAndSets.begin(), endit3 = this->
mAndSets.end(), it4 = rhs.
mAndSets.begin();
758 while (it3 != endit3)
760 if (comp2((*it3), (*it4)) ==
false)
854 std::map<CNormalLogicalItem, bool> truthValueMap;
855 ItemSetOfSets::const_iterator outerIt = this->
mAndSets.begin(), outerEndit = this->
mAndSets.end();
857 while (outerIt != outerEndit && result ==
true)
859 result = (outerIt->second ==
false);
860 ItemSet::const_iterator innerIt = outerIt->first.begin(), innerEndit = outerIt->first.end();
862 while (innerIt != innerEndit && result ==
true)
864 result = (innerIt->second ==
false);
865 truthValueMap[*(innerIt->first)] =
false;
872 if (truthValueMap.size() <= 16)
874 std::vector<CNormalLogicalItem> itemVector;
875 std::map<CNormalLogicalItem, bool>::const_iterator mapIt = truthValueMap.begin(), mapEndit = truthValueMap.end();
877 while (mapIt != mapEndit)
885 if (std::find(itemVector.begin(), itemVector.end(), *pNegatedItem) == itemVector.end())
887 itemVector.push_back(mapIt->first);
894 unsigned int i = 0, iMax = (1 << itemVector.size());
896 while (i < iMax && result ==
true)
900 std::bitset<16> bitSet((
unsigned long long)i);
901 unsigned int j, jMax = itemVector.size();
903 for (j = 0; j < jMax; ++j)
905 truthValueMap[itemVector[j]] = bitSet[j];
912 if (truthValueMap.find(*pNegatedItem) != truthValueMap.end())
914 truthValueMap[*pNegatedItem] = !(bitSet[j]);
928 for (j = 0; j < jMax; ++j)
932 if (bitSet[j] ==
false)
938 tmpSet.insert(std::make_pair(pItem,
false));
941 tmpAndSets.insert(std::make_pair(tmpSet,
false));
963 ItemSetOfSets::const_iterator outerIt = this->
mAndSets.begin(), outerEndit = this->
mAndSets.end();
965 while (outerIt != outerEndit)
967 ItemSet::const_iterator innerIt = outerIt->first.begin(), innerEndit = outerIt->first.end();
968 bool innerResult =
true;
970 while (innerIt != innerEndit && innerResult ==
true)
972 std::map<CNormalLogicalItem, bool>::const_iterator pos = truthValueMap.find(*(innerIt->first));
973 assert(pos != truthValueMap.end());
975 if (pos != truthValueMap.end())
977 innerResult = pos->second;
979 if (innerIt->second ==
true) innerResult = (!innerResult);
989 if (outerIt->second ==
true)
991 innerResult = (!innerResult);
994 if (innerResult ==
true)
1030 ItemSetOfSets::const_iterator outerIt = source.begin(), outerEndit = source.end();
1033 pNegatedNeutralItem->
negate();
1035 std::pair<CNormalLogicalItem*, bool> pNegatedNeutralItemPair1 = std::make_pair(&neutralItem,
false);
1036 std::pair<CNormalLogicalItem*, bool> pNegatedNeutralItemPair2 = std::make_pair(pNegatedNeutralItem,
true);
1038 while (outerIt != outerEndit)
1040 ItemSet::const_iterator innerIt = outerIt->first.begin(), innerEndit = outerIt->first.end();
1041 bool eliminate =
false;
1043 while (innerIt != innerEndit)
1045 if (outerIt->first.find(pNegatedNeutralItemPair1) != outerIt->first.end() || outerIt->first.find(pNegatedNeutralItemPair2) != outerIt->first.end())
1053 std::pair<CNormalLogicalItem*, bool> negatedPair = std::make_pair(pNegatedItem, innerIt->second);
1054 ItemSet::const_iterator pos = outerIt->first.find(negatedPair);
1056 if (pos != outerIt->first.end())
1059 delete pNegatedItem;
1065 pos = outerIt->first.find(negatedPair);
1067 if (pos != outerIt->first.end())
1070 delete pNegatedItem;
1074 delete pNegatedItem;
1084 if (innerIt->second ==
true)
1091 tmpSet.insert(std::make_pair(pTmpItem,
false));
1095 innerIt = outerIt->first.begin(), innerEndit = outerIt->first.end();
1097 while (innerIt != innerEndit)
1101 if (innerIt->second ==
true)
1107 tmpSet.insert(std::make_pair(pNewItem,
false));
1113 if (tmpSet.size() > 1)
1121 std::pair<CNormalLogicalItem*, bool> tmpPair = std::make_pair(pItem,
false);
1122 ItemSet::const_iterator p = tmpSet.find(tmpPair);
1124 if (p != tmpSet.end())
1127 tmpSet.erase(tmpPair);
1137 if (tmpTarget.insert(std::make_pair(tmpSet, outerIt->second)).second ==
false)
1149 outerIt = tmpTarget.begin();
1150 outerEndit = tmpTarget.end();
1152 while (outerIt != outerEndit)
1154 const ItemSet& sourceSet = outerIt->first;
1158 if (sourceSet.size() == 1)
1160 if ((sourceSet.begin()->first->getType() == pNegatedNeutralItem->
getType() && sourceSet.begin()->second ==
false) || (sourceSet.begin()->first->getType() == neutralItem.
getType() && sourceSet.begin()->second ==
true))
1169 neutralSet.insert(std::make_pair(pI,
false));
1171 if (target.insert(std::make_pair(neutralSet,
false)).second ==
false)
1183 tmpSet.insert(std::make_pair(pItem, outerIt->second));
1184 std::pair<ItemSet, bool> tmpPair = std::make_pair(tmpSet,
false);
1186 if (tmpTarget.find(tmpPair) != tmpTarget.end())
1195 neutralSet.insert(std::make_pair(pI,
false));
1197 if (target.insert(std::make_pair(neutralSet,
false)).second ==
false)
1210 if (target.insert(std::make_pair(tmpSet2, outerIt->second)).second ==
false)
1224 if (target.insert(std::make_pair(tmpSet2, outerIt->second)).second ==
false)
1235 delete pNegatedNeutralItem;
1237 if (target.size() > 1)
1242 neutralSet.insert(std::make_pair(pI,
false));
1243 std::pair<ItemSet, bool> neutralPair = std::make_pair(neutralSet,
false);
1244 ItemSetOfSets::const_iterator pos = target.find(neutralPair);
1246 if (pos != target.end())
1249 target.erase(neutralPair);
1254 neutralPair = std::make_pair(neutralSet,
true);
1255 pos = target.find(neutralPair);
1257 if (pos != target.end())
1260 target.erase(neutralPair);
1270 void CNormalLogical::printSetOfSets(
const ItemSetOfSets& set)
1272 ItemSetOfSets::const_iterator outerIt = set.begin(), outerEndit = set.end();
1274 while (outerIt != outerEndit)
1276 ItemSet::const_iterator innerIt = outerIt->first.begin(), innerEndit = outerIt->first.end();
1278 while (innerIt != innerEndit)
1280 std::cout << innerIt->first->
toString() <<
" ^ ";
1284 std::cout << std::endl;
1289 void CNormalLogical::printSetSizes(
const ItemSetOfSets& set)
1291 ItemSetOfSets::const_iterator outerIt = set.begin(), outerEndit = set.end();
1292 std::cout <<
"Number of Sets: " << set.size() << std::endl;
1294 while (outerIt != outerEndit)
1296 std::cout <<
" Number of elements in set: " << outerIt->first.size() << std::endl;
1301 void CNormalLogical::printSetElement(
const ItemSetOfSets& set,
unsigned int index1,
unsigned int index2)
1303 if (index1 >= set.size())
return;
1305 ItemSetOfSets::const_iterator outerIt = set.begin();
1314 if (index2 >= outerIt->first.size())
return;
1317 ItemSet::const_iterator innerIt = outerIt->first.begin();
1325 std::cout << innerIt->first->toString();
1327 #endif // COPASI_DEBUG
static bool negateSets(const TemplateSet< TYPE > &source, TemplateSet< TYPE > &target)
ChoiceSetOfSets & getChoices()
virtual std::string toString() const
static void eliminateNullItems(const ItemSetOfSets &source, ItemSetOfSets &target, bool orSet)
CNormalLogical & operator=(const CNormalLogical &src)
static void cleanSetOfSets(TemplateSetOfSets< TYPE > &s)
bool evaluateExpression(const std::map< CNormalLogicalItem, bool > &truthValueMap) const
TemplateSetOfSets< CNormalLogicalItem > ItemSetOfSets
bool generateCanonicalDNF(ItemSetOfSets &tmpAndSet) const
virtual std::string toString() const
virtual ~CNormalLogical()
void setAndSets(const ItemSetOfSets &set)
static bool negateSetOfSets(const TemplateSetOfSets< TYPE > &source, TemplateSetOfSets< TYPE > &target)
static bool convertAndOrToOrAnd(const TemplateSetOfSets< TYPE > &source, TemplateSetOfSets< TYPE > &target)
void setChoices(const ChoiceSetOfSets &set)
static void cleanSet(const TemplateSet< TYPE > &s)
bool isEqual(const std::pair< TemplateSet< TYPE >, bool > &lhs, const std::pair< TemplateSet< TYPE >, bool > &rhs) const
static void copySetOfSets(const TemplateSetOfSets< TYPE > &source, TemplateSetOfSets< TYPE > &target)
bool operator<(const CNormalLogical &rhs) const
void setIsNegated(bool negate)
virtual CNormalBase * copy() const
static void copySet(const TemplateSet< TYPE > &source, TemplateSet< TYPE > &target)
std::ostream & operator<<(std::ostream &os, const CNormalLogical &logical)
bool operator==(const CNormalLogical &rhs) const
ItemSetOfSets & getAndSets()