Class: Chemicalml::Cml::CanonicalComparison
- Inherits:
-
Object
- Object
- Chemicalml::Cml::CanonicalComparison
- Defined in:
- lib/chemicalml/cml/canonical_comparison.rb
Overview
Semantic document comparison. Two documents are canonically equal if their structural fingerprints match — same element types, same attribute values, same number of children at each node, ignoring attribute order and child order within collection attributes.
Useful for testing (asserting round-trip equality without string-equality noise) and for diff tools that want to ignore formatting differences.
Instance Attribute Summary collapse
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#right ⇒ Object
readonly
Returns the value of attribute right.
Instance Method Summary collapse
-
#diff ⇒ Object
Returns the difference as a hash of element_name → count_delta.
-
#equal? ⇒ Boolean
True if the two documents are canonically equivalent.
-
#initialize(left, right) ⇒ CanonicalComparison
constructor
A new instance of CanonicalComparison.
Constructor Details
#initialize(left, right) ⇒ CanonicalComparison
Returns a new instance of CanonicalComparison.
19 20 21 22 |
# File 'lib/chemicalml/cml/canonical_comparison.rb', line 19 def initialize(left, right) @left = left @right = right end |
Instance Attribute Details
#left ⇒ Object (readonly)
Returns the value of attribute left.
15 16 17 |
# File 'lib/chemicalml/cml/canonical_comparison.rb', line 15 def left @left end |
#right ⇒ Object (readonly)
Returns the value of attribute right.
15 16 17 |
# File 'lib/chemicalml/cml/canonical_comparison.rb', line 15 def right @right end |
Instance Method Details
#diff ⇒ Object
Returns the difference as a hash of element_name → count_delta. Empty hash means canonically equal.
31 32 33 34 35 36 37 38 39 |
# File 'lib/chemicalml/cml/canonical_comparison.rb', line 31 def diff l = fingerprint(left) r = fingerprint(right) keys = (l.keys + r.keys).uniq keys.each_with_object({}) do |k, h| delta = (r[k] || 0) - (l[k] || 0) h[k] = delta if delta.nonzero? end end |
#equal? ⇒ Boolean
True if the two documents are canonically equivalent.
25 26 27 |
# File 'lib/chemicalml/cml/canonical_comparison.rb', line 25 def equal? fingerprint(left) == fingerprint(right) end |