Skip to content

Commit

Permalink
- FMI import.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12467 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adeas31 committed Aug 10, 2012
1 parent f65d900 commit cef3e1a
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,18 @@ external "builtin";
annotation(preferredView="text");
end importFMU;

/* Under Development */
function importFMUNew "Imports the Functional Mockup Unit
Example command:
importFMU(\"A.fmu\");"
input String filename "the fmu file name";
input String workdir := "<default>" "The output directory for imported FMU files. <default> will put the files to current working directory.";
output Boolean success "Returns true on success";
external "builtin";
annotation(preferredView="text");
end importFMUNew;
/* Under Development */

function getSourceFile "Returns the filename of the class."
input TypeName class_;
output String filename "empty on failure";
Expand Down
1 change: 1 addition & 0 deletions Compiler/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ Socket.mo \
System.mo \
TaskGraphExt.mo \
BackendDAEEXT.mo \
FMI.mo \



Expand Down
12 changes: 12 additions & 0 deletions Compiler/Script/CevalScript.mo
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ protected import XMLDump;
protected import ComponentReference;
protected import Uncertainties;
protected import OpenTURNS;
protected import FMI;

public constant Integer RT_CLOCK_SIMULATE_TOTAL = 8;
public constant Integer RT_CLOCK_SIMULATE_SIMULATION = 9;
Expand Down Expand Up @@ -1442,6 +1443,17 @@ algorithm
then
(cache,Values.BOOL(false),st);

case (cache,env,"importFMUNew",{Values.STRING(filename),Values.STRING(workdir)},st,msg)
equation
workdir = Util.if_(System.directoryExists(workdir), workdir, System.pwd());
true = FMI.importFMU(filename, workdir);
then
(cache,Values.BOOL(true),st);

case (cache,env,"importFMUNew",_,st,msg)
then
(cache,Values.BOOL(false),st);

case (cache,env,"iconv",{Values.STRING(str),Values.STRING(from),Values.STRING(to)},st,msg)
equation
str = System.iconv(str,from,to);
Expand Down
43 changes: 43 additions & 0 deletions Compiler/Util/FMI.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
* OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/

encapsulated package FMI
" file: FMI.mo
package: FMI
description: This file contains FMI specific function, which are implemented in C."

public function importFMU
input String inFileName;
input String inWorkingDirectory;
output Boolean outBool;
external "C" outBool=FMIImpl__importFMU(inFileName, inWorkingDirectory) annotation(Library = "omcruntime");
end importFMU;

end FMI;
44 changes: 44 additions & 0 deletions Compiler/runtime/FMI_rml.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
* OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/

#include "FMIimpl.c"
#include "rml.h"

void FMI_5finit(void)
{

}

RML_BEGIN_LABEL(FMI__importFMU)
{
rmlA0 = (void*) mk_icon(FMIImpl__importFMU(RML_STRINGDATA(rmlA0),RML_STRINGDATA(rmlA1)));
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL
125 changes: 125 additions & 0 deletions Compiler/runtime/FMIimpl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
* OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>

#include "systemimpl.h"
#include "errorext.h"

/* return codes of the unzip tool */
#define UNZIP_NO_ERROR 0 /* success */
#define UNZIP_WARNINGS 1 /* one or more warning errors, but successfully completed anyway */
#define UNZIP_NOZIP 9 /* no zip files found */
#define UNZIP_METHOD_DECRYPTION_ERROR 81 /* unsupported compression methods or unsupported decryption */
#define UNZIP_WRONG_PASS 82 /* no files were found due to bad decryption password */
/* model description xml file name */
#define FMI_MODEL_DESCRIPTION_XML "modelDescription.xml"
#define FMI_SYSTEM_PATH_DELIMITER "/"

int extractFMU(char *fileName, char *workingDirectory)
{
int error;
int n;
char *cmd;
n = strlen(fileName) + strlen(workingDirectory) + 16;
cmd = (char*) malloc(n * sizeof(char));
sprintf(cmd, "unzip -q -o %s -d %s", fileName, workingDirectory);
/* -q quiet mode
* -o overwrite files WITHOUT prompting
* -d extract files into exdir
*/
error = system(cmd);
const char *c_tokens[1]={fileName};
if (error != UNZIP_NO_ERROR) {
switch (error) {
case UNZIP_WARNINGS:
c_add_message(-1, ErrorType_scripting, ErrorLevel_warning, gettext("some warnings occurred during decompression, success anyway."), NULL, 0);
break;
case UNZIP_NOZIP:
c_add_message(-1, ErrorType_scripting, ErrorLevel_error, gettext("File not Found: %s."), c_tokens, 1);
return 0;
case UNZIP_METHOD_DECRYPTION_ERROR:
c_add_message(-1, ErrorType_scripting, ErrorLevel_error, gettext("unsupported compression methods or unsupported decryption"), NULL, 0);
return 0;
case UNZIP_WRONG_PASS:
c_add_message(-1, ErrorType_scripting, ErrorLevel_error, gettext("No files were found due to bad decryption password."), NULL, 0);
return 0;
default:
c_add_message(-1, ErrorType_scripting, ErrorLevel_error, gettext("Unknown errors occurred during the decompression of file %s"), c_tokens, 1);
return 0;
}
}
free(cmd);
return 1;
}

char* getFMIModelDescriptionPath(char* workingDirectory)
{
char* modelDescriptionPath;
int len = strlen(workingDirectory) + strlen(FMI_SYSTEM_PATH_DELIMITER) + strlen(FMI_MODEL_DESCRIPTION_XML) + 1;
modelDescriptionPath = (char*) malloc(len * sizeof(char));
sprintf(modelDescriptionPath, "%s%s%s", workingDirectory, FMI_SYSTEM_PATH_DELIMITER, FMI_MODEL_DESCRIPTION_XML);
return modelDescriptionPath;
}

int parseXML(char* modelDescriptionPath)
{
/* Use FMIL to parse XML.*/
return 1;
}


int FMIImpl__importFMU(char *fileName, char* workingDirectory)
{
// check the if the fmu file exists
if (!SystemImpl__regularFileExists(fileName)) {
const char *c_tokens[1]={fileName};
c_add_message(-1, ErrorType_scripting, ErrorLevel_error, gettext("File not Found: %s."), c_tokens, 1);
return 0;
}
// extract the fmu file
if (!extractFMU(fileName, workingDirectory))
return 0;
// get the model description xml file path
char* modelDescriptionPath = getFMIModelDescriptionPath(workingDirectory);
// parse XML
if (!parseXML(modelDescriptionPath))
return 0;
return 1;
}

#ifdef __cplusplus
}
#endif

5 changes: 3 additions & 2 deletions Compiler/runtime/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ endif
RML_COMPAT=$(top_builddir)/SimulationRuntime/c/meta/rml_compatibility.h

SRC = Print_rml.c System_rml.c Settings_rml.c \
SimulationResults_rml.c IOStreamExt_rml.c Database_rml.c Socket_rml.c Lapack_rml.c ModelicaExternalC_rml.c matching.c matching_cheap.c
SimulationResults_rml.c IOStreamExt_rml.c Database_rml.c Socket_rml.c Lapack_rml.c ModelicaExternalC_rml.c matching.c matching_cheap.c FMI_rml.c

CPPSRC = unitparser.cpp UnitParserExt_rml.cpp ptolemyio_rml.cpp \
BackendDAEEXT_rml.cpp ErrorMessage.cpp Error_rml.cpp \
Expand All @@ -31,7 +31,7 @@ CPPSRC = unitparser.cpp UnitParserExt_rml.cpp ptolemyio_rml.cpp \
OBJ = $(SRC:.c=.o) $(CPPSRC:.cpp=.o) $(CPPSRC:.cc=.o)

OMC_OBJ = Error_omc.o Print_omc.o System_omc.o Settings_omc.o \
IOStreamExt_omc.o ErrorMessage.o systemimplmisc.o \
IOStreamExt_omc.o ErrorMessage.o FMI_rml.o systemimplmisc.o \
UnitParserExt_omc.o unitparser.o BackendDAEEXT_omc.o Socket_omc.o matching.o matching_cheap.o \
Database_omc.o Dynload_omc.o SimulationResults_omc.o ptolemyio_omc.o Lapack_omc.o $(OMCCORBASRC)

Expand Down Expand Up @@ -80,6 +80,7 @@ SimulationResults_omc.o : SimulationResults.c SimulationResultsCmp.c errorext.h
ptolemyio_rml.o : ptolemyio.cpp errorext.h
ptolemyio_omc.o : ptolemyio.cpp errorext.h $(RML_COMPAT)
ErrorMessage.o : ErrorMessage.cpp ErrorMessage.hpp errorext.h
FMI_rml.o : FMIimpl.c

clean:
$(RM) -rf *.a *.o omc_communication.cc omc_communication.h omc_communication-*

0 comments on commit cef3e1a

Please sign in to comment.