LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
sn_print.m
1%{ @file sn_print.m
2 % @brief Prints comprehensive information about a NetworkStruct object
3 %
4 % @author LINE Development Team
5%}
6
7%{
8 % @brief Prints comprehensive information about a NetworkStruct object
9 %
10 % @details
11 % This function displays all fields, matrices, lists, and maps in a formatted
12 % manner useful for debugging and inspection of network structures.
13 %
14 % @par Syntax:
15 % @code
16 % sn_print(sn)
17 % @endcode
18 %
19 % @par Parameters:
20 % <table>
21 % <tr><th>Name<th>Description
22 % <tr><td>sn<td>Network structure to inspect
23 % </table>
24%}
25function sn_print(sn)
26
27% Suppress warnings about struct on objects
28warning('off', 'MATLAB:structOnObject');
29
30% Basic integer fields
31fprintf('nstations: %d\n', sn.nstations);
32fprintf('nstateful: %d\n', sn.nstateful);
33fprintf('nnodes: %d\n', sn.nnodes);
34fprintf('nclasses: %d\n', sn.nclasses);
35fprintf('nclosedjobs: %d\n', sn.nclosedjobs);
36fprintf('nchains: %d\n', sn.nchains);
37
38% All matrix fields
39printMatrix('refstat', sn.refstat);
40printMatrix('njobs', sn.njobs);
41printMatrix('nservers', sn.nservers);
42printMatrix('connmatrix', sn.connmatrix);
43printMatrix('scv', sn.scv);
44printMatrix('isstation', sn.isstation);
45printMatrix('isstateful', sn.isstateful);
46printMatrix('isstatedep', sn.isstatedep);
47printMatrix('nodeToStateful', sn.nodeToStateful);
48printMatrix('nodeToStation', sn.nodeToStation);
49printMatrix('stationToNode', sn.stationToNode);
50printMatrix('stationToStateful', sn.stationToStateful);
51printMatrix('statefulToStation', sn.statefulToStation);
52printMatrix('statefulToNode', sn.statefulToNode);
53printMatrix('rates', sn.rates);
54printMatrix('classprio', sn.classprio);
55printMatrix('phases', sn.phases);
56printMatrix('phasessz', sn.phasessz);
57printMatrix('phaseshift', sn.phaseshift);
58printMatrix('schedparam', sn.schedparam);
59printMatrix('chains', sn.chains);
60printMatrix('rt', sn.rt);
61printMatrix('nvars', sn.nvars);
62printMatrix('rtnodes', sn.rtnodes);
63printMatrix('csmask', sn.csmask);
64printMatrix('isslc', sn.isslc);
65printMatrix('cap', sn.cap);
66printMatrix('classcap', sn.classcap);
67printMatrix('refclass', sn.refclass);
68printMatrix('lldscaling', sn.lldscaling);
69printMatrix('fj', sn.fj);
70
71% List fields with content
72printNodeTypeList('nodetype', sn.nodetype);
73if isfield(sn, 'classnames')
74 if iscell(sn.classnames)
75 printList('classnames', sn.classnames);
76 else
77 % Handle single string classname
78 fprintf('classnames: ["%s"]\n', sn.classnames);
79 end
80end
81if isfield(sn, 'nodenames')
82 printList('nodenames', sn.nodenames);
83end
84
85% Map fields with detailed contents
86printMapIfExists(sn, 'rtorig');
87%printMapIfExists(sn, 'lst');
88printMapIfExists(sn, 'state');
89printMapIfExists(sn, 'stateprior');
90printMapIfExists(sn, 'space');
91printMapIfExists(sn, 'routing');
92printMapIfExists(sn, 'procid');
93printMapIfExists(sn, 'mu');
94printMapIfExists(sn, 'phi');
95printMapIfExists(sn, 'proc');
96printMapIfExists(sn, 'pie');
97printMapIfExists(sn, 'sched');
98printMapIfExists(sn, 'inchain');
99printMapIfExists(sn, 'visits');
100printMapIfExists(sn, 'nodevisits');
101printMapIfExists(sn, 'droprule');
102printMapIfExists(sn, 'nodeparam');
103% Special handling for sync field to avoid duplicate printing
104if isfield(sn, 'sync')
105 fprintf('sync: ');
106 value = sn.sync;
107 if iscell(value)
108 % sync is a cell array of sync objects
109 fprintf('{');
110 for i = 1:length(value)
111 if i > 1
112 fprintf(', ');
113 end
114 fprintf('%d: ', i-1); % 0-based index
115
116 syncObj = value{i};
117 if isstruct(syncObj) && isfield(syncObj, 'active') && isfield(syncObj, 'passive')
118 fprintf('{');
119 % Print active events with indices
120 fprintf('"active": {');
121 if iscell(syncObj.active)
122 for j = 1:length(syncObj.active)
123 if j > 1
124 fprintf(', ');
125 end
126 fprintf('%d: ', j-1); % 0-based index
127 printValue(syncObj.active{j});
128 end
129 else
130 fprintf('0: ');
131 printValue(syncObj.active);
132 end
133 fprintf('}');
134
135 % Print passive events with indices
136 fprintf(', "passive": {');
137 if iscell(syncObj.passive)
138 for j = 1:length(syncObj.passive)
139 if j > 1
140 fprintf(', ');
141 end
142 fprintf('%d: ', j-1); % 0-based index
143 printValue(syncObj.passive{j});
144 end
145 else
146 fprintf('0: ');
147 printValue(syncObj.passive);
148 end
149 fprintf('}');
150 fprintf('}');
151 else
152 printValue(syncObj);
153 end
154 end
155 fprintf('}\n');
156 elseif isstruct(value)
157 % Check if it's a map of sync objects
158 fields = fieldnames(value);
159 if ~isempty(fields)
160 fprintf('{');
161 for i = 1:length(fields)
162 if i > 1
163 fprintf(', ');
164 end
165 key = fields{i};
166 % Handle numeric keys like x0, x1
167 if regexp(key, '^x\d+$')
168 fprintf('%s', key(2:end));
169 else
170 fprintf('"%s"', key);
171 end
172 fprintf(': ');
173
174 syncObj = value.(key);
175 if isstruct(syncObj) && isfield(syncObj, 'active') && isfield(syncObj, 'passive')
176 fprintf('{');
177 % Print active events with indices
178 fprintf('"active": {');
179 if iscell(syncObj.active)
180 for j = 1:length(syncObj.active)
181 if j > 1
182 fprintf(', ');
183 end
184 fprintf('%d: ', j-1); % 0-based index
185 printValue(syncObj.active{j});
186 end
187 else
188 fprintf('0: ');
189 printValue(syncObj.active);
190 end
191 fprintf('}');
192
193 % Print passive events with indices
194 fprintf(', "passive": {');
195 if iscell(syncObj.passive)
196 for j = 1:length(syncObj.passive)
197 if j > 1
198 fprintf(', ');
199 end
200 fprintf('%d: ', j-1); % 0-based index
201 printValue(syncObj.passive{j});
202 end
203 else
204 fprintf('0: ');
205 printValue(syncObj.passive);
206 end
207 fprintf('}');
208 fprintf('}');
209 else
210 printValue(syncObj);
211 end
212 end
213 fprintf('}\n');
214 else
215 fprintf('{}\n');
216 end
217 else
218 printValue(value);
219 fprintf('\n');
220 end
221else
222 % Field doesn't exist, skip
223end
224printMapIfExists(sn, 'gsync');
225printMapIfExists(sn, 'cdscaling');
226printMapIfExists(sn, 'jdscaling');
227
228% Restore warnings
229warning('on', 'MATLAB:structOnObject');
230
231end
232
233function matStr = printMatrixCompact(matrix)
234% Helper function to print matrix in compact format for maps
235if isempty(matrix)
236 matStr = '[]';
237 return;
238end
239
240sb = '[';
241[numRows, numCols] = size(matrix);
242for i = 1:numRows
243 if i > 1
244 sb = [sb '; '];
245 end
246 for j = 1:numCols
247 if j > 1
248 sb = [sb ' '];
249 end
250 value = matrix(i, j);
251 % Check if matrix contains only integers or if individual value is integer
252 if (isnumeric(matrix) && all(matrix(:) == floor(matrix(:))) && all(~isinf(matrix(:)))) || ...
253 (value == floor(value) && ~isinf(value))
254 sb = [sb num2str(round(value))];
255 else
256 sb = [sb num2str(value)];
257 end
258 end
259end
260sb = [sb ']'];
261matStr = sb;
262end
263
264function printNodeTypeList(name, nodeTypes)
265% Helper function to print nodetype array with proper formatting
266fprintf('%s: ', name);
267if isempty(nodeTypes)
268 fprintf('[]\n');
269else
270 fprintf('[');
271 for i = 1:length(nodeTypes)
272 if i > 1
273 fprintf(', ');
274 end
275 fprintf('%s', NodeType.toText(nodeTypes(i)));
276 end
277 fprintf(']\n');
278end
279end
280
281function printList(name, list)
282% Helper function to print lists with proper formatting
283fprintf('%s: ', name);
284if isempty(list)
285 fprintf('[]\n');
286elseif iscell(list)
287 fprintf('[');
288 for i = 1:length(list)
289 if i > 1
290 fprintf(', ');
291 end
292 if ischar(list{i}) || isstring(list{i})
293 fprintf('"%s"', char(list{i}));
294 else
295 fprintf('%s', mat2str(list{i}));
296 end
297 end
298 fprintf(']\n');
299elseif isnumeric(list)
300 % Handle numeric arrays
301 fprintf('%s\n', printMatrixCompact(list));
302else
303 % Handle other types
304 fprintf('%s\n', mat2str(list));
305end
306end
307
308function printMatrix(name, matrix)
309% Helper function to print matrix with all values
310if isempty(matrix)
311 fprintf('%s: []\n', name);
312elseif isnumeric(matrix) && all(isnan(matrix(:)))
313 fprintf('%s: null\n', name);
314else
315 fprintf('%s: %s\n', name, printMatrixCompact(matrix));
316end
317end
318
319function printMapIfExists(sn, fieldName)
320% Helper function to print fields only if they exist
321if isfield(sn, fieldName)
322 value = sn.(fieldName);
323 if isstruct(value)
324 printMapContents(fieldName, value);
325 elseif iscell(value)
326 printCellContents(fieldName, value);
327 else
328 % For other types, use printMatrix
329 printMatrix(fieldName, value);
330 end
331end
332end
333
334function printCellContents(name, cellArray)
335% Helper function to print cell array contents
336fprintf('%s: ', name);
337if isempty(cellArray)
338 fprintf('{}\n');
339else
340 % Check if this is a single-element cell array containing a matrix
341 if numel(cellArray) == 1 && isnumeric(cellArray{1})
342 % Just print the matrix directly without extra brackets
343 fprintf('%s\n', printMatrixCompact(cellArray{1}));
344 return;
345 end
346
347 % Check if this is a map-like cell array
348 if numel(cellArray) > 0 && isstruct(cellArray{1})
349 % Print as a map
350 printMapContents(name, cellArray{1});
351 return;
352 end
353
354 % Special handling for certain fields that are map-like
355 if strcmp(name, 'state') || strcmp(name, 'stateprior') || strcmp(name, 'space') || ...
356 strcmp(name, 'mu') || strcmp(name, 'phi') || strcmp(name, 'pie')
357 % These fields use station names as keys
358 fprintf('{');
359 % Assume we have access to station names through evalin
360 try
361 nodenames = evalin('caller', 'sn.nodenames');
362 for i = 1:numel(cellArray)
363 if i > 1
364 fprintf(', ');
365 end
366 if i <= length(nodenames)
367 fprintf('"%s": ', nodenames{i});
368 end
369 printValue(cellArray{i});
370 end
371 catch
372 % Fall back to simple array printing
373 for i = 1:numel(cellArray)
374 if i > 1
375 fprintf(', ');
376 end
377 printValue(cellArray{i});
378 end
379 end
380 fprintf('}\n');
381 else
382 % Regular cell array - use [] for arrays
383 fprintf('[');
384 for i = 1:numel(cellArray)
385 if i > 1
386 fprintf(', ');
387 end
388 printValue(cellArray{i});
389 end
390 fprintf(']\n');
391 end
392end
393end
394
395function printMapContents(name, mapStruct)
396% Helper function to print map contents with detailed structure
397if isempty(mapStruct) || ~exist('mapStruct', 'var')
398 if ~isempty(name)
399 fprintf('%s: null\n', name);
400 else
401 fprintf('null\n');
402 end
403 return;
404end
405
406if ~isstruct(mapStruct)
407 if ~isempty(name)
408 fprintf('%s: null\n', name);
409 else
410 fprintf('null\n');
411 end
412 return;
413end
414
415fields = fieldnames(mapStruct);
416if isempty(fields)
417 if ~isempty(name)
418 fprintf('%s: {}\n', name);
419 else
420 fprintf('{}\n');
421 end
422 return;
423end
424
425if ~isempty(name)
426 fprintf('%s: {', name);
427else
428 fprintf('{');
429end
430first = true;
431for i = 1:length(fields)
432 if ~first
433 fprintf(', ');
434 end
435 first = false;
436
437 % Print key - handle different key types like the Kotlin version
438 key = fields{i};
439 % Check if key is numeric (like x0, x1, etc.)
440 if regexp(key, '^x\d+$')
441 % Extract the numeric part
442 fprintf('%s', key(2:end));
443 elseif ischar(key)
444 fprintf('"%s"', key);
445 else
446 fprintf('"%s"', char(string(key)));
447 end
448
449 fprintf(': ');
450
451 % Print value
452 value = mapStruct.(key);
453 printValue(value);
454end
455fprintf('}\n');
456end
457
458function printValue(value)
459% Helper function to print individual values with appropriate formatting
460if isempty(value)
461 fprintf('null');
462elseif isa(value, 'jline.lang.Event') || (isjava(value) && contains(class(value), 'Event'))
463 % Handle Java Event objects
464 fprintf('{');
465 printed = false;
466
467 % Try to print each field
468 try
469 nodeVal = value.getNode();
470 fprintf('"node": %d', nodeVal);
471 printed = true;
472 catch
473 % Can't get node
474 end
475
476 try
477 eventVal = value.getEvent();
478 if printed
479 fprintf(', ');
480 end
481 fprintf('"event": ');
482 try
483 fprintf('%d', eventVal.getId());
484 catch
485 fprintf('"%s"', char(eventVal.toString()));
486 end
487 printed = true;
488 catch
489 % Can't get event
490 end
491
492 try
493 classVal = value.getJobClass();
494 if printed
495 fprintf(', ');
496 end
497 fprintf('"class": %d', classVal);
498 printed = true;
499 end
500
501 try
502 probVal = value.getProb();
503 if printed
504 fprintf(', ');
505 end
506 fprintf('"prob": ');
507 if isnan(probVal)
508 fprintf('NaN');
509 else
510 fprintf('%g', probVal);
511 end
512 printed = true;
513 catch
514 % Skip prob
515 end
516
517 try
518 stateVal = value.getState();
519 if printed
520 fprintf(', ');
521 end
522 fprintf('"state": ');
523 if isempty(stateVal) || (isa(stateVal, 'jline.util.matrix.Matrix') && stateVal.isEmpty())
524 fprintf('[]');
525 else
526 printValue(stateVal);
527 end
528 printed = true;
529 catch
530 % Skip state
531 end
532
533 try
534 tVal = value.getT();
535 if printed
536 fprintf(', ');
537 end
538 fprintf('"t": ');
539 if isnan(tVal)
540 fprintf('NaN');
541 else
542 fprintf('%g', tVal);
543 end
544 printed = true;
545 catch
546 % Skip t
547 end
548
549 try
550 jobVal = value.getJob();
551 if printed
552 fprintf(', ');
553 end
554 fprintf('"job": ');
555 if isnan(jobVal)
556 fprintf('NaN');
557 else
558 fprintf('%g', jobVal);
559 end
560 printed = true;
561 catch
562 % Skip job
563 end
564
565 % If nothing was printed, show it's an Event
566 if ~printed
567 fprintf('<Event>');
568 end
569
570 fprintf('}');
571elseif isstruct(value)
572 fields = fieldnames(value);
573 if isempty(fields)
574 % Check if this might be a Java object that appears as empty struct
575 className = class(value);
576 if contains(className, 'Event')
577 fprintf('{<Event object - fields not accessible>}');
578 else
579 fprintf('{}');
580 end
581 else
582 fprintf('{');
583 for i = 1:length(fields)
584 if i > 1
585 fprintf(', ');
586 end
587 fprintf('"%s": ', fields{i});
588 printValue(value.(fields{i}));
589 end
590 fprintf('}');
591 end
592elseif isnumeric(value)
593 if numel(value) > 1
594 fprintf('%s', printMatrixCompact(value));
595 else
596 if value == floor(value) && ~isinf(value)
597 fprintf('%d', round(value));
598 else
599 fprintf('%g', value);
600 end
601 end
602elseif ischar(value) || isstring(value)
603 fprintf('"%s"', char(value));
604elseif islogical(value)
605 if value
606 fprintf('true');
607 else
608 fprintf('false');
609 end
610elseif iscell(value)
611 % Check if this is a nested cell array (like proc field)
612 if numel(value) > 0 && iscell(value{1})
613 % Handle nested cell array as indexed map
614 fprintf('{');
615 for i = 1:numel(value)
616 if i > 1
617 fprintf(', ');
618 end
619 fprintf('[%d]: ', i-1); % Use 0-based indexing for consistency with Java
620 printValue(value{i});
621 end
622 fprintf('}');
623 else
624 % Regular cell array
625 fprintf('[');
626 for i = 1:length(value)
627 if i > 1
628 fprintf(', ');
629 end
630 printValue(value{i});
631 end
632 fprintf(']');
633 end
634elseif isa(value, 'function_handle')
635 % Display function handles similar to Java lambda notation
636 funcStr = func2str(value);
637 if contains(funcStr, '@')
638 funcStr = strrep(funcStr, '@', '');
639 end
640 fprintf('%s', funcStr);
641else
642 % Handle objects and other types
643 try
644 % Check if it's a Sync object
645 if isa(value, 'Sync')
646 fprintf('{');
647 fprintf('"active": ');
648 printValue(value.active);
649 fprintf(', "passive": ');
650 printValue(value.passive);
651 fprintf('}');
652 else
653 str = char(string(value));
654 % Check if this is a sync string that needs special handling
655 if contains(str, 'sync:')
656 % This is likely the sync field being printed as a string
657 % Extract the content after "sync:"
658 syncContent = strtrim(extractAfter(str, 'sync:'));
659 fprintf('%s', syncContent);
660 elseif contains(str, 'x') && contains(str, ' ')
661 % Clean up MATLAB object notation (e.g., 1x1 ClosedClass)
662 parts = split(str, ' ');
663 if length(parts) >= 2
664 fprintf('%s', parts{end});
665 else
666 fprintf('%s', str);
667 end
668 else
669 fprintf('%s', str);
670 end
671 end
672 catch
673 % If conversion fails, just display the class name
674 fprintf('<%s>', class(value));
675 end
676end
677end