2013-02-07 17:50:02 +00:00
|
|
|
#ifndef YAP_PACKAGES_CLPBN_HORUS_CONSTRAINTTREE_H_
|
|
|
|
#define YAP_PACKAGES_CLPBN_HORUS_CONSTRAINTTREE_H_
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
2013-02-07 20:09:10 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
#include <ostream>
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
#include "TinySet.h"
|
|
|
|
#include "LiftedUtils.h"
|
|
|
|
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
class CTNode;
|
|
|
|
class ConstraintTree;
|
2013-02-07 22:37:45 +00:00
|
|
|
|
|
|
|
|
2013-02-08 01:11:18 +00:00
|
|
|
typedef std::vector<CTNode*> CTNodes;
|
|
|
|
typedef std::vector<ConstraintTree*> ConstraintTrees;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
class CTNode {
|
2012-05-23 14:56:01 +01:00
|
|
|
private:
|
2013-02-07 22:37:45 +00:00
|
|
|
struct CmpSymbol {
|
|
|
|
bool operator() (const CTNode* n1, const CTNode* n2) const {
|
|
|
|
return n1->symbol() < n2->symbol();
|
|
|
|
}};
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
public:
|
2013-02-07 22:37:45 +00:00
|
|
|
typedef TinySet<CTNode*, CmpSymbol> CTChilds;
|
|
|
|
|
|
|
|
CTNode (const CTNode& n, const CTChilds& chs = CTChilds())
|
2012-05-23 14:56:01 +01:00
|
|
|
: symbol_(n.symbol()), childs_(chs), level_(n.level()) { }
|
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
CTNode (Symbol s, unsigned l, const CTChilds& chs = CTChilds())
|
2012-05-23 14:56:01 +01:00
|
|
|
: symbol_(s), childs_(chs), level_(l) { }
|
|
|
|
|
|
|
|
unsigned level (void) const { return level_; }
|
|
|
|
|
|
|
|
void setLevel (unsigned level) { level_ = level; }
|
|
|
|
|
|
|
|
Symbol symbol (void) const { return symbol_; }
|
|
|
|
|
|
|
|
void setSymbol (const Symbol s) { symbol_ = s; }
|
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
CTChilds& childs (void) { return childs_; }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
const CTChilds& childs (void) const { return childs_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t nrChilds (void) const { return childs_.size(); }
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
bool isRoot (void) const { return level_ == 0; }
|
|
|
|
|
|
|
|
bool isLeaf (void) const { return childs_.empty(); }
|
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
CTChilds::iterator findSymbol (Symbol symb);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
void mergeSubtree (CTNode*, bool = true);
|
|
|
|
|
|
|
|
void removeChild (CTNode*);
|
|
|
|
|
|
|
|
void removeChilds (void);
|
|
|
|
|
|
|
|
void removeAndDeleteChild (CTNode*);
|
|
|
|
|
|
|
|
void removeAndDeleteAllChilds (void);
|
|
|
|
|
|
|
|
SymbolSet childSymbols (void) const;
|
|
|
|
|
|
|
|
static CTNode* copySubtree (const CTNode*);
|
|
|
|
|
|
|
|
static void deleteSubtree (CTNode*);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void updateChildLevels (CTNode*, unsigned);
|
|
|
|
|
2012-12-27 22:25:45 +00:00
|
|
|
Symbol symbol_;
|
2013-02-07 22:37:45 +00:00
|
|
|
CTChilds childs_;
|
2012-12-27 22:25:45 +00:00
|
|
|
unsigned level_;
|
|
|
|
|
|
|
|
DISALLOW_ASSIGN (CTNode);
|
2012-05-23 14:56:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
typedef CTNode::CTChilds CTChilds;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
inline CTChilds::iterator
|
|
|
|
CTNode::findSymbol (Symbol symb)
|
|
|
|
{
|
|
|
|
CTNode tmp (symb, 0);
|
|
|
|
return childs_.find (&tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
std::ostream& operator<< (std::ostream&, const CTNode&);
|
2013-02-06 00:24:02 +00:00
|
|
|
|
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
class ConstraintTree {
|
2012-05-23 14:56:01 +01:00
|
|
|
public:
|
|
|
|
ConstraintTree (unsigned);
|
|
|
|
|
|
|
|
ConstraintTree (const LogVars&);
|
|
|
|
|
|
|
|
ConstraintTree (const LogVars&, const Tuples&);
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
ConstraintTree (std::vector<std::vector<std::string>> names);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
ConstraintTree (const ConstraintTree&);
|
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
ConstraintTree (const CTChilds& rootChilds, const LogVars& logVars);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
~ConstraintTree (void);
|
|
|
|
|
|
|
|
CTNode* root (void) const { return root_; }
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
bool empty (void) const { return root_->childs().empty(); }
|
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
const LogVars& logVars (void) const;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
const LogVarSet& logVarSet (void) const;
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
size_t nrLogVars (void) const;
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
void addTuple (const Tuple&);
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
bool containsTuple (const Tuple&);
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
void moveToTop (const LogVars&);
|
|
|
|
|
|
|
|
void moveToBottom (const LogVars&);
|
|
|
|
|
|
|
|
void join (ConstraintTree*, bool oneTwoOne = false);
|
|
|
|
|
|
|
|
unsigned getLevel (LogVar) const;
|
|
|
|
|
|
|
|
void rename (LogVar, LogVar);
|
|
|
|
|
|
|
|
void applySubstitution (const Substitution&);
|
|
|
|
|
|
|
|
void project (const LogVarSet&);
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-10-29 21:37:58 +00:00
|
|
|
ConstraintTree projectedCopy (const LogVarSet&);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
void remove (const LogVarSet&);
|
|
|
|
|
|
|
|
bool isSingleton (LogVar);
|
|
|
|
|
|
|
|
LogVarSet singletons (void);
|
|
|
|
|
|
|
|
TupleSet tupleSet (unsigned = 0) const;
|
|
|
|
|
|
|
|
TupleSet tupleSet (const LogVars&);
|
|
|
|
|
|
|
|
unsigned size (void) const;
|
|
|
|
|
|
|
|
unsigned nrSymbols (LogVar);
|
|
|
|
|
|
|
|
void exportToGraphViz (const char*, bool = false) const;
|
|
|
|
|
|
|
|
bool isCountNormalized (const LogVarSet&);
|
|
|
|
|
|
|
|
unsigned getConditionalCount (const LogVarSet&);
|
|
|
|
|
|
|
|
TinySet<unsigned> getConditionalCounts (const LogVarSet&);
|
|
|
|
|
|
|
|
bool isCartesianProduct (const LogVarSet&);
|
|
|
|
|
|
|
|
std::pair<ConstraintTree*, ConstraintTree*> split (const Tuple&);
|
|
|
|
|
|
|
|
std::pair<ConstraintTree*, ConstraintTree*> split (
|
|
|
|
const LogVars&, ConstraintTree*, const LogVars&);
|
|
|
|
|
|
|
|
ConstraintTrees countNormalize (const LogVarSet&);
|
|
|
|
|
|
|
|
ConstraintTrees jointCountNormalize (
|
|
|
|
ConstraintTree*, ConstraintTree*, LogVar, LogVar, LogVar);
|
|
|
|
|
|
|
|
LogVars expand (LogVar);
|
|
|
|
|
|
|
|
ConstraintTrees ground (LogVar);
|
|
|
|
|
2012-12-18 23:51:51 +00:00
|
|
|
void cloneLogVar (LogVar, LogVar);
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-10-30 00:15:40 +00:00
|
|
|
ConstraintTree& operator= (const ConstraintTree& ct);
|
2012-12-17 18:39:42 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
private:
|
|
|
|
unsigned countTuples (const CTNode*) const;
|
|
|
|
|
|
|
|
CTNodes getNodesBelow (CTNode*) const;
|
|
|
|
|
|
|
|
CTNodes getNodesAtLevel (unsigned) const;
|
|
|
|
|
|
|
|
unsigned nrNodes (const CTNode* n) const;
|
|
|
|
|
|
|
|
void appendOnBottom (CTNode* n1, const CTChilds&);
|
|
|
|
|
|
|
|
void swapLogVar (LogVar);
|
|
|
|
|
2012-05-24 22:55:20 +01:00
|
|
|
bool join (CTNode*, const Tuple&, size_t, CTNode*);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
void getTuples (CTNode*, Tuples, unsigned, Tuples&, CTNodes&) const;
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<std::pair<CTNode*, unsigned>> countNormalize (
|
2012-05-23 14:56:01 +01:00
|
|
|
const CTNode*, unsigned);
|
|
|
|
|
2013-02-13 14:26:47 +00:00
|
|
|
static void split (CTNode*, CTNode*, CTChilds&, CTChilds&, unsigned);
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
CTNode* root_;
|
|
|
|
LogVars logVars_;
|
|
|
|
LogVarSet logVarSet_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
|
|
|
|
inline const LogVars&
|
|
|
|
ConstraintTree::logVars (void) const
|
|
|
|
{
|
|
|
|
assert (LogVarSet (logVars_) == logVarSet_);
|
|
|
|
return logVars_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline const LogVarSet&
|
|
|
|
ConstraintTree::logVarSet (void) const
|
|
|
|
{
|
|
|
|
assert (LogVarSet (logVars_) == logVarSet_);
|
|
|
|
return logVarSet_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline size_t
|
|
|
|
ConstraintTree::nrLogVars (void) const
|
|
|
|
{
|
|
|
|
return logVars_.size();
|
|
|
|
assert (LogVarSet (logVars_) == logVarSet_);
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
} // namespace Horus
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2013-02-08 00:20:01 +00:00
|
|
|
#endif // YAP_PACKAGES_CLPBN_HORUS_CONSTRAINTTREE_H_
|
2012-05-23 14:56:01 +01:00
|
|
|
|