级别: 初级 Louis Tur (louist@us.ibm.com), 高级 IT 专家 , IBM Corporation
2005 年 7 月 14 日 学习如何使用 WebSphere® Voice Server SDK 的语音技术从 U2 数据库管理系统中检索数据。
简介
您是一名想提供从电话或其他设备对 U2 数据的访问的 RedBack® 开发人员吗?您愿意在自己的笔记本上演示此特性吗?本文将向您展示如何去做。使用 IBM WebSphere® Voice Server Software Developers Kit,您可以提交使用现有 RedBack 业务对象的语音请求。
关于本项目
本项目提示调用者输入一个名称,并检索匹配的联系信息。示例代码将展示如何创建一个外部的语法文件、如何确认所说的名称,以及如何取得匹配的记录。本项目基于 Kulvir Singh Bhogal 和 Joe Pull 合著的标题为“How WebSphere Voice SDK can get your enterprise data to speak up for itself”的一篇文章。对来自这篇文章的示例代码进行了修改,以满足 RedBack 开发人员的需要。请参阅 参考资料。
所使用的软件
该应用程序使用 VoiceXML (VXML)、Java Server Pages (JSP) 和 RedBack 的 Java Beans 来访问 U2 数据。WebSphere Voice Server SDK 是用于 VXML 执行的引擎,而 WebSphere Application Server 是用于 JSP 和 Java Beans 执行的引擎。在后端,RedBack Scheduler 处理对数据库的请求。
该项目使用以下软件:
- IBM WebSphere Voice Server SDK 3.1
- IBM WebSphere Application Server 5.0
- IBM U2 UniVerse 10.1 for Windows
- IBM U2 RedBack 4.2.3
设置应用程序
安装了必要的软件之后,需要在运行应用程序之前做以下工作:
- 为 RedBack Gateway for WebSphere Application Server 创建一个 rgw.ini 配置文件。将文件保存到 Windows 系统目录。该文件的内容应该如下所示:
rbexamples localhost:8401 |
- 在 IBM HTTP 服务器上,将 rgw.exe 从 RedBack 产品 CD 复制到 cgi-bin 目录中。
- 在 WebSphere Application Server 上安装 rbexamples.war 包。为了进行演示,RedBack 提供了一个 Web Archive (war) 文件。该文件是一个自成体系的包,它包含一些示例和 RedBack Java Beans。要在 WebSphere Application Server 5.0 中安装此文件,请执行以下操作:
- 在 WebSphere Application Server上,通过将您的 Web 浏览器指向 http://localhost:9090/admin 来启动 Administrative Console。
- 在控制台的左边扩展 Applications。
- 选择 Install New Application。
- 单击右窗格中的 Browse 按钮。
- 导航到 rbexamples.war 文件(在产品 CD 上的 RedBeans 目录下),然后选择该文件并单击 Open。
- 在 Context Root 中输入
rbexamples。然后单击 Next。
- 将 Virtual Host 设为 default_host。然后单击 Next。
- 在 Application Name 中输入
rbexamples。然后单击 Next。
- 单击 Finish。
- 单击 Save to Master Configuration 链接保存此设置。
- 在控制台的左边选择 Enterprise Applications。
- 选择 rbexamples 并单击 start。
- 连接到以下 URL:http://locahost:9080/rbexamples/jspindex.htm。
在 WebSphere Application Server 上安装了 rbexamples.war 包之后,必须为该项目设置数据库。此应用程序使用了 RBEXAMPLES UniVerse 帐号中的 EMPLOYEES 表。而数据库是在安装 RedBack Scheduler 期间创建的。
以下 UniVerse BASIC 程序构建了一个语法文件,VoiceXML 应用程序将使用此文件来确认所说的名称。先将代码剪切并粘贴到一个文本编辑器中。然后修改变量 WDIR,使其匹配您的 WebSphere 目录路径。接着将编辑好的程序代码保存到 rbexamples UniVerse 目录下的 BP 目录中。最后从 REBEXAMPLES UniVerse 帐号编译并运行程序 BUILDGRAMFILE。BUILDGRAMFILE 程序将构建语法文件 name.gram,该文件包含 EMPLOYEES 表中姓氏的列表,此列表的格式是一种 Voice Server SDK 可以处理的格式。
BUILDGRAMFILE
BUILDGRAMFILE
10 PRINT "THIS PROGRAM WILL CREATE THE name.gram FILE FOR VOICE ACCESS"
PRINT "INPUT Y/N TO CONTINUE :":;INPUT X
IF X = "N" THEN STOP
IF X # "Y" THEN GOTO 10
* The path for WebSphere rbexamples directory may be different on your pc so change it accordingly
WDIR="C:\WEBSPHERE\APPSERVER\INSTALLEDAPPS\LOCALHOST\RBEXAMPLES.EAR\RBEXAMPLES.WAR"
* Remove the old name.gram file from the and create a empty file
CMD = "DOS /c rm ":WDIR"\name.gram"
EXECUTE CMD
CMD = "DOS /c touch ":WDIR:"\name.gram"
EXECUTE CMD
**********
* Grammar string needed
*********
CR=CHAR(13)
GRAM="#JSGF V1.0 ;":CR
GRAM=GRAM:"grammar name ;":CR
GRAM=GRAM:'public <rule1>= ':CR
* Now open name.gram file
OPENSEQ WDIR:"/name.gram" TO NAME
ELSE PRINT "PROBLEM OPENING name.gram";STOP
* Now open the EMPLOYEES table and select all records
OPEN "","EMPLOYEES" TO EMP
ELSE PRINT "PROBLEM OPENING EMPLOYEES FILE";STOP
SELECT EMP
FIRST=1
* Now loop and read each employee record and build array containing LAST.NAME
100 READNEXT ID ELSE GOTO 200
READV LAST.NAME FROM EMP,ID,2 ELSE GOTO 100
IF FIRST THEN
FIRST=0
GRAM=GRAM:LAST.NAME:CR
END ELSE
GRAM=GRAM:"|":LAST.NAME:CR
END
GOTO 100
200 *
GRAM=GRAM:";"
* Now write the array to the name.gram file
WRITESEQ GRAM ON NAME THEN PRINT "OK"
ELSE PRINT "PROBLEM WRITING name.gram"
CLOSESEQ NAME
PRINT "DONE WRITE"
STOP
Listing EMPLOYEES Table by LAST.NAME
EMPLOYEES. Last Name..
1012 Berry
1015 Byrne
1014 Caddick
1007 Chazan
1009 Delas
1019 Drury
1013 Fox
1023 Gassis
1024 Green
1020 Jenkins
1005 Joarder
1017 Kaynor
1006 Kennedy
1011 Kesic
1008 Kontorovich
1010 La
1022 MacKenzie
1004 Prinz
1016 Rabinovitch
1002 Reyburn
1018 Smith
1003 Teo
1001 Wedewer
23 records listed
|
接下来,您需要创建文件 rbwelcome.vxml 和 u2peoplesearch.jsp。复制下面所示的示例代码,将它粘贴到一个文本编辑器中,并将每个文件保存到 rbexamples.war 目录中(例如,c:\WebSphere\AppServer\installedApps\localhost\rbexamples.ear\rbexamples.war)。此目录是在 WebSphere Application Server 中安装 rbexamples.war 时创建的。这个目录也是 UniVerse BASIC 程序 BUILDGRAMFILE 编写 name.gram 文件的地方。
rbwelcome.vxml
<vxml version="1.0">
<var name="name">
<var name="decide">
<form id="rbwelcome" >
<-- welcome Message -->
<block>
HELLO AND WELCOME TO IBM'S U2 PEOPLE SEARCH APPLICATION.
<block>
<field name="LASTNAME">
<grammar src = "name.gram" />
<!-- Prompt user for input -->
<prompt>
Please say the last name of the person you want to search for.
<prompt>
<noinput>
<prompt>
I did not hear you. Please say the last name of the person you want to search for.
<prompt>
</noinput>
<help>
Please say the last name of the person you want to search for.
</help>
<!-- If input received reconfirm with user -->
<filled>
<assign name="name" expr="LASTNAME"/>
<prompt>
I heard you say <value expr="name"/>
</prompt>
<goto next="#confirm"/>
</filled>
</field>
</form>
<-- Reconfirm form which sends the request back to server if the user confirms the last name -->
<form id="confirm" >
<field name="test" type="boolean">
<prompt>
Would you like to continue.
<prompt>
<noinput>
<reprompt/>
</noinput>
<help>
Please say yes to confirm or No to search again.
<help>
<filled>
<if cond="test == true">
<prompt>
Please wait as I process your request.
</prompt>
<submit next= "http://localhost/rbexamples/u2peoplesearch.jsp" namelist="name"/>
<else/>
<goto next="#rbwelcome"/>
<if>
</filled>
</field>
</form>
</vxml>
|
u2peoplesearch.jsp
<vxml version="1.0">
<%@ page import = "com.ibm.redback.redbeans.*" %>
<jsp:useBean id="connection" class="com.ibm.redback.redbeans.Connection" scope="session"/>
<jsp:setProperty name="connection" property="connectionURL" value="rbexamples"/>
<%@ page isThreadSafe="false" import="java.util.*"
errorPage="error.jsp" %>
<%
// Get the user last name and instantiate the EmployeeList object.
String name = request.getParameter("name");
com.ibm.redback.redbeans.RedObject obj = null; // handle to this page's redobject
com.ibm.redback.redbeans.RecordSet rs = null;
obj = new com.ibm.redback.redbeans.RedObject();
obj.setActiveConnection(connection);
obj.setRBOClass("EXMOD:EmployeeList");
obj.open();
obj.setProperty("select_criteria", "LAST.NAME = \""+ name + "\"");
// Now do selection
rs = obj.callMethod("Select");
String maxr = obj.getProperty("MaxRows");
<form id="results">
<block>
// output number of records found
Records found = <%= obj.getProperty("MaxRows")%>
The person you searched for
<%
if(rs.isEOF()){
out.println("could not be found. Good Bye");
}
else{
// script for each employee row
rs.moveFirst();
out.println(rs.getProperty("FIRST.NAME") +" "+rs.getProperty("LAST.NAME")+".");
// if more than one employee match output each -
for (int i = 2; i <= rs.getPageSize() ; i++){
rs.moveNext();
out.println(rs.getProperty("FIRST.NAME") +" "+rs.getProperty("LAST.NAME") + ".");
}
}
%>
</block>
</form>
</vxml>
</code </vxml>
|

 |

|
运行应用程序
- 在命令行窗口中,转至安装 Voice Server SDK 的目录。默认目录是 C:\Program Files\VoiceServerSDK\bin\。
- 在 bin 目录中输入以下 DOS 命令:
vsaudio_en_US "http://localhost:9080/rbexamples/rbwelcome.vxml"
- 在第一次运行该程序时,会有一个向导帮助您设置麦克风。这之后,将显示一个对话框,如下所示:
系统:您好,欢迎使用 IBM 的 U2 People Search Application(人员查找应用程序)。请说出您想要查找人员的姓氏。
用户:[说出想要查找人员的姓氏]
- 这时,系统将对照外部语法文件 name.gram 检验该姓氏。
- 在完成语法检验之后,系统将执行 rbwelcome.vxml 文件中的
<filled> 元素。
系统:我听见您说 $userInput,您希望继续吗?
用户:是的
- 通过使用 VoiceXML 标签
<submit>,系统将请求发送给 u2peoplesearch.jsp。并使用 namelist 属性将这个人的姓氏发送给 JSP 页面。
- 为了使 VoiceServer SDK 能够执行 JSP 页面中的 Voice 标签,代码的第一行应该是
<vxml version="1.0">。要获得用户从请求对象口头提供的名称,需要使用以下语法:
String name = request.getParameter("name");
- 该示例使用
com.ibm.redback.redbeans.RedObject 类实例化了调用 EmployeeList 的 rbexamples U2 帐号中的现有 RedBack 业务对象,并调用了传递标准 LAST.NAME = name 的方法 Select。
- Voice Server SDK 处理来自查询的结果,并告诉用户所选中员工的名和姓。
结束语
IBM 的 Voice Server SDK 和 RedBack 产品使得将语音识别功能集成到 U2 数据库管理系统中变得非常简单。稍微定制一下这里提供的示例,就可以为您的用户提供对您的数据的语音访问。
参考资料
关于作者  | 
|  | Louis Tur 是 IBM 的一名 IT 专家,负责为 ISV 提供关于 U2 产品的技术销售支持。 |
对本文的评价
|