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
|
# Copyright (c) 1997-2024
# Ewgenij Gawrilow, Michael Joswig, and the polymake team
# Technische Universität Berlin, Germany
# https://polymake.org
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version: http://www.gnu.org/licenses/gpl.txt.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#-------------------------------------------------------------------------------
use strict;
use namespaces;
use warnings qw(FATAL void syntax misc);
package Polymake::Core::Rule;
use Polymake::Enum Flags => {
is_function => 0x1, # does not return anything, can be called arbitrarily many times
is_precondition => 0x2, # returns a boolean value
is_definedness_check => 0x4, # additional flag for a precondition consisting of a mere check defined($this->PROPERTY)
is_dyn_weight => 0x8, # precondition returning an additional weight [major, minor] or 'false' disabling the production rule connected to this
is_production => 0x10, # production rule with one or more sources and targets
is_perm_action => 0x20, # pseudo-rule concluding the restoration of a property from a permuted subobject
is_perm_restoring => 0x40, # production rule directly suplying a PermAction
is_default_value => 0x80, # production rule with a single target and without sources
is_initial => 0x100, # rule executed at the initial commit of a new object, maybe without targets
is_spez_precondition => 0x200, # precondition for a restricted object type specialization
in_restricted_spez => 0x400 # rule defined within a scope of a restricted object type spezialization
};
# The result code of a rule execution
# exec_OK: execution successful
# exec_retry: execution was not successful, but it's not the rule's fault
# exec_failed: the rule has failed and should be disabled for the object permanently
# exec_infeasible: the rule can't be applied due to lacking input properties and/or unsatisfied preconditions
#
use Polymake::Enum Exec => qw( OK retry failed infeasible );
use Polymake::Struct (
[ 'new' => '$;$$$' ],
[ '$header' => '#1' ], # 'ORIGINAL HEADER' for diagnostic and debug output
'@input', # ( [ prop_ref ] ) each inner list encodes a group of alternatives
# prop_ref ::= SimpleProperty | [ SubobjProperty, ..., SimpleProperty ]
'@output', # ( prop_ref )
[ '$flags' => '0' ], # is_*
[ '$code' => '#2' ], # sub { ... } compiled perl code of the rule body
[ '$defined_for' => '#3' ], # BigObjectType
[ '$overridden_in' => 'undef' ], # [ BigObjectType ... ]
'@labels', # rule labels
'$weight', # [ weight_category, weight_value ]
'@preconditions', # ( Rule )
[ '$dyn_weight' => 'undef' ], # Rule also inserted in preconditions
[ '$credit' => '#4' ], # optional Credit from the rule file
[ '$with_permutation' => 'undef' ], # CreatingPermutation
[ '$not_for_twin' => 'undef' ], # Property->key of a twin property this rule has been flipped for
);
# The ARRAY elements in the input lists may have following array flags (see get_array_flags),
# reflecting the occurrence of corresponding properties:
# Property::Flags::is_permutation
# The ARRAY elements in the output lists may have following array flags,
# reflecting the occurrence of corresponding properties:
# Property::Flags::is_permutation
# Property::Flags::is_multiple_new for a multiple subobject property with attribute (new)
# Property::Flags::is_multiple for a multiple subobject property with attribute (any)
declare $std_weight=[2, 10]; # standard rule weight
declare $zero_weight=[0, 0];
declare $max_major=$std_weight->[0]+1;
declare $timeout;
sub without_permutation { $_[0] }
####################################################################################
# private:
sub check_repeated_properties {
my ($path, $seen, $tag)=@_;
my ($prop, $state);
{
$prop = local pop @$path;
$seen=($seen->{$_->key} //= { }) for @$path;
}
if ($tag != ($state=($seen->{$prop->key} += $tag))) {
my $name=Property::print_path($path);
if ($state>0) {
croak( "Rule source '$name' occurs twice" );
} elsif ($state<0) {
croak( "Rule target '$name' occurs twice" );
} else {
croak( "Property '$name' occurs both as a source and a target" );
}
}
if ($tag>0 and $prop->flags & Property::Flags::is_multiple) {
croak( "A rule may not have an entire multiple subobject as a source but only its single properties" );
}
if ($prop->flags & Property::Flags::is_permutation) {
if ($tag>0) {
croak( "A rule may not have an entire permuted subobject as a source but only its single properties" );
} else {
croak( "A permuted subobject can't be produced as a whole" );
}
}
}
####################################################################################
# private:
sub parse_input {
my ($self, $sources, $seen)=@_;
@{$self->input}=map {
my $input=$self->defined_for->encode_read_request($_);
if ($enable_plausibility_checks) {
check_repeated_properties($_, $seen, 1) for @$input;
}
$input
} split /\s*,\s*/, $sources;
}
####################################################################################
# bit offset for storing the depth of a multiple subobject with `any' attribute in a rule target path
my $any_mult_depth_shift=10;
# private:
sub parse_output {
my ($self, $targets, $seen)=@_;
foreach my $target (split /\s*,\s*/, $targets) {
my $proto=$self->defined_for;
my ($prop, @out);
my $array_flag=0;
my $any_mult_depth=0;
foreach (split /\./, $target) {
if (my ($prop_name, $attrib)= /^($prop_name_re) (?: \(\s* ($prop_name_re) \s*\) )? $/xo) {
$prop=$proto->property($prop_name);
$proto=$prop->type;
push @out, $prop;
if ($prop->flags & Property::Flags::is_multiple) {
if (defined $attrib) {
if ($enable_plausibility_checks) {
foreach my $output (@{$self->output}) {
my $l=Property::equal_path_prefixes(\@out, $output);
if ($l==@out) {
$attrib eq "any"
or croak( "Contradicting attributes `any' and `new' for multiple subobject $prop_name in rule targets ",
Property::print_path($output), " and $target" );
} elsif ($l==$#out && $output->[$l]==$prop->new_instance_deputy) {
$attrib eq "new"
or croak( "Contradicting attributes `new' and `any' for multiple subobject $prop_name in rule targets ",
Property::print_path($output), " and $target" );
}
}
}
if ($attrib eq "new") {
$array_flag & Property::Flags::is_permutation
and croak( "Rule dealing with a permutation may not create a new instance of multiple subobject $prop_name" );
$out[-1]=$prop->new_instance_deputy;
$array_flag |= Property::Flags::is_multiple_new;
} elsif ($attrib eq "any") {
$array_flag & Property::Flags::is_multiple_new
and croak( "Attribute `any' may not appear underneath a new multiple subobject instance in path $target" );
$array_flag |= Property::Flags::is_multiple;
$any_mult_depth ||= @out;
} else {
croak( "Unknown property attribute '$attrib'" );
}
} else {
$array_flag & Property::Flags::is_multiple_new
and croak( "Multiple subobject $prop_name in path $target must be attributed with `new' because it belongs to a new instance of another multiple subobject" );
$any_mult_depth
and croak( "Multiple subobject $prop_name in path $target must be attributed with `any' because it does not occur among rule inputs" );
$array_flag |= Property::Flags::is_multiple;
}
} else {
if ($enable_plausibility_checks && defined($attrib)) {
$attrib eq "new" || $attrib eq "any"
? croak( "Attribute '$attrib' is only applicable to multiple subobject properties" )
: croak( "Unknown property attribute '$attrib'" );
}
if ($prop->flags & Property::Flags::is_permutation) {
if ($enable_plausibility_checks && $array_flag == Property::Flags::is_multiple_new) {
croak( "Rule dealing with a permutation may not create new instances of multiple subobjects" );
}
$array_flag |= Property::Flags::is_permutation;
}
}
} else {
croak( "Invalid property name '$_'\n" );
}
}
if ($enable_plausibility_checks) {
if ($prop->flags & Property::Flags::is_multiple) {
croak( "A rule may not create a multiple subobject as a whole but only its single properties" );
}
check_repeated_properties(\@out, $seen, -1);
}
push @{$self->output}, \@out;
if ($array_flag) {
set_array_flags($self->output->[-1], $array_flag | ($any_mult_depth << $any_mult_depth_shift));
}
}
}
####################################################################################
# private:
sub parse_labels {
my ($self, $labels)=@_;
my $app=$self->defined_for->application;
@{$self->labels} = map { $app->add_label($_) } split /\s*,\s*/, $labels;
}
sub nonexistent {
die "attempted to execute a rule labeled as nonexistent\n";
}
# for error messages pointing to the place of definition in the rulefile
sub source_location {
my $self=shift;
(sub_file($self->code), " line ", sub_firstline($self->code))
}
####################################################################################
#
# Constructor - compile the rule
#
# new Rule(header, \&body, BigObjectType);
#
sub new {
my $self=&_new;
my @parts;
my ($is_nonexistent);
if (defined $self->code) {
@parts=split /\s*:\s*/, $self->header, 3;
if ($enable_plausibility_checks and @parts < 2 || $parts[-1] =~ /:/) {
croak( "ill-formed rule header" );
}
# digest the pseudo-labels
if ($parts[0] eq "initial") {
# initial rules may have output properties
if (@parts==3) {
shift @parts;
} else {
$parts[0]="";
}
$self->flags = Flags::is_initial;
} elsif ($is_nonexistent = $parts[0] eq "nonexistent") {
shift @parts;
}
parse_labels($self, shift @parts) if @parts==3;
if (defined &{$self->code}) {
if ($self->flags == Flags::is_initial) {
$self->weight = $zero_weight;
} else {
$self->flags = Flags::is_production;
$self->weight = $std_weight;
}
} else {
$is_nonexistent or croak( "rules without code must be labeled as nonexistent" );
$self->code = \&nonexistent;
}
} else {
@parts=split /\s*=\s*/, $self->header;
if (@parts != 2) {
croak( "ill-formed shortcut rule header" );
}
bless $self, "Polymake::Core::Rule::Shortcut";
$self->flags = Flags::is_production;
$self->weight = $zero_weight;
pop @parts if $parts[1] =~ /^\s*\$this\s*$/;
}
my %seen;
parse_input($self, $parts[1], \%seen);
parse_output($self, $parts[0], \%seen);
# to print warnings for plain subobjects as rule-targets
# if ($enable_plausibility_checks) {
# foreach my $output (@{$self->output}) {
# if (($output->[-1]->flags & (Property::Flags::is_subobject | Property::Flags::is_subobject_array)) == Property::Flags::is_subobject) {
# warn_print( "rule target ", Property::print_path($output), " is a subobject which is deprecated; at ", $self->source_location );
# }
# }
# }
if ($enable_plausibility_checks && !defined($self->code)) {
if (@{$self->input}>1 || @{$self->output}>1) {
croak( "A shortcut rule must have exactly one source and one target" );
}
}
analyze_spez_preconditions($self);
$self;
}
####################################################################################
# decide whether to protect the rule defined in a specialization with the preconditions of the latter
sub analyze_spez_preconditions {
my ($self)=@_;
if ($self->defined_for->enclosed_in_restricted_spez || defined($self->defined_for->preconditions)) {
$self->flags |= Flags::in_restricted_spez;
}
}
####################################################################################
#
# Constructor for special rules
#
# special Rule 'Header', \&body, BigObjectType
#
sub special {
my $self=&_new;
my %seen;
if ($self->header =~ /\s*:\s* (.*\S) \s*$/x) {
my $labels=$`;
parse_input($self, $1, \%seen);
parse_labels($self, $labels) if length($labels);
}
undef $self->output;
$self->weight=$zero_weight;
$self;
}
####################################################################################
sub append_precondition {
my ($self, $precond, $checks_definedness, $override)=@_;
$precond->flags= $checks_definedness ? Flags::is_precondition | Flags::is_definedness_check : Flags::is_precondition;
$precond->header="precondition " . $precond->header . " ( " . $self->header . " )";
push @{$self->preconditions}, $precond;
if ($override) {
if ($enable_plausibility_checks) {
$self->flags & Flags::in_restricted_spez
or croak( "useless or repeated `override precondition'" );
}
$self->flags &= ~Flags::in_restricted_spez;
}
}
sub append_existence_check {
my ($self, $not, $req_string, $proto) = @_;
my $req = $proto->encode_read_request($req_string);
my $neg = length($not);
my $precond = special Rule(':',
$neg
? sub { !defined($_[0]) || is_defined_and_false($_[0]->lookup_request($req, 1)) } # forbid shortcuts
: sub { defined($_[0]) && is_object($_[0]->lookup_request($req, 1)) },
$proto);
$precond->header = "precondition : ${not}exists($req_string) ( " . $self->header . " )";
$precond->flags = Flags::is_precondition;
push @{$self->preconditions}, $precond;
}
sub append_weight {
my ($self, $major, $minor, $spec) = @_;
if (defined $major) {
$self->weight = [ $major, $minor ];
assign_max($max_major, $major+1); # reserve the highest category for preference violation penalties
} else {
$self->weight = $zero_weight;
}
if ($spec) {
$spec->flags = Flags::is_precondition | Flags::is_dyn_weight;
$spec->header = "weight " . $spec->header . " ( " . $self->header . " )";
$self->dyn_weight = $spec;
push @{$self->preconditions}, $spec;
}
}
sub append_permutation {
my ($self, $perm_name) = @_;
my @perm_path = $self->defined_for->encode_descending_path($perm_name);
if ($enable_plausibility_checks) {
if (Property::find_first_in_path(\@perm_path, Property::Flags::is_permutation) != $#perm_path) {
croak( "$perm_name is not a permutation" );
}
if (defined $self->with_permutation) {
croak( "Sorry, multiple permutations per production rule are not implemented" );
}
}
$self->with_permutation=new CreatingPermutation($self, bless \@perm_path, "Polymake::Core::Rule::PermutationPath");
}
####################################################################################
sub has_matching_input {
my ($self, $path)=@_;
my $lp=@$path;
foreach my $input (@{$self->input}) {
foreach my $input_path (@$input) {
if ($lp==@$input_path) {
my $match=Property::equal_path_prefixes($path, $input_path);
return 1 if $match==$lp or $match+1==$lp && $input_path->[-1]->key == $path->[-1]->key;
}
}
}
0
}
####################################################################################
sub flip_path_for_twin {
my ($path, $proto, $twin_prop)=@_;
my $result;
if ($path->[0]->key == $twin_prop->key) {
$result=[ @$path[1..$#$path] ];
} elsif ($twin_prop->type->isa($path->[0]->defined_for)) {
$result=[ $twin_prop, @$path ];
} else {
return;
}
if (is_object($path)) {
inherit_class($result, $path);
} elsif (my $flags=get_array_flags($path)) {
set_array_flags($result, $flags);
}
$result
}
sub flip_rule_for_twin {
my ($self, $twin_prop)=@_;
my $flipped_input=[ map { [ map { flip_path_for_twin($_, $self->defined_for, $twin_prop) // return } @$_ ] } @{$self->input} ];
my $flipped_output= defined($self->output) ? [ map { flip_path_for_twin($_, $self->defined_for, $twin_prop) // return } @{$self->output} ] : undef;
my $flipped_perm_path= defined($self->with_permutation) ? flip_path_for_twin($self->with_permutation->perm_path, $self->defined_for, $twin_prop) // return : undef;
my $code=$self->code;
my $flipped_rule=_new($self, $self->header." (applied to ".$twin_prop->name.")",
$code && ($self->flags & Flags::is_precondition
? sub { my ($this)=@_; $code->($this->descend([ $twin_prop, undef ])) }
: sub { my ($this)=@_; $code->($this->descend_and_create([ $twin_prop, undef ])) } ),
$self->defined_for, $self->credit);
$flipped_rule->input=$flipped_input;
$flipped_rule->output=$flipped_output;
$flipped_rule->flags=$self->flags;
$flipped_rule->weight=$self->weight;
@{$flipped_rule->preconditions}=map { flip_rule_for_twin($_, $twin_prop) } @{$self->preconditions};
if (defined($self->dyn_weight)) {
$flipped_rule->dyn_weight=$flipped_rule->preconditions->[list_index($self->preconditions, $self->dyn_weight)];
}
if (defined($flipped_perm_path)) {
$flipped_rule->with_permutation=new CreatingPermutation($flipped_rule, $flipped_perm_path);
}
$flipped_rule->not_for_twin=$twin_prop->key;
$flipped_rule;
}
####################################################################################
sub needs_finalization {
my ($self) = @_;
my @path_to_perm;
foreach my $input (@{$self->input}) {
if (get_array_flags($input) & Property::Flags::is_permutation) {
if ($enable_plausibility_checks) {
if ($self->flags & Flags::is_initial) {
croak( "initial rules may not trigger permutations" );
}
foreach my $path (@$input) {
my $perm_index = Property::find_first_in_path($path, Property::Flags::is_permutation);
if ($perm_index < 0) {
croak( "A permuted subobject mixed with non-permuted properties in one source group" );
}
if (@path_to_perm) {
if (Property::equal_path_prefixes(\@path_to_perm, $path) <= $perm_index) {
croak( "Different permuted subobjects occur as sources in the same rule" );
}
}
@path_to_perm = @$path[0..$perm_index];
if (grep { $_->flags & Property::Flags::is_permutation } @$path[$perm_index+1..$#$path]) {
croak( "Nested permuted subobjects are not supported" );
}
}
} else {
@path_to_perm = @{$input->[0]}[0..Property::find_first_in_path($input->[0], Property::Flags::is_permutation)];
last;
}
}
}
if (@path_to_perm) {
if ($path_to_perm[-1]->analyze_rule($self)) {
$self->flags |= Flags::is_perm_restoring;
return;
}
} else {
croak( "Only permutation rules may be labeled as nonexistent") if $self->code == \&nonexistent;
push @{$self->defined_for->production_rules}, $self;
}
$self
}
####################################################################################
sub finalize {
my ($self, $for_twin_rule) = @_;
my $proto = $self->defined_for;
my $perm_deputy = $self->with_permutation;
my $perm_path = $perm_deputy && $perm_deputy->perm_path;
if ($self->flags & Flags::is_initial) {
if (defined($perm_deputy)) {
die "rule ", $self->header, " can't trigger permutations at ", $self->source_location, "\n";
}
$proto->add_producers_of(BigObjectType::init_pseudo_prop()->key, $self);
foreach my $input (@{$self->input}) {
foreach my $input_path (grep { @$_ > 1 } @$input) {
(undef, my @ancestors) = reverse(@$input_path);
my $prod_key = BigObjectType::init_pseudo_prop()->get_prod_key(@ancestors);
$proto->add_producers_of($prod_key, $self);
}
}
if (@{$self->output}) {
# an initial rule producing some new properties is only applicable if none of those exists
my $non_existence_checker = sub {
# fail on any existing or undefined property
not grep { !is_defined_and_false($_[0]->lookup_descending_path($_, 1)) } @{$self->output}
};
my $precond = special Rule(":", $non_existence_checker, $self->defined_for);
$precond->header = "precondition: " . join(" && ", map { "!exists(" . Property::print_path($_) . ")" } @{$self->output}) . " ( " . $self->header . " )";
$precond->flags = Flags::is_precondition;
push @{$self->preconditions}, $precond;
}
return;
}
my (%created_new_multi, %twin_targets);
my $i=0;
foreach my $output (@{$self->output}) {
my ($created_prop, @prod);
if (@$output == 1) {
$created_prop = $output->[0];
if ($perm_path && @$perm_path == 1) {
if (defined(my $recovery = $perm_path->[0]->sensitive_props->{$created_prop->key})) {
my $action = new PermAction($self, $i, $recovery, 0);
push @{$perm_deputy->actions}, $action;
push @prod, $action if $recovery != $Permutation::is_non_recoverable;
} else {
push @prod, $perm_deputy;
}
}
$proto->add_producers_of($created_prop->key, $self, @prod);
} else {
my ($prop, @ancestors) = reverse(@$output);
$created_prop = $prop;
if ($perm_path && Property::equal_path_prefixes($perm_path, $output) == (my $perm_depth = $#$perm_path)) {
my $permutation = $perm_path->[-1];
my $sub_perm_hash = $permutation->sub_permutations;
local splice @$output, 0, $perm_depth;
my $last = @$output-1;
my $sub_perm_exists;
for (my $depth=1; $depth <= $last; ++$depth) {
if (defined(my $sub_permutation = $sub_perm_hash->{$output->[$depth-1]->key})) {
if (defined(my $recovery =
$depth == $last ? $sub_permutation->sensitive_props->{$output->[$depth]->key}
: $sub_permutation->find_sensitive_sub_property(@$output[$depth..$last]))) {
my $action = new PermAction($self, $i, $recovery, $depth+$perm_depth, $sub_permutation);
push @{$perm_deputy->actions}, $action;
push @prod, $action if $recovery != $Permutation::is_non_recoverable;
$sub_perm_exists = true;
last;
}
}
defined($sub_perm_hash = $sub_perm_hash->{$Permutation::sub_key}) &&
defined($sub_perm_hash = $sub_perm_hash->{$output->[$depth-1]->key})
or last;
}
unless ($sub_perm_exists) {
if (defined(my $recovery = $permutation->find_sensitive_sub_property(@$output))) {
my $action = new PermAction($self, $i, $recovery, $perm_depth, $perm_depth && $permutation);
push @{$perm_deputy->actions}, $action;
push @prod, $action if $recovery != $Permutation::is_non_recoverable;
} else {
push @prod, $perm_deputy;
}
}
}
my $flags = get_array_flags($output);
my $any_mult_depth = $flags >> $any_mult_depth_shift;
my $common_prefix; # the longest initial subpath of output occurring among inputs
my $bad_twin;
if ($flags & Property::Flags::is_permutation) {
# rules dealing with permutations may not create new subobjects: pretend a full match
$common_prefix = $#$output;
} else {
if (!$for_twin_rule && $ancestors[-1]->flags & Property::Flags::is_twin) {
$twin_targets{$ancestors[-1]} = true;
$bad_twin = true;
}
if ($any_mult_depth==0 || $enable_plausibility_checks) {
$common_prefix = 0;
foreach my $rule ($self, @{$self->preconditions}) {
foreach my $input (@{$rule->input}) {
foreach my $input_path (@$input) {
my $l=Property::equal_path_prefixes($output, $input_path);
assign_max($common_prefix, $l);
if ($enable_plausibility_checks) {
$bad_twin &&= $l>0 if $rule == $self;
if ($flags & Property::Flags::is_multiple_new &&
$l < $#$output &&
$l < $#$input_path &&
$output->[$l]->flags & Property::Flags::is_multiple_new &&
$output->[$l]->property == $input_path->[$l]) {
die "Multiple subobject ", $output->[$l]->name, " can't be created because it occurs among ",
$rule==$self ? "rule" : "precondition", " sources at ", $self->source_location, "\n";
}
if ($any_mult_depth && $any_mult_depth <= $l) {
die "Superfluous attribute `any' for multiple subobject ", $output->[$any_mult_depth-1]->name,
" because it occurs among ",
$rule==$self ? "rule" : "precondition", " sources at ", $self->source_location, "\n";
}
}
}
}
}
if ($enable_plausibility_checks && $bad_twin) {
die "Twin property ", $ancestors[-1]->name, " occurs in all sources and in at least one target - rule should be flipped at ",
$self->source_location, "\n";
}
}
if ($flags & Property::Flags::is_multiple_new) {
# strip off all properties below the first new instance,
# they are not stored in producers lists
my $pos=Property::find_first_in_path($output, Property::Flags::is_multiple_new);
$prop=$output->[$pos];
splice @ancestors, 0, @ancestors-$pos;
}
if ($flags & Property::Flags::is_multiple) {
if ($enable_plausibility_checks) {
foreach ($common_prefix..($any_mult_depth || @$output)-2) {
if (($output->[$_]->flags & (Property::Flags::is_multiple | Property::Flags::is_multiple_new)) == Property::Flags::is_multiple) {
die "Multiple subobject ", $output->[$_]->name, " in path ", Property::print_path($output),
" must be attributed with `any' or `new' because it does not occur among rule or precondition sources at ",
$self->source_location, "\n";
}
}
}
if ($any_mult_depth) {
# find the last multiple property with `any' attribute
$common_prefix=$any_mult_depth;
foreach ($any_mult_depth+1..$#$output-1) {
if (($output->[$_]->flags & (Property::Flags::is_multiple | Property::Flags::is_multiple_new)) == Property::Flags::is_multiple) {
$common_prefix=$_;
}
}
}
}
}
for (;;) {
my $prod_key=$prop->get_prod_key(@ancestors);
unless ($flags & Property::Flags::is_multiple_new && $created_new_multi{$prod_key}++) {
$proto->add_producers_of($prod_key, $self, @prod);
}
last if @ancestors <= $common_prefix;
$prop=shift @ancestors;
@prod=($perm_deputy) if @prod; # subobjects are created as soon as the rule starts, not when the atomic property is permuted back
}
}
if ($created_prop->flags & Property::Flags::is_subobject) {
$created_prop->flags |= Property::Flags::is_produced_as_whole;
}
} continue { ++$i }
if ($enable_plausibility_checks) {
if (defined($perm_deputy) && !@{$perm_deputy->actions}) {
die "None of the target properties is sensitive to the permutation ",
Property::print_path($perm_deputy->perm_path), " which pretends to be triggered at ", $self->source_location, "\n";
}
if ($self->flags & Flags::in_restricted_spez && defined($self->defined_for->preconditions)) {
foreach my $output (@{$self->output}) {
foreach my $precond (@{$self->defined_for->preconditions}) {
if ($precond->has_matching_input($output)) {
die "Target ", Property::print_path($output), " is an input to a specialization ", $precond->header, "\n",
(@{$self->preconditions} ? "one of preconditions of the production rule must be declared as `override'"
: "the production rule must be equipped with an `override' precondition"),
" at ", $self->source_location, "\n";
}
}
}
}
}
if (@{$self->labels}) {
$proto->add_rule_labels($self, $self->labels);
$proto->add_rule_labels($perm_deputy, $self->labels) if defined $perm_deputy;
}
if (defined($self->overridden_in)) {
$proto->override_rule(@$_) for @{$self->overridden_in};
undef $self->overridden_in;
}
if (!$for_twin_rule && keys %twin_targets) {
foreach my $twin_prop (keys %twin_targets) {
if (defined(my $flipped_rule = flip_rule_for_twin($self, $twin_prop))) {
push @{$proto->production_rules}, $flipped_rule;
finalize($flipped_rule, true);
}
}
}
}
sub sensitivity_check { $_[0]->with_permutation->perm_path }
####################################################################################
# protected:
sub prepare_header_search_pattern {
# remove whitespaces from input
$_[0] =~ s/\s+//g;
# expect optional spaces around punctuation characters
$_[0] =~ s/(?<=\w)(?=[|:,])/\\s*/g;
$_[0] =~ s/(?<=[|:,])(?=\w)/\\s*/g;
# protect characters having special meaning
$_[0] =~ s/([.|()])/\\$1/g;
}
sub find_by_pattern {
my ($owner, $pattern, $filter) = @_;
if ($pattern =~ /^ (!)? ($hier_id_re) $/xo) {
# specified by label
my $label = $owner->find_rule_label($2)
or die "unknown label \"$pattern\"\n";
defined($filter)
? grep { &$filter } $label->list_all_rules($1)
: $label->list_all_rules($1)
} elsif ($pattern =~ /:/) {
# specified by header
prepare_header_search_pattern($pattern);
# expect optional labels omitted in the search pattern
grep { $_->header =~ qr/^(?: $hier_ids_re \s*:\s* )? (?-x:$pattern) $/x } $owner->all_production_rules
} else {
croak( "invalid rule search pattern: expected a label or a complete header in the form \"OUTPUT : INPUT\"" );
}
}
sub all_to_disable {
my ($self) = @_;
if (defined(my $with_perm = $self->with_permutation)) {
($self, $with_perm, @{$with_perm->actions})
} else {
($self)
}
}
####################################################################################
# private:
# create a closure working as a filter in BigObject::copy
sub create_splitting_filter {
my ($rule, $permutation, $descend_path, $prop_hash, $sub_perm_hash, $depth) = @_;
sub {
my ($dst, $pv, $prop) = @_;
$prop //= $pv->property;
my $recovery;
if (defined($prop_hash) &&
defined($recovery = $prop_hash->{$prop->key}) &&
$recovery != $Permutation::is_non_recoverable) {
my $out = [ @$descend_path, $prop ];
push @{$rule->output}, $out;
my $action = new PermAction($rule, $#{$rule->output}, $recovery, $depth, $depth && $permutation);
push @{$rule->with_permutation->actions}, $action;
my $perm_dst = $dst->add_perm_in_parent($#$descend_path-$depth+1, $permutation, $rule);
local splice @$out, 0, $depth;
my ($perm_obj) = $perm_dst->descend_and_create($out);
return ($perm_obj, $pv->copy($perm_obj, $prop));
}
if (defined($sub_perm_hash) &&
defined(my $sub_permutation = $sub_perm_hash->{$prop->key})) {
return ($dst, $pv->copy($dst, $prop, create_splitting_filter($rule, $sub_permutation, [ @$descend_path, $prop ],
$sub_permutation->sensitive_props, $sub_permutation->sub_permutations,
$#$descend_path+2)));
}
my $down_prop = $prop_hash->{$Permutation::sub_key};
$down_prop &&= $down_prop->{$prop->key};
my $down_sub = $sub_perm_hash->{$Permutation::sub_key};
$down_sub &&= $down_sub->{$prop->key};
if (defined($down_prop) || defined($down_sub)) {
return ($dst, $pv->copy($dst, $prop, create_splitting_filter($rule, $permutation, [ @$descend_path, $prop ],
$down_prop, $down_sub, $depth)));
}
return ($dst, $pv->copy($dst, $prop));
}
}
sub permuting {
my ($pkg, $proto, $perm_path) = @_;
my $self = _new($pkg, 'copying', undef, $proto);
$self->weight = $zero_weight;
$self->with_permutation = new FakeCreatingPermutation($self, $perm_path);
my $permutation = local pop @$perm_path;
my $filter;
if (@$perm_path) {
my ($hash, $prop) = Permutation::descend_and_create(my $sub_perms = { }, @$perm_path);
$hash->{$prop->key} = $permutation;
$hash->{$Permutation::sub_key}->{$prop->key} = $permutation->sub_permutations;
$filter = create_splitting_filter($self, undef, [ ], undef, $sub_perms, 0);
} else {
$filter = create_splitting_filter($self, $permutation, [ ], $permutation->sensitive_props, $permutation->sub_permutations, 0);
}
($self, $filter)
}
####################################################################################
#
# Reduced constructor for pseudo-rules
#
# create Rule 'Header', [ input list ], allow_undefs
sub create {
my $self = _new(splice @_, 0, 2);
$self->input = shift;
undef $self->output;
$self->flags = (shift) ? Flags::is_function | Flags::is_definedness_check : Flags::is_function;
$self->weight = $zero_weight;
$self
}
####################################################################################
# Execute the rule on a separate transaction level
sub execute {
local interrupts(block);
my ($self, $object, $force) = @_;
unless ($self->flags & Flags::is_precondition) {
my $trans = new RuleTransaction($object, $self, $force==2);
unless ($force || defined($trans->changed)) {
$trans->rollback($object);
return Exec::OK;
}
}
dbg_print("applying rule ", $self->header) if $Verbose::rules>2;
&execute_me;
}
# private:
sub execute_me {
my ($self, $object) = @_;
my ($rc, $retval) = (Exec::failed);
eval {
## my $alarm_time = $timeout && !($self->flags & Flags::is_precondition);
## alarm $alarm_time if $alarm_time;
local interrupts(enable);
if (wantarray || $self->flags & Flags::is_precondition) {
$retval=$self->code->($object);
} else {
# call production rules in void context
$self->code->($object);
}
## alarm 0 if $alarm_time;
if ($self->flags & Flags::is_precondition) {
$rc= $retval ? Exec::OK : Exec::failed;
if ($Verbose::rules > 1) {
if ($retval) {
dbg_print( $self->header, " satisfied" ) if $self->flags & Flags::is_precondition and $Verbose::rules > 2;
} else {
warn_print( $self->header, $self->flags & Flags::is_precondition ? " not satisfied" : " failed" );
}
}
} else {
$object->add_credit($self->credit) if defined($self->credit);
# ignore the return code of the production rule
$rc = Exec::OK;
}
$object->commit unless ($self->flags & Flags::is_precondition);
};
if ($@) {
$object->rollback unless ($self->flags & Flags::is_precondition);
if ($@ eq "Interrupted\n") {
die $@;
}
if ($@ eq "Infeasible\n") {
$rc = Exec::infeasible;
} else {
$rc = Exec::failed;
$object->failed_rules->{$self} = 1;
if (defined(my $with_perm = $self->with_permutation)) {
$object->failed_rules->{$_} = 1 for $with_perm, @{$with_perm->actions};
}
}
}
wantarray ? ($rc, $retval) : $rc;
}
####################################################################################
sub list_results {
my ($self, $proto)=@_;
$self->flags & (Flags::is_function | Flags::is_precondition)
||
scalar(grep { get_array_flags($_) & Property::Flags::is_permutation } @{$self->input})
? ()
: map {
my $obj_type=$proto;
join(".", map {
my $name=$obj_type->property_name($_);
$obj_type=$_->type;
$name
} @$_)
} @{$self->output}
}
####################################################################################
package Polymake::Core::Rule::Shortcut;
use Polymake::Struct (
[ '@ISA' => 'Rule' ],
[ new => '$$$' ],
[ '$code' => 'undef' ],
[ '$output' => '[ #1 ]' ],
[ '$input' => '[ #2 ]' ],
[ '$flags' => 'Flags::is_production' ],
[ '$weight' => '$zero_weight' ],
);
sub new {
my $self=&_new;
$self->header=Property::print_path($self->output->[0]) . " = " . Property::print_path($self->input->[0]);
$self;
}
sub append_weight {
croak( "A shortcut rule has always zero weight" );
}
sub append_permutation {
croak( "A shortcut rule cannot trigger any permutations" );
}
sub descend_to_source {
my ($self, $object)=@_;
my ($prop, $content_index);
if (@{$self->input}) {
if (($object, $prop)=$object->descend($self->input->[0]->[0])
and defined ($content_index=$object->dictionary->{$prop->key})) {
$object->contents->[$content_index]
} else {
undef
}
} else {
$object
}
}
sub execute {
local interrupts(block);
my ($self, $object) = @_;
dbg_print("applying rule ", $self->header) if $Verbose::rules > 2;
if (defined(my $pv = &descend_to_source)) {
my $trans = $object->begin_transaction;
my ($subobj, $prop) = $object->descend_and_create($self->output->[0]);
my $ref_pv = new PropertyValue($prop,
$pv != $object ? ($pv->value, defined($pv->value) && PropertyValue::Flags::is_shortcut)
: ($object, PropertyValue::Flags::is_weak_ref));
push @{$subobj->contents}, $ref_pv;
$subobj->dictionary->{$prop->key} = $#{$subobj->contents};
$trans->commit($object);
wantarray ? (Exec::OK, $ref_pv) : Exec::OK;
} else {
Exec::infeasible;
}
}
sub source_location {
my $self=shift;
($self->credit->[0], " line ", $self->credit->[1])
}
####################################################################################
package Polymake::Core::Rule::Dummy;
use Polymake::Struct (
[ '@ISA' => 'Rule' ],
[ new => '$$$' ],
[ '$flags' => '#2' ],
[ '$weight' => '$zero_weight' ],
);
*new=\&_new;
sub execute { Exec::OK }
####################################################################################
package Polymake::Core::Rule::Deputy;
# Base class for various rule proxies, especially those used in the Scheduler.
use Polymake::Struct (
[ new => '$' ],
[ '$rule' => '#1' ], # the proper Rule doing the hard job
[ '$flags' => '#1->flags' ],
[ '$weight' => '#1->weight' ],
);
sub header { $_[0]->rule->header }
sub list_results { $_[0]->rule->list_results($_[1]) }
sub input { $_[0]->rule->input }
sub output { $_[0]->rule->output }
sub labels { $_[0]->rule->labels }
sub preconditions { $_[0]->rule->preconditions }
sub dyn_weight { $_[0]->rule->dyn_weight }
sub overridden_in { $_[0]->rule->overridden_in }
sub with_permutation { $_[0]->rule->with_permutation }
sub defined_for { $_[0]->rule->defined_for }
sub has_matching_input { $_[0]->rule->has_matching_input($_[1]) }
sub not_for_twin { $_[0]->rule->not_for_twin }
####################################################################################
package Polymake::Core::Rule::CreatingPermutation;
use Polymake::Struct (
[ '@ISA' => 'Deputy' ],
[ new => '$$' ],
[ '$rule' => 'weak(#1)' ], # production Rule
[ '$perm_path' => '#2' ], # property path leading to a permutation
'@actions', # PermAction descriptors
);
sub header {
my ($self)=@_;
$self->rule->header . " ( creating " . Property::print_path($self->perm_path) . " )";
}
sub with_permutation { undef }
sub without_permutation { $_[0]->rule }
sub execute {
local interrupts(block);
my ($self, $object, $force) = @_;
new RuleTransactionWithPerm($object, $self, $force==2);
dbg_print("applying rule ", $self->header) if $Verbose::rules>2;
execute_me($self->rule, $object);
}
sub record_sensitivity {
my ($self, $object)=@_;
my ($sub_obj, $perm)=$object->descend($self->perm_path);
$sub_obj->sensitive_to->{$perm->key}=1;
foreach my $action (@{$self->actions}) {
if ($action->depth) {
my $out=$self->rule->output->[$action->output];
local splice @$out, $action->depth+1;
my ($sub_obj)=$object->descend($out);
$sub_obj->sensitive_to->{$action->sub_permutation->key}=1;
}
}
}
package Polymake::Core::Rule::FakeCreatingPermutation;
use Polymake::Struct [ '@ISA' => 'CreatingPermutation' ];
sub execute {
my $object = $_[1];
$object->commit;
&record_sensitivity;
Exec::OK
}
package Polymake::Core::Rule::PermAction;
use Polymake::Struct (
[ new => '$$$$;$' ],
[ '$perm_trigger' => 'weak(#1)' ], # production rule triggering the permutation
[ '$output' => '#2' ], # index into its output where the sensitive property lies
[ '$recovery' => '#3' ], # Permutation::Recovery to obtain Rules moving the output property back to the basis
[ '$depth' => '#4' ], # how deep to descend in the base object
[ '$sub_permutation' => '#5' ], # Permutation for the subobject
);
sub header {
my ($self) = @_;
Property::print_path($self->perm_trigger->output->[$self->output])
. " extracted from " . Property::print_path($self->perm_path) . " after " . $self->perm_trigger->header;
}
sub list_results { () }
sub perm_path { $_[0]->perm_trigger->with_permutation->perm_path }
sub flags { Flags::is_perm_action }
sub weight { $zero_weight }
sub enabled { $_[0]->recovery != $Permutation::is_non_recoverable }
sub preconditions { [ ] }
sub with_permutation { undef }
sub overridden_in { undef }
sub not_for_twin { $_[0]->perm_trigger->not_for_twin }
####################################################################################
package Polymake::Core::Rule::PermutationPath;
sub header { "sensitivity check for " . &Property::print_path }
sub list_results { () }
sub flags { Flags::is_function }
sub weight { $zero_weight }
sub preconditions { [ ] }
sub with_permutation { undef }
sub execute {
my ($self, $object) = @_;
dbg_print("performing ", $self->header) if $Verbose::rules > 2;
my ($sub_obj, $perm) = $object->descend($self);
$sub_obj->has_sensitive_to($perm) ? Exec::failed : Exec::OK
}
sub find_sensitive_sub_property {
my ($self, @path) = @_;
if (Property::equal_path_prefixes($self, \@path) == $#$self) {
$self->[-1]->find_sensitive_sub_property(splice @path, $#$self)
} else {
undef
}
}
####################################################################################
package Polymake::Core::Rule::Credit;
use Polymake::Struct (
[ new => '$$' ],
[ '$product' => '#1' ],
[ '$text' => '#2' ],
[ '$file_string' => 'undef' ],
[ '$shown' => '0' ],
);
sub hide() { 3 } # suppresses display if assigned to ->shown
sub display {
my ($self)=@_;
dbg_print( "used package ", $self->product, "\n", $self->text, "\n" );
$self->shown=1;
}
sub toFileString {
my ($self)=@_;
$self->file_string //= do {
my ($copyright)= $self->text =~ /(copyright\b.*)/im;
my ($URL)= $self->text =~ /(?:url:?\s+)(\S+)/im;
if (!defined($URL) && $self->text =~ m{(?:\w+://|(?:www|ftp)\.)\S+}) {
$URL=$&;
}
defined($copyright) && defined($URL) ? "\n$copyright\n$URL\n" : $self->text
}
}
1
# Local Variables:
# cperl-indent-level:3
# indent-tabs-mode:nil
# End:
|