1function stationStateAggr = sampleAggr(self, node, numEvents, markActivePassive)
2% STATIONSTATEAGGR = SAMPLEAGGR(NODE, NUMEVENTS)
4if GlobalConstants.DummyMode
5 stationStateAggr = NaN;
9if nargin<2 %~exist(
'node',
'var')
10 line_error(mfilename,'sampleAggr requires to specify a node.');
13if strcmp(self.getOptions.lang,'java')
14 % Log-based sampling relies on per-node CSV logs written and parsed by
the
15 % native JMT path;
the JLINE (lang=java) delegation does not produce them.
16 line_error(mfilename,'SolverJMT log-based sampling (sampleAggr/getProbAggr)
is not supported with lang=''java''. Use lang=''matlab'' or SolverCTMC.');
20 markActivePassive = false;
23if nargin<3 %~exist('numEvents','var')
26 line_warning(mfilename,'JMT does not allow to fix
the number of events for individual
nodes. The number of returned events may be inaccurate.\n');
27 %numEvents = numEvents - 1; % we include
the initialization as an event
31Q = getAvgQLenHandles(self);
33modelCopy = self.model.copy;
34modelCopy.resetNetwork;
37isNodeClassLogged = false(modelCopy.getNumberOfNodes, modelCopy.getNumberOfClasses);
38ind = self.model.getNodeIndex(node.name);
39for r=1:modelCopy.getNumberOfClasses
40 isNodeClassLogged(ind,r) = true;
42% apply logging to
the copied model
44isNodeLogged = max(isNodeClassLogged,[],2);
46modelCopy.linkAndLog(Plinked, isNodeLogged, logpath);
47% simulate
the model copy and retrieve log data
48solverjmt = SolverJMT(modelCopy, self.getOptions);
49if nargin>=3 && numEvents > 0
50 solverjmt.maxEvents = numEvents*sn.nnodes*sn.nclasses;
52 solverjmt.maxEvents = -1;
53 numEvents = self.getOptions.samples;
55solverjmt.getAvg(); % log data
56logData = SolverJMT.parseLogs(modelCopy, isNodeLogged, MetricType.toText(MetricType.QLen));
58% from here convert from
nodes in logData to stations
59sn = modelCopy.getStruct;
60ind = self.model.getNodeIndex(node.getName);
62nir = cell(1,sn.nclasses);
63event = cell(1,sn.nclasses);
64%ids = cell(1,sn.nclasses);
67 if isempty(logData{ind,r})
70 [~,uniqTS] = unique(logData{ind,r}.t);
71 % isNodeClassLogged
is allocated and written in NODE index space, so it
72 % must be read with
the node index. Reading it with
the stateful index
73 % silently skipped
this branch, and hence returned an empty sample path,
74 %
for every model whose target node sits above a non-stateful node such
75 % as a Sink, which shifts nodeToStateful(ind) away from ind.
76 if isNodeClassLogged(ind,r)
77 if ~isempty(logData{ind,r})
78 t = logData{ind,r}.t(uniqTS);
79 t = [t(2:end);t(end)];
80 nir{r} = logData{ind,r}.QLen(uniqTS);
81 event{r} = logData{ind,r}.event;
82 %ids{r} = logData{ind,r}.
87if isfinite(self.options.timespan(2))
88 stopAt = find(t>self.options.timespan(2),1,'first');
89 if ~isempty(stopAt) && stopAt>1
92 nir{r} = nir{r}(1:(stopAt-1));
97if length(t) < 1+numEvents
98 line_warning(mfilename,
'LINE could not estimate correctly the JMT simulation length to return the desired number of events at the specified node. Try to re-run increasing the number of events.\n');
101stationStateAggr =
struct();
102stationStateAggr.handle = node;
103stationStateAggr.t = t;
104stationStateAggr.t = stationStateAggr.t(1:min(length(t),1+numEvents),:);
105stationStateAggr.t = [0; stationStateAggr.t(1:end-1)];
106stationStateAggr.state = cell2mat(nir);
107stationStateAggr.state = stationStateAggr.state(1:min(length(t),1+numEvents),:);
108%stationStateAggr.job_id =
110event = cellmerge(event);
111event = {
event{cellisa(event,
'Event')}}
';
112event_t = cellfun(@(c) c.t, event);
113event_t = event_t(event_t <= max(stationStateAggr.t));
115stationStateAggr.event = {event{I}};
116stationStateAggr.event = stationStateAggr.event';
117stationStateAggr.isaggregate =
true;
120 apevent = cell(1,length(stationStateAggr.t)-1);
121 for ti = 1:length(apevent)
122 apevent{ti} =
struct(
'active',[],
'passive',[]);
124 for e=1:length(stationStateAggr.event)
125 ti = find(stationStateAggr.event{e}.t == stationStateAggr.t);
127 switch stationStateAggr.event{e}.event
129 apevent{ti-1}.passive = stationStateAggr.event{e};
131 apevent{ti-1}.active = stationStateAggr.event{e};
135 stationStateAggr.event = apevent
';