示例#1
0
func Test_CheckTransitionFrequency(t *testing.T) {
	experiment := func(parameter float64, frequency float64) {
		tr := sm.CreateTransition("", triggers.NewProbabilisticTrigger(parameter))
		sum := 0.0
		for i := 0; i < 10000; i++ {
			sum += tr.Trigger().Time(0.0)
		}
		if math.Abs(sum/10000.0-frequency) > frequency*0.1 {
			t.Errorf("Expected frequency %v but have got %v", frequency, sum/10000.0)
		}
	}

	// 100 times in a time slot
	experiment(100.0, 0.01)

	// 10 times in a time slot
	experiment(10, 0.1)

	// once in a time slot
	experiment(1, 1)

	// once in 10 time slots
	experiment(0.1, 10)

	// once in 100 time slots
	experiment(0.01, 100)
}
示例#2
0
func Test_ProbabilisticTrigger_Marshal(t *testing.T) {
	var trigger *triggers.ProbabilisticTrigger
	var data []byte
	var err error

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

	// marshal parameter with comment
	trigger = triggers.NewProbabilisticTrigger(0.123)
	trigger.SetComment("ok")
	data, err = json.Marshal(trigger)
	assert.Nil(t, err)
	assert.Equal(t, string(data), `{"type":"probabilistic","distribution":"exponential","parameter":0.123,"comment":"ok"}`)
}
示例#3
0
func Test_ProbabilisticTrigger_Create(t *testing.T) {
	_ = triggers.NewProbabilisticTrigger(1.0)
}
示例#4
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
}