Skip to content

Added formula extraction and evaluation - #19

Draft
HassanAkbar wants to merge 13 commits into
mainfrom
feat/formula-extraction
Draft

HassanAkbar wants to merge 13 commits into
mainfrom
feat/formula-extraction

Conversation

@HassanAkbar

Copy link
Copy Markdown
Member

Adds Dcc::Extract::Formula and a dcc extract formulae command. A certificate can
embed a MathML formula plus its parameters; this reads both and evaluates them in
BigDecimal, with -v NAME=VALUE to override a parameter.

All five reference values match PTB's own dcclib test suite exactly.

Known and accepted:

  • Opens against fix/parse-data-loss, not main. Formula parameters live inside
    dcc:list, which that branch stops dropping. On main this fails 22 examples.
  • Rejects <cn> forms it cannot read exactly rather than guessing. A base
    attribute or a <sep/>-separated value now raises, where both previously
    returned a plausible wrong number: <cn base="8">17</cn> read as 17, not 15.
  • Operand counts are checked in Ast::Apply's constructor, so a hand-built AST is
    held to the same contract as a parsed one. <power/> with one operand used to
    compute x**x.
  • Adds bigdecimal to the gemspec, unpinned like thor. Only the long-stable
    BigMath subset is used.
  • Quantity#uncertainty is carried through but nothing populates it yet.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds first-class support for extracting and evaluating embedded Content MathML formulae from DCC documents, including a CLI entrypoint to print evaluated results with optional variable overrides.

Changes:

  • Introduces Dcc::Extract::Formula with AST construction, MathML parsing, binding extraction, and BigDecimal-based evaluation.
  • Adds dcc extract formulae FILE with repeatable -v NAME=V1,V2 overrides and formatted output.
  • Adds comprehensive RSpec coverage for parsing, evaluation semantics, error handling, and CLI behavior.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
spec/dcc/extract/formula/parser_spec.rb New specs covering MathML parsing, operand ordering, roots, and malformed input handling.
spec/dcc/extract/formula/evaluator_spec.rb New specs covering numeric evaluation, precision, domain errors, broadcasting, and override validation behavior.
spec/dcc/extract/formula_spec.rb New integration specs verifying end-to-end extraction, binding resolution (including nested lists), and evaluation matches reference values.
spec/dcc/cli/cli_spec.rb Adds CLI specs for extract formulae output, repeatable -v, broadcasting, and clean failure modes.
lib/dcc/extract/formula/quantity.rb Adds Quantity coercion/validation boundary for numeric inputs (scalars + lists) into finite BigDecimals.
lib/dcc/extract/formula/parser.rb Adds MathML-to-AST parser, including operator mapping, constant handling, and structured <cn> rejection.
lib/dcc/extract/formula/evaluator.rb Adds BigDecimal/BigMath-based evaluator with precision rules, finite-result checks, and list broadcasting.
lib/dcc/extract/formula/ast.rb Adds AST node types and arity enforcement for operator application nodes.
lib/dcc/extract/formula.rb Adds extraction pipeline wiring: locate formulae, gather quantity bindings (including nested lists), and build ASTs.
lib/dcc/cli/cli.rb Extends extract command with formulae target and repeatable -v overrides parsing/validation + printing.
dcc.gemspec Adds bigdecimal runtime dependency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/dcc/extract/formula/parser.rb
Comment thread lib/dcc/extract/formula/evaluator.rb
Comment thread lib/dcc/extract/formula/evaluator.rb

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

Suppressed comments (1)

lib/dcc/extract/formula.rb:3

  • data_blocks uses ::Set.new but this file never requires the stdlib set library. Nothing in the repo appears to require "set", so this relies on incidental load order from dependencies and can break with Ruby/dep changes (NameError: uninitialized constant Set).
require "bigdecimal"

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.

Suppressed comments (1)

lib/dcc/extract/formula.rb:4

  • Dcc::Extract::Formula uses ::Set (walk(dcc, ::Set.new, ...)) but this file does not require "set". Set is not part of Ruby core, so in a minimal runtime this will raise NameError: uninitialized constant Set when formula extraction is invoked. Add an explicit require so extraction doesn’t depend on transitive requires from other libraries.
# frozen_string_literal: true

require "bigdecimal"

HassanAkbar and others added 3 commits August 24, 2026 19:39
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Base automatically changed from fix/parse-data-loss to main August 24, 2026 14:52
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