Skip to content

Commit

Permalink
Add reporter.Callback, sensible zero UpdateInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 committed May 13, 2024
1 parent bb84971 commit e423f88
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

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

### Features

* Adds `reporter.Callback` for direct retrieval of observer output in Go code (#61)

### Bugfixes

* Fix typo in error message when adding UI system as normal system (#60)
* Reporters did not work with unspecified UpdateInterval (#61)

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

Expand Down
40 changes: 40 additions & 0 deletions reporter/callback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package reporter

import (
"github.com/mlange-42/arche-model/observer"
"github.com/mlange-42/arche/ecs"
)

// Callback reporter calling a function on each update.
type Callback struct {
Observer observer.Row // Observer to get data from.
UpdateInterval int // Update/print interval in model ticks.
HeaderCallback func(header []string)
Callback func(step int, row []float64)
step int64
}

// Initialize the system
func (s *Callback) Initialize(w *ecs.World) {
s.Observer.Initialize(w)
if s.UpdateInterval == 0 {
s.UpdateInterval = 1
}
if s.HeaderCallback != nil {
s.HeaderCallback(s.Observer.Header())
}
s.step = 0
}

// Update the system
func (s *Callback) Update(w *ecs.World) {
s.Observer.Update(w)
if 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) {}
34 changes: 34 additions & 0 deletions reporter/callback_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package reporter_test

import (
"fmt"

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

func ExampleCallback() {
// Create a new model.
m := model.New()

data := [][]float64{}

// Add a Print reporter with an Observer.
m.AddSystem(&reporter.Callback{
Observer: &ExampleObserver{},
Callback: func(step int, row []float64) {
data = append(data, row)
},
})

// Add a termination system that ends the simulation.
m.AddSystem(&system.FixedTermination{Steps: 3})

// Run the simulation.
m.Run()

fmt.Println(data)
// Output:
// [[1 2 3] [1 2 3] [1 2 3]]
}
3 changes: 3 additions & 0 deletions reporter/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type CSV struct {
func (s *CSV) Initialize(w *ecs.World) {
s.Observer.Initialize(w)
s.header = s.Observer.Header()
if s.UpdateInterval == 0 {
s.UpdateInterval = 1
}
if s.Sep == "" {
s.Sep = ","
}
Expand Down
3 changes: 3 additions & 0 deletions reporter/csv_snaphot.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type SnapshotCSV struct {
func (s *SnapshotCSV) Initialize(w *ecs.World) {
s.Observer.Initialize(w)
s.header = s.Observer.Header()
if s.UpdateInterval == 0 {
s.UpdateInterval = 1
}

if s.Sep == "" {
s.Sep = ","
Expand Down
3 changes: 3 additions & 0 deletions reporter/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type Print struct {
func (s *Print) Initialize(w *ecs.World) {
s.Observer.Initialize(w)
s.header = s.Observer.Header()
if s.UpdateInterval == 0 {
s.UpdateInterval = 1
}
s.step = 0
}

Expand Down

0 comments on commit e423f88

Please sign in to comment.