#!/usr/bin/perl -w #----------------------------------------------------------------------------# # FILE: decisiontablebuilder.cgi # # WRITTEN BY: Paru Patel, Adam Rose, Bella Sherman, Viktoriya Strumba # # DATE: 12/08/2001 # # PURPOSE: Decision Table Builder - This tool guides the user through the # # tasksrequired to create a decision table. It queries for all # # conditions, actions, and then helps define rules. # #----------------------------------------------------------------------------# #----------------------------------------------------------------------------- # library paths #----------------------------------------------------------------------------- use lib "./lib"; #----------------------------------------------------------------------------- # modules #----------------------------------------------------------------------------- use CGI qw(:standard); # use standard CGI module use CGI::Carp qw(fatalsToBrowser); # redirect errors to client browser use navbuttons; #----------------------------------------------------------------------------- # constants #----------------------------------------------------------------------------- *MAX_CONDITIONS = \10; # maximum number of conditions *MAX_ACTIONS = \26; # maximum number of actions *MAX_FIELDS = \5; # maximum number of text input fields *MAX_CHARS = \80; # strings are limited to 80 characters in length #----------------------------------------------------------------------------- # variables and data structures #----------------------------------------------------------------------------- my @conditions = (); # List of all user entered conditions my @actions = (); # List of all user entered actions my @condition_rules; # Data structure containing condition rules my @action_rules; # Data structure containing action rules my %states = (); # State table mapping pages to functions my $current_screen = param( "state" ) || "default"; # The current screen my $created_table = param( "created_table" ); # Have we created the table? my $ran_diagnostics = param( "ran_diagnostics" ); # Have we run run diagnostics? my $process_new_rule = param( "process_new_rule" ); # Do we need to process a new rule my $edit_rule_num = param( "edit_rule_num" ); # Do we need to edit a rule my $update_rule_num = param( "update_rule_num" ); # Do we need to process changes made to a rule my $added_actions = param( "added_actions" ); # Do we need to update the rule hash for new actions my $added_conditions = param( "added_conditions" ); # Do we need to update the rule hash for new conditions #----------------------------------------------------------------------------- # variable default values #----------------------------------------------------------------------------- unless( defined( $edit_rule_num ) ) { $edit_rule_num = -1; } unless( defined( $update_rule_num ) ) { $update_rule_num = -1; } #----------------------------------------------------------------------------- # create hash table mapping state values to functions #----------------------------------------------------------------------------- %states = ( 'default' => \&initial_page, 'enter conditions' => \&enter_conditions, 'add conditions' => \&add_conditions, 'edit conditions' => \&edit_conditions, 'enter actions' => \&enter_actions, 'add actions' => \&add_actions, 'edit actions' => \&edit_actions, 'make rule' => \&make_rule, 'edit rule' => \&edit_rule, 'remove rule' => \&remove_rule, 'create table' => \&create_table, 'print friendly' => \&print_friendly, 'run diagnostics' => \&run_diagnostics, ); #----------------------------------------------------------------------------- # MAIN #----------------------------------------------------------------------------- #----------------------------------------------------------------------------- # output the general beginning html code #----------------------------------------------------------------------------- output_begin_html(); #----------------------------------------------------------------------------- # populate the data structures #----------------------------------------------------------------------------- populate_data_structures(); #----------------------------------------------------------------------------- # do any special processing #----------------------------------------------------------------------------- if( $process_new_rule ) { process_new_rule(); } if( $update_rule_num >= 0 ) { update_rule( $update_rule_num ); } if( $added_conditions ) { added_conditions(); } if( $added_actions ) { added_actions(); } #----------------------------------------------------------------------------- # output the program's common page heading #----------------------------------------------------------------------------- output_page_heading(); #----------------------------------------------------------------------------- # output the side navigational frame #----------------------------------------------------------------------------- output_navigation_frame(); #----------------------------------------------------------------------------- # output the beginning html to start main frame #----------------------------------------------------------------------------- output_main_frame_begin(); #----------------------------------------------------------------------------- # output the body of the page by referencing the function that maps to state #----------------------------------------------------------------------------- die "Could not find screen for state $current_screen" unless $states{ $current_screen }; while( my ( $screen_name, $function ) = each %states ) { if( $screen_name eq $current_screen ) { $function->(); last; } } #----------------------------------------------------------------------------- # store the current values of the data structures #----------------------------------------------------------------------------- store_data_structures(); #----------------------------------------------------------------------------- # output the ending html code #----------------------------------------------------------------------------- output_end_html(); #----------------------------------------------------------------------------- # terminate the script #----------------------------------------------------------------------------- exit( 0 ); #============================================================================= #----------------------------------------------------------------------------- # subroutines #----------------------------------------------------------------------------- #============================================================================= #----------------------------------------------------------------------------- # SUBROUTINE: populate_data_structure # PURPOSE: subroutine to grab the data structure data from the parameters #----------------------------------------------------------------------------- sub populate_data_structures { #--------------------------------------------------------------------------- # variables #--------------------------------------------------------------------------- my $index = 0; my $rule_value = ""; my $value = ""; #--------------------------------------------------------------------------- # populate the list of conditions #--------------------------------------------------------------------------- foreach $value ( param( 'conditions' ) ) { if( $value ) { push( @conditions, $value ); } } #--------------------------------------------------------------------------- # populate the list of actions #--------------------------------------------------------------------------- foreach $value ( param( 'actions' ) ) { if( $value ) { push( @actions, $value ); } } #--------------------------------------------------------------------------- # populate the lists of condition values for each rule #--------------------------------------------------------------------------- $index = 0; foreach $rule_value ( param( 'condition_rules' ) ) { if( $rule_value ) { @{ $condition_rules[ $index++ ] } = split( //, $rule_value ); } } #--------------------------------------------------------------------------- # populate the list of action values for each rule #--------------------------------------------------------------------------- $index = 0; foreach $rule_value ( param( 'action_rules' ) ) { if( $rule_value ) { @{ $action_rules[ $index++ ] } = split( //, $rule_value ); } } #--------------------------------------------------------------------------- # strings are limited to MAX_CHARS characters in length #--------------------------------------------------------------------------- @conditions = validate_length( @conditions ); @actions = validate_length( @actions ); } # end sub populate_data_structures #----------------------------------------------------------------------------- # SUBROUTINE: store_data_structures # PURPOSE: subroutine to store the data structure to the web page #----------------------------------------------------------------------------- sub validate_length { #--------------------------------------------------------------------------- # parameters #--------------------------------------------------------------------------- my @array = @_; #--------------------------------------------------------------------------- # variables #--------------------------------------------------------------------------- my @ret_array = (); #--------------------------------------------------------------------------- # foreach element, check length #--------------------------------------------------------------------------- foreach my $string ( @array ) { if( length( $string ) > $MAX_CHARS ) { my @chars = split( //, $string ); $string = join( '', @chars[ 0..$MAX_CHARS ] ); $string = $string . "..."; } push( @ret_array, $string ); } #--------------------------------------------------------------------------- # return validate array #--------------------------------------------------------------------------- return @ret_array; } #----------------------------------------------------------------------------- # SUBROUTINE: store_data_structures # PURPOSE: subroutine to store the data structure to the web page #----------------------------------------------------------------------------- sub store_data_structures { #--------------------------------------------------------------------------- # variables #--------------------------------------------------------------------------- my $i = 0; my $string = ""; #--------------------------------------------------------------------------- # store the list of conditions #--------------------------------------------------------------------------- unless( ( $current_screen eq "add conditions" ) || ( $current_screen eq "edit conditions" ) ) { foreach my $condition ( @conditions ) { print " \n"; } } #--------------------------------------------------------------------------- # store the list of actions #--------------------------------------------------------------------------- unless( ( $current_screen eq "add actions" ) || ( $current_screen eq "edit actions" ) ) { foreach my $action ( @actions ) { print " \n"; } } #--------------------------------------------------------------------------- # foreach rule, store the condition values as a string #--------------------------------------------------------------------------- for( $i = 0; $i < scalar( @condition_rules ); $i++ ) { $string = join( "", @{ $condition_rules[ $i ] } ); print " \n"; } #--------------------------------------------------------------------------- # foreach rule, store the action values as a string #--------------------------------------------------------------------------- for( $i = 0; $i < scalar( @action_rules ); $i++ ) { $string = join( "", @{ $action_rules[ $i ] } ); print " \n"; } #--------------------------------------------------------------------------- # store special variables #--------------------------------------------------------------------------- if( $created_table ) { print " \n"; } if( $ran_diagnostics ) { print " \n"; } } # end sub store_data_structures #----------------------------------------------------------------------------- # SUBROUTINE: output_begin_html # PURPOSE: output the beginning thml code including any javascript code #----------------------------------------------------------------------------- sub output_begin_html { print header(); # output the begin http header information print " \n"; print "
\n"; print "