Procházet zdrojové kódy

removed active receiver set and transition buffer, replaced with receiver flag

Snej69420 před 6 měsíci
rodič
revize
ddf5ddf44e
2 změnil soubory, kde provedl 44 přidání a 35 odebrání
  1. 44 33
      src/AtomicDEVS.jl
  2. 0 2
      src/CoupledDEVS.jl

+ 44 - 33
src/AtomicDEVS.jl

@@ -8,8 +8,10 @@ import DataStructures
 
     last_event_time::Float64 = 0.0
     next_ta::Float64 = -1.0 # -1, initialization needed
-    is_phantom_pop::Bool = false
+
     is_imminent::Bool = false
+    is_receiving::Bool = false
+    is_phantom_pop::Bool = false
 end
 
 # DEVS Interface (To be extended by the modeler)
@@ -56,15 +58,14 @@ function send_message!(agent, model, port::Symbol, msg)
     source_key = (agent.id, port)
     if haskey(model.resolved_couplings, source_key)
         for (target_id, target_array) in model.resolved_couplings[source_key]
-
-            # Direct memory push!
             push!(target_array, msg)
 
-            # Engine tracks who got a message
-            if !(target_id in model.active_receivers_set)
+            target_agent = model[target_id]
+            if !target_agent.is_receiving
+                target_agent.is_receiving = true
                 push!(model.active_receivers, target_id)
-                push!(model.active_receivers_set, target_id)
             end
+
         end
     end
 end
@@ -74,7 +75,6 @@ function route_outputs!(model)
         empty!(model.out_log)
     end
     empty!(model.active_receivers)
-    empty!(model.active_receivers_set)
 
     for id in model.imminent
         a = model[id]
@@ -87,53 +87,64 @@ end
 function transition!(model, current_time, triggering_agent_id)
     # clear buffers previous transition
     empty!(model.requeue_buffer)
-    empty!(model.transition_buffer)
-    append!(model.transition_buffer, model.imminent)
-
-    # add receiving models to transition buffer, without adding duplicates
-    for id in model.active_receivers
-        if !model[id].is_imminent
-            push!(model.transition_buffer, id)
-        end
-    end
 
-    # determine transition type and execute
-    for id in model.transition_buffer
+    for id in model.imminent
         a = model[id]
-        is_imm = model[id].is_imminent
-        is_receiving = !all(isempty, values(a.inbox)) # switch out for id in model.active_receivers_set
-        e = current_time - a.last_event_time
 
         trans_type::Transitions = NONE
-        if is_imm && is_receiving
+        if a.is_receiving
             confluent_transition!(a, model, a.inbox)
             trans_type = CONFLUENT
-        elseif is_imm && !is_receiving
+        else
             internal_transition!(a, model)
             trans_type = INTERNAL
-        elseif !is_imm && is_receiving
-            external_transition!(a, model, e, a.inbox)
-            trans_type = EXTERNAL
         end
 
-        a.is_imminent = false
         a.last_event_time = current_time
-        a.next_ta = time_advance(a, model) # actual time advance
-
-        if trans_type !== NONE
-            log_transition!(model, current_time, a, trans_type)
-        end
+        a.next_ta = time_advance(a, model)
+        log_transition!(model, current_time, a, trans_type)
 
         for queue in values(a.inbox)
             empty!(queue)
         end
-
         if id != triggering_agent_id && a.next_ta < Inf
             push!(model.requeue_buffer, a.id)
         end
     end
 
+    # receivers only
+    for id in model.active_receivers
+        a = model[id]
+
+        # imminent? -> CONFLUENT -> skip
+        if !a.is_imminent
+            e = current_time - a.last_event_time
+            external_transition!(a, model, e, a.inbox)
+
+            a.last_event_time = current_time
+            a.next_ta = time_advance(a, model)
+            log_transition!(model, current_time, a, EXTERNAL)
+
+            for queue in values(a.inbox)
+                empty!(queue)
+            end
+            if id != triggering_agent_id && a.next_ta < Inf
+                push!(model.requeue_buffer, a.id)
+            end
+        end
+    end
+
+    # clean up
+    for id in model.imminent
+        model[id].is_imminent = false
+        model[id].is_receiving = false
+    end
+    for id in model.active_receivers
+        model[id].is_receiving = false
+    end
+
     empty!(model.imminent)
+    empty!(model.active_receivers)
 
     for req_id in model.requeue_buffer
         agent = model[req_id]

+ 0 - 2
src/CoupledDEVS.jl

@@ -7,9 +7,7 @@ Base.@kwdef struct DEVSProperties{M}
 
     active_routing_keys::Vector{Tuple{Int, Symbol}} = Tuple{Int, Symbol}[]
     active_receivers::Vector{Int} = Int[]
-    active_receivers_set::BitSet = BitSet() # O(1) look up
 
-    transition_buffer::Vector{Int} = Int[]
     imminent::Vector{Int} = Int[]
     requeue_buffer::Vector{Int} = Int[]