|
|
@@ -1,15 +1,27 @@
|
|
|
-function print_initial_conditions(model)
|
|
|
- # Sort for deterministic output
|
|
|
+using DelimitedFiles
|
|
|
+using Printf
|
|
|
+
|
|
|
+function _get_sorted_initial_conditions(model)
|
|
|
path_to_id = model.path_to_id
|
|
|
sorted_paths = sort(collect(keys(path_to_id)))
|
|
|
|
|
|
- println("\n__ Current Time: 0.000000 __________________________________________ \n")
|
|
|
+ return ((path, path_to_id[path], model[path_to_id[path]]) for path in sorted_paths)
|
|
|
+end
|
|
|
+
|
|
|
+function log_initial_conditions!(model)
|
|
|
+ for (path, id, agent) in _get_sorted_initial_conditions(model)
|
|
|
+ state_str = format_state(agent)
|
|
|
+ ta = time_advance(agent, model)
|
|
|
|
|
|
- for path in sorted_paths
|
|
|
- id = path_to_id[path]
|
|
|
- agent = model[id]
|
|
|
+ # Push the INIT row at t=0.0
|
|
|
+ push!(model.event_logs, (0.0, id, :INIT, nothing, nothing, state_str, ta))
|
|
|
+ end
|
|
|
+end
|
|
|
|
|
|
- # These call the model-specific overrides via multiple dispatch
|
|
|
+function print_initial_conditions(model)
|
|
|
+ println("\n__ Current Time: 0.000000 __________________________________________ \n")
|
|
|
+
|
|
|
+ for (path, id, agent) in _get_sorted_initial_conditions(model)
|
|
|
state_str = format_state(agent)
|
|
|
ta = time_advance(agent, model)
|
|
|
|
|
|
@@ -38,13 +50,12 @@ function print_trace(model)
|
|
|
current_log_time = time
|
|
|
end
|
|
|
|
|
|
- # Check if this is the first entry at this specific timestamp
|
|
|
+ # first entry
|
|
|
is_first_at_time = log === model.event_logs[findfirst(x -> x[1] == time, model.event_logs)]
|
|
|
leading_nl = is_first_at_time ? "" : "\n"
|
|
|
|
|
|
print("$(leading_nl)\t$(trans_type) TRANSITION in model <$agent_name>\n")
|
|
|
|
|
|
- # --- Formatted Input Ports ---
|
|
|
print("\t\tInput Port Configuration:\n")
|
|
|
if in_bag !== nothing && !isempty(in_bag)
|
|
|
for (port, msgs) in in_bag
|
|
|
@@ -58,7 +69,6 @@ function print_trace(model)
|
|
|
|
|
|
print("\t\tNew State: $state_str\n")
|
|
|
|
|
|
- # --- Formatted Output Ports ---
|
|
|
print("\t\tOutput Port Configuration:\n")
|
|
|
if out_msg !== nothing && !isempty(out_msg)
|
|
|
for (port, msgs) in out_msg
|
|
|
@@ -73,4 +83,38 @@ function print_trace(model)
|
|
|
next_time_str = next_ta == Inf ? "inf" : string(time + next_ta)
|
|
|
print("\t\tNext scheduled internal transition at time $next_time_str\n")
|
|
|
end
|
|
|
+end
|
|
|
+
|
|
|
+function export_trace_to_csv(model, filepath::String)
|
|
|
+ id_to_path = Dict(v => k for (k, v) in model.path_to_id)
|
|
|
+ sort!(model.event_logs, by = x -> x[1])
|
|
|
+
|
|
|
+ open(filepath, "w") do io
|
|
|
+ write(io, "time,model_path,transition_type,next_ta,inputs,outputs,state\n")
|
|
|
+
|
|
|
+ for log in model.event_logs
|
|
|
+ time, id, trans_type, in_bag, out_msg, state_str, next_ta = log
|
|
|
+ model_path = get(id_to_path, id, "Unknown")
|
|
|
+
|
|
|
+ trans_str = string(trans_type)
|
|
|
+
|
|
|
+ format_arr(arr) = "[" * join(arr, ", ") * "]"
|
|
|
+
|
|
|
+ in_str = "none"
|
|
|
+ if in_bag !== nothing && !isempty(in_bag)
|
|
|
+ in_str = join(["$k:$(format_arr(in_bag[k]))" for k in sort(collect(keys(in_bag)))], " | ")
|
|
|
+ end
|
|
|
+
|
|
|
+ out_str = "none"
|
|
|
+ if out_msg !== nothing && !isempty(out_msg)
|
|
|
+ out_str = join(["$k:$(format_arr(out_msg[k]))" for k in sort(collect(keys(out_msg)))], " | ")
|
|
|
+ end
|
|
|
+
|
|
|
+ time_str = @sprintf("%.6f", time)
|
|
|
+ ta_str = next_ta == Inf ? "inf" : @sprintf("%.6f", next_ta)
|
|
|
+ safe_state = replace(state_str, "\n" => " ", "\r" => "")
|
|
|
+
|
|
|
+ write(io, "$time_str,$model_path,$trans_str,$ta_str,$in_str,$out_str,$safe_state\n")
|
|
|
+ end
|
|
|
+ end
|
|
|
end
|