Skip to content

Refactor Mechanism #460

@K20shores

Description

@K20shores

Refactor Mechanism in musica.mechanism_configuration to use Python wrapper classes consistently for reactions, species, and phases.

Acceptance Criteria

  • Mechanism no longer inherits directly from _Mechanism.
  • Holds internal instance _instance of _Mechanism.
  • Constructor accepts Python-native objects: reactions (list of Python reaction classes), species (list of Python Species), phases (list of Python Phase), version (Python Version).
  • reactions attribute wraps and manages internal C++ reaction objects but exposes Python reaction classes.
  • to_dict() uses Python classes’ serialize() methods exclusively; no direct C++ class checks.
  • Remove all direct C++ type checks; rely on Python class hierarchy or duck typing.
  • export() uses MechanismSerializer.serialize passing the Python Mechanism instance.
  • MechanismSerializer.serialize() validates type, creates directory, and writes JSON or YAML from to_dict() output.
  • Raise errors for unsupported file formats and unsupported reaction types using Python classes.

Ideas

  • Implement composition pattern: hold _Mechanism instance in self._instance.
  • Map Python reaction classes in to_dict() instead of C++ types.
  • Use isinstance checks with Python classes only.
  • Avoid static methods for serialization; prefer instance methods.
  • Handle version as a Python class member, not as a C++ member.

Tasks

  • Change class Mechanism(_Mechanism) to class Mechanism holding self._instance.
  • Update __init__ to accept Python reaction, species, phase, version objects and create _Mechanism instance internally.
  • Refactor to_dict() to iterate Python-side lists and call their serialize() methods.
  • Remove C++-type checks (_Arrhenius, _Branched, etc.); rely on Python class type checks.
  • Update export() method to call MechanismSerializer.serialize(self, file_path).
  • Modify MechanismSerializer.serialize() to accept Python Mechanism and use to_dict().
  • Add proper error handling for unknown reaction types based on Python classes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions