Class: Chemicalml::Convention::Molecular::Constraints::BondIdsUniqueWithinMolecule

Inherits:
Constraint::DocumentConstraint show all
Defined in:
lib/chemicalml/convention/molecular/constraints/bond_ids_unique_within_molecule.rb

Overview

Molecular convention: a <bond> id MUST be unique within the eldest containing molecule. Mirrors the existing AtomIdsUniqueWithinMolecule constraint for bonds.

Instance Method Summary collapse

Instance Method Details

#check(document) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/chemicalml/convention/molecular/constraints/bond_ids_unique_within_molecule.rb', line 11

def check(document)
  violations = []
  walk_nodes(document) do |node, path|
    next unless molecule?(node)

    ids = bond_ids_within(node)
    duplicates = ids.group_by { |x| x }.select { |_, v| v.length > 1 }.keys
    duplicates.each do |dup|
      violations << violation(
        path: (path + [describe(node)]).join('/'),
        message: "duplicate bond id #{dup.inspect} within molecule #{node.id.inspect}"
      )
    end
  end
  violations
end