InputParser

class geojson_modelica_translator.modelica.input_parser.InputParser(modelica_filename: str | Path)

Bases: object

Class to read in Modelica files (.mo) and provide basic operations.

This class is not recommended to be used and ModelicaBuilder should be used instead which is syntax-aware of the Modelica language.

__init__(modelica_filename: str | Path) None

Initialize the class with the modelica file to parse

Parameters:

modelica_filename (Union[str, Path]) – Path to the modelica file (.mo) to parse

Raises:
  • Exception – SyntaxError, more than one within

  • Exception – SyntaxError, unknown token

Methods

__init__(modelica_filename)

Initialize the class with the modelica file to parse

add_connect(a, b, annotation)

Add a new connection of port a to port b.

add_model_object(model_name, model_instance, ...)

Add a new model object to the model

add_parameter(var_type, var_name, value, ...)

Add a new parameter.

find_connect(port_a, port_b)

Find an existing connection that has port_a and/or port_b.

find_model_object(obj_name)

Find a model object in the list of parsed objects

init_vars()

parse_mo()

Parse the input if it is a .mo file.

reload()

Reparse the data.

remove_connect_string(a, b)

Remove a connection string that matches the a, b.

remove_object(obj_name)

Remove an object by a name.

replace_connect_string(a, b, new_a, new_b[, ...])

Replace content of the connect string with new_a and/or new_b

replace_model_string(model_name, ...)

Go through the models and find the model_name with a model_instance and change the value in the field to the new_value.

replace_within_string(new_string)

Replacement of the path portion of the within string

save()

Save the resulting file to the same file from which it was initialized

save_as(new_filename)

Save the resulting file with a new filename

serialize()

Serialize the modelica object to a string with line feeds

add_connect(a: str, b: str, annotation: str) None

Add a new connection of port a to port b. The annotation will be appended on a new line.

Parameters:
  • a (str) – port a

  • b (str) – port b

  • annotation (str) – description

add_model_object(model_name: str, model_instance: str, data: List[str]) None

Add a new model object to the model

Parameters:
  • model_name (str) – name of the model

  • model_instance (str) – model instance name

  • data (List[str]) – list of data to add

add_parameter(var_type: str, var_name: str, value: Any, description: str) None

Add a new parameter. Will be prepended to the top of the models list

Parameters:
  • var_type (str) – type of Modelica variable, Real, Integer, String, Modelica.Units.SI.Area, etc.

  • var_name (str) – name of the variable. Note that this does not check for conflicts.

  • value (any) – value to set the variable name to.

  • description (str) – description of the parameter

find_connect(port_a: str, port_b: str) Tuple[int | None, str | None]

Find an existing connection that has port_a and/or port_b. If there are more than one, then it will only return the first.

Parameters:
  • port_a (str) – port a

  • port_b (str) – port b

Raises:

Exception – could not find the connection

Returns:

index and connection tuple

Return type:

Tuple[Union[int, None], Union[str, None]]

find_model_object(obj_name: str) Tuple[int | None, str | None]

Find a model object in the list of parsed objects

Parameters:

obj_name (str) – name (including the instance)

Returns:

index and string of object

Return type:

Tuple[Union[int, None], Union[str, None]]

parse_mo()

Parse the input if it is a .mo file. This will populate the within, model, connections, and equations along with various other tokens. This is a very basic parser and will not work for all cases.

# TODO: move over to token-based parsing and assessment of the files. # TODO: strip all spacing and reconstruct on export

Raises:
  • Exception – General exception Exception(“More than one ‘within’ lines found”)

  • Exception – _description_

reload()

Reparse the data. This will remove any unsaved changes.

remove_connect_string(a: str, b: str) None

Remove a connection string that matches the a, b.

Parameters:
  • a (str) – existing port a

  • b (str) – existing port b

remove_object(obj_name: str) None

Remove an object by a name. Can be any part of the object name.

Parameters:

obj_name (str) – object name to match

replace_connect_string(a: str, b: str, new_a: str | None, new_b: str | None, replace_all: bool = False) None

Replace content of the connect string with new_a and/or new_b

Parameters:
  • a (str) – existing port a

  • b (str) – existing port b

  • new_a (str) – new port (or none)

  • new_b (str) – new port b (or none)

  • replace_all (bool, optional) – allow replacement of all strings. Defaults to False.

replace_model_string(model_name: str, model_instance: str, old_string: str, new_string: str)

Go through the models and find the model_name with a model_instance and change the value in the field to the new_value. This will replace the entire value of the model field.

This will not work with arrays or lists (e.g., {…}, […])

Parameters:
  • model_name (str) – name of the model

  • model_instance (str) – instance of the model

  • old_string (str) – name of the old string to replace

  • new_string (str) – new string

replace_within_string(new_string: str) None

Replacement of the path portion of the within string

Parameters:

new_string (str) – what to replace the existing within string with.

save() None

Save the resulting file to the same file from which it was initialized

save_as(new_filename: str | Path) None

Save the resulting file with a new filename

Parameters:

new_filename (Union[str, Path]) – name of the new file to save as

serialize() str

Serialize the modelica object to a string with line feeds

Returns:

string representation of the data

Return type:

str