Skip to content

Commit

Permalink
Move "params" from "modules" to "utils" and rename "keyvalue" (#354)
Browse files Browse the repository at this point in the history
These keyvalue structs act as the interface between amod and actr when setting module params.
  • Loading branch information
asmaloney authored Aug 5, 2023
1 parent 634a61f commit 7108c84
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 136 deletions.
1 change: 1 addition & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"gosec",
"Infof",
"Infoln",
"keyvalue",
"Naur",
"productionstring",
"pyactr",
Expand Down
11 changes: 6 additions & 5 deletions actr/actr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ package actr
import (
"github.com/asmaloney/gactar/actr/buffer"
"github.com/asmaloney/gactar/actr/modules"
"github.com/asmaloney/gactar/actr/params"

"github.com/asmaloney/gactar/util/container"
"github.com/asmaloney/gactar/util/keyvalue"
)

type Options struct {
Expand Down Expand Up @@ -250,13 +251,13 @@ func (model Model) LookupBuffer(bufferName string) buffer.Interface {
return nil
}

func (model *Model) SetParam(param *params.Param) (err error) {
func (model *Model) SetParam(param *keyvalue.KeyValue) (err error) {
value := param.Value

switch param.Key {
case "log_level":
if (value.Str == nil) || !ValidLogLevel(*value.Str) {
return params.ErrInvalidOption{Expected: ACTRLoggingLevels}
return modules.ErrInvalidValue{Expected: ACTRLoggingLevels}
}

model.LogLevel = ACTRLogLevel(*value.Str)
Expand All @@ -271,15 +272,15 @@ func (model *Model) SetParam(param *params.Param) (err error) {

case "random_seed":
if value.Number == nil {
return params.ErrInvalidType{ExpectedType: params.Number}
return keyvalue.ErrInvalidType{ExpectedType: keyvalue.Number}
}

seed := uint32(*value.Number)

model.RandomSeed = &seed

default:
return params.ErrUnrecognizedParam
return modules.ErrUnrecognizedOption{Option: param.Key}
}

return
Expand Down
5 changes: 3 additions & 2 deletions actr/modules/declarative_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"golang.org/x/exp/slices"

"github.com/asmaloney/gactar/actr/buffer"
"github.com/asmaloney/gactar/actr/params"

"github.com/asmaloney/gactar/util/keyvalue"
)

type RetrievalTimeParams struct {
Expand Down Expand Up @@ -209,7 +210,7 @@ func (d DeclarativeMemory) BufferName() string {
}

// SetParam is called to set our module's parameter from the parameter in the code ("param")
func (d *DeclarativeMemory) SetParam(param *params.Param) (err error) {
func (d *DeclarativeMemory) SetParam(param *keyvalue.KeyValue) (err error) {
err = d.ValidateParam(param)
if err != nil {
return
Expand Down
5 changes: 3 additions & 2 deletions actr/modules/extra_buffers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package modules

import (
"github.com/asmaloney/gactar/actr/buffer"
"github.com/asmaloney/gactar/actr/params"

"github.com/asmaloney/gactar/util/keyvalue"
)

// ExtraBuffers module is used to declare one or more extra goal-style buffers in the model.
Expand All @@ -23,7 +24,7 @@ func NewExtraBuffers() *ExtraBuffers {
}

// SetParam is called to set our module's parameter from the parameter in the code ("param")
func (eb *ExtraBuffers) SetParam(param *params.Param) (err error) {
func (eb *ExtraBuffers) SetParam(param *keyvalue.KeyValue) (err error) {
newBuffer := goalBuffer{
buffer.Buffer{
Name: param.Key,
Expand Down
5 changes: 3 additions & 2 deletions actr/modules/goal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package modules

import (
"github.com/asmaloney/gactar/actr/buffer"
"github.com/asmaloney/gactar/actr/params"

"github.com/asmaloney/gactar/util/keyvalue"
)

// Goal is a module which provides the ACT-R "goal" buffer.
Expand Down Expand Up @@ -49,7 +50,7 @@ func NewGoal() *Goal {
}

// SetParam is called to set our module's parameter from the parameter in the code ("param")
func (g *Goal) SetParam(param *params.Param) (err error) {
func (g *Goal) SetParam(param *keyvalue.KeyValue) (err error) {
err = g.ValidateParam(param)
if err != nil {
return
Expand Down
5 changes: 3 additions & 2 deletions actr/modules/imaginal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package modules

import (
"github.com/asmaloney/gactar/actr/buffer"
"github.com/asmaloney/gactar/actr/params"

"github.com/asmaloney/gactar/util/keyvalue"
)

// Imaginal is a module which provides the ACT-R "imaginal" buffer.
Expand Down Expand Up @@ -45,7 +46,7 @@ func NewImaginal() *Imaginal {
}

// SetParam is called to set our module's parameter from the parameter in the code ("param")
func (i *Imaginal) SetParam(param *params.Param) (err error) {
func (i *Imaginal) SetParam(param *keyvalue.KeyValue) (err error) {
err = i.ValidateParam(param)
if err != nil {
return
Expand Down
20 changes: 10 additions & 10 deletions actr/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"golang.org/x/exp/slices"

"github.com/asmaloney/gactar/actr/buffer"
"github.com/asmaloney/gactar/actr/params"

"github.com/asmaloney/gactar/util/keyvalue"
)

const BuiltIn = "built-in"
Expand Down Expand Up @@ -45,8 +46,8 @@ type Interface interface {
Parameters() []ParamInterface
ParameterInfo(name string) ParamInterface

ValidateParam(param *params.Param) error
SetParam(param *params.Param) error
ValidateParam(param *keyvalue.KeyValue) error
SetParam(param *keyvalue.KeyValue) error

AllowsMultipleInit() bool
}
Expand Down Expand Up @@ -95,11 +96,10 @@ func (m Module) Parameters() []ParamInterface {
}

// ValidateParam given an actr param will validate it against our modules parameters
func (m Module) ValidateParam(param *params.Param) (err error) {

func (m Module) ValidateParam(param *keyvalue.KeyValue) (err error) {
paramInfo := m.ParameterInfo(param.Key)
if paramInfo == nil {
return params.ErrUnrecognizedParam
return ErrUnrecognizedOption{Option: param.Key}
}

min := paramInfo.GetMin()
Expand All @@ -109,25 +109,25 @@ func (m Module) ValidateParam(param *params.Param) (err error) {

// we currently only have numbers
if value.Number == nil {
return params.ErrInvalidType{ExpectedType: params.Number}
return keyvalue.ErrInvalidType{ExpectedType: keyvalue.Number}
}

if (min != nil) && (max != nil) &&
((*value.Number < *min) || (*value.Number > *max)) {
return params.ErrOutOfRange{
return ErrValueOutOfRange{
Min: min,
Max: max,
}
}

if min != nil && (*value.Number < *min) {
return params.ErrOutOfRange{
return ErrValueOutOfRange{
Min: min,
}
}

if max != nil && (*value.Number > *max) {
return params.ErrOutOfRange{
return ErrValueOutOfRange{
Max: max,
}
}
Expand Down
40 changes: 40 additions & 0 deletions actr/modules/params.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
package modules

import (
"fmt"
"strings"

"github.com/asmaloney/gactar/util/numbers"
)

type ErrUnrecognizedOption struct {
Option string
}

func (e ErrUnrecognizedOption) Error() string {
return fmt.Sprintf("unrecognized option %q", e.Option)
}

type ErrValueOutOfRange struct {
Min *float64
Max *float64
}

func (e ErrValueOutOfRange) Error() string {
if e.Min != nil && e.Max == nil {
return fmt.Sprintf("is out of range (minimum %s)", numbers.Float64Str(*e.Min))
}

if e.Min == nil && e.Max != nil {
return fmt.Sprintf("is out of range (maximum %s)", numbers.Float64Str(*e.Max))
}

return fmt.Sprintf("is out of range (%s-%s)", numbers.Float64Str(*e.Min), numbers.Float64Str(*e.Max))
}

type ErrInvalidValue struct {
Expected []string
}

func (e ErrInvalidValue) Error() string {
return fmt.Sprintf("must be must be one of %q", strings.Join(e.Expected, ", "))
}

// Ptr simply returns a pointer to a literal. e.g. Ptr(0.5)
// This is useful when passing literals to functions which require pointers to basic types.
func Ptr[T any](v T) *T {
Expand Down
4 changes: 2 additions & 2 deletions actr/modules/procedural.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package modules

import (
"github.com/asmaloney/gactar/actr/params"
"github.com/asmaloney/gactar/util/keyvalue"
)

type Procedural struct {
Expand Down Expand Up @@ -35,7 +35,7 @@ func NewProcedural() *Procedural {
}

// SetParam is called to set our module's parameter from the parameter in the code ("param")
func (p *Procedural) SetParam(param *params.Param) (err error) {
func (p *Procedural) SetParam(param *keyvalue.KeyValue) (err error) {
err = p.ValidateParam(param)
if err != nil {
return
Expand Down
73 changes: 0 additions & 73 deletions actr/params/errors.go

This file was deleted.

Loading

0 comments on commit 7108c84

Please sign in to comment.