Lossless BNG-XML \xe2\x86\x92 BNGL round-trip for tfun, rule modifiers, selectors#87
Open
wshlavacek wants to merge 1 commit intoRuleWorld:mainfrom
Open
Lossless BNG-XML \xe2\x86\x92 BNGL round-trip for tfun, rule modifiers, selectors#87wshlavacek wants to merge 1 commit intoRuleWorld:mainfrom
wshlavacek wants to merge 1 commit intoRuleWorld:mainfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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