Skip to content

Lossless BNG-XML \xe2\x86\x92 BNGL round-trip for tfun, rule modifiers, selectors#87

Merged
jrfaeder merged 1 commit into
RuleWorld:mainfrom
wshlavacek:modelapi-roundtrip-xml-fidelity
May 11, 2026
Merged

Lossless BNG-XML \xe2\x86\x92 BNGL round-trip for tfun, rule modifiers, selectors#87
jrfaeder merged 1 commit into
RuleWorld:mainfrom
wshlavacek:modelapi-roundtrip-xml-fidelity

Conversation

@wshlavacek
Copy link
Copy Markdown
Contributor

Summary

Three related fidelity fixes when reading BNG2.pl-emitted BNG-XML and re-emitting BNGL. Each fixes a specific shape that the parser currently drops, mangles, or warns about and ignores.

1. `tfun` placeholder reconstruction

BNG2.pl serializes `tfun(...)` calls — both the inline-array form `tfun([xs],[ys],ctr)` and the file-based `TFUN(ctr,"file")` form — by replacing the function body with a `TFUN_VAL` / `TFUN__VAL` placeholder and stashing the real arguments on attributes of the `` element (`@ctrName`, `@xData`, `@yData`, `@method`, `@file`).

`FunctionBlockXML.parse_xml` was reading the placeholder verbatim into the regenerated BNGL, which BNG2.pl can't re-parse. New `_resolve_expression` helper reconstructs the call from the attributes.

2. `RuleMod` supports multiple modifiers per rule

A single rule can legitimately carry, say, `DeleteMolecules` together with `include_reactants(...)`. `RuleMod.modifiers: list[str]` stores the BNGL serialization of each modifier in insertion order. `str` prefers the list when non-empty, falling back to the single `self.type` so existing callers that read `rule_mod.type` keep working.

3. `include` / `exclude` reactants / products as rule modifiers

These are now emitted as rule modifiers instead of being dropped with a stderr warning. BNG2.pl serializes them as `ListOfInclude{Reactants,Products}` / `ListOfExclude{Reactants,Products}` children on the rule, each carrying a pattern-index suffix (`_RP` / `_PP`) on the selector id and one or more `` entries. New helpers `_build_selector_modifier` and `_format_selector_pattern` mirror those back as e.g. `include_reactants(1, MyMol)`.

The `get_rule_mod` body is also tightened: it now tolerates rules that have no `ListOfOperations` at all (previously `KeyError`) and returns `None` only when there's genuinely no modifier to report, instead of dropping a real one when `ListOfOperations` happens to be missing.

Note on conflict with #76

This PR and #76 both modify the import block of `xmlparsers.py` to add `import re`, and both modify `FunctionBlockXML.parse_xml` to wrap the `Expression` read. The two rewrites compose — `_decode_xml_boolean_ops(_resolve_expression(...))` is the combined call — but I've kept them as separate PRs since the audit's table 0.4 split them. Whichever lands first, the other rebases trivially.

Test plan

  • Existing CI passes
  • A model using `tfun(...)` round-trips through XML and BNG2.pl re-parses the regenerated BNGL
  • A rule using `include_reactants(...)` round-trips through XML
  • A rule combining `DeleteMolecules` with `include_reactants(...)` emits both modifiers

Three related fidelity fixes when reading BNG2.pl-emitted BNG-XML and
re-emitting BNGL:

1. tfun placeholder reconstruction. BNG2.pl serializes ``tfun(...)``
   calls (both the inline-array form ``tfun([xs],[ys],ctr)`` and the
   file-based ``TFUN(ctr,"file")`` form) by replacing the function body
   with a ``__TFUN_VAL__`` / ``__TFUN__VAL__`` placeholder and stashing
   the real arguments on attributes of the <Function> element
   (``@ctrName``, ``@xData``, ``@yData``, ``@method``, ``@file``).
   ``FunctionBlockXML`` was reading the placeholder verbatim into the
   regenerated BNGL, which BNG2.pl can't re-parse. Reconstruct the call
   from the attributes via a new ``_resolve_expression`` helper.

2. ``RuleMod`` now supports multiple modifiers per rule. A rule can
   legitimately carry, say, ``DeleteMolecules`` together with
   ``include_reactants(...)``. ``RuleMod.modifiers: list[str]`` stores
   the BNGL serialization of each modifier in insertion order;
   ``__str__`` prefers the list when non-empty, falling back to the
   single ``self.type`` for backwards compatibility.

3. ``include_reactants`` / ``exclude_reactants`` /
   ``include_products`` / ``exclude_products`` are now emitted as rule
   modifiers instead of being dropped with a stderr warning. BNG2.pl
   serializes these as ``ListOfInclude{Reactants,Products}`` /
   ``ListOfExclude{Reactants,Products}`` children on the rule, each
   carrying a pattern-index suffix (``_RP<n>`` / ``_PP<n>``) on the
   selector id and one or more ``<Pattern>`` entries. New helpers
   ``_build_selector_modifier`` and ``_format_selector_pattern`` on
   ``RuleBlockXML`` mirror those back as e.g.
   ``include_reactants(1, MyMol)`` so the rule round-trips through
   BNG2.pl's BNGL parser.

The ``get_rule_mod`` body is also tightened: it now tolerates rules
that have no ``ListOfOperations`` at all (previously: ``KeyError``)
and returns ``None`` only when there is genuinely no modifier to
report, rather than dropping a real modifier on the floor when the
``ListOfOperations`` happens to be empty.
@jrfaeder jrfaeder merged commit 9042323 into RuleWorld:main May 11, 2026
17 checks passed
@wshlavacek wshlavacek deleted the modelapi-roundtrip-xml-fidelity branch May 11, 2026 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants