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
|
#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2021-07-30 19:52:52 +0300 (Fri, 30 Jul 2021) $
#$Revision: 8840 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.7.0/scripts/cif_ddl1_dic_check $
#------------------------------------------------------------------------------
#*
#* Check DDL1 dictionaries against a set of best practice rules.
#*
#* USAGE:
#* $0 --options cif_core.dic
#**
use strict;
use warnings;
binmode STDOUT, ':encoding(UTF-8)';
binmode STDERR, ':encoding(UTF-8)';
use List::MoreUtils qw( any uniq );
use COD::CIF::Parser qw( parse_cif );
use COD::CIF::DDL::DDL1 qw( classify_dic_blocks
convert_pseudo_data_name_to_category_name
get_category_name
get_data_type
get_data_name
get_data_names
get_enumeration_defaults
get_list_constraint_type );
use COD::SOptions qw( getOptions get_value );
use COD::SUsage qw( usage options );
use COD::ErrorHandler qw( process_parser_messages );
use COD::ToolsVersion qw( get_version_string );
##
# Checks if all of the provided data blocks have a unique data block code.
#
# @source [1]
# 2.2.7.1.4. General features,
# "International Tables for Crystallography Volume G:
# Definition and exchange of crystallographic data",
# 2005, 25-26, paragraph (6), doi: 10.1107/97809553602060000107
#
# @param $data_blocks
# Reference to an array of data block as returned by
# the COD::CIF::Parser.
# @return
# Array reference to a list of validation messages.
##
sub check_data_block_code_uniqueness
{
my ( $data_blocks ) = @_;
my %block_code_frequency;
for my $data_block ( @{$data_blocks} ) {
my $block_code = $data_block->{'name'};
push @{$block_code_frequency{lc $block_code}}, $block_code;
}
my @notes;
for my $block_code ( sort keys %block_code_frequency ) {
my $count = @{$block_code_frequency{$block_code}};
next if $count < 2;
push @notes,
"data block code is not unique -- code '$block_code' " .
"appears $count times as [" .
( join ', ', map {"'$_'"} @{$block_code_frequency{$block_code}} ) .
']'
}
return \@notes;
}
##
# Checks if all data names given in the provided data blocks are unique.
#
# @param $data_blocks
# Reference to an array of data block as returned by
# the COD::CIF::Parser.
# @return
# Array reference to a list of validation messages.
##
sub check_data_name_uniqueness
{
my ( $data_blocks ) = @_;
my @notes;
my %data_name_to_block_codes;
for my $data_block ( @{$data_blocks} ) {
my $data_names = get_data_names( $data_block );
next if !defined $data_names;
$data_names = [ map {lc} @{$data_names} ];
for my $data_name ( sort { $a cmp $b } uniq @{$data_names} ) {
push @{$data_name_to_block_codes{$data_name}}, $data_block->{'name'};
}
}
for my $data_name ( sort keys %data_name_to_block_codes ) {
my $block_codes = $data_name_to_block_codes{$data_name};
next if @{$block_codes} < 2;
push @notes,
"data name is not unique -- data name '$data_name' is defined " .
'by data blocks [' .
( join ', ', map { "'data_$_'"} @{$block_codes} )
. ']';
}
return \@notes;
}
##
# Checks if certain looped data items only contain unique data values.
#
# @param $data_blocks
# Reference to an array of data block as returned by
# the COD::CIF::Parser.
# @return
# Array reference to a list of validation messages.
##
sub check_data_value_uniqueness
{
my ( $data_block ) = @_;
my @looped_item_names = qw (
_enumeration
_list_link_child
_list_link_parent
_list_reference
_list_uniqueness
_name
_related_item
_type_conditions
);
my @notes;
for my $name ( @looped_item_names ) {
next if !exists $data_block->{'values'}{$name};
my %value_counts;
for my $value ( @{$data_block->{'values'}{$name}} ) {
$value_counts{$value}++;
}
for my $value ( sort keys %value_counts ) {
next if $value_counts{$value} == 1;
push @notes,
"data item '$name' value '$value' is needlessly repeated " .
"$value_counts{$value} times";
}
}
return \@notes;
}
##
# Checks if data items that belong to the same category all share the same
# looped list constraints.
#
# @reference
# Based on an e-mail received from Brian McMahon on 2016-04-16.
#
# DDL1 defines data items as having one of three states in regards to looped
# lists. These states are expressed as enumeration values ('yes', 'no', both')
# and recorded using the '_list' data item. Since this looped list constraint
# is individually assigned to each data item, data items from the same category
# may end up with differing states. Although this is not prohibited, it is
# advised against since categories with mixed looped list constraints do no
# map well to the relational model.
#
# @param $dic
# DDL1 dictionary that contains the definition of the checked data item.
# Passed as a reference to a data structure returned by the
# build_dic_struct() subroutine.
# @return
# Array reference to a list of validation messages.
##
sub check_list_constraint_uniformity
{
my ( $dic ) = @_;
my %category_items;
for my $item ( @{ get_item_blocks( $dic ) } ) {
my $category_name = get_category_name( $item );
next if !defined $category_name;
push @{ $category_items{$category_name} }, $item;
}
my %category_to_list_groupings;
for my $category_name ( sort keys %category_items ) {
my %list_constraint_groups;
my $category_items = $category_items{$category_name};
for my $item ( @{$category_items} ) {
my $data_name = get_data_name( $item );
next if !defined $data_name;
my $list_constraint = get_list_constraint_type( $item );
push @{ $list_constraint_groups{$list_constraint} }, $data_name;
}
$category_to_list_groupings{$category_name} = \%list_constraint_groups;
}
my @notes;
my $max_item_count = 2;
# TODO: data items from external dictionaries that share the same
# category should also be eventually considered
for my $category_name ( sort keys %category_to_list_groupings ) {
my $list_constraint_groups = $category_to_list_groupings{$category_name};
next if keys %{$list_constraint_groups} < 2;
push @notes,
report_internal_list_constraint_inconsistensies(
$category_name,
$list_constraint_groups,
$max_item_count
)
}
return \@notes;
}
sub report_internal_list_constraint_inconsistensies
{
my ( $category_name, $list_constraint_groups, $max_item_count ) = @_;
my @group_strings;
for my $constraint ( sort keys %{$list_constraint_groups} ) {
my @item_names = @{$list_constraint_groups->{$constraint}};
my $item_count = scalar @item_names;
my $group_string =
"'$constraint' ($item_count item" .
( $item_count > 1 ? 's' : '' ) . ', ';
my $name_list_string;
if ( $item_count > $max_item_count ) {
$name_list_string =
join ', ', map { "'$_'" }
@item_names[0..($max_item_count-1)];
$name_list_string .= ', ...';
} else {
$name_list_string =
join ', ', map { "'$_'" } @item_names;
}
$group_string .= "[$name_list_string])";
push @group_strings, $group_string;
}
my $message = "data items that belong to the '$category_name' " .
'category have differing looped list constraints -- ' .
( join ', ', @group_strings ) ;
return $message;
}
sub check_list_item_compatability
{
my ( $data_item ) = @_;
my $list_constraint_type = get_list_constraint_type( $data_item );
my @list_only_data_names = qw(
_list_mandatory
_list_reference
_list_uniqueness
);
my @notes;
if ( $list_constraint_type eq 'no' ) {
for my $data_name ( @list_only_data_names ) {
next if !exists $data_item->{'values'}{$data_name};
push @notes,
"data item '$data_name' should only be used in the " .
'definitions of data items that are permitted to appear in ' .
'a looped list';
}
}
if ( exists $data_item->{'values'}{'_list_uniqueness'} ) {
if ( get_list_mandatory( $data_item ) ne 'yes' ) {
push @notes,
"data item '_list_uniqueness' must only appear in the " .
"definitions of data items that have the '_list_mandatory' " .
"data value set to 'yes'";
}
}
return \@notes;
}
##
# Checks the validity of child data item references.
#
# @param $data_item
# Reference to an item definition data block as returned by
# the COD::CIF::Parser.
# @param $dic
# DDL1 dictionary that contains the definition of the checked data item.
# Passed as a reference to a data structure returned by the
# build_dic_struct() subroutine.
# @return
# Array reference to a list of validation messages.
##
sub check_list_link_child
{
my ( $data_item, $dic ) = @_;
my $child_item_names = get_child_item_names( $data_item );
return [] if !defined $child_item_names;
my @notes;
for my $child_item_name ( @{$child_item_names} ) {
my $data_names = get_data_names( $data_item );
if ( defined $data_names ) {
if ( any { $child_item_name eq $_ } @{$data_names} ) {
push @notes,
'data item references itself as its child data item';
next;
}
}
my $child_block = get_item_block_by_name( $dic, $child_item_name );
if ( defined $child_block ) {
my $child_type = get_data_type( $child_block );
my $parent_type = get_data_type( $data_item );
if ( defined $child_type && defined $parent_type &&
$child_type ne $parent_type ) {
push @notes,
'data types of the given data item and the referenced ' .
"child data item '$child_item_name' do not match " .
"('$parent_type' vs. '$child_type')";
}
} else {
push @notes,
"definition of the child data item '$child_item_name' " .
'could not be located';
}
}
return \@notes;
}
##
# Checks the validity of parent data item references.
#
# @param $data_item
# Reference to an item definition data block as returned by
# the COD::CIF::Parser.
# @param $dic
# DDL1 dictionary that contains the definition of the checked data item.
# Passed as a reference to a data structure returned by the
# build_dic_struct() subroutine.
# @param $extra_dics
# A set of DDL1 dictionaries that are additionally checked in case
# the parent data item definition cannot be located in the $dic
# dictionary. Passed as a reference to a hash of data structures
# returned by the build_dic_struct() subroutine.
# @return
# Array reference to a list of validation messages.
##
sub check_list_link_parent
{
my ( $data_item, $dic, $extra_dics ) = @_;
my $parent_item_name = get_parent_item_name( $data_item );
return [] if !defined $parent_item_name;
my @notes;
my $data_names = get_data_names( $data_item );
if ( defined $data_names ) {
if ( any { $parent_item_name eq $_ } @{$data_names} ) {
push @notes,
'data item references itself as its parent data item';
return \@notes;
}
}
my $parent_block = get_item_block_by_name( $dic, $parent_item_name );
for my $key ( keys %{$extra_dics} ) {
last if defined $parent_block;
my $extra_dic = $extra_dics->{$key};
$parent_block = get_item_block_by_name( $extra_dic, $parent_item_name );
}
if ( defined $parent_block ) {
my $child_type = get_data_type( $data_item );
my $parent_type = get_data_type( $parent_block );
if ( defined $child_type && defined $parent_type &&
$child_type ne $parent_type ) {
push @notes,
'data types of the given data item and the referenced ' .
"parent data item '$parent_item_name' do not match " .
"('$child_type' vs. '$parent_type')";
}
} else {
push @notes,
'definition of the referenced parent data item ' .
"'$parent_item_name' could not be located";
}
return \@notes;
}
##
# Checks if references to parent and child data items are internally
# consistent in the scope of a single dictionary. References to parent
# and child data items from other dictionaries are silently ignored.
#
# DDL1 defines parent-child relationships using two separate data items.
# The '_list_link_parent' data item is used to specify the parent item,
# whereas the '_list_link_child' data item is used to list the children
# of the data item. Due to this, it is possible to create one-sided
# parent-child definitions, e.g., provide a reference from the child
# item to the parent item, but omit the one from the parent item to
# the child item.
#
# This subroutine detects the following inconsistencies:
# * Missing references to parent data items;
# * Missing references to child data items;
# * Child data items that are reference by more than one parent data item.
#
# Restrictions:
# * References are only checked in the scope of a single dictionary;
# * Missing data item definitions are silently skipped since detection
# of such anomalies is implemented in the 'check_list_link_parent'
# and 'check_list_link_child' subroutines.
#
# @param $dic
# DDL1 dictionary that contains the definition of the checked data item.
# Passed as a reference to a data structure returned by the
# build_dic_struct() subroutine.
# @return
# Array reference to a list of validation messages.
##
sub check_child_parent_reciprocity
{
my ( $dic ) = @_;
my %child_to_parent;
my %parent_to_children;
for my $data_item ( @{ get_item_blocks( $dic ) } ) {
my $data_names = get_data_names( $data_item );
next if !defined $data_names;
my $parent_item_name = get_parent_item_name( $data_item );
if ( defined $parent_item_name ) {
for my $data_name ( @{ $data_names } ) {
$child_to_parent{$data_name} = $parent_item_name;
}
}
my $child_item_names = get_child_item_names( $data_item );
if ( defined $child_item_names ) {
for my $data_name ( @{ $data_names } ) {
$parent_to_children{$data_name} = $child_item_names;
}
}
}
my @notes;
for my $child_item_name ( sort keys %child_to_parent ) {
my $parent_item_name = $child_to_parent{$child_item_name};
my $parent_item = get_item_block_by_name( $dic, $parent_item_name );
next if !defined $parent_item;
if ( !exists $parent_to_children{$parent_item_name} ||
!any { $_ eq $child_item_name } @{$parent_to_children{$parent_item_name}} ) {
push @notes,
"data item '$child_item_name' references data item " .
"'$parent_item_name' as its parent, but a reciprocal " .
'parent-child reference is not explicitly provided';
}
}
for my $parent_item_name ( sort keys %parent_to_children ) {
my $child_item_names = $parent_to_children{$parent_item_name};
for my $child_item_name ( @{$child_item_names} ) {
my $child_item = get_item_block_by_name( $dic, $child_item_name );
next if !defined $child_item;
if ( !exists $child_to_parent{$child_item_name} ||
$child_to_parent{$child_item_name} ne $parent_item_name ) {
push @notes,
"data item '$parent_item_name' references data item " .
"'$child_item_name' as its child, but a reciprocal " .
'child-parent reference is not explicitly provided';
}
}
}
my %implicit_child_to_parent;
for my $parent_item_name ( sort keys %parent_to_children ) {
my $child_item_names = $parent_to_children{$parent_item_name};
for my $child_item_name ( @{$child_item_names} ) {
push @{$implicit_child_to_parent{$child_item_name}}, $parent_item_name;
}
}
for my $child_item_name ( sort keys %implicit_child_to_parent ) {
my $implicit_parent_names = $implicit_child_to_parent{$child_item_name};
if ( @{$implicit_parent_names} > 1 ) {
push @notes,
"data item '$child_item_name' is referenced as a child " .
'item by data items [' .
( join ', ', map { "'$_'" } @{$implicit_parent_names} ) .
'] -- data item should only have a single parent data item';
}
}
return \@notes;
}
##
# Checks the validity of a looped list item reference.
#
# @param $data_item
# Reference to an item definition data block as returned by
# the COD::CIF::Parser.
# @param $dic
# DDL1 dictionary that contains the definition of the checked data item.
# Passed as a reference to a data structure returned by the
# build_dic_struct() subroutine.
# @param $extra_dics
# A set of DDL1 dictionaries that are additionally checked in case
# the parent data item definition cannot be located in the $dic
# dictionary. Passed as a reference to a hash of data structures
# returned by the build_dic_struct() subroutine.
# @return
# Array reference to a list of validation messages.
##
sub check_list_reference
{
my ( $data_item, $dic, $extra_dics ) = @_;
my $list_references = get_list_references( $data_item );
return [] if !defined $list_references;
my @notes;
for my $list_reference ( @{$list_references} ) {
my $reference_block = get_list_reference_block( $dic, $list_reference );
for my $key ( keys %{$extra_dics} ) {
last if defined $reference_block;
my $extra_dic = $extra_dics->{$key};
$reference_block = get_list_reference_block( $extra_dic, $list_reference );
}
if ( !defined $reference_block ) {
push @notes, 'definition of a list reference data item ' .
"'$list_reference' could not be located";
} else {
# rely on the hash references to check data block identity
if ( $data_item eq $reference_block && @{$list_references} == 1 ) {
push @notes,
'data item needlessly references itself as ' .
'a list reference data item';
}
if ( get_list_constraint_type( $reference_block ) eq 'no' ) {
push @notes,
'the declared list reference data item ' .
"'$list_reference' is not permitted to appear " .
'in a looped list';
}
my $item_category = get_category_name( $data_item );
next if !defined $item_category;
my $reference_category = get_category_name( $reference_block );
next if !defined $reference_category;
if ( $item_category ne $reference_category ) {
push @notes,
'categories of the given data item and the declared ' .
"list reference data item '$list_reference' do not " .
"match ('$item_category' vs. '$reference_category')";
}
}
}
return \@notes;
}
sub check_related_item
{
my ( $data_item, $dic, $extra_dics ) = @_;
my $related_items = get_related_items( $data_item );
return [] if !defined $related_items;
my @notes;
for my $related_item ( @{$related_items} ) {
my $related_block = get_list_reference_block( $dic, $related_item );
for my $key ( keys %{$extra_dics} ) {
last if defined $related_block;
my $extra_dic = $extra_dics->{$key};
$related_block = get_list_reference_block( $extra_dic, $related_item );
}
if ( !defined $related_block ) {
push @notes, 'definition of a related data item ' .
"'$related_item' could not be located";
next;
}
# rely on the hash references to check data block identity
if ( $data_item eq $related_block ) {
push @notes,
'data item references itself as a related data item';
};
}
return \@notes;
}
sub check_list_uniqueness
{
my ( $data_item, $dic, $extra_dics ) = @_;
my $list_uniqueness = get_list_uniqueness( $data_item );
return [] if !defined $list_uniqueness;
my @notes;
for my $list_unique_item ( @{$list_uniqueness} ) {
my $unique_block = get_item_block_by_name( $dic, $list_unique_item );
for my $key ( keys %{$extra_dics} ) {
last if defined $unique_block;
my $extra_dic = $extra_dics->{$key};
$unique_block = get_item_block_by_name( $extra_dic, $list_unique_item );
}
if ( !defined $unique_block ) {
push @notes, 'definition of a list unique data item ' .
"'$list_unique_item' could not be located";
} else {
if ( get_list_constraint_type( $unique_block ) eq 'no' ) {
push @notes,
'the declared list unique data item ' .
"'$list_unique_item' is not permitted to appear " .
'in a looped list';
}
my $item_cat = get_category_name( $data_item );
next if !defined $item_cat;
my $list_unique_cat = get_category_name( $unique_block );
next if !defined $list_unique_cat;
if ( $item_cat ne $list_unique_cat ) {
push @notes,
'categories of the given data item and the declared ' .
"list unique data item '$list_unique_item' do not " .
"match ('$item_cat' vs. '$list_unique_cat')";
}
}
}
return \@notes;
}
sub check_simultaneous_item_presence
{
my ( $data_block ) = @_;
my %item_pairs = (
'_enumeration_detail' => '_enumeration',
'_example_detail' => '_example',
'_related_function' => '_related_item',
'_related_item' => '_related_function',
'_units_detail' => '_units',
);
my @notes;
for my $sub_item ( sort keys %item_pairs ) {
my $main_item = $item_pairs{$sub_item};
next if !exists $data_block->{'values'}{$sub_item};
if ( !exists $data_block->{'values'}{$main_item} ) {
push @notes,
"data item '$main_item' should be accompanied by the " .
"'$sub_item' data item";
}
}
return \@notes;
}
sub check_data_item_name_syntax
{
my ( $data_block ) = @_;
my $data_names = get_data_names( $data_block );
return [] if !defined $data_names;
my @notes;
for my $data_name ( @{$data_names} ) {
if ( $data_name !~ m/^_/ ) {
push @notes,
"data item name '$data_name' should start with an " .
"underscore symbol ('_')";
}
}
return \@notes;
}
sub check_type_conditions
{
my ( $data_item ) = @_;
my $data_type = get_data_type( $data_item );
return [] if !defined $data_type;
my @notes;
my $type_conditions = get_type_conditions( $data_item );
if ( @{$type_conditions} > 1 &&
any { $_ eq 'none' } @{$type_conditions} ) {
push @notes,
"the 'none' type condition should not be provided alongside other " .
'type conditions';
}
return \@notes if $data_type eq 'numb';
for my $condition ( @{$type_conditions} ) {
if ( $condition eq 'esd' || $condition eq 'su' ) {
push @notes,
"type condition '$condition' is not compatible with the " .
"declared data type '$data_type' -- standard uncertainty " .
'values can only be applied to data items of the numeric ' .
"'numb' type";
last;
}
}
return \@notes;
}
sub check_category_population
{
my ( $category_block, $data_items ) = @_;
my $data_name = get_data_name( $category_block );
return [] if !defined $data_name;
$data_name = convert_pseudo_data_name_to_category_name( $data_name );
my $has_related_items = 0;
for my $data_item ( @{$data_items} ) {
my $category_name = get_category_name( $data_item );
next if !defined $category_name;
if ( $data_name eq $category_name ) {
$has_related_items = 1;
last;
}
}
my @notes;
if ( !$has_related_items ) {
push @notes,
"category '$data_name' does not have any related " .
'data item definitions';
}
return \@notes;
}
##
# Checks the validity of a data item category reference.
#
# @param $data_item
# Reference to an item definition data block as returned by
# the COD::CIF::Parser.
# @param $dic
# DDL1 dictionary that contains the definition of the checked data item.
# Passed as a reference to a data structure returned by the
# build_dic_struct() subroutine.
# @param $extra_dics
# A set of DDL1 dictionaries that are additionally checked in case
# the parent data item definition cannot be located in the $dic
# dictionary. Passed as a reference to a hash of data structures
# returned by the build_dic_struct() subroutine.
# @return
# Array reference to a list of validation messages.
##
sub check_category_references
{
my ( $data_block, $dic, $extra_dics ) = @_;
my $cat_name = get_category_name( $data_block );
return [] if !defined $cat_name;
my $cat_found = defined get_category_block_by_name( $dic, $cat_name );
for my $key ( keys %{$extra_dics} ) {
last if $cat_found;
my $extra_dic = $extra_dics->{$key};
$cat_found = defined get_category_block_by_name( $extra_dic, $cat_name );
}
my @notes;
if ( !$cat_found ) {
push @notes,
"definition of the parent category '$cat_name' could " .
'not be located';
}
return \@notes;
}
##
# Evaluates if the data name adheres to the category naming convention.
#
# @source [1]
# https://www.iucr.org/resources/cif/dictionaries/cif_core/diffs2.0-1.0
#
# @param $data_name
# Name of the category.
# @return
# 1 is the data name is a proper category name,
# 0 otherwise
##
sub is_proper_category_name
{
my ( $data_name ) = @_;
return $data_name =~ m/_\[[^\]]*\]$/;
}
sub check_metadata_block_multiplicity
{
my ( $dic ) = @_;
my $metadata_blocks = get_dictionary_blocks( $dic );
my @notes;
if ( !@{$metadata_blocks} ) {
push @notes,
'dictionary metadata data block could not be located -- ' .
'the data block is conventionally assigned the ' .
'\'on_this_dictionary\' block code';
return \@notes;
}
if ( @{$metadata_blocks} > 1 ) {
push @notes,
'more than one dictionary metadata data block located -- ' .
( scalar @{$metadata_blocks} ) . ' data blocks are assigned the ' .
'\'on_this_dictionary\' block code';
return \@notes;
}
return \@notes;
}
sub check_metadata_block
{
my ( $data_block ) = @_;
my @notes;
# name and version allows to uniquely identify the dictionary
my @mandatory_metadata_items = qw( _dictionary_name _dictionary_version );
for my $data_name ( @mandatory_metadata_items ) {
if ( !exists $data_block->{'values'}{$data_name} ) {
push @notes,
"missing mandatory data item -- metadata item '$data_name' " .
'must be provided in the metadata data block';
}
}
my @recommended_metadata_items = qw( _dictionary_update _dictionary_history );
for my $data_name ( @recommended_metadata_items ) {
if ( !exists $data_block->{'values'}{$data_name} ) {
push @notes,
"missing recommended metadata item -- data item '$data_name' " .
'should be provided in the metadata data block';
}
}
my @all_metadata_items = ( @mandatory_metadata_items,
@recommended_metadata_items );
for my $data_name ( @{$data_block->{'tags'}} ) {
if (! any { $_ eq $data_name } @all_metadata_items ) {
push @notes,
"data item '$data_name' should not appear in the metadata " .
'data block'
}
}
return \@notes;
}
##
# Checks if the category definition meets the IUCr category definition
# convention.
#
# @source [1]
# https://www.iucr.org/resources/cif/dictionaries/cif_core/diffs2.0-1.0
#
# @param $data_block
# Reference to a category definition data block returned by
# the COD::CIF::Parser.
# @return
# Array reference to a list of validation messages.
##
sub check_category_block
{
my ( $data_block ) = @_;
my @notes;
my $type = get_data_type( $data_block );
if ( !defined $type ) {
push @notes,
'improper category definition -- the \'null\' data type should ' .
'be explicitly specified using the \'_type\' data item';
}
my $category_name = get_category_name( $data_block );
if ( !defined $category_name ) {
push @notes,
'improper category definition -- the \'category_overview\' ' .
'parent category should be explicitly specified using ' .
'the \'_category\' data item';
} elsif ( $category_name ne 'category_overview' ) {
push @notes,
'improper category definition -- data item \'_category\' value ' .
"'$category_name' should be set to 'category_overview'";
}
my $data_name = get_data_name( $data_block );
if ( defined $data_name ) {
if ( !is_proper_category_name( $data_name ) ) {
push @notes,
'improper category definition -- data item \'_name\' value ' .
"'$data_name' should end with an underscore and a pair of " .
'square brackets that may optionally enclose a code for the ' .
"relevant dictionary extension, e.g. '_[]', '_[pd]'";
}
my $block_code = $data_block->{'name'};
if ( $data_name ne '_' . $block_code ) {
push @notes,
"category definition block code '$block_code' is not " .
"compatible with the provided data name '$data_name' -- " .
'the block code should be derived from the data name by ' .
"removing the leading underscore ('_') symbol";
}
}
my @mandatory_items = qw( _name );
for my $data_name ( @mandatory_items ) {
if ( !exists $data_block->{'values'}{$data_name} ) {
push @notes,
"missing mandatory data item -- data item '$data_name' ".
'must be provided in a category definition data block';
}
}
my @recommended_items = qw( _definition );
for my $data_name ( @recommended_items ) {
if ( !exists $data_block->{'values'}{$data_name} ) {
push @notes,
"missing recommended data item -- data item '$data_name' ".
'should be provided in a category definition data block';
}
}
my @category_items = ( @mandatory_items,
@recommended_items,
'_category',
'_type',
'_example',
'_example_detail' );
for my $data_name ( @{$data_block->{'tags'}} ) {
if (! any { $_ eq $data_name } @category_items ) {
push @notes,
"data item '$data_name' should not appear in a category " .
'definition data block'
}
}
return \@notes;
}
sub check_item_block
{
my ( $data_block ) = @_;
my @notes;
my @mandatory_items = qw( _name _type _category );
for my $data_name ( @mandatory_items ) {
if ( !exists $data_block->{'values'}{$data_name} ) {
push @notes,
"missing mandatory data item -- data item '$data_name' ".
'must be provided in a data item definition data block';
}
}
my @recommended_items = qw( _definition );
for my $data_name ( @recommended_items ) {
if ( !exists $data_block->{'values'}{$data_name} ) {
push @notes,
"missing recommended data item -- data item '$data_name' ".
'should be provided in a data item definition data block';
}
}
my @allowed_items = ( @mandatory_items, @recommended_items,
qw(
_enumeration
_enumeration_default
_enumeration_detail
_enumeration_range
_example
_example_detail
_list
_list_level
_list_link_child
_list_link_parent
_list_link_parent
_list_mandatory
_list_reference
_list_uniqueness
_related_function
_related_item
_type_conditions
_type_construct
_units
_units_detail
) );
for my $data_name ( @{$data_block->{'tags'}} ) {
if (! any { $_ eq $data_name } @allowed_items ) {
push @notes,
"data item '$data_name' should not appear in a data item " .
'definition data block'
}
}
return \@notes;
}
##
# Checks if the data names used in the free-text description of the data
# item are defined in the dictionary. This subroutine treats all string
# that contain underscores as data item/category names thus false warnings
# might be produced.
#
# @param $data_block
# Reference to a category definition data block returned by
# the COD::CIF::Parser.
# @param $dic
# DDL1 dictionary that contains the definition of the checked data item.
# Passed as a reference to a data structure returned by the
# build_dic_struct() subroutine.
# @return
# Array reference to a list of validation messages.
##
sub check_references_in_descriptions
{
my ( $data_block, $dic, $extra_dics ) = @_;
return [] if !exists $data_block->{'values'}{'_definition'};
my @notes;
my $description = $data_block->{'values'}{'_definition'}[0];
while ( $description =~ m/([^\s]*_[^\s]*)/g ) {
my $referenced_tag = $1;
$referenced_tag =~ s/^[({']+//;
$referenced_tag =~ s/[\n.)},']+$//;
if ( $referenced_tag =~ m/^_/ && $referenced_tag !~ m/_\[\]$/ ) {
next if defined get_item_block_by_name( $dic, $referenced_tag );
my $is_known_data_name = 0;
for my $extra_dic ( values %{$extra_dics} ) {
if ( defined get_item_block_by_name( $extra_dic, $referenced_tag ) ) {
$is_known_data_name = 1;
last;
}
}
if ( !$is_known_data_name ) {
push @notes,
'the data block description seems to be referencing ' .
"the '$referenced_tag' data item which is not defined " .
'in any of the supplied dictionaries';
}
} else {
my $lc_name = lc $referenced_tag;
next if defined get_category_block_by_lc_name( $dic, $lc_name );
next if defined get_category_block_by_lc_pseudo_name( $dic, $lc_name );
my $is_known_data_name = 0;
for my $extra_dic ( values %{$extra_dics} ) {
if ( defined get_category_block_by_lc_name( $extra_dic, $lc_name ) ||
defined get_category_block_by_lc_pseudo_name( $extra_dic, $lc_name ) ) {
$is_known_data_name = 1;
last;
}
}
if ( !$is_known_data_name ) {
push @notes,
'the data block description seems to be referencing ' .
"the '$referenced_tag' category which is not defined " .
'in any of the supplied dictionaries';
}
}
}
return \@notes;
}
##
# Subroutines relating to the DDL1 data structure.
##
sub build_dic_struct
{
my ( $data ) = @_;
my $classified_blocks = classify_dic_blocks($data);
my %dic_struct = (
'item' => [],
'category' => [],
'dictionary' => [],
'search' => {
'item' => {
'by_block_code' => {},
'by_data_name' => {},
},
'category' => {
'by_block_code' => {},
'by_name' => {},
'by_psuedo_name' => {},
'by_lc_name' => {},
'by_lc_psuedo_name' => {}
},
}
);
$dic_struct{'dictionary'} = $classified_blocks->{'dictionary'};
$dic_struct{'category'} = $classified_blocks->{'category'};
for my $category_block ( @{$dic_struct{'category'}} ) {
my $search_struct = $dic_struct{'search'}{'category'};
my $block_code = $category_block->{'name'};
$search_struct->{'by_block_code'}{$block_code} = $category_block;
my $pseudo_name = get_data_name( $category_block );
next if !defined $pseudo_name;
$search_struct->{'by_pseudo_name'}{$pseudo_name} = $category_block;
$search_struct->{'by_lc_pseudo_name'}{lc $pseudo_name} = $category_block;
my $name = convert_pseudo_data_name_to_category_name( $pseudo_name );
$search_struct->{'by_name'}{$name} = $category_block;
$search_struct->{'by_lc_name'}{lc $name} = $category_block;
}
$dic_struct{'item'} = $classified_blocks->{'item'};
for my $item_block ( @{$dic_struct{'item'}} ) {
my $search_struct = $dic_struct{'search'}{'item'};
my $block_code = $item_block->{'name'};
$search_struct->{'by_block_code'}{$block_code} = $item_block;
my $data_names = get_data_names( $item_block );
next if !defined $data_names;
for my $data_name ( @{$data_names} ) {
$search_struct->{'by_data_name'}{$data_name} = $item_block;
}
}
return \%dic_struct;
}
sub get_item_blocks
{
my ( $dic ) = @_;
return $dic->{'item'};
}
sub get_item_block_by_name
{
my ( $dic, $data_name ) = @_;
if ( exists $dic->{'search'}{'item'}{'by_data_name'}{$data_name} ) {
return $dic->{'search'}{'item'}{'by_data_name'}{$data_name};
}
return;
}
sub get_item_block_by_block_code
{
my ( $dic, $data_name ) = @_;
if ( exists $dic->{'search'}{'item'}{'by_block_code'}{$data_name} ) {
return $dic->{'search'}{'item'}{'by_block_code'}{$data_name};
}
return;
}
sub get_category_blocks
{
my ( $dic ) = @_;
return $dic->{'category'};
}
sub get_category_block_by_pseudo_name
{
my ( $dic, $data_name ) = @_;
return $dic->{'search'}{'category'}{'by_pseudo_name'}{$data_name};
}
sub get_category_block_by_name
{
my ( $dic, $data_name ) = @_;
return $dic->{'search'}{'category'}{'by_name'}{$data_name};
}
sub get_category_block_by_lc_pseudo_name
{
my ( $dic, $data_name ) = @_;
return $dic->{'search'}{'category'}{'by_lc_pseudo_name'}{$data_name};
}
sub get_category_block_by_lc_name
{
my ( $dic, $data_name ) = @_;
return $dic->{'search'}{'category'}{'by_lc_name'}{$data_name};
}
sub get_dictionary_blocks
{
my ( $dic ) = @_;
return $dic->{'dictionary'};
}
sub get_list_reference_block
{
my ( $dic, $list_reference ) = @_;
my $reference_block = get_item_block_by_name( $dic, $list_reference );
return $reference_block if defined $reference_block;
my $list_reference_by_block = $list_reference;
$list_reference_by_block =~ s/^_//;
$reference_block = get_item_block_by_block_code( $dic, $list_reference_by_block );
return $reference_block;
}
##
# The following subroutines could be eventually moved to the
# COD::CIF::DDL::DDL1 module
##
sub get_parent_item_name
{
my ( $dic_item ) = @_;
return if !exists $dic_item->{'values'}{'_list_link_parent'};
return $dic_item->{'values'}{'_list_link_parent'}[0];
}
sub get_child_item_names
{
my ( $dic_item ) = @_;
return if !exists $dic_item->{'values'}{'_list_link_child'};
return $dic_item->{'values'}{'_list_link_child'};
}
sub get_dic_item_values
{
my ( $data_frame, $data_name ) = @_;
my $data_item_defaults = get_enumeration_defaults();
my $values;
if ( defined $data_item_defaults->{$data_name} ) {
push @{$values}, $data_item_defaults->{$data_name};
}
if ( exists $data_frame->{'values'}{$data_name} ) {
$values = $data_frame->{'values'}{$data_name};
};
return $values;
}
sub get_list_references
{
my ( $dic_item ) = @_;
my $values = get_dic_item_values( $dic_item, '_list_reference' );
return $values;
}
sub get_list_mandatory
{
my ( $dic_item ) = @_;
my $values = get_dic_item_values( $dic_item, '_list_mandatory' );
return $values->[0];
}
sub get_type_conditions
{
my ( $dic_item ) = @_;
my $values = get_dic_item_values( $dic_item, '_type_conditions' );
return $values;
}
sub get_related_items
{
my ( $dic_item ) = @_;
my $values = get_dic_item_values( $dic_item, '_related_item' );
return $values;
}
sub get_list_uniqueness
{
my ( $dic_item ) = @_;
my $values = get_dic_item_values( $dic_item, '_list_uniqueness' );
return $values;
}
my $use_parser = 'c';
my $check_references_in_descriptions = 0;
my @ref_dics;
#* OPTIONS:
#* -d, --dictionaries 'cif_core.dic,cif_cod.dic'
#* A list of CIF dictionary files that conform to the DDL1.
#* Dictionaries in this list are only used to resolve
#* category and data item references provided in main
#* checked dictionary. List elements are separated
#* by the comma (',') symbol. In case the file path
#* of the included dictionary contains the comma symbol,
#* the --add-dictionary option should be used.
#* -D, --add-dictionary 'additional DDL1 dictionary.dic'
#* Add an additional DDL1 dictionary to the list.
#* --clear-dictionaries
#* Remove all DDL1 dictionaries from the list.
#*
#* --check-references-in-descriptions
#* Check if the data names referenced in the free-text
#* descriptions of other data items are defined in the
#* dictionary. This check use ad hoc code to recognise
#* data names and thus might produce false-negatives.
#* --no-check-references-in-descriptions
#* Do not check if the data names referenced in the
#* free-text descriptions of other data items are defined
#* in the dictionary (default).
#*
#* --help, --usage
#* Output a short usage message (this message) and exit.
#* --version
#* Output version information and exit.
#**
@ARGV = getOptions(
'-d,--dictionaries' => sub{ @ref_dics = split m/,/, get_value() },
'-D,--add-dictionary' => sub { push @ref_dics, get_value() },
'--clear-dictionaries' => sub { @ref_dics = () },
'--check-references-in-descriptions' =>
sub { $check_references_in_descriptions = 1 },
'--no-check-references-in-descriptions' =>
sub { $check_references_in_descriptions = 0 },
'--options' => sub{ options; exit },
'--help,--usage' => sub{ usage; exit },
'--version' => sub { print get_version_string(), "\n"; exit }
);
my $die_on_error_level = {
'ERROR' => 1,
'WARNING' => 0,
'NOTE' => 0
};
my $parser_options = { 'parser' => $use_parser, 'no_print' => 1 };
my %reference_dics;
for my $ref_dic_path ( @ref_dics ) {
my ( $data, $err_count, $messages ) = parse_cif( $ref_dic_path, $parser_options );
process_parser_messages( $messages, $die_on_error_level );
$reference_dics{$ref_dic_path} = build_dic_struct( $data );
}
my $err_level = 'NOTE';
for my $filename ( @ARGV ) {
my ( $data, $err_count, $messages ) = parse_cif( $filename, $parser_options );
process_parser_messages( $messages, $die_on_error_level );
my $dic = build_dic_struct( $data );
if ( $check_references_in_descriptions ) {
for my $data_block ( @{$data} ) {
for ( @{ check_references_in_descriptions( $data_block, $dic, \%reference_dics ) } ) {
print "$0: $filename $data_block->{'name'}: $err_level, $_.\n";
}
}
}
my @dictionary_notes;
push @dictionary_notes, @{ check_data_block_code_uniqueness( $data )};
push @dictionary_notes, @{ check_data_name_uniqueness( $data ) };
push @dictionary_notes, @{ check_metadata_block_multiplicity( $dic ) };
push @dictionary_notes, @{ check_child_parent_reciprocity( $dic ) };
push @dictionary_notes, @{ check_list_constraint_uniformity( $dic ) };
for ( @dictionary_notes ) {
print "$0: $filename: $err_level, $_.\n";
}
for my $data_block ( @{ get_dictionary_blocks( $dic ) } ) {
for ( @{ check_metadata_block( $data_block ) } ) {
print "$0: $filename $data_block->{'name'}: $err_level, $_.\n";
}
}
for my $data_block ( @{ get_category_blocks( $dic ) } ) {
my @notes;
push @notes, @{ check_category_block( $data_block ) };
push @notes, @{ check_category_population( $data_block, get_item_blocks( $dic ) ) };
for ( @notes ) {
print "$0: $filename $data_block->{'name'}: $err_level, $_.\n";
}
}
for my $data_block ( @{ get_item_blocks( $dic ) } ) {
my @notes;
push @notes, @{ check_data_item_name_syntax( $data_block ) };
push @notes, @{ check_type_conditions( $data_block ) };
push @notes, @{ check_simultaneous_item_presence( $data_block ) };
push @notes, @{ check_item_block( $data_block ) };
push @notes, @{ check_data_value_uniqueness( $data_block ) };
push @notes, @{ check_list_item_compatability( $data_block ) };
push @notes, @{ check_list_link_parent(
$data_block,
$dic,
\%reference_dics
) };
push @notes, @{ check_list_link_child( $data_block, $dic ) };
push @notes, @{ check_category_references( $data_block, $dic, \%reference_dics ) };
push @notes, @{ check_related_item( $data_block, $dic, \%reference_dics )};
push @notes, @{ check_list_reference( $data_block, $dic, \%reference_dics ) };
push @notes, @{ check_list_uniqueness( $data_block, $dic, \%reference_dics ) };
for ( @notes ) {
print "$0: $filename $data_block->{'name'}: $err_level, $_.\n";
}
}
}
|