Skip to content

Commit

Permalink
state change definition
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick committed Dec 5, 2023
1 parent 2f50853 commit 10eb5e0
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions src/main/java/org/myrobotlab/service/FiniteStateMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.myrobotlab.framework.Service;
import org.myrobotlab.framework.interfaces.MessageListener;
import org.myrobotlab.framework.interfaces.ServiceInterface;
import org.myrobotlab.generics.SlidingWindowList;
import org.myrobotlab.logging.Level;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.LoggingFactory;
Expand Down Expand Up @@ -51,7 +52,7 @@ public class FiniteStateMachine extends Service<FiniteStateMachineConfig> {
/**
* state history of fsm
*/
protected List<String> history = new ArrayList<>();
protected List<StateChange> history = new SlidingWindowList<>(100);

// TODO - .from("A").to("B").on(Messages.ANY)
// TODO - .from("A").to("B").on(Messages.EMPTY)
Expand All @@ -65,17 +66,27 @@ public class Tuple {
}

public class StateChange {
public String last;
public String current;
/**
* timestamp
*/
public long ts = System.currentTimeMillis();

/**
* current new state
*/
public String state;

/**
* event which activated new state
*/
public String event;
public StateChange(String last, String current, String event) {
this.last = last;
this.current = current;
public StateChange(String current, String event) {
this.state = current;
this.event = event;
}

public String toString() {
return String.format("%s --%s--> %s", last, event, current);
return String.format("%s --%s--> %s", last, event, state);
}
}

Expand Down Expand Up @@ -113,11 +124,8 @@ public String getNext(String key) {
public void init() {
stateMachine.init();
State state = stateMachine.getCurrent();
if (history.size() > 100) {
history.remove(0);
}
if (state != null) {
history.add(state.getName());
history.add(new StateChange(state.getName(), String.format("%s.setCurrent", getName())));
}
}

Expand Down Expand Up @@ -194,8 +202,9 @@ public void fire(String event) {
log.info("fired event ({}) -> ({}) moves to ({})", event, last == null ? null : last.getName(), current == null ? null : current.getName());

if (last != null && !last.equals(current)) {
invoke("publishStateChange", new StateChange(last.getName(), current.getName(), event));
history.add(current.getName());
StateChange stateChange = new StateChange(current.getName(), event);
invoke("publishStateChange", stateChange);
history.add(stateChange);
}
} catch (Exception e) {
log.error("fire threw", e);
Expand Down Expand Up @@ -247,7 +256,7 @@ public StateChange publishStateChange(StateChange stateChange) {
for (String listener : messageListeners) {
ServiceInterface service = Runtime.getService(listener);
if (service != null) {
org.myrobotlab.framework.Message msg = org.myrobotlab.framework.Message.createMessage(getName(), listener, CodecUtils.getCallbackTopicName(stateChange.current), null);
org.myrobotlab.framework.Message msg = org.myrobotlab.framework.Message.createMessage(getName(), listener, CodecUtils.getCallbackTopicName(stateChange.state), null);
service.in(msg);
}
}
Expand Down Expand Up @@ -419,12 +428,20 @@ public void setCurrent(String state) {
stateMachine.setCurrent(state);
current = stateMachine.getCurrent();
if (last != null && !last.equals(current)) {
invoke("publishStateChange", new StateChange(last.getName(), current.getName(), null));
invoke("publishStateChange", new StateChange(current.getName(), String.format("%s.setCurrent", getName())));
}
} catch (Exception e) {
log.error("setCurrent threw", e);
error(e.getMessage());
}
}

public String getPreviousState() {
if (history.size() == 0) {
return null;
} else {
return history.get(history.size() - 2).state;
}
}

}

0 comments on commit 10eb5e0

Please sign in to comment.