Class: OMF::SFA::Util::BriteParser

Inherits:
Base::LObject
  • Object
show all
Defined in:
lib/omf-sfa/util/brite_parser.rb

Instance Method Summary collapse

Constructor Details

#initializeBriteParser

Returns a new instance of BriteParser.



26
27
28
29
30
31
# File 'lib/omf-sfa/util/brite_parser.rb', line 26

def initialize()
  @nodes = {}
  @edges = []
  #@sliver = OMF::SFA::Resource::Sliver.def_sliver

end

Instance Method Details

#_parse_edges(l, i) ⇒ Object



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
# File 'lib/omf-sfa/util/brite_parser.rb', line 126

def _parse_edges(l, i)
  return if i == 0
  unless m = l.match(/([\d]+)\s+([\d]+)\s+([\d]+)\s+([\d\.]+)\s+([\d\.]+)\s+([\d\.]+)\s+([\d\-]+)\s+([\d\-]+)\s+([^\s]+)\s+([^\s]+)/)
    fatal "Can't parse edge declaration - #{l}"
    exit -1
  end
  if (pa = m.to_a[1 .. -1]).length != 10
    fatal "Expected 10 parameters in edge declaration, but got '#{pa.length}' - #{pa}"
    exit -1
  end
  # [EdgeID]  [fromNodeID]  [toNodeID]  [Length]  [Delay]  [Bandwidth]  [ASFromNodeID]  [ASToNodeID]  [EdgeType]  [Direction]
  e = {}
  [[:id, :i], [:from, :i], [:to, :i], [:length, :f], [:delay, :f], [:bw, :f], nil, nil, [:type], [:direction]].each_with_index do |k, i|
    next if k.nil?

    v = pa[i]
    case k[1]
    when :i
      v = v.to_i
    when :f
      v = v.to_f
    else
      v.strip!
    end
    #puts "name: #{name} i: #{i}"
    e[k[0]] = v
  end
  create_edge(e)
  #@edges << e
  #puts "EDGES - #{e}"
end

#_parse_header(l, i) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/omf-sfa/util/brite_parser.rb', line 78

def _parse_header(l, i)
  case l
  when /^Topology/
    # ignore Topology: ( 111 Nodes, 111 Edges )
  when /^Model/
    unless m = l.match(/Model\W*([\d]*)\W*:*(.*)/)
      fatal "Missing 'Model' declaration in header"
      exit -1
    end
    @model_type = m[1]
    @model_opts = m[2]
  else
    warn "Ignoring unexpected header line - #{l}"
  end
end

#_parse_nodes(l, i) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/omf-sfa/util/brite_parser.rb', line 94

def _parse_nodes(l, i)
  return if i == 0

  unless m = l.match(/([\d]+)\s+([\d\.]+)\s+([\d\.]+)\s+([\d]+)\s+([\d]+)\s+([\d\-]+)\s+(.*)/)
    fatal "Can't parse node declaration - #{l}"
    exit -1
  end
  if (pa = m.to_a[1 .. -1]).length != 7
    fatal "Expected 7 parameters in node declaration, but got '#{pa.length}' - #{l}"
    exit -1
  end
  # [NodeID]  [x-coord]  [y-coord]  [inDegree] [outDegree] [ASid]  [type]
  n = {}
  [[:id, :i], [:x, :f], [:y, :f], nil, nil, nil, [:type]].each_with_index do |k, i|
    next if k.nil?

    v = pa[i]
    case k[1]
    when :i
      v = v.to_i
    when :f
      v = v.to_f
    else
      v.strip!
    end
    #puts "name: #{name} i: #{i}"
    n[k[0]] = v
  end
  create_node(n)
  #puts "NODES - #{n}"
end

#create_edge(opts) ⇒ Object



46
47
48
49
50
51
# File 'lib/omf-sfa/util/brite_parser.rb', line 46

def create_edge(opts)
  from = @nodes[opts[:from]]
  to = @nodes[opts[:to]]
  edge = @on_new_edge.call(opts, from, to)
  @edges << edge if edge
end

#create_node(opts) ⇒ Object



41
42
43
44
# File 'lib/omf-sfa/util/brite_parser.rb', line 41

def create_node(opts)
  node = @on_new_node.call(opts)
  @nodes[opts[:id]] = node if node
end

#on_new_edge(&block) ⇒ Object



37
38
39
# File 'lib/omf-sfa/util/brite_parser.rb', line 37

def on_new_edge(&block)
  @on_new_edge = block
end

#on_new_node(&block) ⇒ Object



33
34
35
# File 'lib/omf-sfa/util/brite_parser.rb', line 33

def on_new_node(&block)
  @on_new_node = block
end

#parse_file(file_name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/omf-sfa/util/brite_parser.rb', line 57

def parse_file(file_name)
  f = File.open(File.absolute_path(file_name))
  sp = [
    lambda {|l, i| _parse_header(l, i)},
    lambda {|l, i| _parse_nodes(l, i)},
    lambda {|l, i| _parse_edges(l, i)},
  ]
  p = sp.shift
  i = 0
  f.each do |l|
    if l.strip!.empty?
      next if i == 0 # skip multiple consectutive empty lines
      p = sp.shift
      i = 0
      next
    end
    p.call(l, i)
    i += 1
  end
end

#to_rspecObject



53
54
55
# File 'lib/omf-sfa/util/brite_parser.rb', line 53

def to_rspec()
  OComponent.to_rspec(@nodes.values() + @edges, :request, suppress_id: true)
end