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 Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConstraint

Returns a new instance of Constraint.



50
51
52
# File 'lib/chemicalml/convention/constraint.rb', line 50

def initialize
  freeze
end

Class Attribute Details

.applies_to_rolesObject (readonly)

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

.descriptionString

Human-readable description of the rule this constraint enforces. Subclasses MAY override. Falls back to the class name. Used by chemicalml constraints and the auto-generated constraint docs.

Returns:

  • (String)


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

def description
  @description || name.split('::').last
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



23
24
25
# File 'lib/chemicalml/convention/constraint.rb', line 23

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

.default_severitySymbol

Severity this constraint emits. Subclasses MAY override. Default :error — warning subclasses override to :warning.

Returns:

  • (Symbol)

    :error or :warning.



45
46
47
# File 'lib/chemicalml/convention/constraint.rb', line 45

def default_severity
  :error
end

Instance Method Details

#check(_document) ⇒ Object

Raises:

  • (NotImplementedError)


54
55
56
57
# File 'lib/chemicalml/convention/constraint.rb', line 54

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