Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

so much to do...

  • Loading branch information...
commit 68e777c460a2e63e2966f93e5ca16c1162418ea7 1 parent ab91495
Dave Parfitt authored March 15, 2009
68  mm/mm.rb
@@ -62,11 +62,14 @@ def create(opname,title="none")
62 62
     end
63 63
 
64 64
     def run
  65
+      begin
  66
+        @ops.values.each do |op|        
  67
+          op.starting
  68
+        end
  69
+      rescue StandardError => boom
  70
+        puts "Error starting network: #{boom}"
  71
+      end        
65 72
 
66  
-      @ops.values.each do |op|
67  
-        op.starting
68  
-      end
69  
-      
70 73
       @stophack = 0
71 74
       running = true
72 75
       while running
@@ -82,9 +85,13 @@ def run
82 85
         end
83 86
       end
84 87
 
85  
-      @ops.values.each do |op|
86  
-        op.stopping
87  
-      end
  88
+      begin
  89
+        @ops.values.each do |op|
  90
+          op.stopping
  91
+        end
  92
+      rescue StandardError => boom
  93
+        puts "Error starting network: #{boom}"
  94
+      end        
88 95
     end
89 96
 
90 97
     def steponceinit
@@ -147,6 +154,7 @@ class IOs
147 154
     attr_accessor :outputs
148 155
     attr_accessor :configs
149 156
 
  157
+
150 158
     def initialize
151 159
       @inputs  = {}
152 160
       @outputs = {}
@@ -205,10 +213,12 @@ class Op
205 213
     attr_accessor :packets
206 214
     attr_accessor :title
207 215
     attr_accessor :network
208  
-    attr_accessor :opid
  216
+    attr_reader   :opid
  217
+    attr_reader   :errors
209 218
 
210  
-    def initialize
  219
+    def initialize      
211 220
       ios = @@configs[self.class]
  221
+      ios.outputs["errors"]=1      
212 222
 
213 223
       @opid = @@opidcounter
214 224
       @@opidcounter += 1
@@ -229,9 +239,14 @@ def initialize
229 239
     end
230 240
 
231 241
     def getopname
  242
+      # probably a method in Class that i haven't found or looked for yet
232 243
       self.class.name[4..-1]
233 244
     end
234 245
     
  246
+    def getopid
  247
+      "#{getopname}:#{@opid}"
  248
+    end
  249
+    
235 250
     def acceptsnil?
236 251
       false
237 252
     end
@@ -246,23 +261,34 @@ def Op.getopnames
246 261
 
247 262
     # i don't think i need the static methods anymore since
248 263
     # i'm not accessing from java
249  
-    def definedconfigs
  264
+    def allconfigs
250 265
       @@configs[self.class].configs
251 266
     end
252 267
 
253  
-    
254  
-    def Op.getOpInputs(opname)
255  
-      @@configs[opname].inputs.keys
  268
+    #todo: rename + cleanup
  269
+     def Op.getOpInputs(opname)
  270
+       @@configs[opname].inputs.keys
  271
+     end
  272
+
  273
+    def alloutputs
  274
+      @@configs[self.class].outputs.keys
256 275
     end
257 276
 
  277
+    def allinputs
  278
+      @@configs[self.class].inputs.keys
  279
+    end
  280
+
  281
+    #todo: rename + cleanup
258 282
     def Op.getOpOutputs(opname)
259 283
       @@configs[opname].outputs.keys
260 284
     end
261 285
 
  286
+    #todo: rename + cleanup
262 287
     def Op.getOpConfigs(opname)
263 288
       @@configs[opname].configs.keys
264 289
     end
265 290
 
  291
+    #todo: rename + cleanup
266 292
     def Op.getOpConfigDefaultValueAndType(opname,attname)
267 293
       v = instance_eval(opname.to_s + ".new()." + attname.to_s)
268 294
       return [v,v.class.name]
@@ -286,6 +312,7 @@ def Op.input(name,proc)
286 312
       ios = @@configs[self]
287 313
       ios.inputs[name]=proc
288 314
     end
  315
+    
289 316
 
290 317
     # used to define an output in the class definition
291 318
     def Op.output(name)	    
@@ -314,7 +341,15 @@ def Op.config(name)
314 341
     def processpacket
315 342
       packet = @packets.shift	      
316 343
       if packet != nil or (packet== nil and acceptsnil?)
317  
-        self.send(packet.destinput, packet.value)	    
  344
+        begin
  345
+          self.send(packet.destinput, packet.value)	    
  346
+        rescue StandardError => boom
  347
+          puts "#{boom}"
  348
+          puts "Invalid packet: [#{packet}]"
  349
+          errors.out(boom)
  350
+        end
  351
+        
  352
+        
318 353
         receivedpacket(packet)
319 354
       end
320 355
     end
@@ -339,6 +374,11 @@ def stopping
339 374
     #override  me if you like
340 375
     def setupio
341 376
     end
  377
+
  378
+    #override  me if you like
  379
+    def errorhandler(value)
  380
+      puts "Error: #{value}"
  381
+    end
342 382
   end
343 383
 
344 384
 
120  mm/ops.rb
@@ -16,7 +16,11 @@
16 16
 #    along with this program; if not, write to the Free Software
17 17
 #    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 18
 
19  
-require "DBI"
  19
+require 'rubygems'
  20
+gem 'dbi'
  21
+require 'dbi'
  22
+
  23
+
20 24
 module MM
21 25
 ###############################################################################
22 26
 #
@@ -46,6 +50,27 @@ def echo(value)
46 50
   end
47 51
 end
48 52
 
  53
+class DummyTable < Op
  54
+  input :dummyin, :din
  55
+  output :dummyout
  56
+  
  57
+  def starting    
  58
+    data = []
  59
+    1.upto(10) do |i|      
  60
+      row = []
  61
+      1.upto(10) do |j|
  62
+        row << j
  63
+      end
  64
+      data << row      
  65
+    end
  66
+       
  67
+    @dummyout.out(data)
  68
+  end
  69
+      
  70
+  def din    
  71
+  end
  72
+end
  73
+
49 74
 class Print < Op
50 75
   input :printin, :print
51 76
   output :printout 
@@ -224,47 +249,97 @@ def mod(value)
224 249
 class Each < Op
225 250
   input :eachin, :doeach
226 251
   output :eachout
227  
-  
  252
+  output :eof
  253
+
228 254
   def doeach(value)
229 255
     puts "EACH: #{value}"
  256
+
230 257
     value.to_a.each do |item|
231 258
       eachout.out(item)
232 259
     end
  260
+    eof.out("eof")
233 261
   end
234 262
 end
235 263
 
236  
-# probably not going to use this
237  
-class PipeOut < Op
238  
-  input :datain, :dodatain
  264
+
  265
+class EachPair < Op
  266
+  input :eachin, :doeach
  267
+  output :eachout
  268
+  output :eof
  269
+
  270
+  def doeach(value)
  271
+    puts "EACH: #{value}"
  272
+    
  273
+    value.each_pair do |k,v|
  274
+      eachout.out(item)
  275
+    end
  276
+    eof.out("eof")
  277
+  end
  278
+end
  279
+
  280
+class Buffer < Op
  281
+  input :bufferin, :doin
  282
+  input :trigger, :dotrigger
  283
+  output :bufferout
  284
+    
  285
+  def initialize
  286
+    super
  287
+    @buf = Array.new
  288
+  end
239 289
   
240  
-  def starting    
241  
-    @network.pipesout[self.opid] = Array.new()
  290
+  def doin(value)
  291
+    @buf << value
242 292
   end
243 293
   
244  
-  def dodatain(value)
245  
-    @network.pipesout[self.opid] << value
  294
+  def dotrigger(value)    
  295
+    bufferout.out(@buf.clone)
  296
+    @buf.clear
246 297
   end
  298
+
247 299
 end
248 300
 
  301
+
  302
+# probably not going to use this
  303
+# class PipeOut < Op
  304
+#   input :datain, :dodatain
  305
+  
  306
+#   def starting    
  307
+#     @network.pipesout[self.opid] = Array.new()
  308
+#   end
  309
+  
  310
+#   def dodatain(value)
  311
+#     @network.pipesout[self.opid] << value
  312
+#   end
  313
+# end
  314
+
  315
+
  316
+
249 317
 # need to spit out db metadata also!
  318
+# should probably pass around the db connection?
  319
+# need a way to manage resources (open + close things like files, db connections etc)
250 320
 class DBQuery < Op
251 321
   input :sql, :runsql
252  
-  output :queryout
  322
+  output :rowout
  323
+
  324
+  output :metadataout
  325
+  output :rowcountout
  326
+  output :colcountout #redundant
  327
+  output :columnnames 
  328
+  output :eof
  329
+  
253 330
   config :username
254 331
   config :password
255 332
   config :connstr
256  
-  
  333
+
257 334
   def initialize
258 335
     super
259  
-    @username=""
  336
+    @username="root"
260 337
     @password=""
261  
-    @connstr="DBI:OCI8:sid"
  338
+    @connstr="DBI:Mysql:ub"
262 339
   end
263 340
 
264 341
   def starting
265 342
     puts "DB connect"
266  
-    puts "username = [#{@username}"
267  
-    puts "password = [#{@password}"
268 343
     puts "connstr = [#{@connstr}"
269 344
     @dbh=DBI.connect(@connstr,@username,@password)
270 345
   end
@@ -275,11 +350,18 @@ def stopping
275 350
   end
276 351
 
277 352
   def runsql(value)   
278  
-    results = @dbh.select_all(value)
279  
-    queryout.out(results)
  353
+    results = @dbh.execute(value)
  354
+    rowcount = 0
  355
+    results.each do |row|
  356
+      rowout.out(row)
  357
+      rowcount += 1
  358
+    end
  359
+    rowcountout.out(rowcount)
  360
+    colcountout.out(results.column_names.size)
  361
+    metadataout.out(results.column_info)
  362
+    columnnames.out(results.column_names)
  363
+    eof.out("eof")
280 364
   end
281  
-
282  
-
283 365
 end
284 366
 
285 367
 
131  mmgui/gui.rb
... ...
@@ -1,11 +1,13 @@
  1
+require "rubygems"
1 2
 require "wx"
2 3
 require "mm/mm.rb"
  4
+require "mmgui/guiops.rb"
3 5
 
4 6
 class MMWin < Wx::Frame
5 7
   
6 8
   def initialize    
7 9
     super(nil, -1, "Metamatter",Wx::DEFAULT_POSITION,Wx::Size.new(800,600))      
8  
-    
  10
+    puts "Double buffering = #{self.is_double_buffered}"
9 11
     @network = MM::Network.new
10 12
     @loadedops = MM::Op.getopnames.sort
11 13
     
@@ -37,8 +39,10 @@ def initialize
37 39
       end 
38 40
     end
39 41
     evt_key_up() {|event| @shiftdown=false }
40  
-  end  
  42
+    #evt_erase_background() { | event | event.skip() }
41 43
 
  44
+  end  
  45
+  
42 46
   def on_left_dclick(event)
43 47
     @clickedop = @visops.find { |op| op.rect.contains(event.get_x, event.get_y) }
44 48
     if not @clickedop.nil?
@@ -58,14 +62,25 @@ def on_mouse_motion(event)
58 62
       @draggingop.rect.x = event.get_x - @dragoffx -2 
59 63
       @draggingop.rect.y = event.get_y - @dragoffy -2
60 64
 
61  
-      # will accept flicker for now :-)
62  
-      refresh
63  
-
64  
-      #biggerrect = Wx::Rect.new(@draggingop.rect.x, @draggingop.rect.y, 
65  
-      #                           @draggingop.rect.width+2, @draggingop.rect.height+2)
66  
-      #refresh_rect(@oldrect,true)
67  
-      #refresh_rect(@draggingop.rect,true)
68  
-      #refresh_rect(biggerrect,true)
  65
+      # need to think this through... 
  66
+      # will accept flicker for now :-)      
  67
+      @visconns.each do |conn|
  68
+        if conn.dest_visop == @draggingop 
  69
+          #conn.repaint = true
  70
+        elsif conn.source_visop == @draggingop               
  71
+          #conn.repaint = true
  72
+        end
  73
+      end
  74
+      #peers.reject! {|item| item.nil?}     
  75
+      peers = Array.new
  76
+      peers << @draggingop.rect
  77
+      peers << @oldrect
  78
+      minx = peers.collect { |rect| rect.x }.min
  79
+      maxx = peers.collect { |rect| rect.x + rect.width }.max
  80
+      miny = peers.collect { |rect| rect.y }.min
  81
+      maxy = peers.collect { |rect| rect.y + rect.height }.max                                   
  82
+      fillrect = Wx::Rect.new(minx-3,miny-3,maxx+3,maxy+3)
  83
+      refresh_rect(fillrect)      
69 84
     end
70 85
   end
71 86
   
@@ -84,10 +99,14 @@ def on_right_up(event)
84 99
   end
85 100
   
86 101
   def on_left_down(event)
87  
-    #puts "Left down: #{event.get_x} #{event.get_y}"
88 102
     @clickedop = @visops.find { |op| op.rect.contains(event.get_x, event.get_y) }
89 103
     if not @clickedop.nil?
90 104
       @draggingop = @clickedop 
  105
+      
  106
+      # pull the current op to the front
  107
+      @visops.delete(@draggingop)
  108
+      @visops <<  @draggingop
  109
+
91 110
       @dragoffx = event.get_x - @draggingop.rect.x
92 111
       @dragoffy = event.get_y - @draggingop.rect.y
93 112
       if @selectlist.size >= 1 and @shiftdown
@@ -111,6 +130,7 @@ def on_paint(event)
111 130
     # a little bit of flicker moving around ops. need double buffering code,
112 131
     # but not sure how to do it in wx yet
113 132
     rect = self.get_client_size    
  133
+    # paint_buffered... 
114 134
     paint do |dc|
115 135
       gdc = Wx::GraphicsContext.create(dc)      
116 136
 
@@ -121,10 +141,8 @@ def on_paint(event)
121 141
       @visconns.each do |vc|
122 142
         sourcex = vc.source_visop.rect.x + vc.source_visop.rect.width/2
123 143
         sourcey = vc.source_visop.rect.y + vc.source_visop.rect.height/2
124  
-
125 144
         destx = vc.dest_visop.rect.x + vc.dest_visop.rect.width/2
126  
-        desty = vc.dest_visop.rect.y + vc.dest_visop.rect.height/2
127  
-                  
  145
+        desty = vc.dest_visop.rect.y + vc.dest_visop.rect.height/2                  
128 146
         gdc.stroke_line(sourcex, sourcey, destx, desty)
129 147
       end
130 148
       
@@ -142,10 +160,11 @@ def on_paint(event)
142 160
 
143 161
         gdc.set_pen(Wx::WHITE_PEN)
144 162
         gdc.set_brush(Wx::BLACK_BRUSH)  
145  
-        
  163
+
146 164
         font = gdc.create_font(Wx::Font.new(8, Wx::SWISS, Wx::NORMAL, Wx::NORMAL))
147 165
         gdc.set_font(font)                        
148  
-        gdc.draw_text(visop.op.getopname, r.x+3, r.y+3)                
  166
+        gdc.draw_text(visop.op.getopid, r.x+3, r.y+3)                
  167
+        
149 168
       end            
150 169
 
151 170
 
@@ -155,8 +174,11 @@ def on_paint(event)
155 174
   def setup_menubar
156 175
     menuBar = Wx::MenuBar.new()
157 176
     fileMenu = Wx::Menu.new()
158  
-    fileMenu.append(Wx::ID_EXIT,"Exit")
  177
+
159 178
     menuBar.append(fileMenu,"File")
  179
+    save = fileMenu.append(-1,"Save")
  180
+    load = fileMenu.append(-1,"Load")
  181
+    fileMenu.append(Wx::ID_EXIT,"Exit")
160 182
     
161 183
     networkMenu = Wx::Menu.new()
162 184
     start = networkMenu.append(-1,"Start")
@@ -169,9 +191,12 @@ def setup_menubar
169 191
     evt_menu(Wx::ID_EXIT) { |event| exit }
170 192
     evt_menu(start.get_id) { |event| start_network }
171 193
     evt_menu(stop.get_id) { |event| stop_network } 
172  
-    
173  
-  end
174 194
 
  195
+    evt_menu(save.get_id) { |event| save_network }
  196
+    evt_menu(load.get_id) { |event| load_network }
  197
+  end
  198
+  
  199
+  
175 200
   def setup_opinstmenu
176 201
     @opinstmenu = Wx::Menu.new("Op")
177 202
     delete = @opinstmenu.append(-1,"Delete")
@@ -220,6 +245,7 @@ def show_connect_menu(event)
220 245
           s = "source.#{output} >> dest.#{input}"          
221 246
           instance_eval s   
222 247
           @visconns << VisConn.new(@selectlist[0],@selectlist[1],output,input)
  248
+          refresh
223 249
         end
224 250
       end
225 251
     end
@@ -236,6 +262,29 @@ def stop_network
236 262
     puts "Stopping network doesn't work :-("
237 263
     
238 264
   end
  265
+
  266
+
  267
+  def save_network
  268
+    File.open('c:\foo.m','w+') do |f|
  269
+      ops = @visops.collect  do |vop| 
  270
+        r = [vop.rect.x,vop.rect.y,vop.rect.width,vop.rect.height]
  271
+        [vop.op,r]
  272
+      end      
  273
+      Marshal.dump(ops,f)
  274
+      
  275
+    end
  276
+  end
  277
+  
  278
+  def load_network
  279
+    File.open('c:\foo.m') do |f|
  280
+      data = Marshal.load(f)
  281
+      data.each do |val|
  282
+        (op,r) = val
  283
+        @visops << VisOp.new(op,Wx::Rect.new(r[0],r[1],r[2],r[3]))
  284
+      end
  285
+    end
  286
+  end
  287
+  
239 288
 end
240 289
 
241 290
 class MMGui < Wx::App  
@@ -248,27 +297,34 @@ def on_init
248 297
 class MMOpConfigFrame < Wx::Frame
249 298
   attr_accessor :visop
250 299
   def initialize(visualop)
251  
-    super(nil, -1, "Opconfig",Wx::DEFAULT_POSITION,Wx::Size.new(200,200))      
  300
+    super(nil, -1, visualop.op.getopid,Wx::DEFAULT_POSITION,Wx::Size.new(500,300),
  301
+          Wx::FRAME_TOOL_WINDOW | Wx::RESIZE_BORDER| Wx::SYSTEM_MENU|Wx::CAPTION|Wx::CLOSE_BOX|Wx::CLIP_CHILDREN)
  302
+    set_background_colour(Wx::LIGHT_GREY)
252 303
     @visop = visualop
253 304
     
254  
-    configs = @visop.op.definedconfigs
255  
-    sizer= Wx::FlexGridSizer.new(configs.keys.size,2,2,2)
256  
-    sizer.add_growable_col(1)
257  
-    configs.keys.each do |k|
258  
-      value=@visop.op.send(k)     
259  
-      label = Wx::StaticText.new(self,-1,k.to_s)
260  
-      tc = Wx::TextCtrl.new(self,-1,value)      
261  
-      sizer.add(label, 0, Wx::ALIGN_LEFT, 2)      
262  
-      sizer.add(tc, 1, Wx::GROW|Wx::ALL, 2)      
263  
-      evt_text(tc.get_id) { |event | @visop.op.send(k.to_s+"=",event.get_string) }
264  
-      
  305
+    configs = @visop.op.allconfigs
  306
+    sizer= Wx::GridSizer.new(configs.keys.size,2,2,2)
  307
+    #sizer.add_growable_col(1)
  308
+    if configs.keys.size == 0 then
  309
+      label = Wx::StaticText.new(self,-1,"No parameters available")
  310
+      sizer.add(label,1,Wx::GROW|Wx::ALL,2)
  311
+    else       
  312
+      configs.keys.each do |k|
  313
+        value=@visop.op.send(k)     
  314
+        if not value.nil? then 
  315
+          label = Wx::StaticText.new(self,-1,k.to_s.capitalize)
  316
+          tc = Wx::TextCtrl.new(self,-1,value,Wx::DEFAULT_POSITION, Wx::DEFAULT_SIZE,Wx::TE_MULTILINE)
  317
+          sizer.add(label, 0, Wx::ALIGN_CENTER | Wx::GROW|Wx::ALL, 2)      
  318
+          sizer.add(tc, 1, Wx::GROW|Wx::ALL, 2)      
  319
+          evt_text(tc.get_id) { |event | @visop.op.send(k.to_s+"=",event.get_string) }
  320
+        end
  321
+      end
265 322
     end
266 323
     set_sizer(sizer)
267 324
     
268  
-    
  325
+
269 326
   end
270  
-  
271  
-  
  327
+   
272 328
 end
273 329
 
274 330
 
@@ -279,21 +335,24 @@ class VisOp
279 335
   def initialize(operator, rectangle)
280 336
     @op = operator
281 337
     @rect = rectangle
282  
-  end
  338
+  end  
283 339
 end
284 340
 
285 341
 class VisConn
286 342
   attr_accessor :source_visop
287 343
   attr_accessor :source_output
288  
-
  344
+  
289 345
   attr_accessor :dest_visop
290 346
   attr_accessor :dest_input
291 347
 
  348
+  #attr_accessor :repaint
  349
+  
292 350
   def initialize(source, dest, output, input)
293 351
     @source_visop = source
294 352
     @dest_visop = dest
295 353
     @source_output = output
296 354
     @dest_input = input
  355
+    #@repaint = true
297 356
   end
298 357
 end
299 358
 

0 notes on commit 68e777c

Please sign in to comment.
Something went wrong with that request. Please try again.