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
|
#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2022-07-31 23:15:34 +0300 (Sun, 31 Jul 2022) $
#$Revision: 9355 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.7.0/scripts/cif_compare_dics $
#------------------------------------------------------------------------------
#*
#* Checks a pair of DDL1/DDLm dictionaries for compatibility breaking
#* differences. Discrepancies between corresponding enumeration sets,
#* default values, permitted ranges as well as data items unique to
#* one of the dictionaries are reported.
#*
#* USAGE:
#* $0 --ddl1 cif_core_ddl1.dic --ddlm cif_core_ddlm.dic --options
#*
#* ENVIRONMENT:
#* COD_TOOLS_DDLM_IMPORT_PATH
#* A list of directories in which to look for the
#* DDLm-compliant CIF dictionaries that are imported
#* by other DDLm-compliant CIF dictionaries. List
#* elements are separated by the colon symbol (':').
#* Directories listed in COD_TOOLS_DDLM_IMPORT_PATH
#* have a lower priority than those provided using
#* the command line option (--add-dictionary-import-path),
#* but higher than the default import path directory
#* (directory of the importing dictionary).
#**
use strict;
use warnings;
binmode STDOUT, ':encoding(UTF-8)';
binmode STDERR, ':encoding(UTF-8)';
use File::Basename qw( fileparse );
use List::MoreUtils qw( none );
use COD::CIF::Parser qw( parse_cif );
use COD::CIF::DDL::DDLm qw( get_category_id
get_data_name
get_data_alias
get_definition_class
get_definition_scope
get_type_container
get_type_purpose );
use COD::CIF::DDL::DDLm::Import qw( get_ddlm_import_path_from_env
resolve_dic_imports );
use COD::CIF::DDL::Ranges qw( parse_range );
use COD::SOptions qw( getOptions get_value );
use COD::SUsage qw( usage options );
use COD::ErrorHandler qw( process_parser_messages
process_warnings
report_message );
use COD::UserMessage qw( sprint_message );
use COD::ToolsVersion qw( get_version_string );
sub find_unique_values
{
my ($array_1, $array_2, $case_sensitive) = @_;
my %seen;
foreach ( @{$array_1} ) {
$seen{($case_sensitive ? $_ : lc $_ )} = $_;
}
delete @seen {($case_sensitive ? @{$array_2} : map {lc $_} @{$array_2} )};
my @unique = values %seen;
return \@unique;
}
sub ddl1_to_generic_dic
{
my ($data) = @_;
my @tags;
my %enum_values;
my %enum_defaults;
my %enum_ranges;
my %su_eligibility;
my %loop_eligibility;
my %data_containers;
for my $datablock ( @{$data} ) {
# Skip the data block that describes the dictionary itself
next if lc $datablock->{'name'} eq 'on_this_dictionary';
my $values = $datablock->{'values'};
if ( !exists $values->{'_type'} ) {
warn "the '$datablock->{'name'}' data block does not contain " .
'the \'_type\' data item -- data block will be ignored' .
"\n";
next;
}
next if $values->{'_type'}[0] eq 'null';
if ( !exists $values->{'_name'} ) {
warn "the '$datablock->{'name'}' data block does not contain " .
'the \'_name\' data item -- data block will be ignored' .
"\n";
next;
}
for my $name ( map { lc } @{$values->{'_name'}} ) {
# Data names
push @tags, $name;
# Enumeration values
if ( exists $values->{'_enumeration'} ) {
$enum_values{$name} = $values->{'_enumeration'};
}
# Enumeration defaults
if ( $values->{'_enumeration_default'} ) {
$enum_defaults{$name} = $values->{'_enumeration_default'}[0];
}
# Enumeration ranges
if ( exists $values->{'_enumeration_range'} ) {
$enum_ranges{$name} = $values->{'_enumeration_range'}[0];
}
# Standard uncertainty eligibility
# FIXME: consider the default
if ( exists $values->{'_type_conditions'} ) {
$su_eligibility{$name} =
$values->{'_type_conditions'}[0] eq 'su' ||
$values->{'_type_conditions'}[0] eq 'esd' ;
}
# Looped list eligibility
my $is_looped = 0;
if ( exists $values->{'_list'} ) {
if ( $values->{'_list'}[0] eq 'yes' ||
$values->{'_list'}[0] eq 'both' ) {
$is_looped = 1;
}
}
$loop_eligibility{$name} = $is_looped;
# Data containers
$data_containers{$name} = 'Single';
}
}
return {
'data_names' => \@tags,
'enum_values' => \%enum_values,
'enum_defaults' => \%enum_defaults,
'enum_ranges' => \%enum_ranges,
'su_eligibility' => \%su_eligibility,
'loop_eligibility' => \%loop_eligibility,
'data_containers' => \%data_containers,
};
}
sub ddlm_to_generic_dic
{
my ($data) = @_;
my @ddlm_tags;
my %enum_values;
my %enum_defaults;
my %enum_ranges;
my %su_eligibility;
my %loop_eligibility;
my %data_containers;
for my $save_block ( @{$data->{'save_blocks'}} ) {
next if get_definition_scope( $save_block ) ne 'Item';
my $data_name = get_data_name( $save_block );
if ( !defined $data_name ) {
warn "the '$save_block->{'name'}' save block does not contain " .
'the \'_definition.id\' data item -- save block will be ' .
'ignored' . "\n";
next;
}
my $cat_block = get_category_block( $save_block, $data->{'save_blocks'} );
my @data_names = ( lc $data_name );
push @data_names, map { lc } @{get_data_alias($save_block)};
my $values = $save_block->{'values'};
# Enumeration values
if ( exists $values->{'_enumeration_set.state'} ) {
foreach ( @data_names ) {
$enum_values{$_} = $values->{'_enumeration_set.state'};
}
}
# Enumeration defaults
if ( exists $values->{'_enumeration.default'} ) {
foreach ( @data_names ) {
$enum_defaults{$_} = $values->{'_enumeration.default'}[0];
}
}
# Enumeration defaults
if ( exists $values->{'_enumeration.range'} ) {
foreach ( @data_names ) {
$enum_ranges{$_} = $values->{'_enumeration.range'}[0];
}
}
# Standard uncertainty eligibility
my $is_su_eligible = lc get_type_purpose( $save_block ) eq 'measurand';
foreach ( @data_names ) {
$su_eligibility{$_} = $is_su_eligible;
}
# Looped list eligibility
my $is_loop_eligible = is_looped_category( $cat_block );
if ( defined $is_loop_eligible ) {
foreach ( @data_names ) {
$loop_eligibility{$_} = $is_loop_eligible;
}
}
# Data containers
foreach ( @data_names ) {
$data_containers{$_} = get_type_container( $save_block );
}
#~ # Looped list key
#~ my $looped_list_key = get_ddlm_category_key( $data->{'save_blocks'} );
# Data names
push @ddlm_tags, @data_names;
}
return {
'data_names' => \@ddlm_tags,
'enum_values' => \%enum_values,
'enum_defaults' => \%enum_defaults,
'enum_ranges' => \%enum_ranges,
'su_eligibility' => \%su_eligibility,
'loop_eligibility' => \%loop_eligibility,
'data_containers' => \%data_containers,
};
}
sub get_category_block
{
my ( $save_block, $dic_blocks ) = @_;
my $category_id = get_category_id( $save_block );
if ( !defined $category_id ) {
warn "the '$save_block->{'name'}' save block does not contain " .
'the mandatory \'_name.category_id\' data item -- ' .
'some comparisons for this data item will be skipped' . "\n";
return;
}
$category_id = lc $category_id;
my @category_blocks;
for my $dic_block ( @{$dic_blocks} ) {
my $data_name = get_data_name( $dic_block );
next if !defined $data_name;
$data_name = lc $data_name;
if ( $data_name eq $category_id ) {
push @category_blocks, $dic_block;
}
}
if ( !@category_blocks ) {
warn "faulty category definition -- the '$save_block->{'name'}' " .
"save block refers to the '$category_id' category that could " .
'not be located' . "\n";
return;
}
if ( @category_blocks > 1 ) {
warn 'faulty category definition -- more than one save block ' .
"defines the '$category_id' category\n";
return;
}
return $category_blocks[0];
}
sub is_looped_category
{
my ( $category_block ) = @_;
return if !defined $category_block;
my $definition_class = lc get_definition_class( $category_block );
return ( $definition_class eq 'loop' );
}
sub get_category_key
{
my ( $category_block ) = @_;
my @loop_keys;
if ( exists $category_block->{'values'}{'_category_key.name'} ) {
push @loop_keys, @{$category_block->{'values'}{'_category_key.name'}};
} elsif ( exists $category_block->{'values'}{'_category.key_id'} ) {
push @loop_keys, @{$category_block->{'values'}{'_category.key_id'}};
}
return \@loop_keys;
}
sub check_dic_compatability
{
my ( $old_dic, $new_dic ) = @_;
my $signatures = {
'old_dic_signature' => $old_dic->{'signature'},
'new_dic_signature' => $new_dic->{'signature'},
};
my @compatability_notes;
my %new_tags = map { $_ => 1 } @{$new_dic->{'data_names'}};
for my $tag ( @{$old_dic->{'data_names'}} ) {
if ( exists $new_tags{$tag} ) {
push @compatability_notes,
@{ compare_enumeration_values(
$tag,
$old_dic->{'enum_values'},
$new_dic->{'enum_values'},
$signatures ) };
push @compatability_notes,
@{ compare_enumeration_defaults(
$tag,
$old_dic->{'enum_defaults'},
$new_dic->{'enum_defaults'},
$signatures ) };
push @compatability_notes,
@{ compare_enumeration_ranges(
$tag,
$old_dic->{'enum_ranges'},
$new_dic->{'enum_ranges'},
$signatures ) };
push @compatability_notes,
@{ compare_su_eligibility(
$tag,
$old_dic->{'su_eligibility'},
$new_dic->{'su_eligibility'},
$signatures ) };
push @compatability_notes,
@{ compare_loop_eligibility(
$tag,
$old_dic->{'loop_eligibility'},
$new_dic->{'loop_eligibility'},
$signatures ) };
push @compatability_notes,
@{ compare_data_containers(
$tag,
$old_dic->{'data_containers'},
$new_dic->{'data_containers'},
$signatures ) };
} else {
push @compatability_notes,
"WARNING, data name '$tag' is defined in the " .
$signatures->{'old_dic_signature'} .
' dictionary, but seems to be missing in the ' .
$signatures->{'new_dic_signature'} . ' dictionary';
}
}
return \@compatability_notes;
}
sub are_equivalent_limits
{
my ($limit_1, $limit_2) = @_;
my $sigma = 0.000001;
return 1 if ( !defined $limit_1 && !defined $limit_2 );
return 0 if ( !defined $limit_1 || !defined $limit_2 );
return 0 if ( abs($limit_1 - $limit_2) > $sigma );
return 1;
}
sub compare_enumeration_values
{
my ( $tag, $old_enum_values, $new_enum_values, $signatures ) = @_;
my $new_dic_signature = $signatures->{'new_dic_signature'};
my $old_dic_signature = $signatures->{'old_dic_signature'};
return [] if !exists $old_enum_values->{$tag};
my $unique_in_old = $old_enum_values->{$tag};
if ( exists $new_enum_values->{$tag} ) {
$unique_in_old = find_unique_values( $old_enum_values->{$tag},
$new_enum_values->{$tag}, 0 );
}
my @messages;
if ( @{$unique_in_old} ) {
push @messages,
'WARNING, enumeration values [' .
( join ', ', map {"'$_'"} sort @{$unique_in_old} ) .
"] of data item '$tag' are defined in " .
"the $old_dic_signature dictionary, but not in " .
"the $new_dic_signature dictionary";
};
return \@messages;
}
sub compare_enumeration_defaults
{
my ( $tag, $old_enum_default, $new_enum_default, $signatures ) = @_;
my $new_dic_signature = $signatures->{'new_dic_signature'};
my $old_dic_signature = $signatures->{'old_dic_signature'};
return [] if !exists $old_enum_default->{$tag};
my @messages;
if ( exists $new_enum_default->{$tag} ) {
if ( $old_enum_default->{$tag} ne $new_enum_default->{$tag} ) {
push @messages,
"WARNING, enumeration default of the '$tag' data item " .
"differs between the $old_dic_signature dictionary and " .
"the $new_dic_signature dictionary " .
"('$old_enum_default->{$tag}' vs. '$new_enum_default->{$tag}')";
}
} else {
push @messages,
"WARNING, enumeration default '$old_enum_default->{$tag}' " .
"is defined for data item '$tag' in the $old_dic_signature " .
"dictionary, but not in the $new_dic_signature dictionary";
}
return \@messages;
}
sub compare_enumeration_ranges
{
my ( $tag, $old_enum_range, $new_enum_range, $signatures ) = @_;
my $new_dic_signature = $signatures->{'new_dic_signature'};
my $old_dic_signature = $signatures->{'old_dic_signature'};
return [] if !exists $new_enum_range->{$tag};
my @messages;
if ( exists $old_enum_range->{$tag} ) {
my @old_range = @{parse_range($old_enum_range->{$tag})};
my @new_range = @{parse_range($new_enum_range->{$tag})};
if ( !are_equivalent_limits( $old_range[0], $new_range[0] ) ||
!are_equivalent_limits( $old_range[1], $new_range[1] ) ) {
push @messages,
"WARNING, enumeration range of the '$tag' data item " .
"differs between the $old_dic_signature dictionary and " .
"the $new_dic_signature dictionary " .
"('$old_enum_range->{$tag}' vs. '$new_enum_range->{$tag}')";
}
} else {
push @messages,
"WARNING, enumeration range '$new_enum_range->{$tag}' " .
"is defined for data item '$tag' in the $new_dic_signature " .
"dictionary, but not in the $old_dic_signature dictionary";
}
return \@messages;
}
sub compare_su_eligibility
{
my ( $tag, $old_su_eligibility, $new_su_eligibility, $signatures ) = @_;
my $new_dic_signature = $signatures->{'new_dic_signature'};
my $old_dic_signature = $signatures->{'old_dic_signature'};
return [] if !exists $old_su_eligibility->{$tag};
my @messages;
if ( $old_su_eligibility->{$tag} &&
!$new_su_eligibility->{$tag} ) {
push @messages,
"WARNING, data item '$tag' is allowed to contain standard " .
"uncertainty (s.u.) values by the $old_dic_signature " .
"dictionary, but not by the $new_dic_signature dictionary";
};
return \@messages;
}
sub compare_loop_eligibility
{
my ( $tag, $old_loop_eligibility, $new_loop_eligibility, $signatures ) = @_;
my $new_dic_signature = $signatures->{'new_dic_signature'};
my $old_dic_signature = $signatures->{'old_dic_signature'};
return [] if !exists $old_loop_eligibility->{$tag};
my @messages;
if ( $old_loop_eligibility->{$tag} &&
!$new_loop_eligibility->{$tag} ) {
push @messages,
"WARNING, data item '$tag' is allowed to appear in a looped " .
"context by the $old_dic_signature dictionary, but not by the " .
"$new_dic_signature dictionary";
};
return \@messages;
}
##
# Checks if the data structure of the given data item is compatible
# between the old version and the new version of the dictionary.
#
# @param $tag
# Data name of the data item that should be checked.
# @param $old_data_structure
# Reference to a hash that maps data item names to their container type
# as defined in the old dictionary.
# @param $new_data_structure
# Reference to a hash that maps data item names to their container type
# as defined in the new dictionary.
# @param $signatures
# Reference to a hash that contains short human-readable identifiers
# of the old and new dictionary, i.e.:
# {
# 'new_dic_signature' => "'cif_cod.dic' DDL1",
# 'old_dic_signature' => "'cif_cod.dic' DDLm",
# }
# @return
# Reference to an array of warning messages.
##
sub compare_data_containers
{
my ( $tag, $old_data_structure, $new_data_structure, $signatures ) = @_;
my $new_dic_signature = $signatures->{'new_dic_signature'};
my $old_dic_signature = $signatures->{'old_dic_signature'};
return [] if !exists $old_data_structure->{$tag};
my @messages;
if ( $old_data_structure->{$tag} ne $new_data_structure->{$tag} ) {
push @messages,
"WARNING, container type of the '$tag' data item differs " .
"between the $old_dic_signature dictionary and " .
"the $new_dic_signature dictionary " .
"('$old_data_structure->{$tag}' vs. '$new_data_structure->{$tag}')";
}
return \@messages;
}
my $use_parser = 'c';
my $ddl1_file;
my $ddlm_file;
my @dic_import_path;
my @LEGACY_TYPES = qw( ddl1 ddlm );
my $legacy_type = $LEGACY_TYPES[0];
#* OPTIONS:
#* -1, --ddl1
#* The name of the DDL1 conforming dictionary that should
#* be used in the comparison.
#* -m, --ddlm
#* The name of the DDLm conforming dictionary that should
#* be used in the comparison.
#*
#* -I, --add-ddlm-import-path './ddlm/cod/'
#* Prepend an additional directory to the dictionary
#* import path. The dictionary import path specifies
#* a list of directories in which to look for files
#* that are imported by DDLm-compliant CIF dictionaries.
#* Directories provided using this option are assigned
#* the highest priority and are searched prior to
#* the directories listed in the COD_TOOLS_DDLM_IMPORT_PATH
#* environment variable or the default import path
#* (directory of the importing dictionary).
#* --clear-ddlm-import-path
#* Remove all directories from the dictionary import path
#* that were added using the --add-ddlm-import-path option.
#* Neither COD_TOOLS_DDLM_IMPORT_PATH environment variable
#* nor the default import path is affected by this option.
#*
#* --legacy-dic-type ddl1
#* The type of the dictionary that should treated as the
#* legacy one. Accepted values: 'ddl1' (default), 'ddlm'.
#*
#* --help, --usage
#* Output a short usage message (this message) and exit.
#* --version
#* Output version information and exit.
#**
@ARGV = getOptions(
'-1,--ddl1' => sub { $ddl1_file = get_value() },
'-m,--ddlm' => sub { $ddlm_file = get_value() },
'-I,--add-ddlm-import-path' => sub { push @dic_import_path, get_value() },
'--clear-ddlm-import-path' => sub { @dic_import_path = () },
'--legacy-dic-type' => sub { $legacy_type = get_value() },
'--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,
};
if ( none { $legacy_type eq $_ } @LEGACY_TYPES ) {
report_message(
{
'program' => $0,
'err_level' => 'ERROR',
'message' =>
"the specified legacy dictionary type '$legacy_type' is " .
'not one of the supported dictionary types [' .
( join ',', map {"'$_'"} @LEGACY_TYPES ) . '].'
}, $die_on_error_level
)
}
my $options = { 'parser' => $use_parser, 'no_print' => 1 };
my ( $ddl1_data, $ddlm_data, $err_count, $messages );
( $ddl1_data, $err_count, $messages ) = parse_cif( $ddl1_file, $options );
process_parser_messages( $messages, $die_on_error_level );
( $ddlm_data, $err_count, $messages ) = parse_cif( $ddlm_file, $options );
process_parser_messages( $messages, $die_on_error_level );
# This logic was adapted from the ddlm_validate script
$ddlm_data = $ddlm_data->[0];
my $dirs = (fileparse($ddlm_file))[1];
push @dic_import_path, @{get_ddlm_import_path_from_env()};
$ddlm_data = resolve_dic_imports(
$ddlm_data,
{
'import_path' => [ @dic_import_path, $dirs ],
'parser_options' => $options,
'die_on_error_level' => $die_on_error_level,
'importing_file' => $ddlm_file,
}
);
my $ddl1_dic;
{
local $SIG{__WARN__} = sub {
process_warnings( {
'message' => @_,
'program' => $0,
'filename' => $ddl1_file,
}, $die_on_error_level )
};
my $ddl1_name = (fileparse($ddl1_file))[0];
$ddl1_dic = ddl1_to_generic_dic($ddl1_data);
$ddl1_dic->{'signature'} = "'$ddl1_name' DDL1";
}
my $ddlm_dic;
{
local $SIG{__WARN__} = sub {
process_warnings( {
'message' => @_,
'program' => $0,
'filename' => $ddlm_file,
}, $die_on_error_level )
};
my $ddlm_name = (fileparse($ddlm_file))[0];
$ddlm_dic = ddlm_to_generic_dic($ddlm_data);
$ddlm_dic->{'signature'} = "'$ddlm_name' DDLm";
}
my $notes;
if ( $legacy_type eq 'ddl1' ) {
$notes = check_dic_compatability( $ddl1_dic, $ddlm_dic )
} else {
$notes = check_dic_compatability( $ddlm_dic, $ddl1_dic )
};
for ( @{$notes} ) {
print sprint_message( {
'program' => $0,
'message' => $_,
} )
}
|