Skip to content

Commit 02fcfba

Browse files
committed
Move operators.jl to utils and introduce scheduling with datetimes
1 parent f1fd769 commit 02fcfba

8 files changed

Lines changed: 45 additions & 8 deletions

File tree

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ julia> Pkg.add("SimJulia")
3939

4040
* Version 0.4 is a complete rewrite: more julian and less pythonic.
4141
* Only supports Julia v0.6 and above.
42+
* Scheduling of events can be done with `Base.Dates.Datetime` and `Base.Dates.Period`:
43+
```
44+
using SimJulia
45+
using Base.Dates
46+
47+
function datetimetest(sim::Simulation)
48+
println(nowDatetime(sim))
49+
yield(Timeout(sim, Day(2)))
50+
println(nowDatetime(sim))
51+
end
52+
53+
datetime = now()
54+
sim = Simulation(datetime)
55+
@process datetimetest(sim)
56+
run(sim, datetime+Month(3))
57+
println(nowDatetime(sim))
58+
```
4259
* The discrete event features are on par with version 0.3. (STABLE)
4360
* Two ways of making `Processes` are provided:
4461
- using the existing concept of `Tasks`:
@@ -54,7 +71,7 @@ julia> Pkg.add("SimJulia")
5471
end
5572
5673
sim = Simulation()
57-
@Process fibonnaci(sim)
74+
@process fibonnaci(sim)
5875
run(sim, 10)
5976
```
6077
- using a novel finite-statemachine approach:
@@ -70,7 +87,7 @@ julia> Pkg.add("SimJulia")
7087
end
7188
7289
sim = Simulation()
73-
@Coroutine fibonnaci(sim)
90+
@coroutine fibonnaci(sim)
7491
run(sim, 10)
7592
```
7693
* The continuous time simulation is based on a quantized state system solver. (EXPERIMENTAL)

src/SimJulia.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module SimJulia
1919
export (&), (|)
2020
export Simulation
2121
export run, now, active_process
22+
export nowDatetime
2223
export Process, @process
2324
export yield, interrupt
2425
export FiniteStateMachine, @stateful, @yield
@@ -28,10 +29,10 @@ module SimJulia
2829

2930
include("base.jl")
3031
include("events.jl")
31-
include("operators.jl")
32-
include("tasks/base.jl")
32+
include("utils/operators.jl")
3333
include("simulations.jl")
3434
include("utils/time.jl")
35+
include("tasks/base.jl")
3536
include("processes.jl")
3637
include("finitestatemachines/utils.jl")
3738
include("finitestatemachines/transforms.jl")

src/base.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function schedule(ev::AbstractEvent, delay::Number=zero(Float64); priority::Int8
6161
env = environment(ev)
6262
bev = ev.bev
6363
bev.value = value
64-
env.heap[bev] = EventKey(env.time + delay, priority, env.sid+=one(UInt))
64+
env.heap[bev] = EventKey(now(env) + delay, priority, env.sid+=one(UInt))
6565
bev.state = scheduled
6666
end
6767

File renamed without changes.

src/utils/time.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ function Simulation(initial_time::DateTime)
33
end
44

55
function run(env::Environment, until::DateTime)
6-
run(env, Base.Dates.datetime2epochms(until)-now(env))
6+
run(env, Base.Dates.datetime2epochms(until))
77
end
88

99
function Timeout(env::Environment, delay::Period; priority::Int8=zero(Int8), value::Any=nothing)
1010
time = now(env)
1111
del = Base.Dates.datetime2epochms(Base.Dates.epochms2datetime(time)+delay)-time
1212
Timeout(env, del;priority=priority, value=value)
1313
end
14+
15+
function nowDatetime(env::Environment)
16+
Base.Dates.epochms2datetime(now(env))
17+
end

test/runtests.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ testpath(f) = joinpath(dirname(@__FILE__), f)
22

33
for test_file in [
44
"base.jl",
5-
"simulations.jl",
65
"events.jl",
7-
"operators.jl",
6+
"utils/operators.jl",
7+
"simulations.jl",
8+
"utils/time.jl",
89
"tasks/base.jl",
910
"processes.jl",
1011
"finitestatemachines/utils.jl",

test/utils/time.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using SimJulia
2+
using Base.Dates
3+
4+
function datetimetest(sim::Simulation)
5+
println(nowDatetime(sim))
6+
yield(Timeout(sim, Day(2)))
7+
println(nowDatetime(sim))
8+
end
9+
10+
datetime = now()
11+
sim = Simulation(datetime)
12+
@process datetimetest(sim)
13+
run(sim, datetime+Month(3))
14+
println(nowDatetime(sim))

0 commit comments

Comments
 (0)