From: Yasushi Shoji Date: 2000-05-04T15:54:02+09:00 Subject: [ruby-list:22259] Re: StateGraph: state machine interpreter やすし % GUI大好きです。 From: Hideto ISHIBASHI Subject: [ruby-list:22218] Re: StateGraph: state machine interpreter Date: Mon, 1 May 2000 15:45:44 +0900 > > 説明: > > http://www.rr.iij4u.or.jp/~hideto-i/rb/stategraph/index.html に、ある sample.rbは、 tra = Transition.new(str, sta, :activated) { print "activated\n" } じゃなくて tra = Transition.new(str, sta, :activated, proc { print "activated\n"}) ですか? あと、めんどくさがりな自分のために‥ module StateGraph class StateMachine def add(arg) case arg when Transition add_transition(arg) when State add_state(arg) else raise "invalid type '#{arg.type}'" end end end end > 実現方法は上記ウェブページに書きました。 > しかしサンプルは書いていません。 > どなたかが書いてくだされば、ぜひリンクを張らせて > (あるいは置かせて) いただきます。 汚いサンプルを付けてみます。こうしたら良いなどのつっこみおねがいします。 # StateGraph::StateMachine#addをしなくても動くのはなぜ? -- yashi require 'stategraph' require 'gtk' module StateGraph class StateMachine def add(arg) case arg when Transition add_transition(arg) when State add_state(arg) else raise "invalid type '#{arg.type}'" end end end end include StateGraph w = Gtk::Window.new vbox = Gtk::VBox.new button = Gtk::Button.new('run') sb = Gtk::Statusbar.new id = sb.get_context_id("default") $>.sync = true sta = State.new("active") stw = State.new("wait") str = State.new("ready") ste = State.new("exit") sm = StateMachine.new str tra = Transition.new(str, sta, :activate, proc { Thread.new do print "thread started #{Thread.current}..." start = Time.now sleep 2 dulation = Time.now-start print "done: #{dulation}sec\n" sm.event :stop, dulation end }) trk1 = Transition.new(sta, sta, :kill, proc { sb.push (id, "active: can't exit") }) trk2 = Transition.new(str, ste, :kill, proc { Gtk.main_quit }) trs = Transition.new(sta, str, :stop, proc { |sec| sb.push(id, "done: #{sec}sec") }) # sm.add tra # sm.add trk1 # sm.add trk2 # sm.add trs w.signal_connect ('delete-event') do sm.event(:kill) true end button.signal_connect ('clicked') do sm.event(:activate) end vbox.pack_start(button, true, true) vbox.pack_start(sb, false, true) w.set_usize(150,100) w.add vbox w.show_all Gtk.main