Class: Udb::ExtensionRequirementExpression::LogicNode

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/udb/req_expression.rb

Overview

Abstract syntax tree of the logic

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, children, term_idx: nil)

Parameters:

Raises:

  • (ArgumentError)


382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/udb/req_expression.rb', line 382

def initialize(type, children, term_idx: nil)
  raise ArgumentError, "Children must be singular" if [TYPES::Term, TYPES::Not].include?(type) && children.size != 1
  raise ArgumentError, "Children must have two elements" if [TYPES::And, TYPES::Or, TYPES::If].include?(type) && children.size != 2

  if type == TYPES::Term
    raise ArgumentError, "Term must be an ExtensionRequirement (found #{children[0]})" unless children[0].is_a?(ExtensionRequirement)
  else
    raise ArgumentError, "All Children must be LogicNodes" unless children.all? { |child| child.is_a?(LogicNode) }
  end

  @type = type
  @children = children

  raise ArgumentError, "Need term_idx" if term_idx.nil? && type == TYPES::Term
  raise ArgumentError, "term_idx isn't an int" if !term_idx.is_a?(Integer) && type == TYPES::Term

  @term_idx = term_idx
end

Instance Attribute Details

#typeTYPES

Returns:



379
380
381
# File 'lib/udb/req_expression.rb', line 379

def type
  @type
end

Instance Method Details

#eval(term_values) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/udb/req_expression.rb', line 413

def eval(term_values)
  if @type == TYPES::Term
    ext_req = T.cast(@children[0], ExtensionRequirement)
    term_value = term_values.find { |tv| tv.name == ext_req.name }
    return false if term_value.nil?

    ext_req.satisfied_by?(term_value)
  elsif @type == TYPES::If
    cond_ext_ret = T.cast(@children[0], LogicNode)
    if cond_ext_ret.eval(term_values)
      T.cast(@children[1], LogicNode).eval(term_values)
    else
      false
    end
  elsif @type == TYPES::Not
    !T.cast(@children[0], LogicNode).eval(term_values)
  elsif @type == TYPES::And
    @children.all? { |child| T.cast(child, LogicNode).eval(term_values) }
  elsif @type == TYPES::Or
    @children.any? { |child| T.cast(child, LogicNode).eval(term_values) }
  end
end

#termsArray<ExtensionRequirement>

Returns The terms (leafs) of this tree.

Returns:



403
404
405
406
407
408
409
410
# File 'lib/udb/req_expression.rb', line 403

def terms
  @terms ||=
    if @type == TYPES::Term
      [@children[0]]
    else
      @children.map { |child| T.cast(child, LogicNode).terms }.flatten.uniq
    end
end

#to_sString

Returns:

  • (String)


437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/udb/req_expression.rb', line 437

def to_s
  if @type == TYPES::Term
    "(#{@children[0].to_s})"
  elsif @type == TYPES::Not
    "!#{@children[0]}"
  elsif @type == TYPES::And
    "(#{@children[0]} ^ #{@children[1]})"
  elsif @type == TYPES::Or
    "(#{@children[0]} v #{@children[1]})"
  elsif @type == TYPES::If
    "(#{@children[0]} -> #{@children[1]})"
  else
    T.absurd(@type)
  end
end