f96e3ca9c8588bfb473f8dd00c9a3f5c9eaf0a37
[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     if (extF == """")  then, ..
84         funam1 = modelica_directory + nameF + "".mo""; ..
85     elseif fileinfo(funam) == [] then, ..
86         funam1 = funam; ..
87     end; ..
88     mputl(tt, funam1);")
89     %nx=lstsize(scs_m.objs)
90     funcprot(%mprt)
91     for %kk=1:%nx
92         o=scs_m.objs(%kk)
93         if typeof(o)=="Block" then
94             model=o.model
95             if model.sim=="super"| (model.sim=="csuper"& ~isequal(model.ipar,1)) then  //exclude mask
96                 sblock=model.rpar
97                 context=sblock.props.context
98                 [scicos_context1,ierr]=script2var(context,%scicos_context)
99                 if ierr <>0 then
100                     %now_win=xget("window")
101                     scicosmessage(["Cannot evaluate a context";lasterror()])
102                     xset("window",%now_win)
103                 else
104                     previous_full_uids = full_uids;
105                     if length(o.doc) >= 1 then
106                         full_uids = [full_uids o.doc(1)];
107                     end
108                     [sblock,%w,needcompile2,ok]=do_eval(sblock,list(),scicos_context1)
109                     needcompile1=max(needcompile1,needcompile2)
110                     full_uids = previous_full_uids;
111                     if ok then
112                         scs_m.objs(%kk).model.rpar=sblock
113                     else
114                         return
115                     end
116                 end
117             elseif o.model.sim(1)=="asuper" then
118             else
119                 model=o.model
120                 if ~isdef(o.gui) | ~or(type(evstr(o.gui) == [13 11])) then
121                     if length(o.doc) >= 1 then
122                         uid = [full_uids o.doc(1)];
123
124                         html = "<html><body>";
125                         html = html + "<em>" + gettext("Evaluation problem: Unknown block") + "</em><br/>";
126                         html = html + "</body></html>";
127                         warnBlockByUID(uid, html);
128                     else
129                         error("do_eval: " + gettext("Evaluation problem: Unknown block") + " " + o.gui);
130                     end
131                 end
132
133                 %scicos_prob=%f
134                 ier=execstr("o="+o.gui+"(''set'',o)","errcatch", "m")
135                 if ier==0& %scicos_prob==%f then
136                     needcompile1=max(needcompile1,needcompile) // for scifunc_block
137                     model_n=o.model
138                     if or(model.blocktype<>model_n.blocktype)|.. // type 'c','d','z','l'
139                         or(model.dep_ut<>model_n.dep_ut)|..
140                         (model.nzcross<>model_n.nzcross)|..
141                         (model.nmode<>model_n.nmode) then
142                         needcompile1=4
143                     end
144                     if (size(model.in,"*")<>size(model_n.in,"*"))|..
145                         (size(model.out,"*")<>size(model_n.out,"*"))|..
146                         (size(model.evtin,"*")<>size(model_n.evtin,"*")) then
147                         // number of input (evt or regular ) or output  changed
148                         needcompile1=4
149                     end
150                     if model.sim=="input"|model.sim=="output" then
151                         if model.ipar<>model_n.ipar then
152                             needcompile1=4
153                         end
154                     end
155
156                     itisanMBLOCK=%f
157                     if prod(size(model.sim))>1 then
158                         if (model.sim(2)==30004) then
159                             itisanMBLOCK=%t
160                         end
161                     end
162
163                     if (prod(size(model.sim))==1 & ~model.equations==list()) | itisanMBLOCK then
164                         if ~isequal(model.equations.parameters,model_n.equations.parameters) then
165                             param_name   = model.equations.parameters(1);
166                             param_name_n = model_n.equations.parameters(1);
167                             if ~isequal(param_name,param_name_n) then
168                                 needcompile1=4
169                             else
170                                 for i=1:length(model.equations.parameters(2))
171                                     if or((model.equations.parameters(2)(i))<>(model_n.equations.parameters(2)(i))) then
172                                         needcompile=0
173
174                                         // the initialization XML files is re-created by translator if needed
175                                         // XML=TMPDIR+'/'+stripblanks(scs_m.props.title(1))+'_imf_init.xml';
176                                         // if ~deletefile(XML) then
177                                         //   messagebox(msprintf(_('Unable to delete the file: %s'),XML),'error','modal');
178                                         // end
179
180                                         // the XML simulation is no more created with Xcos
181                                         // XMLTMP=TMPDIR+'/'+stripblanks(scs_m.props.title(1))+'_imSim.xml'
182                                         // if ~deletefile(XMLTMP) then
183                                         //   messagebox(msprintf(_('Unable to delete the file: %s'),XMLTMP),'error','modal');
184                                         // end
185
186                                         break;
187                                     end
188                                 end
189                             end
190                         end
191                     end
192
193                     scs_m.objs(%kk)=o
194                 else
195                     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)));
196                     ok=%f
197                     return
198                 end
199             end
200         end
201     end
202     needcompile=needcompile1
203     if needcompile==4 then cpr=list(),end
204 endfunction