#include <BoolExpr.h>
Public Types | |
enum | Type { VALUE, AND, OR, NOT } |
Possible types for a boolean expression tree node. More... | |
Public Member Functions | |
BoolExpr (const T &initValue=T()) | |
Creates a VALUE node with the given initial value. | |
BoolExpr (Type t, BoolExpr *l, BoolExpr *r) | |
Creates a node of the given type and with the given children. | |
~BoolExpr () | |
Calls delete on the left and right subtrees of this node. | |
Type | getType () const |
Returns the type of this node. | |
const BoolExpr * | getLeft () const |
Returns the left-hand child of this node. | |
BoolExpr * | getLeft () |
Returns the left-hand child of this node. | |
const BoolExpr * | getRight () const |
Returns the right-hand child of this node. | |
BoolExpr * | getRight () |
Returns the right-hand child of this node. | |
const T & | getValue () const |
Returns the value of this node. | |
T & | getValue () |
Returns the value of this node. | |
void | setType (Type t) |
Changes the type of this node. | |
void | setLeft (BoolExpr *subtree) |
Attaches a subtree as this node's left-hand child. | |
void | setRight (BoolExpr *subtree) |
Attaches a subtree as this node's right-hand child. | |
void | setValue (const T &v) |
Changes the value of this node. | |
bool | isDisjunctiveNormalForm () const |
Determines if the tree rooted at this node is in the DNF. | |
template<class OutputIter> | |
OutputIter | getDNFTermRoots (OutputIter dest) const |
Gets the roots of the terms of an tree in DNF form. | |
void | getTreeVariables (std::set< T > &positives, std::set< T > &negatives) const |
Returns the variables that are used in the tree rooted at this node. | |
bool | isDNFTermUseful () const |
Determines if this DNF term always evaluates to false. | |
void | print (std::ostream &out) const |
Prints the boolean expression tree rooted at this node in a stream. | |
std::string | print () const |
Static Public Member Functions | |
BoolExpr * | cloneTree (const BoolExpr *root) |
Returns a copy of the designated tree. | |
BoolExpr * | getDisjunctiveNormalForm (BoolExpr *root) |
Transforms the designated tree into its Disjunctive Normal Form. | |
BoolExpr * | getRawDNF (BoolExpr *root) |
Like getDisjunctiveNormalForm(), but without simplifications. | |
Friends | |
class | BoolExprParser |
All objects of this class are expected to be allocated by operator new. The value type T must be a concrete type with a copy constructor and an assignment operator. Type T must be LessThan Comparable (i.e., it supports the operators < and ==). See class BoolExprParser for a way to obtain a tree from a textual boolean expression.
|
Possible types for a boolean expression tree node. VALUE nodes have no children, while NOT nodes only have a right-hand child. |
|
Creates a VALUE node with the given initial value. Type T must have a constructor that requires no arguments. If the print() method is called, there must be a method of the form operator << (ostream &, const T &). This library expects all BoolExpr objects to be allocated by operator new.
|
|
Creates a node of the given type and with the given children. A NOT node must only have a right-hand child, while AND and OR nodes must have both left-hand and right-hand children.
Example:
|
|
Calls delete on the left and right subtrees of this node. This library expects all BoolExpr objects to be destroyed by operator delete. |
|
Returns a copy of the designated tree. All nodes in the returned tree are independent copies of those in the original tree. All the cloned nodes are created with operator new. The caller must eventually destroy the cloned tree by calling operator delete on its root node.
|
|
Transforms the designated tree into its Disjunctive Normal Form. The proper way to call this method is the following: root = BoolExpr<SomeType>::getDisjunctiveNormalForm(root); The original tree root does not necessarily remain the root of the transformed tree. A simplification is applied: when a term of the form a&!a&(something) is seen, it is deleted unless it is the root of the whole tree. CAUTION: this method can return a NULL pointer; such a result should be interpreted as a "false" boolean expression. Examples are when the original (or resulting) tree is a&!a, or a&!a|b&!b. This method also returns NULL if 'root' is NULL.
|
|
Gets the roots of the terms of an tree in DNF form. The DNF is a sum of products. Each term in this sum is represented by a subtree of the tree rooted at the current node. This method produces the BoolExpr<T> pointers that represent the roots of the term subtrees. Returns the iterator at the position past the last insertion. The tree must first be in DNF. See getDisjunctiveNormalForm(). For example, if the current node is the root a of DNF tree representing the expression a&b | c | d&e, then three pointers will be stored: one for the 'a&b' subtree, one for the 'c' subtree (a single node) and one for the 'd&e' subtree. If the tree is a single node, then 'this' designates the only term in the sum and it is returned as the root of the unique term. The stored pointers must not be destroyed directly.
Example:
|
|
Returns the left-hand child of this node. Operator delete should not be called on the subtrees returned by getLeft(). Only the root of a tree should be the target of a destruction. |
|
Returns the left-hand child of this node.
|
|
Like getDisjunctiveNormalForm(), but without simplifications.
|
|
Returns the right-hand child of this node. Operator delete should not be called on the subtrees returned by getRight(). Only the root of a tree should be the target of a destruction. |
|
Returns the right-hand child of this node.
|
|
Returns the variables that are used in the tree rooted at this node. Example: with T == string and the expression a&b&!a&!c, the 'positives' set will contain "a" and "b" and the 'negatives' set will contain "a" and "c". When the intersection between the two sets is not empty and the only binary operator used in the tree is AND, the tree always evaluates to false (because we have an expression of the form (a&!a)&(whatever)). If the only binary operator is OR, the tree always evaluates to true.
|
|
Returns the type of this node.
|
|
Returns the value of this node. getValue() should only be called on a node for which getType() returns BoolExpr::VALUE. |
|
Returns the value of this node. getValue() should only be called on a node for which getType() returns BoolExpr::VALUE. |
|
Determines if the tree rooted at this node is in the DNF.
|
|
Determines if this DNF term always evaluates to false. Must only be called on a term of a DNF tree, which can be obtained with the getDNFTermRoots() method. (e.g., a&b&!a).
|
|
|
|
Prints the boolean expression tree rooted at this node in a stream. Does not print a newline afterwards. Uses no unnecessary parentheses. Uses '!', '|' and '&' as the NOT, OR and AND operator. If this method is called, there must be a method of the form operator << (ostream &, const T &).
|
|
Attaches a subtree as this node's left-hand child. If this node already had a non null left-hand child, it is not destroyed before attaching the new child.
|
|
Attaches a subtree as this node's right-hand child. If this node already had a non null right-hand child, it is not destroyed before attaching the new child.
|
|
Changes the type of this node.
|
|
Changes the value of this node. This method should only be called on a node of type VALUE. The given value is copied into this node's value field.
|
|
|