Class: Chemicalml::Convention::Coordinator

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

Overview

Single-pass constraint dispatcher. Replaces the previous O(N*C) walk (every constraint walked the whole tree) with O(N + total_applicable) — one tree walk per validation, with each node dispatched only to constraints that declared applies_to matching one of the node's ancestors.

Constraints that don't declare applies_to fall into a default "all nodes" bucket and run for every visited node, preserving backward compatibility.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constraint_classes) ⇒ Coordinator

Returns a new instance of Coordinator.



17
18
19
20
21
22
23
24
25
26
# File 'lib/chemicalml/convention/coordinator.rb', line 17

def initialize(constraint_classes)
  @document_constraints = constraint_classes
                            .select { |c| c < Constraint::DocumentConstraint }
                            .map(&:new)
  @node_constraints = constraint_constraints(constraint_classes)
                        .map(&:new)
  @always_applicable = []
  @by_role = Hash.new { |h, k| h[k] = [] }
  @node_constraints.each { |c| register(c) }
end

Instance Attribute Details

#document_constraintsObject (readonly)

Returns the value of attribute document_constraints.



15
16
17
# File 'lib/chemicalml/convention/coordinator.rb', line 15

def document_constraints
  @document_constraints
end

#node_constraintsObject (readonly)

Returns the value of attribute node_constraints.



15
16
17
# File 'lib/chemicalml/convention/coordinator.rb', line 15

def node_constraints
  @node_constraints
end

Instance Method Details

#validate(document) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chemicalml/convention/coordinator.rb', line 28

def validate(document)
  violations = @document_constraints.flat_map do |constraint|
    Array(constraint.check(document))
  end
  return violations unless visitable?(document)

  walk(document) do |node, path|
    applicable_for(node).each do |constraint|
      violations.concat(Array(constraint.check_node(node, path)))
    end
  end
  violations
end