2012-05-23 14:56:01 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "Var.h"
|
|
|
|
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2013-03-09 16:18:13 +00:00
|
|
|
std::unordered_map<VarId, Var::VarInfo> Var::varsInfo_;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
Var::Var (const Var* v)
|
|
|
|
{
|
|
|
|
varId_ = v->varId();
|
|
|
|
range_ = v->range();
|
|
|
|
evidence_ = v->getEvidence();
|
2013-02-08 21:01:53 +00:00
|
|
|
index_ = Util::maxUnsigned();
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Var::Var (VarId varId, unsigned range, int evidence)
|
|
|
|
{
|
|
|
|
assert (range != 0);
|
|
|
|
assert (evidence < (int) range);
|
|
|
|
varId_ = varId;
|
|
|
|
range_ = range;
|
|
|
|
evidence_ = evidence;
|
2013-02-08 21:01:53 +00:00
|
|
|
index_ = Util::maxUnsigned();
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
Var::isValidState (int stateIndex)
|
|
|
|
{
|
|
|
|
return stateIndex >= 0 && stateIndex < (int) range_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-12-28 01:35:59 +00:00
|
|
|
Var::setEvidence (int evidence)
|
2012-12-20 23:19:10 +00:00
|
|
|
{
|
2012-12-28 01:35:59 +00:00
|
|
|
assert (evidence < (int) range_);
|
|
|
|
evidence_ = evidence;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
std::string
|
2013-02-28 19:45:37 +00:00
|
|
|
Var::label() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
if (Var::varsHaveInfo()) {
|
2013-03-09 16:18:13 +00:00
|
|
|
assert (Util::contains (varsInfo_, varId_));
|
|
|
|
return varsInfo_.find (varId_)->second.first;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
std::stringstream ss;
|
2012-05-23 14:56:01 +01:00
|
|
|
ss << "x" << varId_;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
States
|
2013-02-28 19:45:37 +00:00
|
|
|
Var::states() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
if (Var::varsHaveInfo()) {
|
2013-03-09 16:18:13 +00:00
|
|
|
assert (Util::contains (varsInfo_, varId_));
|
|
|
|
return varsInfo_.find (varId_)->second.second;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
States states;
|
|
|
|
for (unsigned i = 0; i < range_; i++) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::stringstream ss;
|
2012-05-23 14:56:01 +01:00
|
|
|
ss << i ;
|
|
|
|
states.push_back (ss.str());
|
|
|
|
}
|
|
|
|
return states;
|
|
|
|
}
|
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
|
|
|
|
|
2013-02-08 00:22:51 +00:00
|
|
|
void
|
2013-02-06 00:24:02 +00:00
|
|
|
Var::addVarInfo (
|
2013-02-07 13:37:15 +00:00
|
|
|
VarId vid, std::string label, const States& states)
|
2013-02-06 00:24:02 +00:00
|
|
|
{
|
2013-02-08 21:01:53 +00:00
|
|
|
assert (Util::contains (varsInfo_, vid) == false);
|
2013-02-07 13:37:15 +00:00
|
|
|
varsInfo_.insert (std::make_pair (vid, VarInfo (label, states)));
|
2013-02-06 00:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-08 00:22:51 +00:00
|
|
|
bool
|
2013-02-28 19:45:37 +00:00
|
|
|
Var::varsHaveInfo()
|
2013-02-06 00:24:02 +00:00
|
|
|
{
|
|
|
|
return varsInfo_.empty() == false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-08 00:22:51 +00:00
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
Var::clearVarsInfo()
|
2013-02-06 00:24:02 +00:00
|
|
|
{
|
|
|
|
varsInfo_.clear();
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
} // namespace Horus
|
2013-02-07 23:53:13 +00:00
|
|
|
|