Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 | 137x 137x 137x 137x 137x 137x 21509x 137x 137x 137x 137x 137x 137x 137x 137x 6439x 20139x 20139x 6439x 6439x 6439x 6439x 6439x 3973x 3973x 3973x 7672x 2329x 411x 411x 7672x 7672x 137x 137x 137x 137x 137x 137x 137x 6028x 137x 137x 6028x 137x 3973x 3973x 3973x 2192x 1918x 274x 274x 411x 411x 274x 274x 411x 274x 6850x 6850x 6850x 6850x 6850x 6850x 6850x 1233x 6850x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 14111x 14111x 6987x 7124x 7124x 7124x 7124x 7124x 7124x 7124x 411x 7124x 7124x 7124x 8083x 8083x 8083x 4110x 8083x 4795x 7124x 548x 411x 274x 411x 548x 7124x 7124x 7124x 411x 7124x 7124x 6439x 6439x 7124x 7124x 7124x 15070x 15070x 15070x 15070x 7398x 7398x 7672x 7672x 7672x 7672x 7672x 1096x 1096x 1096x 7672x 6439x 6576x 6439x 2192x 2192x 2192x 137x 137x 137x 3562x 3562x 3562x 3562x 3562x 548x 548x 548x 548x 6439x 411x 411x 6439x 6439x 6439x 6439x 6439x 6439x 6439x 2192x 4247x 3562x 3562x 685x 685x 685x 685x 137x 137x 137x 137x 548x 548x 548x 548x 411x 548x 548x 548x 548x 548x 548x 548x 548x 137x 411x 411x 411x 411x 411x 137x 274x 274x 274x 8083x 8083x 3699x 4384x 4384x 1096x 3288x | /**
* The **display set context**: the named values that the display set split
* rules read, in addition to the DICOM keywords of the instance.
*
* A context name starts with a lowercase letter. The global context defines a
* name for every rule, and the `context` of a rule defines or replaces a name
* for that rule only. An entry is a fixed value, an expression, a declaration,
* a template or a group; the compile infers its instance or series scope. See
* "The display set context" in
* `packages/docs/docs/concepts/cornerstone-metadata/display-sets.md`.
*
* @module displayset/displaySetContext
*/
import { asArray, logging } from '@cornerstonejs/utils';
import {
at,
collectReads,
didYouMean,
compileExpressionAt,
FORBIDDEN_PROPERTIES,
hasNoValue,
hasOwn,
invalidAt,
isUnsafeKey,
matchForm,
parseExpressionSource,
parseTemplate,
readOwn,
RuleCompileError,
} from '../safeFunctions';
import type {
CompiledExpression,
ExpressionNode,
ExpressionRead,
SchemaSite,
TemplateSegment,
} from '../safeFunctions';
import { keywordIgnoringCase, resolveHexFromKeyword } from '../utilities/Tags';
import { sortInstancesCopy } from './sortInstances';
import { instanceKey } from './seriesFunctions';
import {
bulkAnnotationSopClassUids,
chartSopClassUids,
ecgSopClassUids,
encapsulatedDocumentSopClassUids,
imageSopClassUids,
longVideoMinFrames,
longVideoSopClassUids,
parametricMapSopClassUids,
rtStructureSetSopClassUids,
segmentationSopClassUids,
structuredReportSopClassUids,
videoSopClassUids,
videoTransferSyntaxUids,
wholeSlideSopClassUids,
} from './sopClassUidLists';
import { ATTRIBUTE_ONLY_NAMES, splitRuleSchema } from './splitRuleSchema';
import type {
NaturalizedInstance,
SeriesContext,
SeriesFacts,
SeriesFieldScope,
SeriesFunction,
SeriesFunctionDefinition,
} from './types';
const log = logging.metadataLog.getLogger('displayset', 'displaySetContext');
/** The values of the context, as a code entry reads them: `values.sliceIndex`. */
export type ContextValues = Readonly<Record<string, unknown>>;
/** A code entry with the instance scope. */
export type InstanceContextFunction = (
instance: NaturalizedInstance,
values: ContextValues
) => unknown;
/**
* A group entry: one call of a series function. It runs over the series; in
* `sortBy` and `attributes`, `displaySet.<name>` runs it over the display set.
*/
export type ContextGroupEntry = {
function: string | SeriesFunction | SeriesFunctionDefinition;
args?: Record<string, unknown>;
};
/** One entry of a display set context. See the module documentation. */
export type ContextEntry =
| number
| boolean
| null
| readonly unknown[]
| string
| { attribute: string; vr?: string; vm?: string }
| { template: string }
| ContextGroupEntry
| InstanceContextFunction;
/** A map of context names to entries: the `context` option, or a rule `context`. */
export type DisplaySetContext = Readonly<Record<string, ContextEntry>>;
/** The series fact under which a split keeps the values of the context. */
export const DISPLAY_SET_CONTEXT_FACT = 'displaySetContext';
/**
* The default metadata context. The default rules, the example modules and a
* site's own rules read these names. Every entry is data, so a host or a
* server applies the same JSON overlay to it.
*
* - `timeClusters` and `planeGeometry` are groups; their aliases give names to
* the result fields. A rule changes an argument (`timeClustersMaxGap`) or
* selects another group (`"timeClusters": "longTimeClusters"`) in its
* `context`, and every alias follows.
* - The `*SopClassUids` and `*TransferSyntaxUids` entries are the lists of
* `sopClassUidLists.ts`, under the same names. `isImage`, `isVideo`,
* `isEcg`, `isWsi` and one `is*` entry for each other kind of object test
* the lists; `isImageInstance` and the other code tests give the same result.
* - `imageId` declares the synthetic attribute that the series functions read.
*/
export const defaultMetadataContext: DisplaySetContext = {
timeClustersMaxGap: 30,
timeClusters: {
function: 'timeClusters',
args: { maxGap: 'timeClustersMaxGap' },
},
longTimeClusters: { function: 'timeClusters', args: { maxGap: 60 } },
sweepCount: 'timeClusters.clusters',
sweepStart: 'timeClusters.start',
acquisitionSeconds: 'timeClusters.time',
sweepOffset: 'round(sweepStart - timeClusters.first)',
planeGeometryTolerance: 0.1,
planeGeometry: {
function: 'planeGeometry',
args: { tolerance: 'planeGeometryTolerance' },
},
sliceSpacing: 'planeGeometry.spacing',
sliceIndex: 'planeGeometry.index',
sliceDistance: 'planeGeometry.distance',
// Revision chains of PredecessorDocumentsSequence, inside one series.
revisionChains: { function: 'revisionChains' },
revisionChainUid: 'revisionChains.active',
isActiveRevision: 'SOPInstanceUID == revisionChainUid',
seriesInstanceCount: 'series.instances.count()',
isMultiFrame: 'NumberOfFrames > 1',
imageId: { attribute: 'imageId' },
imageSopClassUids,
videoSopClassUids,
videoTransferSyntaxUids,
longVideoSopClassUids,
longVideoMinFrames,
ecgSopClassUids,
wholeSlideSopClassUids,
segmentationSopClassUids,
rtStructureSetSopClassUids,
parametricMapSopClassUids,
structuredReportSopClassUids,
encapsulatedDocumentSopClassUids,
bulkAnnotationSopClassUids,
chartSopClassUids,
isImage: 'SOPClassUID in imageSopClassUids',
transferSyntaxKnown:
'AvailableTransferSyntaxUID != null || TransferSyntaxUID != null',
// The same three steps as isVideoInstance.
isVideo:
'SOPClassUID in videoSopClassUids || ' +
'AvailableTransferSyntaxUID in videoTransferSyntaxUids || ' +
'TransferSyntaxUID in videoTransferSyntaxUids || ' +
'(!transferSyntaxKnown && SOPClassUID in longVideoSopClassUids && ' +
'NumberOfFrames >= longVideoMinFrames)',
isEcg: 'SOPClassUID in ecgSopClassUids',
isWsi: "SOPClassUID in wholeSlideSopClassUids || Modality == 'SM'",
isSegmentation: 'SOPClassUID in segmentationSopClassUids',
isRtStructureSet: 'SOPClassUID in rtStructureSetSopClassUids',
isParametricMap: 'SOPClassUID in parametricMapSopClassUids',
isStructuredReport: 'SOPClassUID in structuredReportSopClassUids',
isEncapsulatedDocument: 'SOPClassUID in encapsulatedDocumentSopClassUids',
isBulkAnnotation: 'SOPClassUID in bulkAnnotationSopClassUids',
isChart: 'SOPClassUID in chartSopClassUids',
};
const CONTEXT_NAME = /^[a-z][A-Za-z0-9_]*$/;
/** The literals of the expression language: no context entry can have these names. */
const RESERVED_LITERALS = new Set([
'true',
'false',
'null',
'undefined',
'defined',
]);
const HEX_TAG = /^[0-9A-Fa-f]{8}$/;
/** True when the name is a keyword of the DICOM dictionary of dcmjs. */
export function isTagKeyword(name: string): boolean {
return resolveHexFromKeyword(name) !== undefined;
}
type Scope = SeriesFieldScope;
const DISPLAY_SETS_RESERVED =
'displaySets aggregates are not available yet; see https://github.com/cornerstonejs/cornerstone3D/issues/3000';
/** The expression of a context entry: `series.instances.*(...)` only. */
const EXPRESSION_SCOPE = Object.freeze({
params: Object.freeze(['#values', '#instances']),
implicitScope: '#values',
receivers: Object.freeze({ series: '#instances' }),
unavailableReceivers: Object.freeze({
displaySet:
'displaySet.instances aggregates are available only in the { "expression" } form of attributes',
displaySets: DISPLAY_SETS_RESERVED,
}),
});
/** The expression of `attributes`: also `displaySet.instances.*(...)`. */
const ATTRIBUTE_EXPRESSION_SCOPE = Object.freeze({
params: Object.freeze(['#values', '#instances', '#displaySetInstances']),
implicitScope: '#values',
receivers: Object.freeze({
series: '#instances',
displaySet: '#displaySetInstances',
}),
unavailableReceivers: Object.freeze({ displaySets: DISPLAY_SETS_RESERVED }),
});
/** The receivers of the aggregates: no context entry can have these names. */
const RESERVED_RECEIVERS = new Set(['series', 'displaySet', 'displaySets']);
/** One argument of a group: a fixed value, or an expression. */
type GroupArgument = {
name: string;
fixed?: unknown;
evaluate?: CompiledExpression;
reads: ExpressionRead[];
};
/** One parsed entry of one layer (the global context, or a rule context). */
type Definition = {
name: string;
/** The rule of a rule entry; undefined for a global entry. */
ruleId?: string;
site: SchemaSite;
/** Equal for two entries with the same data, for the cache key. */
identity: string;
kind: 'fixed' | 'declaration' | 'expression' | 'template' | 'group' | 'code';
reads: ExpressionRead[];
value?: unknown;
attribute?: string;
/** The VR and the VM of a declaration, recorded for a later JSON Schema. */
vr?: string;
vm?: string;
evaluate?: CompiledExpression;
/** An expression that is one bare name: a candidate alias of a group. */
aliasOf?: string;
segments?: TemplateSegment[];
compute?: SeriesFunction;
fields?: Readonly<Record<string, Scope>>;
args?: GroupArgument[];
fn?: InstanceContextFunction;
};
/**
* What a name in an entry or a rule reads. `displaySet` is the `displaySet.`
* prefix of `sortBy` and `attributes`: its fields read over the display set.
*/
type Dependency =
| { kind: 'keyword' }
| { kind: 'attribute' }
| { kind: 'entry'; bound: Bound }
| { kind: 'series'; inner: Dependency }
| { kind: 'displaySet'; fields: Map<string, Dependency> };
/** The places where a bare name reads the series, and `displaySet.` the display set. */
const DISPLAY_SET_PLACES = new Set(['sortBy', 'attributes']);
const DISPLAY_SET_PREFIX_ONLY =
"the 'displaySet.' prefix is valid only in sortBy and attributes";
/** Extra values of a place: the attribute-only names of `attributes`. */
type Extras = Readonly<Record<string, unknown>>;
/**
* One entry bound into the context of one rule: its scope, its dependencies,
* and its cache key. Rules whose replacements do not reach an entry share one
* bound entry, and so one computation for each series.
*/
type Bound = {
key: string;
name: string;
scope: Scope;
/** Set when the value is the result of a series function. */
group?: { fields?: Readonly<Record<string, Scope>> };
deps: Map<string, Dependency>;
/** The names that the rule replaces and that this entry reads. */
closure: ReadonlySet<string>;
/** The rule of a rule entry; undefined for a global entry. */
ruleId?: string;
/**
* True when the value can differ for each display set: a group, a code
* entry, or an entry that reads one.
*/
perDisplaySet: boolean;
/** For a code entry: the layer that resolves the names it reads at run time. */
layer?: ContextLayer;
/** For a code entry: its definition, so that a read of its own name gets the global entry. */
definition?: Definition;
compute: (
runtime: ContextRuntime,
instance: NaturalizedInstance | undefined
) => unknown;
};
/** The reader of one name at one place of a rule. */
export type ContextReader = (
runtime: ContextRuntime,
instance: NaturalizedInstance | undefined,
extras?: Extras
) => unknown;
let functionCount = 0;
const functionIds = new WeakMap<object, string>();
/** A text that is equal for two entries with the same data. */
function identityOf(raw: unknown): string {
return JSON.stringify(raw, (_key, value) => {
Eif (typeof value !== 'function') {
return value;
}
let id = functionIds.get(value);
if (!id) {
id = `#function${++functionCount}`;
functionIds.set(value, id);
}
return id;
});
}
/** The message for a name that is not a tag keyword and not a context name. */
function unknownName(
name: string,
place: string,
candidates: Iterable<string>
): string {
const suggestion = didYouMean(name, [
keywordIgnoringCase(name),
...candidates,
]);
if (suggestion) {
return `unknown name '${name}'${suggestion}`;
}
let hint: string;
if (HEX_TAG.test(name)) {
hint = `a hex tag needs a declaration in the context, e.g. "myValue": { "attribute": "${name}" }`;
} else if (RESERVED_LITERALS.has(name)) {
hint = `'${name}' is a literal; write X == ${name} in a context entry`;
} else if (/^[a-z]/.test(name)) {
hint = 'no context entry has this name';
} else {
hint = `it is not a keyword of the DICOM dictionary; a private, vendor or synthetic attribute needs a declaration, e.g. "myValue": { "attribute": "${name}" }`;
}
const fixedText =
place === 'attributes'
? `; fixed text is { "template": ${JSON.stringify(name)} }`
: '';
return `unknown name '${name}': ${hint}${fixedText}`;
}
/** Throws for a context name that the rules do not allow. */
function checkContextName(name: string, site: SchemaSite): void {
Iif (isUnsafeKey(name) || FORBIDDEN_PROPERTIES.has(name)) {
invalidAt(site, `the context name "${name}" is not allowed`);
}
Iif (RESERVED_LITERALS.has(name) || RESERVED_RECEIVERS.has(name)) {
invalidAt(
site,
`'${name}' is reserved by the expression language, and cannot be a context name`
);
}
Iif (isTagKeyword(name)) {
invalidAt(
site,
`the context name '${name}' is a DICOM keyword; a context cannot hide a value of the instance`
);
}
Iif (!CONTEXT_NAME.test(name)) {
invalidAt(
site,
`the context name '${name}' must start with a lowercase letter, and hold only letters, digits and _`
);
}
Iif ((ATTRIBUTE_ONLY_NAMES as readonly string[]).includes(name)) {
invalidAt(
site,
`'${name}' is a value of the display set, and cannot be a context name`
);
}
}
/** Parses one expression at a site: the compiled function and its reads. */
function parseExpression(
source: string,
site: SchemaSite,
scope:
| typeof EXPRESSION_SCOPE
| typeof ATTRIBUTE_EXPRESSION_SCOPE = EXPRESSION_SCOPE
): {
evaluate: CompiledExpression;
reads: ExpressionRead[];
single?: string;
ast: ExpressionNode;
} {
const evaluate = compileExpressionAt(source, site, scope);
const ast = parseExpressionSource(source);
return {
evaluate,
reads: collectReads(ast, { aggregates: true }),
single: ast.type === 'identifier' ? ast.name : undefined,
ast,
};
}
/**
* The field names that an expression reads below a value: the property of
* each member access, and the text of each literal string index. A computed
* index gives `undefined`. A numeric literal index reads an element or an
* item, and gives nothing.
*/
function propertyReads(node: ExpressionNode): Array<string | undefined> {
const found: Array<string | undefined> = [];
const walk = (current: ExpressionNode): void => {
switch (current.type) {
case 'member':
found.push(current.property);
walk(current.object);
return;
case 'index':
if (current.index.type !== 'literal') {
found.push(undefined);
} else if (typeof current.index.value === 'string') {
found.push(current.index.value);
}
walk(current.object);
walk(current.index);
return;
case 'call':
current.args.forEach(walk);
return;
case 'unary':
walk(current.argument);
return;
case 'binary':
case 'logical':
walk(current.left);
walk(current.right);
return;
case 'conditional':
walk(current.test);
walk(current.consequent);
walk(current.alternate);
return;
case 'array':
current.elements.forEach(walk);
return;
case 'template':
current.parts.forEach(
(part) => part.kind === 'expr' && walk(part.node)
);
return;
}
};
walk(node);
return found;
}
/**
* True for a fixed value: `null`, a boolean, a finite number, or (where
* `arrays` is true) an array.
*/
export function isFixedValue(raw: unknown, arrays = true): boolean {
return (
raw === null ||
typeof raw === 'boolean' ||
(typeof raw === 'number' && Number.isFinite(raw)) ||
(arrays && Array.isArray(raw))
);
}
/**
* A fixed array as a copy that the compile owns: one stable array for each
* compile, so a membership test can cache its `Set`, and a later change of the
* input does not reach the compiled rules.
*/
function copyFixedValue(raw: unknown): unknown {
return Array.isArray(raw) ? [...raw] : raw;
}
/** The text of a loop of entries, e.g. `a -> b -> a`. */
function loopText(entries: { name: string; ruleId?: string }[]): string {
return entries
.map(({ name, ruleId }) =>
ruleId === undefined ? name : `${name} (rule '${ruleId}')`
)
.join(' -> ');
}
/**
* The series facts for a new order of `instances`. When an instance is not in
* the instances of the display set context of the facts (the series got new
* instances after the split), the values are computed again over the old and
* the new instances. Otherwise the facts are returned as they are.
*/
export function withInstances(
series: SeriesFacts,
instances: readonly NaturalizedInstance[]
): SeriesFacts {
const values = readOwn(series, DISPLAY_SET_CONTEXT_FACT);
if (!(values instanceof DisplaySetContextValues)) {
return series;
}
// By key, not by object: a host can naturalize the instances again.
const keyOf = (instance: NaturalizedInstance) =>
instanceKey(instance) ?? instance;
const known = new Set(values.instances.map(keyOf));
const added = instances.filter((instance) => !known.has(keyOf(instance)));
if (!added.length) {
return series;
}
return {
...series,
// The default order, as a new split of the same series gives.
[DISPLAY_SET_CONTEXT_FACT]: new DisplaySetContextValues(
sortInstancesCopy([...values.instances, ...added])
),
};
}
/**
* The values of the display set context for one list of instances, and one
* cache for each compiled rule set.
*
* A split keeps the values of the series as the series fact
* {@link DISPLAY_SET_CONTEXT_FACT}, so every rule of the split reads the same
* values. In `sortBy` and `attributes`, a bare name also reads the values of
* the split, and a `displaySet.<name>` reads the values of one display set
* ({@link displaySetContextFor}): a series function such as `planeGeometry`
* runs over the instances of the display set, and `series.instances.*(...)`
* still runs over the series.
*/
export class DisplaySetContextValues {
private readonly runtimes = new WeakMap<ContextProgram, ContextRuntime>();
/**
* @param instances - the instances that a series function runs over: the
* series, or one display set.
* @param seriesInstances - the instances of `series.instances.*(...)`.
* Defaults to `instances`.
* @param split - for one display set: the values of the split, which keep
* the series values that are the same for every display set.
*/
constructor(
readonly instances: readonly NaturalizedInstance[],
readonly seriesInstances: readonly NaturalizedInstance[] = instances,
private readonly split?: DisplaySetContextValues
) {}
/** The cache of one compiled rule set. */
runtime(program: ContextProgram): ContextRuntime {
let runtime = this.runtimes.get(program);
if (!runtime) {
runtime = new ContextRuntime(
this.instances,
this.seriesInstances,
this.split?.runtime(program)
);
this.runtimes.set(program, runtime);
}
return runtime;
}
}
/**
* The context values of one display set: a series function runs over
* `instances`, and `series.instances.*(...)` over the instances of the split
* that `series` holds. The split computes each value that does not read a
* series function (an aggregate of `series.instances`, for example), so the
* value is computed one time for each split. Without a split in `series`, the
* values cover `instances` only.
*/
export function displaySetContextFor(
series: SeriesFacts | undefined,
instances: readonly NaturalizedInstance[]
): DisplaySetContextValues {
const values = readOwn(series, DISPLAY_SET_CONTEXT_FACT);
return values instanceof DisplaySetContextValues
? new DisplaySetContextValues(instances, values.seriesInstances, values)
: new DisplaySetContextValues(instances);
}
/**
* The values of one compiled rule set for one split. A value is computed at
* the first read, and kept: a series value once for the series, an instance
* value once for each instance.
*/
export class ContextRuntime {
private readonly seriesValues = new Map<string, unknown>();
private readonly instanceValues = new Map<
NaturalizedInstance,
Map<string, unknown>
>();
private readonly views = new Map<NaturalizedInstance, Map<string, unknown>>();
private readonly envs = new Map<
string,
Map<NaturalizedInstance | undefined, object>
>();
private readonly instanceEnvs = new Map<string, object[]>();
/** The entries that are in progress, in the order of the reads. */
private readonly inProgress: {
bound: Bound;
instance: NaturalizedInstance | undefined;
}[] = [];
/**
* @param instances - the instances that a series function runs over.
* @param seriesInstances - the instances of `series.instances.*(...)`.
* @param split - for the runtime of one display set: the runtime of the
* split, which computes and keeps each series value that is the same for
* every display set. So such a value is computed one time for each split.
*/
constructor(
readonly instances: readonly NaturalizedInstance[],
readonly seriesInstances: readonly NaturalizedInstance[] = instances,
private readonly split?: ContextRuntime
) {}
/** The runtime of the split: a bare name in `sortBy` and `attributes` reads it. */
get seriesRuntime(): ContextRuntime {
return this.split ?? this;
}
/** The value of a bound entry: for the series, or for one instance. */
value(bound: Bound, instance: NaturalizedInstance | undefined): unknown {
if (bound.scope === 'series') {
if (this.split && !bound.perDisplaySet) {
return this.split.value(bound, undefined);
}
if (!this.seriesValues.has(bound.key)) {
this.seriesValues.set(bound.key, this.compute(bound, undefined));
}
return this.seriesValues.get(bound.key);
}
if (!instance) {
return undefined;
}
let values = this.instanceValues.get(instance);
if (!values) {
values = new Map();
this.instanceValues.set(instance, values);
}
if (!values.has(bound.key)) {
values.set(bound.key, this.compute(bound, instance));
}
return values.get(bound.key);
}
/**
* Computes one entry, and records it as in progress. A read of an entry
* that is already in progress, for the same instance and at the same level
* (the bound entry: global, or the replacement of a rule), is a loop. Only a
* loop through a code entry gets here; the compile rejects a loop of data
* entries.
*/
private compute(
bound: Bound,
instance: NaturalizedInstance | undefined
): unknown {
const start = this.inProgress.findIndex(
(entry) => entry.bound === bound && entry.instance === instance
);
if (start !== -1) {
const loop = [
...this.inProgress.slice(start).map((entry) => entry.bound),
bound,
];
throw new RuleCompileError(
'display set rules',
bound.name,
`Invalid display set rules: the context entries form a loop: ${loopText(loop)}`
);
}
this.inProgress.push({ bound, instance });
try {
return bound.compute(this, instance);
} finally {
this.inProgress.pop();
}
}
/**
* The result of a group as one instance sees it: a series field as is, and
* an instance field as the value of that instance.
*/
groupView(bound: Bound, instance: NaturalizedInstance | undefined): unknown {
const result = this.value(bound, instance);
const fields = bound.group?.fields;
if (!instance || !fields || !result || typeof result !== 'object') {
return result;
}
let views = this.views.get(instance);
if (!views) {
views = new Map();
this.views.set(instance, views);
}
if (!views.has(bound.key)) {
const key = instanceKey(instance);
const view: Record<string, unknown> = Object.create(null);
for (const [field, scope] of Object.entries(fields)) {
const value = readOwn(result, field);
view[field] =
scope === 'instance'
? key === undefined
? undefined
: readOwn(value, key)
: value;
}
views.set(bound.key, view);
}
return views.get(bound.key);
}
/** The value of a dependency, read by `name`. */
read(
name: string,
dependency: Dependency | undefined,
instance: NaturalizedInstance | undefined,
extras?: Extras
): unknown {
if (!dependency) {
return undefined;
}
switch (dependency.kind) {
case 'keyword':
return instance ? readOwn(instance, name) : undefined;
case 'attribute':
return readOwn(extras, name);
case 'entry':
return dependency.bound.group
? this.groupView(dependency.bound, instance)
: this.value(dependency.bound, instance);
case 'series':
return this.seriesRuntime.read(
name,
dependency.inner,
instance,
extras
);
case 'displaySet': {
const fields: Record<string, unknown> = Object.create(null);
dependency.fields.forEach((field, fieldName) => {
fields[fieldName] = this.read(fieldName, field, instance, extras);
});
return fields;
}
}
}
/**
* The names that an entry reads, as the implicit scope of its expression:
* for the series, or for one instance.
*/
env(
bound: Pick<Bound, 'key' | 'deps' | 'layer' | 'definition'>,
instance: NaturalizedInstance | undefined,
extras?: Extras,
fresh = false
): object {
const cacheable = !extras && !fresh;
let byInstance = cacheable ? this.envs.get(bound.key) : undefined;
const cached = byInstance?.get(instance);
if (cached) {
return cached;
}
const env = new Proxy(Object.create(null), {
has: () => true,
get: (_target, name) => {
if (typeof name !== 'string') {
return undefined;
}
const dependency =
bound.deps.get(name) ??
bound.layer?.dynamicDependency(name, bound.definition);
return this.read(name, dependency, instance, extras);
},
});
if (cacheable) {
if (!byInstance) {
byInstance = new Map();
this.envs.set(bound.key, byInstance);
}
byInstance.set(instance, env);
}
return env;
}
/**
* The instance environments of an entry, for the aggregates. The
* environments of the series instances come from the split when they are
* the same for every display set.
*/
envsOf(
bound: Pick<
Bound,
'key' | 'deps' | 'layer' | 'definition' | 'perDisplaySet'
>,
instances: readonly NaturalizedInstance[] = this.seriesInstances,
extras?: Extras
): object[] {
if (extras || instances !== this.seriesInstances) {
return instances.map((instance) => this.env(bound, instance, extras));
}
if (this.split && !bound.perDisplaySet) {
return this.split.envsOf(bound, this.split.seriesInstances);
}
let envs = this.instanceEnvs.get(bound.key);
if (!envs) {
envs = instances.map((instance) => this.env(bound, instance));
this.instanceEnvs.set(bound.key, envs);
}
return envs;
}
}
/**
* The context of one rule: the global entries, and the entries of the rule
* `context`, which replace or add names for this rule only.
*/
export class ContextLayer {
readonly memo = new Map<Definition, Bound>();
private readonly dynamic = new Map<
Definition | undefined,
Map<string, Dependency | undefined>
>();
constructor(
readonly program: ContextProgram,
readonly ruleId: string | undefined,
readonly local: ReadonlyMap<string, Definition>
) {}
/**
* The entry that a name gives, read from `from`. An entry of the rule that
* reads its own name gets the global entry.
*/
resolve(name: string, from?: Definition): Definition | undefined {
Iif (from && from.ruleId !== undefined && from.name === name) {
return this.program.global.get(name);
}
return this.local.get(name) ?? this.program.global.get(name);
}
/**
* A dependency of a code entry, resolved at the first read. `from` is the
* code entry, so that a rule entry that reads its own name gets the global
* entry. A name on the deny list of the host reads nothing.
*/
dynamicDependency(name: string, from?: Definition): Dependency | undefined {
let byName = this.dynamic.get(from);
if (!byName) {
byName = new Map();
this.dynamic.set(from, byName);
}
if (!byName.has(name)) {
let dependency: Dependency | undefined;
if (this.program.isDenied(name)) {
log.warn(
`a code entry of the context read '${name}', which is on the deny list of the host; the read gives undefined`
);
} else if (isTagKeyword(name)) {
dependency = { kind: 'keyword' };
} else {
const definition = this.resolve(name, from);
if (definition) {
dependency = {
kind: 'entry',
bound: this.program.bind(definition, this, []),
};
} else if ((ATTRIBUTE_ONLY_NAMES as readonly string[]).includes(name)) {
dependency = { kind: 'attribute' };
}
}
byName.set(name, dependency);
}
return byName.get(name);
}
}
/** Options of {@link ContextProgram}. */
export type ContextProgramOptions = {
seriesFunctions: Readonly<
Record<string, SeriesFunction | SeriesFunctionDefinition>
>;
/** The site of the global context, for the messages. */
site: SchemaSite;
/** Names that no rule and no code entry can read (`RL-SCOPE-9`). */
deniedNames?: readonly string[];
};
/**
* One compiled display set context: the global entries, the bound entries
* of every rule, and the `series` hook that gives a split its values.
*/
export class ContextProgram {
readonly global = new Map<string, Definition>();
private readonly bounds = new Map<string, Bound>();
private readonly seriesFunctions: ContextProgramOptions['seriesFunctions'];
private readonly empty: ContextLayer;
private readonly denied: ReadonlySet<string>;
/** The hex tags of the denied keywords, so a declaration cannot read one. */
private readonly deniedTags: ReadonlySet<string>;
private fallbackLogged = false;
constructor(context: unknown, options: ContextProgramOptions) {
this.seriesFunctions = options.seriesFunctions;
this.denied = new Set(options.deniedNames ?? []);
this.deniedTags = new Set(
[...this.denied].map(resolveHexFromKeyword).filter(Boolean)
);
for (const definition of this.parseLayer(
context,
options.site,
undefined
)) {
this.global.set(definition.name, definition);
}
this.empty = new ContextLayer(this, undefined, new Map());
// Every global entry compiles, also an entry that no rule reads.
for (const definition of this.global.values()) {
this.bind(definition, this.empty, []);
}
}
/**
* The `series` hook of every compiled rule: the first rule of a split adds
* the values of the context, and the other rules read them.
*/
readonly seriesHook = ({ instances, series }: SeriesContext): SeriesFacts =>
hasOwn(series, DISPLAY_SET_CONTEXT_FACT)
? {}
: { [DISPLAY_SET_CONTEXT_FACT]: new DisplaySetContextValues(instances) };
/** True when the host denies the name. */
isDenied(name: string): boolean {
return this.denied.has(name);
}
/**
* Parses one expression, and applies the deny list to the reads below a
* name. The name check sees only the first name of a path, so a field of a
* sequence item (`OtherPatientIDsSequence[0].PatientID`) is checked here. A
* computed field name cannot be checked, so it is a compile error when the
* host has a deny list.
*/
private parseExpression(
source: string,
site: SchemaSite,
scope?: typeof EXPRESSION_SCOPE | typeof ATTRIBUTE_EXPRESSION_SCOPE
): ReturnType<typeof parseExpression> {
const parsed = parseExpression(source, site, scope);
Iif (this.denied.size) {
for (const property of propertyReads(parsed.ast)) {
if (property === undefined) {
invalidAt(
site,
'a computed field name is not allowed when the host has a deny list; use a literal index, e.g. ViewCodeSequence[0].CodeValue'
);
}
if (this.denied.has(property)) {
invalidAt(
site,
`the name '${property}' is on the deny list of the host, so no rule can read it`
);
}
}
}
return parsed;
}
/**
* The runtime of a call, from its context. A context with `displaySet` (a
* sort or the attributes of one display set) gives the runtime of that
* display set. Else the series facts give the runtime of the split. A direct
* call of a hook with no split context computes the values over the
* instances of the call alone (one instance, or the pair of a comparator):
* so a series value such as `seriesInstanceCount` is for those instances
* only.
*/
runtimeFrom(
context: unknown,
fallback: NaturalizedInstance | readonly NaturalizedInstance[]
): ContextRuntime {
const displaySet = readOwn(context, 'displaySet');
if (displaySet instanceof DisplaySetContextValues) {
return displaySet.runtime(this);
}
const values = readOwn(
readOwn(context, 'series'),
DISPLAY_SET_CONTEXT_FACT
);
if (values instanceof DisplaySetContextValues) {
return values.runtime(this);
}
if (!this.fallbackLogged) {
this.fallbackLogged = true;
log.debug(
'a split rule hook was called with no split context; its context values cover only the instances of the call'
);
}
return new DisplaySetContextValues(
fallback ? (asArray(fallback) as NaturalizedInstance[]) : []
).runtime(this);
}
/**
* The value of the global entry `name` for one instance, with no split: the
* runtime covers that instance only. A series-scope entry is an error.
*/
instanceValue(name: string, instance: NaturalizedInstance): unknown {
const definition = this.global.get(name);
if (!definition) {
throw new Error(`the metadata context has no entry '${name}'`);
}
const bound = this.bind(definition, this.empty, []);
if (bound.scope !== 'instance') {
throw new Error(
`the context entry '${name}' has the series scope, so one instance cannot give its value`
);
}
return new DisplaySetContextValues([instance])
.runtime(this)
.read(name, { kind: 'entry', bound }, instance);
}
/** The context of one rule, with its `context` block. */
layer(ruleId: string, context: unknown, site: SchemaSite): ContextLayer {
if (context === undefined) {
return this.empty;
}
const local = new Map<string, Definition>();
for (const definition of this.parseLayer(context, site, ruleId)) {
local.set(definition.name, definition);
log.debug(
this.global.has(definition.name)
? `rule '${ruleId}': the context entry '${definition.name}' replaces the global entry for this rule`
: `rule '${ruleId}': the context entry '${definition.name}' defines a new name for this rule`
);
}
const layer = new ContextLayer(this, ruleId, local);
for (const definition of local.values()) {
this.bind(definition, layer, []);
}
return layer;
}
/**
* The reader of a name at a place of a rule: a tag keyword, a context name,
* or (in `attributes` only) a value of the display set. A field reads a
* result field of a group.
*
* In `sortBy` and `attributes`, a bare name reads the value of the series,
* and `displaySet.<name>` (or `displaySet.<name>.<field>`) reads the value
* over the instances of the display set. Every other place rejects the
* prefix.
*/
readerAt(
layer: ContextLayer,
name: string,
site: SchemaSite,
options: { field?: string; place?: string } = {}
): ContextReader {
const place = options.place ?? '';
let { field } = options;
let target = name;
const prefixed = name === 'displaySet' || name.startsWith('displaySet.');
Iif (prefixed) {
if (!DISPLAY_SET_PLACES.has(place)) {
invalidAt(site, DISPLAY_SET_PREFIX_ONLY);
}
const path = name.split('.').slice(1);
if (field !== undefined) {
path.push(field);
}
if (!path.length || path.length > 2 || path.some((part) => !part)) {
invalidAt(
site,
`'${name}' must be displaySet.<name> or displaySet.<name>.<field>, e.g. displaySet.sliceSpacing`
);
}
[target, field] = path;
}
let dependency = this.dependencyOf(
{ name: target, field, inAggregate: false },
undefined,
layer,
[],
site,
place
);
if (!prefixed && DISPLAY_SET_PLACES.has(place)) {
dependency = { kind: 'series', inner: dependency };
}
return (runtime, instance, extras) => {
const value = runtime.read(target, dependency, instance, extras);
return field === undefined ? value : readOwn(value, field);
};
}
/**
* An expression of `attributes`, read over the instances of a display set.
* A tag keyword reads the first instance. `series.instances.*(...)` runs
* over the instances of the series, and `displaySet.instances.*(...)` over
* the instances of the display set. A bare context name reads the series,
* and `displaySet.<name>` the display set.
*/
attributeExpression(
layer: ContextLayer,
source: string,
site: SchemaSite
): (
runtime: ContextRuntime,
instances: readonly NaturalizedInstance[],
extras: Extras
) => unknown {
const { evaluate, reads } = this.parseExpression(
source,
site,
ATTRIBUTE_EXPRESSION_SCOPE
);
const deps = new Map<string, Dependency>();
const displaySetFields = new Map<string, Dependency>();
for (const read of reads) {
Iif (read.name === 'displaySet') {
if (read.field === undefined) {
invalidAt(
site,
"'displaySet' needs a name, e.g. displaySet.sliceSpacing"
);
}
displaySetFields.set(
read.field,
this.dependencyOf(
{ name: read.field, inAggregate: read.inAggregate },
undefined,
layer,
[],
site,
'attributes'
)
);
continue;
}
const dependency = this.dependencyOf(
read,
undefined,
layer,
[],
site,
'attributes'
);
deps.set(read.name, { kind: 'series', inner: dependency });
}
Iif (displaySetFields.size) {
deps.set('displaySet', { kind: 'displaySet', fields: displaySetFields });
}
const perDisplaySet =
displaySetFields.size > 0 ||
[...deps.values()].some(
(dependency) =>
dependency.kind === 'series' && dependency.inner.kind === 'attribute'
);
const scope = { key: `#attribute:${site.path}`, deps, perDisplaySet };
return (runtime, instances, extras) =>
evaluate(
runtime.env(scope, instances[0], extras),
// With no attribute-only name, the series environments need no extras.
perDisplaySet
? runtime.envsOf(scope, runtime.seriesInstances, extras)
: runtime.envsOf(scope),
runtime.envsOf(scope, instances, extras)
);
}
/**
* Binds one entry into the context of a rule: resolves its names, infers
* its scope, finds its cache key, and builds its computation. A loop of
* entries is a compile error.
*/
bind(
definition: Definition,
layer: ContextLayer,
stack: Definition[]
): Bound {
const memo = layer.memo.get(definition);
if (memo) {
return memo;
}
const loopStart = stack.indexOf(definition);
Iif (loopStart !== -1) {
const loop = [...stack.slice(loopStart), definition]
.map(({ name }) => name)
.join(' -> ');
return invalidAt(
definition.site,
`the context entries form a loop: ${loop}`
);
}
stack.push(definition);
try {
const deps = new Map<string, Dependency>();
const closure = new Set<string>();
if (definition.ruleId !== undefined) {
closure.add(definition.name);
}
Iif (definition.kind === 'code') {
// A code entry can read any name: every replacement can reach it.
layer.local.forEach((_entry, name) => closure.add(name));
}
let scope: Scope =
definition.kind === 'declaration' || definition.kind === 'code'
? 'instance'
: 'series';
for (const read of definition.reads) {
const dependency = this.dependencyOf(
read,
definition,
layer,
stack,
definition.site,
'context'
);
deps.set(read.name, dependency);
if (dependency.kind === 'entry') {
dependency.bound.closure.forEach((name) => closure.add(name));
}
if (!read.inAggregate && readsInstance(read, dependency)) {
scope = 'instance';
}
}
if (definition.kind === 'group') {
for (const argument of definition.args) {
const instanceRead = argument.reads.find(
(read) =>
!read.inAggregate && readsInstance(read, deps.get(read.name))
);
Iif (instanceRead) {
invalidAt(
at(at(definition.site, 'args'), argument.name),
`the argument must have one value for the series, but '${instanceRead.name}' has one value for each instance`
);
}
}
scope = 'series';
}
const replaced = [...closure].sort();
// `@` keeps a rule entry apart from the global entry of the same name.
const level = definition.ruleId === undefined ? '' : '@';
const key = replaced.length
? `${level}${definition.name}{${replaced
.map((name) => `${name}=${layer.local.get(name)?.identity}`)
.join(',')}}`
: `${level}${definition.name}`;
let bound = this.bounds.get(key);
if (!bound) {
bound = this.createBound(definition, layer, key, scope, deps, closure);
this.bounds.set(key, bound);
}
layer.memo.set(definition, bound);
return bound;
} finally {
stack.pop();
}
}
private dependencyOf(
read: ExpressionRead,
from: Definition | undefined,
layer: ContextLayer,
stack: Definition[],
site: SchemaSite,
place: string
): Dependency {
const { name, field } = read;
Iif (this.denied.has(name)) {
return invalidAt(
site,
`the name '${name}' is on the deny list of the host, so no rule can read it`
);
}
Iif (name === 'displaySet') {
return invalidAt(site, DISPLAY_SET_PREFIX_ONLY);
}
if (isTagKeyword(name)) {
Iif (field !== undefined) {
return invalidAt(
site,
`'${name}' is a DICOM keyword and has no field '${field}'; read an item of a sequence with an index, e.g. ${name}[0].${field}`
);
}
return { kind: 'keyword' };
}
const definition = layer.resolve(name, from);
Iif (!definition) {
if ((ATTRIBUTE_ONLY_NAMES as readonly string[]).includes(name)) {
if (place === 'attributes') {
return { kind: 'attribute' };
}
return invalidAt(
site,
`'${name}' is a value of the display set, and only attributes can read it`
);
}
if (from && from.ruleId !== undefined && from.name === name) {
return invalidAt(
site,
`the rule entry '${name}' reads its own name, but the global context has no entry '${name}'`
);
}
return invalidAt(
site,
unknownName(name, place, [
...layer.local.keys(),
...this.global.keys(),
...ATTRIBUTE_ONLY_NAMES,
])
);
}
const bound = this.bind(definition, layer, stack);
Iif (read.indexed && bound.group) {
return invalidAt(
site,
`read a field of the group '${name}' by path, e.g. ${name}.${Object.keys(bound.group.fields ?? {})[0] ?? 'field'}`
);
}
if (field !== undefined) {
Iif (!bound.group) {
return invalidAt(
site,
`'${name}' is not a group, so '${name}.${field}' reads nothing`
);
}
const { fields } = bound.group;
Iif (fields && !hasOwn(fields, field)) {
return invalidAt(
site,
`the group '${name}' has no field '${field}'${didYouMean(field, Object.keys(fields))}; fields: ${Object.keys(fields).join(', ')}`
);
}
}
return { kind: 'entry', bound };
}
private createBound(
definition: Definition,
layer: ContextLayer,
key: string,
scope: Scope,
deps: Map<string, Dependency>,
closure: ReadonlySet<string>
): Bound {
const bound: Bound = {
key,
name: definition.name,
scope,
deps,
closure,
ruleId: definition.ruleId,
// A code entry resolves its names at run time, so it can read anything.
perDisplaySet:
definition.kind === 'code' ||
definition.kind === 'group' ||
[...deps.values()].some(
(dependency) =>
dependency.kind === 'entry' && dependency.bound.perDisplaySet
),
compute: () => undefined,
};
switch (definition.kind) {
case 'fixed': {
const { value } = definition;
bound.compute = () => value;
break;
}
case 'declaration': {
const { attribute } = definition;
bound.compute = (_runtime, instance) => readOwn(instance, attribute);
break;
}
case 'code': {
const { fn } = definition;
bound.layer = layer;
bound.definition = definition;
// A new values wrapper for each call.
bound.compute = (runtime, instance) =>
instance
? fn(
instance,
runtime.env(bound, instance, undefined, true) as ContextValues
)
: undefined;
break;
}
case 'expression': {
const target =
definition.aliasOf !== undefined
? deps.get(definition.aliasOf)
: undefined;
Iif (target?.kind === 'entry' && target.bound.group) {
// An alias of a group is the group: same result, same fields.
const group = target.bound;
bound.group = group.group;
bound.scope = 'series';
bound.compute = (runtime) => runtime.value(group, undefined);
break;
}
const { evaluate } = definition;
bound.compute = (runtime, instance) =>
evaluate(runtime.env(bound, instance), runtime.envsOf(bound));
break;
}
case 'template': {
const parts = definition.segments.map((segment) =>
'literal' in segment
? () => segment.literal
: (runtime: ContextRuntime, instance?: NaturalizedInstance) => {
const value = runtime.read(
segment.name,
deps.get(segment.name),
instance
);
return segment.field === undefined
? value
: readOwn(value, segment.field);
}
);
bound.compute = (runtime, instance) =>
parts
.map((part) => {
const value = part(runtime, instance);
return hasNoValue(value) ? '' : String(value);
})
.join('');
break;
}
case 'group': {
const { compute, args } = definition;
bound.group = { fields: definition.fields };
bound.compute = (runtime) => {
const values: Record<string, unknown> = {};
for (const argument of args) {
values[argument.name] = argument.evaluate
? argument.evaluate(
runtime.env(bound, undefined),
runtime.envsOf(bound)
)
: argument.fixed;
}
// In the runtime of a display set, `instances` are the display set.
return compute(runtime.instances as NaturalizedInstance[], {
args: Object.freeze(values),
});
};
break;
}
}
return bound;
}
/** Parses the entries of one layer, and checks each name and form. */
private parseLayer(
context: unknown,
site: SchemaSite,
ruleId: string | undefined
): Definition[] {
Iif (!context || typeof context !== 'object' || Array.isArray(context)) {
return invalidAt(
site,
'expected an object of context entries, keyed by context name',
context
);
}
return Object.entries(context).map(([name, raw]) => {
const entrySite = at(site, name);
checkContextName(name, entrySite);
Iif (this.denied.has(name)) {
invalidAt(
entrySite,
`the context name '${name}' is on the deny list of the host`
);
}
return this.parseEntry(name, raw, entrySite, ruleId);
});
}
private parseEntry(
name: string,
raw: unknown,
site: SchemaSite,
ruleId: string | undefined
): Definition {
const base = { name, ruleId, site, identity: identityOf(raw) };
Iif (typeof raw === 'function') {
return {
...base,
kind: 'code',
reads: [],
fn: raw as InstanceContextFunction,
};
}
if (isFixedValue(raw)) {
return { ...base, kind: 'fixed', reads: [], value: copyFixedValue(raw) };
}
if (typeof raw === 'string') {
const { evaluate, reads, single } = this.parseExpression(raw, site);
return { ...base, kind: 'expression', reads, evaluate, aliasOf: single };
}
Iif (!raw || typeof raw !== 'object') {
return invalidAt(
site,
'expected a fixed value (a number, a boolean, null or an array), an expression string, { attribute }, { template }, { function, args? }, or a function',
raw
);
}
const [form] = matchForm(splitRuleSchema.contextEntry, raw, site);
const fields = raw as Record<string, unknown>;
if (form === 'declaration') {
Iif (!fields.attribute) {
return invalidAt(at(site, 'attribute'), 'expected a non-empty string');
}
const attribute = String(fields.attribute);
Iif (
this.denied.has(attribute) ||
this.deniedTags.has(attribute.replace(/^x/i, '').toUpperCase())
) {
return invalidAt(
at(site, 'attribute'),
`the attribute '${attribute}' is on the deny list of the host, so no rule can read it`
);
}
return {
...base,
kind: 'declaration',
reads: [],
attribute: fields.attribute as string,
vr: fields.vr as string | undefined,
vm: fields.vm as string | undefined,
};
}
Iif (form === 'template') {
const segments = parseTemplate(
fields.template as string,
at(site, 'template')
);
const reads = segments.flatMap((segment) =>
'literal' in segment
? []
: [{ name: segment.name, field: segment.field, inAggregate: false }]
);
return { ...base, kind: 'template', reads, segments };
}
const definition = this.seriesFunctionOf(
fields.function,
at(site, 'function')
);
const args = this.parseArguments(fields.args, at(site, 'args'));
return {
...base,
kind: 'group',
reads: args.flatMap((argument) => argument.reads),
compute: definition.compute,
fields: definition.fields,
args,
};
}
private seriesFunctionOf(
value: unknown,
site: SchemaSite
): SeriesFunctionDefinition {
Iif (typeof value === 'function') {
return { compute: value as SeriesFunction };
}
Iif (
value &&
typeof value === 'object' &&
typeof (value as SeriesFunctionDefinition).compute === 'function'
) {
return value as SeriesFunctionDefinition;
}
Eif (typeof value === 'string') {
const registered = readOwn(this.seriesFunctions, value);
Iif (typeof registered === 'function') {
return { compute: registered as SeriesFunction };
}
Eif (registered && typeof registered === 'object') {
return registered as SeriesFunctionDefinition;
}
}
return invalidAt(
site,
`unknown series function ${JSON.stringify(value)}; known: ${Object.keys(this.seriesFunctions).join(', ') || '(none)'}`
);
}
private parseArguments(args: unknown, site: SchemaSite): GroupArgument[] {
if (args === undefined) {
return [];
}
Iif (!args || typeof args !== 'object' || Array.isArray(args)) {
return invalidAt(site, 'expected an object of arguments', args);
}
return Object.entries(args).map(([name, raw]) => {
const argumentSite = at(site, name);
Iif (isUnsafeKey(name)) {
invalidAt(argumentSite, `the argument name "${name}" is not allowed`);
}
if (isFixedValue(raw)) {
return { name, fixed: copyFixedValue(raw), reads: [] };
}
Eif (typeof raw === 'string') {
const { evaluate, reads } = this.parseExpression(raw, argumentSite);
return { name, evaluate, reads };
}
return invalidAt(
argumentSite,
'an argument is a fixed value (a number, a boolean, null or an array) or an expression string',
raw
);
});
}
}
/** True when a read gives one value for each instance. */
function readsInstance(
read: ExpressionRead,
dependency: Dependency | undefined
): boolean {
Iif (!dependency) {
return false;
}
switch (dependency.kind) {
case 'keyword':
return true;
case 'attribute':
case 'displaySet':
return false;
case 'series':
return readsInstance(read, dependency.inner);
case 'entry': {
const { bound } = dependency;
if (read.field !== undefined) {
return bound.group?.fields?.[read.field] === 'instance';
}
return bound.scope === 'instance';
}
}
}
|