Class: Sbuilder::Model

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

Constant Summary collapse

PROGNAME =

mixer

"Model"
@@validInterfaceExtensionProperties =
["matcher", "implementation"]

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sbuilder/model.rb', line 27

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 = {}
  

end

Instance Attribute Details

#assumptionsObject (readonly)

Hash:Array

properties ‘name’, ‘desc’



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

def assumptions
  @assumptions
end

#definitionsObject (readonly)

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



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

def definitions
  @definitions
end

#domainsObject (readonly)

hash name => Domain



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

def domains
  @domains
end

#factoryObject (readonly)

to create domain



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

def factory
  @factory
end

#interfaceExtensionsObject (readonly)

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



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

def interfaceExtensions
  @interfaceExtensions
end

#interfacesObject (readonly)

hash of interfaffces { paramSet.get -> paramSet }



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

def interfaces
  @interfaces
end

#invariantsObject (readonly)

Hash:Array

properties ‘name’, ‘desc’



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

def invariants
  @invariants
end

#possibilitiesObject (readonly)

String:Array

possibilities in setup



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

def possibilities
  @possibilities
end

#possibilityObject (readonly)

String

of setup possibility - may be nil



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

def possibility
  @possibility
end

#setupDefObject (readonly)

Hash

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



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

def setupDef
  @setupDef
end

#stepsObject (readonly)

array of paramSetSteps



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

def steps
  @steps
end

Instance Method Details

#addAssumption(assumption) ⇒ Object



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

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

#addInvariant(invariant) ⇒ Object


invariants + assumptions



52
53
54
# File 'lib/sbuilder/model.rb', line 52

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

#defineSetup(setupDef) ⇒ Object

setter for setup configuration, called for each setup



76
77
78
79
# File 'lib/sbuilder/model.rb', line 76

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

#domainEncountered(domainName) ⇒ Object

called when loading mappers for each domain encountered, return domain



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

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

#extendDomain(domain) ⇒ Object

called from a separte plase



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/sbuilder/model.rb', line 113

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 not among resolved domains.

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

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

#extendInterface(interfaceExtensionDef) ⇒ Object

extend



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/sbuilder/model.rb', line 180

def extendInterface( interfaceExtensionDef )

  # validate properties 'interfaceExtensionDef'
  begin
    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'
  putImplementationExtension( interfaceParamSet, interfaceExtensionDef )

  # return the extended interface
  interfaceParamSet

end

#extendStep(stepExtension) ⇒ Object


step stuff



162
163
164
# File 'lib/sbuilder/model.rb', line 162

def extendStep( stepExtension )
  @steps << stepExtension
end

#getDomain(domainName) ⇒ Object


domain stuff



99
100
101
102
103
# File 'lib/sbuilder/model.rb', line 99

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

#getImplementationExtension(interfaceParamSet) ⇒ Object

provide information on extension implementation during generation phase



229
230
231
# File 'lib/sbuilder/model.rb', line 229

def getImplementationExtension( interfaceParamSet )
   interfaceExtensions[interfaceParamSet.getId] ? interfaceExtensions[interfaceParamSet.getId]['implementation'] : nil
end

#getInterface(matcher) ⇒ Object

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



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/sbuilder/model.rb', line 202

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’



154
155
156
157
158
# File 'lib/sbuilder/model.rb', line 154

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

#interfaceIdsObject

array of known interface ids



234
235
236
# File 'lib/sbuilder/model.rb', line 234

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

#modelDefinition(paramSet) ⇒ Object


definition stuff



241
242
243
244
245
246
247
248
249
250
# File 'lib/sbuilder/model.rb', line 241

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



171
172
173
174
175
176
# File 'lib/sbuilder/model.rb', line 171

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

#paramSetsObject


collection of all param sets: intefaces + definitions



149
150
151
# File 'lib/sbuilder/model.rb', line 149

def paramSets
  interfaces.values + definitions.values
end

#putImplementationExtension(interfaceParamSet, interfaceExtensionDef) ⇒ Object

record extension in ‘@interfaceExtensions’



224
225
226
# File 'lib/sbuilder/model.rb', line 224

def putImplementationExtension( interfaceParamSet, interfaceExtensionDef )
  @interfaceExtensions[interfaceParamSet.getId] = interfaceExtensionDef
end

#setPossibilities(possis) ⇒ Object



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

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

#setPossibility(possi) ⇒ Object


possibility stuff



63
64
65
# File 'lib/sbuilder/model.rb', line 63

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



87
88
89
90
91
92
93
# File 'lib/sbuilder/model.rb', line 87

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) ⇒ Object

return hash for mustache for ‘templateName’ in ‘interfaces’, ‘domains’, ‘variables’



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

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 = {
      "possibility"    => lambda { hash['possibility'].to_yaml },                    
      "possibility"    => possibility, # possibility defined in setup, currently being generated
    }
  when 'possibilities'        
    hash = {
      "possibilities"  => 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,
          :bindSets  => step.bindExact ?  step.inputs.map.with_index { |input,i| input[:_comma] = i<step.inputs.length-1 ? ',' : ''; input }  : false, 
        }
      },
    }
  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 },          
      "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,
           :_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_value => setup_nil_values,
          :domain_values => domain.domain_values,
        }
      end,                                      # => TLA domain
    }
  else
    raise "Unknown template '#{domainName}'"
  end # case
  
end