Skip to content

Commit

Permalink
Merge pull request #15 from RomainFranceschini/refactor/state
Browse files Browse the repository at this point in the history
Refactor model state definition
  • Loading branch information
RomainFranceschini authored Apr 24, 2020
2 parents 1502ff1 + f03ba3f commit 878f013
Showing 36 changed files with 810 additions and 1,273 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/crystal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Quartz CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

container:
image: crystallang/crystal

steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: shards install
- name: Run tests
run: crystal spec
13 changes: 8 additions & 5 deletions benchmarks/devstone.cr
Original file line number Diff line number Diff line change
@@ -5,7 +5,9 @@ module DEVStone
input :in1, :in2
output :out1, :out2

state_var phase : Symbol = :active
state do
var phase : Symbol = :active
end

def time_advance : Quartz::Duration
if phase == :active
@@ -16,11 +18,11 @@ module DEVStone
end

def external_transition(messages)
@phase = :active
state.phase = :active
end

def internal_transition
@phase = :idle
state.phase = :idle
end

def output
@@ -31,7 +33,7 @@ module DEVStone
class Generator < Quartz::AtomicModel
output :out

state_var phase : Symbol = :generate
state { var phase : Symbol = :generate }

def time_advance : Quartz::Duration
if phase == :generate
@@ -46,7 +48,7 @@ module DEVStone
end

def internal_transition
@phase = :idle
state.phase = :idle
end

def external_transition(bag)
@@ -149,6 +151,7 @@ simulation = Quartz::Simulation.new(
maintain_hierarchy: false,
scheduler: :binary_heap
)
# simulation.loggers.level = Logger::DEBUG

simulation.simulate

12 changes: 7 additions & 5 deletions benchmarks/direct_connection.cr
Original file line number Diff line number Diff line change
@@ -6,7 +6,9 @@ module DEVStone
input :in1, :in2
output :out1, :out2

state_var phase : Symbol = :idle
state do
var phase : Symbol = :idle
end

def time_advance : Quartz::Duration
if phase == :idle
@@ -17,11 +19,11 @@ module DEVStone
end

def external_transition(messages)
@phase = :active
state.phase = :active
end

def internal_transition
@phase = :idle
state.phase = :idle
end

def output
@@ -32,7 +34,7 @@ module DEVStone
class Generator < Quartz::AtomicModel
output :out

state_var phase : Symbol = :init
state { var phase : Symbol = :init }

def time_advance : Quartz::Duration
if phase == :init
@@ -47,7 +49,7 @@ module DEVStone
end

def internal_transition
@phase = :generate if @phase == :init
state.phase = :generate if phase == :init
end

def external_transition(bag)
12 changes: 7 additions & 5 deletions benchmarks/push_pull.cr
Original file line number Diff line number Diff line change
@@ -4,7 +4,9 @@ class Worker < Quartz::AtomicModel
input :in
output :out

state_var phase : Symbol = :idle
state do
var phase : Symbol = :idle
end

def time_advance : Quartz::Duration
if phase == :idle
@@ -15,11 +17,11 @@ class Worker < Quartz::AtomicModel
end

def external_transition(bag)
@phase = :active
state.phase = :active
end

def internal_transition
@phase = :idle
state.phase = :idle
end

def output
@@ -30,7 +32,7 @@ end
class Generator < Quartz::AtomicModel
output :out

state_var phase : Symbol = :generate
state { var phase : Symbol = :generate }

def time_advance : Quartz::Duration
case phase
@@ -49,7 +51,7 @@ class Generator < Quartz::AtomicModel

def internal_transition
@events -= 1
@phase = :idle if @events == 0
state.phase = :idle if @events == 0
end

def external_transition(bag)
12 changes: 7 additions & 5 deletions benchmarks/sink.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require "../src/quartz"

class Sink < Quartz::AtomicModel
state_var phase : Symbol = :idle
state do
var phase : Symbol = :idle
end

def time_advance : Quartz::Duration
if phase == :idle
@@ -12,11 +14,11 @@ class Sink < Quartz::AtomicModel
end

def external_transition(messages)
@phase = :active
state.phase = :active
end

def internal_transition
@phase = :idle
state.phase = :idle
end

def output
@@ -27,7 +29,7 @@ end
class Generator < Quartz::AtomicModel
output :out

state_var phase : Symbol = :generate
state { var phase : Symbol = :generate }

def time_advance : Quartz::Duration
case phase
@@ -46,7 +48,7 @@ class Generator < Quartz::AtomicModel

def internal_transition
@events -= 1
@phase = :idle if @events == 0
state.phase = :idle if @events == 0
end

def external_transition(bag)
64 changes: 34 additions & 30 deletions examples/dyn_traffic_light.cr
Original file line number Diff line number Diff line change
@@ -4,31 +4,33 @@ class TrafficLight < Quartz::AtomicModel
input :interrupt
output :observed

state_var phase : Symbol = :red
state do
var phase : Symbol = :red
end

def external_transition(messages)
value = messages[input_ports[:interrupt]].first.as_sym
@phase = case value
when :to_manual
@phase = :manual
else # :to_autonomous
@phase = :red
end
self.phase = case value
when :to_manual
:manual
else # :to_autonomous
:red
end
end

def internal_transition
@phase = case @phase
when :red
:green
when :green
:orange
else # orange
:red
end
self.phase = case phase
when :red
:green
when :green
:orange
else # orange
:red
end
end

def output
observed = case @phase
observed = case phase
when :red, :orange
:grey
when :green
@@ -40,7 +42,7 @@ class TrafficLight < Quartz::AtomicModel
end

def time_advance : Quartz::Duration
case @phase
case phase
when :red then Quartz.duration(60)
when :green then Quartz.duration(50)
when :orange then Quartz.duration(10)
@@ -51,27 +53,29 @@ class TrafficLight < Quartz::AtomicModel
end

class Policeman < Quartz::AtomicModel
state_var phase : Symbol = :idle1
state do
var phase : Symbol = :idle1
end

output :alternate, :add_coupling, :remove_coupling

def external_transition(bag)
end

def internal_transition
@phase = case @phase
when :idle1 then :working1
when :working1 then :move1_2
when :move1_2 then :idle2
when :idle2 then :working2
when :working2 then :move2_1
else # move2_1
:idle1
end
self.phase = case phase
when :idle1 then :working1
when :working1 then :move1_2
when :move1_2 then :idle2
when :idle2 then :working2
when :working2 then :move2_1
else # move2_1
:idle1
end
end

def output
case @phase
case phase
when :idle1, :idle2
post :to_manual, :alternate
when :working1, :working2
@@ -87,7 +91,7 @@ class Policeman < Quartz::AtomicModel
tl2 = tl1.dup
tl2[:dst] = :traffic_light2

if @phase == :move1_2
if phase == :move1_2
post(tl1, :remove_coupling)
post(tl2, :add_coupling)
else # move2_1
@@ -98,7 +102,7 @@ class Policeman < Quartz::AtomicModel
end

def time_advance : Quartz::Duration
case @phase
case phase
when :idle1, :idle2
Quartz.duration(50)
when :working1, :working2
38 changes: 21 additions & 17 deletions examples/dynamic_structure.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require "../src/quartz"

class OneTimeModel < Quartz::AtomicModel
state_var phase : Symbol = :active
state do
var phase : Symbol = :active
end

def time_advance : Quartz::Duration
case phase
@@ -16,7 +18,7 @@ class OneTimeModel < Quartz::AtomicModel

def internal_transition
puts "#{name} does something."
@phase = :idle
self.phase = :idle
end

def output
@@ -26,8 +28,10 @@ end
class BirthController < Quartz::DSDE::Executive
output :birth, :death, :add_coupling, :remove_coupling

state_var phase : Symbol = :init
state_var counter : Int32 = 0
state do
var phase : Symbol = :init
var counter : Int32 = 0
end

def time_advance : Quartz::Duration
case phase
@@ -38,20 +42,20 @@ class BirthController < Quartz::DSDE::Executive
end

def internal_transition
if phase == :death
remove_coupling_from_network(:out, from: :in, between: "model_0", and: "model_#{@counter}")
remove_model_from_network("model_#{@counter}")
@counter -= 1
@phase = :idle
if self.phase == :death
remove_coupling_from_network(:out, from: :in, between: "model_0", and: "model_#{counter}")
remove_model_from_network("model_#{counter}")
self.counter -= 1
self.phase = :idle
else
add_model_to_network(OneTimeModel.new("model_#{@counter}"))
add_coupling_to_network(:out, to: :in, between: "model_0", and: "model_#{@counter}") if @counter > 0
@phase = if @counter == 2
:death
else
@counter += 1
:birth
end
add_model_to_network(OneTimeModel.new("model_#{counter}"))
add_coupling_to_network(:out, to: :in, between: "model_0", and: "model_#{counter}") if counter > 0
self.phase = if self.counter == 2
:death
else
self.counter += 1
:birth
end
end
end

Loading

0 comments on commit 878f013

Please sign in to comment.