1function [outspace, outrate, outprob, eventCache] = afterEventRouter(sn, ind, event,
class, isSimulation, eventCache, space_buf, space_srv, space_var, key)
2% [OUTSPACE, OUTRATE, OUTPROB, EVENTCACHE] = AFTEREVENTROUTER(SN, IND, EVENT, CLASS, ISSIMULATION, EVENTCACHE, SPACE_BUF, SPACE_SRV, SPACE_VAR, KEY)
4% Handle router afterEvent logic
6% Copyright (c) 2012-2026, Imperial College London
15 space_srv(:,class) = space_srv(:,class) + 1;
16 outspace = [space_srv, space_var]; % buf
is empty
17 outrate = -1*ones(size(outspace,1)); % passive action, rate
is unspecified
20 space_srv(:,class) = space_srv(:,class) - 1;
21 switch sn.routing(ind,
class)
22 case RoutingStrategy.RROBIN
23 slot = sum(sn.nvars(ind,1:(sn.nclasses+class)));
24 outlinks = sn.nodeparam{ind}{
class}.outlinks;
25 idx = find(space_var(slot) == outlinks);
26 if isempty(idx) || idx >= length(outlinks)
27 space_var(slot) = outlinks(1);
29 space_var(slot) = outlinks(idx+1);
31 case RoutingStrategy.WRROBIN
32 slot = sum(sn.nvars(ind,1:(sn.nclasses+class)));
33 % WRR cycles through weighted_outlinks (each outlink
34 % replicated by its weight). The state slot holds the
35 % POSITION in
this list rather than a destination value,
36 % so that repeated outlinks advance correctly.
37 if isfield(sn.nodeparam{ind}{class},
'weighted_outlinks') ...
38 && ~isempty(sn.nodeparam{ind}{class}.weighted_outlinks)
39 cycle_len = length(sn.nodeparam{ind}{
class}.weighted_outlinks);
41 cycle_len = length(sn.nodeparam{ind}{class}.outlinks);
43 pos = space_var(slot);
44 if pos < 1 || pos > cycle_len
46 elseif pos >= cycle_len
49 space_var(slot) = pos + 1;
52 outspace = [space_srv, space_var]; % buf
is empty
53 outrate = GlobalConstants.Immediate*ones(size(outspace,1)); % immediate action
58 if nargin>=8 && isobject(eventCache)
59 eventCache(key) = {outprob, outspace,outrate};
61 if size(outspace,1) > 1
62 tot_rate = sum(outrate);
63 cum_rate = cumsum(outrate) / tot_rate;
64 firing_ctr = 1 + max([0,find( rand > cum_rate
' )]); % select action
65 outspace = outspace(firing_ctr,:);
66 outrate = sum(outrate);
67 outprob = outprob(firing_ctr,:);