Beispiel #1
0
func getTransition(d dynamics.Dwimmer, s *term.SettingT, quotedSetting term.T) term.T {
	setting, err := represent.ToSetting(d, quotedSetting)
	if err != nil {
		return term.Make("asked to get a transition in setting [], "+
			"but while converting to a setting received []").T(quotedSetting, err)
	}
	result, ok := d.Get(setting)
	if !ok {
		return NoCompiledAction.T()
	}
	return core.Answer.T(represent.Transition(result))
}
Beispiel #2
0
func findAction(d dynamics.Dwimmer, s *term.SettingT, quotedSetting term.T) term.T {
	setting, err := represent.ToSetting(d, quotedSetting)
	if err != nil {
		return term.Make("asked to decide what to do in setting [], "+
			"but while converting to a setting received []").T(quotedSetting, err)
	}
	transition, ok := d.Get(setting)
	if !ok {
		transition := ElicitAction(d, s, setting)
		d.Save(setting, transition)
	}
	return core.Answer.T(represent.Transition(transition))
}
Beispiel #3
0
func getContinuations(d dynamics.Dwimmer, s *term.SettingT, quotedSetting term.T) term.T {
	setting, err := represent.ToSetting(d, quotedSetting)
	if err != nil {
		return term.Make("asked to return the continuations from a setting, " +
			"but while converting to a setting received []").T(err)
	}
	continuations := d.Continuations(setting)
	result := make([]term.T, len(continuations))
	for i, c := range continuations {
		result[i] = represent.Setting(c)
	}
	return core.Answer.T(represent.List(result))
}
Beispiel #4
0
func setTransition(d dynamics.Dwimmer, s *term.SettingT, quotedTransition, quotedSetting term.T) term.T {
	transition, err := represent.ToTransition(d, quotedTransition)
	if err != nil {
		return term.Make("asked to set a setting to transition [], "+
			"but while converting to a transition received []").T(quotedTransition, err)
	}
	setting, err := represent.ToSetting(d, quotedSetting)
	if err != nil {
		return term.Make("asked to set a transition in setting [], "+
			"but while converting to a setting received []").T(quotedSetting, err)
	}
	d.Save(setting, transition)
	return core.OK.T()
}
Beispiel #5
0
func displayTemplate(d dynamics.Dwimmer, context *term.SettingT, quotedSetting term.T) term.T {
	setting, err := represent.ToSetting(d, quotedSetting)
	if err != nil {
		return represent.ConversionError.T(quotedSetting, err)
	}
	settingS := addNames(setting)
	ShowSettingS(d, settingS)
	d.Println("")
	d.Println("")
	quotedNames := make([]term.T, len(settingS.Names))
	for i, name := range settingS.Names {
		quotedNames[i] = represent.Str(name)
	}
	return Names.T(represent.List(quotedNames))
}
Beispiel #6
0
func getID(d dynamics.Dwimmer, s *term.SettingT, quoted term.T) term.T {
	var result int
	switch quoted.Head() {
	case represent.QuotedSetting:
		setting, err := represent.ToSetting(d, quoted)
		if err != nil {
			return term.Make("was asked to find the ID of a setting, " +
				"but while converting to a setting received []").T(err)
		}
		result = int(setting.ID)
	case term.Int(0).Head():
		result = int(term.IDer.PackInt(int(quoted.(term.Int))).(intern.ID))
	case term.Str("").Head():
		result = int(term.IDer.PackString(string(quoted.(term.Str))).(intern.ID))
	}
	return core.Answer.T(represent.Int(result))
}
Beispiel #7
0
func nativeSuggestedActions(d dynamics.Dwimmer, context *term.SettingT, quotedSetting, quotedN term.T) term.T {
	setting, err := represent.ToSetting(d, quotedSetting)
	if err != nil {
		return term.Make("was asked to generate suggestions in setting [], "+
			"but received [] while converting it to native form").T(quotedSetting, err)
	}
	n, err := represent.ToInt(d, quotedN)
	if err != nil {
		return term.Make("was asked to generate [] suggestions, "+
			"but received [] while converting it to native form").T(quotedN, err)
	}
	suggestions, _ := Suggestions(d, setting, n)
	quotedSuggestions := make([]term.T, len(suggestions))
	for i, suggestion := range suggestions {
		quotedSuggestions[i] = represent.ActionC(suggestion)
	}
	return core.Answer.T(represent.List(quotedSuggestions))

}
Beispiel #8
0
func TestRepresentations(t *testing.T) {
	d := dwimmer.TestDwimmer()
	defer d.Close()
	template := term.Make("term with argument [] and second half here")
	template2, err := represent.ToTemplate(d, represent.Template(template))
	if err != nil {
		t.Errorf("received error %v", err)
	}
	if template != template2 {
		t.Errorf("%v != %v", template, template2)
	}
	setting := term.Init().Append(template)
	setting2, err := represent.ToSetting(d, represent.Setting(setting))
	if err != nil {
		t.Errorf("received error %v", err)
	}
	if term.IDSetting(setting) != term.IDSetting(setting2) {
		t.Errorf("%v != %v", setting, setting2)
	}
	actions := []term.ActionC{term.ReturnC(term.Cr(3)), term.ClarifyC(term.Cr(2), core.OK.C()), term.DeleteC(7)}
	for _, action := range actions {
		action2, err := represent.ToActionC(d, represent.ActionC(action))
		if err != nil {
			t.Errorf("received error %v", err)
		}
		if term.IDActionC(action) != term.IDActionC(action2) {
			t.Errorf("%v != %v", action, action2)
		}
	}
	stub := term.Make("stub")
	tm := template.T(stub.T())
	tm2, err := represent.ToT(d, represent.T(tm))
	if err != nil {
		t.Errorf("received error %v", err)
	}
	if tm2.String() != tm.String() {
		t.Errorf("%v != %v", tm2, tm)
	}
	rep, err := d.Answer(represent.Explicit.T(represent.T(tm)))
	if err != nil {
		t.Errorf("failed to make representation explicit: %v", err)
	}
	tm3, err := represent.ToT(d, rep)
	if err != nil {
		t.Errorf("received error %v", err)
	}
	if tm3.String() != tm.String() {
		t.Errorf("%v != %v", tm3, tm)
	}
	settingT := term.InitT().AppendTerm(tm)
	settingT2, err := represent.ToSettingT(d, represent.SettingT(settingT))
	if err != nil {
		t.Errorf("received error %v")
	}
	if settingT2.Setting.ID != settingT.Setting.ID {
		t.Errorf("%v != %v", settingT2, settingT)
	}

	n := -127
	n2, err := represent.ToInt(d, represent.Int(n))
	if err != nil {
		t.Errorf("received error %v", err)
	}
	if n != n2 {
		t.Errorf("%v != %v", n, n2)
	}

	s := "hello ₳"
	s2, err := represent.ToStr(d, represent.Str(s))
	if err != nil {
		t.Errorf("received error %v", err)
	}
	if s != s2 {
		t.Errorf("%s != %s", s, s2)
	}
}