External Actions

Creating an External_Action

knowledgewright4/samples/support/tutorial/printer_3.kb

If your knowledgebase runs on your website, and no solution can be found for a user, wouldn't it be nice to forward the problem to support personnel via e-mail.Use Object/New to create an external_action named email. Enter the following parameters into the table:

parameter
value
type
sendmail
command
/usr/sbin/sendmail -t
to
support@company.com
from
%user_email%
subject
No Solution in Support Knowledgebase
cc
 
bcc
 
message
Known facts:

This particular external action has been implemented in the CGI interface. The parameters are recognized and processed by that interface. Also, it appends a list of all the known facts and their values onto the end of the message parameter. This means the support person can see all the answers entered by the user as well as any intermediate results deduced by the reasoning engine.

All the entries in the parameters table are text strings, so double-quotes are not needed. However, you can use percent signs around object names to include their values instead. For example, %user_email% will be replaced with the value of the object named user_email. To make this work, we need to create a question called user_email as follows:

id
user_email
path
/
prompt
What is your e-mail address?
question_type
fill_in_the_blank
answer_type
text
length
50
default
  
ask_also
   

Finally, open the no_solution solution and under actions add email. Change the fix text to read "We are forwarding your problem to tech support."

Implementing External Actions

You can implement your own external actions with any needed parameters. This can be done in the CGI interface, in a JSP or ASP interface, or a Delphi, Java, C/C++ or Visual Basic application.

Here's some of the Prolog code that implements the sendmail external_action (from runtime/cgi/kwcgirun.pro). First, the KWI is used to get the actions. If there are no actions, then 'none' is returned by the get_action KWI call:

cgi_take_actions(SID) :-
   once kwi(SID, get_action, ACTION),
   ACTION \= none,
   once cgi_take_action(SID, ACTION),
   cgi_take_actions(SID).
   cgi_take_actions(_).

The following code looks for an external action, and if the type parameter is set to sendmail, it executes it.

% Take external actions
cgi_take_action(SID, tell(external, PROPS)) :-
   member(type = sendmail, PROPS),
   !,
   cgi_sendmail(SID, PROPS).

The email is sent by this code which uses the KWI to get the current state of the reasoning engine (called a session). All the known facts are extracted from the session and the email contents are written to a file by calling cgi_email_write_file() (not shown). Finally the command parameter is extracted and the command is executed and the email file is deleted.

% Email Action
cgi_sendmail(SID, PropList) :-
once kwi(SID, get_session, ILIST),
once member(known = FactList, ILIST),
(once(cgi_email_write_file(PropList, FactList, FileName)) ->
true
; throw([$ERROR: Unable to write email to a temporary file$]) ),
(member(command = EMailCommand, PropList) ->
true
; throw([$ERROR: No command specified for sendmail action$]) ),
stringlist_concat([EMailCommand, $ <$, FileName], MailCmdLine),
(system(MailCmdLine) ->
true
; throw([$ERROR: Unable to execute command line for sendmail action$]) ),
(delfile(FileName, _) ->
true
; throw([$ERROR: Unable to delete temporary email file$]) ),
true.
cgi_sendmail(_, PropList) :-
(member(goal = NAME, PropList) ->
true
; NAME = $*unknown action*$ ),
throw([$ERROR: Unable to execute sendmail action: $, NAME]).

You can implement your own external actions by adapting the code in any of the runtime interfaces. Full source code is provided for each.

Run the Knowledgebase

Select Run/Run to test your knowledgebase. Select "light_output" as the symptom. You should be asked for your e-mail address, then the no_solution solution will be displayed as well as a dialog box displaying the parameters and their values for the email external_action.

Exercises

  1. Read the tutorial lesson on rules_tables. Change the 'to' parameter from "support@company.com" to "%support_email%. Then create a rules_table named support_email that selects a person to email to based on the symptom. For example, "light output" problems go to sally@company.com, whereas "no output" problems go to fred@company.com.



Copyright ©1995-2006 Amzi! inc. All Rights Reserved. Privacy Policy.
Amzi!, KnowledgeWright and WebLS are registered trademarks and Adventure in Prolog, Logic Server, Logic Explorer, Adventure in Prolog and Subscription Plus are trademarks of Amzi! inc.