-
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.
WIP adding unit tets for Julia interface
- Loading branch information
1 parent
da41e40
commit 8d7ee33
Showing
5 changed files
with
201 additions
and
20 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
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,170 @@ | ||
using SparseArrays, MaxPlus | ||
|
||
# Note: soon will included in MaxPlus.jl | ||
include("TimedPetriNetEditor.jl") | ||
|
||
# Create an empty Petri net and return its handle. You can create several nets. | ||
pn = petri_net() | ||
@assert pn.handle == 0 | ||
|
||
# Or create new Petri net by loading it. | ||
pn1 = petri_net("../../data/examples/TrafficLights.json") | ||
@assert pn1.handle == 1 | ||
|
||
# Duplicate the net | ||
pn2 = petri_net(pn) | ||
@assert pn2.handle == 2 | ||
|
||
# Failed loading file. FIXME shall not store pn3 internally | ||
pn3 = petri_net("doesnotexist.json") | ||
pn3 = petri_net(pn1) | ||
@assert pn3.handle == 4 # FIXME shall be 3 | ||
|
||
# Has no places and no transitions? Return true in this case. | ||
is_empty(pn) | ||
@assert ans == true | ||
is_empty(pn1) | ||
@assert ans == false | ||
is_empty(pn2) | ||
@assert ans == true | ||
is_empty(pn3) | ||
@assert ans == false | ||
|
||
# Clear the Petri net (remove all nodes and arcs) | ||
clear!(pn3) | ||
@assert is_empty(pn3) == true | ||
|
||
# Create places. X-Y coordinate (3.15, 4.15) and 5 tokens for Place 0. | ||
# Return its identifier. | ||
p0 = add_place!(pn, 100.0, 100.0, 5) | ||
@assert ans == 0 | ||
p1 = add_place!(pn, 200.0, 200.0, 0) | ||
@assert ans == 1 | ||
p2 = add_place!(pn, Place(210.0, 210.0, 10)) | ||
@assert ans == 2 | ||
|
||
# Get the place content | ||
p3 = place(pn, p2) | ||
@assert p3.x == 210.0 | ||
@assert p3.y == 210.0 | ||
@assert p3.tokens == 10 | ||
|
||
# Set/Get the number of tokens | ||
tokens(pn, p0) | ||
@assert ans == 5 | ||
tokens!(pn, p0, 2) | ||
@assert ans == true | ||
tokens(pn, p0) | ||
@assert ans == 2 | ||
|
||
# Create transitions. X-Y coordinate (1.0, 2.0) for Transition 0. | ||
# Return its identifier. | ||
t0 = add_transition!(pn, 150.0, 150.0) | ||
@assert ans == 0 | ||
t1 = add_transition!(pn, 250.0, 250.0) | ||
@assert ans == 1 | ||
t2 = add_transition!(pn, Transition(200.0, 240.0)) | ||
@assert ans == 2 | ||
|
||
# Get the transition content | ||
t3 = transition(pn, t2) | ||
@assert t3.x == 200.0 | ||
@assert t3.y == 240.0 | ||
|
||
# Remove nodes. Be careful the handle of the latest inserted node is invalidated | ||
remove_place!(pn, p1) | ||
@assert ans == true | ||
remove_transition!(pn, t0) | ||
@assert ans == true | ||
|
||
# Get the number of nodes | ||
count_transitions(pn) | ||
@assert ans == 2 | ||
count_places(pn) | ||
@assert ans == 2 | ||
|
||
# Get the list of places | ||
places(pn) | ||
@assert size(ans) == (2,) | ||
|
||
# Get the list of transitions | ||
transitions(pn) | ||
@assert size(ans) == (2,) | ||
|
||
# TODO missing API for arcs :( | ||
|
||
# You can save the Petri net to JSON file | ||
# Note: you cannot save empty net | ||
add_place!(pn3, 100.0, 100.0, 5) | ||
is_empty(pn3) | ||
@assert ans == false | ||
save(pn3, "/tmp/petri.json") | ||
@assert ans == true | ||
clear!(pn3) | ||
@assert ans == true | ||
is_empty(pn3) | ||
@assert ans == true | ||
# Load the same file back (old net is deleted) | ||
load!(pn3, "/tmp/petri.json") | ||
@assert ans == true | ||
is_empty(pn2) | ||
@assert ans == true | ||
save(pn2, "/tmp/dummy.json") | ||
@assert ans == true | ||
load!(pn3, "/tmp/dummy.json") | ||
@assert ans == true | ||
is_empty(pn3) | ||
@assert ans == true | ||
|
||
# Or create one | ||
pn4 = load("../../data/examples/Howard2.json") | ||
@assert pn4.handle == 5 # FIXME should be ideally 4 | ||
|
||
# Number of nodes and arcs | ||
count_places(pn4) | ||
@assert ans == 5 | ||
count_transitions(pn4) | ||
@assert ans == 4 | ||
# TODO count_arcs(pn4) | ||
# @assert ans == 4 | ||
|
||
# Get the list of marks (number of tokens for each place P0, P1 .. Pn) | ||
tokens(pn4) | ||
@assert ans == [2; 0; 0; 0; 0] | ||
|
||
# Check if Petri net is an event graph | ||
is_event_graph(pn4) | ||
@assert ans == true | ||
is_event_graph(pn1) | ||
@assert ans == false | ||
|
||
# Modify the number of tokens for each place | ||
tokens!(pn4, [0; 1; 2; 3; 4]) | ||
@assert ans == true | ||
|
||
# If the Petri net is an event graph, you can return the canonical form | ||
pn5 = canonic(pn4) | ||
@assert pn5.handle == 6 # FIXME should be ideally 5 | ||
|
||
# Show the counter and dater form | ||
show_dater_equation(pn5, false, false) | ||
@assert ans == true | ||
show_counter_equation(pn5, false, false) | ||
@assert ans == true | ||
|
||
# If the Petri net is an event graph, you can generate the graph the (max,+) | ||
# adjacency sparse matrices (that could be used with SimpleGraphs.jl). | ||
N,T = to_graph(pn4) | ||
|
||
# Sparse to full (max,+) matrices | ||
full(N) | ||
full(T) | ||
|
||
# If the Petri net is an event graph, you can generate the implicit dynamic | ||
# linear (max,+) system. | ||
S = to_syslin(pn5) | ||
show(S.D) | ||
show(S.A) | ||
show(S.B) | ||
show(S.C) | ||
show(S.x0) |
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