ReturnType Recognizer::addGrammar(Integers& id, const Grammar& grammar) {
    if (decoder == NULL) return BAD_STATE;
    std::ostringstream grammar_name;
    grammar_name << grammar_index;
    grammar_names.push_back(grammar_name.str());
    current_grammar = fsg_model_init(grammar_names.back().c_str(), logmath, 1.0, grammar.numStates);
    if (current_grammar == NULL)
      return RUNTIME_ERROR;
    current_grammar->start_state = grammar.start;
    current_grammar->final_state = grammar.end;
    for (int i=0;i<grammar.transitions.size();i++) {
      if (grammar.transitions.at(i).word.size() == 0)
	fsg_model_null_trans_add(current_grammar, grammar.transitions.at(i).from, grammar.transitions.at(i).to, grammar.transitions.at(i).logp);
      else
	fsg_model_trans_add(current_grammar, grammar.transitions.at(i).from, grammar.transitions.at(i).to, grammar.transitions.at(i).logp, fsg_model_word_add(current_grammar, grammar.transitions.at(i).word.c_str()));
    }
    fsg_model_add_silence(current_grammar, "<sil>", -1, 1.0);
    if (current_grammar != fsg_set_add(grammar_set, grammar_names.back().c_str(), current_grammar)) {
      return RUNTIME_ERROR;
    }
    if (id.size() == 0) id.push_back(grammar_index);
    else id.at(0) = grammar_index;
    grammar_index++;
    ps_update_fsgset(decoder);
    fsg_model_t * fsg = fsg_set_select(grammar_set, grammar_names.back().c_str());
    if (fsg == NULL)
      return RUNTIME_ERROR;
    if (ps_update_fsgset(decoder) == NULL)
      return RUNTIME_ERROR;
    return SUCCESS;
  }
static void test_reinit_fsg()
{
       ps_decoder_t *ps;
       cmd_ln_t *config;
       config = cmd_ln_init(NULL, ps_args(), TRUE,
			    "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
			    "-lm", MODELDIR "/lm/en_US/wsj0vp.5000.DMP",
			    "-dict", MODELDIR "/lm/en_US/cmu07a.dic",
                            NULL);
       ps = ps_init(config);
       ps_update_fsgset (ps);
       ps_update_fsgset (ps);
       ps_free(ps);
       cmd_ln_free_r(config);
}
 ReturnType Recognizer::switchGrammar(int id) {
   if (decoder == NULL) return BAD_STATE;
   std::ostringstream grammar_name;
   grammar_name << id;
   fsg_model_t * fsg = fsg_set_select(grammar_set, grammar_name.str().c_str());
   if (fsg == NULL)
     return RUNTIME_ERROR;
   if (ps_update_fsgset(decoder) == NULL)
     return RUNTIME_ERROR;
   return SUCCESS;
 }
static void test_reinit_fsg_missing()
{
       ps_decoder_t *ps;
       cmd_ln_t *config;
       config = cmd_ln_init(NULL, ps_args(), TRUE,
			    "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
			    "-dict", MODELDIR "/lm/en_US/cmu07a.dic",
                            NULL);
       ps = ps_init(config);
       cmd_ln_set_str_r(config, "-fsg", "/some/fsg");
       ps_update_fsgset (ps);
       ps_free(ps);
       cmd_ln_free_r(config);
}
void gst_sphinx_sink_set_grammar (GstSphinxSink *sink, GSList *phrases)
{	
	fsg_set_t *fsgs;

	if (sink->ad.listening)
		return;
		
	sink->fsg = gst_sphinx_construct_fsg (sink, phrases);

        fsgs = ps_get_fsgset(sink->decoder);
        fsg_set_remove_byname(fsgs, "desktop-control");
        fsg_set_add(fsgs, fsg_model_name(sink->fsg), sink->fsg);
        if (!fsg_set_select(fsgs, fsg_model_name(sink->fsg))) {
#if DEBUG
	   g_message("Failed to configure grammar");
#endif
           exit(1);
        }

        ps_update_fsgset (sink->decoder);
#if DEBUG
	g_message ("New fsg is set");
#endif
}
  ReturnType Recognizer::init(const Config& config) {
    parseStringList(HMM_FOLDERS, &acoustic_models, &default_acoustic_model);
#ifdef LM_FILES
    parseStringList(LM_FILES, &language_models, &default_language_model);
#endif /* LM_FILES */
#ifdef DICT_FILES
    parseStringList(DICT_FILES, &dictionaries, &default_dictionary);
#endif /* DICT_FILES */

    const arg_t cont_args_def[] = {
      POCKETSPHINX_OPTIONS,
      { "-argfile",
	ARG_STRING,
	NULL,
	"Argument file giving extra arguments." },
      { "-adcdev",
	ARG_STRING,
	NULL,
	"Name of audio device to use for input." },
      { "-infile",
	ARG_STRING,
	NULL,
	"Audio file to transcribe." },
      { "-time",
	ARG_BOOLEAN,
	"no",
	"Print word times in file transcription." },
      CMDLN_EMPTY_OPTION
    };

    std::map<std::string, std::string> parameters;
    for (int i=0 ; i< config.size() ; ++i)
      parameters[config[i].key] = config[i].value;
    
    if ((parameters.find("-hmm") == parameters.end()) || (acoustic_models.find(parameters["-hmm"]) == acoustic_models.end()))
      parameters["-hmm"] = default_acoustic_model;
    if (parameters.find("-bestpath") == parameters.end())
      parameters["-bestpath"] = "no";

    int argc = 2 * parameters.size();
    char ** argv = new char*[argc];
    int index = 0;
    for (StringsMapIterator i = parameters.begin() ; i != parameters.end(); ++i) {
      if (isValidParameter(i->first, i->second)) {
	if (i->first == "-lm") is_fsg = false;
	argv[index++] = (char*) i->first.c_str();
	argv[index++] = (char*) i->second.c_str();
      }
    }

    cmd_ln_t * cmd_line = cmd_ln_parse_r(NULL, cont_args_def, argc, argv, FALSE);
    if (cmd_line == NULL) {
      delete [] argv;
      return RUNTIME_ERROR;
    }
    decoder = ps_init(cmd_line);
    delete [] argv;
    if (decoder == NULL) {
      return RUNTIME_ERROR;
    }
    if (is_fsg) {
      grammar_set = ps_update_fsgset(decoder);
      if (grammar_set == NULL) {
	return RUNTIME_ERROR;
      }
    }
    logmath = logmath_init(1.0001, 0, 0);
    if (logmath == NULL) {
      return RUNTIME_ERROR;
    }
    return SUCCESS;
  }