InputItemRecognitionEventStrategy extends AbstractInputItemEventStrategy<FieldFormItem> implements EventStrategyPrototype { @Override protected boolean handleEvent(final FieldFormItem field, final JVoiceXMLEvent event) throws JVoiceXMLEvent { final RecognitionEvent recognitionEvent = (RecognitionEvent) event; final RecognitionResult result = recognitionEvent .getRecognitionResult(); setApplicationLastResult(result); final VoiceXmlInterpreterContext ctx = getVoiceXmlInterpreterContext(); final String confidencelevel = ctx .getProperty("confidencelevel", "0.5"); float level; try { level = Float.parseFloat(confidencelevel); } catch (Exception e) { throw new SemanticError("The <property>'s confidencelevel '" + confidencelevel + "'could not be parsed.", e); } if (result.getConfidence() < level) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("result not accepted: " + "confidence was too low: " + "expected: " + level + ", " + "actual: " + result.getConfidence()); } throw new NomatchEvent(recognitionEvent.getSource(), recognitionEvent.getSessionId(), result); } if (!result.isAccepted()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("result not accepted"); } return false; } return true; } InputItemRecognitionEventStrategy();  InputItemRecognitionEventStrategy(
            final VoiceXmlInterpreterContext ctx,
            final VoiceXmlInterpreter interpreter,
            final FormInterpretationAlgorithm algorithm,
            final FormItem formItem); @Override EventStrategy newInstance(final VoiceXmlInterpreterContext ctx,
            final VoiceXmlInterpreter interpreter,
            final FormInterpretationAlgorithm fia, final FormItem item);  }
@Test public void testHandleEvent() throws Exception, JVoiceXMLEvent { final VoiceXmlDocument document = new VoiceXmlDocument(); final Vxml vxml = document.getVxml(); final Form form = vxml.appendChild(Form.class); final Field field = form.appendChild(Field.class); field.setName("field"); final RecognitionResult result = Mockito.mock(RecognitionResult.class); Mockito.when(result.getUtterance()).thenReturn("hello world"); Mockito.when(result.isAccepted()).thenReturn(true); Mockito.when(result.getConfidence()).thenReturn(0.55f); Mockito.when(result.getMode()).thenReturn(ModeType.VOICE); Mockito.when(context.getProperty("confidencelevel", "0.5")).thenReturn( "0.5"); final FieldFormItem formItem = new FieldFormItem(context, field); final InputItemRecognitionEventStrategy strategy = new InputItemRecognitionEventStrategy( context, interpreter, null, formItem); final JVoiceXMLEvent event = new RecognitionEvent(null, null, result); final boolean handled = strategy.handleEvent(formItem, event); Assert.assertTrue("event should have been handled", handled); Mockito.verify(context.getDataModel()).createVariable( "lastresult$.utterance", "hello world", Scope.APPLICATION); }
@Test public void testHandleEventNotHandledNoMatch() throws Exception, JVoiceXMLEvent { final VoiceXmlDocument document = new VoiceXmlDocument(); final Vxml vxml = document.getVxml(); final Form form = vxml.appendChild(Form.class); final Field field = form.appendChild(Field.class); field.setName("field"); final RecognitionResult result = Mockito.mock(RecognitionResult.class); Mockito.when(result.getUtterance()).thenReturn("hello world"); Mockito.when(result.isAccepted()).thenReturn(false); Mockito.when(result.getConfidence()).thenReturn(0.55f); Mockito.when(result.getMode()).thenReturn(ModeType.VOICE); Mockito.when(context.getProperty("confidencelevel", "0.5")).thenReturn( "0.5"); final FieldFormItem formItem = new FieldFormItem(context, field); final InputItemRecognitionEventStrategy strategy = new InputItemRecognitionEventStrategy( context, interpreter, null, formItem); final JVoiceXMLEvent event = new RecognitionEvent(null, null, result); final boolean handled = strategy.handleEvent(formItem, event); Assert.assertFalse("event should not have been handled", handled); Mockito.verify(context.getDataModel()).createVariable( "lastresult$.utterance", "hello world", Scope.APPLICATION); }
@Test(expected = NomatchEvent.class) public void testHandleEventNotHandledBelowConfidence() throws Exception, JVoiceXMLEvent { final VoiceXmlDocument document = new VoiceXmlDocument(); final Vxml vxml = document.getVxml(); final Form form = vxml.appendChild(Form.class); final Field field = form.appendChild(Field.class); field.setName("field"); final RecognitionResult result = Mockito.mock(RecognitionResult.class); Mockito.when(result.getUtterance()).thenReturn("hello world"); Mockito.when(result.isAccepted()).thenReturn(true); Mockito.when(result.getConfidence()).thenReturn(0.55f); Mockito.when(result.getMode()).thenReturn(ModeType.VOICE); Mockito.when(context.getProperty("confidencelevel", "0.5")).thenReturn( "0.6"); final FieldFormItem formItem = new FieldFormItem(context, field); final InputItemRecognitionEventStrategy strategy = new InputItemRecognitionEventStrategy( context, interpreter, null, formItem); final JVoiceXMLEvent event = new RecognitionEvent(null, null, result); strategy.handleEvent(formItem, event); }