Class: Chemicalml::Convention::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/chemicalml/convention/constraint.rb

Overview

Abstract base class for a single constraint. Subclasses implement check_node (one node at a time) or check (whole document).

A constraint subclass MAY declare applies_to — one or more Cml::Role::* modules. When it does, the Coordinator only invokes check_node for nodes matching that role. Without applies_to, the constraint receives every node (legacy behaviour). Declaring applies_to is the fast path: the dispatch is O(node.ancestors) per node, not O(constraint_count).

Defined Under Namespace

Classes: DocumentConstraint, NodeConstraint

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConstraint

Returns a new instance of Constraint.



30
31
32
# File 'lib/chemicalml/convention/constraint.rb', line 30

def initialize
  freeze
end

Class Method Details

.applies_to(*roles) ⇒ Object

Declare one or more Role modules this constraint handles. applies_to Chemicalml::Cml::Role::Atom applies_to Chemicalml::Cml::Role::Bond, Chemicalml::Cml::Role::AtomParity



25
26
27
# File 'lib/chemicalml/convention/constraint.rb', line 25

def applies_to(*roles)
  @applies_to_roles = roles.flatten.freeze
end

.applies_to_rolesObject

Returns the Role module(s) this constraint applies to, or nil if it must run for every node.



18
19
20
# File 'lib/chemicalml/convention/constraint.rb', line 18

def applies_to_roles
  @applies_to_roles
end

Instance Method Details

#check(_document) ⇒ Object

Raises:

  • (NotImplementedError)


34
35
36
37
# File 'lib/chemicalml/convention/constraint.rb', line 34

def check(_document)
  raise NotImplementedError,
        "#{self.class} must implement #check(document)"
end