OpenModelica / OMPython

A Python interface to OpenModelica communicating via CORBA or ZeroMQ
Other
108 stars 58 forks source link

OMPython does not parse correctly expressions in array_subscripts #231

Open kataph opened 3 days ago

kataph commented 3 days ago

Description

in modelica, array declarations allow subscripts to be expressions (consider e.g. this lines in the specifications:

array_subscripts :
"[" subscript { "," subscript } "]"
subscript :
":" | expression

) Indeed, the openmodelica compiler can parse such things. However, OMPython cannot deal with the ensuing outputs.

Steps to Reproduce

Minimum example:

import OMPython
omc = OMPython.OMCSessionZMQ()                                                                      
omc.sendExpression("loadString(\"model A Integer x[1+1,1]; end A;\")")
omc.sendExpression("getComponents(A)")

Actual result:

>>> import OMPython
>>> omc = OMPython.OMCSessionZMQ()                                                                      
>>> omc.sendExpression("loadString(\"model A Integer x[1+1,1]; end A;\")")
True
>>> omc.sendExpression("getComponents(A)")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Francesco\AppData\Local\Programs\Python\Python312\Lib\site-packages\OMPython\__init__.py", line 783, in sendExpression
    answer = OMTypedParser.parseString(result)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Francesco\AppData\Local\Programs\Python\Python312\Lib\site-packages\OMPython\OMTypedParser.py", line 120, in parseString
    res = omcGrammar.parseString(string)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Francesco\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyparsing\util.py", line 256, in _inner
    return fn(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Francesco\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyparsing\core.py", line 1200, in parse_string
    raise exc.with_traceback(None)
pyparsing.exceptions.ParseException: Expected end of text, found '{'  (at char 0), (line:1, col:1)

Expected Behavior

That OMPython is able to parse correctly the raw output of the open modelica compiler getComponents scripting function, which in this case would be:

>> getComponents(A)
{{Integer, x, "", "public", false, false, false, false, "unspecified", "none", "unspecified", {1 + 1,1}}}

so, hypothetically, something like

(('Integer', 'x', '', 'public', False, False, False, False, 'unspecified', 'none', 'unspecified', (1+1, 1)),)

Version and OS

casella commented 3 days ago

@arun3688 how do you see that? Is it easy to fix?

arun3688 commented 3 days ago

@kataph the immediate solution would be to use parsed=False in sendExpression and then you do some conversion on the string by yourself in python see below

>>> import OMPython
>>> omc = OMPython.OMCSessionZMQ()                                                                      
>>> omc.sendExpression("loadString(\"model A Integer x[1+1,1]; end A;\")")
>>> omc.sendExpression("getComponents(A)", parsed=False) ## this returns the unparsed string 

the problem seems to be x[1+1, 1]

kataph commented 3 days ago

@arun3688 Yes, the problem appears to be (at least) due arithmetic expressions. In the codebase I have to interact with there are various parameters that determine the dimensions of certain arrays by means of arithmetic operations, in particular summation. The solution I am using at the moment indeed takes the raw input and parses it in a cursorily way. However, it is not trivial for me to develop a parser that could parse any output of getComponent(...) with full generality.

arun3688 commented 3 days ago

@kataph I added the rules to parser which can handle simple arithmetic operations in array dimensions, see the below examples

>>> import OMPython
>>> omc = OMPython.OMCSessionZMQ()
>>> omc.sendExpression("loadString(\"model A Integer x[5*3,1+7]; end A;\")")
True
>>> omc.sendExpression("getComponents(A)")
(('Integer', 'x', '', 'public', False, False, False, False, 'unspecified', 'none', 'unspecified', (15, 8)),) 

I hope you have simple arithmetic operations like in the example, if that is the case i will push the code soon

arun3688 commented 2 days ago

@kataph the issue is fixed with this commit 317b944d32e98d824e66dc644b62331b20664fd8, update your master or pip install viapython -m pip install -U https://github.com/OpenModelica/OMPython/archive/master.zip