Пример #1
0
func main() {
	fsm := fsm.NewFSM(
		"closed",
		fsm.Events{
			{Name: "open", Src: []string{"closed"}, Dst: "open"},
			{Name: "close", Src: []string{"open"}, Dst: "closed"},
		},
		fsm.Callbacks{},
	)

	fmt.Println(fsm.Current())

	err := fsm.Event("open")
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(fsm.Current())

	err = fsm.Event("close")
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(fsm.Current())
}
Пример #2
0
func main() {
	fsm := fsm.NewFSM(
		"idle",
		fsm.Events{
			{Name: "scan", Src: []string{"idle"}, Dst: "scanning"},
			{Name: "working", Src: []string{"scanning"}, Dst: "scanning"},
			{Name: "situation", Src: []string{"scanning"}, Dst: "scanning"},
			{Name: "situation", Src: []string{"idle"}, Dst: "idle"},
			{Name: "finish", Src: []string{"scanning"}, Dst: "idle"},
		},
		fsm.Callbacks{
			"scan": func(e *fsm.Event) {
				fmt.Println("after_scan: " + e.FSM.Current())
			},
			"working": func(e *fsm.Event) {
				fmt.Println("working: " + e.FSM.Current())
			},
			"situation": func(e *fsm.Event) {
				fmt.Println("situation: " + e.FSM.Current())
			},
			"finish": func(e *fsm.Event) {
				fmt.Println("finish: " + e.FSM.Current())
			},
		},
	)

	fmt.Println(fsm.Current())

	err := fsm.Event("scan")
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("1:" + fsm.Current())

	err = fsm.Event("working")
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("2:" + fsm.Current())

	err = fsm.Event("situation")
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("3:" + fsm.Current())

	err = fsm.Event("finish")
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("4:" + fsm.Current())

}
Пример #3
0
func GetNextState(currentState, event string) (string, []string) {
	initialState := currentState
	notProceedingEvents := map[string]bool{
		"NotInterested":  true,
		"ClosedLost":     true,
		"SWJobCancelled": true,
		"Unresponsive":   true,
	}

	disqualifiedEvent := map[string]bool{
		"Disqualified":        true,
		"Insufficient Credit": true,
		"Non-ownership":       true,
	}

	if notProceedingEvents[event] {
		initialState = "NotProceeding"
	} else if disqualifiedEvent[event] {
		initialState = "Disqualified"
	}

	fsm := fsm.NewFSM(
		initialState,
		fsm.Events{
			{
				Name: "OutboundTask",
				Src:  []string{state["Attempting-Contact:Open"]},
				Dst:  state["Attempting-Contact:Contacted"],
			}, {
				Name: "RequiredDealFieldsFilled",
				Src:  []string{state["Attempting-Contact:Open"], state["Attempting-Contact:Contacted"]},
				Dst:  state["Qualification:DealInfoVerified"],
			}, {
				Name: "ConsultationScheduled",
				Src:  []string{state["Qualification:DealInfoVerified"]},
				Dst:  state["Qualification:ConsultationScheduled"],
			}, {
				Name: "DealUsageNotNull",
				Src:  []string{state["Attempting-Contact:Contacted"], state["Qualification:DealInfoVerified"]},
				Dst:  state["Consultation:UtilityUsageCollected"],
			}, {
				Name: "DealUsageNotNull",
				Src:  []string{state["Qualification:ConsultationScheduled"]},
				Dst:  state["Consultation:UtilityUsageCollected"],
			}, {
				Name: "CreditPassed",
				Src:  []string{state["Qualification:ConsultationScheduled"]},
				Dst:  state["Consultation:CreditPassed"],
			}, {
				Name: "CreditPassed",
				Src:  []string{state["Consultation:UtilityUsageCollected"], state["Consultation:InitialSysDesign"], state["Consultation:ProposalPresented"]},
				Dst:  state["Consultation:CreditPassed"],
			}, {
				Name: "DesignGenerated",
				Src:  []string{state["Qualification:ConsultationScheduled"], state["Consultation:UtilityUsageCollected"], state["Consultation:CreditPassed"]},
				Dst:  state["Consultation:InitialSysDesign"],
			}, {
				Name: "PresentationOpened",
				Src:  []string{state["Consultation:CreditPassed"], state["Consultation:InitialSysDesign"]},
				Dst:  state["Consultation:Presented"],
			}, {
				Name: "IntialContractSigned",
				Src:  []string{state["Consultation:CreditPassed"], state["Consultation:Presented"]},
				Dst:  state["Consultation:ContractSigned"],
			},
		},
		fsm.Callbacks{
			"before_IntialContractSigned": func(e *fsm.Event) {

			},
			"onNotProceeding": func(e *fsm.Event) {
				fmt.Printf("After Not Proceeding")
			},
			"outbound": func(e *fsm.Event) {
				fmt.Println("after_scan: " + e.FSM.Current())
			},
			"working": func(e *fsm.Event) {
				fmt.Println("working: " + e.FSM.Current())
			},
			"situation": func(e *fsm.Event) {
				fmt.Println("situation: " + e.FSM.Current())
			},
			"finish": func(e *fsm.Event) {
				fmt.Println("finish: " + e.FSM.Current())
			},
			"onenterstate": func(e *fsm.Event) {
				fmt.Println("In All states: " + e.FSM.Current())
			},
		},
	)
	fsm.Event(event)
	var possibleEvents []string
	for k, _ := range state {
		if fsm.Can(k) {
			possibleEvents = append(possibleEvents, k)
		}
	}
	return fsm.Current(), possibleEvents

}