Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed introduction of squirrel-foundation finite state machine

2025-02-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "detailed introduction of squirrel-foundation finite state machine". The explanation content in this article is simple and clear, and it is easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn the "detailed introduction of squirrel-foundation finite state machine" together.

What is a finite state machine?

Finite state machine: A tool for modeling object behavior, describing the sequence of states an object experiences during its life cycle and how it responds to events from the outside world. In computer science, finite state machines are widely used for modeling application behavior, hardware circuit system design, software engineering, compilers, network protocols, and the study of computation and languages.

This article focuses on learning an open source implementation of java squirrel-foundation state machine

Element 1 of the state machine is present state, condition, action and secondary state. "Present state" and "condition" are causes,"action" and "secondary state" are effects Why use state machines

Generally speaking, as the project develops, the code becomes more and more complex, and the relationship between states, such as order system state, activity system state, etc., becomes more and more complex. It's hard to have one person who can sort out the whole picture of a product or development. Using state machines to manage object lifecycles can make code more readable, maintainable, and testable.

A finite state machine is a behavioral modeling tool for objects that have a well-defined and complex life flow (generally more than three states) and have different trigger conditions and processing behaviors for state transitions.

Let's look at the example given above on squirrel-foundation github to have an intuitive understanding of the state machine.

demo

Add maven dependency

123456 org.squirrelframework squirrel-foundation 0.3.8

QuickStartSample demo

12345678910111213141516171819202122232425262728293031323334353637383940414243import org.squirrelframework.foundation.fsm.StateMachineBuilderFactory;import org.squirrelframework.foundation.fsm.UntypedStateMachine;import org.squirrelframework.foundation.fsm.UntypedStateMachineBuilder;import org.squirrelframework.foundation.fsm.annotation.StateMachineParameters;import org.squirrelframework.foundation.fsm.impl.AbstractUntypedStateMachine;/** * @Author wtx * @Date 2019/5/5 */public class QuickStartSample { // 1. Define State Machine Event enum FSMEvent { ToA, ToB, ToC, ToD } // 2. Define State Machine Class @StateMachineParameters(stateType=String.class, eventType=FSMEvent.class, contextType=Integer.class) static class StateMachineSample extends AbstractUntypedStateMachine { protected void fromAToB(String from, String to, FSMEvent event, Integer context) { System.out.println("Transition from '"+from+"' to '"+to+"' on event '"+event+ "' with context '"+context+"'. "); } protected void ontoB(String from, String to, FSMEvent event, Integer context) { System.out.println("Entry State \'"+to+"\'. "); } } public static void main(String[] args) { // 3. Build State Transitions UntypedStateMachineBuilder builder = StateMachineBuilderFactory.create(StateMachineSample.class); builder.externalTransition().from("A").to("B").on(FSMEvent.ToB).callMethod("fromAToB"); builder.onEntry("B").callMethod("ontoB"); // 4. Use State Machine UntypedStateMachine fsm = builder.newStateMachine("A"); fsm.fire(FSMEvent.ToB, 10); System.out.println("Current state is "+fsm.getCurre5ntState()); }} Define StateMachineBuilderFactory

First use StateMachineBuilderFactory to construct an UntypedStateMachineBuilder, then builder.externalTransition(),builder internalTransition() besides externalTransition,

.from("A").to("B").on(FSMEvent.ToB).callMethod("fromAToB");

When a ToB event occurs in state A, the method fromAToB is called, and then the state transitions to B.

builder.onEntry("B").callMethod("ontoB");

When the state transitions to B, the method ontoB is called.

UntypeStateMachine fsm123 UntypeStateMachine fsm = builder.newStateMachine("A");//Trigger ToB event fsm.fire (FSMEvent.ToB, 10); Get the current state 1fsm.getCurrentState() Thank you for reading, the above is the "squirrel-foundation finite state machine detailed introduction" of the content, after learning this article, I believe we have a deeper understanding of the squirrel-foundation finite state machine detailed introduction of this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 289

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report