📄 baserecognizer.java
字号:
/** * Utility function to generate FOCUS_GAINED event and post it * to the event queue. Eventually fireFocusGained will be called * by dispatchSpeechEvent as a result of this action. */ protected void postFocusGained(long oldState, long newState) { SpeechEventUtilities.postSpeechEvent( this, new RecognizerEvent(this, RecognizerEvent.FOCUS_GAINED, oldState, newState, null)); } /** * Utility function to generate FOCUS_GAINED event and * send it to all recognizer listeners. */ public void fireFocusGained(RecognizerEvent event) { if (engineListeners == null) { return; } Enumeration E = engineListeners.elements(); while (E.hasMoreElements()) { EngineListener el = (EngineListener) E.nextElement(); if (el instanceof RecognizerListener) { ((RecognizerListener) el).focusGained(event); } } } /** * Utility function to generate FOCUS_LOST event and post it * to the event queue. Eventually fireFocusLost will be called * by dispatchSpeechEvent as a result of this action. */ protected void postFocusLost(long oldState, long newState) { SpeechEventUtilities.postSpeechEvent( this, new RecognizerEvent(this, RecognizerEvent.FOCUS_LOST, oldState, newState, null)); } /** * Utility function to generate FOCUS_LOST event and * send it to all recognizer listeners. */ public void fireFocusLost(RecognizerEvent event) { if (engineListeners == null) { return; } Enumeration E = engineListeners.elements(); while (E.hasMoreElements()) { EngineListener el = (EngineListener) E.nextElement(); if (el instanceof RecognizerListener) { ((RecognizerListener) el).focusLost(event); } } } /** * Utility function to generate RECOGNIZER_PROCESSING event and post it * to the event queue. Eventually fireRecognizerProcessing will be called * by dispatchSpeechEvent as a result of this action. */ protected void postRecognizerProcessing(long oldState, long newState) { SpeechEventUtilities.postSpeechEvent( this, new RecognizerEvent(this, RecognizerEvent.RECOGNIZER_PROCESSING, oldState, newState, null)); } /** * Utility function to generate RECOGNIZER_PROCESSING event and * send it to all recognizer listeners. */ public void fireRecognizerProcessing(RecognizerEvent event) { if (engineListeners == null) { return; } Enumeration E = engineListeners.elements(); while (E.hasMoreElements()) { EngineListener el = (EngineListener) E.nextElement(); if (el instanceof RecognizerListener) { ((RecognizerListener) el).recognizerProcessing(event); } } } /** * Utility function to generate RECOGNIZER_SUSPENDED event and post it * to the event queue. Eventually fireRecognizerSuspended will be called * by dispatchSpeechEvent as a result of this action. */ protected void postRecognizerSuspended(long oldState, long newState) { SpeechEventUtilities.postSpeechEvent( this, new RecognizerEvent(this, RecognizerEvent.RECOGNIZER_SUSPENDED, oldState, newState, null)); } /** * Utility function to generate RECOGNIZER_SUSPENDED event and * send it to all recognizer listeners. */ public void fireRecognizerSuspended(RecognizerEvent event) { if (engineListeners == null) { return; } Enumeration E = engineListeners.elements(); while (E.hasMoreElements()) { EngineListener el = (EngineListener) E.nextElement(); if (el instanceof RecognizerListener) { ((RecognizerListener) el).recognizerSuspended(event); } } }//////////////////////// End utility methods for sending RecognizerEvents////////////////////// /** * Cycle through each rule in each grammar * calling the method changeRule for each method * that had changed or is new. Also make a list of * all enabled rules. * * Nominally calling changeRule method will propogate * the new rule to the underlying recognizer * */ protected void commitChangeInternal() { boolean haveChanges = false; Vector enabled = new Vector(); RuleGrammar G[] = listRuleGrammars(); if (!supportsNULL || !supportsVOID) { try { G = RecognizerUtilities.transform(G,!supportsNULL,!supportsVOID); } catch (GrammarException e) { e.printStackTrace(); return; } } for (int i=0; i < G.length; i++) { BaseRuleGrammar JG = (BaseRuleGrammar) G[i]; if (JG.grammarChanged) { haveChanges=true; break; } } // if (reloadAll) haveChanges=true; if (haveChanges) { startGrammarChanges(); } // find and output rules that have changes for (int i=0; i < G.length; i++) { String gname = G[i].getName(); BaseRuleGrammar JG = (BaseRuleGrammar)G[i]; String rnames[] = G[i].listRuleNames(); for (int j=0; j < rnames.length; j++) { String ruleName = rnames[j]; if (G[i].isEnabled(ruleName)) { enabled.addElement(gname + "_" + ruleName); } if (!haveChanges || (!JG.isRuleChanged(ruleName) && !reloadAll)) { continue; } JG.setRuleChanged(ruleName,false); boolean isPublic = false; try { isPublic = G[i].isRulePublic(ruleName); } catch (IllegalArgumentException nse) { } Rule rule = G[i].getRule(ruleName); currentGrammar = G[i]; changeRule(gname,ruleName,rule,isPublic); } JG.grammarChanged = false; JG.postGrammarChangesCommitted(); // send events } if (haveChanges) { endGrammarChanges(); } changeEnabled(enabled); } /** * Called at the start of the commit process. */ protected void startGrammarChanges() { } /** * Called at the end of the commit process. */ protected void endGrammarChanges() { } /** * Called with list of rules that should be enabled. */ protected void changeEnabled(Vector enabled) { } /** * Called to propogate new rule to underying recognizer. */ protected void changeRule(String gname, String ruleName, Rule rule, boolean isPublic) { } /** * Check each grammar and load and imported grammars * that are not already loaded. */ static public void loadAllImports(Recognizer R) throws GrammarException, IOException { RuleGrammar rlist[] = R.listRuleGrammars(); for (int i=0; i<rlist.length; i++) { loadImports(R,rlist[i],null,false,false,null); } } /** * Load grammars imported by the specified RuleGrammar * if they are not already loaded. */ static private void loadImports(Recognizer R, RuleGrammar G,URL context, boolean recurse,boolean relo,Vector grams) throws GrammarException, IOException { RuleGrammar G2=null; RuleName imports[] = G.listImports(); if (imports != null) { for (int i=0; i<imports.length; i++) { //RecognizerUtilities.debugMessageOut("Checking import " + // imports[i].getRuleName()); String gname = imports[i].getFullGrammarName(); RuleGrammar GI = R.getRuleGrammar(gname); if (GI == null) { URL grammarURL = gnameToURL(context,imports[i].getFullGrammarName()); // RecognizerUtilities.debugMessageOut("loading " + grammarURL); G2 = JSGFParser.newGrammarFromJSGF(grammarURL, R); if (G2 == null) { RecognizerUtilities.debugMessageOut( "ERROR LOADING GRAMMAR " + grammarURL); } else { if (grams!=null) grams.addElement(G2); if (recurse) loadImports(R,G2,context,recurse,relo,grams); } } } } } /** * Resolve and linkup all rule references contained * in all rules in all grammars. */ protected void linkGrammars() throws GrammarException { RuleGrammar rlist[] = listRuleGrammars(); for (int i=0; i<rlist.length; i++) { ((BaseRuleGrammar)(rlist[i])).resolveAllRules(); } } /** * Determine if the Recognizer has any modal grammars. This * sets the global flag named hasModalGrammars. */ protected void checkForModalGrammars() { hasModalGrammars = false; if (grammarList == null) { return; } Enumeration e = grammarList.elements(); while (e.hasMoreElements()) { RuleGrammar rg = (RuleGrammar) e.nextElement(); if (rg.getActivationMode() == Grammar.RECOGNIZER_MODAL) { hasModalGrammars = true; return; } } } /** * Determine if the given Grammar is active. This is a combination * of the enabled state and activation modes of the Grammar as well * as the current focus state of the recognizer. NOT JSAPI. */ protected boolean isActive(Grammar grammar) { //[[[WDW - check engineState?]]] if (!grammar.isEnabled()) { return false; } else if (grammar.getActivationMode() == Grammar.GLOBAL) { return true; } else if (testEngineState(FOCUS_ON)) { if (grammar.getActivationMode() == Grammar.RECOGNIZER_MODAL) { return true; } else if (!hasModalGrammars) { return true; } } return false; } /** * Notify any grammars if their activation state has been changed. */ protected void notifyGrammarActivation() { if (grammarList == null) { return; } Enumeration e = grammarList.elements(); while (e.hasMoreElements()) { BaseRuleGrammar rg = (BaseRuleGrammar) e.nextElement(); boolean active = isActive(rg); if (active != rg.grammarActive) { rg.grammarActive = active; if (active) { rg.postGrammarActivated(); } else { rg.postGrammarDeactivated(); } } } } /** * Factory constructor for <code>EngineProperties</code> object. * * @return a <code>BaseEngineProperties</code> object specific to * a subclass. */ protected BaseEngineProperties createEngineProperties() { return null; } /** * Called from the <code>resume</code> method. Override in subclasses. */ protected void handleResume() { } /** * Called from the <code>pause</code> method. Override this in subclasses. */ protected void handlePause() { } /** * Called from the <code>allocate</code> method. Override this in * subclasses. * * @see #allocate * * @throws EngineException if problems are encountered */ protected void handleAllocate() throws EngineException { } /** * Called from the <code>deallocate</code> method. Override this in * subclasses. * * @throws EngineException if this <code>Engine</code> cannot be * deallocated. */ protected void handleDeallocate() throws EngineException { } /** * Dispatch a SpeechEvent. This is a method from SpeechEventDispatcher. * The dispatcher should notify all listeners of the speech event * from this method. */ public void dispatchSpeechEvent(SpeechEvent event) { switch (event.getId()) { case RecognizerEvent.CHANGES_COMMITTED: fireChangesCommitted((RecognizerEvent) event); break; case RecognizerEvent.FOCUS_GAINED: fireFocusGained((RecognizerEvent) event); break; case RecognizerEvent.FOCUS_LOST: fireFocusLost((RecognizerEvent) event); break; case RecognizerEvent.RECOGNIZER_PROCESSING: fireRecognizerProcessing((RecognizerEvent) event); break; case RecognizerEvent.RECOGNIZER_SUSPENDED: fireRecognizerSuspended((RecognizerEvent) event); break; case ResultEvent.AUDIO_RELEASED: fireAudioReleased((ResultEvent) event); break; case ResultEvent.GRAMMAR_FINALIZED: fireGrammarFinalized((ResultEvent) event); break; case ResultEvent.RESULT_ACCEPTED: fireResultAccepted((ResultEvent) event); break; case ResultEvent.RESULT_CREATED: fireResultCreated((ResultEvent) event); break; case ResultEvent.RESULT_REJECTED: fireResultRejected((ResultEvent) event); break; case ResultEvent.RESULT_UPDATED: fireResultUpdated((ResultEvent) event); break; case ResultEvent.TRAINING_INFO_RELEASED: fireTrainingInfoReleased((ResultEvent) event); break; // Defer to BaseEngine to handle the rest. // default: super.dispatchSpeechEvent(event); break; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -