Skip to main content

Modelica Builder

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

  1. Bump version to <NEW_VERSION> in pyproject.toml (use semantic versioning, e.g., 0.5.0, 0.5.1.rc1).
  2. Run poetry run pre-commit run --all-files
  3. 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.md. Cull any items that are repeated.
    • Discard the draft release (you will create an official one off of the main branch later)
  4. Merge the prep-release branch into develop.
  5. From command line, merge develop into main with: git checkout main; git pull; git merge --ff-only origin develop; git push
  6. In GitHub, tag the release against main. Copy and paste the changelog entry into the notes. Verify the release is posted to PyPI.

Release files for modelica-builder 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modelica-builder 0.8.0
File Size Uploaded
modelica_builder-0.8.0.tar.gz 83.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for modelica-builder 0.8.0
File Interpreter ABI Platform
modelica_builder-0.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 174.5 kB

Release files / modelica_builder-0.8.0.tar.gz

Download URL modelica_builder-0.8.0.tar.gz
Size 83.3 kB
Tags Source
SHA-256 checksum
How to use checksums
282b0394289c38d3958973061e13652b2bff1901edc9417b8135e7ad84c6293b
BLAKE2b-256 checksum
How to use checksums
66915110cdf16a8793a0b02f88071aa3bdfb427403e25b760fb2ee4669012e2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 15, 2026.

Transparency log

Release files / modelica_builder-0.8.0-py3-none-any.whl

Download URL modelica_builder-0.8.0-py3-none-any.whl
Size 91.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea4858c373dfed987be37d587254b09e11fa857628083d658276d422e3e3ac33
BLAKE2b-256 checksum
How to use checksums
4daa2b34f02a688c8f3d18395f3d80adca1fc022e5b3bbda3a30ec11eb95a7fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 15, 2026.

Transparency log
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page