HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- j U$ K. u0 W; w4 { [& B
7 n( }% |8 U! u Z/ B6 s public Object buildActions () {6 ~6 G4 I; Z5 `3 R, R
super.buildActions();8 t- h) _# r( c6 d7 i$ w7 `
- L1 f7 Y. g: Y6 _6 N // Create the list of simulation actions. We put these in( D# k; D1 |# u3 n A9 E+ M+ }
// an action group, because we want these actions to be; d: _: t" Z6 n$ v/ B/ C5 b# x
// executed in a specific order, but these steps should
, J: u, J& [- p% \) _ // take no (simulated) time. The M(foo) means "The message5 |# D+ ^/ X2 M& p, |4 _, N* H
// called <foo>". You can send a message To a particular
# y) y [: \9 Q // object, or ForEach object in a collection.+ V4 a/ y B! n9 w# z: d Z
$ r. V# A; o# k2 I: ?3 [ // Note we update the heatspace in two phases: first run3 z0 [. R% y6 V
// diffusion, then run "updateWorld" to actually enact the
4 \" P/ J. z6 b- k* p3 ~7 U) y1 \ // changes the heatbugs have made. The ordering here is
' b) C6 K. |, |- t. r // significant!- I7 ~& w8 p* D- C
8 H" c+ H& O, r, v5 ]$ |* S // Note also, that with the additional
( i; f& d6 G5 P8 w1 F" x // `randomizeHeatbugUpdateOrder' Boolean flag we can: ~* Z2 L, A$ o: _
// randomize the order in which the bugs actually run
+ i: A- @5 P) e // their step rule. This has the effect of removing any
/ W$ W* F m! a# }3 J. u- b) Q6 ] // systematic bias in the iteration throught the heatbug
0 j: ?9 w4 Z2 D+ g8 } // list from timestep to timestep, e8 q' M( H7 I" P, e- q
6 _; h, O4 [1 f: C$ a/ q, w7 u // By default, all `createActionForEach' modelActions have
4 L0 @+ `* E; H9 \8 [ // a default order of `Sequential', which means that the& P! @$ ~( ]' X/ ]1 ?1 O8 y
// order of iteration through the `heatbugList' will be; g! a) C/ |! [- Y0 V
// identical (assuming the list order is not changed
2 f% b: K, b2 @4 B; A // indirectly by some other process).
" |/ u& \8 X' Y$ A! F
/ P b' p2 z: D5 x modelActions = new ActionGroupImpl (getZone ());7 T$ K4 w7 O0 v, k6 h8 }& C. u
2 `, C3 m ~5 @2 [, [% e5 V
try {! b* M. \- p0 P0 [" R
modelActions.createActionTo$message! B# m8 f# y9 K% M
(heat, new Selector (heat.getClass (), "stepRule", false));
7 h0 ^9 x# y @; L1 \0 @1 ?6 { } catch (Exception e) {
# `/ R/ o1 Z( z System.err.println ("Exception stepRule: " + e.getMessage ());
: x1 c/ }% D# u2 w3 R6 } }
4 i3 k. t/ [( c0 x" {; d+ U6 v& }9 g, e
try {
; V- o5 f- c# Z; L6 q1 z Heatbug proto = (Heatbug) heatbugList.get (0);
1 r" L- v0 |2 W* o2 Y$ m Selector sel = # }) r0 e4 v# x) q2 `" _: d) s0 `
new Selector (proto.getClass (), "heatbugStep", false);& i" S1 F* G, Z. t# y( Q
actionForEach =
9 B+ q; E0 ]# K# b modelActions.createFActionForEachHomogeneous$call
+ Y' V$ }1 N; m( k* O9 F. o (heatbugList,
8 S9 M+ P, u( N' \! ~$ ~1 \9 _ new FCallImpl (this, proto, sel,
' a+ o. A+ Q- Z new FArgumentsImpl (this, sel)));
; Y; ?, I1 B D4 e8 c" D; m } catch (Exception e) {
/ |) Q& W7 J& J4 Y- [ e.printStackTrace (System.err);
3 B% m& g+ e& K1 _3 r }
, D8 A; W& ^% O
: p9 `+ f& Q# i: G syncUpdateOrder ();! b3 D" h' {- \, e( t, r( Y( P
) M0 B8 X7 E: `9 u- F7 @# k: w
try {4 Z) e! P' k+ x! j9 `8 A3 w9 Z
modelActions.createActionTo$message Y) W( v! E& {+ P: P6 u* {0 S9 z
(heat, new Selector (heat.getClass (), "updateLattice", false));
& ^+ ^7 a8 {4 S$ C0 B } catch (Exception e) {( w! T4 _9 ?2 ?
System.err.println("Exception updateLattice: " + e.getMessage ());- _7 w! F- I3 }( s1 l* q, k
}2 ^$ j; E" o# c0 P8 [: T
+ C: M" K# Z. j7 n // Then we create a schedule that executes the
$ {) e3 ^! l# h( x$ ?7 e // modelActions. modelActions is an ActionGroup, by itself it
7 a# G; y2 r1 y4 Y3 }" ? // has no notion of time. In order to have it executed in$ L" F2 k* J, q4 G2 H- w
// time, we create a Schedule that says to use the: m1 u0 _* Y# G- I. g i9 U
// modelActions ActionGroup at particular times. This- N; ]) ?1 o7 G8 z
// schedule has a repeat interval of 1, it will loop every
+ D. S" C4 S9 S1 p // time step. The action is executed at time 0 relative to, z/ [' C/ r1 x: C
// the beginning of the loop.) h* m4 ~* a A% ~* T3 u
! ^ L2 c) i- v( G
// This is a simple schedule, with only one action that is
4 d C9 R( t$ o) B // just repeated every time. See jmousetrap for more
0 }7 ]& \1 }9 N // complicated schedules.1 B0 U' T- i$ B! e6 g9 w( S
! t8 [3 {2 ~! c3 V% i w modelSchedule = new ScheduleImpl (getZone (), 1);
4 v4 D% v7 @6 c$ x# ^1 F' y; f modelSchedule.at$createAction (0, modelActions);
. o3 }4 `! s+ T' p% n* Y9 d4 `
& E ^% @: }/ K9 D return this;" Z" z- w! D) X# W$ s E
} |