sbuilder-ial 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,32 @@
1
+ module Sbuilder
2
+ module Ial
3
+ module Action
4
+
5
+ class ScriptEval
6
+
7
+ # @param [String] filename script to evaluate
8
+ #
9
+ # @param [Object] applicationConfiguration made available for
10
+ # the Ruby script to evaluate
11
+ #
12
+ # @return [Result|Error]
13
+ def self.script_eval( filename, applicationConfiguration=nil )
14
+ begin
15
+ # applicationConfiguration can be accessed in 'filename' in variable 'configuration'
16
+ configuration = applicationConfiguration
17
+ proc = Proc.new {
18
+ }
19
+ ret = eval(File.read(filename), proc.binding, filename)
20
+ Result( ret )
21
+ rescue Errno::ENOENT => err
22
+ Error( err )
23
+ rescue SyntaxError => err
24
+ Error( err )
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,17 @@
1
+ module Sbuilder
2
+ module Ial
3
+ module Action
4
+
5
+
6
+ class Text
7
+
8
+ def self.from_string( str )
9
+ new( str )
10
+ end
11
+
12
+ include Sbuilder::Fp::Compositable
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,512 @@
1
+ module Sbuilder
2
+ module Ial
3
+ module Action
4
+ class TlaRules
5
+
6
+ #
7
+ # Rules is a hash, map with [ ruleId --> rule ].
8
+ #
9
+ # ruleId : Symbol
10
+ #
11
+ # rule : Hash with keys
12
+ # - :navigations : Navigation|Navigation:Array
13
+ # - :translator : lamda function ->(ctx) { }
14
+ #
15
+ # ctx is a hash map with properties
16
+ # - :navigationResults: TranslateResult
17
+ # - <rule> -symbol (of current translate step, and all steps abover
18
+ #
19
+ # Navigation: Hash with keys
20
+ # - :rule: ruleId (mandatory)
21
+ # - :navigate: Symbol which must exist in curent node
22
+
23
+ # @param [ElementGenarator] gen to create expressions
24
+ def self.rules( gen=nil )
25
+ {
26
+ #
27
+ :start => {
28
+ :navigations => [
29
+ { :rule => :domains_defines },
30
+ # { :rule => :states_defines },
31
+ ],
32
+ :translator => ->( ctx ) {
33
+ { :domains => ctx[:navigationResults][:domains_defines].flatten,
34
+ # :states => ctx[:navigationResults][:states_defines].flatten,
35
+ } },
36
+ },
37
+ :test => {
38
+ :navigations => [ { :rule => :domains_defines } ],
39
+ :translator => ->( ctx ) { { :domains => ctx[:navigationResults][:domains_defines]}},
40
+ },
41
+
42
+ # transaction_interfaces: for API loader extension point
43
+ :transaction_interfaces => {
44
+ :debug => false,
45
+ :navigations => [ { :navigate => :transaction, :rule => :transactions_interface_iterate } ],
46
+ :translator => ->( ctx ) do
47
+ (ctx[:navigationResults][:transactions_interface_iterate]).flatten
48
+ end,
49
+ },
50
+ :transactions_interface_iterate => {
51
+ :debug => false,
52
+ :navigations => [ { :iterate => true, :rule => :transaction_interface } ],
53
+ :translator => ->( ctx ) { ctx[:navigationResults][:transaction_interface] },
54
+ },
55
+ :transaction_interface => {
56
+ :translator => ->( ctx ) do
57
+ tx = ctx[:transaction_interface]
58
+ appName = tx.name
59
+ action = tx.operation
60
+ {
61
+ :path => appName,
62
+ :operation => action,
63
+ :request => tx.request,
64
+ :response => tx.response,
65
+ }
66
+ end
67
+ },
68
+
69
+ # snippets produced only once & first
70
+ :library_snippets => {
71
+ :debug => false,
72
+ :navigations => [ { :navigate => :library, :rule => :library_snippet_iterate } ],
73
+ :translator => ->( ctx ) { ctx[:navigationResults][:library_snippet_iterate].flatten },
74
+ },
75
+ :library_snippet_iterate => {
76
+ :debug => false,
77
+ :navigations => [ { :iterate => true, :rule => :library_snippet } ],
78
+ :translator => ->( ctx ) { ctx[:navigationResults][:library_snippet] },
79
+ },
80
+ :library_snippet => {
81
+ :debug => false,
82
+ :navigations => [ { :navigate => :modules, :rule => :library_module } ],
83
+ :translator => ->( ctx ) { ctx[:navigationResults][:library_module] },
84
+ },
85
+ :library_module => {
86
+ :debug => false,
87
+ :navigations => [ { :rule => ->(node) do
88
+ nodeType = node[:type]
89
+ case nodeType
90
+ when :variable
91
+ [:lib_type, :variable_define ]
92
+ else
93
+ raise "Unknwon node type #{nodeType} for #{node}"
94
+ end
95
+ end
96
+ } ],
97
+ :translator => ->( ctx ) { ctx[:navigationResults][:lib_type].flatten(1) }
98
+ },
99
+
100
+ # macros_snippets: macro definitions
101
+ :macros_snippets => {
102
+ :debug => false,
103
+ :navigations => [ { :navigate => :macro, :rule => :macros_iterate } ],
104
+ :translator => ->( ctx ) do
105
+ (ctx[:navigationResults][:macros_iterate]).flatten
106
+ end,
107
+ },
108
+ :macros_iterate => {
109
+ :debug => false,
110
+ :navigations => [ { :iterate => true, :rule => :macro_entry } ],
111
+ :translator => ->( ctx ) { ctx[:navigationResults][:macro_entry] },
112
+ },
113
+ :macro_entry => {
114
+ :navigations => [
115
+ { :navigate => :block, :rule => :block_statements },
116
+ ],
117
+ :translator => ->( ctx ) do
118
+ macroName = ctx[:macro_entry].name
119
+ params = ctx[:macro_entry].parameters.map(&:name)
120
+ stmts = ctx[:navigationResults][:block_statements].flatten
121
+ gen.p_macro( macroName, params, stmts )
122
+ end
123
+ },
124
+
125
+ # operators
126
+ :operators_snippets => {
127
+ :debug => false,
128
+ :navigations => [ { :navigate => :operator, :rule => :operators_iterate } ],
129
+ :translator => ->( ctx ) do
130
+ (ctx[:navigationResults][:operators_iterate]).flatten
131
+ end,
132
+ },
133
+ :operators_iterate => {
134
+ :debug => false,
135
+ :navigations => [ { :iterate => true, :rule => :operator_entry } ],
136
+ :translator => ->( ctx ) { ctx[:navigationResults][:operator_entry] },
137
+ },
138
+ :operator_entry => {
139
+ :navigations => [ { :navigate => :body, :rule => :rval } ],
140
+ :translator => ->( ctx ) do
141
+ operatorName = ctx[:operator_entry].name
142
+ params = ctx[:operator_entry].parameters.map(&:name)
143
+ body = ctx[:navigationResults][:rval].flatten(1)
144
+ gen.p_operator( operatorName, params, body )
145
+ end
146
+ },
147
+
148
+
149
+ # transaction_entry_snippets: macro called
150
+ :transaction_entry_snippets => {
151
+ :debug => false,
152
+ :navigations => [ { :navigate => :transaction, :rule => :transactions_entry_iterate } ],
153
+ :translator => ->( ctx ) do
154
+ (ctx[:navigationResults][:transactions_entry_iterate]).flatten
155
+ end,
156
+ },
157
+ :transactions_entry_iterate => {
158
+ :debug => false,
159
+ :navigations => [ { :iterate => true, :rule => :transaction_entry } ],
160
+ :translator => ->( ctx ) { ctx[:navigationResults][:transaction_entry] },
161
+ },
162
+ :transaction_entry => {
163
+ :debug => false,
164
+ :navigations => [
165
+ { :navigate => :entryBlock, :rule => :block_statements },
166
+ ],
167
+ :translator => ->( ctx ) do
168
+ txName = ctx[:transaction_entry].name
169
+ operationName = ctx[:transaction_entry].operation
170
+ stmts = ctx[:navigationResults][:block_statements].flatten
171
+ gen.p_tx_entry( txName, operationName, stmts )
172
+ end
173
+ },
174
+
175
+ # produce function for transaction implementation
176
+ :transaction_function_snippets => {
177
+ :debug => false,
178
+ :navigations => [ { :navigate => :transaction, :rule => :transactions_function_iterate } ],
179
+ :translator => ->( ctx ) do
180
+ (ctx[:navigationResults][:transactions_function_iterate]).flatten
181
+ end,
182
+ },
183
+ :transactions_function_iterate => {
184
+ :debug => false,
185
+ :navigations => [ { :iterate => true, :rule => :transaction_function } ],
186
+ :translator => ->( ctx ) { ctx[:navigationResults][:transaction_function] },
187
+ },
188
+ :transaction_function => {
189
+ :debug => false,
190
+ :navigations => [
191
+ { :navigate => :block, :rule => :block_statements },
192
+ ],
193
+ :translator => ->( ctx ) do
194
+ txName = ctx[:transaction_function].name
195
+ operationName = ctx[:transaction_function].operation
196
+ stmts = ctx[:navigationResults][:block_statements].flatten
197
+ # Just array of strings
198
+ locals = ctx[:transaction_function].locals.map { |l| l.is_a?(Sbuilder::Ial::Model::Local) ? l.name : l }
199
+ gen.p_tx_function( txName, operationName, locals, stmts )
200
+ end
201
+ },
202
+
203
+ # produce block of statmentes 'gen_stmts( s(gen_skip))'
204
+ :block_statements => {
205
+ :debug => false,
206
+ :navigations => [ { :navigate => :statements, :rule => :block_statement } ],
207
+ :translator => ->( ctx ) do
208
+ stmts = ctx[:navigationResults][:block_statement]
209
+ if stmts && stmts.any?
210
+ gen.gen_stmts( stmts.reduce(&:concat) )
211
+ else
212
+ []
213
+ end
214
+ end,
215
+ },
216
+ :block_statement => {
217
+ :debug => false,
218
+ :navigations => [ { :rule => ->(node) do
219
+ nodeType = node[:type]
220
+ case nodeType
221
+ when :printStatement
222
+ [:stmt_type, :print_statement ]
223
+ when :skipStatement
224
+ [:stmt_type, :skip_statement ]
225
+ when :callStatement
226
+ [:stmt_type, :call_statement ]
227
+ when :conditionalStatement
228
+ [:stmt_type, :conditional_statement ]
229
+ when :assignStatement
230
+ [:stmt_type, :assign_statement ]
231
+ else
232
+ raise "Unknwon node type '#{nodeType}' for #{node}"
233
+ end
234
+ end
235
+ } ],
236
+ # :translator => ->( ctx ) { ctx[:navigationResults][:stmt_type].inject( s() ) {|m,stmts| m += stmts; m} }
237
+ :translator => ->( ctx ) { ctx[:navigationResults][:stmt_type].flatten(1) }
238
+ },
239
+
240
+ # various statement types s( s(), s(), ... )
241
+ :skip_statement => {
242
+ :translator => ->( ctx ) { gen.p_skipStatement }
243
+ },
244
+ :conditional_statement => {
245
+ :debug => false,
246
+ :navigations => [
247
+ { :navigate => :condition, :rule => :rval },
248
+ { :navigate => :ifBlock, :rule => ->(node) { [ :ifBlock, :block_statements ]} },
249
+ { :navigate => :elseBlock, :rule => ->(node) {
250
+ if node.elseBlock == false
251
+ # no else block to traverse
252
+ [ :elseBlock, false ]
253
+ else
254
+ [ :elseBlock, :block_statements ]
255
+ end
256
+ }
257
+ },
258
+ ],
259
+ :translator => ->( ctx ) do
260
+ condition = ctx[:navigationResults][:rval].flatten(1)
261
+ ifBlock = ctx[:navigationResults][:ifBlock].flatten(1)
262
+ elseBlock = ctx[:navigationResults][:elseBlock].flatten(1)
263
+ gen.p_conditionalStatement( condition, ifBlock, elseBlock )
264
+ end
265
+ },
266
+ :call_statement => {
267
+ :debug => false,
268
+ :navigations => [ { :navigate => :parameters, :rule => :rval } ],
269
+ :translator => ->( ctx ) {
270
+ targetType = ctx[:call_statement][:targetType]
271
+ parameters = ctx[:navigationResults][:rval]
272
+ case targetType
273
+ when :macro
274
+ target = ctx[:call_statement][:target]
275
+ gen.p_callMacroStatement( target, parameters )
276
+ when :service
277
+ txName = ctx[:call_statement][:target]
278
+ operation = ctx[:call_statement][:operation]
279
+ gen.p_callServiceStatement( txName, operation, parameters )
280
+ else
281
+ raise "Unknwon target type '#{targetType}' in call-statements #{ctx[:call_statement]}"
282
+ end
283
+ }
284
+ },
285
+ :print_statement => {
286
+ :navigations => [ { :navigate => :expression, :rule => :rval } ],
287
+ :translator => ->( ctx ) do
288
+ node = ctx[:print_statement]
289
+ printExpr = ctx[:navigationResults][:rval]
290
+ gen.p_printStatement( printExpr )
291
+ end
292
+ },
293
+ :assign_statement => {
294
+ :debug => false,
295
+ :navigations => [
296
+ { :navigate => [:variable, :ref ], :rule => :rval_reference },
297
+ { :navigate => :expression, :rule => :rval },
298
+ ],
299
+ :translator => ->( ctx ) do
300
+ assignTo = ctx[:navigationResults][:rval_reference].flatten(1)
301
+ rval = ctx[:navigationResults][:rval].flatten(1)
302
+ gen.p_assignTo( assignTo, rval )
303
+ end
304
+ },
305
+
306
+
307
+ # expression rval
308
+ :rval => {
309
+ :debug => false,
310
+ :navigations => [ { :rule => ->(node) do
311
+ nodeType = node[:type]
312
+ case nodeType
313
+ when :constantExpression
314
+ [:rval, :rval_constant ]
315
+ when :referenceExpression
316
+ [:rval, :rval_reference ]
317
+ when :binaryExpression
318
+ [:rval, :rval_binary_expression ]
319
+ when :tlaExpression
320
+ [:rval, :rval_tla_expression ]
321
+ when :operatorExpression
322
+ [:rval, :rval_operator_expr ]
323
+ when :variable
324
+ [:rval, :rval_variable ]
325
+ else
326
+ raise "Unknwon node type '#{nodeType}' for #{node}"
327
+ end
328
+ end
329
+ } ],
330
+ :translator => ->( ctx ) { ctx[:navigationResults][:rval].flatten(1) }
331
+ },
332
+ :lval => {
333
+ # used in binary operator where lval and rval are both
334
+ # expressions, just forward to :rval,
335
+ :debug => false,
336
+ :navigations => [ { :rule => :rval } ],
337
+ :translator => ->( ctx ) { ctx[:navigationResults][:rval].flatten(1) },
338
+ },
339
+ :rval_binary_expression => {
340
+ :navigations => [ { :navigate => :lval, :rule => :lval }, { :navigate => :rval, :rule => :rval } ],
341
+ :translator => ->( ctx ) {
342
+ lval = ctx[:navigationResults][:lval].flatten(1)
343
+ rval = ctx[:navigationResults][:rval].flatten(1)
344
+ gen.p_binary_expression( lval, ctx[:rval_binary_expression].op, rval ) }
345
+ },
346
+ :rval_constant => {
347
+ # rval_constant is ConstantExpression
348
+ :translator => ->( ctx ) { gen.p_constant_expression( ctx[:rval_constant].const ) }
349
+ },
350
+ :rval_reference => {
351
+ :navigations => [{ :rule => ->(node) do
352
+ refType = node.refType
353
+ case refType
354
+ when :domain
355
+ [:ref, :rval_domain ]
356
+ when :variable
357
+ [:ref, :rval_variable ]
358
+ when :parameter
359
+ [:ref, :rval_parameter ]
360
+ when :local
361
+ [:ref, :rval_local ]
362
+ when :plain
363
+ [:ref, :rval_local ]
364
+ else
365
+ raise "Unknwon refType type '#{refType}' for #{node}"
366
+ end
367
+ end
368
+ }],
369
+ :translator => ->( ctx ) { ctx[:navigationResults][:ref].flatten(1) }
370
+ },
371
+ :rval_variable => {
372
+ :translator => ->( ctx ) { gen.p_variable_expression( ctx[:rval_variable].ref ) }
373
+ },
374
+ :rval_operator_expr => {
375
+ :navigations => [ { :navigate => :args, :rule => :rval } ],
376
+ :translator => ->( ctx ) do
377
+ operatorName = ctx[:rval_operator_expr].called
378
+ args = ctx[:navigationResults][:rval]
379
+ if ctx[:rval_operator_expr].infrastructureService
380
+ gen.p_infra_operator_expression( operatorName, args )
381
+ else
382
+ gen.p_operator_expression( operatorName, args )
383
+ end
384
+ end
385
+ },
386
+ :rval_parameter => {
387
+ :translator => ->( ctx ) {
388
+ # Reference to one of :transaction_function, :macro_entry
389
+ if ctx[:transaction_function]
390
+ txName = ctx[:transaction_function].name
391
+ operationName = ctx[:transaction_function].operation
392
+ parameterName = ctx[:rval_parameter].ref
393
+ gen.p_tx_parameter_expression( txName, operationName, parameterName )
394
+ elsif ctx[:macro_entry]
395
+ macroName = ctx[:macro_entry].name
396
+ parameterName = ctx[:rval_parameter].ref
397
+ gen.p_macro_parameter_expression( macroName, parameterName )
398
+ elsif ctx[:operator_entry]
399
+ operatorName = ctx[:operator_entry].name
400
+ parameterName = ctx[:rval_parameter].ref
401
+ gen.p_operator_parameter_expression( operatorName, parameterName )
402
+ else
403
+ raise "Expect to be called within :transaction_function, :operator_entry, or :macro_entry"
404
+ end
405
+ }
406
+ },
407
+ :rval_domain => {
408
+ :translator => ->( ctx ) { gen.p_domain_expression( ctx[:rval_domain].ref ) }
409
+ },
410
+ :rval_local => {
411
+ :translator => ->( ctx ) { gen.p_local_expression( ctx[:rval_local].ref ) }
412
+ },
413
+ :rval_tla_expression => {
414
+ :debug => false,
415
+ :navigations => [ { :navigate => :args, :rule => :rval } ],
416
+ :translator => ->( ctx ) {
417
+ # args is array of parameters p1,p2, ... [p1,p2,...],
418
+ # using splat * to pass these parameters to to gen-methods
419
+ args = ctx[:navigationResults][:rval]
420
+ gen.p_template_expression( ctx[:rval_tla_expression].templateEval, args) }
421
+ },
422
+
423
+
424
+ # transaction_exit_snippets
425
+ :base_snippets => {
426
+ :debug => false,
427
+ :translator => ->( ctx ) do
428
+ gen.p_base_snippets( )
429
+ end,
430
+ },
431
+
432
+ # transaction_exit_snippets
433
+ :transaction_exit_snippets => {
434
+ :debug => false,
435
+ :navigations => [ { :navigate => :transaction, :rule => :transactions_exit_iterate } ],
436
+ :translator => ->( ctx ) do
437
+ (ctx[:navigationResults][:transactions_exit_iterate]).flatten
438
+ end,
439
+ },
440
+ :transactions_exit_iterate => {
441
+ :debug => false,
442
+ :navigations => [ { :iterate => true, :rule => :transaction_exit } ],
443
+ :translator => ->( ctx ) { ctx[:navigationResults][:transaction_exit] },
444
+ },
445
+ :transaction_exit => {
446
+ :translator => ->( ctx ) do
447
+ txName = ctx[:transaction_exit].name
448
+ operationName = ctx[:transaction_exit].operation
449
+ gen.p_tx_exit( txName, operationName )
450
+ end
451
+ },
452
+
453
+ # Create snippets for variable definitions
454
+ :variables_snippets => {
455
+ :navigations => [ { :navigate => :variable, :rule => :variable_iterate } ],
456
+ :translator => ->( ctx ) do
457
+ ctx[:navigationResults][:variable_iterate].flatten
458
+ end,
459
+ },
460
+ :variable_iterate => {
461
+ :debug => false,
462
+ :navigations => [ { :iterate => true, :rule => :variable_define } ],
463
+ :translator => ->( ctx ) { ctx[:navigationResults][:variable_define] }
464
+ },
465
+ :variable_define => {
466
+ :debug => false,
467
+ :navigations => [ { :navigate => :init, :rule => :rval } ],
468
+ :translator => ->( ctx ) do
469
+ varName = ctx[:variable_define].name
470
+ init = ctx[:navigationResults][:rval].flatten
471
+ gen.p_variable_definition( varName, init )
472
+ end
473
+ },
474
+
475
+ :api_definitions => {
476
+ :debug => false,
477
+ :navigations => [ { :navigate => :definition, :rule => :api_definitions_iterate } ],
478
+ :translator => ->( ctx ) do
479
+ ctx[:navigationResults][:api_definitions_iterate].flatten(1)
480
+ end
481
+ },
482
+ :api_definitions_iterate => {
483
+ :debug => false,
484
+ :navigations => [ { :iterate => true, :rule => :api_definition } ],
485
+ :translator => ->( ctx ) { ctx[:navigationResults][:api_definition] },
486
+ },
487
+ :api_definition => {
488
+ :translator => ->( ctx ) { { :name => ctx[:api_definition].name, :properties => ctx[:api_definition].properties || [] } }
489
+ },
490
+
491
+
492
+ # { :name, :values }:Array
493
+ :domains_defines => {
494
+ :navigations => [ { :navigate => :domain, :rule => :domain_iterate } ],
495
+ :translator => ->( ctx ) do
496
+ ctx[:navigationResults][:domain_iterate].flatten
497
+ end
498
+ },
499
+ :domain_iterate => {
500
+ :navigations => [ { :iterate => true, :rule => :domain_define } ],
501
+ :translator => ->( ctx ) { ctx[:navigationResults][:domain_define] }
502
+ },
503
+ :domain_define => {
504
+ :translator => ->( ctx ) { { :name => ctx[:domain_define].name, :values => nil } }
505
+ },
506
+ }
507
+ end
508
+
509
+ end
510
+ end
511
+ end
512
+ end