]> gerrit.simantics Code Review - simantics/fmil.git/blob - org.simantics.fmil.core/native/FMILibrary/src/Import/src/FMI1/fmi1_import.c
Switch to full JavaSE-11+ compatibility
[simantics/fmil.git] / org.simantics.fmil.core / native / FMILibrary / src / Import / src / FMI1 / fmi1_import.c
1 /*
2     Copyright (C) 2012 Modelon AB
3
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the BSD style license.
6
7      This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10     FMILIB_License.txt file for more details.
11
12     You should have received a copy of the FMILIB_License.txt file
13     along with this program. If not, contact Modelon AB <http://www.modelon.com>.
14 */
15
16 #include <stdio.h>
17 #include <stdarg.h>
18
19 #include <JM/jm_named_ptr.h>
20 #include "fmi1_import_impl.h"
21 #include "fmi1_import_variable_list_impl.h"
22
23 #include <FMI1/fmi1_types.h>
24 #include <FMI1/fmi1_functions.h>
25 #include <FMI1/fmi1_enums.h>\r
26 #include <FMI1/fmi1_capi.h>
27
28 static const char* module = "FMILIB";
29
30 /*#include "fmi1_import_vendor_annotations_impl.h"
31 #include "fmi1_import_parser.h"
32 */
33 fmi1_import_t* fmi1_import_allocate(jm_callbacks* cb) {
34         fmi1_import_t* fmu = (fmi1_import_t*)cb->calloc(1, sizeof(fmi1_import_t));
35     
36         if(!fmu || (jm_vector_init(char)(&fmu->logMessageBufferCoded,JM_MAX_ERROR_MESSAGE_SIZE,cb) < JM_MAX_ERROR_MESSAGE_SIZE)) {
37                 jm_log_fatal(cb, module, "Could not allocate memory");
38         if(fmu) cb->free(fmu);
39                 return 0;
40         }
41     
42
43         fmu->dirPath = 0;
44         fmu->location = 0;
45         fmu->callbacks = cb;
46         fmu->capi = 0;
47         fmu->md = fmi1_xml_allocate_model_description(cb);
48         fmu->registerGlobally = 0;
49         jm_vector_init(char)(&fmu->logMessageBufferExpanded,0,cb);
50
51         if(!fmu->md) {
52                 cb->free(fmu);
53                 return 0;
54         }
55
56         return fmu;
57 }
58
59 const char* fmi1_import_get_last_error(fmi1_import_t* fmu) {\r
60         return jm_get_last_error(fmu->callbacks);\r
61 }\r
62
63 fmi1_import_t* fmi1_import_parse_xml( fmi_import_context_t* context, const char* dirPath) {
64         char* xmlPath; 
65         char absPath[FILENAME_MAX + 2];
66         jm_callbacks* cb;
67         fmi1_import_t* fmu;
68     int configuration = 0;
69
70         if(!context) return 0;
71
72         cb = context->callbacks; 
73         
74         xmlPath =  fmi_import_get_model_description_path(dirPath, context->callbacks);
75
76         fmu = fmi1_import_allocate(context->callbacks);
77
78         if(!fmu) {
79                 context->callbacks->free(xmlPath);
80                 return 0;
81         }
82         
83         jm_log_verbose( cb, "FMILIB", "Parsing model description XML");
84
85     /* convert the import configuration to the xml configuration */
86     if (context->configuration & FMI_IMPORT_NAME_CHECK) {
87         configuration |= FMI1_XML_NAME_CHECK;
88     }
89
90         if(fmi1_xml_parse_model_description( fmu->md, xmlPath, configuration)) {
91                 fmi1_import_free(fmu);
92                 cb->free(xmlPath);
93                 return 0;
94         }
95         cb->free(xmlPath);
96         
97         fmu->dirPath =  (char*)cb->calloc(strlen(dirPath) + 1, sizeof(char));
98 \r
99         if(jm_get_dir_abspath(cb, dirPath, absPath, FILENAME_MAX + 2)) {\r
100                 fmu->location = fmi_import_create_URL_from_abs_path(cb, absPath);\r
101         }
102         
103         if ((fmu->dirPath == NULL) || (fmu->location == 0)){
104                 jm_log_fatal( cb, "FMILIB", "Could not allocated memory");
105                 fmi1_import_free(fmu);
106                 cb->free(xmlPath);
107                 return 0;
108         }
109         strcpy(fmu->dirPath, dirPath);
110
111         jm_log_verbose( cb, "FMILIB", "Parsing finished successfully");
112
113         return fmu;
114 }
115
116 void fmi1_import_free(fmi1_import_t* fmu) {
117     jm_callbacks* cb = fmu->callbacks;
118
119         if(!fmu) return;
120         jm_log_verbose( fmu->callbacks, "FMILIB", "Releasing allocated library resources");     
121
122         fmi1_import_destroy_dllfmu(fmu);\r
123         fmi1_xml_free_model_description(fmu->md);
124         jm_vector_free_data(char)(&fmu->logMessageBufferCoded);
125         jm_vector_free_data(char)(&fmu->logMessageBufferExpanded);
126
127         cb->free(fmu->dirPath);
128         cb->free(fmu->location);
129     cb->free(fmu);
130 }
131
132 const char* fmi1_import_get_model_name(fmi1_import_t* fmu) {
133         if(!fmu->md) {
134                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
135                 return 0;
136         }
137         return fmi1_xml_get_model_name(fmu->md);
138 }
139
140 const char* fmi1_import_get_model_identifier(fmi1_import_t* fmu) {
141         if(!fmu->md) {
142                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
143                 return 0;
144         }
145         return fmi1_xml_get_model_identifier(fmu->md);
146 }
147
148 const char* fmi1_import_get_GUID(fmi1_import_t* fmu){
149         if(!fmu->md) {
150                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
151                 return 0;
152         }
153     return fmi1_xml_get_GUID(fmu->md);
154 }
155
156 const char* fmi1_import_get_description(fmi1_import_t* fmu) {
157         if(!fmu->md) {
158                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
159                 return 0;
160         }
161         return fmi1_xml_get_description(fmu->md);
162 }
163
164 const char* fmi1_import_get_author(fmi1_import_t* fmu) {
165         if(!fmu->md) {
166                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
167                 return 0;
168         }
169         return fmi1_xml_get_author(fmu->md);
170 }
171
172 const char* fmi1_import_get_model_standard_version(fmi1_import_t* fmu) {
173         if(!fmu->md) {
174                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
175                 return 0;
176         }
177         return fmi1_xml_get_model_standard_version(fmu->md);
178 }
179
180 const char* fmi1_import_get_model_version(fmi1_import_t* fmu) {
181         if(!fmu->md) {
182                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
183                 return 0;
184         }
185         return fmi1_xml_get_model_version(fmu->md);
186 }
187
188 const char* fmi1_import_get_generation_tool(fmi1_import_t* fmu) {
189         if(!fmu->md) {
190                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
191                 return 0;
192         }
193         return fmi1_xml_get_generation_tool(fmu->md);
194 }
195
196 const char* fmi1_import_get_generation_date_and_time(fmi1_import_t* fmu) {
197         if(!fmu->md) {
198                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
199                 return 0;
200         }
201         return fmi1_xml_get_generation_date_and_time(fmu->md);
202 }
203
204 fmi1_variable_naming_convension_enu_t fmi1_import_get_naming_convention(fmi1_import_t* fmu) {
205         if(!fmu->md) {
206                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
207                 return fmi1_naming_enu_unknown;
208         }
209         return fmi1_xml_get_naming_convention(fmu->md);
210 }
211
212 unsigned int fmi1_import_get_number_of_continuous_states(fmi1_import_t* fmu) {
213         if(!fmu->md) {
214                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
215                 return 0;
216         }
217         return fmi1_xml_get_number_of_continuous_states(fmu->md);
218 }
219
220 unsigned int fmi1_import_get_number_of_event_indicators(fmi1_import_t* fmu) {
221         if(!fmu->md) {
222                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
223                 return 0;
224         }
225         return fmi1_xml_get_number_of_event_indicators(fmu->md);
226 }
227
228 double fmi1_import_get_default_experiment_start(fmi1_import_t* fmu) {
229         if(!fmu->md) {
230                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
231                 return 0;
232         }
233         return fmi1_xml_get_default_experiment_start(fmu->md);
234 }
235
236 void fmi1_import_set_default_experiment_start(fmi1_import_t* fmu, double t) {
237         if(!fmu->md) {
238                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
239                 return;
240         }
241         fmi1_xml_set_default_experiment_start(fmu->md, t);
242 }
243
244 double fmi1_import_get_default_experiment_stop(fmi1_import_t* fmu) {
245         if(!fmu->md) {
246                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
247                 return 0;
248         }
249         return fmi1_xml_get_default_experiment_stop(fmu->md);
250 }
251
252 void fmi1_import_set_default_experiment_stop(fmi1_import_t* fmu, double t) {
253         if(!fmu->md) {
254                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
255                 return;
256         }
257         fmi1_xml_set_default_experiment_stop(fmu->md, t);
258 }
259
260 double fmi1_import_get_default_experiment_tolerance(fmi1_import_t* fmu) {
261         if(!fmu->md) {
262                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
263                 return 0;
264         }
265         return fmi1_xml_get_default_experiment_tolerance(fmu->md);
266 }
267
268 void fmi1_import_set_default_experiment_tolerance(fmi1_import_t* fmu, double tol) {
269         if(!fmu->md) {
270                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
271                 return;
272         }
273         fmi1_xml_set_default_experiment_tolerance(fmu->md, tol);
274 }
275
276 fmi1_import_vendor_list_t* fmi1_import_get_vendor_list(fmi1_import_t* fmu) {
277         if(!fmu->md) {
278                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
279                 return 0;
280         }
281         return fmi1_xml_get_vendor_list(fmu->md);
282 }
283
284 unsigned int  fmi1_import_get_number_of_vendors(fmi1_import_vendor_list_t* vl) {
285         return fmi1_xml_get_number_of_vendors(vl);
286 }
287
288 fmi1_import_vendor_t* fmi1_import_get_vendor(fmi1_import_vendor_list_t* v, unsigned int  index) {
289         return fmi1_xml_get_vendor(v, index);
290 }
291
292 fmi1_import_unit_definitions_t* fmi1_import_get_unit_definitions(fmi1_import_t* fmu) {
293         if(!fmu->md) {
294                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
295                 return 0;
296         }
297         return fmi1_xml_get_unit_definitions(fmu->md);
298 }
299
300 unsigned int  fmi1_import_get_unit_definitions_number(fmi1_import_unit_definitions_t* ud) {
301         return fmi1_xml_get_unit_definitions_number(ud);
302 }
303
304 fmi1_import_type_definitions_t* fmi1_import_get_type_definitions(fmi1_import_t* fmu) {
305         if(!fmu->md) {
306                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
307                 return 0;
308         }
309         return fmi1_xml_get_type_definitions(fmu->md);
310 }
311
312 /* Get the list of all the variables in the model */
313 fmi1_import_variable_list_t* fmi1_import_get_variable_list(fmi1_import_t* fmu) {
314         jm_vector(jm_voidp)* vars;
315     fmi1_import_variable_list_t* vl;
316     size_t nv, i;
317         if(!fmu->md) {
318                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
319                 return 0;
320         }
321         vars = fmi1_xml_get_variables_original_order(fmu->md);
322         if(vars)
323                 nv = jm_vector_get_size(jm_voidp)(vars);
324         else
325                 nv = 0;
326     vl = fmi1_import_alloc_variable_list(fmu, nv);
327     if(!vl) return 0;
328     for(i = 0; i< nv; i++) {
329         jm_vector_set_item(jm_voidp)(&vl->variables, i, jm_vector_get_item(jm_voidp)(vars, i));
330     }
331     return vl;
332 }
333
334 /* Get the list of all the variables in the model in alphabetical order */
335 fmi1_import_variable_list_t* fmi1_import_get_variable_list_alphabetical_order(fmi1_import_t* fmu) {
336         jm_vector(jm_named_ptr)* vars;
337     fmi1_import_variable_list_t* vl;
338     size_t nv, i;
339         if(!fmu->md) {
340                 jm_log_error(fmu->callbacks, module,"No FMU is loaded");
341                 return 0;
342         }
343         vars = fmi1_xml_get_variables_alphabetical_order(fmu->md);
344     nv = jm_vector_get_size(jm_named_ptr)(vars);
345     vl = fmi1_import_alloc_variable_list(fmu, nv);
346     if(!vl) return 0;
347     for(i = 0; i< nv; i++) {
348                 jm_vector_set_item(jm_voidp)(&vl->variables, i, jm_vector_get_item(jm_named_ptr)(vars, i).ptr);
349     }
350     return vl;
351 }