Esempio n. 1
0
func NewTransitionTable(C database.C) *TransitionTable {
	result := &TransitionTable{
		collection:    C,
		table:         make(map[term.SettingID]Transition),
		continuations: make(map[term.SettingID]([]*term.Setting)),
	}
	for _, transition := range C.All() {
		settingRecord, ok := transition["key"]
		if !ok {
			settingRecord, ok = transition["setting"]
		}
		if !ok {
			logger.Println("failed to find setting record in", transition)
			continue
		}
		actionRecord, ok := transition["value"]
		if !ok {
			actionRecord, ok = transition["action"]
		}
		if !ok {
			logger.Println("failed to find action record in", transition)
			continue
		}
		setting, ok := term.LoadSetting(settingRecord)
		if !ok {
			logger.Println("failed to load setting from", settingRecord)
			continue
		}
		action, ok := term.LoadActionC(actionRecord)
		if !ok {
			logger.Println("failed to load action from", actionRecord)
			continue
		}
		if !action.IsValid() {
			logger.Println("loaded invalid action", action)
			continue
		}
		result.SetSimpleC(setting, action)
	}
	logger.Println("length:", len(result.table))
	return result
}
Esempio n. 2
0
func TestEncoding(t *testing.T) {
	collection := Collection("testing")
	collection.Empty("testing")
	template := term.Make("test [] term")
	c := template.C(term.ReferenceC{0})
	cc := term.ConstC{template.T(term.Make("stub").T())}
	action := term.ReturnC(c)
	settingS := term.InitS().AppendTemplate(template, "q").AppendAction(action)
	setting := settingS.Setting
	collection.Set(1, term.SaveSetting(setting))
	collection.Set(2, term.SaveC(cc))
	found := 0
	for _, x := range collection.All() {
		if x["key"] == 1 {
			t.Log(x["value"])
			newVal, ok := term.LoadSetting(x["value"])
			if !ok {
				t.Error("failed to load setting")
			}
			newID := newVal.ID
			oldID := setting.ID
			if newID != oldID {
				t.Errorf("%v != %v", newVal, setting)
			}
			found++
		}
		if x["key"] == 2 {
			newVal, ok := term.LoadC(x["value"])
			if !ok {
				t.Error("failed to load C")
			}
			newID := term.IDC(newVal)
			oldID := term.IDC(cc)
			if newID != oldID {
				t.Errorf("%v != %v", newVal, cc)
			}
			found++
		}
	}
	if found < 2 {
		t.Errorf("found %d < 2 items", found)
	}
	{
		savedSetting, ok := collection.Get(1)
		if !ok {
			t.Error("failed to retrieve from database")
		}
		newSetting, ok := term.LoadSetting(savedSetting)
		if !ok {
			t.Errorf("failed to load setting %v", savedSetting)
		}
		newID := newSetting.ID
		oldID := setting.ID
		if newID != oldID {
			t.Errorf("%v != %v", newSetting, setting)
		}
	}
	{
		savedC, ok := collection.Get(2)
		if !ok {
			t.Error("failed to retriev from database")
		}
		newC, ok := term.LoadC(savedC)
		if !ok {
			t.Errorf("failed to load %v", savedC)
		}
		newID := term.IDC(newC)
		oldID := term.IDC(cc)
		if newID != oldID {
			t.Errorf("%v != %v", newC, cc)
		}
	}
}