1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-23 21:29:13 +02:00

A little more work on Expert System.

This commit is contained in:
DjWarmonger 2011-02-05 18:24:32 +00:00
parent a84a95f0ee
commit 78fc212489
2 changed files with 49 additions and 26 deletions

@ -14,9 +14,9 @@
template <typename ruleType, typename facts> template <typename cond> void ExpertSystemShell<ruleType, facts>::DataDrivenReasoning(runType type) template <typename ruleType, typename facts> template <typename cond> void ExpertSystemShell<ruleType, facts>::DataDrivenReasoning(runType type)
{ {
std::set<ruleType>::iterator ir; std::vector<ruleType>::iterator ir;
std::list<fact>::iterator iF; std::list<fact>::iterator iF;
std::set<std::pair<cond, input*>>::iterator ic; std::vector<std::pair<cond, input*>>::iterator ic;
bool factWasAdded = false; //carry it over inner loop bool factWasAdded = false; //carry it over inner loop
switch (type) switch (type)
{ {
@ -72,7 +72,7 @@ template <typename ruleType, typename facts> template <typename cond> void Exper
} }
void BonusRule::fireRule() void BonusRule::fireRule()
{ {
for (std::set<std::pair<BonusCondition, BonusHolder*>>::iterator it = cons.begin(); it != cons.end(); it++) for (std::vector<std::pair<BonusCondition, BonusHolder*>>::iterator it = cons.begin(); it != cons.end(); it++)
{ {
switch (it->first.parameter) switch (it->first.parameter)
{ //compare fact with condition { //compare fact with condition
@ -114,18 +114,26 @@ TLogic operator&&(const TLogic &first, const TLogic &second)
{ {
return LogicConjunction(first, second); return LogicConjunction(first, second);
} }
//TODO: find out why it does not compile TLogic operator||(const TLogic &first, const TLogic &second)
//template <typename input, typename conType> void Rule<input, conType>::refreshRule(std::set<conType> &conditionSet) {
//{ return LogicAlternative(first, second);
// cons.clear(); }
// for (std::set<conType>::iterator it = conditionSet.begin(); it != conditionSet.end(); it++) template <typename input, typename conType> void Rule<input, conType>::refreshRule(std::vector<conType> &conditionSet)
// cons.insert(std::make_pair<conType,input*>(*it, NULL)); //pointer to condition and null fact {//replace conditions with new vector
//} cons.clear();
//template <typename input, typename conType> void Rule<input, conType>::refreshRule() std::pair <conType,input*> para;
//{ para.second = NULL;
// for (std::set<std::pair<conType, input*>>::iterator it = cons.begin(); it != cons.end(); it++) for (std::vector<conType>::iterator it = conditionSet.begin(); it != conditionSet.end(); it++)
// *it->second = NULL; {
//} para.first = *it;
cons.push_back(para); //pointer to condition and null fact
}
}
template <typename input, typename conType> void Rule<input, conType>::refreshRule()
{//clear matching facts
for (std::vector<std::pair<conType, input*>>::iterator it = cons.begin(); it != cons.end(); it++)
it->second = NULL;
}
bool BonusCondition::matchesFact(Bonus &fact) bool BonusCondition::matchesFact(Bonus &fact)
{ {
if (object(fact)) //Bonus(fact) matches local Selector(object) if (object(fact)) //Bonus(fact) matches local Selector(object)

@ -2,7 +2,7 @@
#include "../vcmi/CCallback.h" #include "../vcmi/CCallback.h"
#include "../vcmi/lib/HeroBonus.h" #include "../vcmi/lib/HeroBonus.h"
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <set> #include <vector>
/* /*
* ExpertSystem.h, part of VCMI engine * ExpertSystem.h, part of VCMI engine
@ -28,11 +28,11 @@ template <typename ruleType, typename fact> class ExpertSystemShell
private: private:
ICallback* m_cb; ICallback* m_cb;
protected: protected:
std::set<ruleType> knowledge; std::vector<ruleType> knowledge;
std::set<ruleType*> rulesToErase; std::vector<ruleType*> rulesToErase;
std::set<ruleType*> rulesToAdd; std::vector<ruleType*> rulesToAdd;
std::set<fact*> factsToErase; std::vector<fact*> factsToErase;
std::set<fact*> factsToAdd; std::vector<fact*> factsToAdd;
ui16 goalCounter; //count / evaluate achieved goals for runType ui16 goalCounter; //count / evaluate achieved goals for runType
public: public:
ExpertSystemShell(){goalCounter = 0;}; ExpertSystemShell(){goalCounter = 0;};
@ -73,16 +73,16 @@ public:
bool fired; //if conditions of rule were met and it produces some output bool fired; //if conditions of rule were met and it produces some output
ui8 conditionCounter; ui8 conditionCounter;
protected: protected:
std::set<std::pair<conType, input*>> cons; //conditions and matching facts std::vector<std::pair<conType, input*>> cons; //conditions and matching facts
input decision; input decision;
virtual void canBeFired(); //if this data makes any sense for rule - type check 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(); //if condition is true or false
virtual bool checkCondition(std::set<input*> &feed); virtual bool checkCondition(std::vector<input*> &feed);
virtual void fireRule(); //use paired conditions and facts by default virtual void fireRule(); //use paired conditions and facts by default
virtual void fireRule(ExpertSystemShell<input, conType> &system); virtual void fireRule(ExpertSystemShell<input, conType> &system);
virtual void fireRule(std::set<input*> &feed); virtual void fireRule(std::vector<input*> &feed);
virtual void refreshRule(); virtual void refreshRule();
virtual void refreshRule(std::set<conType> &conditionSet); //in case conditions were erased virtual void refreshRule(std::vector<conType> &conditionSet); //in case conditions were erased
public: public:
Rule(){fired = false; conditionCounter = 0; decision = NULL;}; Rule(){fired = false; conditionCounter = 0; decision = NULL;};
template <typename givenInput> bool matchesInput() //if condition and data match type template <typename givenInput> bool matchesInput() //if condition and data match type
@ -183,11 +183,26 @@ public:
} }
bool operator()(int prop, si32 val) const 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); 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 class KnowledgeHandler///I'd opt for one omniscent knowledge manager, so no templates here
{ {
public: public: