From 78fc212489b573f70e9ca515fa57b27ed395dc7f Mon Sep 17 00:00:00 2001 From: DjWarmonger Date: Sat, 5 Feb 2011 18:24:32 +0000 Subject: [PATCH] A little more work on Expert System. --- AI/GeniusAI/ExpertSystem.cpp | 38 ++++++++++++++++++++++-------------- AI/GeniusAI/ExpertSystem.h | 37 ++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/AI/GeniusAI/ExpertSystem.cpp b/AI/GeniusAI/ExpertSystem.cpp index 2bff9171a..7d22d06fb 100644 --- a/AI/GeniusAI/ExpertSystem.cpp +++ b/AI/GeniusAI/ExpertSystem.cpp @@ -14,9 +14,9 @@ template template void ExpertSystemShell::DataDrivenReasoning(runType type) { - std::set::iterator ir; + std::vector::iterator ir; std::list::iterator iF; - std::set>::iterator ic; + std::vector>::iterator ic; bool factWasAdded = false; //carry it over inner loop switch (type) { @@ -72,7 +72,7 @@ template template void Exper } void BonusRule::fireRule() { - for (std::set>::iterator it = cons.begin(); it != cons.end(); it++) + for (std::vector>::iterator it = cons.begin(); it != cons.end(); it++) { switch (it->first.parameter) { //compare fact with condition @@ -114,18 +114,26 @@ TLogic operator&&(const TLogic &first, const TLogic &second) { return LogicConjunction(first, second); } -//TODO: find out why it does not compile -//template void Rule::refreshRule(std::set &conditionSet) -//{ -// cons.clear(); -// for (std::set::iterator it = conditionSet.begin(); it != conditionSet.end(); it++) -// cons.insert(std::make_pair(*it, NULL)); //pointer to condition and null fact -//} -//template void Rule::refreshRule() -//{ -// for (std::set>::iterator it = cons.begin(); it != cons.end(); it++) -// *it->second = NULL; -//} +TLogic operator||(const TLogic &first, const TLogic &second) +{ + return LogicAlternative(first, second); +} +template void Rule::refreshRule(std::vector &conditionSet) +{//replace conditions with new vector + cons.clear(); + std::pair para; + para.second = NULL; + for (std::vector::iterator it = conditionSet.begin(); it != conditionSet.end(); it++) + { + para.first = *it; + cons.push_back(para); //pointer to condition and null fact + } +} +template void Rule::refreshRule() +{//clear matching facts + for (std::vector>::iterator it = cons.begin(); it != cons.end(); it++) + it->second = NULL; +} bool BonusCondition::matchesFact(Bonus &fact) { if (object(fact)) //Bonus(fact) matches local Selector(object) diff --git a/AI/GeniusAI/ExpertSystem.h b/AI/GeniusAI/ExpertSystem.h index 8a60909a7..4ed7c953d 100644 --- a/AI/GeniusAI/ExpertSystem.h +++ b/AI/GeniusAI/ExpertSystem.h @@ -2,7 +2,7 @@ #include "../vcmi/CCallback.h" #include "../vcmi/lib/HeroBonus.h" #include -#include +#include /* * ExpertSystem.h, part of VCMI engine @@ -28,11 +28,11 @@ template class ExpertSystemShell private: ICallback* m_cb; protected: - std::set knowledge; - std::set rulesToErase; - std::set rulesToAdd; - std::set factsToErase; - std::set factsToAdd; + std::vector knowledge; + std::vector rulesToErase; + std::vector rulesToAdd; + std::vector factsToErase; + std::vector factsToAdd; ui16 goalCounter; //count / evaluate achieved goals for runType public: ExpertSystemShell(){goalCounter = 0;}; @@ -73,16 +73,16 @@ public: bool fired; //if conditions of rule were met and it produces some output ui8 conditionCounter; protected: - std::set> cons; //conditions and matching facts + std::vector> cons; //conditions and matching facts input decision; virtual void canBeFired(); //if this data makes any sense for rule - type check virtual bool checkCondition(); //if condition is true or false - virtual bool checkCondition(std::set &feed); + virtual bool checkCondition(std::vector &feed); virtual void fireRule(); //use paired conditions and facts by default virtual void fireRule(ExpertSystemShell &system); - virtual void fireRule(std::set &feed); + virtual void fireRule(std::vector &feed); virtual void refreshRule(); - virtual void refreshRule(std::set &conditionSet); //in case conditions were erased + virtual void refreshRule(std::vector &conditionSet); //in case conditions were erased public: Rule(){fired = false; conditionCounter = 0; decision = NULL;}; template bool matchesInput() //if condition and data match type @@ -183,11 +183,26 @@ public: } bool operator()(int prop, si32 val) const { - return first(prop,val) && second(prop,val); //TODO: + return first(prop,val) && second(prop,val); } }; TLogic operator&&(const TLogic &first, const TLogic &second); +class LogicAlternative +{ + const TLogic first, second; +public: + LogicAlternative(const TLogic First, const TLogic Second) + :first(First), second(Second) + { + } + bool operator()(int prop, si32 val) const + { + return first(prop,val) || second(prop,val); + } +}; +TLogic operator||(const TLogic &first, const TLogic &second); + class KnowledgeHandler///I'd opt for one omniscent knowledge manager, so no templates here { public: