package infosapient.system;
/*
* Copyright (c) 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 java.util.*;
import java.rmi.server.ObjID;
import infosapient.util.TextOutput;
import infosapient.util.XMLOutput;
import infosapient.xml.KbXMLCompiler;
/**
* @(#)FzyRule.java
* Class FzyRule is responsible for the modeling the behavior of a <i>CONDITIONAL</i> rule.
* <p>A conditional rule has the structure:
* <blockquote><code><b>if</b> <i>premise</i> <b>then</b> <i>consequent</i>;</code></blockquote>
*
* @see FzyUnconditionalRule
* @see FzyPremise
* @see FzyConsequent
* @see FzyOperator
* @author Michael McConnell
* @version $Revision: 1.1.1.1 $
*
* @package infosapient.system
* <h3>Modification log:</h3><ul>
* <li>12/23/99: MSStofferahn: added factory method create(), which
* uses a parser to create a valid rule.
* <li>01/12/00: MSStofferahn: added getPremiseAttributes() which returns an Enumeration
* of all the attributes used in the premise of the rule.
* <ul>
*/
public class FzyRule extends FzySystemComponent implements
Observer,
java.io.Serializable,
infosapient.util.XMLOutput,
infosapient.util.TextOutput {
static final long serialVersionUID = -982178499173157575L;
private boolean hasFired = false;
private String name = null;
private FzyKnowledgebase theKB = null;
private FzyPremise myPremise = null;
private FzyConsequent myConsequent = null;
private String ruleText;
//
// Constructors
//
public FzyRule() {
setID(new ObjID());
}
public FzyRule(FzyKnowledgebase aKB){
this();
setKB(aKB);
addObserver(aKB);
}
public FzyRule(FzyKnowledgebase aKB, String rt){
this(aKB);
ruleText = rt;
}
public FzyRule addConsequent(FzyConsequent acons){
myConsequent = acons;
setChanged();
return this;
}
public FzyRule addPremise(FzyPremise aPremise){
myPremise = aPremise;
setChanged();
return this;
}
public FzyConsequent getConsequent(){
return myConsequent;
}
/**
* Insert the method's description here.
* @author Michael McConnell Creation date: (1/20/00 10:42:15 AM)
* @return infosapient.system.FzyKnowledgebase
*/
public FzyKnowledgebase getKB() {
return theKB;
}
public FzyPremise getPremise(){
return myPremise;
}
/**
* Returns an Enumeration
* of all the attributes used in the premise of the rule.
* Creation date: (1/12/00 1:40:42 PM)
* @return Enumeration of FzyAttributes
*/
public Enumeration getPremiseAttributes() {
Vector v = this.getPremise().getAttributes();
if (v!= null) {
return v.elements();
} else {
return null;
}
}
public String getText() {
return ruleText;
}
public boolean hasFired(){
return hasFired;
}
public void printOn(java.io.Writer outWriter, int nTabs) throws java.io.IOException {
int ktabs = nTabs+1;
StringBuffer sb = new StringBuffer(50);
for (int kx = 0; kx < ktabs; kx++) {
sb.append("\t");
}
sb.append(getName()).append("\n\t");
outWriter.write(sb.toString());
sb = new StringBuffer(50);
for (int kx = 0; kx < ktabs; kx++) {
sb.append("\t");
}
sb.append(getText()).append("\n\n");
outWriter.write(sb.toString());
return;
}
public void reset() {
getPremise().reset();
getConsequent().reset();
hasFired = false;
}
public FzyRule setFired(){
hasFired = true;
setChanged();
return this;
}
public FzyRule setKB(FzyKnowledgebase akb){
theKB = akb;
setChanged();
return this;
}
public void setText(String rt) {
ruleText = rt;
setChanged();
}
public String toString() {
return getName();
}
/**
* Represent this rule as text for display.
* Creation date: (2/1/00 5:53:23 PM)
* @return java.lang.StringBuffer
*/
public String toText() {
return ruleText;
}
/**
* Represent this rule as xml tags.
* Creation date: (1/31/00 1:18:17 PM)
* @return java.lang.StringBuffer
*/
public StringBuffer toXML(int nTabs) {
StringBuffer xmlSB = new StringBuffer(128);
for (int i = 0; i < nTabs; i++) xmlSB.append("\t");
xmlSB.append("<rule id=\""+getName()+"\">\n");
xmlSB.append(this.toText());
for (int i = 0; i < nTabs; i++) xmlSB.append("\t");
xmlSB.append("</rule>\n");
return xmlSB;
}
public void update(Observable juo, Object obj) {}
}