1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: expressionTree.C,v 1.13 2003/08/26 09:17:49 oliver Exp $
//
#include <BALL/KERNEL/expressionTree.h>
using namespace::std;
namespace BALL
{
ExpressionTree::ExpressionTree()
: type_(INVALID),
negate_(false),
predicate_(0),
children_()
{
}
void ExpressionTree::dump(std::ostream& os, Size depth) const
{
BALL_DUMP_STREAM_PREFIX(os);
BALL_DUMP_HEADER(os, this, this);
BALL_DUMP_DEPTH(os, depth);
os << "[type = " << type_
<< " negate = " << negate_
<< " arg = " << (predicate_ == 0 ? "<NULL>" : predicate_->getArgument().c_str())
<< "]" << endl;
list<const ExpressionTree*>::const_iterator it = children_.begin();
for (; it != children_.end(); ++it)
{
(*it)->dump(os, depth + 2);
}
BALL_DUMP_STREAM_SUFFIX(os);
}
ExpressionTree::ExpressionTree
(Type type, list<const ExpressionTree*> children, bool negate)
: type_(type),
negate_(negate),
predicate_(0),
children_(children)
{
}
ExpressionTree::ExpressionTree(ExpressionPredicate* predicate, bool negate)
: type_(INVALID),
negate_(negate),
predicate_(predicate),
children_()
{
}
ExpressionTree::ExpressionTree(const ExpressionTree& tree)
: type_(tree.type_),
negate_(tree.negate_),
predicate_(0),
children_()
{
// Clone the predicate
if (tree.predicate_ != 0)
{
predicate_ = (ExpressionPredicate*)tree.predicate_->create();
}
// Clone the children.
std::list<const ExpressionTree*>::const_iterator it(tree.children_.begin());
for (; it != tree.children_.end(); ++it)
{
children_.push_back(new ExpressionTree(**it));
}
}
ExpressionTree::~ExpressionTree()
{
clear();
}
void ExpressionTree::clear()
{
type_ = INVALID;
negate_ = false;
// Delete the predicate.
delete predicate_;
predicate_ = 0;
// Delete the node's children.
std::list<const ExpressionTree*>::const_iterator it(children_.begin());
for (; it != children_.end(); ++it)
{
delete *it;
}
children_.clear();
}
ExpressionTree& ExpressionTree::operator = (const ExpressionTree& tree)
{
// Avoid self assignment.
if (&tree == this)
{
return *this;
}
clear();
type_ = tree.type_;
negate_ = tree.negate_;
// Clone the predicate.
if (tree.predicate_ != 0)
{
predicate_ = (ExpressionPredicate*)tree.predicate_->create();
}
// Clone the children.
std::list<const ExpressionTree*>::const_iterator it(tree.children_.begin());
for (; it != tree.children_.end(); ++it)
{
children_.push_back(new ExpressionTree(**it));
}
return *this;
}
bool ExpressionTree::operator () (const Atom& atom) const
{
bool result = true;
if (type_ == LEAF)
{
// if there is a predicate associated with this leaf, return its
// value, else return false
if (predicate_ != 0)
{
result = (negate_ ^ (predicate_->operator () (atom)));
}
else
{
result = false;
}
}
else
{
// this is either a conjunction or a disjunction or a node that
// represents a bracket pair (which is marked as INVALID)
// the empty clause is always true
if (children_.empty())
{
return (!negate_);
}
// evaluate all children
list<const ExpressionTree*>::const_iterator list_it = children_.begin();
bool abort = false;
for (; !abort && list_it != children_.end(); ++list_it)
{
result = (*list_it)->operator () (atom);
// OR expressions may be aborted, if the first subexpression yields true
if (type_ == OR)
{
if (result)
{
abort = true;
}
}
else
{
// AND expressions may be aborted, if the first subexpression
// yields false
if (!result)
{
abort = true;
}
}
}
}
return result;
}
bool ExpressionTree::operator == (const ExpressionTree& tree) const
{
if ((predicate_ == 0) && (tree.predicate_ == 0))
{
// both pointers are null pointers. Expressions should have null
// pointers only if they are default constructed, so a consistency
// check might be useful
return ((type_ == tree.type_)
&& (negate_ == tree.negate_)
&& compareChildren_(tree));
}
else
{
if ((predicate_ == 0) || (tree.predicate_ == 0))
{
// one of the pointers is NULL, so hte instances cannot be equal.
return false;
}
else
{
return ((type_ == tree.type_)
&& (negate_ == tree.negate_)
&& (*predicate_ == *tree.predicate_)
&& compareChildren_(tree));
}
}
}
bool ExpressionTree::operator != (const ExpressionTree& tree) const
{
return ! operator == (tree);
}
bool ExpressionTree::compareChildren_(const ExpressionTree& tree) const
{
if (children_.size() != tree.children_.size())
{
return false;
}
list<const ExpressionTree*>::const_iterator it1 = children_.begin();
list<const ExpressionTree*>::const_iterator it2 = tree.children_.begin();
for (; it1 != children_.end(); ++it1, ++it2)
{
if (**it1 != **it2)
{
return false;
}
}
return true;
}
void ExpressionTree::setType(Type type)
{
type_ = type;
}
ExpressionTree::Type ExpressionTree::getType() const
{
return type_;
}
void ExpressionTree::setNegate(bool negate)
{
negate_= negate;
}
bool ExpressionTree::getNegate() const
{
return negate_;
}
void ExpressionTree::setPredicate(ExpressionPredicate* predicate)
{
predicate_ = predicate;
}
ExpressionPredicate* ExpressionTree::getPredicate() const
{
return predicate_;
}
void ExpressionTree::appendChild(const ExpressionTree* child)
{
children_.push_back(child);
}
const list<const ExpressionTree*>& ExpressionTree::getChildren() const
{
return children_;
}
}
|