Skip to content

Commit 2bf79df

Browse files
author
x02lucpo
committed
added hierarchical code generation
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@1423 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 55875d1 commit 2bf79df

File tree

6 files changed

+1052
-293
lines changed

6 files changed

+1052
-293
lines changed

modeq/interactive.rml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ module Interactive:
8686
relation get_classnames_in_elts: (Absyn.ElementItem list) => string list
8787
relation get_component_name_and_type_and_comment: (Absyn.Element,Env.Env) => string list
8888
relation get_components: (Absyn.ComponentRef,Absyn.Program) => string
89+
relation is_primitive_class: (Absyn.Class,Absyn.Program) => bool
8990
relation get_componentitems_name: Absyn.ComponentItem list => string list
9091
end
9192

modeq/runtime/tornadoext.cpp

Lines changed: 284 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <set>
55
#include <string>
66
#include <vector>
7-
7+
#include <strstream>
88

99
using namespace std;
1010

@@ -15,98 +15,328 @@ extern "C"
1515
#include "rml.h"
1616
#include "../absyn_builder/yacclib.h"
1717

18-
map<string,int> output_var_number;
19-
map<string,int> input_var_number;
20-
map<string,int> param_var_number;
18+
string top_class;
19+
20+
struct variable {
21+
string name;
22+
string type;
23+
string direction;
24+
int index;
25+
variable(string n,string val, string t){name = n;type = val; direction = t; index = -1;};
26+
variable(){name="";type="";direction=""; index = -1;};
27+
};
2128

29+
map<string,map<string,variable> > generated_classes;
2230

23-
2431
void TORNADOEXT_5finit(void)
2532
{
2633
}
2734

35+
int get_no_of_direction_vars_with_type(string direction, string type, map<string,variable>& variables)
36+
{
37+
38+
map<string,variable>::const_iterator search2;
39+
int return_val = 0;
40+
41+
for(search2 = variables.begin();
42+
search2 != variables.end();
43+
++search2)
44+
{
45+
//output << search2->second.name << " : " << search2->second.direction << " = " << search2->second.type << endl;
46+
if((search2->second.direction == direction)
47+
&& (search2->second.type == type))
48+
{
49+
return_val++;
50+
}
51+
}
52+
return return_val;
53+
}
54+
55+
56+
void set_index_on_variable(string name, string class_name, int index)
57+
{
58+
59+
map<string, map<string,variable> >::iterator search;
60+
search = generated_classes.find(class_name);
61+
62+
if(search != generated_classes.end()){
63+
map<string,variable>::iterator search2;
64+
search2 = search->second.find(name);
65+
if(search2 != search->second.end())
66+
{
67+
search2->second.index = index;
68+
cout << "FOUND:" << name << " " << class_name << " " << index << endl;
69+
}
70+
else
71+
{
72+
cout << name << " var NAN " << class_name << " " << index << endl;
73+
}
74+
75+
}else{
76+
cout << name << " class NAN " << class_name << " " << index << endl;
77+
}
78+
79+
}
80+
81+
string generate_impl_code_from_class(const string& class_name,
82+
map<string,variable>& variables)
83+
{
84+
strstream output;
85+
string class_name_with_c = "C" + class_name;
86+
map<string,variable>::const_iterator search2;
87+
output << class_name_with_c << "::\n";
88+
output << class_name_with_c << "(const wchar_t* Name)\n";
89+
output << "{\n";
90+
output << " SetName(Name);\n";
91+
output << " SetDesc(L\" \");\n\n";
92+
int no_of_input_vars = get_no_of_direction_vars_with_type(string("input"),
93+
string("variable"),
94+
variables);
95+
if(no_of_input_vars > 0){
96+
output << " SetNoInputVars(" << no_of_input_vars << ");\n\n";
97+
}
98+
99+
int no_of_params = get_no_of_direction_vars_with_type(string(""),
100+
string("parameter"),
101+
variables);
102+
if(no_of_params > 0){
103+
output << " SetNoParam(" << no_of_params << ");\n\n";
104+
}
105+
106+
for(search2 = variables.begin();
107+
search2 != variables.end();
108+
++search2)
109+
{
110+
if(search2->second.type == string("parameter")){
111+
output << " SetParam(" << search2->second.index << ", new CParam(L\"" << search2->second.name;
112+
output << "\", L\"\", 0.000000, MSLE_MIN_INF, MSLE_PLUS_INF, L\"" << search2->second.name << "\"));\n";
113+
}
114+
}
115+
116+
117+
118+
int no_of_output_vars = get_no_of_direction_vars_with_type(string("output"),
119+
string("variable"),
120+
variables);
121+
if(no_of_output_vars > 0){
122+
output << " SetNoOutputVars(" << no_of_output_vars << ");\n\n";
123+
}
124+
125+
for(search2 = variables.begin();
126+
search2 != variables.end();
127+
++search2)
128+
{
129+
if((search2->second.direction == string("input"))
130+
&& (search2->second.type == string("variable"))){
131+
output << " SetInputVar(" << search2->second.index << ", new CInputVar(L\"" << search2->second.name;
132+
output << "\", L\"\", 0.000000, MSLE_MIN_INF, MSLE_PLUS_INF, L\"" << search2->second.name << "\"));\n";
133+
} else if((search2->second.direction == string("output"))
134+
&& (search2->second.type == string("variable"))){
135+
output << " SetOutputVar(" << search2->second.index << ", new COutputVar(L\"" << search2->second.name;
136+
output << "\", L\"\", 0.000000, MSLE_MIN_INF, MSLE_PLUS_INF, L\"" << search2->second.name << "\"));\n";
137+
}
138+
}
139+
output << "\n";
140+
output << " SetNoIndepVars(1);\n\n";
141+
output << " SetIndepVar(0, new CIndepVar(L\"t\",L\"d\", 0.000000, MSLE_PLUS_INF, L\"t\"));\n\n";
142+
143+
int no_of_state_vars = get_no_of_direction_vars_with_type(string(""),
144+
string("state"),
145+
variables);
146+
if(no_of_state_vars > 0){
147+
output << " SetNoDerStateVars(" << no_of_state_vars << ");\n\n";
148+
}
149+
150+
for(search2 = variables.begin();
151+
search2 != variables.end();
152+
++search2)
153+
{
154+
if(search2->second.type == string("state")){
155+
output << " SetDerStateVar(" << search2->second.index << ", new CDerStateVar(L\"" << search2->second.name;
156+
output << "\", L\"\", 0.000000, MSLE_MIN_INF, MSLE_PLUS_INF, L\"" << class_name << "\"));\n";
157+
}
158+
}
159+
//fixme der state var
160+
output << "}\n\n";
161+
output << class_name_with_c << "::\n";
162+
output << "ComputeOutput()\n{\n}\n\n";
163+
output << class_name_with_c << "::\n";
164+
output << "ComputeTerminal()\n{\n}\n\n";
165+
output << class_name_with_c << "::\n";
166+
output << "ComputeState()\n{\n}\n\n";
167+
output << class_name_with_c << "::\n";
168+
output << "ComputeInitial()\n{\n}\n\n";
169+
output << ends;
170+
return string(output.str());
171+
172+
}
173+
174+
string generate_header_code_from_class(const string& class_name)
175+
{
176+
strstream output;
177+
string class_name_with_c = "C" + class_name;
178+
output << "class " << class_name_with_c << " : public Tornado::CDAEModel\n";
179+
output << "{\n";
180+
output << " public:\n\n";
181+
output << " " << class_name_with_c << "(const wchar_t* Name);\n\n";
182+
output << " public:\n\n";
183+
output << " ComputeInitial();\n";
184+
output << " ComputeTerminal();\n";
185+
output << " ComputeState();\n";
186+
output << " ComputeOutput();\n";
187+
output << "};\n\n";
188+
output << ends;
189+
return string(output.str());
190+
191+
}
192+
28193

29194
RML_BEGIN_LABEL(TORNADOEXT__dump_5ftesting)
30195
{
31-
int nvars = RML_UNTAGFIXNUM(rmlA0);
32-
cout << "testing tornadoext" << endl
33-
<< "================" << nvars << endl;
196+
197+
map<string, map<string,variable> >::iterator search;
198+
map<string,variable>::const_iterator search2;
199+
for(search = generated_classes.begin();
200+
search != generated_classes.end();
201+
++search)
202+
{
203+
cout << "\n -------------- class: " << search->first << endl;
204+
for(search2 = search->second.begin();
205+
search2 != search->second.end();
206+
++search2)
207+
{
208+
cout << search2->second.name << " : " << search2->second.direction << " = " << search2->second.type << " index: " << search2->second.index << endl;
209+
}
210+
}
211+
34212

35213
RML_TAILCALLK(rmlSC);
36214
}
37215
RML_END_LABEL
38216

39-
RML_BEGIN_LABEL(TORNADOEXT__get_5foutput_5fvar_5fnumber)
217+
RML_BEGIN_LABEL(TORNADOEXT__get_5fvar_5findex)
40218
{
41-
int nvars = RML_UNTAGFIXNUM(rmlA0);
42219
char* str = RML_STRINGDATA(rmlA0);
43-
string str_key = string(str);
44-
45-
map<string, int>::const_iterator search;
46-
search = output_var_number.find(str_key);
220+
string variable_name = string(str);
221+
char* class_str = RML_STRINGDATA(rmlA1);
222+
string class_str_key = string(class_str);
223+
// char* direction_str = RML_STRINGDATA(rmlA2);
224+
// string direction = string(direction_str);
225+
// char* type_str = RML_STRINGDATA(rmlA3);
226+
// string type = string(direction_str);
47227
int ret_val = 0;
48-
if(search != output_var_number.end()){
49-
ret_val = static_cast<int>(search->second);
50-
ret_val += 1;
51-
// cout << "\nSTATE_ALG:" << str_key << " " << ret_val << nvars << endl;
52228

53-
} // else {
54-
// cout << "\nNO STATE_ALG:" << str_key << " " << ret_val << nvars << endl;
55-
// }
229+
// cout << variable_name << " in class " << class_str_key << endl;
230+
231+
map<string, map<string,variable> >::iterator search;
232+
search = generated_classes.find(class_str_key);
233+
234+
if(search != generated_classes.end()){
235+
map<string,variable>::const_iterator search2;
236+
search2 = search->second.find(variable_name);
237+
if(search2 != search->second.end())
238+
{
239+
ret_val = search2->second.index;
240+
}
241+
242+
}
56243

57-
output_var_number[str_key] = ret_val;
58244
rmlA0 = (void*) mk_icon(ret_val);
59245
RML_TAILCALLK(rmlSC);
60246
}
61247
RML_END_LABEL
62248

63-
RML_BEGIN_LABEL(TORNADOEXT__get_5finput_5fvar_5fnumber)
249+
250+
251+
252+
253+
RML_BEGIN_LABEL(TORNADOEXT__add_5fvariable_5fto_5fclass)
64254
{
65-
int nvars = RML_UNTAGFIXNUM(rmlA0);
66-
char* str = RML_STRINGDATA(rmlA0);
67-
string str_key = string(str);
255+
char* class_name = RML_STRINGDATA(rmlA0);
256+
char* variable_name = RML_STRINGDATA(rmlA1);
257+
char* type = RML_STRINGDATA(rmlA2);
258+
char* direction = RML_STRINGDATA(rmlA3);
259+
string variable_name_str = string(variable_name);
260+
variable var(variable_name_str,string(type),string(direction));
261+
262+
string str_key = string(class_name);
68263

69-
map<string, int>::const_iterator search;
70-
search = input_var_number.find(str_key);
71-
int ret_val = 0;
72-
if(search != input_var_number.end()){
73-
ret_val = static_cast<int>(search->second);
74-
ret_val += 1;
75-
// cout << "\nSTATE_ALG:" << str_key << " " << ret_val << nvars << endl;
264+
map<string, map<string,variable> >::iterator search;
265+
search = generated_classes.find(str_key);
266+
267+
if(search == generated_classes.end()){
268+
map<string,variable> var_list;
269+
// no class exist so the index is 0
270+
var.index = 0;
271+
var_list[variable_name_str] = var;
272+
generated_classes[str_key] = var_list;
273+
//cout << "\nSTATE_ALG:" << str_key << " " << ret_val << nvars << endl;
76274

77-
} // else {
275+
} else {
276+
map<string,variable>::const_iterator search2;
277+
//count the already added variables with same type and direction
278+
var.index = get_no_of_direction_vars_with_type(var.direction,var.type,search->second);
279+
search2 = search->second.find(variable_name_str);
280+
if(search2 == search->second.end())
281+
{
282+
283+
search->second[variable_name_str] = var;
284+
}
78285
// cout << "\nNO STATE_ALG:" << str_key << " " << ret_val << nvars << endl;
79-
// }
80286

81-
input_var_number[str_key] = ret_val;
82-
rmlA0 = (void*) mk_icon(ret_val);
287+
}
288+
83289
RML_TAILCALLK(rmlSC);
84290
}
85291
RML_END_LABEL
86-
87-
RML_BEGIN_LABEL(TORNADOEXT__get_5fparam_5fvar_5fnumber)
292+
293+
// RML_BEGIN_LABEL(TORNADOEXT__set_5ftop_5fmodel)
294+
// {
295+
// char* class_name = RML_STRINGDATA(rmlA0);
296+
// top_class = string(class_name);
297+
// RML_TAILCALLK(rmlSC);
298+
// }
299+
// RML_END_LABEL
300+
301+
302+
RML_BEGIN_LABEL(TORNADOEXT__get_5fhierachical_5fcode)
88303
{
89-
int nvars = RML_UNTAGFIXNUM(rmlA0);
90-
char* str = RML_STRINGDATA(rmlA0);
91-
string str_key = string(str);
304+
char* class_name = RML_STRINGDATA(rmlA0);
305+
top_class = string(class_name);
306+
ostrstream output_impl;
307+
ostrstream output_header;
92308

93-
map<string, int>::const_iterator search;
94-
search = param_var_number.find(str_key);
95-
int ret_val = 0;
96-
if(search != param_var_number.end()){
97-
ret_val = static_cast<int>(search->second);
98-
ret_val += 1;
99-
// cout << "\nSTATE_ALG:" << str_key << " " << ret_val << nvars << endl;
309+
map<string, map<string,variable> >::iterator search;
310+
map<string,variable>::const_iterator search2;
100311

101-
} // else {
102-
// cout << "\nNO STATE_ALG:" << str_key << " " << ret_val << nvars << endl;
103-
// }
312+
for(search = generated_classes.begin();
313+
search != generated_classes.end();
314+
++search)
315+
{
316+
if(search->first != class_name){
317+
output_impl << generate_impl_code_from_class(search->first, search->second);
318+
output_header << generate_header_code_from_class(search->first);
319+
// output_impl << "\n -------------- class: " << search->first << endl;
320+
// for(search2 = search->second.begin();
321+
// search2 != search->second.end();
322+
// ++search2)
323+
// {
324+
// output_impl << search2->second.name << " : " << search2->second.direction << " = " << search2->second.type << endl;
325+
// }
326+
}
327+
}
328+
329+
output_impl << ends;
330+
output_header << ends;
331+
332+
rmlA0 = (void*) mk_scon(output_header.str());
333+
rmlA1 = (void*) mk_scon(output_impl.str());
104334

105-
param_var_number[str_key] = ret_val;
106-
rmlA0 = (void*) mk_icon(ret_val);
107335
RML_TAILCALLK(rmlSC);
108336
}
109337
RML_END_LABEL
338+
339+
110340

111341

112342
} // extern "C"

0 commit comments

Comments
 (0)