sbuilder-al 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ module Sbuilder
2
+ module Al
3
+ module Model
4
+ class Constants
5
+
6
+ # ------------------------------------------------------------------
7
+ # @!group Expression values
8
+
9
+
10
+ CONSTANT_EXPRESSION_NIL = :nil
11
+ CONSTANT_EXPRESSION_EMPTY = :empty
12
+
13
+ CONSTANT_EXPRESSIONS = {
14
+ CONSTANT_EXPRESSION_EMPTY => '{}',
15
+ CONSTANT_EXPRESSION_NIL => 'Nil',
16
+ }
17
+
18
+
19
+ # @!endgroup
20
+
21
+ end # class Constants
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,534 @@
1
+ module Sbuilder
2
+ module Al
3
+
4
+ ##
5
+ # Application model POJO classes
6
+ module Model
7
+
8
+ # not catched
9
+ class ModelException < Exception; end
10
+ # to be catched
11
+ class ModelError < Exception; end
12
+
13
+ ##
14
+ # Top level class in application object {Sbuilder::Al::Model}
15
+ # class hierarchy.
16
+ #
17
+ # Implementation set instance variable
18
+ # {Sbuilder::Al::Model::RootObject#type} in initialize to identify
19
+ # application object class. Operator
20
+ # {Sbuilder::Al::Model::RootObject#[]} walk up class hierarchy, when
21
+ # looking up for a property.
22
+ #
23
+ # Implementation includes {Sbuilder::Al::Meta} module to add
24
+ # primitives such as 'descendants' instance method, a
25
+ # attribute_reader, attribute_write, and attribute_accessor.
26
+ #
27
+ #
28
+ #
29
+
30
+ class RootObject
31
+ # Include meta services available also for
32
+ # sub.classes. Example of a meta svc 'attribute_reader'
33
+ include Sbuilder::Al::Meta
34
+
35
+ # @!attribute type [Symbol]
36
+ # model element type (=class name)
37
+ # All objects under RootObject -hierarchy define type,
38
+ # defined using 'attr_reader', which does not show up
39
+ # meta-documentation
40
+ attr_reader :type
41
+
42
+
43
+ # @param type [Symbol] model element type (=class name)
44
+ def initialize( type: nil )
45
+ @type = type
46
+ end
47
+
48
+ # Lookup instance attribute in object, or in its parent.
49
+ #
50
+ # Implementation:
51
+ # 1) checks if object defines attr_reader :k
52
+ # 2) delegates to parent ( unless is RootObject of hierarch )
53
+ # 3) thrown an error 'No property found'
54
+ #
55
+ # @param k [Symbol] name of attribute to lookup for
56
+ #
57
+ # @return [RootObject] attribute value 'k' found in self or in
58
+ # ancestor classes, ModelError if attribute 'k'
59
+ # unknown
60
+ #
61
+ # @raise [ModelError] "No property found" - for unknown property
62
+ #
63
+ def [](k)
64
+ # I know how to resolve :k my self
65
+ return self.public_send( k ) if self.respond_to?( k )
66
+ # I delegate [] to my parent - if applicable
67
+ return self.superclass.[](k) unless self..instance_of?( RootObject )
68
+ # Not found
69
+ raise ModelError, "No property found '#{k}' in #{self.to_s}"
70
+ end
71
+
72
+ # Dump AL object with indentation indented with level +indent+
73
+ #
74
+ def strOutput( level=999, indent=0, start="" )
75
+ return "" if level <= 0
76
+ attributeLines = self.class.metaAncestors.map do |kl|
77
+ kl.attributes.map do |attr|
78
+
79
+ if self[attr].is_a?( Array)
80
+ if !self[attr].empty?
81
+ arrPrefix = self[attr][0].respond_to?(:strOutput) ? ( level > 1 ? "\n" : "" ) : ""
82
+ arrSep = self[attr][0].respond_to?(:strOutput) ? "" : ""
83
+ arrPostix = self[attr][0].respond_to?(:strOutput) ? ( level > 1 ? "\n" : "" ) : ","
84
+ end
85
+
86
+ end
87
+
88
+ " " *( (indent +1 )*4) +
89
+ # name of attribute
90
+ attr.to_s + "=" +
91
+ ( self[attr].respond_to?(:strOutput) ?
92
+ # recursion would not output anything
93
+ ( level > 1 ? "\n" : "" ) +
94
+ + self[attr].strOutput( level-1,indent + 2) :
95
+ ( self[attr].is_a?( Array) ?
96
+ (
97
+ # empty array printed []
98
+ self[attr].empty? ? [].to_s :
99
+ # array with some content
100
+ # arrPrefix + self[attr].map{ |a| a.respond_to?(:strOutput) ? a.strOutput( level-1,indent + 2) : (" " *( (indent +1 )*4) + a.to_s) }.join(arrPostix)
101
+ arrPrefix + self[attr].map do |a|
102
+ a.respond_to?(:strOutput) ?
103
+ (a.strOutput( level-1,indent + 2, "\n" ) + arrSep ) :
104
+ (" " + a.to_s )
105
+ end.join(arrPostix)
106
+ ) :
107
+ # simple ruby attribute
108
+ self[attr].to_s
109
+ ))
110
+ end
111
+ end.flatten
112
+
113
+ str =
114
+ <<~HERE
115
+ #{start}#{" " *( (indent +0 )*4)}#{self.class.to_s} do
116
+ #{attributeLines.join("\n" ) }
117
+ #{" " *( (indent +0 )*4)}end
118
+ HERE
119
+ # remove last \n
120
+ str.rstrip
121
+ end
122
+ end
123
+
124
+
125
+ ##
126
+ # Domain definition
127
+ #
128
+ class Domain < RootObject
129
+ attribute_accessor :name
130
+ attribute_accessor :values
131
+ alias_method :appName, :name
132
+ def initialize( type: nil, name: nil, values: nil )
133
+ self.name = name
134
+ self.values = values || []
135
+ super( type: type )
136
+ end
137
+ end
138
+
139
+ ## Storage types
140
+ class Storage < RootObject
141
+ attribute_accessor :name
142
+ attribute_accessor :meta
143
+ attribute_accessor :init
144
+ def initialize( type=nil, name=nil, meta=nil, init=nil )
145
+ self.name = name
146
+ self.meta = meta
147
+ self.init = init
148
+ super( type: type )
149
+ end
150
+ end
151
+ class Variable < Storage
152
+ alias_method :appName, :name
153
+ end
154
+ class Parameter < Storage
155
+ end
156
+ class Local < Storage
157
+ def initialize( type=nil, name=nil, meta=nil )
158
+ super( type=type, name=name, meta=meta )
159
+ end
160
+ end
161
+
162
+ ## Statements
163
+
164
+ class Block < RootObject
165
+ attribute_accessor :statements
166
+ def initialize( type: nil, statements: [] )
167
+ self.statements = statements || []
168
+ super( type: type )
169
+ end
170
+ end
171
+
172
+ class Statement < RootObject
173
+ end
174
+ class SkipStatement < Statement
175
+ attribute_accessor :labeled
176
+ def initialize( type: nil, labeled: nil )
177
+ self.labeled = labeled.nil? ? false : labeled
178
+ super( type: type )
179
+ end
180
+ end
181
+
182
+ # Return statement types 1) :returnValue (variable && expression
183
+ # non nil) 2) :returnStatement (status + expression non nil)
184
+ #
185
+ class ReturnStatement < Statement
186
+ attribute_accessor :status
187
+ attribute_accessor :expression
188
+ attribute_accessor :variable
189
+ def initialize( type: nil, status: nil, expression: nil, variable: nil )
190
+ self.status = status
191
+ self.expression = expression
192
+ self.variable = variable
193
+ super( type: type )
194
+ end
195
+
196
+ end
197
+ class JumpStatement < Statement
198
+ attribute_accessor :label
199
+ def initialize( type: nil, label: nil )
200
+ self.label = label
201
+ super( type: type )
202
+ end
203
+ end
204
+
205
+ class ConditionalStatement < Statement
206
+ attribute_accessor :condition
207
+ attribute_accessor :ifBlock
208
+ attribute_accessor :elseBlock
209
+
210
+ # used in build block
211
+ def elseThen( &blk )
212
+ self.elseBlock = Sbuilder::Al::Model::Api::block( &blk )
213
+ end
214
+
215
+ def initialize( type: nil, condition: nil, ifBlock: nil, elseBlock: nil )
216
+ self.condition = condition
217
+ self.ifBlock = ifBlock
218
+ self.elseBlock = elseBlock || false
219
+ super( type: type )
220
+ end
221
+ end
222
+ class CallStatement < Statement
223
+
224
+ attribute_accessor :meta # of 'called'
225
+ attribute_accessor :name # method bening called
226
+ attribute_accessor :operation # for callType :service
227
+ attribute_accessor :callType # constant :service
228
+ attribute_accessor :parameters
229
+ def parameter( v )
230
+ self.parameters += v.is_a?( Array ) ? v : [v]
231
+ end
232
+
233
+ def initialize( type: nil, meta: nil, name:nil, callType: nil, parameters: nil, operation: nil )
234
+ self.callType = callType
235
+ self.name = name
236
+ self.meta = meta
237
+ self.operation = operation
238
+ self.parameters = parameters || []
239
+ super( type: type )
240
+ end
241
+ end
242
+ class OutputStatement < Statement
243
+ attribute_accessor :expression
244
+ def initialize( type: nil, expression: nil )
245
+ self.expression = expression
246
+ super( type: type )
247
+ end
248
+ end
249
+ class DataManipulationStatement < Statement
250
+ attribute_accessor :variable # of ReferenceExpresion
251
+ def initialize( type: nil, variable: nil )
252
+ self.variable = variable
253
+ super( type: type )
254
+ end
255
+ end
256
+ class AssignStatement < DataManipulationStatement
257
+ attribute_accessor :rval
258
+ def initialize( type: nil, variable: nil, rval: nil )
259
+ self.rval = rval
260
+ super( type: type, variable: variable )
261
+ end
262
+ end
263
+
264
+ ## Expression
265
+ class Expression < RootObject
266
+ def initialize( type: nil )
267
+ super( type: type )
268
+ end
269
+ end
270
+
271
+ class BinaryExpression < Expression
272
+ attribute_accessor :lval
273
+ attribute_accessor :op
274
+ attribute_accessor :rval
275
+ def initialize( type: nil, lval: nil, op: nil, rval: nil )
276
+ self.lval = lval
277
+ self.op = op
278
+ self.rval = rval
279
+ super( type: type )
280
+ end
281
+ end
282
+
283
+ class UnaryExpression < Expression
284
+ attribute_accessor :op
285
+ attribute_accessor :expr
286
+ def initialize( type: nil, expr: nil, op: nil )
287
+ self.expr = expr
288
+ self.op = op
289
+ super( type: type )
290
+ end
291
+ end
292
+
293
+
294
+ class ReferenceExpression < Expression
295
+ attribute_accessor :reference # of type Reference
296
+ def initialize( meta: nil, reference: nil )
297
+ self.reference = reference
298
+ super( type: :reference_expression )
299
+ end
300
+ end
301
+
302
+ class ParameterizedExpression < Expression
303
+ attribute_accessor :args
304
+ def initialize( type: nil, args: nil )
305
+ self.args = args || []
306
+ super( type: type )
307
+ end
308
+ end
309
+
310
+ class TlaExpression < ParameterizedExpression
311
+ # Lamba not documented
312
+ attr_accessor :templateEval
313
+ attribute_accessor :template
314
+ def initialize( type: nil, template: nil, templateEval: nil, args: nil )
315
+ self.template = template
316
+ self.templateEval = templateEval
317
+ super( type: type, args: args )
318
+ end
319
+ end
320
+
321
+ class ConstantExpression < Expression
322
+ attribute_accessor :value
323
+ def initialize( type: nil, value: nil )
324
+ self.value = value
325
+ super( type: type )
326
+ end
327
+
328
+ end
329
+
330
+ # References
331
+ class Reference < RootObject
332
+ attribute_accessor :name
333
+ attribute_accessor :fields
334
+ def initialize( type: nil, name: nil, fields: nil )
335
+ self.name = name
336
+ self.fields = fields
337
+ super( type: type )
338
+ end
339
+ end
340
+ class TxParameterReference < Reference
341
+ attribute_accessor :meta
342
+ attribute_accessor :operation
343
+ def initialize( type: nil, name: nil, meta: nil, operation: nil, fields: nil )
344
+ self.meta = meta
345
+ self.operation = operation
346
+ super( type: type, name: name, fields: fields )
347
+ end
348
+ end
349
+ class VariableReference < Reference
350
+ attribute_accessor :meta
351
+ def initialize( type: nil, name: nil, meta: nil, fields: nil )
352
+ self.meta = meta
353
+ super( type: type, name: name, fields: fields )
354
+ end
355
+ end
356
+
357
+ class SimpleReference < Reference
358
+ def initialize( type: nil, name: nil, fields: nil )
359
+ super( type: type, name: name, fields: fields )
360
+ end
361
+ end
362
+
363
+ # Callables
364
+ class Callable < RootObject
365
+ attribute_accessor :name
366
+ def initialize( type: nil, name: nil )
367
+ self.name = name
368
+ super( type: type )
369
+ end
370
+ end
371
+
372
+ # allow free text to entered into tla-code
373
+ class Snippet < Callable
374
+ alias_method :appName, :name
375
+ attribute_accessor :meta
376
+ attribute_accessor :txt
377
+ def initialize( type: nil, name: nil, meta: nil, txt: nil )
378
+ self.meta = meta
379
+ self.txt = txt
380
+ super( type: type, name: name )
381
+ end
382
+ end
383
+
384
+ class Alias < Callable
385
+ attribute_accessor :meta
386
+ alias_method :appName, :name
387
+ attribute_accessor :specName
388
+ def initialize( type: nil, name: nil, meta: nil, specName: nil )
389
+ self.meta = meta
390
+ self.specName = specName
391
+ super( type: type, name: name )
392
+ end
393
+ end
394
+
395
+ class ParameterizedCallable < Callable
396
+ attribute_accessor :parameters
397
+ def initialize( type: nil, name: nil, parameters: nil )
398
+ @parameters = parameters || []
399
+ super( type: type, name: name )
400
+ end
401
+ end
402
+ class FunctionTypeCallable < Callable
403
+ attribute_accessor :request
404
+ attribute_accessor :locals
405
+ attribute_accessor :response
406
+ attribute_accessor :block
407
+ def initialize( type: nil, name: nil, request: nil, response: nil, locals: nil, block: nil )
408
+ self.request = request
409
+ self.response = response
410
+ self.locals = locals || [] # empty array (of local variables)
411
+ self.block = block || [] # empty array (of statements)
412
+ super( type: type, name: name )
413
+ end
414
+ end
415
+ class Transaction < FunctionTypeCallable
416
+ # alias_method :appName, :name
417
+ def appName
418
+ "#{name}(#{operation})"
419
+ end
420
+ attribute_accessor :returnOption
421
+ attribute_accessor :interfaceOption
422
+ attribute_accessor :meta
423
+
424
+ # attribute_accessor :entryBlock
425
+ # attribute_accessor :exitBlock
426
+ attribute_accessor :operation
427
+
428
+ # Meta data not shown in documentation
429
+ attr_accessor :sourceModule
430
+ attr_accessor :sourceLine
431
+ attr_accessor :sourceColumn
432
+
433
+ def initialize( type: nil, meta: nil, name: nil, operation: nil,
434
+ request: nil, response: nil, locals: nil, block: nil, interfaceOption: false, returnOption: false, sourceModule: nil, sourceLine: nil, sourceColumn: nil )
435
+ self.meta = meta
436
+ self.operation = operation
437
+ self.sourceModule = sourceModule
438
+ self.sourceLine = sourceLine
439
+ self.sourceColumn = sourceColumn
440
+ self.returnOption = returnOption
441
+ self.interfaceOption = interfaceOption
442
+ super( type: type, name: name, request: request, response: response,
443
+ locals: locals, block: block )
444
+ end
445
+ end
446
+ # class Function < FunctionTypeCallable
447
+ # alias_method :appName, :name
448
+ # attribute_accessor :meta
449
+ # def initialize( type: nil, name: nil, meta: nil, request: nil, response: nil, locals: nil, block: nil )
450
+ # self.meta = meta
451
+ # super( type: type, name: name, request: request, response: response, locals: locals, block: block )
452
+ # end
453
+ # end
454
+ class Macro < ParameterizedCallable
455
+ alias_method :appName, :name
456
+ attribute_accessor :meta
457
+ attribute_accessor :block
458
+ def initialize( type: nil, meta: nil, name: nil, parameters: nil, block: nil )
459
+ self.block = block || [] # empty array (of statements)
460
+ self.meta = meta
461
+ super( type: type, name: name, parameters: parameters )
462
+ end
463
+ end
464
+ class Operator < ParameterizedCallable
465
+ alias_method :appName, :name
466
+ attribute_accessor :meta
467
+ attribute_accessor :rval
468
+ def initialize( type: nil, meta: nil, name: nil, parameters: nil, rval: nil )
469
+ self.rval = rval
470
+ self.meta = meta
471
+ super( type: type, name: name, parameters: parameters )
472
+ end
473
+ end
474
+
475
+ # Parameter sets
476
+ class Property < RootObject
477
+ attribute_accessor :name
478
+ attribute_accessor :isArray
479
+ alias_method :appName, :name
480
+ attribute_accessor :domain
481
+ attribute_accessor :definition
482
+ def initialize( type: nil, name: nil, domain: nil, definition: nil, isArray: false)
483
+ self.name = name
484
+ self.domain = domain
485
+ self.isArray = isArray.nil? ? false : isArray
486
+ self.definition = definition
487
+ super( type: type )
488
+ end
489
+ end
490
+
491
+ class ParameterSet < RootObject
492
+ attribute_accessor :properties
493
+ def initialize( type: nil, properties: nil )
494
+ self.properties = properties
495
+ super( type: type )
496
+ end
497
+ end
498
+
499
+ class ApiDefinition < ParameterSet
500
+ attribute_accessor :name
501
+ alias_method :appName, :name
502
+ def initialize( type: nil, name: nil, properties: nil )
503
+ self.name = name
504
+ super( type: type, properties: properties )
505
+ end
506
+ end
507
+
508
+ # Api Type Definition
509
+ class Definition < ApiDefinition
510
+ end
511
+
512
+ # Api Type Definition
513
+ class FunctionDefinition < ApiDefinition
514
+ end
515
+
516
+ class Request < ParameterSet
517
+ attribute_accessor :name
518
+ def initialize( type: nil, name: nil, properties: nil )
519
+ self.name = name
520
+ super( type: type, properties: properties )
521
+ end
522
+
523
+ end
524
+ class Response < ParameterSet
525
+ attribute_accessor :name
526
+ def initialize( type: nil, name: nil, properties: nil )
527
+ self.name = name
528
+ super( type: type, properties: properties )
529
+ end
530
+ end
531
+
532
+ end # module Model
533
+ end # module Al
534
+ end # module Sbuilder