Class: VirtualNode

Inherits:
Object
  • Object
show all
Defined in:
app/models/virtual_node.rb

Constant Summary collapse

DRAW_PROJECTION =
{1 => "rectangular uniform coordinate",
 2 => "semi-logarithmic coordinate (y axis)",
 3 => "semi-logarithmic coordinate (x axis)",
 4 => "logarithmic coordinate",
 5 => "polar coordinate",
 6 => "bipolar coordinate",
#                     7 => "elliptic coordinate",
 10 => "equidistant cylindrical projection",
 11 => "Mercator's projection",
 12 => "Mollweide's projection",
 13 => "Hammer's projection",
 14 => "Eckert VI projection",
 15 => "Kitada's elliptic projection",
 20 => "equidistant conical projection",
 21 => "Lambert's equal-area conical projection",
 22 => "Lambert's conformal conical projection",
 23 => "Bonne's projection",
 30 => "orthographic projection",
 31 => "polar stereo projection",
 32 => "azimuthal equidistant projection",
 33 => "Lambert's azimuthal equal-area projection"
}
DRAW_SIZE =
[[700,700], [550,550], [400,400], [250,250]]
@@draw_options =
{
  "x_axis" => {:type => "string"},
  "y_axis" => {:type => "string"},
  "projection" => {:default => 1, :type => "int"},
  "region" => {:default => {}, :type => "hash"},
  "pileup" => {:default => false, :type => "boolean"},
  "size" => {:default => [400,400], :type => "array_int"},
  "anim" => {:default => false, :type => "boolean", :optional => true},
  "anim_dim" => {:type => "string", :optional => true},
  "viewport" => {:default => [0.2, 0.8, 0.2, 0.8], :type => "array_float", :optional => true},
  "map" => {:default => false, :type => "boolean", :optional => true}
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ VirtualNode

Returns a new instance of VirtualNode.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/virtual_node.rb', line 48

def initialize(nodes)
  nodes = [nodes] unless Array === nodes
  nodes.each{|node|
    if Node === node
      unless node.variable?
        raise "node must be variable"
      end
    elsif ! VirtialNode === node
      raise "node is invalid"
    end
  }
  @original_nodes = nodes
  @num_vars = nodes.length
  @functions = Array.new
  @draw_method = nil
end

Instance Attribute Details

#draw_methodObject

Returns the value of attribute draw_method.



46
47
48
# File 'app/models/virtual_node.rb', line 46

def draw_method
  @draw_method
end

#functionsObject

Returns the value of attribute functions.



46
47
48
# File 'app/models/virtual_node.rb', line 46

def functions
  @functions
end

#original_nodesObject

Returns the value of attribute original_nodes.



46
47
48
# File 'app/models/virtual_node.rb', line 46

def original_nodes
  @original_nodes
end

Instance Method Details

#[](*ind) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/virtual_node.rb', line 83

def [](*ind)
  if ind.length == 0
    return [false, "index is invalid"]
  end
  ind.each{|i|
    unless Integer === i
      return [false, "indices must be integer"]
    end
  }
  if ind.max >= @num_vars
    return [false, "index out of range"]
  end
  @functions.push( {:type => :index, :index => ind} )
  @num_vars = ind.length
  return true
end

#cut!(*val) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/virtual_node.rb', line 100

def cut!(*val)
  if val.length == 0
    return [false, "value is invalid"]
  end
  val.each{|i|
    unless Numeric === i || Range === i || TrueClass === i || FalseClass === i
      return [false, "value is invalid"]
    end
  }
  @functions.push( {:type => :cut, :value => val} )
  return true
end

#draw(dm, opts = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/virtual_node.rb', line 113

def draw(dm,opts=nil)
  unless DrawMethod === dm
    raise "draw method is invalid"
  end
  unless @num_vars%dm.nvars == 0
    return [false, "wrong number of variables (#{@num_vars}%#{dm.nvars}!=0)"]
  end
  opts_new = Hash.new
  dm.draw_method_attributes.each{|dma|
    on = dma.name
    if ( val = opts.delete(on) )
      opts_new[on] = val
    end
  }
  @@draw_options.each{|on,v|
    if ( val = opts.delete(on) )
      opts_new[on] = val
    end
  }
  unless opts.empty?
    return [false, "wrong options were specified: #{opts.keys.join(", ")}"]
  end
  @draw_method = [dm,opts_new]
  return true
end

#function(func, args = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/virtual_node.rb', line 65

def function(func,args=nil)
  unless Function === func
    return [false, "function is invalid"]
  end
  if @draw_method
    return [false, "cannot apply function after drawing"]
  end
  unless @num_vars%func.nvars == 0
    return [false, "wrong number of variables (#{@num_vars}%#{func.nvars} != 0)"]
  end
  if args && args.length > (al = func.function_arguments.length)
    return [false, "wrong number of arguments (#{args.length} for #{al})"]
  end
  @functions.push( {:type => :func, :func => func, :args => args} )
  @num_vars = func.function_outputs.length * (@num_vars/func.nvars)
  return true
end

#get(path) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/virtual_node.rb', line 144

def get(path)
  fname = File.temp_name(path,"_001.png")
  dirname = File.dirname(fname)
  basename = File.basename(fname,"_001.png")
  res,code = get_code(basename)
  if res
  #    File.open(File.join(dirname,basename)+".rb","w"){|file| file.print code} # for debug
    msg = execute(code,dirname)
  else
    msg = code
  end
  if msg
    files = nil
  else
    files = Dir[File.join(dirname,basename)+"_*.png"].sort
  end
  return [files, msg]
end

#options_to_str(opts) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/models/virtual_node.rb', line 193

def options_to_str(opts)
  unless opts && Hash === opts && !opts.empty?
    return ""
  end
  ary = Array.new
  opts.sort.each{|k,v|
    case v
    when String, Numric
      ary.push "#{k.to_s}=#{v}"
    when Array
      ary.push "#{k.to_s}=#{v.join(',')}"
    else
      raise "not supported type"
    end
  }
  return "?" + ary.join("&")
end

#pathObject



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
# File 'app/models/virtual_node.rb', line 163

def path
  str = @original_nodes.collect{|on| on.path}.join(",")
  @functions.each{|f|
    case f[:type]
    when :func
      str += "/analysis"
      str += f[:func].path
      hash = Hash.new
      if ( args = f[:args] )
        args.each_with_index{|arg,i|
          hash["argv[#{i}]"] = arg
        }
        str += options_to_str(hash).gsub(/\?/,"%3F")
      end
    when :index
      str += "/[#{f[:index].join(',')}]"
    when :cut
      str += "/#{f[:value].join(',')}"
    else
      raise "invalid type"
    end
  }
  if @draw_method
    str += "/draw"
    str += @draw_method[0].path
    str += options_to_str(@draw_method[1])
  end
  return str
end

#to_xml(arg = {}) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'app/models/virtual_node.rb', line 211

def to_xml(arg={})
  uri_prefix = arg.delete(:uri_prefix)
  nodes = Array.new
  @original_nodes.each{|on|
    case on
    when Node
      path = on.path
      nodes.push( {"path" => path, "uri" => File.join(uri_prefix, "data", path+".xml")} )
    when VirtualNode
      nodes.push node
    end
  }
  funcs = Array.new
  @functions.each{|func|
    case func[:type]
    when :func
      path = func[:func].path
      hash = {"path" => path, "uri" => File.join(uri_prefix, "data", path+".xml")}
      hash["arguments"] = func[:args] if func[:args]
      funcs.push hash
    when :index
      funcs.push( {"indices" => func[:index]} )
    when :cut
      funcs.push( {"cut" => func[:value]} )
    end
  }
  hash = {"nodes" => nodes, "functions" => funcs}
  if @draw_method
    path = @draw_method[0].path
    hash["draw_method"] = {"path" => @draw_method[0].path, "uri" => File.join(uri_prefix, "data", path+".xml")}
    hash["draw_method"]["options"] = @draw_method[1] if @draw_method[1]
  end
  return hash.to_xml(*arg)
end

#typeObject



140
141
142
# File 'app/models/virtual_node.rb', line 140

def type
  @draw_method ? "draw" : "analysis"
end