Appendix C Correlator
The correlator is the most interesting in this chapter. Much of
the code for the examples is left out since it is extremely
repetitive. This repetition could have been avoided by making
some way to save and load trees and alarm sets from a text file.
The last section is a utility type file for reading the user input
from the command prompt.
C.1 Correlator.java
import javax.swing.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
/** The correlator uses the alarms and an RDR Tree
* to sort out what the alarms mean.
* @author Veronica Clark
* @see Alarm
* @see AlarmState
* @since jdk1.2
*/
public class Correlator
{
private static AlarmSet alarms = new AlarmSet();
private static RdrTree rdrTree = new RdrTree();
public static void main(String args[])
{
RdrTreeNode[] lastPair = new RdrTreeNode[2];
rdrTree = Examples.tree1();
alarms = Examples.alarms1();
System.out.print(" *The Correlator Manager* "+
"\n "+
"\n (Type ? for help)"+
"\n ");
while(1==1){
String op = new String();
String input = new String();
//listen for new instructions
input = V.getInput();
try{
op = input;
if(op==null){continue;}
}catch(NullPointerException npe){
}
if(op.compareTo("start")==0){
String conclusion = new String();
alarms.displayAlarms();
// rdrTree.printTree(); ****This is a handy line if you can't use swing components
conclusion = rdrTree.searchTree(alarms);
System.out.println(conclusion);
continue;
}
if(op.compareTo("?")==0){
System.out.println(helpScreen);
continue;
}
if(op.compareTo("q")==0){
System.out.println("GoodBye!");
break;
}
if(op.compareTo("fix")==0){
String conditions = new String();
String conclusion = new String();
RdrTreeNode newNode = new RdrTreeNode();
AlarmSet cornerStone = new AlarmSet();
lastPair = rdrTree.getLastPair();
conditions = lastPair[1].getConditions();
conclusion = lastPair[0].getConclusion();
System.out.println("The Last Conditions: "+conditions);
System.out.println(" (which generated this conclusion: "+conclusion+")");
System.out.print("Are the conditions satisfied? (yes or no)");
boolean yes = V.getYesNo();
newNode = rdrTree.newTreeNode(lastPair[1],yes);
newNode.setCornerStone(alarms);
cornerStone = lastPair[0].getCornerStone();
cornerStone.displayAlarms();
System.out.print("Enter the conditions for the new rule: ");
conditions = V.getInput();
newNode.setConditions(conditions);
System.out.print("Enter the conclusion for the new rule: ");
conclusion = V.getInput();
newNode.setConclusion(conclusion);
continue;
}
if(op.startsWith("a")){
int egg = 0;
String number = new String();
number = op.substring(1);
System.out.println( "Alarm set # "+number+" loaded");
egg = Integer.parseInt(number.trim());
alarms = Examples.pickSet(egg);
}
if(op.startsWith("t")){
int egg = 0;
String number = new String();
number = op.substring(1);
System.out.println( "Rules Tree # "+number+" loaded");
egg = Integer.parseInt(number.trim());
rdrTree = Examples.pickTree(egg);
}
}
System.exit(0);
}//end of main
static String helpScreen = (" * *********Help Instructions********* *\n"+
"\n"+
"To start the correlator type `start'\n"+
"\n"+
"To fix the last correlation type `fix'\n"+
"\n"+
"To choose a new alarm set type `a' followed by the set number\n"+
"\n"+
"To choose a new rule tree type `t' followed by the tree number\n"+
"\n"+
"To quit type 'quit'\n");
}
C.2 Example.java
import java.lang.*;
import java.util.*;
//Single RDR
/** A class of alarm sets and rdr trees ready for
* correlating.
* @author Veronica Clark
* @see RdrTree
* @see RdrTreeNode
* @see AlarmSet
* @see Alarm
*/
public class Examples {
public static AlarmSet pickSet(int a){
AlarmSet whichSet = new AlarmSet();
switch(a){
case 1: whichSet = alarms1();break;
case 2: whichSet = alarms2();break;
case 3: whichSet = alarms3();break;
case 4: whichSet = alarms4();break;
:
case 32: whichSet = alarms32();break;
case 33: whichSet = alarms33();break;
default: System.out.println("Can't find that set");
}
return(whichSet);
}
public static RdrTree pickTree(int a){
RdrTree whichTree = new RdrTree();
switch(a){
case 1: whichTree = tree1();break;
case 2: whichTree = tree2();break;
case 3: whichTree = tree3();break;
default: System.out.println("Can't find that tree");
}
return(whichTree);
}
/** The Corner Stone case for node 1 of the
* `dinner' tree */
public static AlarmSet alarms1() {
AlarmSet divers = new AlarmSet(1);
Alarm trial0 = new Alarm();
trial0.setAlarm(AlarmState.MIN);
trial0.setObjectId("meat");
trial0.setDescription("12 frozen thick sausages");
divers.addAlarm(trial0);
Here each alarm in the set is defined, but not printed
here, because it takes too long!
Alarm trial4 = new Alarm();
trial4.setAlarm(AlarmState.WAR);
trial4.setObjectId("tired");
trial4.setDescription("No one wants to cook dinner");
divers.addAlarm(trial4);
return(divers);
}
All the other corner stone cases are defined here, as above.
/** The `What shall we have for dinner?' Tree */
public static RdrTree tree1(){
RdrTree arbor = new RdrTree();
RdrTreeNode parent = arbor.getRootNode();
RdrTreeNode node1 = arbor.newTreeNode(parent,true);
node1.setConditions("If today is Friday");
node1.setConclusion("We'll have fish and chips for dinner");
node1.setCornerStone(alarms1());
RdrTreeNode node2 = arbor.newTreeNode(node1,true);
node2.setConditions("If there are eggs with CRITICAL or MAJOR status");
node2.setConclusion("We'll have quiche for dinner");
node2.setCornerStone(alarms2());
Here all the nodes of the tree are defined, but not printed
here, because, again, it takes too long!
RdrTreeNode node11 = arbor.newTreeNode(node4,true);
node11.setConditions("If there are leftover cooked sausages");
node11.setConclusion("We'll have Yorkshire Pudding");
node11.setCornerStone(alarms11());
return(arbor);
}
Similar code is done for the simple telecommunications example,
both defining the tree and the corner stone cases. This
is left out for brevity.
}
C.3 V.java
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
/** Reading data from standard input can be a bit of a hassle
* so here are some utility files to use.
* @author Veronica Clark
* @since JDK1.1.8
*/
public class V{
public static void V(){
}
static StringTokenizer st;
static int i;
/** @return Whatever you type in broken up into a String[3] */
static String[] getInputs() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String shoelace = new String("");
String[] shoelaces = new String[3];
try {
shoelace = in.readLine();
}catch (IOException e) {}
st = new StringTokenizer(shoelace);
i=0;
while (st.hasMoreTokens())
{
shoelaces[i]=st.nextToken();
i++;
}
return(shoelaces);
}//end of getinputs
/** @return whatever you type in as one String */
public static String getInput() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String shoelace = new String("");
try {
shoelace = in.readLine();
}catch (IOException e) {
System.out.println("IO exception in getInput");
}
return(shoelace);
}//end of getinput
/** This is case sensitive. It loops until you type something it understands.
* @return true if you type "yes" and false if "no"
*/
public static boolean getYesNo() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String shoelace = new String("");
Boolean truth = new Boolean("true");
readingLoop: while(1==1){
try {
shoelace = in.readLine();
}catch (IOException e) {
System.out.println("Having trouble reading Shoelace");}
if (shoelace.compareTo("yes")==0){
truth = Boolean.TRUE;
break readingLoop;
}
else if (shoelace.compareTo("no")==0){
truth = Boolean.FALSE;
break readingLoop;
}
else{
System.out.print("Sorry, I don't understand "+shoelace+". Please try again, yes or no?");
}
}
return(truth.booleanValue());
}//end of getinput
}