Class: Chemicalml::Cml::CanonicalComparison

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(left, right) ⇒ CanonicalComparison

Returns a new instance of CanonicalComparison.

Parameters:

  • left (Lutaml::Model::Serializable)

    one document.

  • right (Lutaml::Model::Serializable)

    the other document.



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

#leftObject (readonly)

Returns the value of attribute left.



15
16
17
# File 'lib/chemicalml/cml/canonical_comparison.rb', line 15

def left
  @left
end

#rightObject (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

#diffObject

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.

Returns:

  • (Boolean)


25
26
27
# File 'lib/chemicalml/cml/canonical_comparison.rb', line 25

def equal?
  fingerprint(left) == fingerprint(right)
end