⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 api_gen.cpp

📁 一个非常好的人工智能开发工具开源软件
💻 CPP
字号:
/*** *** See the file "L2_RTI_EO1/disclaimers-and-notices-L2.txt" for *** information on usage and redistribution of this file, *** and for a DISCLAIMER OF ALL WARRANTIES. ***//* $Id: api_gen.cpp,v 1.2 2004/11/09 00:40:05 shayden Exp $ */#include <debuggers/L2_string_map.h>#include <debuggers/tracker_debug.h>#include <transition/transitioned.h>#include <debuggers/livingstone_debug.h>#include <debuggers/tracker_debug.h>#include <livingstone/version.h>#include <mba_utils/MBA_string.h>#include <mba_utils/slist_iterator.h>#include <transition/T_system.h>#include <stdlib.h>#include <fstream.h>#include <iostream.h>/** The file output stream for ./enumapi.h */ofstream fout("enumapi.h");/** * Substitutes each occurrence of '.' with '_', since C++ identifiers can * not contain the '.' character. */MBA_string convert_to_enum_name(const MBA_string& name) {    const char *src = name.c_str();    char * mystring = new char [name.size()+1];    for(unsigned i=0; src[i] ; ++i) {        if (src[i] == '.')            mystring[i] = '_';        else            mystring[i] = src[i];    }    mystring[name.size()] = '\0';    MBA_string ret(mystring);    delete [] mystring;    return ret;}/** * For each Assignable or Transitioned in the list, write to fout an enum * definition, each of the form * enum var_values { *      var_value0 = 0, *      var_value1 = 1 * }; */void write_domain(Variable *var, T_system_debug &system_debug) {    MBA_string obsname =      convert_to_enum_name(system_debug.get_var_name(*var));    // The first one:    // enum <name>_values {    //   <name>_<value> = 0    fout << "enum "	 << obsname	 << "_values"	 << " {\n    "	 << obsname	 << "_"	 << system_debug.get_variable_value(*var, 0)	 << "="	 << 0;    // Later ones have a preceding comma:    // ,    // <name>_<value> = <index>    for (unsigned value = 1; value < var->get_nvalues(); ++value) {      fout << ",\n    "	   << obsname	   << "_" 	   << system_debug.get_variable_value(*var, value)	   << "="	   << value;    }    fout << "\n};\n";}void write_observable_domains(T_system& system, T_system_debug& system_debug) {  // Descend for compatibility  for (unsigned i = 0; i < system.npresent_variables(); i++) {    Variable *pVariable =      system.get_present_variable(system.npresent_variables() - i - 1);    if (pVariable->is_observable()) {      write_domain(pVariable, system_debug);    }  }}void write_command_domains(T_system &system, T_system_debug &system_debug) {  // Descend for compatibility  for (unsigned i = 0; i < system.npresent_variables(); i++) {    Variable *pVariable =      system.get_present_variable(system.npresent_variables() - i - 1);    if (pVariable->is_command()) {      write_domain(pVariable, system_debug);    }  }}void write_mode_domains(T_system &system, T_system_debug &system_debug) {  // Descend for compatibility  for (unsigned i = 0; i < system.npresent_variables(); i++) {    Variable *pVariable =      system.get_present_variable(system.npresent_variables() - i - 1);    if (pVariable->is_transitioned()) {      write_domain(pVariable, system_debug);    }  }}/** * Write out variable names. First get the variable pointer from the * conflict_db. Then get its name from the debugger and write it out. */void write_name_header(const MBA_string& name) {  // enum <name>_types {  fout << "enum " << name << "_types {" << endl;}void write_name(Variable *var, T_system_debug& dbg) {  // <name> = <index>,  fout << "    "       << convert_to_enum_name(dbg.get_var_name(*var))       << " = "       << var->get_id()       << ","       << endl;}void write_name_footer(const MBA_string& name, unsigned count) {  // NUM_<name> = <count>  fout << "    NUM_" << name << "_types=" << count << endl << "};" << endl;}void write_observable_names(T_system &system, T_system_debug& dbg) {  write_name_header("observable");  unsigned count = 0;  // Descend for compatibility  for (unsigned i = 0; i < system.npresent_variables(); i++) {    Variable *pVariable =      system.get_present_variable(system.npresent_variables() - i - 1);    if (pVariable->is_observable()) {      write_name(pVariable, dbg);      count++;    }  }  write_name_footer("observable", count);}void write_command_names(T_system &system, T_system_debug& dbg) {  write_name_header("command");  unsigned count = 0;  for (unsigned i = 0; i < system.npresent_variables(); i++) {    Variable *pVariable =      system.get_present_variable(system.npresent_variables() - i - 1);    if (pVariable->is_command()) {      write_name(pVariable, dbg);      count++;    }  }  write_name_footer("command", count);}void write_mode_names(T_system &system, T_system_debug& dbg) {  write_name_header("mode");  unsigned count = 0;  for (unsigned i = 0; i < system.npresent_variables(); i++) {    Variable *pVariable      = system.get_present_variable(system.npresent_variables() - i - 1);    if (pVariable->is_transitioned()) {      write_name(pVariable, dbg);      count++;    }  }  write_name_footer("mode", count);}/** * Now that the model is in memory, map over its assumptions and * assignables and spit out the commands, modes and observables into an * API file ./enumapi.h */void writeToFile(T_system& sys) {  time_t t = time(0);  fout<< "/*\n"      << " * automatically generated on " << ctime(&t) /* ctime has \n */      << " * by pitex_api_gen version " << endl      << " */\n\n";  // Begin guard  //fout<< "#ifndef _ENUMAPI_H_\n"  //<< "#define _ENUMAPI_H_\n";  fout << "#define NUMBER_OF_L2VARS " << sys.npresent_variables() << "\n";  fout << "#define NUMBER_OF_QVALS 10\n";  fout << "\n\n/*  Initial definitions */\n"       << "typedef struct {" << endl       << "   char variable_name_primary[150];" << endl       << "   char variable_values_primary[20][100];" << endl       << "   char variable_name_secondary[150];" << endl       << "   char variable_values_secondary[20][100];" << endl       << "   } L2_var_complex;" << endl       << "L2_var_complex L2_model["<< sys.npresent_variables() <<"] = {" << endl;  // End guard  //fout << "\n#endif\n";}/**   Writes out L2_var_complex data structure which defines the mappings between   var and val indecies and the corresponding names. */void testPrintNameAux(Variable* pVariable, L2_string_map* pL2_string_map) {  const MBA_string variableName =    pL2_string_map->get_variable_name(*pVariable);  cout << "Variable[" << pVariable->get_id() << "]="       << variableName << " ";  const char *src = variableName.c_str();  const char *src_II;  src_II = strchr(src,'#');  if (src_II!=NULL)       fout << "{\"" << (src_II+1) << "\"}," << endl;  else   fout << "{\"" << variableName << "\"}," << endl;  if      (pVariable->is_command())      cout << "Command";  else if (pVariable->is_observable())   cout << "Observable";  else if (pVariable->is_transition())   cout << "Transition";  else if (pVariable->is_transitioned()) cout << "Transitioned";  else if (!pVariable->is_assignable())  cout << "Dependent";  else                                   cout << "UNCLASSIFIED";  cout << endl;  const MBA_string* valueNames =    pL2_string_map->get_variable_values(*pVariable);  fout << "{" << endl;  for (unsigned j = 0; j < pVariable->get_nvalues(); j++) {    const MBA_string valueName = valueNames[j];    cout << "Value[" << j << "]=" << valueName << endl;    fout << "{\"" << valueName << "\"}," << endl;  }  fout << "}," << endl;}int main(int argc,char** argv) {  // Validate command-line arguments  if (argc != 2)  {    cout << "Usage: " << endl;    cout << "       " << argv[0] << " <model-name>" << endl;    return(-1);  } else {    // Read in the model    cout << "Reading " << argv[1] << endl;    Livingstone_debug livingstone;    livingstone.set_filename(argv[1]);        if (livingstone.read_file()) {      livingstone.set_history(0);      livingstone.create_tracker();      livingstone.create_debugger();      if (livingstone.initialize_tracker()) {	Tracker_debug* pTracker_debug = livingstone.get_debugger();	Tracker* pTracker = pTracker_debug->get_tracker();	T_system& t_system = pTracker->get_t_system();	writeToFile(t_system);	L2_string_map* pL2_string_map = livingstone.get_string_mapping();	for (unsigned i = 0; i < t_system.npresent_variables(); i++) {	  fout << "{" << endl;	  Variable* pVariable = t_system.get_present_variable(i);	  testPrintNameAux(pVariable, pL2_string_map);	  if (pVariable->is_transitioned()) {	    Transitioned* pTransitioned = static_cast<Transitioned *>(pVariable);	    	    Transition* pTransition = pTransitioned->get_zenith();	    if (pTransition != NULL) {	      cout <<"TransitionedVariable" << endl;	      testPrintNameAux(pTransition, pL2_string_map);	    }}	  else	    fout << "{},{}," << endl;	  fout << "}," << endl;	}	fout << "};" << endl;      }}    else {      cerr << "Failed to read model file" << endl;    }}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -