| 1 | package VoiceXML::Client::Engine; | ||
| 2 | |||
| 3 | use VoiceXML::Client::Parser; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | |||
| 7 | =head1 COPYRIGHT AND LICENSE | ||
| 8 | |||
| 9 | |||
| 10 | Copyright (C) 2007,2008 by Pat Deegan. | ||
| 11 | All rights reserved | ||
| 12 | http://voicexml.psychogenic.com | ||
| 13 | |||
| 14 | This library is released under the terms of the GNU GPL version 3, making it available only for | ||
| 15 | free programs ("free" here being used in the sense of the GPL, see http://www.gnu.org for more details). | ||
| 16 | Anyone wishing to use this library within a proprietary or otherwise non-GPLed program MUST contact psychogenic.com to | ||
| 17 | acquire a distinct license for their application. This approach encourages the use of free software | ||
| 18 | while allowing for proprietary solutions that support further development. | ||
| 19 | |||
| 20 | |||
| 21 | This file is part of VoiceXML::Client. | ||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | VoiceXML::Client is free software: you can redistribute it and/or modify | ||
| 26 | it under the terms of the GNU General Public License as published by | ||
| 27 | the Free Software Foundation, either version 3 of the License, or | ||
| 28 | (at your option) any later version. | ||
| 29 | |||
| 30 | VoiceXML::Client is distributed in the hope that it will be useful, | ||
| 31 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 32 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 33 | GNU General Public License for more details. | ||
| 34 | |||
| 35 | You should have received a copy of the GNU General Public License | ||
| 36 | along with VoiceXML::Client. If not, see <http://www.gnu.org/licenses/>. | ||
| 37 | |||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | =head2 new | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | sub new { | ||
| 46 | my $class = shift; | ||
| 47 | my $rootDocument = shift; | ||
| 48 | my %params = @_; | ||
| 49 | |||
| 50 | |||
| 51 | my $self = {}; | ||
| 52 | |||
| 53 | bless $self, ref $class || $class; | ||
| 54 | |||
| 55 | |||
| 56 | $self->{'interpreter'} = new VoiceXML::Client::Engine::Component::Interpreter::JavaScript->new(); | ||
| 57 | $self->{'runtime'} = $self->{'interpreter'}->runtime(); | ||
| 58 | $self->{'parser'} = VoiceXML::Client::Parser->new(); | ||
| 59 | |||
| 60 | $self->{'VXMLRootDoc'} = $self->{'parser'}->parse($rootDocument, $self->{'runtime'}) if ($rootDocument); | ||
| 61 | |||
| 62 | |||
| 63 | $self->init(\%params); | ||
| 64 | |||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | sub init { | ||
| 70 | my $self = shift; | ||
| 71 | my $params = shift; | ||
| 72 | |||
| 73 | return 1; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub componentRequest { | ||
| 77 | my $self = shift; | ||
| 78 | my $componentObj = shift; | ||
| 79 | my $requestType = shift; | ||
| 80 | my @params = @_; | ||
| 81 | |||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | sub FIA { | ||
| 88 | my $self = shift; | ||
| 89 | |||
| 90 | $self->initialize(); | ||
| 91 | |||
| 92 | } | ||
| 93 | |||
| 94 | sub FIA_initialize { | ||
| 95 | my $self = shift; | ||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | // | ||
| 100 | // Initialization Phase | ||
| 101 | // | ||
| 102 | |||
| 103 | foreach ( <var> and form item variable, in document order ) | ||
| 104 | Declare the variable, initializing it to the value of | ||
| 105 | the "expr" attribute, if any, or else to undefined. | ||
| 106 | |||
| 107 | foreach ( field item ) | ||
| 108 | Declare a prompt counter and set it to 1. | ||
| 109 | |||
| 110 | if ( there is an initial item ) | ||
| 111 | Declare a prompt counter and set it to 1. | ||
| 112 | |||
| 113 | if ( user entered form by speaking to its | ||
| 114 | grammar while in a different form ) | ||
| 115 | { | ||
| 116 | Enter the main loop below, but start in | ||
| 117 | the process phase, not the select phase: | ||
| 118 | we already have a collection to process. | ||
| 119 | } | ||
| 120 | |||
| 121 | // | ||
| 122 | // Main Loop: select next form item and execute it. | ||
| 123 | // | ||
| 124 | |||
| 125 | while ( true ) | ||
| 126 | { | ||
| 127 | // | ||
| 128 | // Select Phase: choose a form item to visit. | ||
| 129 | // | ||
| 130 | |||
| 131 | if ( the last main loop iteration ended | ||
| 132 | with a <goto nextitem> ) | ||
| 133 | Select that next form item. | ||
| 134 | |||
| 135 | else if (there is a form item with an | ||
| 136 | unsatisfied guard condition ) | ||
| 137 | Select the first such form item in document order. | ||
| 138 | |||
| 139 | else | ||
| 140 | Do an <exit/> -- the form is full and specified no transition. | ||
| 141 | |||
| 142 | // | ||
| 143 | // Collect Phase: execute the selected form item. | ||
| 144 | // | ||
| 145 | // Queue up prompts for the form item. | ||
| 146 | |||
| 147 | unless ( the last loop iteration ended with | ||
| 148 | a catch that had no <reprompt> ) | ||
| 149 | { | ||
| 150 | Select the appropriate prompts for the form item. | ||
| 151 | |||
| 152 | Queue the selected prompts for play prior to | ||
| 153 | the next collect operation. | ||
| 154 | |||
| 155 | Increment the form item's prompt counter. | ||
| 156 | } | ||
| 157 | |||
| 158 | // Activate grammars for the form item. | ||
| 159 | |||
| 160 | if ( the form item is modal ) | ||
| 161 | Set the active grammar set to the form item grammars, | ||
| 162 | if any. (Note that some form items, e.g. <block>, | ||
| 163 | cannot have any grammars). | ||
| 164 | else | ||
| 165 | Set the active grammar set to the form item | ||
| 166 | grammars and any grammars scoped to the form, | ||
| 167 | the current document, the application root | ||
| 168 | document, and then elements up the <subdialog> | ||
| 169 | call chain. | ||
| 170 | |||
| 171 | // Execute the form item. | ||
| 172 | |||
| 173 | if ( a <field> was selected ) | ||
| 174 | Collect an utterance or an event from the user. | ||
| 175 | else if ( a <record> was chosen ) | ||
| 176 | Collect an utterance (with a name/value pair | ||
| 177 | for the recorded bytes) or event from themuser. | ||
| 178 | else if ( an <object> was chosen ) | ||
| 179 | Execute the object, setting the <object>'s | ||
| 180 | form item variable to the returned ECMAScript value. | ||
| 181 | else if ( a <subdialog> was chosen ) | ||
| 182 | Execute the subdialog, setting the <subdialog>'s | ||
| 183 | form item variable to the returned ECMAScript value. | ||
| 184 | else if ( a <transfer> was chosen ) | ||
| 185 | Do the transfer, and (if wait is true) set the | ||
| 186 | <transfer> form item variable to the returned | ||
| 187 | result status indicator. | ||
| 188 | else if ( the <initial> was chosen ) | ||
| 189 | Collect an utterance or an event from the user. | ||
| 190 | else if ( a <block> was chosen ) | ||
| 191 | { | ||
| 192 | Set the block's form item variable to a defined value. | ||
| 193 | |||
| 194 | Execute the block's executable context. | ||
| 195 | } | ||
| 196 | |||
| 197 | // | ||
| 198 | // Process Phase: process the resulting utterance or event. | ||
| 199 | // | ||
| 200 | |||
| 201 | // Process an event. | ||
| 202 | |||
| 203 | if ( the form item execution resulted in an event ) | ||
| 204 | { | ||
| 205 | Find the appropriate catch for the event. | ||
| 206 | |||
| 207 | Execute the catch (this may leave the FIA). | ||
| 208 | |||
| 209 | continue | ||
| 210 | } | ||
| 211 | |||
| 212 | // Must have an utterance: process ones from outside grammars. | ||
| 213 | |||
| 214 | if ( the utterance matched a grammar from outside the form ) | ||
| 215 | { | ||
| 216 | if ( the grammar belongs to a <link> element ) | ||
| 217 | Execute that link's goto or throw, leaving the FIA. | ||
| 218 | |||
| 219 | if ( the grammar belongs to a menu's <choice> element ) | ||
| 220 | Execute the choice's goto or throw, leaving the FIA. | ||
| 221 | |||
| 222 | // The grammar belongs to another form (or menu). | ||
| 223 | |||
| 224 | Transition to that form (or menu), carrying the utterance | ||
| 225 | to the other form (or menu)'s FIA. | ||
| 226 | } | ||
| 227 | |||
| 228 | // Process an utterance spoken to a grammar from this form. | ||
| 229 | // First copy utterance slot values into corresponding | ||
| 230 | // form item variables. | ||
| 231 | |||
| 232 | Clear all "just_filled" flags. | ||
| 233 | |||
| 234 | foreach ( slot in the user's utterance ) | ||
| 235 | { | ||
| 236 | if ( the slot corresponds to a field item ) | ||
| 237 | { | ||
| 238 | Copy the slot value into the field item's form item variable. | ||
| 239 | |||
| 240 | Set this field item's "just_filled" flag. | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | // Set <initial> form item variable if any field items are filled. | ||
| 245 | |||
| 246 | if ( any field item variable is set as a result of the user utterance ) | ||
| 247 | Set the <initial> form item variable. | ||
| 248 | |||
| 249 | // Next execute any <filled> actions triggered by this utterance. | ||
| 250 | |||
| 251 | foreach ( <filled> action in document order ) | ||
| 252 | { | ||
| 253 | // Determine the form item variables the <filled> applies to. | ||
| 254 | |||
| 255 | N = the <filled>'s "namelist" attribute. | ||
| 256 | |||
| 257 | if ( N equals "" ) | ||
| 258 | { | ||
| 259 | if ( the <filled> is a child of a form item ) | ||
| 260 | N = the form item's form item variable name. | ||
| 261 | else if ( the <filled> is a child of a form ) | ||
| 262 | N = the form item variable names of all the form | ||
| 263 | items in that form. | ||
| 264 | } | ||
| 265 | |||
| 266 | // Is the <filled> triggered? | ||
| 267 | |||
| 268 | if ( any form item variable in the set N was "just_filled" | ||
| 269 | AND ( the <filled> mode is "all" | ||
| 270 | AND all variables in N are filled | ||
| 271 | OR the <filled> mode is "any" | ||
| 272 | AND any variables in N are filled) ) | ||
| 273 | Execute the <filled> action. | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | 1; | ||
| 279 |