Appendix B Ripple-Down Rules Tree
This part implements a single conclusion Ripple-Down Rules Tree.
The details are explained in chapter 6 and chapter
8. The capability for searching through a tree for
correlation purposes is defined in the RDR tree node itself. The
RDR tree uses this recursive search in its search of the whole
tree.
B.1 RdrTreeNode.java
import java.lang.*;
import java.util.*;
/** A Ripple Down Rules tree holds rules at each node.
* The tree structure is designed for ease of maintenance.
* This gives a node for a simple single conclusion RDR tree.
* @author Veronica Clark
* @see V
* @see RdrTreeNode
* @see AlarmSet
* @see Alarm
* @since jdk1.1.8
*/
public class RdrTreeNode {
private RdrTreeNode parent;
private int nodeId;
private String conditions;
private String conclusion;
private RdrTreeNode ifTrueNode;
private RdrTreeNode ifFalseNode;
private AlarmSet cornerStone;
public RdrTreeNode(){
}
/** Generates a node with all the default values.
* @param id each node should have a unique identifier
* at the initialisation.
*/
public RdrTreeNode(int id){
nodeId = id;
parent = null;
conditions = new String();
conclusion = new String();
ifTrueNode = null;
ifFalseNode = null;
cornerStone = null;
}
/** Retrieves the identification number of a node */
public int getId(){
return(nodeId);
}
//Setting the Id should be allocated by the computer
//at the initialisation of a node, but just in case
//here's the method:
/** Setting the id number should be done with the constructor,
* but just in case, here's the method
*/
public void setId(int a) {
nodeId = a;
}
public String getConclusion() {
return(conclusion);
}
public String getConditions() {
return(conditions);
}
public AlarmSet getCornerStone() {
return(cornerStone);
}
public void setConditions(String newConditions) {
conditions = newConditions;
}
public void setConclusion(String newConclusion) {
conclusion = newConclusion;
}
public void setCornerStone(AlarmSet a) {
cornerStone = a;
}
public void setFalseNode(RdrTreeNode childNode){
ifFalseNode=childNode;
}
public RdrTreeNode getFalseNode(){
return(ifFalseNode);
}
public void setTrueNode(RdrTreeNode childNode){
ifTrueNode=childNode;
}
public RdrTreeNode getTrueNode(){
return(ifTrueNode);
}
public String toString(){
String nodeDescription = new String();
nodeDescription = "Node #"+nodeId+": "+conditions+" -> "+conclusion;
return(nodeDescription);
}
/**This recursive search returns the last node satisfied by the Alarm Set.
For the moment the satisfaction of conditions is done by the user.
Automating this will take time.
Also, on the agenda, is the database of cornerstone cases
to be mapped to each RdrTreeNode.
This is needed for adding refinement rules to the data base.
@return two nodes, one for the last satisfied node, the other for the last node searched*/
public RdrTreeNode[] recursiveSearch(AlarmSet alarms, RdrTreeNode lastGoodNode){
RdrTreeNode[] pair = new RdrTreeNode[2];
RdrTreeNode theGoodNode = this;
//For now, just print to screen the conditions
System.out.println("The Current Conditions: "+conditions);
System.out.print("Are the conditions satisfied? (yes or no)");
boolean yes = V.getYesNo();
if(yes){
if(ifTrueNode==null){
pair[0]=this;
pair[1]=this;
}
else{
pair = ifTrueNode.recursiveSearch(alarms,this);
theGoodNode = pair[0];
}
}
else{
if(ifFalseNode==null){
pair[0]=lastGoodNode;
pair[1]=this;
}
else{
pair=ifFalseNode.recursiveSearch(alarms,lastGoodNode);
}
}
return(pair);
}
}
B.2 RdrTree.java
import java.util.*;
/** A Ripple Down Rules tree holds rules at each node.
* The tree structure is designed for ease of maintenance.
* This tree gives only one conclusion. Further work
* is needed to implement a multiple conclusion RDR tree.
* @author Veronica Clark
* @see V
* @see RdrTreeNode
* @see AlarmSet
* @see Alarm
*/
public class RdrTree {
private int sizeOf;
private Vector allNodes = new Vector();
private RdrTreeNode[] lastPair = new RdrTreeNode[2];
public RdrTree(){
sizeOf = 1;
RdrTreeNode rootNode = new RdrTreeNode(0);
rootNode.setConditions("1==1");
rootNode.setConclusion("No Conclusion");
allNodes.addElement(rootNode);
}
/** Creates a new node from the parent node.
* @param tOrF The new node will be placed on the 'true'
* branch if 0 and the false if 1
* @param parentNode The node will stem from this parentNode
*/
public RdrTreeNode newTreeNode(RdrTreeNode parentNode, boolean yes)
{
RdrTreeNode fred = new RdrTreeNode(sizeOf);
allNodes.addElement(fred);
if(yes) {
parentNode.setTrueNode(fred);
}
else {
parentNode.setFalseNode(fred);
}
sizeOf++;
return(fred);
}
public RdrTreeNode getRootNode(){
RdrTreeNode rootNode = getTreeNode(0);
return(rootNode);
}
public int getTreeSize(){
return(sizeOf);
}
public RdrTreeNode getTreeNode(int indexNumber){
Enumeration lineUp = allNodes.elements();
RdrTreeNode culprit = new RdrTreeNode();
int i;
for(i=0;i==indexNumber;i++){
if(! lineUp.hasMoreElements()){
break;
}
culprit = (RdrTreeNode)lineUp.nextElement();
}
return(culprit);
}
public RdrTreeNode[] getLastPair(){
return(lastPair);
}
public void printTree(){
// System.out.println("Inside the printTree()");
Enumeration treeNodes = allNodes.elements();
RdrTreeNode currNode = new RdrTreeNode();
RdrTreeNode falseNode = new RdrTreeNode();
RdrTreeNode trueNode = new RdrTreeNode();
while(treeNodes.hasMoreElements()){
currNode = (RdrTreeNode)treeNodes.nextElement();
System.out.println("Node number: "+currNode.getId());
falseNode = currNode.getFalseNode();
trueNode = currNode.getTrueNode();
if(!(falseNode==null)){
System.out.println(" If false: "+falseNode.getId());
}
if(!(trueNode==null)){
System.out.println(" If true: "+trueNode.getId());
}
}
}
public String searchTree(AlarmSet alarms){
System.out.println("Inside the searchTree()");
RdrTreeNode firstNode = getRootNode();
RdrTreeNode lastNode = new RdrTreeNode();
RdrTreeNode[] nodes = new RdrTreeNode[2];
lastPair = firstNode.recursiveSearch(alarms,firstNode);
lastNode = lastPair[0];
firstNode = lastPair[1];
//^-this node is the one to be added to if we need to fix the tree
String conclusion = new String();
conclusion = lastNode.getConclusion();
return(conclusion);
}
}