/**
* A representation of propositions in Propositional (Boolean) Logic.
* This class implements the ADT of Propositions specified in
* the COMP213 Lectures 14-16.
*
*/
public class BoolTerm {
/**
* The top operator of the proposition.
*
*/
private Operator op;
/**
* The operands of the {@link #op top operator}.
*
*/
private BoolTerm[] subterms;
/**
* Creates a new BoolTerm
instance.
*
* @param o the top operator of the proposition
* @param subs an array of operands;
* this should contain the correct number of propositions/terms
* for the given operator
*/
public BoolTerm(Operator o, BoolTerm[] subs) {
op = o;
subterms = subs;
}
public BoolTerm(Operator o) {
op = o;
subterms = new BoolTerm[0];
}
/**
* Give a string representation of the proposition that has
* no unnecessary brackets
*
* @return a String
value
*/
public String toString() {
return toString(Operators.MAX_PREC);
}
/**
* Give a string representation of the proposition that has
* no unnecessary brackets
* The whole term will be enclosed in brackets if the precedence of its
* {@link #op top operator} is greater than parameter prec
;
* this parameter can be thought of as the precedence of the operator
* immediately above this proposition, forcing it to be put in
* brackets if necessary.
*
* @param prec the precedence of the operator "immediately above"
* @return a string representation of the proposition, with
* no unnecessary brackets
*/
public String toString(int prec) {
return op.toString(subterms, prec);
}
/**
* Checks if the number of subterms of the proposition matches
* the expected number of subterms for the {@link #op top operator} ;
* for example, a not
operator would only need an array
* of subterms of size 1, whereas an or
operator would
* need an array of subterms of size 2.
*
* @return true
if the term is well formed; otherwise,
* false
.
*/
public boolean isWellFormed() {
return op.isWellFormed(subterms);
}
}// BoolTerm