Class: SpotFeel::Dmn::LiteralExpression

Inherits:
Object
  • Object
show all
Defined in:
lib/spot_feel/dmn/literal_expression.rb

Direct Known Subclasses

UnaryTests

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, text:) ⇒ LiteralExpression

Returns a new instance of LiteralExpression.



12
13
14
15
# File 'lib/spot_feel/dmn/literal_expression.rb', line 12

def initialize(id: nil, text:)
  @id = id
  @text = text&.strip
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/spot_feel/dmn/literal_expression.rb', line 6

def id
  @id
end

#textObject (readonly)

Returns the value of attribute text.



6
7
8
# File 'lib/spot_feel/dmn/literal_expression.rb', line 6

def text
  @text
end

Class Method Details

.builtin_functionsObject



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
132
133
134
135
136
137
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
165
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
208
209
210
211
212
213
214
215
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/spot_feel/dmn/literal_expression.rb', line 84

def self.builtin_functions
  HashWithIndifferentAccess.new({
    # Conversion functions
    "string": ->(from) { 
      return if from.nil?
      from.to_s 
    },
    "number": ->(from) { 
      return if from.nil?
      from.include?(".") ? from.to_f : from.to_i 
    },
    # Boolean functions
    # "not": ->(value) { value == true ? false : true },
    "is defined": ->(value) { 
      return if value.nil?
      !value.nil? 
    },
    "get or else": ->(value, default) { 
      value.nil? ? default : value 
    },
    # String functions
    "substring": ->(string, start, length) { 
      return if string.nil? || start.nil?
      return "" if length.nil?
      string[start - 1, length] 
    },
    "substring before": ->(string, match) { 
      return if string.nil? || match.nil?
      string.split(match).first 
    },
    "substring after": ->(string, match) { 
      return if string.nil? || match.nil?
      string.split(match).last 
    },
    "string length": ->(string) { 
      return if string.nil?
      string.length 
    },
    "upper case": ->(string) { 
      return if string.nil?
      string.upcase
    },
    "lower case": -> (string) { 
      return if string.nil?
      string.downcase 
    },
    "contains": ->(string, match) { 
      return if string.nil? || match.nil?
      string.include?(match) 
    },
    "starts with": ->(string, match) { 
      return if string.nil? || match.nil?
      string.start_with?(match) 
    },
    "ends with": ->(string, match) { 
      return if string.nil? || match.nil?
      string.end_with?(match) 
    },
    "matches": ->(string, match) { 
      return if string.nil? || match.nil?
      string.match?(match) 
    },
    "replace": ->(string, match, replacement) { 
      return if string.nil? || match.nil? || replacement.nil?
      string.gsub(match, replacement) 
    },
    "split": ->(string, match) { 
      return if string.nil? || match.nil?
      string.split(match) 
    },
    "strip": -> (string) { 
      return if string.nil?
      string.strip 
    },
    "extract": -> (string, pattern) {
      return if string.nil? || pattern.nil?
      string.match(pattern).captures 
    },
    # Numeric functions
    "decimal": ->(n, scale) { 
      return if n.nil? || scale.nil?
      n.round(scale) 
    },
    "floor": ->(n) {
      return if n.nil?
      n.floor 
    },
    "ceiling": ->(n) { 
      return if n.nil?
      n.ceil 
    },
    "round up": ->(n) { 
      return if n.nil?
      n.ceil 
    },
    "round down": ->(n) { 
      return if n.nil?
      n.floor 
    },
    "abs": ->(n) { 
      return if n.nil?
      n.abs 
    },
    "modulo": ->(n, divisor) { 
      return if n.nil? || divisor.nil?
      n % divisor 
    },
    "sqrt": ->(n) { 
      return if n.nil?
      Math.sqrt(n) 
    },
    "log": ->(n) { 
      return if n.nil?
      Math.log(n) 
    },
    "exp": ->(n) { 
      return if n.nil?
      Math.exp(n) 
    },
    "odd": ->(n) { 
      return if n.nil?
      n.odd? 
    },
    "even": ->(n) { 
      return if n.nil?
      n.even? 
    },
    "random number": ->(n) { 
      return if n.nil?
      rand(n) 
    },
    # List functions
    "list contains": ->(list, match) { 
      return if list.nil?
      return false if match.nil?
      list.include?(match) 
    },
    "count": ->(list) { 
      return if list.nil?
      return 0 if list.empty?
      list.length 
    },
    "min": ->(list) { 
      return if list.nil?
      list.min 
    },
    "max": ->(list) { 
      return if list.nil?
      list.max 
    },
    "sum": ->(list) { 
      return if list.nil?
      list.sum 
    },
    "product": ->(list) { 
      return if list.nil?
      list.inject(:*) 
    },
    "mean": ->(list) {
      return if list.nil?
      list.sum / list.length 
    },
    "median": ->(list) {
      return if list.nil?
      list.sort[list.length / 2] 
    },
    "stddev": ->(list) {
      return if list.nil?
      mean = list.sum / list.length.to_f
      Math.sqrt(list.map { |n| (n - mean)**2 }.sum / list.length)
    },
    "mode": ->(list) { 
      return if list.nil?
      list.group_by(&:itself).values.max_by(&:size).first 
    },
    "all": ->(list) {
      return if list.nil?
      list.all? 
    },
    "any": ->(list) {
      return if list.nil?
      list.any? 
    },
    "sublist": ->(list, start, length) { 
      return if list.nil? || start.nil?
      return [] if length.nil?
      list[start - 1, length] 
    },
    "append": ->(list, item) { 
      return if list.nil?
      list + [item] 
    },
    "concatenate": ->(list1, list2) { 
      return [nil, nil] if list1.nil? && list2.nil?
      return [nil] + list2 if list1.nil?
      return list1 + [nil] if list2.nil? 
      Array.wrap(list1) + Array.wrap(list2)
    },
    "insert before": ->(list, position, item) { 
      return if list.nil? || position.nil?
      list.insert(position - 1, item) 
    },
    "remove": ->(list, position) { 
      return if list.nil? || position.nil?
      list.delete_at(position - 1); list 
    },
    "reverse": ->(list) { 
      return if list.nil?
      list.reverse 
    },
    "index of": ->(list, match) { 
      return if list.nil?
      return [] if match.nil?
      list.index(match) + 1 
    },
    "union": ->(list1, list2) { 
      return if list1.nil? || list2.nil?
      list1 | list2 
    },
    "distinct values": ->(list) { 
      return if list.nil?
      list.uniq 
    },
    "duplicate values": ->(list) { 
      return if list.nil?
      list.select { |e| list.count(e) > 1 }.uniq 
    },
    "flatten": ->(list) { 
      return if list.nil?
      list.flatten 
    },
    "sort": ->(list) { 
      return if list.nil?
      list.sort 
    },
    "string join": ->(list, separator) { 
      return if list.nil?
      list.join(separator) 
    },
    # Context functions
    "get value": ->(context, name) { 
      return if context.nil? || name.nil?
      context[name] 
    },
    "context put": ->(context, name, value) { 
      return if context.nil? || name.nil?
      context[name] = value; context 
    },
    "context merge": ->(context1, context2) { 
      return if context1.nil? || context2.nil?
      context1.merge(context2) 
    },
    "get entries": ->(context) { 
      return if context.nil?
      context.entries 
    },
    # Temporal functions
    "now": ->() { Time.now },
    "today": ->() { Date.today },
    "day of week": ->(date) { 
      return if date.nil?
      date.wday 
    },
    "day of year": ->(date) { 
      return if date.nil?
      date.yday 
    },
    "week of year": ->(date) { 
      return if date.nil?
      date.cweek 
    },
    "month of year": ->(date) { 
      return if date.nil?
      date.month 
    },
  })
end

.from_json(json) ⇒ Object



8
9
10
# File 'lib/spot_feel/dmn/literal_expression.rb', line 8

def self.from_json(json)
  LiteralExpression.new(id: json[:id], text: json[:text])
end

Instance Method Details

#as_jsonObject



362
363
364
365
366
367
# File 'lib/spot_feel/dmn/literal_expression.rb', line 362

def as_json
  {
    id: id,
    text: text,
  }
end

#evaluate(variables = {}) ⇒ Object



26
27
28
# File 'lib/spot_feel/dmn/literal_expression.rb', line 26

def evaluate(variables = {})
  tree.eval(functions.merge(variables))
end

#functionsObject



30
31
32
33
34
# File 'lib/spot_feel/dmn/literal_expression.rb', line 30

def functions
  builtins = LiteralExpression.builtin_functions
  custom = (SpotFeel.config.functions || {})
  ActiveSupport::HashWithIndifferentAccess.new(builtins.merge(custom))
end

#named_functionsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/spot_feel/dmn/literal_expression.rb', line 36

def named_functions
  # Initialize a set to hold the qualified names
  function_names = Set.new

  # Define a lambda for the recursive function
  walk_tree = lambda do |node|
    # If the node is a qualified name, add it to the set
    if node.is_a?(SpotFeel::FunctionInvocation)
      function_names << node.fn_name.text_value
    end

    # Recursively walk the child nodes
    node.elements&.each do |child|
      walk_tree.call(child)
    end
  end

  # Start walking the tree from the root
  walk_tree.call(tree)

  # Return the array of functions
  function_names.to_a
end

#named_variablesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/spot_feel/dmn/literal_expression.rb', line 60

def named_variables
  # Initialize a set to hold the qualified names
  qualified_names = Set.new

  # Define a lambda for the recursive function
  walk_tree = lambda do |node|
    # If the node is a qualified name, add it to the set
    if node.is_a?(SpotFeel::QualifiedName)
      qualified_names << node.text_value
    end

    # Recursively walk the child nodes
    node.elements&.each do |child|
      walk_tree.call(child)
    end
  end

  # Start walking the tree from the root
  walk_tree.call(tree)

  # Return the array of qualified names
  qualified_names.to_a
end

#treeObject



17
18
19
# File 'lib/spot_feel/dmn/literal_expression.rb', line 17

def tree
  @tree ||= SpotFeel::Parser.parse(text)
end

#valid?Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/spot_feel/dmn/literal_expression.rb', line 21

def valid?
  return false if text.blank?
  tree.present?
end