Skip to content

Commit

Permalink
Move initial buffers into new run options (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaloney authored Mar 1, 2024
1 parent 90dd90f commit 31dc92b
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 51 deletions.
12 changes: 6 additions & 6 deletions framework/ccm_pyactr/ccm_pyactr.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ func (c CCMPyACTR) Model() (model *actr.Model) {

// Run generates the python code from the amod file, writes it to disk, creates a "run" file
// to actually run the model, and returns the output (stdout and stderr combined).
func (c *CCMPyACTR) Run(options *runoptions.Options, initialBuffers framework.InitialBuffers) (result *framework.RunResult, err error) {
runFile, err := c.WriteModel(c.tmpPath, options, initialBuffers)
func (c *CCMPyACTR) Run(options *runoptions.Options) (result *framework.RunResult, err error) {
runFile, err := c.WriteModel(c.tmpPath, options)
if err != nil {
return
}
Expand All @@ -138,7 +138,7 @@ func (c *CCMPyACTR) Run(options *runoptions.Options, initialBuffers framework.In
}

// WriteModel converts the internal actr.Model to Python and writes it to a file.
func (c *CCMPyACTR) WriteModel(path string, options *runoptions.Options, initialBuffers framework.InitialBuffers) (outputFileName string, err error) {
func (c *CCMPyACTR) WriteModel(path string, options *runoptions.Options) (outputFileName string, err error) {
// If our model has a print statement, then write out our support file
if c.model.HasPrintStatement() {
err = framework.WriteSupportFile(path, ccmPrintFileName, ccmPrintPython)
Expand All @@ -165,7 +165,7 @@ func (c *CCMPyACTR) WriteModel(path string, options *runoptions.Options, initial
return "", err
}

_, err = c.GenerateCode(options, initialBuffers)
_, err = c.GenerateCode(options)
if err != nil {
return
}
Expand All @@ -179,8 +179,8 @@ func (c *CCMPyACTR) WriteModel(path string, options *runoptions.Options, initial
}

// GenerateCode converts the internal actr.Model to Python code.
func (c *CCMPyACTR) GenerateCode(options *runoptions.Options, initialBuffers framework.InitialBuffers) (code []byte, err error) {
patterns, err := framework.ParseInitialBuffers(c.model, initialBuffers)
func (c *CCMPyACTR) GenerateCode(options *runoptions.Options) (code []byte, err error) {
patterns, err := framework.ParseInitialBuffers(c.model, options.InitialBuffers)
if err != nil {
return
}
Expand Down
4 changes: 3 additions & 1 deletion framework/ccm_pyactr/ccm_pyactr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/kylelemons/godebug/diff"

"github.com/asmaloney/gactar/framework"

"github.com/asmaloney/gactar/util/cli"
"github.com/asmaloney/gactar/util/runoptions"
)

func init() {
Expand Down Expand Up @@ -56,7 +58,7 @@ func TestCodeGeneration(t *testing.T) {
}

func runCodeGenerationTest(t *testing.T, fw framework.Framework, input, output string) { //nolint to avoid Helper info since it doesn't apply
code, err := framework.GenerateCodeFromFile(fw, input, framework.InitialBuffers{})
code, err := framework.GenerateCodeFromFile(fw, input, runoptions.InitialBuffers{})
if err != nil {
t.Error(err)
return
Expand Down
10 changes: 3 additions & 7 deletions framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,13 @@ type Framework interface {
SetModel(model *actr.Model) (err error)
Model() (model *actr.Model)

Run(options *runoptions.Options, initialBuffers InitialBuffers) (result *RunResult, err error)
WriteModel(path string, options *runoptions.Options, initialBuffers InitialBuffers) (outputFileName string, err error)
GenerateCode(options *runoptions.Options, initialBuffers InitialBuffers) (code []byte, err error)
Run(options *runoptions.Options) (result *RunResult, err error)
WriteModel(path string, options *runoptions.Options) (outputFileName string, err error)
GenerateCode(options *runoptions.Options) (code []byte, err error)
}

type List map[string]Framework

// InitialBuffers is a map of buffer names to initial contents of the buffer.
// This is used when passing in user-defined initial contents e.g. through a web API.
type InitialBuffers map[string]string

// ParsedInitialBuffers is a map of buffer name to a parsed version of the initial contents.
// This is used when passing in user-defined initial contents e.g. through a web API.
type ParsedInitialBuffers map[string]*actr.Pattern
Expand Down
12 changes: 6 additions & 6 deletions framework/pyactr/pyactr.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ func (p PyACTR) Model() (model *actr.Model) {
return p.model
}

func (p *PyACTR) Run(options *runoptions.Options, initialBuffers framework.InitialBuffers) (result *framework.RunResult, err error) {
runFile, err := p.WriteModel(p.tmpPath, options, initialBuffers)
func (p *PyACTR) Run(options *runoptions.Options) (result *framework.RunResult, err error) {
runFile, err := p.WriteModel(p.tmpPath, options)
if err != nil {
return
}
Expand All @@ -157,7 +157,7 @@ func (p *PyACTR) Run(options *runoptions.Options, initialBuffers framework.Initi
}

// WriteModel converts the internal actr.Model to Python and writes it to a file.
func (p *PyACTR) WriteModel(path string, options *runoptions.Options, initialBuffers framework.InitialBuffers) (outputFileName string, err error) {
func (p *PyACTR) WriteModel(path string, options *runoptions.Options) (outputFileName string, err error) {
// If our model has a print statement, then write out our support file
if p.model.HasPrintStatement() {
err = framework.WriteSupportFile(path, pyactrPrintFileName, pyactrPrintPython)
Expand All @@ -176,7 +176,7 @@ func (p *PyACTR) WriteModel(path string, options *runoptions.Options, initialBuf
return "", err
}

_, err = p.GenerateCode(options, initialBuffers)
_, err = p.GenerateCode(options)
if err != nil {
return
}
Expand All @@ -190,8 +190,8 @@ func (p *PyACTR) WriteModel(path string, options *runoptions.Options, initialBuf
}

// GenerateCode converts the internal actr.Model to Python code.
func (p *PyACTR) GenerateCode(options *runoptions.Options, initialBuffers framework.InitialBuffers) (code []byte, err error) {
patterns, err := framework.ParseInitialBuffers(p.model, initialBuffers)
func (p *PyACTR) GenerateCode(options *runoptions.Options) (code []byte, err error) {
patterns, err := framework.ParseInitialBuffers(p.model, options.InitialBuffers)
if err != nil {
return
}
Expand Down
4 changes: 3 additions & 1 deletion framework/pyactr/pyactr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/kylelemons/godebug/diff"

"github.com/asmaloney/gactar/framework"

"github.com/asmaloney/gactar/util/cli"
"github.com/asmaloney/gactar/util/runoptions"
)

func init() {
Expand Down Expand Up @@ -56,7 +58,7 @@ func TestCodeGeneration(t *testing.T) {
}

func runCodeGenerationTest(t *testing.T, fw framework.Framework, input, output string) { //nolint to avoid Helper info since it doesn't apply
code, err := framework.GenerateCodeFromFile(fw, input, framework.InitialBuffers{})
code, err := framework.GenerateCodeFromFile(fw, input, runoptions.InitialBuffers{})
if err != nil {
t.Error(err)
return
Expand Down
10 changes: 7 additions & 3 deletions framework/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"github.com/asmaloney/gactar/util/chalk"
"github.com/asmaloney/gactar/util/filesystem"
"github.com/asmaloney/gactar/util/python"
"github.com/asmaloney/gactar/util/runoptions"
)

// Some tools for working with our frameworks

// Reads a file, generates a model, validates it, and generates code from it for a given framework.
// This is useful for testing.
func GenerateCodeFromFile(fw Framework, inputFile string, initialBuffers InitialBuffers) (code []byte, err error) {
func GenerateCodeFromFile(fw Framework, inputFile string, initialBuffers runoptions.InitialBuffers) (code []byte, err error) {
amodCode, err := os.ReadFile(inputFile)
if err != nil {
return
Expand All @@ -43,7 +44,10 @@ func GenerateCodeFromFile(fw Framework, inputFile string, initialBuffers Initial
return
}

code, err = fw.GenerateCode(&model.DefaultParams, initialBuffers)
options := model.DefaultParams
options.InitialBuffers = initialBuffers

code, err = fw.GenerateCode(&options)
if err != nil {
return
}
Expand Down Expand Up @@ -73,7 +77,7 @@ func Setup(info *Info) (err error) {
return
}

func ParseInitialBuffers(model *actr.Model, initialBuffers InitialBuffers) (parsed ParsedInitialBuffers, err error) {
func ParseInitialBuffers(model *actr.Model, initialBuffers runoptions.InitialBuffers) (parsed ParsedInitialBuffers, err error) {
parsed = ParsedInitialBuffers{}

for bufferName, bufferInit := range initialBuffers {
Expand Down
12 changes: 6 additions & 6 deletions framework/vanilla_actr/vanilla_actr.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (c VanillaACTR) Model() (model *actr.Model) {
return c.model
}

func (v *VanillaACTR) Run(options *runoptions.Options, initialBuffers framework.InitialBuffers) (result *framework.RunResult, err error) {
modelFile, err := v.WriteModel(v.tmpPath, options, initialBuffers)
func (v *VanillaACTR) Run(options *runoptions.Options) (result *framework.RunResult, err error) {
modelFile, err := v.WriteModel(v.tmpPath, options)
if err != nil {
return
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func (v *VanillaACTR) Run(options *runoptions.Options, initialBuffers framework.
}

// WriteModel converts the internal actr.Model to Lisp and writes it to a file.
func (v *VanillaACTR) WriteModel(path string, options *runoptions.Options, initialBuffers framework.InitialBuffers) (outputFileName string, err error) {
func (v *VanillaACTR) WriteModel(path string, options *runoptions.Options) (outputFileName string, err error) {
// If our model has a print statement, then write out our support file
if v.model.HasPrintStatement() {
err = framework.WriteSupportFile(path, vanillaPrintFileName, vanillaPrint)
Expand All @@ -161,7 +161,7 @@ func (v *VanillaACTR) WriteModel(path string, options *runoptions.Options, initi
return "", err
}

_, err = v.GenerateCode(options, initialBuffers)
_, err = v.GenerateCode(options)
if err != nil {
return
}
Expand All @@ -175,8 +175,8 @@ func (v *VanillaACTR) WriteModel(path string, options *runoptions.Options, initi
}

// GenerateCode converts the internal actr.Model to Lisp code.
func (v *VanillaACTR) GenerateCode(options *runoptions.Options, initialBuffers framework.InitialBuffers) (code []byte, err error) {
patterns, err := framework.ParseInitialBuffers(v.model, initialBuffers)
func (v *VanillaACTR) GenerateCode(options *runoptions.Options) (code []byte, err error) {
patterns, err := framework.ParseInitialBuffers(v.model, options.InitialBuffers)
if err != nil {
return
}
Expand Down
4 changes: 3 additions & 1 deletion framework/vanilla_actr/vanilla_actr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/kylelemons/godebug/diff"

"github.com/asmaloney/gactar/framework"

"github.com/asmaloney/gactar/util/cli"
"github.com/asmaloney/gactar/util/runoptions"
)

func init() {
Expand Down Expand Up @@ -55,7 +57,7 @@ func TestCodeGeneration(t *testing.T) {
}

func runCodeGenerationTest(t *testing.T, fw framework.Framework, input, output string) { //nolint to avoid Helper info since it doesn't apply
code, err := framework.GenerateCodeFromFile(fw, input, framework.InitialBuffers{})
code, err := framework.GenerateCodeFromFile(fw, input, runoptions.InitialBuffers{})
if err != nil {
t.Error(err)
return
Expand Down
5 changes: 3 additions & 2 deletions modes/defaultmode/defaultmode.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/asmaloney/gactar/actr"
"github.com/asmaloney/gactar/amod"
"github.com/asmaloney/gactar/framework"

"github.com/asmaloney/gactar/util/chalk"
"github.com/asmaloney/gactar/util/cli"
"github.com/asmaloney/gactar/util/filesystem"
Expand Down Expand Up @@ -113,7 +114,7 @@ func generateCode(frameworks framework.List, files []string, outputDir string) (
continue
}

fileName, err := f.WriteModel(outputDir, &model.DefaultParams, framework.InitialBuffers{})
fileName, err := f.WriteModel(outputDir, &model.DefaultParams)
if err != nil {
fmt.Println(err.Error())
continue
Expand All @@ -128,7 +129,7 @@ func generateCode(frameworks framework.List, files []string, outputDir string) (
func runCode(frameworks framework.List) {
for _, f := range frameworks {
model := f.Model()
result, err := f.Run(&model.DefaultParams, framework.InitialBuffers{})
result, err := f.Run(&model.DefaultParams)
if err != nil {
fmt.Println(err.Error())
continue
Expand Down
6 changes: 4 additions & 2 deletions modes/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/asmaloney/gactar/util/chalk"
"github.com/asmaloney/gactar/util/cli"
"github.com/asmaloney/gactar/util/issues"
"github.com/asmaloney/gactar/util/runoptions"
"github.com/asmaloney/gactar/util/validate"
)

Expand Down Expand Up @@ -260,11 +261,12 @@ func (s *Shell) cmdRun(initialGoal string) (err error) {
return err
}

initialBuffers := framework.InitialBuffers{
options := s.currentModel.DefaultParams
options.InitialBuffers = runoptions.InitialBuffers{
"goal": strings.TrimSpace(initialGoal),
}

result, err := f.Run(&s.currentModel.DefaultParams, initialBuffers)
result, err := f.Run(&options)
if err != nil {
return err
}
Expand Down
19 changes: 10 additions & 9 deletions modes/web/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"encoding/json"
"net/http"

"github.com/asmaloney/gactar/framework"

"github.com/asmaloney/gactar/util/cli"
"github.com/asmaloney/gactar/util/runoptions"
)

type Session struct {
Expand Down Expand Up @@ -36,11 +35,11 @@ func (w *Web) beginSessionHandler(rw http.ResponseWriter, req *http.Request) {

func (w *Web) runModelSessionHandler(rw http.ResponseWriter, req *http.Request) {
type request struct {
SessionID int `json:"sessionID"`
ModelID int `json:"modelID"`
Buffers framework.InitialBuffers `json:"buffers"` // set the initial buffers
IncludeCode bool `json:"includeCode"` // include generated code in the result
Options runOptionsJSON `json:"options"`
SessionID int `json:"sessionID"`
ModelID int `json:"modelID"`
Buffers runoptions.InitialBuffers `json:"buffers"` // set the initial buffers
IncludeCode bool `json:"includeCode"` // include generated code in the result
Options runOptionsJSON `json:"options"`
}
type response struct {
Results json.RawMessage `json:"results"`
Expand All @@ -67,12 +66,14 @@ func (w *Web) runModelSessionHandler(rw http.ResponseWriter, req *http.Request)
return
}

aoptions, err := w.actrOptionsFromJSON(&model.actrModel.DefaultParams, &data.Options)
options, err := w.actrOptionsFromJSON(&model.actrModel.DefaultParams, &data.Options)
if err != nil {
encodeErrorResponse(rw, err)
return
}

options.InitialBuffers = data.Buffers

// ensure temp dir exists
// https://github.com/asmaloney/gactar/issues/103
_, err = cli.CreateTempDir(w.settings)
Expand All @@ -81,7 +82,7 @@ func (w *Web) runModelSessionHandler(rw http.ResponseWriter, req *http.Request)
return
}

resultMap := w.runModel(model.actrModel, aoptions, data.Buffers)
resultMap := w.runModel(model.actrModel, options)

for key := range resultMap {
result := resultMap[key]
Expand Down
15 changes: 8 additions & 7 deletions modes/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,15 @@ func (w Web) runModelHandler(rw http.ResponseWriter, req *http.Request) {
return
}

aoptions, err := w.actrOptionsFromJSON(&model.DefaultParams, data.Options)
options, err := w.actrOptionsFromJSON(&model.DefaultParams, data.Options)
if err != nil {
encodeErrorResponse(rw, err)
return
}

initialGoal := strings.TrimSpace(data.Goal)
initialBuffers := framework.InitialBuffers{

options.InitialBuffers = runoptions.InitialBuffers{
"goal": initialGoal,
}

Expand All @@ -184,7 +185,7 @@ func (w Web) runModelHandler(rw http.ResponseWriter, req *http.Request) {
return
}

resultMap := w.runModel(model, aoptions, initialBuffers)
resultMap := w.runModel(model, options)

rr := runResult{
Issues: log.AllIssues(),
Expand All @@ -200,7 +201,7 @@ func (w Web) runModelHandler(rw http.ResponseWriter, req *http.Request) {
encodeResponse(rw, json.RawMessage(string(results)))
}

func (w Web) runModel(model *actr.Model, options *runoptions.Options, initialBuffers framework.InitialBuffers) (resultMap frameworkRunResultMap) {
func (w Web) runModel(model *actr.Model, options *runoptions.Options) (resultMap frameworkRunResultMap) {
resultMap = make(frameworkRunResultMap, len(options.Frameworks))

var wg sync.WaitGroup
Expand All @@ -218,7 +219,7 @@ func (w Web) runModel(model *actr.Model, options *runoptions.Options, initialBuf

log := f.ValidateModel(model)
if !log.HasError() {
r, err := runModelOnFramework(model, options, initialBuffers, f)
r, err := runModelOnFramework(model, options, f)
if err != nil {
log.Error(nil, err.Error())
}
Expand Down Expand Up @@ -263,7 +264,7 @@ func (w Web) runModel(model *actr.Model, options *runoptions.Options, initialBuf
return
}

func runModelOnFramework(model *actr.Model, options *runoptions.Options, initialBuffers framework.InitialBuffers, f framework.Framework) (result *framework.RunResult, err error) {
func runModelOnFramework(model *actr.Model, options *runoptions.Options, f framework.Framework) (result *framework.RunResult, err error) {
if model == nil {
err = ErrNoModel
return
Expand All @@ -274,7 +275,7 @@ func runModelOnFramework(model *actr.Model, options *runoptions.Options, initial
return
}

result, err = f.Run(options, initialBuffers)
result, err = f.Run(options)
if err != nil {
return
}
Expand Down
Loading

0 comments on commit 31dc92b

Please sign in to comment.