-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reporter.Callback, sensible zero UpdateInterval (#61)
* Adds `reporter.Callback` for direct retrieval of observer output in Go code * Fix reporters did not work with unspecified `UpdateInterval`
- Loading branch information
Showing
11 changed files
with
103 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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) | ||
}, | ||
HeaderCallback: func(header []string) {}, | ||
}) | ||
|
||
// 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]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters