This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/CLPBN/horus/Var.cpp

108 lines
1.6 KiB
C++
Raw Normal View History

2012-05-23 14:56:01 +01:00
#include <sstream>
#include "Var.h"
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();
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;
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
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
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-08 00:22:51 +00:00
void
Var::addVarInfo (
2013-02-07 13:37:15 +00:00
VarId vid, std::string label, const States& states)
{
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-08 00:22:51 +00:00
bool
Var::varsHaveInfo()
{
return varsInfo_.empty() == false;
}
2013-02-08 00:22:51 +00:00
void
Var::clearVarsInfo()
{
varsInfo_.clear();
}
} // namespace Horus
2013-02-07 23:53:13 +00:00