|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <memory> |
|
#include <sstream> |
|
#include <string> |
|
|
|
#include <dlib/gui_widgets.h> |
|
#include <dlib/directed_graph.h> |
|
#include <dlib/string.h> |
|
#include <dlib/bayes_utils.h> |
|
#include <dlib/set.h> |
|
#include <dlib/graph_utils.h> |
|
#include <dlib/stl_checked.h> |
|
|
|
|
|
using namespace std; |
|
using namespace dlib; |
|
using namespace dlib::bayes_node_utils; |
|
|
|
|
|
|
|
typedef directed_graph<bayes_node>::kernel_1a_c directed_graph_type; |
|
typedef directed_graph<bayes_node>::kernel_1a_c::node_type node_type; |
|
typedef graph<dlib::set<unsigned long>::compare_1b_c, dlib::set<unsigned long>::compare_1b_c>::kernel_1a_c join_tree_type; |
|
|
|
|
|
|
|
class main_window : public drawable_window |
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public: |
|
main_window(); |
|
~main_window(); |
|
|
|
private: |
|
|
|
|
|
|
|
void initialize_node_cpt_if_necessary ( unsigned long index ); |
|
void load_selected_node_tables_into_cpt_grid (); |
|
void load_selected_node_tables_into_ppt_grid (); |
|
void no_node_selected (); |
|
|
|
|
|
|
|
|
|
void on_cpt_grid_modified(unsigned long row, unsigned long col); |
|
void on_evidence_toggled (); |
|
void on_graph_modified (); |
|
void on_menu_file_open (); |
|
void on_menu_file_quit (); |
|
void on_menu_file_save (); |
|
void on_menu_file_save_as (); |
|
void on_menu_help_about (); |
|
void on_menu_help_help (); |
|
void on_node_deleted (); |
|
void on_node_deselected ( unsigned long n ); |
|
void on_node_selected (unsigned long n); |
|
void on_open_file_selected ( const std::string& file_name); |
|
void on_save_file_selected ( const std::string& file_name); |
|
void on_sel_node_evidence_modified (); |
|
void on_sel_node_num_values_modified (); |
|
void on_sel_node_text_modified (); |
|
void on_window_resized (); |
|
void recalculate_probabilities (); |
|
|
|
|
|
|
|
const rgb_pixel color_non_evidence; |
|
const rgb_pixel color_default_bg; |
|
const rgb_pixel color_evidence; |
|
const rgb_pixel color_error; |
|
const rgb_pixel color_gray; |
|
bool graph_modified_since_last_recalc; |
|
|
|
button btn_calculate; |
|
check_box sel_node_is_evidence; |
|
directed_graph_drawer<directed_graph_type> 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; |
|
unsigned long selected_node_index; |
|
bool node_is_selected; |
|
widget_group cpt_group; |
|
widget_group ppt_group; |
|
|
|
std::unique_ptr<bayesian_network_join_tree> solution; |
|
join_tree_type join_tree; |
|
|
|
|
|
std_vector_c<assignment> cpt_grid_assignments; |
|
std::string graph_file_name; |
|
}; |
|
|
|
|
|
|
|
int main() |
|
{ |
|
|
|
main_window my_window; |
|
|
|
|
|
my_window.show(); |
|
|
|
|
|
|
|
my_window.wait_until_closed(); |
|
} |
|
|
|
|
|
|
|
#ifdef WIN32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int WINAPI WinMain (HINSTANCE, HINSTANCE, PSTR cmds, int) |
|
{ |
|
main(); |
|
return 0; |
|
} |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main_window:: |
|
main_window( |
|
) : |
|
color_non_evidence(0,0,0), |
|
color_default_bg(255,255,255), |
|
color_evidence(100,200,100), |
|
color_error(255,0,0), |
|
color_gray(210,210,210), |
|
graph_modified_since_last_recalc(true), |
|
btn_calculate(*this), |
|
sel_node_is_evidence(*this), |
|
graph_drawer(*this), |
|
sel_node_index(*this), |
|
sel_node_num_values_label (*this), |
|
sel_node_text_label(*this), |
|
sel_node_evidence_label(*this), |
|
mbar(*this), |
|
selected_node_rect(*this), |
|
tables(*this), |
|
sel_node_num_values(*this), |
|
sel_node_text(*this), |
|
sel_node_evidence(*this), |
|
cpt_grid(*this), |
|
ppt_grid(*this), |
|
selected_node_index(0), |
|
node_is_selected(false), |
|
cpt_group(*this), |
|
ppt_group(*this) |
|
{ |
|
|
|
|
|
|
|
|
|
set_title("Bayesian Network Utility"); |
|
|
|
|
|
|
|
graph_drawer.set_pos(5,mbar.bottom()+5); |
|
set_size(750,400); |
|
|
|
|
|
btn_calculate.set_click_handler (*this, &main_window::recalculate_probabilities); |
|
cpt_grid.set_text_modified_handler (*this, &main_window::on_cpt_grid_modified); |
|
graph_drawer.set_graph_modified_handler (*this, &main_window::on_graph_modified); |
|
graph_drawer.set_node_deleted_handler (*this, &main_window::on_node_deleted); |
|
graph_drawer.set_node_deselected_handler (*this, &main_window::on_node_deselected); |
|
graph_drawer.set_node_selected_handler (*this, &main_window::on_node_selected); |
|
sel_node_evidence.set_text_modified_handler (*this, &main_window::on_sel_node_evidence_modified); |
|
sel_node_is_evidence.set_click_handler (*this, &main_window::on_evidence_toggled); |
|
sel_node_num_values.set_text_modified_handler(*this, &main_window::on_sel_node_num_values_modified); |
|
sel_node_text.set_text_modified_handler (*this, &main_window::on_sel_node_text_modified); |
|
|
|
|
|
btn_calculate.set_name("Recalculate posterior probability table"); |
|
selected_node_rect.set_name("Selected node"); |
|
sel_node_evidence_label.set_text("evidence value:"); |
|
sel_node_is_evidence.set_name("is evidence"); |
|
sel_node_num_values_label.set_text("Number of values: "); |
|
sel_node_text_label.set_text("Node label:"); |
|
|
|
|
|
|
|
tables.set_number_of_tabs(2); |
|
tables.set_tab_name(0,"Conditional probability table"); |
|
tables.set_tab_name(1,"Posterior probability table"); |
|
cpt_group.add(cpt_grid,0,0); |
|
ppt_group.add(ppt_grid,0,0); |
|
tables.set_tab_group(0,cpt_group); |
|
tables.set_tab_group(1,ppt_group); |
|
|
|
|
|
mbar.set_number_of_menus(2); |
|
mbar.set_menu_name(0,"File",'F'); |
|
mbar.set_menu_name(1,"Help",'H'); |
|
|
|
|
|
mbar.menu(0).add_menu_item(menu_item_text("Open", *this, &main_window::on_menu_file_open, 'O')); |
|
mbar.menu(0).add_menu_item(menu_item_separator()); |
|
mbar.menu(0).add_menu_item(menu_item_text("Save", *this, &main_window::on_menu_file_save, 'S')); |
|
mbar.menu(0).add_menu_item(menu_item_text("Save As",*this, &main_window::on_menu_file_save_as, 'a')); |
|
mbar.menu(0).add_menu_item(menu_item_separator()); |
|
mbar.menu(0).add_menu_item(menu_item_text("Quit", *this, &main_window::on_menu_file_quit, 'Q')); |
|
|
|
|
|
mbar.menu(1).add_menu_item(menu_item_text("Help", *this, &main_window::on_menu_help_help, 'e')); |
|
mbar.menu(1).add_menu_item(menu_item_text("About", *this, &main_window::on_menu_help_about, 'A')); |
|
|
|
|
|
|
|
|
|
no_node_selected(); |
|
on_window_resized(); |
|
} |
|
|
|
|
|
|
|
main_window:: |
|
~main_window( |
|
) |
|
{ |
|
|
|
|
|
|
|
close_window(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void main_window:: |
|
load_selected_node_tables_into_ppt_grid ( |
|
) |
|
{ |
|
|
|
|
|
node_type& node = graph_drawer.graph_node(selected_node_index); |
|
ppt_grid.set_grid_size(2,node.data.table().num_values()); |
|
|
|
|
|
|
|
for (unsigned long col = 0; col < node.data.table().num_values(); ++col) |
|
{ |
|
ppt_grid.set_text(0,col,"P(node=" + cast_to_string(col) + ")"); |
|
ppt_grid.set_background_color(0,col,rgb_pixel(150,150,250)); |
|
ppt_grid.set_editable(0,col,false); |
|
} |
|
|
|
|
|
|
|
if (solution) |
|
{ |
|
|
|
|
|
const matrix<double,1> prob = solution->probability(selected_node_index); |
|
|
|
|
|
for (unsigned long col = 0; col < node.data.table().num_values(); ++col) |
|
{ |
|
ppt_grid.set_text(1,col,cast_to_string(prob(col))); |
|
} |
|
} |
|
|
|
|
|
|
|
for (unsigned long col = 0; col < node.data.table().num_values(); ++col) |
|
{ |
|
ppt_grid.set_background_color(1,col,color_gray); |
|
ppt_grid.set_editable(1,col,false); |
|
} |
|
} |
|
|
|
|
|
|
|
void main_window:: |
|
load_selected_node_tables_into_cpt_grid ( |
|
) |
|
{ |
|
|
|
|
|
|
|
node_type& node = graph_drawer.graph_node(selected_node_index); |
|
|
|
initialize_node_cpt_if_necessary(selected_node_index); |
|
cpt_grid_assignments.clear(); |
|
|
|
|
|
unsigned long cpt_rows = 1; |
|
for (unsigned long i = 0; i < node.number_of_parents(); ++i) |
|
{ |
|
cpt_rows *= node.parent(i).data.table().num_values(); |
|
} |
|
|
|
unsigned long cpt_cols = node.data.table().num_values(); |
|
|
|
cpt_grid.set_grid_size(cpt_rows+1, cpt_cols+ node.number_of_parents()); |
|
const unsigned long num_cols = cpt_grid.number_of_columns(); |
|
|
|
|
|
assignment a(node_first_parent_assignment(graph_drawer.graph(),selected_node_index)); |
|
unsigned long col = 0; |
|
a.reset(); |
|
while (a.move_next()) |
|
{ |
|
cpt_grid.set_text(0,col,cast_to_string(a.element().key()) + ": " + graph_drawer.node_label(a.element().key()) ); |
|
cpt_grid.set_background_color(0,col,rgb_pixel(120,210,210)); |
|
cpt_grid.set_editable(0,col,false); |
|
++col; |
|
} |
|
|
|
|
|
for (col = node.number_of_parents(); col < num_cols; ++col) |
|
{ |
|
cpt_grid.set_text(0,col,"P(node=" + cast_to_string(col-node.number_of_parents()) + ")"); |
|
cpt_grid.set_background_color(0,col,rgb_pixel(150,150,250)); |
|
cpt_grid.set_editable(0,col,false); |
|
} |
|
|
|
|
|
const unsigned long num_values = node.data.table().num_values(); |
|
unsigned long row = 1; |
|
do |
|
{ |
|
col = 0; |
|
|
|
|
|
a.reset(); |
|
while (a.move_next()) |
|
{ |
|
cpt_grid.set_text(row,col,cast_to_string(a.element().value())); |
|
cpt_grid.set_background_color(row,col,rgb_pixel(180,255,255)); |
|
cpt_grid.set_editable(row,col,false); |
|
|
|
++col; |
|
} |
|
|
|
|
|
for (unsigned long value = 0; value < num_values; ++value) |
|
{ |
|
const double prob = node.data.table().probability(value,a); |
|
cpt_grid.set_text(row,col,cast_to_string(prob)); |
|
++col; |
|
} |
|
|
|
|
|
|
|
cpt_grid_assignments.push_back(a); |
|
++row; |
|
} while (node_next_parent_assignment(graph_drawer.graph(),selected_node_index,a)); |
|
|
|
} |
|
|
|
|
|
|
|
void main_window:: |
|
initialize_node_cpt_if_necessary ( |
|
unsigned long index |
|
) |
|
{ |
|
node_type& node = graph_drawer.graph_node(index); |
|
|
|
|
|
|
|
if (node_cpt_filled_out(graph_drawer.graph(), index) == false) |
|
{ |
|
node.data.table().empty_table(); |
|
|
|
const unsigned long num_values = node.data.table().num_values(); |
|
|
|
|
|
|
|
assignment a(node_first_parent_assignment(graph_drawer.graph(), index)); |
|
do |
|
{ |
|
|
|
node.data.table().set_probability(0, a, 1.0); |
|
|
|
|
|
for (unsigned long value = 1; value < num_values; ++value) |
|
node.data.table().set_probability(value, a, 0); |
|
|
|
} while (node_next_parent_assignment(graph_drawer.graph(), index,a)); |
|
} |
|
} |
|
|
|
|
|
|
|
void main_window:: |
|
no_node_selected ( |
|
) |
|
{ |
|
|
|
|
|
|
|
|
|
node_is_selected = false; |
|
tables.disable(); |
|
sel_node_evidence.disable(); |
|
sel_node_is_evidence.disable(); |
|
sel_node_index.disable(); |
|
sel_node_evidence_label.disable(); |
|
sel_node_text_label.disable(); |
|
sel_node_text.disable(); |
|
sel_node_index.set_text("index:"); |
|
sel_node_num_values_label.disable(); |
|
sel_node_num_values.disable(); |
|
cpt_grid.set_grid_size(0,0); |
|
ppt_grid.set_grid_size(0,0); |
|
|
|
sel_node_is_evidence.set_unchecked(); |
|
sel_node_text.set_text(""); |
|
sel_node_num_values.set_text(""); |
|
sel_node_evidence.set_text(""); |
|
sel_node_num_values.set_background_color(color_default_bg); |
|
sel_node_evidence.set_background_color(color_default_bg); |
|
} |
|
|
|
|
|
|
|
void main_window:: |
|
recalculate_probabilities ( |
|
) |
|
{ |
|
|
|
solution.reset(); |
|
if (graph_is_connected(graph_drawer.graph()) == false) |
|
{ |
|
message_box("Error","Your graph has nodes that are completely disconnected from the other nodes.\n" |
|
"You must connect them somehow"); |
|
} |
|
else if (graph_drawer.graph().number_of_nodes() > 0) |
|
{ |
|
if (graph_modified_since_last_recalc) |
|
{ |
|
|
|
const unsigned long num_nodes = graph_drawer.graph().number_of_nodes(); |
|
for (unsigned long i = 0; i < num_nodes; ++i) |
|
{ |
|
initialize_node_cpt_if_necessary(i); |
|
} |
|
|
|
|
|
create_moral_graph(graph_drawer.graph(), join_tree); |
|
create_join_tree(join_tree, join_tree); |
|
graph_modified_since_last_recalc = false; |
|
} |
|
|
|
|
|
solution.reset(new bayesian_network_join_tree(graph_drawer.graph(), join_tree)); |
|
|
|
if (node_is_selected) |
|
{ |
|
load_selected_node_tables_into_ppt_grid(); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_open_file_selected ( |
|
const std::string& file_name |
|
) |
|
{ |
|
try |
|
{ |
|
no_node_selected(); |
|
ifstream fin(file_name.c_str(), ios::binary); |
|
graph_drawer.load_graph(fin); |
|
graph_file_name = file_name; |
|
set_title("Bayesian Network Utility - " + right_substr(file_name,"\\/")); |
|
} |
|
catch (...) |
|
{ |
|
message_box("Error", "Unable to load graph file " + file_name); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_menu_file_open ( |
|
) |
|
{ |
|
|
|
|
|
open_existing_file_box(*this, &main_window::on_open_file_selected); |
|
} |
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_menu_file_save ( |
|
) |
|
{ |
|
|
|
if (graph_file_name.size() == 0) |
|
{ |
|
|
|
|
|
save_file_box(*this, &main_window::on_save_file_selected); |
|
} |
|
else |
|
{ |
|
|
|
ofstream fout(graph_file_name.c_str(), ios::binary); |
|
graph_drawer.save_graph(fout); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_save_file_selected ( |
|
const std::string& file_name |
|
) |
|
{ |
|
ofstream fout(file_name.c_str(), ios::binary); |
|
graph_drawer.save_graph(fout); |
|
graph_file_name = file_name; |
|
set_title("Bayesian Network Utility - " + right_substr(file_name,"\\/")); |
|
} |
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_menu_file_save_as ( |
|
) |
|
{ |
|
|
|
|
|
save_file_box(*this, &main_window::on_save_file_selected); |
|
} |
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_menu_file_quit ( |
|
) |
|
{ |
|
close_window(); |
|
} |
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_menu_help_help ( |
|
) |
|
{ |
|
message_box("Help", |
|
"To create new nodes right click on the drawing area.\n" |
|
"To create edges select the parent node and then shift+left click on the child node.\n" |
|
"To remove nodes or edges select them by left clicking and then press the delete key."); |
|
} |
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_menu_help_about ( |
|
) |
|
{ |
|
message_box("About","This application is the GUI front end to the dlib C++ Library's\n" |
|
"Bayesian Network inference utilities\n\n" |
|
"Version 1.2\n\n" |
|
"See http://dlib.net for updates"); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_graph_modified ( |
|
) |
|
{ |
|
|
|
graph_modified_since_last_recalc = true; |
|
|
|
|
|
solution.reset(); |
|
} |
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_sel_node_evidence_modified ( |
|
) |
|
{ |
|
|
|
node_type& node = graph_drawer.graph_node(selected_node_index); |
|
unsigned long value; |
|
try |
|
{ |
|
|
|
|
|
value = sa = trim(sel_node_evidence.text()); |
|
} |
|
catch (string_cast_error&) |
|
{ |
|
|
|
|
|
|
|
sel_node_evidence.set_background_color(color_error); |
|
return; |
|
} |
|
|
|
|
|
|
|
if (value >= node.data.table().num_values()) |
|
{ |
|
sel_node_evidence.set_background_color(color_error); |
|
} |
|
else |
|
{ |
|
node.data.set_value(value); |
|
sel_node_evidence.set_background_color(color_default_bg); |
|
} |
|
|
|
|
|
|
|
solution.reset(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_sel_node_num_values_modified ( |
|
) |
|
{ |
|
|
|
node_type& node = graph_drawer.graph_node(selected_node_index); |
|
|
|
unsigned long num_values; |
|
try |
|
{ |
|
|
|
num_values = sa = trim(sel_node_num_values.text()); |
|
} |
|
catch (string_cast_error&) |
|
{ |
|
sel_node_num_values.set_background_color(color_error); |
|
return; |
|
} |
|
|
|
|
|
if (num_values < 2 || num_values > 100) |
|
{ |
|
sel_node_num_values.set_background_color(color_error); |
|
} |
|
else |
|
{ |
|
|
|
node.data.table().set_num_values(num_values); |
|
graph_modified_since_last_recalc = true; |
|
sel_node_num_values.set_background_color(color_default_bg); |
|
|
|
on_sel_node_evidence_modified(); |
|
|
|
if (node.data.is_evidence() && node.data.value() >= num_values) |
|
{ |
|
|
|
node.data.set_value(0); |
|
} |
|
|
|
} |
|
|
|
solution.reset(); |
|
|
|
|
|
|
|
load_selected_node_tables_into_cpt_grid(); |
|
load_selected_node_tables_into_ppt_grid(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_cpt_grid_modified(unsigned long row, unsigned long col) |
|
{ |
|
node_type& node = graph_drawer.graph_node(selected_node_index); |
|
solution.reset(); |
|
|
|
double prob; |
|
try |
|
{ |
|
|
|
prob = sa = cpt_grid.text(row,col); |
|
} |
|
catch (string_cast_error&) |
|
{ |
|
cpt_grid.set_background_color(row,col,color_error); |
|
return; |
|
} |
|
|
|
|
|
if (prob < 0 || prob > 1) |
|
{ |
|
cpt_grid.set_background_color(row,col,color_error); |
|
return; |
|
} |
|
|
|
|
|
|
|
const unsigned long cur_val = col-node.number_of_parents(); |
|
|
|
node.data.table().set_probability(cur_val, cpt_grid_assignments[row-1], prob); |
|
|
|
|
|
|
|
|
|
double sum = 0; |
|
if (cur_val != node.data.table().num_values()-1) |
|
{ |
|
for (unsigned long i = 0; i < node.data.table().num_values()-1; ++i) |
|
sum += node.data.table().probability(i, cpt_grid_assignments[row-1]); |
|
} |
|
else |
|
{ |
|
for (unsigned long i = 1; i < node.data.table().num_values(); ++i) |
|
sum += node.data.table().probability(i, cpt_grid_assignments[row-1]); |
|
} |
|
|
|
|
|
if (sum > 1.0) |
|
{ |
|
cpt_grid.set_background_color(row,cpt_grid.number_of_columns()-1,color_error); |
|
} |
|
else |
|
{ |
|
|
|
if (cur_val == node.data.table().num_values()-1) |
|
{ |
|
node.data.table().set_probability(0, cpt_grid_assignments[row-1], 1-sum); |
|
cpt_grid.set_text(row,node.number_of_parents(),cast_to_string(1-sum)); |
|
} |
|
else |
|
{ |
|
node.data.table().set_probability(node.data.table().num_values()-1, cpt_grid_assignments[row-1], 1-sum); |
|
cpt_grid.set_text(row,cpt_grid.number_of_columns()-1,cast_to_string(1-sum)); |
|
} |
|
|
|
cpt_grid.set_background_color(row,cpt_grid.number_of_columns()-1,color_default_bg); |
|
cpt_grid.set_background_color(row,col,color_default_bg); |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_window_resized () |
|
{ |
|
|
|
|
|
|
|
drawable_window::on_window_resized(); |
|
|
|
|
|
unsigned long width,height; |
|
get_size(width,height); |
|
|
|
|
|
|
|
if (width < 500 || height < 350) |
|
return; |
|
|
|
|
|
graph_drawer.set_size(width-370,height-10-mbar.height()); |
|
cpt_grid.set_size((width-graph_drawer.width())-35,height-237); |
|
ppt_grid.set_size((width-graph_drawer.width())-35,height-237); |
|
|
|
|
|
tables.fit_to_contents(); |
|
|
|
|
|
|
|
|
|
|
|
sel_node_index.set_pos(graph_drawer.right()+14,graph_drawer.top()+18); |
|
sel_node_text_label.set_pos(sel_node_index.left(),sel_node_index.bottom()+5); |
|
sel_node_text.set_pos(sel_node_text_label.right()+5,sel_node_index.bottom()); |
|
sel_node_num_values_label.set_pos(sel_node_index.left(), sel_node_text.bottom()+5); |
|
sel_node_num_values.set_pos(sel_node_num_values_label.right(), sel_node_text.bottom()+5); |
|
sel_node_is_evidence.set_pos(sel_node_index.left(),sel_node_num_values.bottom()+5); |
|
sel_node_evidence_label.set_pos(sel_node_index.left(),sel_node_is_evidence.bottom()+5); |
|
sel_node_evidence.set_pos(sel_node_evidence_label.right()+5,sel_node_is_evidence.bottom()); |
|
tables.set_pos(sel_node_index.left(),sel_node_evidence.bottom()+5); |
|
sel_node_evidence.set_width(tables.right()-sel_node_evidence.left()+1); |
|
sel_node_text.set_width(tables.right()-sel_node_text.left()+1); |
|
sel_node_num_values.set_width(tables.right()-sel_node_num_values.left()+1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
selected_node_rect.wrap_around(sel_node_index.get_rect()+ |
|
tables.get_rect()); |
|
|
|
|
|
btn_calculate.set_pos(selected_node_rect.left(), selected_node_rect.bottom()+5); |
|
} |
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_node_selected (unsigned long n) |
|
{ |
|
|
|
node_type& node = graph_drawer.graph_node(n); |
|
|
|
|
|
|
|
selected_node_index = n; |
|
node_is_selected = true; |
|
tables.enable(); |
|
sel_node_is_evidence.enable(); |
|
sel_node_index.enable(); |
|
sel_node_evidence_label.enable(); |
|
sel_node_text_label.enable(); |
|
sel_node_text.enable(); |
|
sel_node_num_values_label.enable(); |
|
sel_node_num_values.enable(); |
|
|
|
|
|
|
|
if (node.data.table().num_values() < 2) |
|
{ |
|
node.data.table().set_num_values(2); |
|
graph_modified_since_last_recalc = true; |
|
} |
|
|
|
|
|
sel_node_index.set_text("index: " + cast_to_string(n)); |
|
if (graph_drawer.graph_node(n).data.is_evidence()) |
|
{ |
|
sel_node_is_evidence.set_checked(); |
|
sel_node_evidence.enable(); |
|
sel_node_evidence.set_text(cast_to_string(graph_drawer.graph_node(n).data.value())); |
|
} |
|
else |
|
{ |
|
sel_node_is_evidence.set_unchecked(); |
|
sel_node_evidence.disable(); |
|
sel_node_evidence.set_text(""); |
|
} |
|
|
|
sel_node_num_values.set_text(cast_to_string(node_num_values(graph_drawer.graph(),n))); |
|
|
|
sel_node_text.set_text(graph_drawer.node_label(n)); |
|
|
|
load_selected_node_tables_into_cpt_grid(); |
|
load_selected_node_tables_into_ppt_grid(); |
|
} |
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_evidence_toggled ( |
|
) |
|
{ |
|
if (sel_node_is_evidence.is_checked()) |
|
{ |
|
graph_drawer.graph_node(selected_node_index).data.set_as_evidence(); |
|
sel_node_evidence.enable(); |
|
sel_node_evidence.set_text(cast_to_string(graph_drawer.graph_node(selected_node_index).data.value())); |
|
|
|
graph_drawer.set_node_color(selected_node_index, color_evidence); |
|
} |
|
else |
|
{ |
|
graph_drawer.graph_node(selected_node_index).data.set_as_nonevidence(); |
|
sel_node_evidence.disable(); |
|
sel_node_evidence.set_text(""); |
|
sel_node_evidence.set_background_color(color_default_bg); |
|
graph_drawer.set_node_color(selected_node_index, color_non_evidence); |
|
} |
|
solution.reset(); |
|
} |
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_node_deselected ( unsigned long ) |
|
{ |
|
no_node_selected(); |
|
} |
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_node_deleted ( ) |
|
{ |
|
no_node_selected(); |
|
} |
|
|
|
|
|
|
|
|
|
void main_window:: |
|
on_sel_node_text_modified ( |
|
) |
|
{ |
|
|
|
graph_drawer.set_node_label(selected_node_index,sel_node_text.text()); |
|
} |
|
|
|
|
|
|
|
|