001 /*
002 * Created on May 19, 2007
003 */
004 package com.x8ing.lsm4j.std;
005
006 import java.io.PrintStream;
007
008 import com.x8ing.lsm4j.Condition;
009 import com.x8ing.lsm4j.GraphListener;
010 import com.x8ing.lsm4j.StateContext;
011 import com.x8ing.lsm4j.state.ProcessableState;
012
013 /**
014 * A default listener implementation which just traces out some information to the console or a specified PrintStream.
015 *
016 * @author Patrick Heusser
017 */
018 public class TracingGraphListener implements GraphListener {
019
020 private PrintStream output = null;
021
022 public TracingGraphListener(PrintStream output) {
023 this.output = output;
024 }
025
026 public TracingGraphListener() {
027 this.output = System.out;
028 }
029
030 public void changedState(ProcessableState previousState, ProcessableState currentState, Condition previousCondition,
031 Condition currentCondition, long loop, StateContext currentStateContext) {
032
033 startTrace();
034
035 output.print("changedState: ");
036
037 output.print("previousState");
038 appendState(previousState);
039 output.print("currentState");
040 appendState(currentState);
041
042 output.print(" previousCondition");
043 appendCondition(previousCondition);
044
045 output.print(" currentCondition");
046 appendCondition(currentCondition);
047
048 output.print("\n");
049
050 }
051
052 private void appendCondition(Condition condition) {
053
054 output.print(" {");
055 if (condition != null) {
056 output.print(condition.traceInfo());
057 } else {
058 output.print("null");
059 }
060 output.print("} ");
061 }
062
063 private void appendState(ProcessableState state) {
064
065 output.print(" {");
066
067 if (state != null) {
068
069 output.print(state.getUniqueIDString());
070 output.print(",");
071 output.print(state.getDescription());
072
073 } else {
074
075 output.print("null");
076 }
077
078 output.print("} ");
079
080 }
081
082 private void startTrace() {
083 output.print("[TracingGraphListener] ");
084 }
085
086 public void startProcessingState(ProcessableState previousState, Condition previosCondition, long loop,
087 StateContext currentStateContext) {
088
089 startTrace();
090
091 output.print("startProcessingState: ");
092 appendState(previousState);
093 output.print("\n");
094
095 }
096
097 public void foundEndState(ProcessableState endState, long loop, StateContext currentStateContext) {
098
099 startTrace();
100
101 output.print("foundEndState: id=");
102 output.print(endState.getUniqueIDString());
103 output.print("\n");
104
105 }
106
107 }
|