Skip to content

Commit

Permalink
Issue387 package.order (lbl-srg#388)
Browse files Browse the repository at this point in the history
* Corrected generation of package.order for lbl-srg#387

* Corrected generation of package.order for lbl-srg#387
  • Loading branch information
mwetter authored Nov 10, 2020
1 parent c047e11 commit 9056b55
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions buildingspy/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ BuildingsPy Changelog
Version 2.2.0, xxx, 2020 -- Release 2.2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- xxx
- Corrected generation of package.order for constant arrays (https://github.com/lbl-srg/BuildingsPy/issues/387)
- Refactored class buildingspy.simulate.Dymola, and added buildingspy.simulate.Optimica
- Added check for wrong derivative implementation that is reported by Dymola 2021x (https://github.com/lbl-srg/BuildingsPy/issues/376).

Expand Down
24 changes: 20 additions & 4 deletions buildingspy/development/refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,25 @@ def write_package_order(directory=".", recursive=False):
filPac.write(p[1] + "\n")


def _get_constants(lines):
""" Get a list with all constants.
:param: lines All lines of the Modelica file.
"""
import re
# Constants can be "constant Real n = ..." or "constant someClass n(..."
# or "constant Real n[:] = ..." or or "constant Real n[4] = ..."
# See also https://regex101.com/r/cD5nE0/2 for testing
f = re.findall(
r"\s*constant\s+[\w+\.]+\s+(\w+)(\[\w+\]|\[\s*[\w:]\s*(,\s*[\w:]\s*)*\])?\s*[=\(]",
lines,
re.MULTILINE)
r = []
for ele in f:
r.append(ele[0])
return r


def _get_package_list_for_file(directory, file_name):
""" Gets the package list for the file `directory/file_name`
"""
Expand All @@ -509,10 +528,7 @@ def _get_package_list_for_file(directory, file_name):
# They need to be added to the package.order as well.
with open(os.path.join(directory, file_name), mode="r", encoding="utf-8-sig") as fil:
lines = fil.read()
# Constants can be 'constant Real n = ..." or "constant someClass n(..."
con = re.findall(
r"\s*constant\s+[a-zA-Z0-9_\.]+\s+(\w+)\s*[=\(]", lines, re.MULTILINE)
# con=re.search(r"constant\s+\w+\s+(\w+)\s*=", lines, re.MULTILINE);
con = _get_constants(lines)
for ele in con:
# Found a constant whose name is in con.group(1)
pacLis.append([__CON, ele])
Expand Down
30 changes: 30 additions & 0 deletions buildingspy/tests/test_development_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@ def test_write_package_order(self):

self.assertEqual(pac_lis, correct, "Parsing package.order failed.")

def test_get_constants_non_empty(self):
import buildingspy.development.refactor as r

lines = """
constant Real a = 1 "some text";
constant Real b = 1;
constant Real A = 1;
constant Real B[2] = {1, 2};
constant Real C[:] = {1, 2};
constant Real D[1,2] = {{1}, {1, 2}};
constant Real E[:,:] = {{1}, {1, 2}};
not_a_constant f = 1;
"""
con = r._get_constants(lines)
self.assertEqual(con, ['a', 'b', 'A', 'B', 'C', 'D', 'E'], "Failed to get all constants.")

def test_get_constants_empty(self):
import buildingspy.development.refactor as r

lines = """
"""
con = r._get_constants(lines)
for ele in con:
print(f"--{ele}--")
self.assertEqual(
con,
[],
"Failed to get all constants for a file content with no constants.")

def test_get_modelica_file_name(self):
import os
import buildingspy.development.refactor as r
Expand Down

0 comments on commit 9056b55

Please sign in to comment.