Bug #13396 fixed - MBLOCK didn't work with an external file containing the modelica...
[scilab.git] / scilab / modules / scicos / macros / scicos_scicos / do_eval.sci
1 //  Scicos
2 //
3 //  Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
4 //  Copyright (C) DIGITEO - 2010 - Jérôme PICARD
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 //
20 // See the file ../license.txt
21 //
22
23 function [scs_m,cpr,needcompile,ok]=do_eval(scs_m,cpr,%scicos_context)
24     // This function (re)-evaluates blocks in the scicos data structure scs_m
25
26     ok=%t
27     needcompile1=max(2,needcompile)
28     %mprt=funcprot()
29     funcprot(0)
30     scicos_getvalue=setvalue;
31     getvalue=setvalue;
32
33     function scicosmessage(txt)
34         messagebox(["In block " + o.gui + ": ";
35         txt;
36         "current parameter value kept"],"Warning","modal");
37         %scicos_prob = resume(%t)
38     endfunction
39
40     // overload message
41     // This function is used to alert the user on setvalue
42     // (invalid exprs we keep the previous parameters)
43     function message(str)
44         if length(arg1.doc) >= 1 then
45             uid = arg1.doc(1);
46             uid = [full_uids uid];
47
48             html = "<html><body>";
49             html = html + "<em>" + gettext("Evaluation problem: value not updated from context.") + "</em><br/>";
50             html = html + strcat(str, "<br/>") + "<br/>";
51             html = html + "</body></html>";
52             warnBlockByUID(uid, html);
53         else
54             txt = gettext("Evaluation problem: value not updated from context.");
55             txt = [txt ; str];
56             disp(txt);
57         end
58
59         %scicos_prob = resume(%t)
60     endfunction
61
62     // path on do_eval hierarchy loop
63     if ~isdef("full_uids") then
64         full_uids = [];
65     end
66
67     global %scicos_prob
68     %scicos_prob=%f
69
70     //## overload some functions used in GUI
71     deff("[ok,tt]        =  FORTR(funam,tt,i,o)","ok=%t")
72     deff("[ok,tt,cancel] =  CFORTR2(funam,tt,i,o)","ok=%t,cancel=%f")
73     deff("[ok,tt,cancel] =  CC4(funam,tt,i,o)","ok=%t,cancel=%f")
74     deff("[ok,tt]        =  CFORTR(funam,tt,i,o)","ok=%t")
75     deff("[x,y,ok,gc]    =  edit_curv(x,y,job,tit,gc)","ok=%t")
76     deff("[ok,tt,dep_ut] = genfunc1(tt,ni,no,nci,nco,nx,nz,nrp,type_)",..
77     "dep_ut=model.dep_ut;ok=%t")
78     deff("result         = dialog(labels,valueini)","result=valueini")
79     deff("[result,Quit]  = scstxtedit(valueini,v2)","result=valueini,Quit=0")
80     deff("[ok,tt]        = MODCOM(funam,tt,vinp,vout,vparam,vparamv,vpprop)",..
81     "[dirF, nameF, extF] = fileparts(funam);..
82     [modelica_path, modelica_directory] = getModelicaPath();..
83     funam1 = []; ..
84     if (extF == """")  then, ..
85         funam1 = modelica_directory + nameF + "".mo""; ..
86     elseif fileinfo(funam) == [] then, ..
87         funam1 = funam; ..
88     end; ..
89     if funam1 <> [] then, ..
90         mputl(tt, funam1); ..
91     end");
92     %nx=lstsize(scs_m.objs)
93     funcprot(%mprt)
94     for %kk=1:%nx
95         o=scs_m.objs(%kk)
96         if typeof(o)=="Block" then
97             model=o.model
98             if model.sim=="super"| (model.sim=="csuper"& ~isequal(model.ipar,1)) then  //exclude mask
99                 sblock=model.rpar
100                 context=sblock.props.context
101                 [scicos_context1,ierr]=script2var(context,%scicos_context)
102                 if ierr <>0 then
103                     %now_win=xget("window")
104                     scicosmessage(["Cannot evaluate a context";lasterror()])
105                     xset("window",%now_win)
106                 else
107                     previous_full_uids = full_uids;
108                     if length(o.doc) >= 1 then
109                         full_uids = [full_uids o.doc(1)];
110                     end
111                     [sblock,%w,needcompile2,ok]=do_eval(sblock,list(),scicos_context1)
112                     needcompile1=max(needcompile1,needcompile2)
113                     full_uids = previous_full_uids;
114                     if ok then
115                         scs_m.objs(%kk).model.rpar=sblock
116                     else
117                         return
118                     end
119                 end
120             elseif o.model.sim(1)=="asuper" then
121             else
122                 model=o.model
123                 if ~isdef(o.gui) | ~or(type(evstr(o.gui) == [13 11])) then
124                     if length(o.doc) >= 1 then
125                         uid = [full_uids o.doc(1)];
126
127                         html = "<html><body>";
128                         html = html + "<em>" + gettext("Evaluation problem: Unknown block") + "</em><br/>";
129                         html = html + "</body></html>";
130                         warnBlockByUID(uid, html);
131                     else
132                         error("do_eval: " + gettext("Evaluation problem: Unknown block") + " " + o.gui);
133                     end
134                 end
135
136                 %scicos_prob=%f
137                 ier=execstr("o="+o.gui+"(''set'',o)","errcatch", "m")
138                 if ier==0& %scicos_prob==%f then
139                     needcompile1=max(needcompile1,needcompile) // for scifunc_block
140                     model_n=o.model
141                     if or(model.blocktype<>model_n.blocktype)|.. // type 'c','d','z','l'
142                         or(model.dep_ut<>model_n.dep_ut)|..
143                         (model.nzcross<>model_n.nzcross)|..
144                         (model.nmode<>model_n.nmode) then
145                         needcompile1=4
146                     end
147                     if (size(model.in,"*")<>size(model_n.in,"*"))|..
148                         (size(model.out,"*")<>size(model_n.out,"*"))|..
149                         (size(model.evtin,"*")<>size(model_n.evtin,"*")) then
150                         // number of input (evt or regular ) or output  changed
151                         needcompile1=4
152                     end
153                     if model.sim=="input"|model.sim=="output" then
154                         if model.ipar<>model_n.ipar then
155                             needcompile1=4
156                         end
157                     end
158
159                     itisanMBLOCK=%f
160                     if prod(size(model.sim))>1 then
161                         if (model.sim(2)==30004) then
162                             itisanMBLOCK=%t
163                         end
164                     end
165
166                     if (prod(size(model.sim))==1 & ~model.equations==list()) | itisanMBLOCK then
167                         if ~isequal(model.equations.parameters,model_n.equations.parameters) then
168                             param_name   = model.equations.parameters(1);
169                             param_name_n = model_n.equations.parameters(1);
170                             if ~isequal(param_name,param_name_n) then
171                                 needcompile1=4
172                             else
173                                 for i=1:length(model.equations.parameters(2))
174                                     if or((model.equations.parameters(2)(i))<>(model_n.equations.parameters(2)(i))) then
175                                         needcompile=0
176
177                                         // the initialization XML files is re-created by translator if needed
178                                         // XML=TMPDIR+'/'+stripblanks(scs_m.props.title(1))+'_imf_init.xml';
179                                         // if ~deletefile(XML) then
180                                         //   messagebox(msprintf(_('Unable to delete the file: %s'),XML),'error','modal');
181                                         // end
182
183                                         // the XML simulation is no more created with Xcos
184                                         // XMLTMP=TMPDIR+'/'+stripblanks(scs_m.props.title(1))+'_imSim.xml'
185                                         // if ~deletefile(XMLTMP) then
186                                         //   messagebox(msprintf(_('Unable to delete the file: %s'),XMLTMP),'error','modal');
187                                         // end
188
189                                         break;
190                                     end
191                                 end
192                             end
193                         end
194                     end
195
196                     scs_m.objs(%kk)=o
197                 else
198                     error(msprintf(gettext("%s: Error while calling block %s [uid=''%s'']: invalid parameter (ier=%f, %%scicos_prob=%%%s).\n"), "do_eval", o.gui, o.model.uid, ier, string(%scicos_prob)));
199                     ok=%f
200                     return
201                 end
202             end
203         end
204     end
205     needcompile=needcompile1
206     if needcompile==4 then cpr=list(),end
207 endfunction