-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
4,624 additions
and
2,813 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* The MIT License | ||
* | ||
* Copyright (c) 2017, Mahmoud Ben Hassine ([email protected]) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.myrobotlab.fsm.api; | ||
|
||
import org.myrobotlab.fsm.util.Utils; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* Abstract class for events to which a FSM should react and make transitions. | ||
* | ||
* @author Mahmoud Ben Hassine ([email protected]) | ||
*/ | ||
public abstract class Event { | ||
|
||
protected String name; | ||
protected long timestamp; | ||
|
||
protected Event() { | ||
this.name = Utils.DEFAULT_EVENT_NAME; | ||
timestamp = System.currentTimeMillis(); | ||
} | ||
|
||
protected Event(final String name) { | ||
this.name = name; | ||
timestamp = System.currentTimeMillis(); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public long getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
final StringBuilder sb = new StringBuilder(); | ||
sb.append("Event"); | ||
sb.append("{name='").append(name).append('\''); | ||
sb.append(", timestamp=").append(new Date(timestamp)); | ||
sb.append('}'); | ||
return sb.toString(); | ||
} | ||
|
||
public abstract String getId(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* The MIT License | ||
* | ||
* Copyright (c) 2017, Mahmoud Ben Hassine ([email protected]) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.myrobotlab.fsm.api; | ||
|
||
|
||
/** | ||
* Abstraction for actions to perform when an event is triggered. | ||
* | ||
* @author Mahmoud Ben Hassine ([email protected]) | ||
*/ | ||
public interface EventHandler { | ||
|
||
/** | ||
* Action method to execute when an event occurs. | ||
* @param event the triggered event | ||
* @throws Exception thrown if a problem occurs during action performing | ||
*/ | ||
void handleEvent(Event event) throws Exception; | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
src/main/java/org/myrobotlab/fsm/api/FiniteStateMachine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* The MIT License | ||
* | ||
* Copyright (c) 2017, Mahmoud Ben Hassine ([email protected]) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.myrobotlab.fsm.api; | ||
|
||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* FSM interface. This is the main abstraction for a finite state machine. | ||
* | ||
* @author Mahmoud Ben Hassine ([email protected]) | ||
*/ | ||
public interface FiniteStateMachine { | ||
|
||
/** | ||
* Return current FSM state. | ||
* @return current FSM state | ||
*/ | ||
State getCurrentState(); | ||
|
||
/** | ||
* Return FSM initial state. | ||
* @return FSM initial state | ||
*/ | ||
|
||
// State getInitialState(); | ||
|
||
/** | ||
* Return FSM final states. | ||
* @return FSM final states | ||
*/ | ||
Set<State> getFinalStates(); | ||
|
||
/** | ||
* Return FSM registered states. | ||
* @return FSM registered states | ||
*/ | ||
Set<State> getStates(); | ||
|
||
/** | ||
* Return FSM registered transitions. | ||
* @return FSM registered transitions | ||
*/ | ||
Set<Transition> getTransitions(); | ||
|
||
/** | ||
* Return the last triggered event. | ||
* @return the last triggered event | ||
*/ | ||
Event getLastEvent(); | ||
|
||
/** | ||
* Return the last transition made. | ||
* @return the last transition made | ||
*/ | ||
Transition getLastTransition(); | ||
|
||
/** | ||
* Fire an event. According to event type, the FSM will make the right transition. | ||
* @param event to fire | ||
* @return The next FSM state defined by the transition to make | ||
* @throws FiniteStateMachineException thrown if an exception occurs during event handling | ||
*/ | ||
// State fire(Event event) throws FiniteStateMachineException; | ||
|
||
List<String> fire(Event event) throws FiniteStateMachineException; | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/org/myrobotlab/fsm/api/FiniteStateMachineException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* The MIT License | ||
* | ||
* Copyright (c) 2017, Mahmoud Ben Hassine ([email protected]) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.myrobotlab.fsm.api; | ||
|
||
|
||
/** | ||
* Exception thrown if a problem occurs during event handling. | ||
* This class gives access to the {@link Transition} and {@link Event} related to the exception. | ||
* | ||
* @author Mahmoud Ben Hassine ([email protected]) | ||
*/ | ||
public class FiniteStateMachineException extends Exception { | ||
|
||
/** | ||
* The transition where the exception occurred. | ||
*/ | ||
private Transition transition; | ||
|
||
/** | ||
* The event triggered when the exception occurred. | ||
*/ | ||
private Event event; | ||
|
||
/** | ||
* The root cause of the exception. | ||
*/ | ||
private Throwable cause; | ||
|
||
/** | ||
* Create a new {@link FiniteStateMachineException}. | ||
* | ||
* @param transition where the exception occurred | ||
* @param event triggered when the exception occurred | ||
* @param cause root cause of the exception | ||
*/ | ||
public FiniteStateMachineException(final Transition transition, final Event event, final Throwable cause) { | ||
this.transition = transition; | ||
this.event = event; | ||
this.cause = cause; | ||
} | ||
|
||
/** | ||
* Get the transition where the exception occurred. | ||
* @return the transition where the exception occurred. | ||
*/ | ||
public Transition getTransition() { | ||
return transition; | ||
} | ||
|
||
/** | ||
* Get the event triggered when the exception occurred. | ||
* @return the event triggered when the exception occurred. | ||
*/ | ||
public Event getEvent() { | ||
return event; | ||
} | ||
|
||
/** | ||
* Get the root cause of the exception. | ||
* @return the root cause of the exception | ||
*/ | ||
public Throwable getCause() { | ||
return cause; | ||
} | ||
} |
Oops, something went wrong.