Method: Musa::Rules::Rules#apply

Defined in:
lib/musa-dsl/generative/rules.rb

#apply(object_or_list, node = nil, **parameters) ⇒ Node

Applies rules to seed objects sequentially.

Processes list of seed objects in sequence, generating possibilities from each confirmed endpoint of previous seed. Returns tree of all valid combination paths.

Examples:

Single seed

tree = rules.apply(:I)
tree.combinations  # => [[:I, :ii, :V, :I], ...]

Multiple seeds

tree = rules.apply([:I, :ii, :V])
tree.combinations  # => combinations starting from each seed

Parameters:

  • object_or_list (Object, Array)

    seed object(s) to process

  • node (Node, nil) (defaults to: nil)

    root node (creates if nil)

  • parameters (Hash)

    additional parameters for rules

Returns:

  • (Node)

    root node with all combination paths



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/musa-dsl/generative/rules.rb', line 228

def apply(object_or_list, node = nil, **parameters)
  list = object_or_list.arrayfy.clone

  node ||= Node.new

  seed = list.shift

  if seed
    result = generate_possibilities seed, node, **parameters

    fished = result.fish

    node.reject! 'All children are rejected' if fished.empty?

    fished.each do |object|
      subnode = node.add(object).mark_as_ended!
      apply list, subnode, **parameters
    end
  end

  node
end