Skip to content

Commit

Permalink
Add functionality to advance the model stepwise (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 authored Jan 29, 2024
1 parent 1e915d9 commit bb02c18
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
24 changes: 23 additions & 1 deletion model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,36 @@ func (m *Model) Seed(seed ...uint64) *Model {
return m
}

// Run runs the model.
// Run the model. Initializes the model if it is not already initialized.
// Finalizes the model after the run.
//
// Runs until Terminate in the resource resource.Termination is set to true
// (see [resource.Termination]).
func (m *Model) Run() {
m.Systems.run()
}

// Initialize the model.
func (m *Model) Initialize() {
m.Systems.initialize()
}

// Update the model's systems.
// Return whether the run should continue.
func (m *Model) Update() bool {
return m.Systems.UpdateSystems()
}

// UpdateUI the model's UI systems.
func (m *Model) UpdateUI() {
m.Systems.UpdateUISystems()
}

// Finalize the model.
func (m *Model) Finalize() {
m.Systems.finalize()
}

// Reset resets the world and removes all systems.
//
// Can be used to run systematic simulations without the need to re-allocate memory for each run.
Expand Down
20 changes: 20 additions & 0 deletions model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ func TestModel(t *testing.T) {
}
}

func TestModelStep(t *testing.T) {
m := model.New()

for i := 0; i < 3; i++ {
m.Reset()
m.Seed(123)

m.AddSystem(&system.FixedTermination{
Steps: 10,
})

m.Initialize()

for m.Update() {
m.UpdateUI()
}
m.Finalize()
}
}

func TestModelSeed(t *testing.T) {
m := model.New()
m.Seed(123)
Expand Down
37 changes: 30 additions & 7 deletions model/systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,12 @@ func (s *Systems) initialize() {

s.nextDraw = time.Time{}
s.nextUpdate = time.Time{}

s.tickRes.Get().Tick = 0
}

// Update all systems.
func (s *Systems) update() {
func (s *Systems) update() bool {
s.locked = true
update := s.updateSystems()
s.updateUISystems(update)
Expand All @@ -223,6 +225,32 @@ func (s *Systems) update() {
} else {
s.wait()
}

return !s.termRes.Get().Terminate
}

// UpdateSystems updates all normal systems
func (s *Systems) UpdateSystems() bool {
s.locked = true
updated := s.updateSystems()
s.locked = false

s.removeSystems()

if updated {
time := s.tickRes.Get()
time.Tick++
}
return !s.termRes.Get().Terminate
}

// UpdateUISystems updates all UI systems
func (s *Systems) UpdateUISystems() {
s.locked = true
s.updateUISystems(true)
s.locked = false

s.removeSystems()
}

// Calculates and waits the time until the next update of UI update.
Expand Down Expand Up @@ -316,12 +344,7 @@ func (s *Systems) run() {
s.initialize()
}

time := s.tickRes.Get()
time.Tick = 0
terminate := s.termRes.Get()

for !terminate.Terminate {
s.update()
for s.update() {
}

s.finalize()
Expand Down

0 comments on commit bb02c18

Please sign in to comment.