File size: 114,190 Bytes
9375c9a |
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 |
<html><!-- Created using the cpp_pretty_printer from the dlib C++ library. See http://dlib.net for updates. --><head><title>dlib C++ Library - bayes_net_gui_ex.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
</font><font color='#009900'>/*
This is a rather involved example illustrating the use of the GUI api from
the dlib C++ Library. This program is a fully functional utility for
creating Bayesian Networks. It allows the user to graphically draw the network,
save/load the network to/from disk, and also to calculate the posterior
probability of any node in the network given a set of evidence.
This is not the first dlib example program you should be looking at. If you
want to see a simpler GUI example please look at the <a href="gui_api_ex.cpp.html">gui_api_ex.cpp</a> or
<a href="image_ex.cpp.html">image_ex.cpp</a> example.
If you want to understand how to use the Bayesian Network utilities in the library
you should definitely look at the <a href="bayes_net_ex.cpp.html">bayes_net_ex.cpp</a> example program. It gives a
comprehensive introduction to creating and manipulating Bayesian Networks. If you
want to see how to load a saved network from disk and use it in a non-GUI application
then look at the <a href="bayes_net_from_disk_ex.cpp.html">bayes_net_from_disk_ex.cpp</a> example.
Now all of that being said, if you have already looked at the other relevant
examples and want to see a more in-depth example then by all means, continue reading. :)
*/</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>memory<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>sstream<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>string<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>gui_widgets.h<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>directed_graph.h<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>string.h<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>bayes_utils.h<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>set.h<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>graph_utils.h<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>stl_checked.h<font color='#5555FF'>></font>
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib;
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib::bayes_node_utils;
<font color='#009900'>// ----------------------------------------------------------------------------
</font>
<font color='#0000FF'>typedef</font> directed_graph<font color='#5555FF'><</font>bayes_node<font color='#5555FF'>></font>::kernel_1a_c directed_graph_type;
<font color='#0000FF'>typedef</font> directed_graph<font color='#5555FF'><</font>bayes_node<font color='#5555FF'>></font>::kernel_1a_c::node_type node_type;
<font color='#0000FF'>typedef</font> graph<font color='#5555FF'><</font>dlib::set<font color='#5555FF'><</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>></font>::compare_1b_c, dlib::set<font color='#5555FF'><</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>></font>::compare_1b_c<font color='#5555FF'>></font>::kernel_1a_c join_tree_type;
<font color='#009900'>// ----------------------------------------------------------------------------
</font>
<font color='#0000FF'>class</font> <b><a name='main_window'></a>main_window</b> : <font color='#0000FF'>public</font> drawable_window
<b>{</b>
<font color='#009900'>/*!
INITIAL VALUE
This window starts out hidden and with an empty Bayesian Network
WHAT THIS OBJECT REPRESENTS
This object is the main window of a utility for drawing Bayesian Networks.
It allows you to draw a directed graph and to set the conditional probability
tables up for each node in the network. It also allows you to compute the
posterior probability of each node. And finally, it lets you save and load
networks from file
!*/</font>
<font color='#0000FF'>public</font>:
<b><a name='main_window'></a>main_window</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
~<b><a name='main_window'></a>main_window</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>private</font>:
<font color='#009900'>// Private helper methods
</font>
<font color='#0000FF'><u>void</u></font> <b><a name='initialize_node_cpt_if_necessary'></a>initialize_node_cpt_if_necessary</b> <font face='Lucida Console'>(</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> index <font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='load_selected_node_tables_into_cpt_grid'></a>load_selected_node_tables_into_cpt_grid</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='load_selected_node_tables_into_ppt_grid'></a>load_selected_node_tables_into_ppt_grid</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='no_node_selected'></a>no_node_selected</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// Event handlers
</font>
<font color='#0000FF'><u>void</u></font> <b><a name='on_cpt_grid_modified'></a>on_cpt_grid_modified</b><font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> row, <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> col<font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_evidence_toggled'></a>on_evidence_toggled</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_graph_modified'></a>on_graph_modified</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_menu_file_open'></a>on_menu_file_open</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_menu_file_quit'></a>on_menu_file_quit</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_menu_file_save'></a>on_menu_file_save</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_menu_file_save_as'></a>on_menu_file_save_as</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_menu_help_about'></a>on_menu_help_about</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_menu_help_help'></a>on_menu_help_help</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_node_deleted'></a>on_node_deleted</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_node_deselected'></a>on_node_deselected</b> <font face='Lucida Console'>(</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> n <font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_node_selected'></a>on_node_selected</b> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> n<font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_open_file_selected'></a>on_open_file_selected</b> <font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&</font> file_name<font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_save_file_selected'></a>on_save_file_selected</b> <font face='Lucida Console'>(</font> <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&</font> file_name<font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_sel_node_evidence_modified'></a>on_sel_node_evidence_modified</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_sel_node_num_values_modified'></a>on_sel_node_num_values_modified</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_sel_node_text_modified'></a>on_sel_node_text_modified</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='on_window_resized'></a>on_window_resized</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='recalculate_probabilities'></a>recalculate_probabilities</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// Member data
</font>
<font color='#0000FF'>const</font> rgb_pixel color_non_evidence;
<font color='#0000FF'>const</font> rgb_pixel color_default_bg;
<font color='#0000FF'>const</font> rgb_pixel color_evidence;
<font color='#0000FF'>const</font> rgb_pixel color_error;
<font color='#0000FF'>const</font> rgb_pixel color_gray;
<font color='#0000FF'><u>bool</u></font> graph_modified_since_last_recalc;
button btn_calculate;
check_box sel_node_is_evidence;
directed_graph_drawer<font color='#5555FF'><</font>directed_graph_type<font color='#5555FF'>></font> graph_drawer;
label sel_node_index;
label sel_node_num_values_label;
label sel_node_text_label;
label sel_node_evidence_label;
menu_bar mbar;
named_rectangle selected_node_rect;
tabbed_display tables;
text_field sel_node_num_values;
text_field sel_node_text;
text_field sel_node_evidence;
text_grid cpt_grid;
text_grid ppt_grid;
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> selected_node_index;
<font color='#0000FF'><u>bool</u></font> node_is_selected;
widget_group cpt_group;
widget_group ppt_group;
std::unique_ptr<font color='#5555FF'><</font>bayesian_network_join_tree<font color='#5555FF'>></font> solution;
join_tree_type join_tree;
<font color='#009900'>// The std_vector_c is an object identical to the std::vector except that it checks
</font> <font color='#009900'>// all its preconditions and throws a dlib::fatal_error if they are violated.
</font> std_vector_c<font color='#5555FF'><</font>assignment<font color='#5555FF'>></font> cpt_grid_assignments;
std::string graph_file_name;
<b>}</b>;
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>int</u></font> <b><a name='main'></a>main</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// create our window
</font> main_window my_window;
<font color='#009900'>// tell our window to put itself on the screen
</font> my_window.<font color='#BB00BB'>show</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// wait until the user closes this window before we let the program
</font> <font color='#009900'>// terminate.
</font> my_window.<font color='#BB00BB'>wait_until_closed</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>#ifdef</font> WIN32
<font color='#009900'>// If you use main() as your entry point when building a program on MS Windows then
</font><font color='#009900'>// there will be a black console window associated with your application. If you
</font><font color='#009900'>// want your application to not have this console window then you need to build
</font><font color='#009900'>// using the WinMain() entry point as shown below and also set your compiler to
</font><font color='#009900'>// produce a "Windows" project instead of a "Console" project. In visual studio
</font><font color='#009900'>// this can be accomplished by going to project->properties->general configuration->
</font><font color='#009900'>// Linker->System->SubSystem and selecting Windows instead of Console.
</font><font color='#009900'>//
</font><font color='#0000FF'><u>int</u></font> WINAPI <b><a name='WinMain'></a>WinMain</b> <font face='Lucida Console'>(</font>HINSTANCE, HINSTANCE, PSTR cmds, <font color='#0000FF'><u>int</u></font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>main</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>return</font> <font color='#979000'>0</font>;
<b>}</b>
<font color='#0000FF'>#endif</font>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// Methods from the main_window object
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
main_window::
<b><a name='main_window'></a>main_window</b><font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font> :
color_non_evidence<font face='Lucida Console'>(</font><font color='#979000'>0</font>,<font color='#979000'>0</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font>,
color_default_bg<font face='Lucida Console'>(</font><font color='#979000'>255</font>,<font color='#979000'>255</font>,<font color='#979000'>255</font><font face='Lucida Console'>)</font>,
color_evidence<font face='Lucida Console'>(</font><font color='#979000'>100</font>,<font color='#979000'>200</font>,<font color='#979000'>100</font><font face='Lucida Console'>)</font>,
color_error<font face='Lucida Console'>(</font><font color='#979000'>255</font>,<font color='#979000'>0</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font>,
color_gray<font face='Lucida Console'>(</font><font color='#979000'>210</font>,<font color='#979000'>210</font>,<font color='#979000'>210</font><font face='Lucida Console'>)</font>,
graph_modified_since_last_recalc<font face='Lucida Console'>(</font><font color='#979000'>true</font><font face='Lucida Console'>)</font>,
btn_calculate<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
sel_node_is_evidence<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
graph_drawer<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
sel_node_index<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
sel_node_num_values_label <font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
sel_node_text_label<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
sel_node_evidence_label<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
mbar<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
selected_node_rect<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
tables<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
sel_node_num_values<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
sel_node_text<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
sel_node_evidence<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
cpt_grid<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
ppt_grid<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
selected_node_index<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
node_is_selected<font face='Lucida Console'>(</font><font color='#979000'>false</font><font face='Lucida Console'>)</font>,
cpt_group<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>,
ppt_group<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// Note that all the GUI widgets take a reference to the window that contains them
</font> <font color='#009900'>// as their constructor argument. This is a universal feature of GUI widgets in the
</font> <font color='#009900'>// dlib library.
</font>
<font color='#BB00BB'>set_title</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Bayesian Network Utility</font>"<font face='Lucida Console'>)</font>;
<font color='#009900'>// position the widget that is responsible for drawing the directed graph, the graph_drawer,
</font> <font color='#009900'>// just below the mbar (menu bar) widget.
</font> graph_drawer.<font color='#BB00BB'>set_pos</font><font face='Lucida Console'>(</font><font color='#979000'>5</font>,mbar.<font color='#BB00BB'>bottom</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>5</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>set_size</font><font face='Lucida Console'>(</font><font color='#979000'>750</font>,<font color='#979000'>400</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// register the event handlers with their respective widgets
</font> btn_calculate.<font color='#BB00BB'>set_click_handler</font> <font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::recalculate_probabilities<font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_text_modified_handler</font> <font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_cpt_grid_modified<font face='Lucida Console'>)</font>;
graph_drawer.<font color='#BB00BB'>set_graph_modified_handler</font> <font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_graph_modified<font face='Lucida Console'>)</font>;
graph_drawer.<font color='#BB00BB'>set_node_deleted_handler</font> <font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_node_deleted<font face='Lucida Console'>)</font>;
graph_drawer.<font color='#BB00BB'>set_node_deselected_handler</font> <font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_node_deselected<font face='Lucida Console'>)</font>;
graph_drawer.<font color='#BB00BB'>set_node_selected_handler</font> <font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_node_selected<font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>set_text_modified_handler</font> <font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_sel_node_evidence_modified<font face='Lucida Console'>)</font>;
sel_node_is_evidence.<font color='#BB00BB'>set_click_handler</font> <font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_evidence_toggled<font face='Lucida Console'>)</font>;
sel_node_num_values.<font color='#BB00BB'>set_text_modified_handler</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_sel_node_num_values_modified<font face='Lucida Console'>)</font>;
sel_node_text.<font color='#BB00BB'>set_text_modified_handler</font> <font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_sel_node_text_modified<font face='Lucida Console'>)</font>;
<font color='#009900'>// now set the text of some of our buttons and labels
</font> btn_calculate.<font color='#BB00BB'>set_name</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Recalculate posterior probability table</font>"<font face='Lucida Console'>)</font>;
selected_node_rect.<font color='#BB00BB'>set_name</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Selected node</font>"<font face='Lucida Console'>)</font>;
sel_node_evidence_label.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>evidence value:</font>"<font face='Lucida Console'>)</font>;
sel_node_is_evidence.<font color='#BB00BB'>set_name</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>is evidence</font>"<font face='Lucida Console'>)</font>;
sel_node_num_values_label.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Number of values: </font>"<font face='Lucida Console'>)</font>;
sel_node_text_label.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Node label:</font>"<font face='Lucida Console'>)</font>;
<font color='#009900'>// Now setup the tabbed display. It will have two tabs, one for the conditional
</font> <font color='#009900'>// probability table and one for the posterior probability table.
</font> tables.<font color='#BB00BB'>set_number_of_tabs</font><font face='Lucida Console'>(</font><font color='#979000'>2</font><font face='Lucida Console'>)</font>;
tables.<font color='#BB00BB'>set_tab_name</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,"<font color='#CC0000'>Conditional probability table</font>"<font face='Lucida Console'>)</font>;
tables.<font color='#BB00BB'>set_tab_name</font><font face='Lucida Console'>(</font><font color='#979000'>1</font>,"<font color='#CC0000'>Posterior probability table</font>"<font face='Lucida Console'>)</font>;
cpt_group.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font>cpt_grid,<font color='#979000'>0</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
ppt_group.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font>ppt_grid,<font color='#979000'>0</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
tables.<font color='#BB00BB'>set_tab_group</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,cpt_group<font face='Lucida Console'>)</font>;
tables.<font color='#BB00BB'>set_tab_group</font><font face='Lucida Console'>(</font><font color='#979000'>1</font>,ppt_group<font face='Lucida Console'>)</font>;
<font color='#009900'>// Now setup the menu bar. We will have two menus. A File and Help menu.
</font> mbar.<font color='#BB00BB'>set_number_of_menus</font><font face='Lucida Console'>(</font><font color='#979000'>2</font><font face='Lucida Console'>)</font>;
mbar.<font color='#BB00BB'>set_menu_name</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,"<font color='#CC0000'>File</font>",'<font color='#FF0000'>F</font>'<font face='Lucida Console'>)</font>;
mbar.<font color='#BB00BB'>set_menu_name</font><font face='Lucida Console'>(</font><font color='#979000'>1</font>,"<font color='#CC0000'>Help</font>",'<font color='#FF0000'>H</font>'<font face='Lucida Console'>)</font>;
<font color='#009900'>// add the entries to the File menu.
</font> mbar.<font color='#BB00BB'>menu</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>add_menu_item</font><font face='Lucida Console'>(</font><font color='#BB00BB'>menu_item_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Open</font>", <font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_menu_file_open, '<font color='#FF0000'>O</font>'<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
mbar.<font color='#BB00BB'>menu</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>add_menu_item</font><font face='Lucida Console'>(</font><font color='#BB00BB'>menu_item_separator</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
mbar.<font color='#BB00BB'>menu</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>add_menu_item</font><font face='Lucida Console'>(</font><font color='#BB00BB'>menu_item_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Save</font>", <font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_menu_file_save, '<font color='#FF0000'>S</font>'<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
mbar.<font color='#BB00BB'>menu</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>add_menu_item</font><font face='Lucida Console'>(</font><font color='#BB00BB'>menu_item_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Save As</font>",<font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_menu_file_save_as, '<font color='#FF0000'>a</font>'<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
mbar.<font color='#BB00BB'>menu</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>add_menu_item</font><font face='Lucida Console'>(</font><font color='#BB00BB'>menu_item_separator</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
mbar.<font color='#BB00BB'>menu</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>add_menu_item</font><font face='Lucida Console'>(</font><font color='#BB00BB'>menu_item_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Quit</font>", <font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_menu_file_quit, '<font color='#FF0000'>Q</font>'<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// Add the entries to the Help menu.
</font> mbar.<font color='#BB00BB'>menu</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>add_menu_item</font><font face='Lucida Console'>(</font><font color='#BB00BB'>menu_item_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Help</font>", <font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_menu_help_help, '<font color='#FF0000'>e</font>'<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
mbar.<font color='#BB00BB'>menu</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>add_menu_item</font><font face='Lucida Console'>(</font><font color='#BB00BB'>menu_item_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>About</font>", <font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_menu_help_about, '<font color='#FF0000'>A</font>'<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// call our helper functions and window resize event to get the widgets
</font> <font color='#009900'>// to all arrange themselves correctly in our window.
</font> <font color='#BB00BB'>no_node_selected</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>on_window_resized</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
main_window::
~<b><a name='main_window'></a>main_window</b><font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// You should always call close_window() in the destructor of window
</font> <font color='#009900'>// objects to ensure that no events will be sent to this window while
</font> <font color='#009900'>// it is being destructed.
</font> <font color='#BB00BB'>close_window</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// Private methods from the main_window object
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> main_window::
<b><a name='load_selected_node_tables_into_ppt_grid'></a>load_selected_node_tables_into_ppt_grid</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// This function just takes the currently selected graph node and loads
</font> <font color='#009900'>// its posterior probabilities into the ppt_graph widget.
</font> node_type<font color='#5555FF'>&</font> node <font color='#5555FF'>=</font> graph_drawer.<font color='#BB00BB'>graph_node</font><font face='Lucida Console'>(</font>selected_node_index<font face='Lucida Console'>)</font>;
ppt_grid.<font color='#BB00BB'>set_grid_size</font><font face='Lucida Console'>(</font><font color='#979000'>2</font>,node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// load the top row of the table into the grid. This row is the "title bar" row
</font> <font color='#009900'>// that tells you what each column contains.
</font> <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> col <font color='#5555FF'>=</font> <font color='#979000'>0</font>; col <font color='#5555FF'><</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>col<font face='Lucida Console'>)</font>
<b>{</b>
ppt_grid.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,col,"<font color='#CC0000'>P(node=</font>" <font color='#5555FF'>+</font> <font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font>col<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> "<font color='#CC0000'>)</font>"<font face='Lucida Console'>)</font>;
ppt_grid.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,col,<font color='#BB00BB'>rgb_pixel</font><font face='Lucida Console'>(</font><font color='#979000'>150</font>,<font color='#979000'>150</font>,<font color='#979000'>250</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
ppt_grid.<font color='#BB00BB'>set_editable</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,col,<font color='#979000'>false</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// If we have a solution to the network on hand then load the probabilities
</font> <font color='#009900'>// from that into the table
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>solution<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// get the probability distribution for the currently selected node out
</font> <font color='#009900'>// of the solution.
</font> <font color='#0000FF'>const</font> matrix<font color='#5555FF'><</font><font color='#0000FF'><u>double</u></font>,<font color='#979000'>1</font><font color='#5555FF'>></font> prob <font color='#5555FF'>=</font> solution<font color='#5555FF'>-</font><font color='#5555FF'>></font><font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>selected_node_index<font face='Lucida Console'>)</font>;
<font color='#009900'>// now load the probabilities into the ppt_grid so the user can see them.
</font> <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> col <font color='#5555FF'>=</font> <font color='#979000'>0</font>; col <font color='#5555FF'><</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>col<font face='Lucida Console'>)</font>
<b>{</b>
ppt_grid.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font><font color='#979000'>1</font>,col,<font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font><font color='#BB00BB'>prob</font><font face='Lucida Console'>(</font>col<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<font color='#009900'>// make the second row of the table non-editable have a color that indicates
</font> <font color='#009900'>// that to the user
</font> <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> col <font color='#5555FF'>=</font> <font color='#979000'>0</font>; col <font color='#5555FF'><</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>col<font face='Lucida Console'>)</font>
<b>{</b>
ppt_grid.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font><font color='#979000'>1</font>,col,color_gray<font face='Lucida Console'>)</font>;
ppt_grid.<font color='#BB00BB'>set_editable</font><font face='Lucida Console'>(</font><font color='#979000'>1</font>,col,<font color='#979000'>false</font><font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> main_window::
<b><a name='load_selected_node_tables_into_cpt_grid'></a>load_selected_node_tables_into_cpt_grid</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// This function just takes the conditional probability table in the
</font> <font color='#009900'>// currently selected graph node and puts it into the cpt_grid widget.
</font>
node_type<font color='#5555FF'>&</font> node <font color='#5555FF'>=</font> graph_drawer.<font color='#BB00BB'>graph_node</font><font face='Lucida Console'>(</font>selected_node_index<font face='Lucida Console'>)</font>;
<font color='#BB00BB'>initialize_node_cpt_if_necessary</font><font face='Lucida Console'>(</font>selected_node_index<font face='Lucida Console'>)</font>;
cpt_grid_assignments.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// figure out how many rows there should be in the cpt
</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> cpt_rows <font color='#5555FF'>=</font> <font color='#979000'>1</font>;
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> node.<font color='#BB00BB'>number_of_parents</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
<b>{</b>
cpt_rows <font color='#5555FF'>*</font><font color='#5555FF'>=</font> node.<font color='#BB00BB'>parent</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> cpt_cols <font color='#5555FF'>=</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_grid_size</font><font face='Lucida Console'>(</font>cpt_rows<font color='#5555FF'>+</font><font color='#979000'>1</font>, cpt_cols<font color='#5555FF'>+</font> node.<font color='#BB00BB'>number_of_parents</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> num_cols <font color='#5555FF'>=</font> cpt_grid.<font color='#BB00BB'>number_of_columns</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// fill in the top row of the grid that shows which parent node the left hand columns go with
</font> assignment <font color='#BB00BB'>a</font><font face='Lucida Console'>(</font><font color='#BB00BB'>node_first_parent_assignment</font><font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>graph</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,selected_node_index<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> col <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
a.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>a.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
<b>{</b>
cpt_grid.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,col,<font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font>a.<font color='#BB00BB'>element</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>key</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> "<font color='#CC0000'>: </font>" <font color='#5555FF'>+</font> graph_drawer.<font color='#BB00BB'>node_label</font><font face='Lucida Console'>(</font>a.<font color='#BB00BB'>element</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>key</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,col,<font color='#BB00BB'>rgb_pixel</font><font face='Lucida Console'>(</font><font color='#979000'>120</font>,<font color='#979000'>210</font>,<font color='#979000'>210</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_editable</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,col,<font color='#979000'>false</font><font face='Lucida Console'>)</font>;
<font color='#5555FF'>+</font><font color='#5555FF'>+</font>col;
<b>}</b>
<font color='#009900'>// fill in the top row of the grid that shows which probability the right hand columns go with
</font> <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font>col <font color='#5555FF'>=</font> node.<font color='#BB00BB'>number_of_parents</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; col <font color='#5555FF'><</font> num_cols; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>col<font face='Lucida Console'>)</font>
<b>{</b>
cpt_grid.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,col,"<font color='#CC0000'>P(node=</font>" <font color='#5555FF'>+</font> <font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font>col<font color='#5555FF'>-</font>node.<font color='#BB00BB'>number_of_parents</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> "<font color='#CC0000'>)</font>"<font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,col,<font color='#BB00BB'>rgb_pixel</font><font face='Lucida Console'>(</font><font color='#979000'>150</font>,<font color='#979000'>150</font>,<font color='#979000'>250</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_editable</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,col,<font color='#979000'>false</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// now loop over all the possible parent assignments for this node
</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> num_values <font color='#5555FF'>=</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> row <font color='#5555FF'>=</font> <font color='#979000'>1</font>;
<font color='#0000FF'>do</font>
<b>{</b>
col <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
<font color='#009900'>// fill in the left side of the grid row that shows what the parent assignment is
</font> a.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>a.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
<b>{</b>
cpt_grid.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>row,col,<font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font>a.<font color='#BB00BB'>element</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>value</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>row,col,<font color='#BB00BB'>rgb_pixel</font><font face='Lucida Console'>(</font><font color='#979000'>180</font>,<font color='#979000'>255</font>,<font color='#979000'>255</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_editable</font><font face='Lucida Console'>(</font>row,col,<font color='#979000'>false</font><font face='Lucida Console'>)</font>;
<font color='#5555FF'>+</font><font color='#5555FF'>+</font>col;
<b>}</b>
<font color='#009900'>// fill in the right side of the grid row that shows what the conditional probabilities are
</font> <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> value <font color='#5555FF'>=</font> <font color='#979000'>0</font>; value <font color='#5555FF'><</font> num_values; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>value<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> prob <font color='#5555FF'>=</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>value,a<font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>row,col,<font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font>prob<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#5555FF'>+</font><font color='#5555FF'>+</font>col;
<b>}</b>
<font color='#009900'>// save this assignment so we can use it later to modify the node's
</font> <font color='#009900'>// conditional probability table if the user modifies the cpt_grid
</font> cpt_grid_assignments.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font>a<font face='Lucida Console'>)</font>;
<font color='#5555FF'>+</font><font color='#5555FF'>+</font>row;
<b>}</b> <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>node_next_parent_assignment</font><font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>graph</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,selected_node_index,a<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> main_window::
<b><a name='initialize_node_cpt_if_necessary'></a>initialize_node_cpt_if_necessary</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> index
<font face='Lucida Console'>)</font>
<b>{</b>
node_type<font color='#5555FF'>&</font> node <font color='#5555FF'>=</font> graph_drawer.<font color='#BB00BB'>graph_node</font><font face='Lucida Console'>(</font>index<font face='Lucida Console'>)</font>;
<font color='#009900'>// if the cpt for this node isn't properly filled out then let's clear it out
</font> <font color='#009900'>// and populate it with some reasonable default values
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>node_cpt_filled_out</font><font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>graph</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, index<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
<b>{</b>
node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>empty_table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> num_values <font color='#5555FF'>=</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// loop over all the possible parent assignments for this node and fill them out
</font> <font color='#009900'>// with reasonable default values
</font> assignment <font color='#BB00BB'>a</font><font face='Lucida Console'>(</font><font color='#BB00BB'>node_first_parent_assignment</font><font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>graph</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, index<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>do</font>
<b>{</b>
<font color='#009900'>// set the first value to have probability 1
</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>set_probability</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>, a, <font color='#979000'>1.0</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// set all the other values to have probability 0
</font> <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> value <font color='#5555FF'>=</font> <font color='#979000'>1</font>; value <font color='#5555FF'><</font> num_values; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>value<font face='Lucida Console'>)</font>
node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>set_probability</font><font face='Lucida Console'>(</font>value, a, <font color='#979000'>0</font><font face='Lucida Console'>)</font>;
<b>}</b> <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>node_next_parent_assignment</font><font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>graph</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, index,a<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> main_window::
<b><a name='no_node_selected'></a>no_node_selected</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// Make it so that no node is selected on the gui. Do this by disabling things
</font> <font color='#009900'>// and clearing out text fields and so forth.
</font>
node_is_selected <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
tables.<font color='#BB00BB'>disable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>disable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_is_evidence.<font color='#BB00BB'>disable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_index.<font color='#BB00BB'>disable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_evidence_label.<font color='#BB00BB'>disable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_text_label.<font color='#BB00BB'>disable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_text.<font color='#BB00BB'>disable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_index.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>index:</font>"<font face='Lucida Console'>)</font>;
sel_node_num_values_label.<font color='#BB00BB'>disable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_num_values.<font color='#BB00BB'>disable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_grid_size</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
ppt_grid.<font color='#BB00BB'>set_grid_size</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
sel_node_is_evidence.<font color='#BB00BB'>set_unchecked</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_text.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'></font>"<font face='Lucida Console'>)</font>;
sel_node_num_values.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'></font>"<font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'></font>"<font face='Lucida Console'>)</font>;
sel_node_num_values.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>color_default_bg<font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>color_default_bg<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> main_window::
<b><a name='recalculate_probabilities'></a>recalculate_probabilities</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// clear out the current solution
</font> solution.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>graph_is_connected</font><font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>graph</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>message_box</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Error</font>","<font color='#CC0000'>Your graph has nodes that are completely disconnected from the other nodes.\n</font>"
"<font color='#CC0000'>You must connect them somehow</font>"<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>graph</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>></font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>graph_modified_since_last_recalc<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// make sure all the cpts are filled out
</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> num_nodes <font color='#5555FF'>=</font> graph_drawer.<font color='#BB00BB'>graph</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>number_of_nodes</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> num_nodes; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>initialize_node_cpt_if_necessary</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// remake the join tree for this graph
</font> <font color='#BB00BB'>create_moral_graph</font><font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>graph</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, join_tree<font face='Lucida Console'>)</font>;
<font color='#BB00BB'>create_join_tree</font><font face='Lucida Console'>(</font>join_tree, join_tree<font face='Lucida Console'>)</font>;
graph_modified_since_last_recalc <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
<b>}</b>
<font color='#009900'>// create a solution to this bayesian network using the join tree algorithm
</font> solution.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font color='#0000FF'>new</font> <font color='#BB00BB'>bayesian_network_join_tree</font><font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>graph</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, join_tree<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>node_is_selected<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>load_selected_node_tables_into_ppt_grid</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// Event handling methods from the main_window object
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user selects a file with a saved
</font><font color='#009900'>// bayesian network in it.
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_open_file_selected'></a>on_open_file_selected</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> std::string<font color='#5555FF'>&</font> file_name
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>try</font>
<b>{</b>
<font color='#BB00BB'>no_node_selected</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
ifstream <font color='#BB00BB'>fin</font><font face='Lucida Console'>(</font>file_name.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, ios::binary<font face='Lucida Console'>)</font>;
graph_drawer.<font color='#BB00BB'>load_graph</font><font face='Lucida Console'>(</font>fin<font face='Lucida Console'>)</font>;
graph_file_name <font color='#5555FF'>=</font> file_name;
<font color='#BB00BB'>set_title</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Bayesian Network Utility - </font>" <font color='#5555FF'>+</font> <font color='#BB00BB'>right_substr</font><font face='Lucida Console'>(</font>file_name,"<font color='#CC0000'>\\/</font>"<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>...<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>message_box</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Error</font>", "<font color='#CC0000'>Unable to load graph file </font>" <font color='#5555FF'>+</font> file_name<font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user selects from the menu bar File->Open
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_menu_file_open'></a>on_menu_file_open</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// display a file chooser window and when the user choses a file
</font> <font color='#009900'>// call the on_open_file_selected() function
</font> <font color='#BB00BB'>open_existing_file_box</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_open_file_selected<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user selects from the menu bar File->Save
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_menu_file_save'></a>on_menu_file_save</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// if we don't currently have any file name associated with our graph
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>graph_file_name.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// display a file chooser window and when the user choses a file
</font> <font color='#009900'>// call the on_save_file_selected() function
</font> <font color='#BB00BB'>save_file_box</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_save_file_selected<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'>else</font>
<b>{</b>
<font color='#009900'>// we know what file to open so just do that and save the graph to it
</font> ofstream <font color='#BB00BB'>fout</font><font face='Lucida Console'>(</font>graph_file_name.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, ios::binary<font face='Lucida Console'>)</font>;
graph_drawer.<font color='#BB00BB'>save_graph</font><font face='Lucida Console'>(</font>fout<font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user choses which file to save the graph to
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_save_file_selected'></a>on_save_file_selected</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> std::string<font color='#5555FF'>&</font> file_name
<font face='Lucida Console'>)</font>
<b>{</b>
ofstream <font color='#BB00BB'>fout</font><font face='Lucida Console'>(</font>file_name.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, ios::binary<font face='Lucida Console'>)</font>;
graph_drawer.<font color='#BB00BB'>save_graph</font><font face='Lucida Console'>(</font>fout<font face='Lucida Console'>)</font>;
graph_file_name <font color='#5555FF'>=</font> file_name;
<font color='#BB00BB'>set_title</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Bayesian Network Utility - </font>" <font color='#5555FF'>+</font> <font color='#BB00BB'>right_substr</font><font face='Lucida Console'>(</font>file_name,"<font color='#CC0000'>\\/</font>"<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user selects from the menu bar File->Save As
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_menu_file_save_as'></a>on_menu_file_save_as</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// display a file chooser window and when the user choses a file
</font> <font color='#009900'>// call the on_save_file_selected() function
</font> <font color='#BB00BB'>save_file_box</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#0000FF'>this</font>, <font color='#5555FF'>&</font>main_window::on_save_file_selected<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user selects from the menu bar File->Quit
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_menu_file_quit'></a>on_menu_file_quit</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>close_window</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user selects from the menu bar Help->Help
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_menu_help_help'></a>on_menu_help_help</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>message_box</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Help</font>",
"<font color='#CC0000'>To create new nodes right click on the drawing area.\n</font>"
"<font color='#CC0000'>To create edges select the parent node and then shift+left click on the child node.\n</font>"
"<font color='#CC0000'>To remove nodes or edges select them by left clicking and then press the delete key.</font>"<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user selects from the menu bar Help->About
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_menu_help_about'></a>on_menu_help_about</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>message_box</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>About</font>","<font color='#CC0000'>This application is the GUI front end to the dlib C++ Library's\n</font>"
"<font color='#CC0000'>Bayesian Network inference utilities\n\n</font>"
"<font color='#CC0000'>Version 1.2\n\n</font>"
"<font color='#CC0000'>See http://dlib.net for updates</font>"<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user modifies the graph_drawer widget. That is,
</font><font color='#009900'>// when the user adds or removes an edge or node in the graph.
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_graph_modified'></a>on_graph_modified</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// make note of the modification
</font> graph_modified_since_last_recalc <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
<font color='#009900'>// clear out the solution object since we will need to recalculate it
</font> <font color='#009900'>// since the graph changed
</font> solution.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user modifies the evidence value for a node
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_sel_node_evidence_modified'></a>on_sel_node_evidence_modified</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// make a reference to the node in the graph that is currently selected
</font> node_type<font color='#5555FF'>&</font> node <font color='#5555FF'>=</font> graph_drawer.<font color='#BB00BB'>graph_node</font><font face='Lucida Console'>(</font>selected_node_index<font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> value;
<font color='#0000FF'>try</font>
<b>{</b>
<font color='#009900'>// get the numerical value of the new evidence value. Here we are taking
</font> <font color='#009900'>// the string from the text field and casting it to an unsigned long.
</font> value <font color='#5555FF'>=</font> sa <font color='#5555FF'>=</font> <font color='#BB00BB'>trim</font><font face='Lucida Console'>(</font>sel_node_evidence.<font color='#BB00BB'>text</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>string_cast_error<font color='#5555FF'>&</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// if the user put something that isn't an integer into the
</font> <font color='#009900'>// text field then make it have a different background color
</font> <font color='#009900'>// so that they can easily see this.
</font> sel_node_evidence.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>color_error<font face='Lucida Console'>)</font>;
<font color='#0000FF'>return</font>;
<b>}</b>
<font color='#009900'>// validate the input from the user and store it in the selected node
</font> <font color='#009900'>// if it is ok
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>value <font color='#5555FF'>></font><font color='#5555FF'>=</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
<b>{</b>
sel_node_evidence.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>color_error<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'>else</font>
<b>{</b>
node.data.<font color='#BB00BB'>set_value</font><font face='Lucida Console'>(</font>value<font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>color_default_bg<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// clear out the solution to the graph since we now need
</font> <font color='#009900'>// to recalculate it.
</font> solution.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user modifies the number of evidence values for
</font><font color='#009900'>// a node.
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_sel_node_num_values_modified'></a>on_sel_node_num_values_modified</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// make a reference to the node in the graph that is currently selected
</font> node_type<font color='#5555FF'>&</font> node <font color='#5555FF'>=</font> graph_drawer.<font color='#BB00BB'>graph_node</font><font face='Lucida Console'>(</font>selected_node_index<font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> num_values;
<font color='#0000FF'>try</font>
<b>{</b>
<font color='#009900'>// get the number of values out of the text field.
</font> num_values <font color='#5555FF'>=</font> sa <font color='#5555FF'>=</font> <font color='#BB00BB'>trim</font><font face='Lucida Console'>(</font>sel_node_num_values.<font color='#BB00BB'>text</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>string_cast_error<font color='#5555FF'>&</font><font face='Lucida Console'>)</font>
<b>{</b>
sel_node_num_values.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>color_error<font face='Lucida Console'>)</font>;
<font color='#0000FF'>return</font>;
<b>}</b>
<font color='#009900'>// validate the input from the user to make sure it is something reasonable
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>num_values <font color='#5555FF'><</font> <font color='#979000'>2</font> <font color='#5555FF'>|</font><font color='#5555FF'>|</font> num_values <font color='#5555FF'>></font> <font color='#979000'>100</font><font face='Lucida Console'>)</font>
<b>{</b>
sel_node_num_values.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>color_error<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'>else</font>
<b>{</b>
<font color='#009900'>// update the graph
</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>set_num_values</font><font face='Lucida Console'>(</font>num_values<font face='Lucida Console'>)</font>;
graph_modified_since_last_recalc <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
sel_node_num_values.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>color_default_bg<font face='Lucida Console'>)</font>;
<font color='#BB00BB'>on_sel_node_evidence_modified</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// also make sure the evidence value of this node makes sense still
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>node.data.<font color='#BB00BB'>is_evidence</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&</font><font color='#5555FF'>&</font> node.data.<font color='#BB00BB'>value</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>></font><font color='#5555FF'>=</font> num_values<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// just set it to zero
</font> node.data.<font color='#BB00BB'>set_value</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
solution.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// call these functions so that the conditional and posterior probability
</font> <font color='#009900'>// tables get updated
</font> <font color='#BB00BB'>load_selected_node_tables_into_cpt_grid</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>load_selected_node_tables_into_ppt_grid</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user modifies the cpt_grid (i.e. the conditional
</font><font color='#009900'>// probability table widget)
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_cpt_grid_modified'></a>on_cpt_grid_modified</b><font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> row, <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> col<font face='Lucida Console'>)</font>
<b>{</b>
node_type<font color='#5555FF'>&</font> node <font color='#5555FF'>=</font> graph_drawer.<font color='#BB00BB'>graph_node</font><font face='Lucida Console'>(</font>selected_node_index<font face='Lucida Console'>)</font>;
solution.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>double</u></font> prob;
<font color='#0000FF'>try</font>
<b>{</b>
<font color='#009900'>// get the new value out of the table
</font> prob <font color='#5555FF'>=</font> sa <font color='#5555FF'>=</font> cpt_grid.<font color='#BB00BB'>text</font><font face='Lucida Console'>(</font>row,col<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>string_cast_error<font color='#5555FF'>&</font><font face='Lucida Console'>)</font>
<b>{</b>
cpt_grid.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>row,col,color_error<font face='Lucida Console'>)</font>;
<font color='#0000FF'>return</font>;
<b>}</b>
<font color='#009900'>// validate the value
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>prob <font color='#5555FF'><</font> <font color='#979000'>0</font> <font color='#5555FF'>|</font><font color='#5555FF'>|</font> prob <font color='#5555FF'>></font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
<b>{</b>
cpt_grid.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>row,col,color_error<font face='Lucida Console'>)</font>;
<font color='#0000FF'>return</font>;
<b>}</b>
<font color='#009900'>// the value of this node that is having its conditional probability
</font> <font color='#009900'>// updated
</font> <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> cur_val <font color='#5555FF'>=</font> col<font color='#5555FF'>-</font>node.<font color='#BB00BB'>number_of_parents</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>set_probability</font><font face='Lucida Console'>(</font>cur_val, cpt_grid_assignments[row<font color='#5555FF'>-</font><font color='#979000'>1</font>], prob<font face='Lucida Console'>)</font>;
<font color='#009900'>// sum the probabilities in the cpt and modify the last one such that they all
</font> <font color='#009900'>// sum to 1. We are excluding either the first or last element from the sum
</font> <font color='#009900'>// because we are going to set it equal to 1-sum below.
</font> <font color='#0000FF'><u>double</u></font> sum <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>cur_val <font color='#5555FF'>!</font><font color='#5555FF'>=</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#979000'>1</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
sum <font color='#5555FF'>+</font><font color='#5555FF'>=</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>i, cpt_grid_assignments[row<font color='#5555FF'>-</font><font color='#979000'>1</font>]<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'>else</font>
<b>{</b>
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>1</font>; i <font color='#5555FF'><</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
sum <font color='#5555FF'>+</font><font color='#5555FF'>=</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>probability</font><font face='Lucida Console'>(</font>i, cpt_grid_assignments[row<font color='#5555FF'>-</font><font color='#979000'>1</font>]<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// make sure all the probabilities sum to 1
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>sum <font color='#5555FF'>></font> <font color='#979000'>1.0</font><font face='Lucida Console'>)</font>
<b>{</b>
cpt_grid.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>row,cpt_grid.<font color='#BB00BB'>number_of_columns</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#979000'>1</font>,color_error<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'>else</font>
<b>{</b>
<font color='#009900'>// edit one of the other elements in the table to ensure that the probabilities still sum to 1
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>cur_val <font color='#5555FF'>=</font><font color='#5555FF'>=</font> node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>
<b>{</b>
node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>set_probability</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>, cpt_grid_assignments[row<font color='#5555FF'>-</font><font color='#979000'>1</font>], <font color='#979000'>1</font><font color='#5555FF'>-</font>sum<font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>row,node.<font color='#BB00BB'>number_of_parents</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,<font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font color='#5555FF'>-</font>sum<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'>else</font>
<b>{</b>
node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>set_probability</font><font face='Lucida Console'>(</font>node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#979000'>1</font>, cpt_grid_assignments[row<font color='#5555FF'>-</font><font color='#979000'>1</font>], <font color='#979000'>1</font><font color='#5555FF'>-</font>sum<font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>row,cpt_grid.<font color='#BB00BB'>number_of_columns</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#979000'>1</font>,<font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font color='#5555FF'>-</font>sum<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
cpt_grid.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>row,cpt_grid.<font color='#BB00BB'>number_of_columns</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#979000'>1</font>,color_default_bg<font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>row,col,color_default_bg<font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user resizes the main_window. Note that unlike the other
</font><font color='#009900'>// events, this event is part of the drawable_window base class that main_window inherits from.
</font><font color='#009900'>// So you won't see any statements in the constructor that say "register the main_window::on_window_resized function"
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_window_resized'></a>on_window_resized</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// when you override any of the drawable_window events you have to make sure you
</font> <font color='#009900'>// call the drawable_window's version of them because it needs to process
</font> <font color='#009900'>// the events as well. So we do that here.
</font> drawable_window::<font color='#BB00BB'>on_window_resized</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// The rest of this function positions the widgets on the window
</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> width,height;
<font color='#BB00BB'>get_size</font><font face='Lucida Console'>(</font>width,height<font face='Lucida Console'>)</font>;
<font color='#009900'>// Don't do anything if the user just made the window too small. That is, leave
</font> <font color='#009900'>// the widgets where they are.
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>width <font color='#5555FF'><</font> <font color='#979000'>500</font> <font color='#5555FF'>|</font><font color='#5555FF'>|</font> height <font color='#5555FF'><</font> <font color='#979000'>350</font><font face='Lucida Console'>)</font>
<font color='#0000FF'>return</font>;
<font color='#009900'>// Set the size of the probability tables and the drawing area for the graph
</font> graph_drawer.<font color='#BB00BB'>set_size</font><font face='Lucida Console'>(</font>width<font color='#5555FF'>-</font><font color='#979000'>370</font>,height<font color='#5555FF'>-</font><font color='#979000'>10</font><font color='#5555FF'>-</font>mbar.<font color='#BB00BB'>height</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
cpt_grid.<font color='#BB00BB'>set_size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>width<font color='#5555FF'>-</font>graph_drawer.<font color='#BB00BB'>width</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#979000'>35</font>,height<font color='#5555FF'>-</font><font color='#979000'>237</font><font face='Lucida Console'>)</font>;
ppt_grid.<font color='#BB00BB'>set_size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>width<font color='#5555FF'>-</font>graph_drawer.<font color='#BB00BB'>width</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#979000'>35</font>,height<font color='#5555FF'>-</font><font color='#979000'>237</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// tell the tabbed display to make itself just the right size to contain
</font> <font color='#009900'>// the two probability tables.
</font> tables.<font color='#BB00BB'>fit_to_contents</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// Now position all the widgets in the window. Note that much of the positioning
</font> <font color='#009900'>// is relative to other widgets. This part of the code I just figured out by
</font> <font color='#009900'>// trying stuff and rerunning the program to see if it looked nice.
</font> sel_node_index.<font color='#BB00BB'>set_pos</font><font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>right</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>14</font>,graph_drawer.<font color='#BB00BB'>top</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>18</font><font face='Lucida Console'>)</font>;
sel_node_text_label.<font color='#BB00BB'>set_pos</font><font face='Lucida Console'>(</font>sel_node_index.<font color='#BB00BB'>left</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,sel_node_index.<font color='#BB00BB'>bottom</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>5</font><font face='Lucida Console'>)</font>;
sel_node_text.<font color='#BB00BB'>set_pos</font><font face='Lucida Console'>(</font>sel_node_text_label.<font color='#BB00BB'>right</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>5</font>,sel_node_index.<font color='#BB00BB'>bottom</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
sel_node_num_values_label.<font color='#BB00BB'>set_pos</font><font face='Lucida Console'>(</font>sel_node_index.<font color='#BB00BB'>left</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, sel_node_text.<font color='#BB00BB'>bottom</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>5</font><font face='Lucida Console'>)</font>;
sel_node_num_values.<font color='#BB00BB'>set_pos</font><font face='Lucida Console'>(</font>sel_node_num_values_label.<font color='#BB00BB'>right</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, sel_node_text.<font color='#BB00BB'>bottom</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>5</font><font face='Lucida Console'>)</font>;
sel_node_is_evidence.<font color='#BB00BB'>set_pos</font><font face='Lucida Console'>(</font>sel_node_index.<font color='#BB00BB'>left</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,sel_node_num_values.<font color='#BB00BB'>bottom</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>5</font><font face='Lucida Console'>)</font>;
sel_node_evidence_label.<font color='#BB00BB'>set_pos</font><font face='Lucida Console'>(</font>sel_node_index.<font color='#BB00BB'>left</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,sel_node_is_evidence.<font color='#BB00BB'>bottom</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>5</font><font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>set_pos</font><font face='Lucida Console'>(</font>sel_node_evidence_label.<font color='#BB00BB'>right</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>5</font>,sel_node_is_evidence.<font color='#BB00BB'>bottom</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
tables.<font color='#BB00BB'>set_pos</font><font face='Lucida Console'>(</font>sel_node_index.<font color='#BB00BB'>left</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,sel_node_evidence.<font color='#BB00BB'>bottom</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>5</font><font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>set_width</font><font face='Lucida Console'>(</font>tables.<font color='#BB00BB'>right</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font>sel_node_evidence.<font color='#BB00BB'>left</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;
sel_node_text.<font color='#BB00BB'>set_width</font><font face='Lucida Console'>(</font>tables.<font color='#BB00BB'>right</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font>sel_node_text.<font color='#BB00BB'>left</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;
sel_node_num_values.<font color='#BB00BB'>set_width</font><font face='Lucida Console'>(</font>tables.<font color='#BB00BB'>right</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font>sel_node_num_values.<font color='#BB00BB'>left</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// Tell the named rectangle to position itself such that it fits around the
</font> <font color='#009900'>// tabbed display that contains the probability tables and the label at the top of the
</font> <font color='#009900'>// screen.
</font> selected_node_rect.<font color='#BB00BB'>wrap_around</font><font face='Lucida Console'>(</font>sel_node_index.<font color='#BB00BB'>get_rect</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font>
tables.<font color='#BB00BB'>get_rect</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// finally set the button to be at the bottom of the named rectangle
</font> btn_calculate.<font color='#BB00BB'>set_pos</font><font face='Lucida Console'>(</font>selected_node_rect.<font color='#BB00BB'>left</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, selected_node_rect.<font color='#BB00BB'>bottom</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>5</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called by the graph_drawer widget when the user selects a node
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_node_selected'></a>on_node_selected</b> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> n<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// make a reference to the selected node
</font> node_type<font color='#5555FF'>&</font> node <font color='#5555FF'>=</font> graph_drawer.<font color='#BB00BB'>graph_node</font><font face='Lucida Console'>(</font>n<font face='Lucida Console'>)</font>;
<font color='#009900'>// enable all the widgets related to the selected node
</font> selected_node_index <font color='#5555FF'>=</font> n;
node_is_selected <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
tables.<font color='#BB00BB'>enable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_is_evidence.<font color='#BB00BB'>enable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_index.<font color='#BB00BB'>enable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_evidence_label.<font color='#BB00BB'>enable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_text_label.<font color='#BB00BB'>enable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_text.<font color='#BB00BB'>enable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_num_values_label.<font color='#BB00BB'>enable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_num_values.<font color='#BB00BB'>enable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#009900'>// make sure the num_values field of the node's cpt is set to something valid.
</font> <font color='#009900'>// So default it to 2 if it isn't set already.
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>num_values</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font> <font color='#979000'>2</font><font face='Lucida Console'>)</font>
<b>{</b>
node.data.<font color='#BB00BB'>table</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>set_num_values</font><font face='Lucida Console'>(</font><font color='#979000'>2</font><font face='Lucida Console'>)</font>;
graph_modified_since_last_recalc <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
<b>}</b>
<font color='#009900'>// setup the evidence check box and input field
</font> sel_node_index.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>index: </font>" <font color='#5555FF'>+</font> <font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font>n<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>graph_node</font><font face='Lucida Console'>(</font>n<font face='Lucida Console'>)</font>.data.<font color='#BB00BB'>is_evidence</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
<b>{</b>
sel_node_is_evidence.<font color='#BB00BB'>set_checked</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>enable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font><font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>graph_node</font><font face='Lucida Console'>(</font>n<font face='Lucida Console'>)</font>.data.<font color='#BB00BB'>value</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'>else</font>
<b>{</b>
sel_node_is_evidence.<font color='#BB00BB'>set_unchecked</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>disable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'></font>"<font face='Lucida Console'>)</font>;
<b>}</b>
sel_node_num_values.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font><font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font><font color='#BB00BB'>node_num_values</font><font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>graph</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,n<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
sel_node_text.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>node_label</font><font face='Lucida Console'>(</font>n<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>load_selected_node_tables_into_cpt_grid</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#BB00BB'>load_selected_node_tables_into_ppt_grid</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user toggles the "is evidence" check box
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_evidence_toggled'></a>on_evidence_toggled</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>sel_node_is_evidence.<font color='#BB00BB'>is_checked</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
<b>{</b>
graph_drawer.<font color='#BB00BB'>graph_node</font><font face='Lucida Console'>(</font>selected_node_index<font face='Lucida Console'>)</font>.data.<font color='#BB00BB'>set_as_evidence</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>enable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font><font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font>graph_drawer.<font color='#BB00BB'>graph_node</font><font face='Lucida Console'>(</font>selected_node_index<font face='Lucida Console'>)</font>.data.<font color='#BB00BB'>value</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
graph_drawer.<font color='#BB00BB'>set_node_color</font><font face='Lucida Console'>(</font>selected_node_index, color_evidence<font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#0000FF'>else</font>
<b>{</b>
graph_drawer.<font color='#BB00BB'>graph_node</font><font face='Lucida Console'>(</font>selected_node_index<font face='Lucida Console'>)</font>.data.<font color='#BB00BB'>set_as_nonevidence</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>disable</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>set_text</font><font face='Lucida Console'>(</font>"<font color='#CC0000'></font>"<font face='Lucida Console'>)</font>;
sel_node_evidence.<font color='#BB00BB'>set_background_color</font><font face='Lucida Console'>(</font>color_default_bg<font face='Lucida Console'>)</font>;
graph_drawer.<font color='#BB00BB'>set_node_color</font><font face='Lucida Console'>(</font>selected_node_index, color_non_evidence<font face='Lucida Console'>)</font>;
<b>}</b>
solution.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user causes no node to be selected
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_node_deselected'></a>on_node_deselected</b> <font face='Lucida Console'>(</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>no_node_selected</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user causes a node to be deleted
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_node_deleted'></a>on_node_deleted</b> <font face='Lucida Console'>(</font> <font face='Lucida Console'>)</font>
<b>{</b>
<font color='#BB00BB'>no_node_selected</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This event is called when the user changes the text in the "node label" text field
</font><font color='#0000FF'><u>void</u></font> main_window::
<b><a name='on_sel_node_text_modified'></a>on_sel_node_text_modified</b> <font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// set the selected node's text to match whatever the user just typed in
</font> graph_drawer.<font color='#BB00BB'>set_node_label</font><font face='Lucida Console'>(</font>selected_node_index,sel_node_text.<font color='#BB00BB'>text</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
</pre></body></html> |