]> gerrit.simantics Code Review - simantics/fmil.git/blob - org.simantics.fmil.core/native/FMILibrary/src/XML/src/FMI1/fmi1_xml_cosim.c
Switch to full JavaSE-11+ compatibility
[simantics/fmil.git] / org.simantics.fmil.core / native / FMILibrary / src / XML / src / FMI1 / fmi1_xml_cosim.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 #include <JM/jm_vector.h>
16 #include "fmi1_xml_model_description_impl.h"
17 #include "fmi1_xml_parser.h"
18
19 static const char * module = "FMI1XML";
20
21 fmi1_fmu_kind_enu_t fmi1_xml_get_fmu_kind(fmi1_xml_model_description_t* md) {
22     return md->fmuKind;
23 }
24
25 fmi1_xml_capabilities_t* fmi1_xml_get_capabilities(fmi1_xml_model_description_t* md){
26     return md->capabilities;
27 }
28
29 size_t fmi1_xml_get_number_of_additional_models(fmi1_xml_model_description_t* md) {
30     return jm_vector_get_size(jm_string)(&md->additionalModels);
31 }
32
33 const char* fmi1_xml_get_additional_model_name(fmi1_xml_model_description_t* md, size_t index) {
34     if(fmi1_xml_get_number_of_additional_models(md) <= index) return 0;
35     return jm_vector_get_item(jm_string)(&md->additionalModels,index);
36 }
37
38
39 const char* fmi1_xml_get_entry_point(fmi1_xml_model_description_t* md) {
40     return jm_vector_char2string(&md->entryPoint);
41 }
42
43 const char* fmi1_xml_get_mime_type(fmi1_xml_model_description_t* md){
44     return jm_vector_char2string(&md->mimeType);
45 }
46
47 int fmi1_xml_get_manual_start(fmi1_xml_model_description_t* md){
48     return md->manual_start;
49 }
50
51
52 int fmi1_xml_handle_Implementation(fmi1_xml_parser_context_t *context, const char* data) {
53     fmi1_xml_model_description_t* md = context->modelDescription;
54     if(!data) {
55                 jm_log_info(context->callbacks, module, "Processing implementation element (co-simulation FMU detected)");
56
57         md->fmuKind = fmi1_fmu_kind_enu_unknown; /* Explicitly set the "unknown" FMU kind here, in order to 
58                                                   * detect if the required CoSimulation_StandAlone and 
59                                                   * CoSimulation_Tool elements has been parsed below. 
60                                                   */
61     }
62     else {
63         /* According to https://trac.fmi-standard.org/ticket/309 we should report an error if neither of the
64          * requiered elements CoSimulation_StandAlone and CoSimulation_Tool is present in the Implementation
65          * element.
66          */
67         if (md->fmuKind != fmi1_fmu_kind_enu_cs_tool && md->fmuKind != fmi1_fmu_kind_enu_cs_standalone) {
68             jm_log_error(context->callbacks, module, 
69                 "Missing requiered XML element. Expected 'CoSimulation_StandAlone' or 'CoSimulation_Tool' element inside the 'Implementation' element.");
70         }
71         /* might give out a warning if(data[0] != 0) */
72     }
73     return 0;
74 }
75
76 int fmi1_xml_handle_CoSimulation_StandAlone(fmi1_xml_parser_context_t *context, const char* data) {
77     fmi1_xml_model_description_t* md = context->modelDescription;
78     if(!data) {
79         md->fmuKind = fmi1_fmu_kind_enu_cs_standalone;
80     }
81     else {
82         /* might give out a warning if(data[0] != 0) */
83     }
84     return 0;
85 }
86
87
88
89 int fmi1_xml_handle_CoSimulation_Tool(fmi1_xml_parser_context_t *context, const char* data) {
90     fmi1_xml_model_description_t* md = context->modelDescription;
91     if(!data) {
92         md->fmuKind = fmi1_fmu_kind_enu_cs_tool;
93     }
94     else {
95         /* might give out a warning if(data[0] != 0) */
96     }
97     return 0;
98 }
99
100 int fmi1_xml_handle_Model(fmi1_xml_parser_context_t *context, const char* data) {
101     fmi1_xml_model_description_t* md = context->modelDescription;
102     if(!data) {
103         return (
104         /* <xs:attribute name="entryPoint"  type="xs:anyURI" use="required"/> */
105         fmi1_xml_set_attr_string(context, fmi1_xml_elmID_Model, fmi_attr_id_entryPoint, 1, &(md->entryPoint)) ||
106         /* <xs:attribute name="manualStart" type="xs:boolean" default="false"/> */
107         fmi1_xml_set_attr_boolean(context,fmi1_xml_elmID_Model,fmi_attr_id_manualStart,0,&md->manual_start,0) ||
108         /* <xs:attribute name="type"        type="xs:string" use="required"/> */
109         fmi1_xml_set_attr_string(context, fmi1_xml_elmID_Model, fmi_attr_id_type,1,&md->mimeType)
110                     );
111     }
112     else {
113         /* might give out a warning if(data[0] != 0) */
114     }
115     return 0;
116 }
117
118 int fmi1_xml_handle_File(fmi1_xml_parser_context_t *context, const char* data) {
119     fmi1_xml_model_description_t* md = context->modelDescription;
120     if(!data) {
121             jm_vector(char)* bufFileName = fmi1_xml_get_parse_buffer(context,2);
122             char* fileName = 0;
123             jm_string* pname;
124             size_t len;
125             if(fmi1_xml_set_attr_string(context, fmi1_xml_elmID_Model, fmi_attr_id_file, 1, bufFileName))
126                 return -1;
127             len = jm_vector_get_size_char(bufFileName);
128             pname = jm_vector_push_back(jm_string)(&md->additionalModels,fileName);
129             if(pname) *pname = fileName =  md->callbacks->malloc(len + 1);
130             if(!pname || !fileName) {
131                 fmi1_xml_parse_fatal(context, "Could not allocate memory");
132                 return -1;
133             }
134             memcpy(fileName, jm_vector_get_itemp(char)(bufFileName,0), len);
135             fileName[len] = 0;
136     }
137     else {
138         /* might give out a warning if(data[0] != 0) */
139     }
140     return 0;
141 }
142