package infosapient.system;
/*
* Copyright (c) 1996-2001, Workplace Performance Tools, All Rights Reserved.
* License to use this program is provided under the COMMON PUBLIC LICENSE 0.5
* THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS COMMON PUBLIC LICENSE
* ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
*/
import infosapient.system.Observer;
import java.rmi.server.ObjID;
import infosapient.system.FzySystemException;
import java.util.*;
import infosapient.opr.FzyOperator;
import infosapient.util.TextOutput;
import infosapient.util.XMLOutput;
import infosapient.system.ObservableImpl;
/**
* Class FzyPremise implements the behavior for a rule's <i>PREMISE</i>(also called the antecedent).
* (The premise is the evidence from which to draw a conclusion or <i>CONSEQUENT</i>.)
* A premise may be either simple or compound.
* <blockquote>Example of simple premise:
* <blockquote><code> ( attribute (is | are) hedge* set ) </code> </blockquote>
* Example of a compound premise:
* <blockquote> ( (premise1) Operator (premise2) ... ) </code></blockquote>
* </blockquote>
* @see FzyRule
* @see FzyOperator
* @see FzyHedge
* @author:
* @version $Revision: 1.1.1.1 $
* @package infosapient.system
*/
/*
* <h3>Modification log:</h3><ul>
* <li>01/12/00: MSStofferahn: added getAttributes() which returns a Vector
* of all the premise attributes. This method uses the new private method getAttributesForClause(),
* which recursively parses complex premises.
* <ul>
*/
public class FzyPremise
extends FzyRuleComponent
implements XMLOutput, TextOutput {
static final long serialVersionUID = -8674553982718454473L;
private boolean attribAdded = false;
private infosapient.system.FzyClauseComponent component = null;
private int inx = -1;
public FzyPremise() {
super.setID(new java.rmi.server.ObjID());
}
public FzyPremise(FzyRule rule) {
this();
setRule(rule);
addObserver(rule);
}
/**
* Returns a Vector of all the attributes used in the premise of a rule.
* Recursivly searches through complex premises.
* The FzyAttributes returned are distinct - no duplicate values.
* Creation date: (1/12/00 1:48:47 PM)
* @return java.util.Vector
*/
public Vector getAttributes() {
FzyClauseComponent comp = this.getComponent();
Vector vAtts = new Vector();
getAttributesForClause(comp, vAtts);
return vAtts;
}
/**
* A recursive method that parses a premise for its FzyAttributes.
* Creation date: (1/11/00 1:44:43 PM)
* @param comp infosapient.system.FzyClauseComponent
* @param v java.util.Vector - used to hold the set of attributes.
*/
private void getAttributesForClause(FzyClauseComponent comp, Vector v) {
if (comp instanceof FzyOperator) {
FzyAttribute at = null;
FzyOperator op = (FzyOperator) comp;
if (op.getRightChild() instanceof FzyAttribClause) {
at = ((FzyAttribClause) op.getRightChild()).getAttribute();
if (!v.contains(at)) {
v.addElement(at);
}
}
getAttributesForClause(op.getLeftChild(), v);
} else {
FzyAttribClause ast = (FzyAttribClause) comp;
FzyAttribute at = ast.getAttribute();
if (!v.contains(at)) {
v.addElement(at);
}
}
}
/**
* Return the clause component -- either a infosapient.system.FzyAttribClause or
* a infosapient.opr.FzyOperator.
* @return FzyClauseComponent
*/
public FzyClauseComponent getComponent() {
return component;
}
public String getName() {
return new String("Premise of " + getRule().getName());
}
public void reset() {
component.reset();
}
/**
* Add a FzyClauseComponent to the Premise.
* @param infosapient.system.FzyClauseComponent the clause component being added.
* @throws IllegalStateException if attempt to add another clauseComponent after first one added.
* @throws IllegalArgumentException if arg is null.
*/
public void setComponent(infosapient.system.FzyClauseComponent fcc)
throws IllegalStateException, IllegalArgumentException {
if (fcc == null)
throw new IllegalArgumentException(
"Attempt to add a null FzyClauseComponent to this Premise. "
+ owningRule.getName());
if (!attribAdded) {
attribAdded = true;
component = fcc;
component.addObserver((Observer) this);
setChanged();
} else
throw new IllegalStateException(" Badly constructed premise. Attempt to add another FzyClauseComponent");
}
/**
* Represent the rules premise as xml tags.
* Creation date: (1/30/00 9:13:18 PM)
* @return java.lang.StringBuffer
*/
public StringBuffer toXML(int nTabs) {
StringBuffer xmlSB = new StringBuffer(1024);
for (int i = 0; i < nTabs; i++)
xmlSB.append("\t");
xmlSB.append("<premise>\n");
if (getComponent() instanceof FzyOperator) {
for (int i = 0; i < nTabs + 1; i++)
xmlSB.append("\t");
xmlSB.append("<clause>\n" + getComponent().toXML(nTabs + 1));
for (int i = 0; i < nTabs + 1; i++)
xmlSB.append("\t");
xmlSB.append("</clause>\n");
} else {
for (int i = 0; i < nTabs + 1; i++)
xmlSB.append("\t");
xmlSB.append(getComponent().toXML(nTabs + 1));
}
for (int i = 0; i < nTabs; i++)
xmlSB.append("\t");
xmlSB.append("</premise>\n");
return xmlSB;
}
}