expression.rb 9.15 KB
Newer Older
Brice Videau's avatar
Brice Videau committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
module CCS
  ExpressionType = enum FFI::Type::INT32, :ccs_expression_type_t, [
    :CCS_OR, 0,
    :CCS_AND,
    :CCS_EQUAL,
    :CCS_NOT_EQUAL,
    :CCS_LESS,
    :CCS_GREATER,
    :CCS_LESS_OR_EQUAL,
    :CCS_GREATER_OR_EQUAL,
    :CCS_ADD,
    :CCS_SUBSTRACT,
    :CCS_MULTIPLY,
    :CCS_DIVIDE,
    :CCS_MODULO,
    :CCS_POSITIVE,
    :CCS_NEGATIVE,
    :CCS_NOT,
19
    :CCS_IN,
Brice Videau's avatar
Brice Videau committed
20
21
22
23
24
25
26
27
28
29
    :CCS_LIST,
    :CCS_LITERAL,
    :CCS_VARIABLE
  ]
  class MemoryPointer
    def read_ccs_expression_type_t
      ExpressionType.from_native(read_int32, nil)
    end
  end

Brice Videau's avatar
Brice Videau committed
30
31
32
33
34
35
36
37
38
39
40
  AssociativityType = enum FFI::Type::INT32, :ccs_associativity_type_t, [
    :CCS_ASSOCIATIVITY_NONE, 0,
    :CCS_LEFT_TO_RIGHT,
    :CCS_RIGHT_TO_LEFT
  ]
  class MemoryPointer
    def read_ccs_associativity_type_t
      AssociativityType.from_native(read_int32, nil)
    end
  end

Brice Videau's avatar
Brice Videau committed
41
  attach_variable :expression_precedence, :ccs_expression_precedence, FFI::ArrayType::new(find_type(:int), ExpressionType.symbol_map.size)
Brice Videau's avatar
Brice Videau committed
42
  attach_variable :expression_associativity, :ccs_expression_associativity, FFI::ArrayType::new(find_type(:ccs_associativity_type_t), ExpressionType.symbol_map.size)
Brice Videau's avatar
Brice Videau committed
43
44
45
46
47
48
  attach_variable :expression_symbols, :ccs_expression_symbols, FFI::ArrayType::new(find_type(:string), ExpressionType.symbol_map.size)
  attach_variable :expression_arity, :ccs_expression_arity, FFI::ArrayType::new(find_type(:int), ExpressionType.symbol_map.size)

  ExpressionPrecedence = ExpressionType.symbol_map.collect { |k, v|
    [k, expression_precedence[v]]
  }.to_h
Brice Videau's avatar
Brice Videau committed
49
50
51
  ExpressionAssociativity = ExpressionType.symbol_map.collect { |k, v|
    [k, expression_associativity[v]]
  }.to_h
Brice Videau's avatar
Brice Videau committed
52
  ExpressionSymbols = ExpressionType.symbol_map.collect { |k, v|
53
54
55
56
    [k, expression_symbols[v]]
  }.to_h
  ExpressionArity = ExpressionType.symbol_map.collect { |k, v|
    [k, expression_arity[v]]
Brice Videau's avatar
Brice Videau committed
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
119
120
121
122
123
124
125
126
127
128
129
130
131
  }.to_h

  attach_function :ccs_create_binary_expression, [:ccs_expression_type_t, :ccs_datum_t, :ccs_datum_t, :pointer], :ccs_result_t
  attach_function :ccs_create_unary_expression, [:ccs_expression_type_t, :ccs_datum_t, :pointer], :ccs_result_t
  attach_function :ccs_create_expression, [:ccs_expression_type_t, :size_t, :pointer, :pointer], :ccs_result_t
  attach_function :ccs_create_literal, [:ccs_datum_t, :pointer], :ccs_result_t
  attach_function :ccs_create_variable, [:ccs_hyperparameter_t, :pointer], :ccs_result_t
  attach_function :ccs_expression_get_type, [:ccs_expression_t, :pointer], :ccs_result_t
  attach_function :ccs_expression_get_num_nodes, [:ccs_expression_t, :pointer], :ccs_result_t
  attach_function :ccs_expression_get_nodes, [:ccs_expression_t, :size_t, :pointer, :pointer], :ccs_result_t
  attach_function :ccs_literal_get_value, [:ccs_expression_t, :pointer], :ccs_result_t
  attach_function :ccs_variable_get_hyperparameter, [:ccs_expression_t, :pointer], :ccs_result_t
  attach_function :ccs_expression_eval, [:ccs_expression_t, :ccs_context_t, :pointer, :pointer], :ccs_result_t
  attach_function :ccs_expression_list_eval_node, [:ccs_expression_t, :ccs_context_t, :pointer, :size_t, :pointer], :ccs_result_t
  attach_function :ccs_expression_get_hyperparameters, [:ccs_expression_t, :size_t, :pointer, :pointer], :ccs_result_t
  attach_function :ccs_expression_check_context, [:ccs_expression_t, :ccs_context_t], :ccs_result_t

  class Expression < Object
    add_property :type, :ccs_expression_type_t, :ccs_expression_get_type, memoize: true
    add_property :num_nodes, :size_t, :ccs_expression_get_num_nodes, memoize: true
    def initialize(handle = nil, retain: false, type: nil, nodes: [])
      if handle
        super(handle, retain: retain)
      else
        count = nodes.size
        p_nodes = MemoryPointer::new(:ccs_datum_t, count)
        ptr = MemoryPointer::new(:ccs_expression_t)
        nodes.each_with_index { |n, i| Datum::new(p_nodes[i]).value = n }
        res = CCS.ccs_create_expression(type, count, p_nodes, ptr)
        CCS.error_check(res)
        super(ptr.read_ccs_expression_t, retain: false)
      end
    end

    def self.from_handle(handle)
      ptr = MemoryPointer::new(:ccs_expression_type_t)
      res = CCS.ccs_expression_get_type(handle, ptr)
      CCS.error_check(res)
      case ptr.read_ccs_expression_type_t
      when :CCS_LIST
        List::new(handle, retain: true)
      when :CCS_LITERAL
	Literal::new(handle, retain: true)
      when :CCS_VARIABLE
        Variable::new(handle, retain: true)
      else
        Expression::new(handle, retain: true)
      end
    end

    def self.binary(type:, left:, right:)
      ptr = MemoryPointer::new(:ccs_expression_t)
      res = CCS.ccs_create_binary_expression(type, Datum.from_value(left), Datum.from_value(right), ptr)
      CCS.error_check(res)
      self.new(ptr.read_ccs_expression_t, retain: false)
    end

    def self.unary(type:, node:)
      ptr = MemoryPointer::new(:ccs_expression_t)
      res = CCS.ccs_create_unary_expression(type, Datum.from_value(node), ptr)
      CCS.error_check(res)
      self.new(ptr.read_ccs_expression_t, retain: false)
    end

    def nodes
      count = num_nodes
      ptr = MemoryPointer::new(:ccs_expression_t, count)
      res = CCS.ccs_expression_get_nodes(@handle, count, ptr, nil)
      CCS.error_check(res)
      count.times.collect { |i| Expression.from_handle(ptr[i].read_pointer) }
    end

    def eval(context: nil, values: nil)
      if values && context
        count = context.num_hyperparameters
132
        raise CCSError, :CCS_INVALID_VALUES if values.size != count
Brice Videau's avatar
Brice Videau committed
133
134
135
        p_values = MemoryPointer::new(:ccs_datum_t, count)
        values.each_with_index{ |v, i| Datum::new(p_values[i]).value = v }
        values = p_values
Brice Videau's avatar
Brice Videau committed
136
137
      elsif values || context
        raise CCSError, :CCS_INVALID_VALUES
Brice Videau's avatar
Brice Videau committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
      end
      ptr = MemoryPointer::new(:ccs_datum_t)
      res = CCS.ccs_expression_eval(@handle, context, values, ptr)
      CCS.error_check(res)
      Datum::new(ptr).value
    end

    def hyperparameters
      @hyperparameters ||= begin
        ptr = MemoryPointer::new(:size_t)
        res = CCS.ccs_expression_get_hyperparameters(@handle, 0, nil, ptr)
        CCS.error_check(res)
        count = ptr.read_size_t
        if count == 0
          []
        else
          ptr = MemoryPointer::new(:ccs_hyperparameter_t, count)
          res = CCS.ccs_expression_get_hyperparameters(@handle, count, ptr, nil)
          CCS.error_check(res)
          count.times.collect { |i| Hyperparameters.from_handle(ptr[i].read_ccs_hyperparameter_t) }
        end
      end
    end

    def check_context(context)
      res = CCS.ccs_expression_check_context(@handle, context)
      CCS.error_check(res)
Brice Videau's avatar
Brice Videau committed
165
      self
Brice Videau's avatar
Brice Videau committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    end

    def to_s
      t = type
      symbol = ExpressionSymbols[t]
      prec = ExpressionPrecedence[t]
      nds = nodes.collect { |n|
        nprec = ExpressionPrecedence[n.type]
        if nprec < prec
          "(#{n})"
        else
          n.to_s
        end
      }
      if nds.length == 1
        "#{symbol}#{nds[0]}"
      else
        "#{nds[0]} #{symbol} #{nds[1]}"
      end
    end
  end

  class Literal < Expression
    def initialize(handle = nil, retain: false, value: nil)
      if handle
        super(handle, retain: retain)
      else
        ptr = MemoryPointer::new(:ccs_expression_t)
        res = CCS.ccs_create_literal(Datum::from_value(value), ptr)
        CCS.error_check(res)
        super(ptr.read_ccs_expression_t, retain: false)
      end
    end

    def value
      ptr = MemoryPointer::new(:ccs_datum_t)
      res = CCS.ccs_literal_get_value(@handle, ptr)
      CCS.error_check(res)
      Datum::new(ptr).value
    end

    def to_s
Brice Videau's avatar
Brice Videau committed
208
      case value
Brice Videau's avatar
Brice Videau committed
209
210
211
      when nil
        "none"
      when String
Brice Videau's avatar
Brice Videau committed
212
213
214
215
        value.inspect
      else
        value.to_s
      end
Brice Videau's avatar
Brice Videau committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
    end
  end

  class Variable < Expression
    def initialize(handle = nil, retain: false, hyperparameter: nil)
      if handle
        super(handle, retain: retain)
      else
        ptr = MemoryPointer::new(:ccs_expression_t)
        res = CCS.ccs_create_variable(hyperparameter, ptr)
        CCS.error_check(res)
        super(ptr.read_ccs_expression_t, retain: false)
      end
    end

    def hyperparameter
      ptr = MemoryPointer::new(:ccs_hyperparameter_t)
      res = CCS.ccs_variable_get_hyperparameter(@handle, ptr)
      CCS.error_check(res)
      Hyperparameter.from_handle(ptr.read_ccs_hyperparameter_t)
    end

    def to_s
      hyperparameter.name
    end
  end

  class List < Expression
    def initialize(handle = nil, retain: false, values: nil)
      if handle
        super(handle, retain: retain)
      else
        super(nil, retain: false, type: :CCS_LIST, nodes: values)
      end
    end

Brice Videau's avatar
Brice Videau committed
252
    def eval(index, context: nil, values: nil)
Brice Videau's avatar
Brice Videau committed
253
254
      if values && context
        count = context.num_hyperparameters
255
        raise CCSError, :CCS_INVALID_VALUES if values.size != count
Brice Videau's avatar
Brice Videau committed
256
257
258
        p_values = MemoryPointer::new(:ccs_datum_t, count)
        values.each_with_index{ |v, i| Datum::new(p_values[i]).value = v }
        values = p_values
Brice Videau's avatar
Brice Videau committed
259
260
      elsif values || context
        raise CCSError, :CCS_INVALID_VALUES
Brice Videau's avatar
Brice Videau committed
261
262
263
264
265
266
267
268
269
270
271
272
      end
      ptr = MemoryPointer::new(:ccs_datum_t)
      res = CCS.ccs_expression_list_eval_node(@handle, context, values, index, ptr)
      CCS.error_check(res)
      Datum::new(ptr).value
    end

    def to_s
      "[ #{nodes.collect(&:to_s).join(", ")} ]"
    end
  end
end