Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for callback reporter to run on finalize #65

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [[unpublished]](https://github.com/mlange-42/arche-model/compare/v0.8.0...main)

### Features

* Adds option `Final` to `reporter.Callback` to report data once on finalize instead of on ticks (#65)

## [[v0.8.1]](https://github.com/mlange-42/arche-model/compare/v0.8.0...v0.8.1)

### Other
Expand Down
13 changes: 11 additions & 2 deletions reporter/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Callback struct {
UpdateInterval int // Update interval in model ticks.
HeaderCallback func(header []string) // Called with the header of the observer during initialization.
Callback func(step int, row []float64) // Called with step and data row on each update (subject to UpdateInterval).
Final bool // Whether Callback should be called on finalization only, instead of on every tick.
step int64
}

Expand All @@ -29,12 +30,20 @@ func (s *Callback) Initialize(w *ecs.World) {
// Update the system
func (s *Callback) Update(w *ecs.World) {
s.Observer.Update(w)
if s.step%int64(s.UpdateInterval) == 0 {

if !s.Final && s.step%int64(s.UpdateInterval) == 0 {
values := s.Observer.Values(w)
s.Callback(int(s.step), values)
}

s.step++
}

// Finalize the system
func (s *Callback) Finalize(w *ecs.World) {}
func (s *Callback) Finalize(w *ecs.World) {
if !s.Final {
return
}
values := s.Observer.Values(w)
s.Callback(int(s.step), values)
}
20 changes: 20 additions & 0 deletions reporter/callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package reporter_test

import (
"fmt"
"testing"

"github.com/mlange-42/arche-model/model"
"github.com/mlange-42/arche-model/reporter"
"github.com/mlange-42/arche-model/system"
"github.com/stretchr/testify/assert"
)

func ExampleCallback() {
Expand Down Expand Up @@ -33,3 +35,21 @@ func ExampleCallback() {
// Output:
// [[1 2 3] [1 2 3] [1 2 3]]
}

func TestCallbackFinal(t *testing.T) {
m := model.New()
counter := 0

m.AddSystem(&reporter.Callback{
Observer: &ExampleObserver{},
Callback: func(step int, row []float64) {
counter++
},
HeaderCallback: func(header []string) {},
Final: true,
})
m.AddSystem(&system.FixedTermination{Steps: 3})
m.Run()

assert.Equal(t, 1, counter)
}
Loading