Module: OutputConcreteBuilder

Defined in:
lib/builders_examples/ruby_builders/output_concrete_builder.rb,
lib/builders_examples/pdm_builders/PhaseI/output_concrete_builder.rb,
lib/builders_examples/pdm_builders/PhaseIWithPriorityQueues/output_concrete_builder.rb

Constant Summary collapse

OUTPUT_RUBY_FILE_NAME =
'ruby_network_topology.rb'
OUTPUT_PDM_FILE_NAME =
'topology.pdm'
OUTPUT_CPP_FILE_NAME =
'FlowDefinitions.cpp'
OUTPUT_SCILAB_FILE_NAME =
'flows_definition.scilabParams'
NUMBER_OF_PDM_MODELS_IN_STRUCTURE =
3
OUTPUT_FLOWS_SCILAB_FILE_NAME =
'flows_definition.scilabParams'
OUTPUT_ROUTERS_SCILAB_FILE_NAME =
'routers_definition.scilabParams'
'links_definition.scilabParams'
OUTPUT_HOSTS_SCILAB_FILE_NAME =
'hosts_definition.scilabParams'

Instance Method Summary collapse

Instance Method Details

#add_elements_of_type(graph_elements, type) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/builders_examples/ruby_builders/output_concrete_builder.rb', line 45

def add_elements_of_type(graph_elements, type)
    elements_to_add = graph_elements.select { |elem| elem.is_a? type }

    elements_def = ''

    elements_to_add.each do |element|
        elements_def += "      #{element.transform_to_output_representation @directory_concrete_builders} \n"
    end

    elements_def = replace_ids_host_router_for_index graph_elements, elements_def if type == Link

    elements_def += "\n"
    elements_def
end

#build_cplusplus_output(cplusplus_flows_defined) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/builders_examples/pdm_builders/PhaseI/output_concrete_builder.rb', line 75

def build_cplusplus_output(cplusplus_flows_defined)
    cplusplus_flows_definition = "#include \"FlowDefinitions.h\" \n"
    cplusplus_flows_definition += "\n"
    cplusplus_flows_definition += "std::multimap<std::string, std::shared_ptr<Flow>> FlowDefinitions::Flows; \n"
    cplusplus_flows_definition += "\n"
    cplusplus_flows_definition += "void FlowDefinitions::defineFlows(){ \n"
    cplusplus_flows_definition += "\n"
    cplusplus_flows_definition += cplusplus_flows_defined
    cplusplus_flows_definition += '}'
    cplusplus_flows_definition
end

#build_flow_outputObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/builders_examples/pdm_builders/PhaseI/output_concrete_builder.rb', line 55

def build_flow_output
    graph_elements = topology_provider.get_topology 

    flows = graph_elements.select { |elem| elem.is_a? Flow }        

    scilab_flows_definition = ''
    cplusplus_flows_defined = ''
    flows.each do |flow|
        flow_output = flow.transform_to_output_representation @directory_concrete_builders
        scilab_flows_definition += flow_output['scilab']
        cplusplus_flows_defined += flow_output['cplusplus']
    end

    write_file "#{@output_directory}/#{OUTPUT_SCILAB_FILE_NAME}",
                scilab_flows_definition

    write_file  "#{@output_directory}/#{OUTPUT_CPP_FILE_NAME}",
               (build_cplusplus_output cplusplus_flows_defined)
end


23
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
# File 'lib/builders_examples/pdm_builders/PhaseIWithPriorityQueues/output_concrete_builder.rb', line 23

def build_host_link_router_output
    graph_elements = topology_provider.get_topology
    pdm_topology = pmd_initial_struture 
    
    router_scilab_params = ''
    link_scilab_params = ''
    host_scilab_params = ''

    elements_which_are_not_flows = graph_elements.select{ |elem| [Host,Router,Link].include? elem.class }

    elements_which_are_not_flows.each do |element|
        output_representation = element.transform_to_output_representation @directory_concrete_builders
        pdm_topology += output_representation['pdm']
        router_scilab_params += output_representation['scilab'] if element.is_a? Router
        link_scilab_params += output_representation['scilab'] if element.is_a? Link
        host_scilab_params += output_representation['scilab'] if element.is_a? Host
    end
    
    pdm_topology += create_lines_between_graph_elements graph_elements

    pdm_topology += pdm_final_structure

    write_file "#{@output_directory}/#{OUTPUT_PDM_FILE_NAME}",
                pdm_topology

    write_file "#{@output_directory}/#{OUTPUT_ROUTERS_SCILAB_FILE_NAME}",
                router_scilab_params

    write_file "#{@output_directory}/#{OUTPUT_LINKS_SCILAB_FILE_NAME}",
                link_scilab_params

    write_file "#{@output_directory}/#{OUTPUT_HOSTS_SCILAB_FILE_NAME}",
                host_scilab_params
end

#build_output_contentObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/builders_examples/ruby_builders/output_concrete_builder.rb', line 10

def build_output_content
    graph_elements = topology_provider.get_topology
    
    graph_elements = graph_elements.select { |elem| [Host,Link,Router].include? elem.class }

    ruby_network_topology = "module NetworkTopology \n"

    get_topology_function_def ="    def get_topology \n "
    get_topology_function_def +="       return @topology.topology_elements if @topology.topology_elements.size != 0 \n"
    get_topology_function_def +="       hosts = [] \n "
    get_topology_function_def +="       routers = [] \n" 
    get_topology_function_def +="       links = [] \n" 
    get_topology_function_def +="\n"

    get_topology_function_def += add_elements_of_type graph_elements, Host
    get_topology_function_def += add_elements_of_type graph_elements, Router
    get_topology_function_def += add_elements_of_type graph_elements, Link

    get_topology_function_def +="      @topology.topology_elements \n "
    get_topology_function_def +="   end \n"

    get_path_definition_function_def = "    def get_path_between(source, destination) \n"
    get_path_definition_function_def += "        raise NotImplementedError, \"NetworkTopology: This method is not implemented (¿does it ever get called?)\" \n"
    get_path_definition_function_def += "   end \n"
                                            
    ruby_network_topology += get_topology_function_def
    ruby_network_topology += "\n"
    ruby_network_topology += get_path_definition_function_def
    ruby_network_topology += 'end'


    write_file "#{@output_directory}/#{OUTPUT_RUBY_FILE_NAME}",
                ruby_network_topology
end

#build_pdm_outputObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/builders_examples/pdm_builders/PhaseI/output_concrete_builder.rb', line 18

def build_pdm_output
    graph_elements = topology_provider.get_topology
    
    graph_elements = graph_elements.select { |elem| [Host,Link,Router].include? elem.class }

    pdm_topology = PDM_INITIAL_STRUCTURE 

    graph_elements.each do |element|
        pdm_topology += element.transform_to_output_representation @directory_concrete_builders
    end
    
    pdm_topology += create_lines_between_graph_elements graph_elements

    pdm_topology += PDM_FINAL_STRUCTURE

    write_file "#{@output_directory}/#{OUTPUT_PDM_FILE_NAME}",
                pdm_topology
end

#create_lines_between_graph_elements(graph_elements) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/builders_examples/pdm_builders/PhaseI/output_concrete_builder.rb', line 37

def create_lines_between_graph_elements(graph_elements)
    links = graph_elements.select { |node| node.is_a? Link }
    lines = ''
    links.each do |link|
        src_pdm_pos = get_pdm_position link.src_element
        dst_pdm_pos = get_pdm_position link.dst_element
        link_pdm_pos = get_pdm_position link
        lines += link.create_pdm_line_between_src_and_dst src_pdm_pos, dst_pdm_pos, link_pdm_pos
    end
    lines
end

#get_pdm_position(network_elem, graph_elements) ⇒ Object



49
50
51
52
53
# File 'lib/builders_examples/pdm_builders/PhaseI/output_concrete_builder.rb', line 49

def get_pdm_position(network_elem)
    #Here links have a modeled representation, so we have to count the link as well.
    #We have routers, hosts and links as either atomic or compunded models
    NUMBER_OF_PDM_MODELS_IN_STRUCTURE + network_elem.my_number
end

#initialize_concrete_builder(topology_provider, directory_concrete_builders, output_directory) ⇒ Object



4
5
6
7
8
# File 'lib/builders_examples/ruby_builders/output_concrete_builder.rb', line 4

def initialize_concrete_builder(topology_provider, directory_concrete_builders, output_directory)
    @topology_provider = topology_provider
    @directory_concrete_builders = directory_concrete_builders # make's sense?
    @output_directory = output_directory
end

#pdm_final_structureObject



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
# File 'lib/builders_examples/pdm_builders/PhaseIWithPriorityQueues/output_concrete_builder.rb', line 202

def pdm_final_structure
  "Atomic
                  {
                  Name = FinalizationCommands
                  Ports = 0 ; 0
                  Path = sinks/multipleSimulationCommands.h
                  Description = Executes Scilab commands when using multiple simulation runs (at the end of each simulation, and at the end of ALL simulations).\\nThis model should run with LAST priority
                  Graphic
                      {
                      Position = -6270 ; -14220
                      Dimension = 540 ; 540
                      Direction = Right
                      Color = 15
                      Icon = %datanetworks%scilab.bmp
                      }
                  Parameters
                      {
                      initSimulationCommandName = Str; ../examples/PhaseI_topologies/lar_with_hlt/firstSimulation.sce ; 
                      eachSimulationCommandName = Str; ../examples/PhaseI_topologies/lar_with_hlt/eachSimulation.sce ; 
                      lastSimulationCommandName = Str; ../examples/PhaseI_topologies/lar_with_hlt/lastSimulation.sce ; 
                      }
                  }
              }
      }"
end

#pmd_initial_strutureObject



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
# File 'lib/builders_examples/pdm_builders/PhaseIWithPriorityQueues/output_concrete_builder.rb', line 114

def pmd_initial_struture
  "Coupled
      {
          Type = Root
          Name = MyTopology
          Ports = 0; 0
          Description = Testing the creation of a topology by getting the info from the controller
          Graphic
              {
                  Position = 0; 0
                  Dimension = 600; 600
                  Direction = Right
                  Color = 15
                  Icon = 
                  Window = 5000; 5000; 5000; 5000
              }
          Parameters
              {
              }
          System
              {
              Atomic
                  {
                  Name = LoadScilabParams
                  Ports = 1 ; 0
                  Path = sinks/RunScilabJob.h
                  Description = If Scilab is configured as backed in the cmd line options, this model runs Scilab commands at Init, Exit and when receive events.
                  Graphic
                      {
                      Position = -13845 ; -14220 
                      Dimension = 540 ; 540
                      Direction = Right
                      Color = 15
                      Icon = %sinks%scilab.ico
                      }
                  Parameters
                      {
                      Run at Init = Str; exec('../examples/PhaseI_topologies/lar_with_hlt/model.scilabParams', 0) ; Scilab Job at Init
                      Run at External = Str;  ; Scilab Job when receive event
                      Run at Exit = Str;  ; Scilab Job at Exit
                      }
                  }
              Atomic
                  {
                  Name = ExperimenetTracker
                  Ports = 0 ; 0
                  Path = sinks/SimulationExperimentTracker.h
                  Description = Allows to use multiple simulation runs setting new parameter values in each run. It configures Scilab variables according to the current simunation number. This model should run with 1st priority!!
                  Graphic
                      {
                      Position = -11220 ; -14220
                      Dimension = 540 ; 540
                      Direction = Right
                      Color = 15
                      Icon = %realtime%lcd.svg
                      }
                  Parameters
                      {
                      ScilabSimulationSetID = Str; SimulationName ; indicates the simulation set ID
                      ScilabSimulationCounterVariableName = Str; ExperimentNumber ; Name of the Scilab variable that indicates the simulation number.
                      ScilabParametersVariableName = Str; ParameterValues ; Name of the Scilab variable that contains the parameter values for each simulation
                      ScilabParametersValuesVariableName = Str; ParameterNames ; Name of the Scilab variable that contains the parameter names for each simulation
                      }
                  }
              Atomic
                  {
                  Name = UpdateScilabParams
                  Ports = 1 ; 0
                  Path = sinks/RunScilabJob.h
                  Description = If Scilab is configured as backed in the cmd line options, this model runs Scilab commands at Init, Exit and when receive events.
                  Graphic
                      {
                      Position = -8670 ; -14220
                      Dimension = 540 ; 540
                      Direction = Right
                      Color = 15
                      Icon = %sinks%scilab.ico
                      }
                  Parameters
                      {
                      Run at Init = Str;  ; Scilab Job at Init
                      Run at External = Str;  ; Scilab Job when receive event
                      Run at Exit = Str;  ; Scilab Job at Exit
                      }
                  }
          "
end

#replace_ids_host_router_for_index(graph_elements, links_definition) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/builders_examples/ruby_builders/output_concrete_builder.rb', line 60

def replace_ids_host_router_for_index(graph_elements, links_definition)
    # Links have a dst and a src. These elements are in one of either hosts or routers array, 
    # however the index is unkown for the Link!, Instead of serializing the src/dst, the link
    # prints it's element id. We now have to find this id (which is a string), and replace it by the 
    # corresponding element of the array.
    hosts = graph_elements.select { |elem| elem.is_a? Host }
    routers = graph_elements.select { |elem| elem.is_a? Router }

    hosts.each_with_index do |host,index|
        links_definition.gsub! host.id, "hosts[#{index}]"
    end

    routers.each_with_index do |router,index|
        links_definition.gsub! router.id, "routers[#{index}]"
    end

    links_definition
end