Xcos MVC: implement firing / dep_ut / label / nzcross / nmode
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / ModelAdapter.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2014-2014 - Scilab Enterprises - Clement DAVID
4  *
5  *  This file must be used under the terms of the CeCILL.
6  *  This source file is licensed as described in the file COPYING, which
7  *  you should have received as part of this distribution.  The terms
8  *  are also available at
9  *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 #include <string>
14 #include <vector>
15 #include <algorithm>
16
17 #include "list.hxx"
18 #include "double.hxx"
19 #include "string.hxx"
20
21 #include "Controller.hxx"
22 #include "ModelAdapter.hxx"
23 #include "ports_management.hxx"
24
25 namespace org_scilab_modules_scicos
26 {
27 namespace view_scilab
28 {
29 namespace
30 {
31
32 struct sim
33 {
34
35     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
36     {
37         model::Block* adaptee = adaptor.getAdaptee();
38
39         // First, extact the function Name
40         std::string name;
41         controller.getObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_NAME, name);
42         types::String* Name = new types::String(1, 1);
43         Name->set(0, name.data());
44
45         // Then the Api. If it is zero, then just return the Name. Otherwise, return a list containing both.
46         int api;
47         controller.getObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_API, api);
48
49         if (api == 0)
50         {
51             return Name;
52         }
53         else
54         {
55             types::Double* Api = new types::Double(1, 1, static_cast<double>(api));
56             types::List* o = new types::List();
57             o->set(0, Name);
58             o->set(1, Api);
59             return o;
60         }
61     }
62
63     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
64     {
65         model::Block* adaptee = adaptor.getAdaptee();
66
67         if (v->getType() == types::InternalType::ScilabString)
68         {
69             types::String* current = v->getAs<types::String>();
70             if (current->getSize() != 1)
71             {
72                 return false;
73             }
74
75             char* c_str = wide_string_to_UTF8(current->get(0));
76             std::string name = std::string(c_str);
77             FREE(c_str);
78
79             // If the input is a scalar string, then the functionApi is 0.
80             int api = 0;
81
82             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_NAME, name);
83             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_API, api);
84         }
85         else if (v->getType() == types::InternalType::ScilabList)
86         {
87             // If the input is a 2-sized list, then it must be string and positive integer.
88             types::List* current = v->getAs<types::List>();
89             if (current->getSize() != 2)
90             {
91                 return false;
92             }
93             if (current->get(0)->getType() != types::InternalType::ScilabString || current->get(1)->getType() != types::InternalType::ScilabDouble)
94             {
95                 return false;
96             }
97
98             types::String* Name = current->get(0)->getAs<types::String>();
99             if (Name->getSize() != 1)
100             {
101                 return false;
102             }
103             char* c_str = wide_string_to_UTF8(Name->get(0));
104             std::string name = std::string(c_str);
105             FREE(c_str);
106
107             types::Double* Api = current->get(1)->getAs<types::Double>();
108             if (Api->getSize() != 1)
109             {
110                 return false;
111             }
112             double api = Api->get(0);
113             if (floor(api) != api)
114             {
115                 return false;
116             }
117             int api_int = static_cast<int>(api);
118
119             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_NAME, name);
120             controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_FUNCTION_API, api_int);
121         }
122         else
123         {
124             return false;
125         }
126         return true;
127     }
128 };
129
130 struct in
131 {
132
133     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
134     {
135         return get_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, INPUTS, controller);
136     }
137
138     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
139     {
140         return update_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, INPUTS, controller, v);
141     }
142 };
143
144 struct in2
145 {
146
147     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
148     {
149         return get_ports_property<ModelAdapter, DATATYPE_COLS>(adaptor, INPUTS, controller);
150     }
151
152     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
153     {
154         return set_ports_property<ModelAdapter, DATATYPE_COLS>(adaptor, INPUTS, controller, v);
155     }
156 };
157
158 struct intyp
159 {
160
161     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
162     {
163         return get_ports_property<ModelAdapter, DATATYPE_TYPE>(adaptor, INPUTS, controller);
164     }
165
166     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
167     {
168         return set_ports_property<ModelAdapter, DATATYPE_TYPE>(adaptor, INPUTS, controller, v);
169     }
170 };
171
172 struct out
173 {
174
175     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
176     {
177         return get_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, OUTPUTS, controller);
178     }
179
180     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
181     {
182         return update_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, OUTPUTS, controller, v);
183     }
184 };
185
186 struct out2
187 {
188
189     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
190     {
191         return get_ports_property<ModelAdapter, DATATYPE_COLS>(adaptor, OUTPUTS, controller);
192     }
193
194     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
195     {
196         return set_ports_property<ModelAdapter, DATATYPE_COLS>(adaptor, OUTPUTS, controller, v);
197     }
198 };
199
200 struct outtyp
201 {
202
203     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
204     {
205         return get_ports_property<ModelAdapter, DATATYPE_TYPE>(adaptor, OUTPUTS, controller);
206     }
207
208     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
209     {
210         return set_ports_property<ModelAdapter, DATATYPE_TYPE>(adaptor, OUTPUTS, controller, v);
211     }
212 };
213
214 struct evtin
215 {
216
217     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
218     {
219         return get_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, EVENT_INPUTS, controller);
220     }
221
222     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
223     {
224         return update_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, EVENT_INPUTS, controller, v);
225     }
226 };
227
228 struct evtout
229 {
230
231     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
232     {
233         return get_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, EVENT_OUTPUTS, controller);
234     }
235
236     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
237     {
238         return update_ports_property<ModelAdapter, DATATYPE_ROWS>(adaptor, EVENT_OUTPUTS, controller, v);
239     }
240 };
241
242 struct state
243 {
244
245     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
246     {
247         model::Block* adaptee = adaptor.getAdaptee();
248
249         std::vector<double> state;
250         controller.getObjectProperty(adaptee->id(), adaptee->kind(), STATE, state);
251
252         double* data;
253         types::Double* o = new types::Double(state.size(), 1, &data);
254
255         std::copy(state.begin(), state.end(), data);
256
257         return o;
258     }
259
260     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
261     {
262
263         if (v->getType() != types::InternalType::ScilabDouble)
264         {
265             return false;
266         }
267
268         types::Double* current = v->getAs<types::Double>();
269         if (current->getCols() != 0 && current->getCols() != 1)
270         {
271             return false;
272         }
273
274         model::Block* adaptee = adaptor.getAdaptee();
275
276         std::vector<double> state;
277         std::copy(current->getReal(), current->getReal() + current->getSize(), state.begin());
278
279         controller.setObjectProperty(adaptee->id(), adaptee->kind(), STATE, state);
280         return true;
281     }
282 };
283
284 struct dstate
285 {
286
287     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
288     {
289         model::Block* adaptee = adaptor.getAdaptee();
290
291         std::vector<double> dstate;
292         controller.getObjectProperty(adaptee->id(), adaptee->kind(), DSTATE, dstate);
293
294         double* data;
295         types::Double* o = new types::Double(dstate.size(), 1, &data);
296
297         std::copy(dstate.begin(), dstate.end(), data);
298
299         return o;
300     }
301
302     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
303     {
304
305         if (v->getType() != types::InternalType::ScilabDouble)
306         {
307             return false;
308         }
309
310         types::Double* current = v->getAs<types::Double>();
311         if (current->getCols() != 0 && current->getCols() != 1)
312         {
313             return false;
314         }
315
316         model::Block* adaptee = adaptor.getAdaptee();
317
318         std::vector<double> dstate;
319         std::copy(current->getReal(), current->getReal() + current->getSize(), dstate.begin());
320
321         controller.setObjectProperty(adaptee->id(), adaptee->kind(), DSTATE, dstate);
322         return true;
323     }
324 };
325
326 struct odstate
327 {
328
329     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
330     {
331         // FIXME: get odstate
332         return 0;
333     }
334
335     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
336     {
337         if (v->getType() != types::InternalType::ScilabList)
338         {
339             return false;
340         }
341
342         types::List* current = v->getAs<types::List>();
343
344         if (current->getSize() == 0)
345         {
346             return true;
347         }
348         else
349         {
350             // FIXME: get the input list and store it in the odstate field
351             return false;
352         }
353     }
354 };
355
356 struct blocktype
357 {
358
359     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
360     {
361         model::Block* adaptee = adaptor.getAdaptee();
362
363         int type;
364         controller.getObjectProperty(adaptee->id(), adaptee->kind(), SIM_BLOCKTYPE, type);
365
366         wchar_t Type = type;
367         types::String* o = new types::String(&Type);
368
369         return o;
370     }
371
372     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
373     {
374         model::Block* adaptee = adaptor.getAdaptee();
375
376         if (v->getType() != types::InternalType::ScilabString)
377         {
378             return false;
379         }
380
381         types::String* current = v->getAs<types::String>();
382         if (current->getSize() != 1)
383         {
384             return false;
385         }
386         // The input must be a character
387         if (current->get(0)[0] == '\0')
388         {
389             return false;
390         }
391         if (current->get(0)[1] != '\0')
392         {
393             return false;
394         }
395
396         int type = current->get(0)[0];
397
398         controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_BLOCKTYPE, type);
399         return true;
400     }
401 };
402
403 struct firing
404 {
405
406     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
407     {
408         return get_ports_property<ModelAdapter, FIRING>(adaptor, OUTPUTS, controller);
409     }
410
411     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
412     {
413         return set_ports_property<ModelAdapter, FIRING>(adaptor, OUTPUTS, controller, v);
414     }
415 };
416
417 struct dep_ut
418 {
419
420     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
421     {
422         model::Block* adaptee = adaptor.getAdaptee();
423
424         std::vector<int> dep_ut;
425         controller.getObjectProperty(adaptee->id(), adaptee->kind(), SIM_DEP_UT, dep_ut);
426
427         int* dep;
428         types::Bool* o = new types::Bool(1, 2, &dep);
429
430         dep[0] = dep_ut[0];
431         dep[1] = dep_ut[1];
432
433         return o;
434     }
435
436     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
437     {
438         model::Block* adaptee = adaptor.getAdaptee();
439
440         if (v->getType() != types::InternalType::ScilabBool)
441         {
442             return false;
443         }
444
445         types::Bool* current = v->getAs<types::Bool>();
446         if (current->getRows() != 1 || current->getCols() != 2)
447         {
448             return false;
449         }
450
451         std::vector<int> dep_ut (2);
452         dep_ut[0] = current->get(0);
453         dep_ut[1] = current->get(1);
454
455         controller.setObjectProperty(adaptee->id(), adaptee->kind(), SIM_DEP_UT, dep_ut);
456         return true;
457     }
458 };
459
460 struct label
461 {
462
463     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
464     {
465         model::Block* adaptee = adaptor.getAdaptee();
466
467         std::string label;
468         controller.getObjectProperty(adaptee->id(), adaptee->kind(), LABEL, label);
469
470         types::String* o = new types::String(1, 1);
471         o->set(0, label.data());
472
473         return o;
474     }
475
476     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
477     {
478         if (v->getType() != types::InternalType::ScilabString)
479         {
480             return false;
481         }
482
483         types::String* current = v->getAs<types::String>();
484         if (current->getSize() != 1)
485         {
486             return false;
487         }
488
489         model::Block* adaptee = adaptor.getAdaptee();
490
491         std::string label;
492         char* c_str = wide_string_to_UTF8(current->get(0));
493         label = std::string(c_str);
494         FREE(c_str);
495
496         controller.setObjectProperty(adaptee->id(), adaptee->kind(), LABEL, label);
497         return true;
498     }
499 };
500
501 struct nzcross
502 {
503
504     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
505     {
506         model::Block* adaptee = adaptor.getAdaptee();
507
508         int nzcross;
509         controller.getObjectProperty(adaptee->id(), adaptee->kind(), NZCROSS, nzcross);
510
511         types::Double* o = new types::Double(static_cast<double>(nzcross));
512
513         return o;
514     }
515
516     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
517     {
518
519         if (v->getType() != types::InternalType::ScilabDouble)
520         {
521             return false;
522         }
523
524         types::Double* current = v->getAs<types::Double>();
525         if (current->getSize() != 1)
526         {
527             return false;
528         }
529         if (floor(current->get(0)) != current->get(0))
530         {
531             return false;
532         }
533
534         model::Block* adaptee = adaptor.getAdaptee();
535
536         int nzcross = static_cast<int>(current->get(0));
537
538         controller.setObjectProperty(adaptee->id(), adaptee->kind(), NZCROSS, nzcross);
539         return true;
540     }
541 };
542
543 struct nmode
544 {
545
546     static types::InternalType* get(const ModelAdapter& adaptor, const Controller& controller)
547     {
548         model::Block* adaptee = adaptor.getAdaptee();
549
550         int nmode;
551         controller.getObjectProperty(adaptee->id(), adaptee->kind(), NMODE, nmode);
552
553         types::Double* o = new types::Double(static_cast<double>(nmode));
554
555         return o;
556     }
557
558     static bool set(ModelAdapter& adaptor, types::InternalType* v, Controller& controller)
559     {
560
561         if (v->getType() != types::InternalType::ScilabDouble)
562         {
563             return false;
564         }
565
566         types::Double* current = v->getAs<types::Double>();
567         if (current->getSize() != 1)
568         {
569             return false;
570         }
571         if (floor(current->get(0)) != current->get(0))
572         {
573             return false;
574         }
575
576         model::Block* adaptee = adaptor.getAdaptee();
577
578         int nmode = static_cast<int>(current->get(0));
579
580         controller.setObjectProperty(adaptee->id(), adaptee->kind(), NMODE, nmode);
581         return true;
582     }
583 };
584
585 } /* namespace */
586
587 template<> property<ModelAdapter>::props_t property<ModelAdapter>::fields = property<ModelAdapter>::props_t();
588
589 ModelAdapter::ModelAdapter(const ModelAdapter& o) :
590     BaseAdapter<ModelAdapter, org_scilab_modules_scicos::model::Block>(o) { }
591
592 ModelAdapter::ModelAdapter(org_scilab_modules_scicos::model::Block* o) :
593     BaseAdapter<ModelAdapter, org_scilab_modules_scicos::model::Block>(o)
594 {
595     if (property<ModelAdapter>::properties_has_not_been_set())
596     {
597         property<ModelAdapter>::fields.reserve(18);
598         property<ModelAdapter>::add_property(L"sim", &sim::get, &sim::set);
599         property<ModelAdapter>::add_property(L"in", &in::get, &in::set);
600         property<ModelAdapter>::add_property(L"in2", &in2::get, &in2::set);
601         property<ModelAdapter>::add_property(L"intyp", &intyp::get, &intyp::set);
602         property<ModelAdapter>::add_property(L"out", &out::get, &out::set);
603         property<ModelAdapter>::add_property(L"out2", &out2::get, &out2::set);
604         property<ModelAdapter>::add_property(L"outtyp", &outtyp::get, &outtyp::set);
605         property<ModelAdapter>::add_property(L"evtin", &evtin::get, &evtin::set);
606         property<ModelAdapter>::add_property(L"evtout", &evtout::get, &evtout::set);
607         property<ModelAdapter>::add_property(L"state", &state::get, &state::set);
608         property<ModelAdapter>::add_property(L"dstate", &dstate::get, &dstate::set);
609         property<ModelAdapter>::add_property(L"odstate", &odstate::get, &odstate::set);
610         property<ModelAdapter>::add_property(L"blocktype", &blocktype::get, &blocktype::set);
611         property<ModelAdapter>::add_property(L"firing", &firing::get, &firing::set);
612         property<ModelAdapter>::add_property(L"dep_ut", &dep_ut::get, &dep_ut::set);
613         property<ModelAdapter>::add_property(L"label", &label::get, &label::set);
614         property<ModelAdapter>::add_property(L"nzcross", &nzcross::get, &nzcross::set);
615         property<ModelAdapter>::add_property(L"nmode", &nmode::get, &nmode::set);
616     }
617 }
618
619 ModelAdapter::~ModelAdapter()
620 {
621 }
622
623 std::wstring ModelAdapter::getTypeStr()
624 {
625     return getSharedTypeStr();
626 }
627
628 std::wstring ModelAdapter::getShortTypeStr()
629 {
630     return getSharedTypeStr();
631 }
632
633 } /* view_scilab */
634 } /* namespace org_scilab_modules_scicos */