File: evaluation.rb

package info (click to toggle)
ruby-morpher 0.2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 520 kB
  • sloc: ruby: 2,366; makefile: 4
file content (118 lines) | stat: -rw-r--r-- 2,341 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module Morpher
  # Abstract namespace class for evaluation states
  class Evaluation
    include AbstractType, Printer::Mixin, Adamantium::Flat, Anima.new(
      :evaluator,
      :input,
      :output,
      :success
    )

    private :success

    # Test if evaluation was successful
    #
    # @return [true]
    #   if evaluation was successful
    #
    # @return [false]
    #   otherwise
    #
    # @api private
    #
    alias_method :success?, :success
    public :success?

    ERROR_DEFAULTS = IceNine.deep_freeze(
      output: Undefined,
      success: false
    )

    SUCCESS_DEFAULTS = IceNine.deep_freeze(
      success: true
    )

    # Return error instance
    #
    # @param [Hash<Symbol, Object>] attributes
    #
    # @return [Evaluation]
    #
    # @api private
    #
    def self.error(attributes)
      new(ERROR_DEFAULTS.merge(attributes))
    end

    # Return successful instance
    #
    # @param [Hash<Symbol, Object>] attributes
    #
    # @return [Evaluation]
    #
    # @api private
    #
    def self.success(attributes)
      new(SUCCESS_DEFAULTS.merge(attributes))
    end

    # Evaluation state for nullary evaluators
    class Nullary < self

      printer do
        name
        indent do
          attributes :input, :output, :success?
          visit :evaluator
        end
      end

    end

    # Evaluation state for nary evaluators
    class Nary < self
      include anima.add(:evaluations)

      printer do
        name
        indent do
          attributes :input, :output, :success?
          attribute_class :evaluator
          visit_many :evaluations
        end
      end

    end # Evaluation

    # Evaluation state for unary evaluators
    class Binary < self
      include anima.add(:left_evaluation, :right_evaluation)

      printer do
        name
        indent do
          attributes :input, :output, :success?
          visit :left_evaluation
          visit :right_evaluation
        end
      end

    end # Unary

    # Evaluation state for unary evaluators
    class Unary < self
      include anima.add(:operand_evaluation)

      printer do
        name
        indent do
          attributes :input, :output, :success?
          visit :operand_evaluation
          visit :evaluator
        end
      end

    end # Unary

  end # Evaluation
end # Morpher