Skip to content

Commit

Permalink
Add "free" as a module_state check & combine buffer and module checks…
Browse files Browse the repository at this point in the history
… when generating code (#352)

This allows the following in one match:

        buffer_state retrieval empty
        module_state memory free
  • Loading branch information
asmaloney authored Aug 3, 2023
1 parent c3bf3c4 commit 634a61f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 22 deletions.
1 change: 1 addition & 0 deletions actr/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const BuiltIn = "built-in"
var validStates = []string{
"busy",
"error",
"free",
}

// Module is an ACT-R module
Expand Down
6 changes: 4 additions & 2 deletions actr/production.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ func (c Constraint) String() string {

type Match struct {
BufferPattern *BufferPatternMatch
BufferState *BufferStateMatch
ModuleState *ModuleStateMatch

// If the buffer and the module's buffer are the same, we will have one of each of these.
BufferState *BufferStateMatch
ModuleState *ModuleStateMatch
}

type BufferPatternMatch struct {
Expand Down
54 changes: 41 additions & 13 deletions amod/amod.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,14 +486,28 @@ func addProductions(model *actr.Model, log *issueLog, productions *productionSec

case match.BufferState != nil:
name := match.BufferState.BufferName
actrMatch := actr.Match{
BufferState: &actr.BufferStateMatch{
Buffer: model.LookupBuffer(name),
State: match.BufferState.State,
},
actrMatch := &actr.BufferStateMatch{
Buffer: model.LookupBuffer(name),
State: match.BufferState.State,
}

prod.Matches = append(prod.Matches, &actrMatch)
// if we have a buffer state match already, add this module state match there
matched := false
for _, match := range prod.Matches {
if (match.ModuleState != nil) &&
(match.ModuleState.Buffer.BufferName() == name) {
match.BufferState = actrMatch

matched = true
break
}
}

if !matched {
prod.Matches = append(prod.Matches, &actr.Match{
BufferState: actrMatch,
})
}

case match.ModuleState != nil:
name := match.ModuleState.ModuleName
Expand All @@ -504,15 +518,29 @@ func addProductions(model *actr.Model, log *issueLog, productions *productionSec
// matter which one we pick as the requests should be on its module.
buffer := module.Buffers().At(0)

actrMatch := actr.Match{
ModuleState: &actr.ModuleStateMatch{
Module: module,
Buffer: buffer,
State: match.ModuleState.State,
},
actrMatch := &actr.ModuleStateMatch{
Module: module,
Buffer: buffer,
State: match.ModuleState.State,
}

prod.Matches = append(prod.Matches, &actrMatch)
// if we have a buffer state match already, add this module state match there
matched := false
for _, match := range prod.Matches {
if (match.BufferState != nil) &&
(match.BufferState.Buffer.BufferName() == buffer.BufferName()) {
match.ModuleState = actrMatch

matched = true
break
}
}

if !matched {
prod.Matches = append(prod.Matches, &actr.Match{
ModuleState: actrMatch,
})
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion amod/amod_productions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,5 +1004,5 @@ func Example_productionMatchModuleStateInvalidState2() {
}`)

// Output:
// ERROR: invalid module state check 'bar' for module 'memory' in production 'start' (should be one of: busy, error) (line 8, col 10)
// ERROR: invalid module state check 'bar' for module 'memory' in production 'start' (should be one of: busy, error, free) (line 8, col 10)
}
4 changes: 2 additions & 2 deletions framework/ccm_pyactr/ccm_pyactr.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,9 @@ func (c CCMPyACTR) outputMatch(match *actr.Match) {
c.Write("%s='%s:True'", bufferName, match.BufferState.State)

case match.ModuleState != nil:
bufferName := match.ModuleState.Buffer.BufferName()
moduleName := match.ModuleState.Module.ModuleName()

c.Write("%s='%s:True'", bufferName, match.ModuleState.State)
c.Write("%s='%s:True'", moduleName, match.ModuleState.State)
}
}

Expand Down
20 changes: 18 additions & 2 deletions framework/pyactr/pyactr.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,20 @@ func (p PyACTR) outputPattern(pattern *actr.Pattern, tabs int) {
}

func (p PyACTR) outputMatch(match *actr.Match) {
tabbedItems := framework.KeyValueList{}

// check for case where we need to combine module & buffer checks
if (match.BufferState != nil) && (match.ModuleState != nil) {
bufferName := match.BufferState.Buffer.BufferName()

p.Writeln(" ?%s>", bufferName)
tabbedItems.Add("buffer", match.BufferState.State)
tabbedItems.Add("state", match.ModuleState.State)
p.TabWrite(2, tabbedItems)

return
}

switch {
case match.BufferPattern != nil:
bufferName := match.BufferPattern.Buffer.BufferName()
Expand All @@ -515,13 +529,15 @@ func (p PyACTR) outputMatch(match *actr.Match) {
bufferName := match.BufferState.Buffer.BufferName()

p.Writeln(" ?%s>", bufferName)
p.Writeln(" buffer %s", match.BufferState.State)
tabbedItems.Add("buffer", match.BufferState.State)
p.TabWrite(2, tabbedItems)

case match.ModuleState != nil:
bufferName := match.ModuleState.Buffer.BufferName()

p.Writeln(" ?%s>", bufferName)
p.Writeln(" state %s", match.ModuleState.State)
tabbedItems.Add("state", match.ModuleState.State)
p.TabWrite(2, tabbedItems)
}
}

Expand Down
20 changes: 18 additions & 2 deletions framework/vanilla_actr/vanilla_actr.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,20 @@ func (v VanillaACTR) outputPattern(pattern *actr.Pattern, tabs int) {
}

func (v VanillaACTR) outputMatch(match *actr.Match) {
tabbedItems := framework.KeyValueList{}

// check for case where we need to combine module & buffer checks
if (match.BufferState != nil) && (match.ModuleState != nil) {
bufferName := match.BufferState.Buffer.BufferName()

v.Writeln("\t?%s>", bufferName)
tabbedItems.Add("buffer", match.BufferState.State)
tabbedItems.Add("state", match.ModuleState.State)
v.TabWrite(2, tabbedItems)

return
}

switch {
case match.BufferPattern != nil:
bufferName := match.BufferPattern.Buffer.BufferName()
Expand All @@ -486,13 +500,15 @@ func (v VanillaACTR) outputMatch(match *actr.Match) {
bufferName := match.BufferState.Buffer.BufferName()

v.Writeln("\t?%s>", bufferName)
v.Writeln("\t\tbuffer %s", match.BufferState.State)
tabbedItems.Add("buffer", match.BufferState.State)
v.TabWrite(2, tabbedItems)

case match.ModuleState != nil:
bufferName := match.ModuleState.Buffer.BufferName()

v.Writeln("\t?%s>", bufferName)
v.Writeln("\t\tstate %s", match.ModuleState.State)
tabbedItems.Add("state", match.ModuleState.State)
v.TabWrite(2, tabbedItems)
}
}

Expand Down

0 comments on commit 634a61f

Please sign in to comment.