Skip to content

Commit

Permalink
StateMachine#generatePlantUML() add return string
Browse files Browse the repository at this point in the history
  • Loading branch information
dudiao committed Jul 7, 2020
1 parent b4fc71d commit 1315a04
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ public interface StateMachine<S, E, C> extends Visitable{
*/
void showStateMachine();

void generatePlantUML();
String generatePlantUML();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
* @date 2020-02-08 8:41 PM
*/
public interface Visitable {
void accept(final Visitor visitor);
String accept(final Visitor visitor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,30 @@
* @date 2020-02-08 8:41 PM
*/
public interface Visitor {

char LF = '\n';

/**
* @param visitable the element to be visited.
* @return
*/
void visitOnEntry(StateMachine<?, ?, ?> visitable);
String visitOnEntry(StateMachine<?, ?, ?> visitable);

/**
* @param visitable the element to be visited.
* @return
*/
void visitOnExit(StateMachine<?, ?, ?> visitable);
String visitOnExit(StateMachine<?, ?, ?> visitable);

/**
* @param visitable the element to be visited.
* @return
*/
void visitOnEntry(State<?, ?, ?> visitable);
String visitOnEntry(State<?, ?, ?> visitable);

/**
* @param visitable the element to be visited.
* @return
*/
void visitOnExit(State<?, ?, ?> visitable);
String visitOnExit(State<?, ?, ?> visitable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,34 @@ public class PlantUMLVisitor implements Visitor {
* otherwise it will be recognized as a sequence diagram.
*
* @param visitable the element to be visited.
* @return
*/
@Override
public void visitOnEntry(StateMachine<?, ?, ?> visitable) {
System.out.println("@startuml");
public String visitOnEntry(StateMachine<?, ?, ?> visitable) {
return "@startuml" + LF;
}

@Override
public void visitOnExit(StateMachine<?, ?, ?> visitable) {
System.out.println("@enduml");
public String visitOnExit(StateMachine<?, ?, ?> visitable) {
return "@enduml";
}

@Override
public void visitOnEntry(State<?, ?, ?> state) {
public String visitOnEntry(State<?, ?, ?> state) {
StringBuilder sb = new StringBuilder();
for(Transition transition: state.getTransitions()){
System.out.println(transition.getSource().getId()+" --> "+transition.getTarget().getId()+" : "+transition.getEvent());
sb.append(transition.getSource().getId())
.append(" --> ")
.append(transition.getTarget().getId())
.append(" : ")
.append(transition.getEvent())
.append(LF);
}
return sb.toString();
}

@Override
public void visitOnExit(State<?, ?, ?> state) {
public String visitOnExit(State<?, ?, ?> state) {
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ public S getId() {
}

@Override
public void accept(Visitor visitor) {
visitor.visitOnEntry(this);
visitor.visitOnExit(this);
public String accept(Visitor visitor) {
String entry = visitor.visitOnEntry(this);
String exit = visitor.visitOnExit(this);
return entry + exit;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ private void isReady() {
}

@Override
public void accept(Visitor visitor) {
visitor.visitOnEntry(this);
public String accept(Visitor visitor) {
StringBuilder sb = new StringBuilder();
sb.append(visitor.visitOnEntry(this));
for(State state: stateMap.values()){
state.accept(visitor);
sb.append(state.accept(visitor));
}
visitor.visitOnExit(this);
sb.append(visitor.visitOnExit(this));
return sb.toString();
}

@Override
Expand All @@ -76,9 +78,9 @@ public void showStateMachine() {
}

@Override
public void generatePlantUML(){
public String generatePlantUML(){
PlantUMLVisitor plantUMLVisitor = new PlantUMLVisitor();
accept(plantUMLVisitor);
return accept(plantUMLVisitor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,37 @@
* @date 2020-02-08 8:48 PM
*/
public class SysOutVisitor implements Visitor {

@Override
public void visitOnEntry(StateMachine<?, ?, ?> stateMachine) {
System.out.println("-----StateMachine:"+stateMachine.getMachineId()+"-------");
public String visitOnEntry(StateMachine<?, ?, ?> stateMachine) {
String entry = "-----StateMachine:"+stateMachine.getMachineId()+"-------";
System.out.println(entry);
return entry;
}

@Override
public void visitOnExit(StateMachine<?, ?, ?> stateMachine) {
System.out.println("------------------------");
public String visitOnExit(StateMachine<?, ?, ?> stateMachine) {
String exit = "------------------------";
System.out.println(exit);
return exit;
}

@Override
public void visitOnEntry(State<?, ?, ?> state) {
System.out.println("State:"+state.getId());
public String visitOnEntry(State<?, ?, ?> state) {
StringBuilder sb = new StringBuilder();
String stateStr = "State:"+state.getId();
sb.append(stateStr).append(LF);
System.out.println(stateStr);
for(Transition transition: state.getTransitions()){
System.out.println(" Transition:"+transition);
String transitionStr = " Transition:"+transition;
sb.append(transitionStr).append(LF);
System.out.println(transitionStr);
}

return sb.toString();
}

@Override
public void visitOnExit(State<?, ?, ?> visitable) {

public String visitOnExit(State<?, ?, ?> visitable) {
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ public void testPlantUML(){
.perform(doAction()));

StateMachine stateMachine = builder.build("AdjustPriceTask");
stateMachine.generatePlantUML();
String plantUML = stateMachine.generatePlantUML();
System.out.println(plantUML);

}

Expand Down

0 comments on commit 1315a04

Please sign in to comment.