GrammarManager.hpp
上传用户:xqtpzdz
上传日期:2022-05-21
资源大小:1764k
文件大小:7k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. /****************License************************************************
  2.  * Vocalocity OpenVXI
  3.  * Copyright (C) 2004-2005 by Vocalocity, Inc. All Rights Reserved.
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  * Vocalocity, the Vocalocity logo, and VocalOS are trademarks or 
  18.  * registered trademarks of Vocalocity, Inc. 
  19.  * OpenVXI is a trademark of Scansoft, Inc. and used under license 
  20.  * by Vocalocity.
  21.  ***********************************************************************/
  22. #ifndef __GRAMMAR_MANAGER_HPP__
  23. #define __GRAMMAR_MANAGER_HPP__
  24. #include "VXIrec.h"
  25. #include "VXIvalue.h"
  26. #include "Scripter.hpp" 
  27. #include <vector>
  28. extern "C" struct VXIrecGrammar;
  29. extern "C" struct VXIrecInterface;
  30. extern "C" struct VXIrecRecordResult;
  31. class GrammarInfo;
  32. class GrammarInfoUniv;
  33. class PropertyList;
  34. class SimpleLogger;
  35. class VXMLElement;
  36. class GrammarManager {
  37. public:
  38.   enum OpType {
  39.     GRAMMAR,            
  40.     RECOGNIZE,
  41.     RECORD,
  42.     TRANSFER
  43.   };
  44.   static void ThrowSpecificEventError(VXIrecResult err, OpType opType);
  45.   
  46.   static const VXIchar * const DTMFTerm;
  47.   static const VXIchar * const FinalSilence;
  48.   static const VXIchar * const MaxTime;
  49.   static const VXIchar * const RecordingType;
  50.   // This function walks through the document, creating grammars as necessary.
  51.   // The id of the document (passed to Enable) is returned.
  52.   //
  53.   // may throw: VXIException::InterpreterError & VXIException::OutOfMemory
  54.   void LoadGrammars(const VXMLElement& doc, vxistring & id, PropertyList &,
  55.     bool isDefaults = false);
  56.   // Deactivates all current grammars.
  57.   void DisableAllGrammars();
  58.   // Activates (for recognition) grammars matching the given dialog & field
  59.   // name.  The documentID is returned by LoadGrammars.
  60.   //
  61.   // Returns: false - no grammars were enabled
  62.   //          true  - at least one grammar is ready
  63.   //
  64.   // may throw VXIException::InterpreterEvent
  65.   bool EnableGrammars(const vxistring & documentID,
  66.                       const vxistring & dialogName,
  67.                       const vxistring & fieldName,
  68.                       const VXIMapHolder & properties,
  69.                       bool isModal,
  70.   Scripter &script,
  71.   PropertyList & propList);
  72.   /** Recognition result */
  73.   enum RecResult {
  74.     Success,       /// Recognition returned a hypothesis
  75.     OutOfMemory,   //  should be gone ??? // Insufficient memory available
  76.     BadMimeType,   //  should be gone ??? // The requested type is not supported
  77.     InternalError  /// VXIrecInterface::Recognize failed gave invalid results
  78.   };
  79.   /**
  80.    * Performs ASR recognition.  The result argument will contain the
  81.    * recognition result, and must be destroyed by the caller by invoking
  82.    * the Destroy function.
  83.    */
  84.   RecResult Recognize(const VXIMapHolder & properties,
  85.                 VXIrecRecognitionResult * & result);
  86.   bool FindMatchedElement(const vxistring & id, VXMLElement & match) const;
  87.   bool FindMatchedElement(const vxistring & id, VXMLElement & match,
  88.                           unsigned long & ginfo) const;
  89.   // Compare the precedence of info1 against info2
  90.   // Returns: 1 if info1 has higher precedence than info2
  91.   //          0 if both has the same precedence
  92.   //         -1 if info2 has higher precedence than info1
  93.   int CompareGrammar(unsigned long info1, unsigned long info2);
  94.   RecResult Record(const VXIMapHolder & properties,
  95.              bool flushBeforeRecord,
  96.              VXIrecRecordResult * & answer);
  97.   // Deletes all current grammars.
  98.   void ReleaseGrammars();
  99.   VXIMap * GetRecProperties(const PropertyList &, int timeout = -1) const;
  100.   VXIMap * GetRecordProperties(const PropertyList &, int timeout = -1) const;
  101.   // may throw: VXIException::OutOfMemory()
  102.   GrammarManager(VXIrecInterface * r, const SimpleLogger & l);
  103.   ~GrammarManager();
  104. private:
  105.   void SetGrammarLoadProperties(const VXMLElement & element, const PropertyList&,
  106.                                 VXIMapHolder & props) const;
  107.   // Invoked by LoadGrammars and called recursively to build grammars.
  108.   //
  109.   // may throw: VXIException::InterpreterError & VXIException::OutOfMemory
  110.   void BuildGrammars(const VXMLElement& doc, const vxistring & id,
  111.                      PropertyList &, VXIMapHolder & flattenedProperties,
  112.                      int menuAcceptLevel = 0);
  113.   // Invoked by LoadGrammars to commit each grammar to the collection.
  114.   //
  115.   // may throw: VXIException::OutOfMemory()
  116.   void AddGrammar(VXIrecGrammar * gr, const vxistring & docID,
  117.                   const VXMLElement & elem, const VXIchar *expr = NULL, const VXIchar *type = NULL);
  118.   void BuildUniversals(const VXMLElement& doc, PropertyList &);
  119.   void AddUniversal(VXIrecGrammar * gr, const VXMLElement & elem,
  120.                     const vxistring & langID, const vxistring & name);
  121.   VXIrecGrammar * BuildInlineGrammar(const VXMLElement & element,
  122.                                      const VXIMapHolder & localProps);
  123.   void BuildOptionGrammars(const vxistring & docID, const VXMLElement& doc,
  124.                            const VXIMapHolder & props);
  125.   
  126.   static bool GetEnclosedText(const SimpleLogger & log,
  127.                               const VXMLElement & doc, vxistring & str);
  128.   static VXIrecGrammar * CreateGrammarFromString(VXIrecInterface * vxirec,
  129.                                                  const SimpleLogger & log,
  130.                                                  const vxistring & source,
  131.                                                  const VXIchar * type,
  132.                                                  const VXIMapHolder & props);
  133.   static VXIrecGrammar * CreateGrammarFromURI(VXIrecInterface * vxirec,
  134.                                               const SimpleLogger & log,
  135.                                               const vxistring & source,
  136.                                               const VXIchar * type,
  137.                                               const VXIMap * fetchProps,
  138.                                               const VXIMapHolder & props);
  139.   unsigned long GetGrammarSequence(void) { return ++grammarSequence; }
  140.   
  141.   void SetInputMode(const VXIchar *mode, VXIMapHolder &map) const;
  142.                                     
  143. private:
  144.   typedef std::vector<GrammarInfo *> GRAMMARS;
  145.   GRAMMARS grammars;
  146.   typedef std::vector<GrammarInfoUniv *> UNIVERSALS;
  147.   UNIVERSALS universals;
  148.   const SimpleLogger & log;
  149.   VXIrecInterface * vxirec;
  150.   unsigned int grammarSequence;
  151. };
  152. #endif