Module: Chemicalml::Cml::Visitable
- Included in:
- Schema24::Abundance, Schema24::Action, Schema24::ActionList, Schema24::Alternative, Schema24::Amount, Schema24::Angle, Schema24::Annotation, Schema24::AnyCml, Schema24::Appinfo, Schema24::Arg, Schema24::Array, Schema24::ArrayList, Schema24::Atom, Schema24::AtomArray, Schema24::AtomParity, Schema24::AtomSet, Schema24::AtomType, Schema24::AtomTypeList, Schema24::AtomicBasisFunction, Schema24::Band, Schema24::BandList, Schema24::BasisSet, Schema24::Bond, Schema24::BondArray, Schema24::BondSet, Schema24::BondStereo, Schema24::BondType, Schema24::BondTypeList, Schema24::CellParameter, Schema24::ComplexObject, Schema24::ConditionList, Schema24::Crystal, Schema24::Definition, Schema24::Description, Schema24::Dictionary, Schema24::DictionaryEntry, Schema24::Dimension, Schema24::Document, Schema24::Documentation, Schema24::Eigen, Schema24::Electron, Schema24::Enumeration, Schema24::Expression, Schema24::Float, Schema24::FloatArray, Schema24::Formula, Schema24::Fragment, Schema24::FragmentList, Schema24::Gradient, Schema24::Identifier, Schema24::Integer, Schema24::IntegerArray, Schema24::Isotope, Schema24::IsotopeList, Schema24::Join, Schema24::Kpoint, Schema24::KpointList, Schema24::Label, Schema24::Lattice, Schema24::LatticeVector, Schema24::Length, Schema24::Line3, Schema24::Link, Schema24::List, Schema24::Map, Schema24::Matrix, Schema24::Mechanism, Schema24::MechanismComponent, Schema24::Metadata, Schema24::MetadataList, Schema24::Module, Schema24::Molecule, Schema24::MoleculeList, Schema24::Name, Schema24::Object, Schema24::Observation, Schema24::Operator, Schema24::Parameter, Schema24::ParameterList, Schema24::Particle, Schema24::Peak, Schema24::PeakGroup, Schema24::PeakList, Schema24::PeakStructure, Schema24::Plane3, Schema24::Point3, Schema24::Potential, Schema24::PotentialForm, Schema24::PotentialList, Schema24::Product, Schema24::ProductList, Schema24::Property, Schema24::PropertyList, Schema24::Reactant, Schema24::ReactantList, Schema24::Reaction, Schema24::ReactionList, Schema24::ReactionScheme, Schema24::ReactionStep, Schema24::ReactionStepList, Schema24::ReactiveCentre, Schema24::Region, Schema24::RelatedEntry, Schema24::Sample, Schema24::Scalar, Schema24::Spectator, Schema24::SpectatorList, Schema24::Spectrum, Schema24::SpectrumData, Schema24::SpectrumList, Schema24::Sphere3, Schema24::Stmml, Schema24::String, Schema24::StringArray, Schema24::Substance, Schema24::SubstanceList, Schema24::Symmetry, Schema24::System, Schema24::Table, Schema24::TableCell, Schema24::TableContent, Schema24::TableHeader, Schema24::TableHeaderCell, Schema24::TableRow, Schema24::TableRowList, Schema24::Tcell, Schema24::Torsion, Schema24::Transform3, Schema24::TransitionState, Schema24::Trow, Schema24::Unit, Schema24::UnitList, Schema24::UnitType, Schema24::UnitTypeList, Schema24::Vector3, Schema24::Xaxis, Schema24::Yaxis, Schema24::ZMatrix, Schema3::Abundance, Schema3::Action, Schema3::ActionList, Schema3::Amount, Schema3::Angle, Schema3::AnyCml, Schema3::Array, Schema3::ArrayList, Schema3::Atom, Schema3::AtomArray, Schema3::AtomParity, Schema3::AtomSet, Schema3::AtomType, Schema3::AtomTypeList, Schema3::AtomicBasisFunction, Schema3::Band, Schema3::BandList, Schema3::BasisSet, Schema3::Bond, Schema3::BondArray, Schema3::BondSet, Schema3::BondStereo, Schema3::BondType, Schema3::BondTypeList, Schema3::CellParameter, Schema3::ConditionList, Schema3::Crystal, Schema3::Definition, Schema3::Description, Schema3::Dictionary, Schema3::DictionaryEntry, Schema3::Dimension, Schema3::Document, Schema3::Documentation, Schema3::Eigen, Schema3::Electron, Schema3::Formula, Schema3::Fragment, Schema3::FragmentList, Schema3::Gradient, Schema3::Identifier, Schema3::Isotope, Schema3::IsotopeList, Schema3::Join, Schema3::Kpoint, Schema3::KpointList, Schema3::Label, Schema3::Lattice, Schema3::LatticeVector, Schema3::Length, Schema3::Line3, Schema3::Link, Schema3::List, Schema3::Map, Schema3::Matrix, Schema3::Mechanism, Schema3::MechanismComponent, Schema3::Metadata, Schema3::MetadataList, Schema3::Module, Schema3::Molecule, Schema3::MoleculeList, Schema3::Name, Schema3::Object, Schema3::Observation, Schema3::Parameter, Schema3::ParameterList, Schema3::Particle, Schema3::Peak, Schema3::PeakGroup, Schema3::PeakList, Schema3::PeakStructure, Schema3::Plane3, Schema3::Point3, Schema3::Potential, Schema3::PotentialForm, Schema3::PotentialList, Schema3::Product, Schema3::ProductList, Schema3::Property, Schema3::PropertyList, Schema3::Reactant, Schema3::ReactantList, Schema3::Reaction, Schema3::ReactionList, Schema3::ReactionScheme, Schema3::ReactionStep, Schema3::ReactionStepList, Schema3::ReactiveCentre, Schema3::Region, Schema3::Sample, Schema3::Scalar, Schema3::Spectator, Schema3::SpectatorList, Schema3::Spectrum, Schema3::SpectrumData, Schema3::SpectrumList, Schema3::Sphere3, Schema3::Stmml, Schema3::Substance, Schema3::SubstanceList, Schema3::Symmetry, Schema3::System, Schema3::Table, Schema3::TableCell, Schema3::TableContent, Schema3::TableHeader, Schema3::TableHeaderCell, Schema3::TableRow, Schema3::TableRowList, Schema3::Torsion, Schema3::Transform3, Schema3::TransitionState, Schema3::Unit, Schema3::UnitList, Schema3::UnitType, Schema3::UnitTypeList, Schema3::Vector3, Schema3::Xaxis, Schema3::Yaxis, Schema3::ZMatrix
- Defined in:
- lib/chemicalml/cml/visitable.rb
Overview
Marker mixin included by every wire class. Provides a uniform interface the convention-constraint walker relies on:
- `wire_children` returns the child wire nodes
- `node_id` returns the id attribute or `nil`
- `element_name` returns the XML tag name
All three use lutaml-model's attribute registry — no duck typing.
A wire class that doesn't declare an :id attribute simply has
node_id return nil.
Instance Method Summary collapse
-
#atom_count ⇒ Object
Count of every Atom in the subtree.
-
#bond_count ⇒ Object
Count of every Bond in the subtree.
- #collect_wire_nodes(value) ⇒ Object
- #declared_attribute?(name) ⇒ Boolean
- #declared_attribute_names ⇒ Object
-
#each_atom ⇒ Object
Iterate every Atom in the subtree.
-
#each_bond ⇒ Object
Iterate every Bond in the subtree.
-
#each_molecule ⇒ Object
Iterate every Molecule in the subtree (top-level + nested).
-
#each_wire_node {|Lutaml::Model::Serializable| ... } ⇒ Enumerator
Walk the entire subtree from this node, yielding each wire instance.
- #element_name ⇒ Object
-
#find_atom(id) ⇒ Object
Find the first atom in the subtree with the given id, or nil.
-
#find_bond(id) ⇒ Object
Find the first bond in the subtree with the given id, or nil.
-
#find_molecule(id) ⇒ Object
Find the first molecule in the subtree with the given id.
-
#molecule_count ⇒ Object
Count of every Molecule in the subtree (recursive).
- #node_id ⇒ Object
- #wire_children ⇒ Object
- #wire_node?(value) ⇒ Boolean
Instance Method Details
#atom_count ⇒ Object
Count of every Atom in the subtree.
113 114 115 |
# File 'lib/chemicalml/cml/visitable.rb', line 113 def atom_count each_atom.count end |
#bond_count ⇒ Object
Count of every Bond in the subtree.
118 119 120 |
# File 'lib/chemicalml/cml/visitable.rb', line 118 def bond_count each_bond.count end |
#collect_wire_nodes(value) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/chemicalml/cml/visitable.rb', line 36 def collect_wire_nodes(value) return [] if value.nil? if value.is_a?(::Array) value.select { |v| wire_node?(v) } elsif wire_node?(value) [value] else [] end end |
#declared_attribute?(name) ⇒ Boolean
58 59 60 61 62 |
# File 'lib/chemicalml/cml/visitable.rb', line 58 def declared_attribute?(name) self.class.model.attributes.key?(name) rescue StandardError false end |
#declared_attribute_names ⇒ Object
52 53 54 55 56 |
# File 'lib/chemicalml/cml/visitable.rb', line 52 def declared_attribute_names self.class.model.attributes.keys rescue StandardError [] end |
#each_atom ⇒ Object
Iterate every Atom in the subtree.
77 78 79 80 81 |
# File 'lib/chemicalml/cml/visitable.rb', line 77 def each_atom(&) return to_enum(:each_atom) unless block_given? each_wire_node { |n| yield(n) if n.is_a?(Chemicalml::Cml::Role::Atom) } end |
#each_bond ⇒ Object
Iterate every Bond in the subtree.
84 85 86 87 88 |
# File 'lib/chemicalml/cml/visitable.rb', line 84 def each_bond(&) return to_enum(:each_bond) unless block_given? each_wire_node { |n| yield(n) if n.is_a?(Chemicalml::Cml::Role::Bond) } end |
#each_molecule ⇒ Object
Iterate every Molecule in the subtree (top-level + nested).
91 92 93 94 95 |
# File 'lib/chemicalml/cml/visitable.rb', line 91 def each_molecule(&) return to_enum(:each_molecule) unless block_given? each_wire_node { |n| yield(n) if n.is_a?(Chemicalml::Cml::Role::Molecule) } end |
#each_wire_node {|Lutaml::Model::Serializable| ... } ⇒ Enumerator
Walk the entire subtree from this node, yielding each wire instance. DFS pre-order. Useful for find-style queries and aggregate counts.
70 71 72 73 74 |
# File 'lib/chemicalml/cml/visitable.rb', line 70 def each_wire_node(&) return to_enum(:each_wire_node) unless block_given? walk(self, &) end |
#element_name ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/chemicalml/cml/visitable.rb', line 28 def element_name xml_mapping = self.class.mappings[:xml] root = xml_mapping&.root root&.name || self.class.name.split('::').last rescue StandardError self.class.name.split('::').last end |
#find_atom(id) ⇒ Object
Find the first atom in the subtree with the given id, or nil.
98 99 100 |
# File 'lib/chemicalml/cml/visitable.rb', line 98 def find_atom(id) each_atom.find { |a| a.node_id == id } end |
#find_bond(id) ⇒ Object
Find the first bond in the subtree with the given id, or nil.
103 104 105 |
# File 'lib/chemicalml/cml/visitable.rb', line 103 def find_bond(id) each_bond.find { |b| b.node_id == id } end |
#find_molecule(id) ⇒ Object
Find the first molecule in the subtree with the given id.
108 109 110 |
# File 'lib/chemicalml/cml/visitable.rb', line 108 def find_molecule(id) each_molecule.find { |m| m.node_id == id } end |
#molecule_count ⇒ Object
Count of every Molecule in the subtree (recursive).
123 124 125 |
# File 'lib/chemicalml/cml/visitable.rb', line 123 def molecule_count each_molecule.count end |
#node_id ⇒ Object
22 23 24 25 26 |
# File 'lib/chemicalml/cml/visitable.rb', line 22 def node_id return id if declared_attribute?(:id) nil end |
#wire_children ⇒ Object
16 17 18 19 20 |
# File 'lib/chemicalml/cml/visitable.rb', line 16 def wire_children declared_attribute_names.flat_map do |name| collect_wire_nodes(public_send(name)) end end |
#wire_node?(value) ⇒ Boolean
48 49 50 |
# File 'lib/chemicalml/cml/visitable.rb', line 48 def wire_node?(value) value.is_a?(Lutaml::Model::Serializable) end |