1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-19 21:10:12 +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

View File

@ -14,9 +14,9 @@
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::set<std::pair<cond, input*>>::iterator ic;
std::vector<std::pair<cond, input*>>::iterator ic;
bool factWasAdded = false; //carry it over inner loop
switch (type)
{
@ -72,7 +72,7 @@ template <typename ruleType, typename facts> template <typename cond> void Exper
}
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)
{ //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 <typename input, typename conType> void Rule<input, conType>::refreshRule(std::set<conType> &conditionSet)
//{
// cons.clear();
// for (std::set<conType>::iterator it = conditionSet.begin(); it != conditionSet.end(); it++)
// cons.insert(std::make_pair<conType,input*>(*it, NULL)); //pointer to condition and null fact
//}
//template <typename input, typename conType> void Rule<input, conType>::refreshRule()
//{
// for (std::set<std::pair<conType, input*>>::iterator it = cons.begin(); it != cons.end(); it++)
// *it->second = NULL;
//}
TLogic operator||(const TLogic &first, const TLogic &second)
{
return LogicAlternative(first, second);
}
template <typename input, typename conType> void Rule<input, conType>::refreshRule(std::vector<conType> &conditionSet)
{//replace conditions with new vector
cons.clear();
std::pair <conType,input*> para;
para.second = NULL;
for (std::vector<conType>::iterator it = conditionSet.begin(); it != conditionSet.end(); it++)
{
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)
{
if (object(fact)) //Bonus(fact) matches local Selector(object)

View File

@ -2,7 +2,7 @@
#include "../vcmi/CCallback.h"
#include "../vcmi/lib/HeroBonus.h"
#include <boost/bind.hpp>
#include <set>
#include <vector>
/*
* ExpertSystem.h, part of VCMI engine
@ -28,11 +28,11 @@ template <typename ruleType, typename fact> class ExpertSystemShell
private:
ICallback* m_cb;
protected:
std::set<ruleType> knowledge;
std::set<ruleType*> rulesToErase;
std::set<ruleType*> rulesToAdd;
std::set<fact*> factsToErase;
std::set<fact*> factsToAdd;
std::vector<ruleType> knowledge;
std::vector<ruleType*> rulesToErase;
std::vector<ruleType*> rulesToAdd;
std::vector<fact*> factsToErase;
std::vector<fact*> 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<std::pair<conType, input*>> cons; //conditions and matching facts
std::vector<std::pair<conType, input*>> 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<input*> &feed);
virtual bool checkCondition(std::vector<input*> &feed);
virtual void fireRule(); //use paired conditions and facts by default
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(std::set<conType> &conditionSet); //in case conditions were erased
virtual void refreshRule(std::vector<conType> &conditionSet); //in case conditions were erased
public:
Rule(){fired = false; conditionCounter = 0; decision = NULL;};
template <typename givenInput> 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: