📄 symbol_table.cc
字号:
iter++) { sym = (* iter).second; if (sym->is_param_symbol() && !(((const Param_symbol *) sym)->is_symbolic())) { psym = (const Param_symbol *) sym; todo.push_back(psym); } } todo.sort(comparator); for (todo_iter=todo.begin(); todo_iter!=todo.end(); todo_iter++) { psym=(*todo_iter); if (globals->cmd_options->use_matlab_symbolic()) res+=psym->to_matlab() + string(" = sym('") + psym->get_name() + string("');\n"); else res+=psym->to_matlab() + string(" = ") + psym->get_value()->to_matlab() + string(";\n"); } return res;}string Symbol_table::summary(string prefix) const { string res; char buf[100]; res += prefix; sprintf(buf, "nxr = %d;\n", count_symbols(STATE_KIND, REAL_TYPE)); res += buf; res += prefix; sprintf(buf, "nxb = %d;\n", count_symbols(STATE_KIND, BOOL_TYPE)); res += buf; res += prefix; sprintf(buf, "nx = %d;\n", count_symbols(STATE_KIND, REAL_TYPE) + count_symbols(STATE_KIND, BOOL_TYPE)); res += buf; res += prefix; sprintf(buf, "nur = %d;\n", count_symbols(INPUT_KIND, REAL_TYPE)); res += buf; res += prefix; sprintf(buf, "nub = %d;\n", count_symbols(INPUT_KIND, BOOL_TYPE)); res += buf; res += prefix; sprintf(buf, "nu = %d;\n", count_symbols(INPUT_KIND, REAL_TYPE) + count_symbols(INPUT_KIND, BOOL_TYPE)); res += buf; res += prefix; sprintf(buf, "nyr = %d;\n", count_symbols(OUTPUT_KIND, REAL_TYPE)); res += buf; res += prefix; sprintf(buf, "nyb = %d;\n", count_symbols(OUTPUT_KIND, BOOL_TYPE)); res += buf; res += prefix; sprintf(buf, "ny = %d;\n", count_symbols(OUTPUT_KIND, REAL_TYPE) + count_symbols(OUTPUT_KIND, BOOL_TYPE)); res += buf; res += prefix; sprintf(buf, "nd = %d;\n", count_symbols(AUX_KIND, BOOL_TYPE)); res += buf; res += prefix; sprintf(buf, "nz = %d;\n", count_symbols(AUX_KIND, REAL_TYPE)); res += buf; // print S.ne is done in MLD_representation::info return res;}string Symbol_table::param_prefix() const { return string("params.");}string Symbol_table::matlab_symb_param_exist_check() const { const Param_symbol * psym; string checks; bool existsym=false; for (const_symbols_iter iter = symbols.begin(); iter != symbols.end(); iter++) { if ((* iter).second->is_param_symbol()) { psym = (const Param_symbol *) (* iter).second; if (psym->is_terminal_symbolic() && psym->is_used()) { existsym=true; checks += string("if ~"); checks += string("isfield(params, '") + psym->get_name() + string("')"); checks += string("\n\terror('error: symbolic parameter ") + psym->get_name() + string(" not defined in params structure');\n"); checks += string("end\n"); } } } if (existsym) { string paramsc; paramsc=string("if ~exist('params', 'var')\n\terror('error: params not available');\nend\n"); paramsc+=string("if ~isa(params, 'struct')\n\terror('error: params is not a struct');\nend\n"); checks=paramsc + checks; } return checks;}stringSymbol_table::symbs_as_matalb_symbolic() const{ const Param_symbol *psym; string res; for (const_symbols_iter iter=symbols.begin(); iter!=symbols.end(); iter++) { if ((*iter).second->is_param_symbol()) { psym=(const Param_symbol*)(*iter).second; if (psym->is_symbolic() && psym->is_used()) { res+=psym->to_matlab() + string(" = sym('") + psym->get_name() + string("');\n"); } } } return res;} string Symbol_table::matlab_simu_header(string fun_name) const { string res; res = string("function [xn, d, z, y] = ") + fun_name + string("(x, u, params)\n"); res+= string("% [xn, d, z, y] = ") + fun_name + string("(x, u, params)\n") + string("% simulates the hybrid system one step ahead.\n") + string("% Parameters:\n") + string("% x: current state\n") + string("% u: input\n") + string("% params: structure containing values for\n") + string("% all symbolic parameters\n") + string("% Output:\n") + string("% xn: state in the next timestep\n") + string("% u: output\n") + string("% d, z: Boolean and real auxiliary variables\n") + string("%\n"); res += license_note(true); return res;}string Symbol_table::matlab_simu() const { const_symbols_iter iter; const Var_symbol * vsym; string res; char buf[100]; list < const Var_symbol * > aux_symbs; struct comp_order_compare comparator; assert(all_computable); //fine //preamble res+=string("if ~exist('x', 'var')\n\terror('error: current state x not supplied');\nend\n"); res+=string("x=x(:);\n"); res+=string("if ~all (size(x)==["); sprintf(buf, "%d", count_symbols(STATE_KIND, BOOL_TYPE)+count_symbols(STATE_KIND, REAL_TYPE)); res+=string(buf); res+=string(" 1])\n\terror('error: state vector has wrong dimension');\nend\n"); res+=string("if ~exist('u', 'var')\n\terror('error: input u not supplied');\nend\n"); res+=string("u=u(:);\n"); res+=string("if ~all (size(u)==["); sprintf(buf, "%d", count_symbols(INPUT_KIND, BOOL_TYPE)+count_symbols(INPUT_KIND, REAL_TYPE)); res+=string(buf); res+=string(" 1])\n\terror('error: input vector has wrong dimension');\nend\n"); res+=string("\n"); sprintf(buf, "d = zeros(%d, 1);\n", count_symbols(AUX_KIND, BOOL_TYPE)); res += buf; sprintf(buf, "z = zeros(%d, 1);\n", count_symbols(AUX_KIND, REAL_TYPE)); res += buf; sprintf(buf, "xn = zeros(%d, 1);\n", count_symbols(STATE_KIND, REAL_TYPE) + count_symbols(STATE_KIND, BOOL_TYPE)); res += buf; sprintf(buf, "y = zeros(%d, 1);\n", count_symbols(OUTPUT_KIND, REAL_TYPE) + count_symbols(OUTPUT_KIND, BOOL_TYPE)); res += buf; res += string("\n"); // check range of input for (iter = symbols.begin(); iter != symbols.end(); iter++) { if ((* iter).second->is_var_symbol()) { vsym = (const Var_symbol *) (* iter).second; if (vsym->get_kind() == STATE_KIND || vsym->get_kind() == INPUT_KIND) { const Min_max_eps *mme = vsym->get_minmaxeps(); if (mme) { res+=string("if (") + vsym->to_matlab() + string(" < ") + mme->get_min()->to_matlab() + string(") | (") + vsym->to_matlab() + string(" > ")+ mme->get_max()->to_matlab() + string(")\n") +string("\terror('variable ") + vsym->get_name() + string(" is out of bounds');\n") +string("end\n"); } } } } res+=string("\n"); // auxiliary symbols for (iter = symbols.begin(); iter != symbols.end(); iter++) { if ((* iter).second->is_var_symbol()) { vsym = (const Var_symbol *) (* iter).second; if (vsym->get_kind() == AUX_KIND) aux_symbs.push_back(vsym); } } aux_symbs.sort(comparator); for (list < const Var_symbol * >::const_iterator aux_iter = aux_symbs.begin(); aux_iter != aux_symbs.end(); aux_iter++) { res += (* aux_iter)->get_definition()->matlab_simu(); } // state update and output for (iter = symbols.begin(); iter != symbols.end(); iter++) { if ((* iter).second->is_var_symbol()) { vsym = (const Var_symbol *) (* iter).second; if (vsym->get_kind() == STATE_KIND || vsym->get_kind()==OUTPUT_KIND) res += vsym->get_definition()->matlab_simu(); } } // make sure output is column-vector res+=string("xn=xn(:);\n"); res+=string("y=y(:);\n"); res+=string("z=z(:);\n"); res+=string("d=d(:);\n"); res+=string("\n\n") + matlab_within(); return res;}stringSymbol_table::matlab_within() const{ string res; res = string("function within(x, lo, hi, line)if x<lo | x>hi error(['bounds violated at line ', num2str(line), ' in the hysdel source']);end"); return res;}list<const Var_symbol*> Symbol_table::get_variables() const{ list<const Var_symbol*> res; const_symbols_iter iter; for (iter = symbols.begin(); iter != symbols.end(); iter++) { if ((* iter).second->is_var_symbol()) { res.push_back( (const Var_symbol *) ((* iter).second) ); } } return res;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -