;; file: $isip/util/demo/isip_comm_control_server/start_servers.pgm
;; version: $Id: dialog_navigation_server.pgm 10572 2006-06-26 21:54:34Z rei5 $
;; NOTICE
;;  
;; This software was produced for the U.S. Government
;; under Contract No. DAAB07-97-C-E601, and is
;; subject to the Rights in Technical Data and Computer
;; Software Clause 252.227-7013 (Oct 88) and Alternate II (APR 93).
;; 
;; Copyright 1998, 1999 The MITRE Corporation

;; NOTE! The "utterance_id" things are used in a way that multiple
;; users might be able to use this at the same time. We are not
;; really supporting that right now so either remove all of these
;; or add\fix them for the rest. MOST PROBABLY -REMOVE
;; yes, we need to remove, the process manager starts a new hub for
;; each of the demos running at the same time.

;; latest edit 2005-02-25 17:35
;;

;; -------------------------------------------------
;;   LOGFILE INSTRUCTIONS
;; -------------------------------------------------

LOG_DIR: /cavs/hse/ies/isip/data/corpora/demo
LOG_VERSION: "cavs_demo 1.0"
USER_ID: spk
TIMESTAMP: recognize_speech

SERVER: data_recorder
HOST: localhost
PORT: DATA_RECORDER_PORT
OPERATIONS: reinitialize receive_signal reset

SERVER: signal_detector
HOST: localhost
PORT: SIGNAL_DETECTOR_PORT
OPERATIONS: reinitialize receive_signal reset

SERVER: speech_recognizer
HOST: localhost
PORT: SPEECH_RECOGNIZER_PORT
OPERATIONS: reinitialize receive_signal reset

SERVER: isip_nlp
HOST: localhost
PORT: DIALOG_NLP_PORT
OPERATIONS: reinitialize receive_data reset
;;; ORIGINAL:: OPERATIONS: reinitialize send_to_parse get_process_id


;; this is the dialog navigation manager
SERVER: dialogue_serv
HOST:localhost
PORT: DIALOG_NAVIGATION_MANAGER_PORT
OPERATIONS: reinitialize receive_data reset
;; ORIGINAL::: OPERATIONS: reinitialize receive_parse receive_db_result reset_task get_process_id 
;; removed (receive_profile notify_playing_has_ended)

;; used to set the database parameters on the server side
;;
SERVER: db_server
HOST: localhost
PORT: DIALOG_NAVIGATION_DATABASE_PORT
OPERATIONS:  reinitialize receive_data check_database_parameters set_database_parameters reset
;;; ORIGINAL::: OPERATIONS: sql_query reinitialize get_process_id


;; -------------------------------------------------
;;   SERVERS
;; -------------------------------------------------

;; this is when the client acts as a server and it connects to the hub
SERVICE_TYPE: dialog_navigation
;;HOST: CLIENT_IPADDRESS
CLIENT_PORT: HUB_PORTNUMBER
OPERATIONS: reinitialize receive_signal end_points receive_hypothesis display_decoding display_database display_nlp display_prompt display_debug database_params reset_state

;;echo_frame is also clientside

;; these are the builtin to hub functions
SERVER: Builtin
OPERATIONS: dispatch_to_main abort_main increment_utterance nop new_session end_session

;; -------------------------------------------------
;;   PROGRAM: main 
;; -------------------------------------------------
INITIAL_TOKEN: {c main :call_answered 1 }
MODE: singlethread
PROGRAM: main

;; ----------- PROCESS ID MESSAGES ---------
;; RULE: :process_id --> data_recorder.receive_signal
;; IN: :process_id :source_name
;; OUT: :server_name :pid :program
;; 
;; RULE: :process_id --> signal_detector.receive_signal
;; IN: :process_id :source_name
;; OUT: :server_name :pid :program
;; 
;; RULE: :process_id --> speech_recognizer.receive_signal
;; IN: :process_id :source_name
;; OUT: :server_name :pid :program
;; 
;; RULE: :process_id --> isip_nlp.receive_data
;; IN: :process_id :source_name
;; OUT: :server_name :pid :program
;; 
;; RULE: :process_id --> db_server.receive_data
;; IN: :process_id :source_name
;; OUT: :server_name :pid :program
;; 
;; RULE: :process_id --> dialogue_serv.receive_data
;; IN: :process_id :source_name
;; OUT: :server_name :pid :program
;; 
;; RULE: :pid --> dialog_navigation.receive_signal
;; IN: :server_name :pid
;; OUT: none!
;; ----------- END PROCESS ID MESSAGES ---------


;; ---- RESET MESSAGES DUE TO MODEL INCOMPATIBILITY

RULE: :reset_all --> signal_detector.reset
IN: :reset_all
OUT: none!

RULE: :reset_all --> data_recorder.reset
IN: :reset_all
OUT: none!

RULE: :reset_all --> speech_recognizer.reset
IN: : :reset_all
OUT: none!

RULE: :reset_all --> isip_nlp.reset
IN: : :reset_all
OUT: none!

RULE: :reset_all --> db_server.reset
IN: : :reset_all
OUT: none!

RULE: :reset_all --> dialogue_serv.reset
IN: : :reset_all
OUT: none!

;; ----- RESET MESSAGE

RULE: :reset --> signal_detector.receive_signal
IN: :reset
OUT: none!

RULE: :reset --> data_recorder.receive_signal
IN: :reset
OUT: none!

;; ----- END RESET MESSAGE

;; ---------- SEND TO BUILTIN
RULE: :call_answered --> Builtin.new_session
IN:  :call_answered :server_id
LOG_IN: :call_answered
SET: :new_dialogue 1
OUT:

RULE: :new_dialogue --> Builtin.increment_utterance
IN: 
SET: increment_utterance_id 1
OUT:
STORE: :utterance_id

;; ---------- SEND TO DATA RECORDER
RULE: :raw_data_endpointed --> data_recorder.receive_signal
IN: :raw_data_endpointed :sample_rate :frame_index :source_name :channel0 :channel1
OUT: 

RULE: :utterance_status --> data_recorder.receive_signal
IN: :utterance_status :source_name
OUT:

;; --------- SEND TO SPEECH RECOGNIZER

;;RULE: :raw_data_endpointed --> speech_recognizer.receive_signal
;;IN: :raw_data_endpointed :sample_rate :frame_index :source_name :channel0 :channel1
;;OUT: 

RULE: :utterance_status --> speech_recognizer.receive_signal
IN: :utterance_status :source_name
OUT:

RULE: :audio_ready --> speech_recognizer.receive_signal
RETRIEVE: (:utterance_id 0)
IN: :utterance_id :client_name :audio_ready :source_name :button_press_counter :hub_logdir :synth_logdir :hub_log_prefix :sample_rate :num_channels :sample_bytes :file_format :frame_duration_d 
OUT: 

RULE: :start_decoding --> speech_recognizer.receive_signal
IN: : :filename
OUT: none!

;; --------- SEND TO SIGNAL DETECTOR

RULE: :raw_data --> signal_detector.receive_signal
IN: :raw_data :sample_rate :frame_index :source_name :channel0 :channel1
OUT:

RULE: :utterance_status --> signal_detector.receive_signal
IN: :utterance_status :source_name
OUT:

RULE: :audio_ready --> signal_detector.receive_signal
RETRIEVE: (:utterance_id 0)
IN: :utterance_id :client_name :audio_ready :source_name :button_press_counter :hub_logdir :synth_logdir :hub_log_prefix :sample_rate :num_channels :sample_bytes :channel_d :frame_duration_d :window_duration_d :nominal_signal_d :signal_adaptation_delta_d :signal_adaptation_constant_d :nominal_noise_d :noise_adaptation_delta_d :noise_adaptation_constant_d :noise_floor_d :utterance_delta_d :min_utt_dur_d :min_utt_sep_d :max_utt_dur_d  :zc_utt_thresh_d :preemph_coef_d :pad_time_d
OUT: 

;; --------- SEND TO DATA RECORDER

RULE: :audio_ready --> data_recorder.receive_signal
RETRIEVE: (:utterance_id 0)
IN: :utterance_id :client_name :audio_ready :source_name :button_press_counter :hub_logdir :synth_logdir :hub_log_prefix :sample_rate :num_channels :sample_bytes :file_format 
OUT: 

RULE: :data_recorder_ack --> data_recorder.receive_signal
IN: :data_recorder_ack :source_name :button_press_counter
OUT: 

;; --------- SEND TO DIALOG DATABASE

RULE: :db_query --> db_server.receive_data
IN: :db_query :task :query_params :access_web :db_info
OUT: :query_result :nfound :column_names :values

;; a query sent from the cleint side to check whether the user has to set
;; the paramaters
;;
;;-- send to GU
RULE: :database_query --> db_server.check_database_parameters
IN: :database_query
OUT: none!


;;-- send to GU
RULE: :username --> db_server.set_database_parameters
IN: :username :password :hostname :database
OUT: none!

;; ---send same to the GUI
RULE: :db_query --> dialog_navigation.display_database
IN: :db_query :task :query_params :access_web :db_info
OUT: none!

;; --------- SEND TO DIALOG NLP 

;; RULE: :nbest_list --> isip_nlp.receive_data
RULE: :input_to_nlp --> isip_nlp.receive_data
IN: :input_string :parse_input :num_frames :path_score :sysid :real_time :total_time
OUT: :parse_output

;; --------- SEND TO DIALOG MANAGER 

RULE: :parse_output --> dialogue_serv.receive_data
LOG_IN: :parse_output
IN: :parse_input :parse_output
OUT: :prompt :slot :speech_act :db_query :web_query :task :question_type :merged_slot
;;; :call_finished

RULE: :query_result --> dialogue_serv.receive_data
IN: :db_query :task :query_params :access_web :question_type :query_result :merged_slot
OUT: :prompt :slot

;;-- send to GU
RULE: :prompt --> dialog_navigation.display_prompt
IN: :prompt :prompt_user :speech_act :slot
OUT: none!

;;-----send same to GUI
RULE: :query_result --> dialog_navigation.display_database
IN: :db_query :task :query_params :access_web :db_info :question_type :query_result :merged_slot
OUT: none!

;; --------- SEND TO DIALOG SYSTEM (client) 

RULE: :nbest_list --> dialog_navigation.receive_hypothesis
IN: :input_string :nbest_list :num_frames :path_score :sysid :real_time :total_time
OUT: none!

RULE: :incompatible --> dialog_navigation.reset_state
IN: :incompatible
OUT: none!

RULE: :dialog_navigation_ack --> dialog_navigation.receive_signal
IN: :dialog_navigation_ack :source_name :button_press_counter :frame_index :filename
OUT: none!

RULE: :start_recording_signal --> dialog_navigation.receive_signal
IN: :start_time :stop_time
OUT: none!

RULE: :start_time --> dialog_navigation.end_points
IN: :start_time :stop_time
OUT: :raw_data_endpointed :source_name

RULE: :start_decoding --> dialog_navigation.display_decoding
IN: : :filename
OUT: none!
;;; this function really can return things, but since this is the FSM
;;; part of the program, it may or may not return something

;;; data from the nlp needs to be displayed 
;;; :input_string :parse_output

RULE: :parse_output --> dialog_navigation.display_nlp
LOG_IN: :parse_output
IN: :parse_input :parse_output
OUT: none!

;; sends a database query to the cleint so that the user can 
;; enter the databse parameters
RULE: :database_parameters --> dialog_navigation.database_params
LOG_IN: :database_parameters	
IN: :database_parameters	
OUT: none!	

;;; manager need to display
;;; :parse_in :parse_output :db_query :query_result :prompt
;;; --notes--
;;; parse_in\parse_output are above in nlp
;;; db_query is below in database
;;; query_result is below in database

;;; database need to display
;;; :db_query :query_result :task :Query_xxx
;;; --notes--
;;; db_query - done, below
;;; task - done, below
;;; :Query_xxx\:query_params - changed to query_params for easy of programming
;;;   -- done, below
;;; :query_result -- done, below

;; this rule lets the servers send anything they want to the dialog system
;; debug window
RULE: :debug_msg --> dialog_navigation.display_debug
IN: :debug_msg :source_server :state_change_old :state_change_new :debug_string :debug_msg_count :state_change_number
OUT: none!

;; Information from the DARPA help website

;; The CONTROL: directive allows the user to specify how the program is to proceed after the rule is fired.
;; * A value of :return means that the script should stop running the program when this rule is complete.
;; * A value of :asynchronous means that the script should not wait for the operation to complete before starting the next applicable rule (SET: and DEL: will be run immediately, before additional rules are tried). When you use the builtin server dispatch functions dispatch_to_main or dispatch_token with CONTROL: :asynchronous, a new token is created for the dispatch, but rules continue to be tried for the current token.
;; * A value of :no_result tells the hub that the server will not return a value from the operation. If you use :no_result, you should also use :asynchronous.

;; Three special values of the OUT: directive, none!, abort! and destroy!,  are shorthands for sets of CONTROL: values.

;; * OUT: none! is equivalent to CONTROL: :no_result :asynchronous
;; * OUT: abort! is equivalent to CONTROL: :return
;; * OUT: destroy!  is equivalent to CONTROL: :no_result :asynchronous :return
