1function self = link(self,
P)
4% Copyright (c) 2012-2026, Imperial College London
7if ~isempty(self.connections)
8 line_error(mfilename,'The Network.link method cannot be used after calling the addLink() method. Use Node.setProbRouting instead to configure routing probabilities.');
11if isa(
P,'RoutingMatrix')
19 self.resetNetwork; % remove artificial class switch
nodes
21K = self.getNumberOfClasses;
22I = self.getNumberOfNodes;
25 line_error(mfilename,'Multiclass model: the linked routing matrix
P must be a cell array, e.g.,
P = model.initRoutingMatrix;
P{1} = Pclass1;
P{2} = Pclass2.
');
29if size(P,1) == size(P,2)
38 % in this case it is possible that P is linear but just because the
39 % routing is state-dependent and therefore some zero entries are
40 % actually unspecified
41 %cacheNodes = find(cellfun(@(c) isa(c,'Cache
'), self.getStatefulNodes));
43 switch class(self.nodes{ind})
45 % note that since a cache needs to distinguish hits and
46 % misses, it needs to do class-switch unless the model is
49 if self.nodes{ind}.server.hitClass == self.nodes{ind}.server.missClass
50 line_warning(mfilename,'Ambiguous use of hitClass and missClass at cache, it
is recommended to use different
classes.\n
');
56% This block is to make sure that P = model.initRoutingMatrix; P{2} writes
57% into P{2,2} rather than being interpreted as P{2,1}.
75% assign routing for self-looping jobs
77 if isa(self.classes{r},'SelfLoopingClass
')
81 P{r,r}(self.classes{r}.refstat, self.classes{r}.refstat) = 1.0;
85% link virtual sinks automatically to sink
86ispool = cellisa(self.nodes,'Sink
');
88 line_error(mfilename,'The model can have at most one sink node.
');
91if sum(cellisa(self.nodes,'Source
')) > 1
92 line_error(mfilename,'The model can have at most one source node.
');
94ispool_nnz = find(ispool)';
107 P((ind-1)*K+1:ind*K,:)=0;
116 P{r,s}(ind,jnd) = Pmat((ind-1)*K+r,(jnd-1)*K+s);
128 P{r}((ind-1)*K+1:ind*K,:)=0;
142isemptyP =
false(K,K);
156csnodematrix = cell(I,I);
159 csnodematrix{ind,jnd} = zeros(K,K);
166 [If,Jf] = find(
P{r,s});
168 csnodematrix{If(k),Jf(k)}(r,s) =
P{r,s}(If(k),Jf(k));
176% Psum=cellsum({
P{r,:}})*ones(M,1);
177%
if min(Psum)<1-GlobalConstants.CoarseTol
178% line_error(mfilename,
'Invalid routing probabilities (Node %d departures, switching from class %d).',minpos(Psum),r);
180%
if max(Psum)>1+GlobalConstants.CoarseTol
181% line_error(mfilename,sprintf(
'Invalid routing probabilities (Node %d departures, switching from class %d).',maxpos(Psum),r));
187% As we will now create a CS
for each link i->j,
188% we now condition on the job going from node i to j
192 S = sum(csnodematrix{ind,jnd}(r,:));
194 csnodematrix{ind,jnd}(r,:)=csnodematrix{ind,jnd}(r,:)/S;
196 csnodematrix{ind,jnd}(r,r)=1.0;
203% eye(K)
is because any job that travels to an autoAdded
class
204%
switch stays in the same
class prior to reaching the ClassSwitch
205% and anyway the diagonal of csMatrix
is irrelevant
for the chains
207nodeNames = self.getNodeNames;
210 csMatrix = csMatrix + csnodematrix{ind,jnd};
211 if ~isdiag(csnodematrix{ind,jnd})
212 self.nodes{end+1} = ClassSwitch(self, sprintf(
'CS_%s_to_%s',nodeNames{ind},nodeNames{jnd}),csnodematrix{ind,jnd});
213 self.nodes{end}.autoAdded =
true;
214 csid(ind,jnd) = length(self.nodes);
220 %
this is to ensure that also stateful cs like caches
222 if isa(self.nodes{ind},
'Cache')
223 for r=find(self.
nodes{ind}.server.hitClass)
224 csMatrix(r,self.nodes{ind}.server.hitClass(r)) = 1.0;
226 for r=find(self.nodes{ind}.server.missClass)
227 csMatrix(r,self.
nodes{ind}.server.missClass(r)) = 1.0;
229 elseif isa(self.nodes{ind},
'ClassSwitch')
230 if isempty(self.
nodes{ind}.server.csMatrix )
231 line_error(mfilename,
'Uninitialized ClassSwitch node, use the setClassSwitchingMatrix method.');
233 csMatrix = csMatrix | self.nodes{ind}.server.csMatrix > 0.0;
237self.csMatrix = csMatrix~=0;
239Ip = length(self.nodes); % number of
nodes after addition of cs
nodes
244 P{r,s}((I+1):Ip,(I+1):Ip)=0;
255 P{r,r}(ind,csid(ind,jnd)) =
P{r,r}(ind,csid(ind,jnd)) +
P{r,s}(ind,jnd);
257 P{s,s}(csid(ind,jnd),jnd) = 1;
265connected = zeros(Ip);
267% Clear non-station
nodes' outputStrategy before setting new routing.
268% This prevents accumulation from previous link() calls,
269% since setProbRouting appends entries and resetNetwork only
270% clears station nodes (e.g., Queue, Delay), not non-station
271% stateful nodes (e.g., Router).
273 if ~isa(nodes{ind}, 'Station
') && ismethod(nodes{ind}.output, 'initDispatcherJobClasses
')
274 nodes{ind}.output.initDispatcherJobClasses(self.classes);
275 % Also update sn.routing so getRoutingMatrix won't
try PROB routing
276 %
for classes whose outputStrategy was just cleared
277 if ~isempty(self.sn) && isfield(self.sn,
'routing') && ind <= size(self.sn.routing, 1)
279 self.sn.routing(ind,k) = RoutingStrategy.DISABLED;
285 [If,Jf,S] = find(
P{r,r});
287 if connected(If(k),Jf(k)) == 0
289 connected(If(k),Jf(k)) = 1;
291 nodes{If(k)}.setProbRouting(self.classes{r},
nodes{Jf(k)}, S(k));
296% check
if the probability out of any node sums to >1.0
298isAboveOne = pSum > 1.0 + GlobalConstants.FineTol;
300 for ind=find(isAboveOne)
301 if SchedStrategy.toId(self.nodes{ind}.schedStrategy) ~= SchedStrategy.FORK
302 line_error(mfilename,sprintf(
'The total routing probability for jobs leaving node %s in class %s is greater than 1.0.',self.nodes{ind}.name,self.classes{r}.name));
304 % elseif pSum < 1.0 - GlobalConstants.FineTol % we cannot check
this case as
class r may not reach station i, in which case its outgoing routing prob
is zero
305 %
if self.nodes{i}.schedStrategy ~= SchedStrategy.EXT %
if not a sink
306 % line_error(mfilename,
'The total routing probability for jobs leaving node %s in class %s is less than 1.0.',self.nodes{i}.name,self.classes{r}.name);
312 if isa(self.nodes{ind},
'Place')
313 self.
nodes{ind}.init;
317if isReset && ~isempty(self.sn) && isfield(self.sn,
'rates')
318 self.refreshChains; % without this exception with linkAndLog
321%% Check for reducible routing (absorbing states)
323 % Pass routing matrix directly to avoid getStruct() call during link()
324 [isErg, ergInfo] = self.isRoutingErgodic(self.sn.rtorig);
325 if ~isErg && ~isempty(ergInfo.absorbingStations)
326 % Build warning message
327 absNames = strjoin(ergInfo.absorbingStations, ', ');
328 line_warning(mfilename, 'Reducible network topology detected, results may be unreliable.\n');
334 % create java version of the network
336 jnetwork = JLINE.from_line_network(self);
340 % compare sn data structures
341 jsn = JLINE.from_jline_struct(jnetwork);
342 fprintf(1,'* Comparison with Java NetworkStruct: ');
343 bool = testJavaStruct(self.getName(),self.getStruct(),jsn);