コード例 #1
0
ファイル: statemachine_test.go プロジェクト: AlexAtNet/hps
func Test_StateMachineWithParameterToJson(t *testing.T) {
	var m sm.Machine
	err := json.Unmarshal([]byte(`{
		"name" : "test",
		"machine" : "default",
		"states" : [ "ok", "fail" ],
		"properties" : { "failure_rate" : { "type" : "ITrigger" } },
		"transitions" : {
			"ok" : { "fail" : [ { "type" : "property", "property" : "failure_rate" } ] },
			"fail" : { "ok" : [ { "type" : "deterministic", "parameter" : 2 } ] }
		}
	}`), &m)
	assert.Nil(t, err)

	duration2h, _ := tools.ParseDuration("2h")
	property, _ := m.Property("failure_rate")
	property.SetValue(triggers.NewDeterministicTrigger(duration2h))

	jsonBin, _ := json.MarshalIndent(&m, "", " ")
	jsonStr := string(jsonBin)
	expected := strings.Join([]string{
		"{",
		" \"name\": \"test\",",
		" \"machine\": \"default\",",
		" \"states\": {",
		"  \"ok\": {",
		"   \"name\": \"ok\",",
		"   \"transitions\": {",
		"    \"fail\": {",
		"     \"to\": \"fail\"",
		"    }",
		"   }",
		"  },",
		"  \"fail\": {",
		"   \"name\": \"fail\",",
		"   \"transitions\": {",
		"    \"ok\": {",
		"     \"to\": \"ok\"",
		"    }",
		"   }",
		"  }",
		" },",
		" \"state\": \"ok\",",
		" \"properties\": {",
		"  \"failure_rate\": {",
		"   \"name\": \"failure_rate\",",
		"   \"value\": {",
		"    \"type\": \"deterministic\",",
		"    \"parameter\": 0.00022831050228310502",
		"   },",
		"   \"required\": true",
		"  }",
		" }",
		"}",
	}, "\n")
	if jsonStr != expected {
		fmt.Println(jsonStr)
		t.Errorf("Unexpected JSON serialisation")
	}
}
コード例 #2
0
func Test_DeterministicTrigger_Marshal(t *testing.T) {
	var trigger *triggers.DeterministicTrigger
	var data []byte
	var err error

	// marshal parameter without comment
	trigger = triggers.NewDeterministicTrigger(0.123)
	data, err = json.Marshal(trigger)
	assert.Nil(t, err)
	assert.Equal(t, string(data), `{"type":"deterministic","parameter":0.123}`)

	// marshal parameter with comment
	trigger = triggers.NewDeterministicTrigger(0.123)
	trigger.SetComment("ok")
	data, err = json.Marshal(trigger)
	assert.Nil(t, err)
	assert.Equal(t, string(data), `{"type":"deterministic","parameter":0.123,"comment":"ok"}`)
}
コード例 #3
0
func Test_CreateTransition(t *testing.T) {
	tr := sm.CreateTransition("test", triggers.NewDeterministicTrigger(0))
	if tr.To() != "test" {
		t.Errorf("The event target should be \"test\".")
	}
}
コード例 #4
0
func Test_DeterministicTrigger_Time(t *testing.T) {
	trigger := triggers.NewDeterministicTrigger(1.0)
	time := trigger.Time(2.0)
	assert.InEpsilon(t, time, 3.0, 0.001)
}
コード例 #5
0
ファイル: Factory.go プロジェクト: AlexAtNet/hps
func (f *Factory) loadTriggerFromJSON(b []byte) (sm.ITrigger, error) {
	var t sm.ITrigger
	var err error

	data := make(map[string]interface{})

	err = json.Unmarshal(b, &data)
	if err != nil {
		return nil, err
	}

	jm := tools.JsonMap(data)

	if transitionType, ok := jm.GetString("type"); ok {
		comment, _ := jm.GetString("comment")

		switch transitionType {
		case "idle":
			t = triggers.NewIdleTrigger()

		case "deterministic":
			if parameter, ok := jm.GetString("parameter"); ok {
				if parameter == "never" {
					fmt.Println("Deprecated: trigger deterministic/never trigger should be replaced with idle trigger type.")
					t = triggers.NewIdleTrigger()
				} else if value, err := tools.ParseDuration(parameter); err == nil {
					fmt.Println("Deprecated: trigger parameter should be numeric.")
					t = triggers.NewDeterministicTrigger(value)
				} else {
					t = triggers.NewIdleTrigger()
					err = fmt.Errorf("Cannot parse transition's deterministic parameter.")
				}
			} else if parameter, ok := jm.GetFloat("parameter"); ok {
				t = triggers.NewDeterministicTrigger(parameter)
			} else if parameter, ok := jm.GetInt("parameter"); ok {
				t = triggers.NewDeterministicTrigger(float64(parameter))
			} else {
				t = triggers.NewIdleTrigger()
				err = fmt.Errorf("Cannot parse transition's deterministic parameter.")
			}

		case "probabilistic":
			if distribution, ok := jm.GetString("distribution"); ok {
				switch distribution {
				case "exponential":
					if param, ok := jm.GetString("parameter"); ok {
						if value, err := tools.ParseDuration(param); err == nil {
							fmt.Println("Deprecated: trigger parameter should be numeric.")
							t = triggers.NewProbabilisticTrigger(value)
							t.SetComment(comment)
						} else {
							t = triggers.NewIdleTrigger()
							err = fmt.Errorf("Cannot parse transition's exponential distribution parameter.")
						}
					} else {
						if value, ok := jm.GetFloat("parameter"); ok {
							t = triggers.NewProbabilisticTrigger(value)
						} else if value, ok := jm.GetInt("parameter"); ok {
							t = triggers.NewProbabilisticTrigger(float64(value))
						} else {
							t = triggers.NewIdleTrigger()
							err = fmt.Errorf("Cannot parse transition's exponential distribution parameter.")
						}
					}
				default:
					t = triggers.NewIdleTrigger()
					err = fmt.Errorf("This distribution is not supported.")
				}
			} else {
				t = triggers.NewIdleTrigger()
				err = fmt.Errorf("Cannot parse transition's distribution type.")
			}

		case "property":
			if property, ok := jm.GetString("property"); ok {
				t = triggers.NewPropertyTrigger(property)
			} else {
				t = triggers.NewIdleTrigger()
				err = fmt.Errorf("Cannot parse transition.")
			}
		}

		t.SetComment(comment)
	} else {
		t = triggers.NewIdleTrigger()
		err = fmt.Errorf("Cannot parse transition type.")
	}

	return t, err
}