Class: Sbuilder::Model

Inherits:
Object
  • Object
show all
Includes:
Utils::MyLogger
Defined in:
lib/sbuilder/model.rb

Constant Summary collapse

PROGNAME =

mixer

nil

Constants included from Utils::MyLogger

Utils::MyLogger::LOGFILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::MyLogger

#getLogger, #logfile

Constructor Details

#initialize(factory, options = {}) ⇒ Model


constructore



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sbuilder/model.rb', line 31

def initialize( factory, options = {} )
  @logger = getLogger( PROGNAME, options )
  @logger.info( "#{__method__} initialized" )
  @factory = factory

  # model content
  # @paramSets = []                       
  @domains = {}
  # create a hash capable for topological sorting
  @definitions = Sbuilder::ParamSets.create( self )
  @interfaces = {}
  @steps = []
  @possibility = nil
  @setupDef = nil
  @possibilities = []
  @invariants = []
  @assumptions = []      
  @interfaceExtensions = {}
  @symbolTable = nil
  @applicationDomainValues = []

end

Instance Attribute Details

#applicationDomainValuesObject (readonly)

@əttr [domainDef:Array] applicationDomainValues to pass to ‘dispatchExtendDomain’



19
20
21
# File 'lib/sbuilder/model.rb', line 19

def applicationDomainValues
  @applicationDomainValues
end

#assumptionsObject (readonly)

Hash:Array

properties ‘name’, ‘desc’



16
17
18
# File 'lib/sbuilder/model.rb', line 16

def assumptions
  @assumptions
end

#definitionsObject (readonly)

hash : { paramSet.getId -> paramSet } of swagger definitions



10
11
12
# File 'lib/sbuilder/model.rb', line 10

def definitions
  @definitions
end

#domainsObject (readonly)

hash name => Domain



6
7
8
# File 'lib/sbuilder/model.rb', line 6

def domains
  @domains
end

#factoryObject (readonly)

to create domain



7
8
9
# File 'lib/sbuilder/model.rb', line 7

def factory
  @factory
end

#interfaceExtensionsObject (readonly)

hash : { paramSet.getId -> { matcher -> …, implementation -> .. } }



8
9
10
# File 'lib/sbuilder/model.rb', line 8

def interfaceExtensions
  @interfaceExtensions
end

#interfacesObject (readonly)

hash of interfaffces { paramSet.get -> paramSet }



9
10
11
# File 'lib/sbuilder/model.rb', line 9

def interfaces
  @interfaces
end

#invariantsObject (readonly)

Hash:Array

properties ‘name’, ‘desc’



14
15
16
# File 'lib/sbuilder/model.rb', line 14

def invariants
  @invariants
end

#possibilitiesObject (readonly)

String:Array

possibilities in setup



13
14
15
# File 'lib/sbuilder/model.rb', line 13

def possibilities
  @possibilities
end

#possibilityObject (readonly)

String

of setup possibility - may be nil



12
13
14
# File 'lib/sbuilder/model.rb', line 12

def possibility
  @possibility
end

#setupDefObject (readonly)

Hash

properties ‘setupDirectory’, ‘desc’, ‘extensions’



15
16
17
# File 'lib/sbuilder/model.rb', line 15

def setupDef
  @setupDef
end

#stepsObject (readonly)

array of paramSetSteps



11
12
13
# File 'lib/sbuilder/model.rb', line 11

def steps
  @steps
end

Instance Method Details

#addApplicationDomainValue(domainDef) ⇒ Object

Add application model domain value definition to an array. These are later (during setup phase) used to extend domains.

Parameters:

  • domainDef (Hash)

    to pass to dispatchExtendDomain

See Also:

  • dispatchExtendDomain


135
136
137
# File 'lib/sbuilder/model.rb', line 135

def addApplicationDomainValue( domainDef )
  @applicationDomainValues << domainDef
end

#addAssumption(assumption) ⇒ Object



61
62
63
# File 'lib/sbuilder/model.rb', line 61

def addAssumption( assumption )
  @assumptions.push( assumption )
end

#addInvariant(invariant) ⇒ Object


invariants + assumptions



57
58
59
# File 'lib/sbuilder/model.rb', line 57

def addInvariant( invariant )
  @invariants.push( invariant )
end

#defineSetup(setupDef) ⇒ Object

setter for setup configuration, called for each setup



81
82
83
84
# File 'lib/sbuilder/model.rb', line 81

def defineSetup( setupDef )
  @logger.info "#{__method__} setupDef=#{setupDef}"
  @setupDef = setupDef
end

#domainEncountered(domainName) ⇒ Object

called when loading mappers for each domain encountered, return domain



122
123
124
125
# File 'lib/sbuilder/model.rb', line 122

def domainEncountered( domainName )
  @logger.info( "#{__method__} domainName #{domainName}" )
  return createOrUpdateDomain( domainName, nil )
end

#extendDomain(domain) ⇒ Object

called from a separte plase



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/sbuilder/model.rb', line 140

def extendDomain( domain )
  @logger.info( "#{__method__} domain=#{domain}" )
  raise ModelException.new <<-EOS  unless domains[domain.name]
  Unknown domain.name=#{domain.name}

  Trying to extend domain '#{domain.name}', which is not known.

  Known domain names are: #{domains.keys.join(', ' )}

  EOS
  domains[domain.name].setExtension( domain )
  # return createOrUpdateDomain( domain.name, domain.cardinality  )      
end

#extendInterface(interfaceExtensionDef) ⇒ Object

extend

Parameters:

  • interfaceExtensionDef (Hash)
  • [String] (Hash)

    a customizable set of options

  • [Sting] (Hash)

    a customizable set of options



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/sbuilder/model.rb', line 210

def extendInterface( interfaceExtensionDef )

  # validate properties 'interfaceExtensionDef'
  begin
    Utils::Validate.validateProperties( interfaceExtensionDef, Constants::VALIDATION[:interface_extension_def][:required], Constants::VALIDATION[:interface_extension_def][:allowed] )
    # validateProperties( interfaceExtensionDef, @@validInterfaceExtensionProperties )
  rescue ModelException => me
    @logger.info( "#{__method__} model exception #{me}" )
    raise ExtensionException.new( me )
  end
  # locate interface being extended
  interfaceParamSet = getInterface( interfaceExtensionDef['matcher'] )

  # record extension in '@interfaceExtensions'
  mergeImplementationExtension( interfaceParamSet, interfaceExtensionDef )

  # return the extended interface
  interfaceParamSet

end

#extendStep(stepExtension) ⇒ Object


step stuff



189
190
191
# File 'lib/sbuilder/model.rb', line 189

def extendStep( stepExtension )
  @steps << stepExtension
end

#getCompletionExtension(interfaceParamSet) ⇒ Object

extension to complete interface service implementation, accessed during generation phase



267
268
269
# File 'lib/sbuilder/model.rb', line 267

def getCompletionExtension( interfaceParamSet )
  doGetInterfaceExtension( interfaceParamSet, 'completion' )      
end

#getDomain(domainName) ⇒ Object


domain stuff



115
116
117
118
119
# File 'lib/sbuilder/model.rb', line 115

def getDomain( domainName )
  ret = domains[domainName]
  raise ModelException.new  "Could not locate domain set #{domainName}, known domains #{domains.keys.join(",")}" unless ret
  return ret
end

#getImplementationExtension(interfaceParamSet) ⇒ Object

extension interface service implementation, accessed during generation phase



262
263
264
# File 'lib/sbuilder/model.rb', line 262

def getImplementationExtension( interfaceParamSet )
  doGetInterfaceExtension( interfaceParamSet, 'implementation' )
end

#getInterface(matcher) ⇒ Object

locate ‘interface’ using ‘matcher’ (string or regexp),raise exception if not found



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/sbuilder/model.rb', line 232

def getInterface( matcher )
  
  @logger.debug( "#{__method__} matcher=#{matcher}" )
  
  interfaces.values.each do |interfaceParamSet|
    if interfaceParamSet.matchesWithParamSet(  matcher ) then
      return interfaceParamSet
    end
  end

  # error w. instructios of valid intefaces - should not reach this point
  raise ExtensionException.new <<-EOS

    Could not find interface matching '#{matcher}'
  
    Known interfaces: #{interfaceIds.join(',')}

  EOS
  
end

#getParamset(paramSetName) ⇒ Object

resolve ‘paramSetName’



181
182
183
184
185
# File 'lib/sbuilder/model.rb', line 181

def getParamset( paramSetName )
  ret = paramSets.select { |p| p.getId == paramSetName }.first
  raise ModelException.new  "Could not locate param set #{paramSetName}" unless ret
  ret
end

#getSymbolTableSbuilder::SymbolTable

Returns symbolTable for the model.

Returns:



106
107
108
109
# File 'lib/sbuilder/model.rb', line 106

def getSymbolTable
  # init only once
  @symbolTable = @symbolTable || factory.createSymbolTable
end

#interfaceIdsObject

array of known interface ids



279
280
281
# File 'lib/sbuilder/model.rb', line 279

def interfaceIds
  interfaces.values.map { |interface| interface.getId }
end

#mergeImplementationExtension(interfaceParamSet, interfaceExtensionDef) ⇒ Object

record extension in ‘@interfaceExtensions’ (merge with previos props)

Parameters:

  • interfaceExtensionDef (Hash)

Options Hash (interfaceExtensionDef):



257
258
259
# File 'lib/sbuilder/model.rb', line 257

def mergeImplementationExtension( interfaceParamSet, interfaceExtensionDef )
  @interfaceExtensions[interfaceParamSet.getId] = ( @interfaceExtensions[interfaceParamSet.getId] || {} ).merge interfaceExtensionDef
end

#modelDefinition(paramSet) ⇒ Object


definition stuff



286
287
288
289
290
291
292
293
294
295
# File 'lib/sbuilder/model.rb', line 286

def modelDefinition( paramSet )
  @logger.info( "#{__method__} paramSet=#{paramSet}, paramSet.class=#{paramSet.class}" )

  # collect definitions
  @definitions[paramSet.getId] = paramSet

  # # collect all paramsets
  # addParamSet( paramSet )
  
end

#modelInterface(paramSet) ⇒ Object

accept parameters set to model



198
199
200
201
202
203
# File 'lib/sbuilder/model.rb', line 198

def modelInterface( paramSet )
  @interfaces[paramSet.getId] = paramSet
  
  # # collect all paramsets
  # addParamSet( paramSet )
end

#paramSetsObject


collection of all param sets: intefaces + definitions



176
177
178
# File 'lib/sbuilder/model.rb', line 176

def paramSets
  interfaces.values + definitions.values
end

#setPossibilities(possis) ⇒ Object



72
73
74
75
# File 'lib/sbuilder/model.rb', line 72

def setPossibilities( possis )
  @possibilities = possis || []
  @logger.info( "#{__method__} @possibilities=#{@possibilities}" )
end

#setPossibility(possi) ⇒ Object


possibility stuff



68
69
70
# File 'lib/sbuilder/model.rb', line 68

def setPossibility( possi )
  @possibility = possi
end

#setup_nil_valuesBoolean

In order to prevent error ‘Attempted to construct a set with too many elements’ we allow setup to define cardinalities without nil. When default cardinality is one and this results to fixed domain ranges.

Returns:

  • (Boolean)

    true/false depeding whether setup should output nil_values



92
93
94
95
96
97
98
# File 'lib/sbuilder/model.rb', line 92

def setup_nil_values
  ret = setupDef && setupDef.key?('configuration') && setupDef['configuration'].key?('allow_domain_nil_values')  ?
          setupDef['configuration']['allow_domain_nil_values'] :
          true 
                                                                                  
  return ret
end

#templateData(domainName) ⇒ Hash

function to map data array to symbol table definitions

element in model for domainName

Parameters:

  • domainName (String)

    in data model

  • dataModelElement (Hash)

    a customizable set of options

Returns:

  • (Hash)

    dataModelElement for mustache for ‘templateName’ in ‘interfaces’, ‘domains’, ‘variables’ etc



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
# File 'lib/sbuilder/model.rb', line 333

def templateData( domainName )

  case domainName
  when 'interfaces'
    # see domainName 'interfaces'        
    templateDataInterfaces( domainName, false )
  # when 'interface_types'
  #   # see domainName 'interfaces', interface_types are not
  #   # filtered i.e. generated model defines data types for all interfaces
  #   templateDataInterfaces( 'interfaces', false )
  when 'infrastructureServices'
    templateDataInterfaces( domainName, true )
  when 'possibility'        
    hash = {
      "dump"           => lambda { hash['possibility'].to_yaml },                    
      "possibility"    => possibility, # possibility defined in setup, currently being generated
    }
  when 'possibilities'        
    hash = {
      "dump"           => lambda { hash['possibilities'].to_yaml },                    
      "possibilities"  => possibilities,      # possibilities in setup, all in current setup
    }
  when 'variables'        
    hash = {
      "dump"           => lambda { hash['variables'].to_yaml },                    
      "variables"      => [], # paramSets,                                       # => TLA state variable
    }
  when 'steps'        
    hash = {
      "dump"        => lambda { hash['steps'].to_yaml },                    
      "steps"       => steps.map.with_index { |step,i|
        {
          :process => step.getName,
          :interface_name => step.interfaceReference.getName,
          :interface_operation => step.interfaceReference.getId,
          :interface_path => step.interfaceReference.path,
          # :interface_id => step.interfaceReference.getId,              
          '_comma' => (i< steps.length-1 ? "," : ""),                                         
          :bindRule => step.bindExact == false ? step.inputs : false,
          :bindRules => step.bindExact == false ?
                          # for bindRules i.e. when bindSets== false:
                          # mustache  iterates set with  these elements
                          step.inputs.map.with_index do |input,index|
                            {
                              :process => step.getName,
                              :interface_name => step.interfaceReference.getName,
                              :interface_operation => step.interfaceReference.getId,
                              :interface_path => step.interfaceReference.path,
                              :bindRule => input,
                              "_comma" => (index<step.inputs.length-1 ? "," : ""),
                            }
                          end :
                          false,
          # :bindSets  => step.bindExact ?  step.inputs.map.with_index { |input,i| input['_comma'] = i<step.inputs.length-1 ? ',' : ''; input }  : false,
          # for 'bindSets' bindRules == false mustache step is 
          # one entry with set for 'bindSets' elements
          :bindSets  => step.bindExact ?  step.inputs : false,
          :noBinds => step.inputs.length == 0,
        }
      },
    }
  when 'none'
    {
    }
  when 'invariants'
    hash = {
      "dump"        => lambda { hash['invariants'].to_yaml },          
      "invariants" => invariants,
    }
  when 'assumptions'
    hash = {
      "dump"        => lambda { hash['assumptions'].to_yaml },          
      "assumptions" => assumptions,
    }
    
  when 'definitions'
    hash = {
      "dump"        => lambda { hash['definitions'].to_yaml },
      # "symbols" => ->( d ) { [
      #                          Sbuilder::Constants::META_MODEL_DEFINITION,
      #                          d[:definition_name],
      #                          d[:definition_name]
      #                        ] },
      'definitions' => definitions.tsort.map.with_index { |parameterName,i|
        # tsort returns array of names, resolve paramSet
        p = getParamset( parameterName )
        {  :definition_name => p.getName,
           :isArray => p.isArray,
           :domainParameters => p.isFunction ? p.parameter_definitions( self, p.domainParameters ) : [],
           :rangeParameters => p.isFunction ?  p.parameter_definitions( self, p.rangeParameters ) : [],                
           :isFunction => p.isFunction,
           '_comma' => (i< definitions.values.length-1 ? "," : ""),
           :parameter_definitions => p.parameter_definitions( self )
        }
      }
    }                                                                       # => TLA definitions
  when 'domains'
    hash = {
      "dump"        => lambda { hash['domains'].to_yaml },
      "domains"     => domains.values.map do |domain|
        {
          :domain_name => domain.domain_name,
          # Nil included: if global configuration does not prevent && domain uses nil
          :nil_value => setup_nil_values && domain.includeNil,
          :tlaBaseType =>  domain.tlaBaseType,                    
          :domain_values => domain.domain_values,
        }
      end,                                      # => TLA domain
    }
  when 'metatypes'
    hash = {
      "dump"        => lambda { hash['metatypes'].to_yaml },
      "metatypes"   => getSymbolTable.metatypes
    }
    
  else
    raise "Unknown template '#{domainName}'"
  end # case
  
end