<commit_before>/* 
 * Copyright (C) 2010-2013 by PhonyTive LLC (http://phonytive.com)
 * http://astivetoolkit.org
 *
 * This file is part of Astive Toolkit(ATK)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.astivetoolkit.menu;

import org.astivetoolkit.menu.event.FailEvent;
import org.astivetoolkit.menu.event.MaxTimeoutEvent;
import org.astivetoolkit.menu.event.MaxFailureEvent;
import org.astivetoolkit.menu.event.AuthenticationEvent;
import org.astivetoolkit.menu.event.KeyEvent;
import org.astivetoolkit.menu.event.InterDigitsTimeoutEvent;
import org.astivetoolkit.menu.event.PositionChangeEvent;
import org.astivetoolkit.menu.event.DigitsEvent;
import org.astivetoolkit.menu.event.ActionEvent;
import java.util.*;
import org.apache.log4j.Logger;
import org.astivetoolkit.agi.AgiException;
import org.astivetoolkit.agi.AgiResponse;
import org.astivetoolkit.agi.CommandProcessor;
import org.astivetoolkit.menu.action.Action;

/**
 * Is the heart/engine of Menu API. It eliminate the necessity of loops commonly
 * needed by AGI to create user iteration.
 * 
 * @since 1.0.0
 */
public class MenuNavigator {
  // A usual logging class
  private static final Logger LOG = Logger.getLogger(MenuNavigator.class);
  private AgiResponse agiResponse;
  private Menu currentMenu;
  private boolean answered;
  private boolean autoAnswer;

  /**
   * Creates a new instance of MenuNavigator.
   * 
   * @param agiResponse allows user iteration with telephony system.
   */
  public MenuNavigator(AgiResponse agiResponse) {
    this.agiResponse = agiResponse;    
    autoAnswer = true;
    answered = false;
  }

  private void authenticate(MenuItem item) {
    if (item.getMustAuthenticate()) {
      if (!item.getAuthenticator().isAuthenticated()) {
        AuthenticationEvent evt = new AuthenticationEvent(item, item.getAuthenticator());
        item.getAuthenticator().signIn();

        if (item.getAuthenticator().isAuthenticated()) {
          item.fireAuthenticationEvent_onSuccess(null);
        } else {
          item.fireAuthenticationEvent_onFailure(evt);
        }
      }
    }
  }

  private boolean checkMaxFailure(Menu menu, String opt) {
    if (menu.getFailuresCount() >= menu.getMaxFailures()) {
      MaxFailureEvent event = new MaxFailureEvent(menu, opt, menu.getMaxFailures());
      menu.fireMaxFailureEvent_maxFailurePerform(event);

      // do break the flow
      return true;
    }

    return false;
  }

  private boolean checkMaxTimeout(Menu menu, String opt) {
    if (menu.getTimeoutCount() >= menu.getMaxTimeouts()) {
      MaxTimeoutEvent event = new MaxTimeoutEvent(menu, opt, menu.getMaxTimeouts());
      menu.fireMaxTimeoutEvent_maxTimeoutPerform(event);

      // do break the flow
      return true;
    }

    return false;
  }

  private ArrayList<String> getChildsKeys(ArrayList<MenuItem> menuChilds) {
    ArrayList<String> result = new ArrayList<String>();<commit_msg>Update MenuNavigator object.<commit_after>/* 
 * Copyright (C) 2010-2013 by PhonyTive LLC (http://phonytive.com)
 * http://astivetoolkit.org
 *
 * This file is part of Astive Toolkit(ATK)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.astivetoolkit.menu;

import org.astivetoolkit.menu.event.FailEvent;
import org.astivetoolkit.menu.event.MaxTimeoutEvent;
import org.astivetoolkit.menu.event.MaxFailureEvent;
import org.astivetoolkit.menu.event.AuthenticationEvent;
import org.astivetoolkit.menu.event.KeyEvent;
import org.astivetoolkit.menu.event.InterDigitsTimeoutEvent;
import org.astivetoolkit.menu.event.PositionChangeEvent;
import org.astivetoolkit.menu.event.DigitsEvent;
import org.astivetoolkit.menu.event.ActionEvent;
import java.util.*;
import org.apache.log4j.Logger;
import org.astivetoolkit.agi.AgiException;
import org.astivetoolkit.agi.AgiResponse;
import org.astivetoolkit.agi.CommandProcessor;
import org.astivetoolkit.menu.action.Action;

/**
 * Is the heart/engine of Menu API. It eliminate the necessity of loops commonly
 * needed by AGI to create user iteration.
 * 
 * @since 1.0.0
 */
public class MenuNavigator {
  // A usual logging class
  private static final Logger LOG = Logger.getLogger(MenuNavigator.class);
  private AgiResponse agiResponse;
  private Menu currentMenu;
  private boolean answered;
  private boolean autoAnswer;

  /**
   * Creates a new instance of MenuNavigator.
   * 
   * @param agiResponse allows user iteration with telephony system.
   */
  public MenuNavigator(AgiResponse agiResponse) {
    this.agiResponse = agiResponse;    
    autoAnswer = true;
    answered = false;
  }

  private void authenticate(MenuItem item) {
    if (item.getMustAuthenticate()) {
      if (!item.getAuthenticator().isAuthenticated()) {
        AuthenticationEvent evt = new AuthenticationEvent(item, item.getAuthenticator());
        item.getAuthenticator().signIn();

        if (item.getAuthenticator().isAuthenticated()) {
          item.fireAuthenticationEvent_onSuccess(null);
        } else {
          item.fireAuthenticationEvent_onFailure(evt);
        }
      }
    }
  }

  private boolean checkMaxFailure(Menu menu, String opt) {
    if (menu.getFailuresCount() >= menu.getMaxFailures()) {
      MaxFailureEvent event = new MaxFailureEvent(menu, opt, menu.getMaxFailures());
      menu.fireMaxFailureEvent_maxFailurePerform(event);

      // do break the flow
      return true;
    }

    return false;
  }

  private boolean checkMaxTimeout(Menu menu, String opt) {
    if (menu.getTimeoutCount() >= menu.getMaxTimeouts()) {
      MaxTimeoutEvent event = new MaxTimeoutEvent(menu, opt, menu.getMaxTimeouts());
      menu.fireMaxTimeoutEvent_maxTimeoutPerform(event);

      // do break the flow
      return true;
    }

    return false;
  }

  private ArrayList<String> getChildsKeys(ArrayList<MenuItem> menuChilds) {
    ArrayList<String> result = new ArrayList<>();
{'fraction_non_alphanumeric': 0.09447104919460166, 'fraction_numerical': 0.004643738209258453, 'mean_word_length': 3.867231638418079, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6346630', 'n_tokens_mistral': 2047, 'n_tokens_neox': 1998, 'n_words': 543}