Package: 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 validityInstallpip install modelica-builderUsageModBuild provides specific methods for reading and modifying files.frommodelica_builder.modelimportModel# parse the model filesource_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 availablename=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 resultmodel.save_as('NewDCMotor.mo')You can also define your own classes for editing the filefrommodelica_builder.editimportEditfrommodelica_builder.selectorimportSelectorfrommodelica_builder.transformationimportSimpleTransformationclassMySelector(Selector):# implement class for selecting AST nodes# define the edit to make to the node's text and combine it with the selectoredit=Edit.make_replace('FOOBAR')selector=MySelector()transformation=SimpleTransformation(selector,edit)model=Model('my_modelica_file.mo')# add your custom transformationmodel.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.DevelopmentFor developers, dependency management is throughPoetry. Installation is accomplished by runningpip install poetry.pipinstallpoetry# install after cloning repopoetryinstallIf 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 containerdockerbuild-tantlr4:latest-fantlr/Dockerfile.# run parser generator for pythondockerrun-v"$(pwd)/modelica_builder/modelica_parser":/var/antlrResult\antlr4:latest\-Dlanguage=Python3/var/antlrResult/modelica.g4# commit results along with grammar fileIf not using Docker, Install antlr4 followingthese instructions# in modelica_building/modelica_parserantlr4-Dlanguage=Python3modelica.g4# commit results along with grammar fileManaged TasksUpdating LicensesTo 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.pyupdate_licensesTestingTo run the tests, simply run the following:poetryrunpytestKnown IssuesThe 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 InstructionsBump version to <NEW_VERSION> in setup.cfg (use semantic versioning as much as possible).Runpoetry run pre-commit –all-filesIn 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.Rungit tag <NEW_VERSION>. (Note thatpython setup.py –versionpulls 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 packagesrm-rfdist/*poetrypublish--buildPush 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.gitpushorigin<NEW_VERSION>