ModelicaSpecification icon indicating copy to clipboard operation
ModelicaSpecification copied to clipboard

External function annotations assumptions

Open adrpo opened this issue 3 years ago • 2 comments

The spec: https://specification.modelica.org/master/functions.html#annotations-for-external-libraries-and-include-files says that annotations: Library, LibraryDirectory, Include and IncludeDirectory can all specify either a scalar value or an array of values.

Why do we have this restriction? All the tools should be able to support constants in these annotations.

It prevents writing flexible packages such as: https://github.com/adrpo/OMCallPython/tree/ConfigurePython using constants:

package Py
  constant PyIncludePath = "";
  constant PyLib = "";

 class PythonLibraryHandle
      extends ExternalObject;
  
      function constructor "External object constructor that loads the python dll"
        input String pyLibPath = PyLib;
        output PythonLibraryHandle pyHandle;

        external "C" pyHandle = omc_PyLibLoad(pyLibPath) annotation (
          IncludeDirectory = {"modelica://OMCallPython/Resources/C-Sources", PyIncludePath},
          Include = "#include \"OMCallPython.h\"");
      end constructor;
 ...
end Py;

model Test
  // here we could even call some functions to fetch these values from some environment variables
  package ConfiguredPy = Py(PyIncludePath = "E:/bin/py64/include", PyLib = "E:/bin/py64/python3.dll");
  ....
  ConfiguredPy.PythonLibraryHandle pyHandle = ConfiguredPy.PythonLibraryHandle();
end Test;

adrpo avatar Mar 09 '22 11:03 adrpo

It's an interesting enhancement, but I see a number of practical issues:

  • All tools do currently not support this.
  • It seems to open up rather wild ideas with string-handling for the Include-directive.
  • It seems necessary to also concatenate constant string arrays, and not only constant string values.
  • Using functions to extract them from environment variables mean that we first run some functions to generate includes for other functions that we can then run, it seems that can in turn be used for the next function ...

HansOlsson avatar Mar 09 '22 12:03 HansOlsson

It's an interesting enhancement, but I see a number of practical issues:

  • All tools do currently not support this.

They do, at least in graphical user annotations.

  • It seems to open up rather wild ideas with string-handling for the Include-directive.

Sure, but much more flexibility.

  • It seems necessary to also concatenate constant string arrays, and not only constant string values.

True

  • Using functions to extract them from environment variables mean that we first run some functions to generate includes for other functions that we can then run, it seems that can in turn be used for the next function ...

One could easy add a getEnvironmentVariable operator to the Modelica Specification. You already have that in the MSL: Modelica.Utilities.System.getEnvironmentVariable. But yes, you could cascade functions like that.

adrpo avatar Mar 09 '22 16:03 adrpo