Skip to main content

Modelica builder enables programmatic parsing and modification of Modelica files.

Project description

The Modelica Builder project aims to make in-place modifications to Modelica language files easier. The principal use case is to load, modify using higher level abstracted methods, and then save the resulting file. The user has access to the entire Abstract Syntax Tree of the entire Modelica grammar.

The Modelica Builder project does not:

  • Compile nor check for syntax validity

Install

pip install modelica-builder

Usage

ModBuild provides specific methods for reading and modifying files.

from modelica_builder.model import Model

# parse the model file
source_file = 'DCMotor.mo'
model = Model(source_file)

# do read and modify the model
# refer to modelica_builder.model.Model class methods to see what's available
name = model.get_name()
model.set_name('New' + name)
model.add_connect('some.component.port_a', 'another.component.port_b')
model.insert_component('MyComponentType', 'myInst',
                        modifications={'arg1': '1234'}, string_comment='my comment',
                        annotations=['my annotation'], insert_index=0)

# save the result
model.save_as('NewDCMotor.mo')

You can also define your own classes for editing the file

from modelica_builder.edit import Edit
from modelica_builder.selector import Selector
from modelica_builder.transformation import SimpleTransformation

class MySelector(Selector):
    # implement class for selecting AST nodes

# define the edit to make to the node's text and combine it with the selector
edit = Edit.make_replace('FOOBAR')
selector = MySelector()
transformation = SimpleTransformation(selector, edit)

model = Model('my_modelica_file.mo')
# add your custom transformation
model.add(transformation)
model.save_as('result.mo')

Transformations specify what nodes to change and how to change them. This is done by combining Selectors and Edits. Selectors specify how to select nodes in the AST, and edits are modifications (insert, replace, delete) to the text of selected nodes.

A Transformer is a collection of Transformations, which can then be applied to a file.

See the tests for more examples and information.

Development

For developers, dependency management is through Poetry. Installation is accomplished by running pip install poetry.

pip install poetry

# install after cloning repo
poetry install

If you change the source grammar file you need to regenerate the parser and lexer.

With docker installed, run these commands from this the repo’s root directory

# build Antlr container
docker build -t antlr4:latest -f antlr/Dockerfile .

# run parser generator for python
docker run -v "$(pwd)/modelica_builder/modelica_parser":/var/antlrResult \
    antlr4:latest \
    -Dlanguage=Python3 /var/antlrResult/modelica.g4

# commit results along with grammar file

If not using Docker, Install antlr4 following these instructions

# in modelica_building/modelica_parser
antlr4 -Dlanguage=Python3 modelica.g4

# commit results along with grammar file

Managed Tasks

Updating Licenses

To apply the copyright/license to all the files, run the following managed task. To update, then update the script, and then rerun the managed task.

./setup.py update_licenses

Testing

To run the tests, simply run the following:

poetry run pytest

Known Issues

  • The transformations occur on strings which are immutable. Need to investigate using byte arrays. This does not cause errors, but can be slow when parsing really large modelica files.

Release Instructions

  • Bump version to <NEW_VERSION> in pyproject.toml (use semantic versioning, e.g., 0.5.0, 0.5.1.rc1).

  • Run poetry run pre-commit –all-files

  • In a prep-release branch, push the changes to GitHub and draft a release against the latest branch.
    • Run ‘auto-generate changelog’ and copy the contents to the CHANGELOG.rst. Cull any items that are repeated.

    • Discard the draft release (you will create an official one off of the main branch)

  • Merge the prep-release branch into develop.

  • Create a PR against develop into main.

  • Run git tag <NEW_VERSION>. (Note that python setup.py –version pulls from the latest tag`.)

  • After main branch passes, then merge and checkout the main branch. Build the distribution using the following code:

# Remove old dist packages
rm -rf dist/*

poetry publish --build
  • Push the tag to GitHub after everything is published to PyPi, then go to GitHub and add in the CHANGELOG.rst notes into the tagged release and officially release.

git push origin <NEW_VERSION>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modelica_builder-0.5.2rc2.tar.gz (82.6 kB view details)

Uploaded Source

Built Distribution

modelica_builder-0.5.2rc2-py3-none-any.whl (89.6 kB view details)

Uploaded Python 3

File details

Details for the file modelica_builder-0.5.2rc2.tar.gz.

File metadata

  • Download URL: modelica_builder-0.5.2rc2.tar.gz
  • Upload date:
  • Size: 82.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for modelica_builder-0.5.2rc2.tar.gz
Algorithm Hash digest
SHA256 e44232d59e0bed9bbed3eeb224e08387403c210bd43fe448d7466de0a7b69534
MD5 e21d57025fe9bf3cbb0b0c2298f085f5
BLAKE2b-256 aeb1401b5bc1de8a5f3db63c7e65ae6c6d05638dca3719356f29b6baa44c1733

See more details on using hashes here.

File details

Details for the file modelica_builder-0.5.2rc2-py3-none-any.whl.

File metadata

File hashes

Hashes for modelica_builder-0.5.2rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 9f30d067637ccb3e3b8e99233654671af6b8713f8d3358adb19fac647d06c969
MD5 75e9a188dc30d7ed2e4374e42385242d
BLAKE2b-256 097a6d249507d4f1808ef8ebf7251759ca15f3b9ffff6a7ded8d1aca40e3cfbf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page