Class: Sbuilder::Ial::Action::TlaRules

Inherits:
Object
  • Object
show all
Defined in:
lib/action/tla_rules.rb

Class Method Summary collapse

Class Method Details

.rules(gen = nil) ⇒ Object

Parameters:

  • gen (ElementGenarator) (defaults to: nil)

    to create expressions



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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/action/tla_rules.rb', line 24

def self.rules( gen=nil )
  {
    # 
    :start => {
      :navigations => [
        { :rule =>  :domains_defines },
        # { :rule =>  :states_defines },               
      ],
      :translator => ->( ctx ) {
        { :domains => ctx[:navigationResults][:domains_defines].flatten,
          # :states => ctx[:navigationResults][:states_defines].flatten,
        } },
    },
    :test => {
      :navigations => [  { :rule =>  :domains_defines } ],
      :translator => ->( ctx ) { { :domains => ctx[:navigationResults][:domains_defines]}},
    },

    # transaction_interfaces: for API loader extension point
    :transaction_interfaces => {
      :debug => false,
      :navigations => [ { :navigate => :transaction, :rule => :transactions_interface_iterate } ],
      :translator => ->( ctx ) do 
        (ctx[:navigationResults][:transactions_interface_iterate]).flatten
      end,
    },
    :transactions_interface_iterate => {
      :debug => false,
      :navigations => [ { :iterate => true, :rule => :transaction_interface } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:transaction_interface] },
    },
    :transaction_interface => {
      :translator => ->( ctx ) do
        tx = ctx[:transaction_interface]
        appName = tx.name
        action = tx.operation
        {
          :path => appName,
          :operation => action,
          :request => tx.request,
          :response => tx.response,
        }
      end
    },

    # snippets produced only once & first
    :library_snippets => {
      :debug => false,              
      :navigations => [ { :navigate => :library, :rule => :library_snippet_iterate } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:library_snippet_iterate].flatten },              
    },
    :library_snippet_iterate => {
      :debug => false,
      :navigations => [ { :iterate => true, :rule => :library_snippet } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:library_snippet] },
    },
    :library_snippet => {
      :debug => false,
      :navigations => [ { :navigate => :modules, :rule => :library_module } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:library_module] },
    },
    :library_module => {
      :debug => false,            
      :navigations => [ { :rule => ->(node) do
                            nodeType = node[:type]
                            case nodeType
                            when :variable
                              [:lib_type, :variable_define ]                                      
                            else
                              raise "Unknwon node type #{nodeType} for #{node}"
                            end
                          end
                        } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:lib_type].flatten(1) }
    },

    # macros_snippets: macro definitions
    :macros_snippets => {
      :debug => false,
      :navigations => [ { :navigate => :macro, :rule => :macros_iterate } ],
      :translator => ->( ctx ) do 
        (ctx[:navigationResults][:macros_iterate]).flatten
      end,
    },
    :macros_iterate => {
      :debug => false,
      :navigations => [ { :iterate => true, :rule => :macro_entry } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:macro_entry] },
    },
    :macro_entry => {
      :navigations => [
        { :navigate => :block, :rule => :block_statements },
      ],
      :translator => ->( ctx ) do
        macroName = ctx[:macro_entry].name
        params = ctx[:macro_entry].parameters.map(&:name)
        stmts = ctx[:navigationResults][:block_statements].flatten
        gen.p_macro( macroName, params, stmts )
      end
    },

    # operators
    :operators_snippets => {
      :debug => false,
      :navigations => [ { :navigate => :operator, :rule => :operators_iterate } ],
      :translator => ->( ctx ) do 
        (ctx[:navigationResults][:operators_iterate]).flatten
      end,
    },
    :operators_iterate => {
      :debug => false,
      :navigations => [ { :iterate => true, :rule => :operator_entry } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:operator_entry] },
    },
    :operator_entry => {
      :navigations => [ { :navigate => :body, :rule => :rval } ],
      :translator => ->( ctx ) do
        operatorName = ctx[:operator_entry].name
        params = ctx[:operator_entry].parameters.map(&:name)
        body = ctx[:navigationResults][:rval].flatten(1)
        gen.p_operator( operatorName, params, body )
      end
    },
    

    # transaction_entry_snippets: macro called 
    :transaction_entry_snippets => {
      :debug => false,
      :navigations => [ { :navigate => :transaction, :rule => :transactions_entry_iterate } ],
      :translator => ->( ctx ) do 
        (ctx[:navigationResults][:transactions_entry_iterate]).flatten
      end,
    },
    :transactions_entry_iterate => {
      :debug => false,
      :navigations => [ { :iterate => true, :rule => :transaction_entry } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:transaction_entry] },
    },
    :transaction_entry => {
      :debug => false,
      :navigations => [
        { :navigate => :entryBlock, :rule => :block_statements },
      ],
      :translator => ->( ctx ) do
        txName = ctx[:transaction_entry].name
        operationName = ctx[:transaction_entry].operation
        stmts = ctx[:navigationResults][:block_statements].flatten
        gen.p_tx_entry( txName, operationName, stmts )
      end
    },

    # produce function for transaction implementation 
    :transaction_function_snippets => {
      :debug => false,
      :navigations => [ { :navigate => :transaction, :rule => :transactions_function_iterate } ],
      :translator => ->( ctx ) do 
        (ctx[:navigationResults][:transactions_function_iterate]).flatten
      end,
    },
    :transactions_function_iterate => {
      :debug => false,
      :navigations => [ { :iterate => true, :rule => :transaction_function } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:transaction_function] },
    },
    :transaction_function => {
      :debug => false,
      :navigations => [
        { :navigate => :block, :rule => :block_statements },
      ],
      :translator => ->( ctx ) do
        txName = ctx[:transaction_function].name
        operationName = ctx[:transaction_function].operation
        stmts = ctx[:navigationResults][:block_statements].flatten
        # Just array of strings
        locals = ctx[:transaction_function].locals.map { |l| l.is_a?(Sbuilder::Ial::Model::Local) ? l.name : l }
        gen.p_tx_function( txName, operationName, locals, stmts  )
      end
    },

    # produce block of statmentes 'gen_stmts( s(gen_skip))'
    :block_statements => {
      :debug => false,
      :navigations => [ { :navigate => :statements, :rule => :block_statement } ],
      :translator => ->( ctx ) do
        stmts = ctx[:navigationResults][:block_statement]
        if stmts && stmts.any?
          gen.gen_stmts( stmts.reduce(&:concat) )
        else
          []
        end
      end,
    },
    :block_statement => {
      :debug => false,
      :navigations => [ { :rule => ->(node) do
                            nodeType = node[:type]
                            case nodeType
                            when :printStatement
                              [:stmt_type, :print_statement ]                                      
                            when :skipStatement
                              [:stmt_type, :skip_statement ]
                            when :callStatement
                              [:stmt_type, :call_statement ]
                            when :conditionalStatement
                              [:stmt_type, :conditional_statement ]
                            when :assignStatement
                              [:stmt_type, :assign_statement ]
                            else
                              raise "Unknwon node type '#{nodeType}' for #{node}"
                            end
                          end
                        } ],
      # :translator => ->( ctx ) { ctx[:navigationResults][:stmt_type].inject( s() ) {|m,stmts| m += stmts; m} }
      :translator => ->( ctx ) { ctx[:navigationResults][:stmt_type].flatten(1) }
    },

    # various statement types s( s(), s(), ... )
    :skip_statement => {
      :translator => ->( ctx ) { gen.p_skipStatement } 
    },
    :conditional_statement => {
      :debug => false,
      :navigations => [
        { :navigate => :condition, :rule => :rval },
        { :navigate => :ifBlock, :rule => ->(node) { [ :ifBlock, :block_statements ]} },
        { :navigate => :elseBlock, :rule => ->(node) {
            if node.elseBlock == false
              # no else block to traverse
              [ :elseBlock, false ]
            else
              [ :elseBlock, :block_statements ]
            end
          }
        },
        ],              
      :translator => ->( ctx ) do
        condition = ctx[:navigationResults][:rval].flatten(1)
        ifBlock =  ctx[:navigationResults][:ifBlock].flatten(1)
        elseBlock =  ctx[:navigationResults][:elseBlock].flatten(1)                
        gen.p_conditionalStatement( condition, ifBlock, elseBlock )
      end
    },
    :call_statement => {
      :debug => false,
      :navigations => [ { :navigate => :parameters, :rule => :rval } ],
      :translator => ->( ctx ) {
        targetType = ctx[:call_statement][:targetType]
        parameters = ctx[:navigationResults][:rval]
        case targetType
        when :macro
          target = ctx[:call_statement][:target]
          gen.p_callMacroStatement( target, parameters )
        when :service
          txName = ctx[:call_statement][:target]
          operation = ctx[:call_statement][:operation]
          gen.p_callServiceStatement( txName, operation, parameters )       
        else
          raise "Unknwon  target type '#{targetType}' in call-statements #{ctx[:call_statement]}"
        end
      } 
    },
    :print_statement => {
      :navigations => [ { :navigate => :expression, :rule => :rval } ],
      :translator => ->( ctx ) do
        node = ctx[:print_statement]
        printExpr = ctx[:navigationResults][:rval]
        gen.p_printStatement(  printExpr  )
      end
    },
    :assign_statement => {
      :debug => false,
      :navigations => [
        { :navigate => [:variable, :ref ], :rule => :rval_reference },                
        { :navigate => :expression, :rule => :rval },
      ],
      :translator => ->( ctx ) do
        assignTo = ctx[:navigationResults][:rval_reference].flatten(1)
        rval = ctx[:navigationResults][:rval].flatten(1)                
        gen.p_assignTo(  assignTo, rval )
      end
    },
    

    # expression rval
    :rval => {
      :debug => false,
      :navigations => [ { :rule => ->(node) do
                            nodeType = node[:type]
                            case nodeType
                            when :constantExpression
                              [:rval, :rval_constant ]                                      
                            when :referenceExpression
                              [:rval, :rval_reference ]                                      
                            when :binaryExpression
                              [:rval, :rval_binary_expression ]                                      
                            when :tlaExpression
                              [:rval, :rval_tla_expression ]                                      
                            when :operatorExpression
                              [:rval, :rval_operator_expr ]                                      
                            when :variable
                              [:rval, :rval_variable ]                                      
                            else
                              raise "Unknwon node type '#{nodeType}' for #{node}"
                            end
                          end
                        } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:rval].flatten(1) }
    },
    :lval => {
      # used in binary operator where lval and rval are both
      # expressions, just forward to :rval,
      :debug => false,
      :navigations => [ { :rule => :rval } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:rval].flatten(1) },
    },
    :rval_binary_expression =>  {
      :navigations => [ { :navigate => :lval, :rule => :lval }, { :navigate => :rval, :rule => :rval } ],
      :translator => ->( ctx ) {
        lval = ctx[:navigationResults][:lval].flatten(1)
        rval = ctx[:navigationResults][:rval].flatten(1)
        gen.p_binary_expression( lval, ctx[:rval_binary_expression].op, rval ) }
    },
    :rval_constant =>  {
      # rval_constant is ConstantExpression
      :translator => ->( ctx ) { gen.p_constant_expression( ctx[:rval_constant].const ) }
    },
    :rval_reference =>  {
      :navigations => [{ :rule => ->(node) do
                          refType = node.refType
                          case refType
                            when :domain
                              [:ref, :rval_domain ]                                      
                            when :variable
                              [:ref, :rval_variable ]                                      
                            when :parameter
                              [:ref, :rval_parameter ]                                      
                            when :local
                              [:ref, :rval_local ]                                      
                            when :plain
                              [:ref, :rval_local ]                                      
                            else
                              raise "Unknwon refType type '#{refType}' for #{node}"
                            end
                          end
                      }],
      :translator => ->( ctx ) { ctx[:navigationResults][:ref].flatten(1) }
    },
    :rval_variable =>  {
      :translator => ->( ctx ) { gen.p_variable_expression( ctx[:rval_variable].ref ) }
    },
    :rval_operator_expr =>  {
      :navigations => [ { :navigate => :args, :rule => :rval } ],
      :translator => ->( ctx ) do
        operatorName = ctx[:rval_operator_expr].called
        args = ctx[:navigationResults][:rval]
        if ctx[:rval_operator_expr].infrastructureService
          gen.p_infra_operator_expression( operatorName, args )                  
        else
          gen.p_operator_expression( operatorName, args )
        end
      end
    },
    :rval_parameter =>  {
      :translator => ->( ctx ) {
        # Reference to one of :transaction_function, :macro_entry
        if ctx[:transaction_function] 
          txName = ctx[:transaction_function].name
          operationName = ctx[:transaction_function].operation
          parameterName = ctx[:rval_parameter].ref
          gen.p_tx_parameter_expression( txName, operationName, parameterName )
        elsif ctx[:macro_entry]
          macroName = ctx[:macro_entry].name
          parameterName = ctx[:rval_parameter].ref
          gen.p_macro_parameter_expression( macroName, parameterName )
        elsif ctx[:operator_entry]
          operatorName = ctx[:operator_entry].name
          parameterName = ctx[:rval_parameter].ref
          gen.p_operator_parameter_expression( operatorName, parameterName )
        else
          raise "Expect to be called within :transaction_function, :operator_entry, or :macro_entry"
        end
      }
    },
    :rval_domain =>  {
      :translator => ->( ctx ) { gen.p_domain_expression( ctx[:rval_domain].ref ) }
    },
    :rval_local =>  {
      :translator => ->( ctx ) { gen.p_local_expression( ctx[:rval_local].ref ) }
    },
    :rval_tla_expression =>  {
      :debug => false,
      :navigations => [ { :navigate => :args, :rule => :rval } ],
      :translator => ->( ctx ) {
        # args is array of parameters p1,p2, ... [p1,p2,...],
        # using splat * to pass these parameters to to gen-methods
        args = ctx[:navigationResults][:rval]
        gen.p_template_expression( ctx[:rval_tla_expression].templateEval, args) }
    },
    

    # transaction_exit_snippets
    :base_snippets => {
      :debug => false,
      :translator => ->( ctx ) do
        gen.p_base_snippets( )
      end,
    },

    # transaction_exit_snippets
    :transaction_exit_snippets => {
      :debug => false,
      :navigations => [ { :navigate => :transaction, :rule => :transactions_exit_iterate } ],
      :translator => ->( ctx ) do 
        (ctx[:navigationResults][:transactions_exit_iterate]).flatten
      end,
    },
    :transactions_exit_iterate => {
      :debug => false,
      :navigations => [ { :iterate => true, :rule => :transaction_exit } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:transaction_exit] },
    },
    :transaction_exit => {
      :translator => ->( ctx ) do
        txName = ctx[:transaction_exit].name
        operationName = ctx[:transaction_exit].operation
        gen.p_tx_exit( txName, operationName  )
      end
    },

    # Create snippets for variable definitions
    :variables_snippets => {
      :navigations => [ { :navigate => :variable, :rule => :variable_iterate } ],
      :translator => ->( ctx ) do 
        ctx[:navigationResults][:variable_iterate].flatten
      end,
    },
    :variable_iterate => {
      :debug => false,
      :navigations => [ { :iterate => true, :rule => :variable_define } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:variable_define] }              
    },
    :variable_define => {
      :debug => false,
      :navigations => [ { :navigate => :init, :rule => :rval } ],
      :translator => ->( ctx ) do
        varName = ctx[:variable_define].name
        init = ctx[:navigationResults][:rval].flatten
        gen.p_variable_definition( varName, init )
      end
    },

    :api_definitions => {
      :debug => false,              
      :navigations => [ { :navigate => :definition, :rule => :api_definitions_iterate } ],
      :translator => ->( ctx ) do
        ctx[:navigationResults][:api_definitions_iterate].flatten(1)
      end
    },
    :api_definitions_iterate => {
      :debug => false,
      :navigations => [ { :iterate => true, :rule => :api_definition } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:api_definition] },
    },
    :api_definition => {
      :translator => ->( ctx ) { { :name => ctx[:api_definition].name, :properties => ctx[:api_definition].properties || [] } }
    },
    

    #  { :name, :values }:Array
    :domains_defines => {
      :navigations => [ { :navigate => :domain, :rule => :domain_iterate } ],
      :translator => ->( ctx ) do
        ctx[:navigationResults][:domain_iterate].flatten
      end
    },
    :domain_iterate => {
      :navigations => [ { :iterate => true, :rule => :domain_define } ],
      :translator => ->( ctx ) { ctx[:navigationResults][:domain_define] }              
    },
    :domain_define => {
      :translator => ->( ctx ) { { :name => ctx[:domain_define].name, :values => nil } }
    },
  }
end