HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ z3 T* P$ h; P1 H' T$ {
$ M+ ]8 W2 x( t1 Q0 T- w
public Object buildActions () {7 J/ W+ n1 j' [8 ]9 L+ u7 ~! b/ a0 |9 W
super.buildActions();$ e' j) B2 Q2 w7 x/ O" R1 _/ P
2 ?- N$ p, {; \ {( A U // Create the list of simulation actions. We put these in6 Y! L/ z. k& i
// an action group, because we want these actions to be
) W0 Y% ]% j% C9 _! k( W+ D4 J% y // executed in a specific order, but these steps should( x! a m4 ~: \6 A- ^
// take no (simulated) time. The M(foo) means "The message
' f- o4 \" d e1 U- M6 u* ?/ | // called <foo>". You can send a message To a particular" U+ B. e/ l4 G3 @2 ?( n
// object, or ForEach object in a collection.
9 z1 ~* i' X5 U4 K5 [+ J' h, ]+ y) _ $ G! t, ]: X( e8 a& h4 H- t
// Note we update the heatspace in two phases: first run
9 m+ r3 ^9 Z( t/ A& C& ?+ k$ U- G // diffusion, then run "updateWorld" to actually enact the' |: O+ ^( H8 e6 h; j
// changes the heatbugs have made. The ordering here is
E3 P8 t" p% ^ // significant!
. \% T2 x& w1 a" d0 E5 @8 M
0 r2 L M' L6 D7 l; E$ Z, ~ // Note also, that with the additional
5 I1 B: _" o7 @6 ] P' a // `randomizeHeatbugUpdateOrder' Boolean flag we can ?+ u9 \! d) t' e2 {$ O
// randomize the order in which the bugs actually run
2 _$ P `3 u% `* h7 o // their step rule. This has the effect of removing any6 c5 Z, ]" X; x- E' l5 ]4 w% ~
// systematic bias in the iteration throught the heatbug
, Y8 w! c- y3 \2 k; N // list from timestep to timestep
4 L1 O2 j" J1 e8 L
4 L/ J: u {# y+ D0 G // By default, all `createActionForEach' modelActions have" o' Z: u j8 a0 |; f! d. H
// a default order of `Sequential', which means that the
4 q' _. s4 x9 K // order of iteration through the `heatbugList' will be
- t$ `( i/ x2 g" z // identical (assuming the list order is not changed
' z& u5 Z7 i j( l. A- d. ? // indirectly by some other process).
& N$ n) S: F+ I4 A
% ?1 |0 q! d7 d9 N modelActions = new ActionGroupImpl (getZone ());) ~! ]# k: E, [: d
- T$ N; n- a. t |# B; Q- Q- ? try {- c9 @" Y. C8 ~; r8 X& I9 u
modelActions.createActionTo$message
- }, l1 V3 Y- G/ l9 `; } (heat, new Selector (heat.getClass (), "stepRule", false));
! p/ a) t) X7 e# D# Y- E } catch (Exception e) {
4 j$ P: K: \* E9 J9 H System.err.println ("Exception stepRule: " + e.getMessage ());
: K7 f4 z" P) C _7 q" @# c( Y }
0 s3 X0 H. \, a7 N# o; M l
4 @% \1 b5 U$ j: `9 d; E4 S& U try {7 L8 J$ k3 r4 t' t s
Heatbug proto = (Heatbug) heatbugList.get (0);
) q5 J ^6 W9 s) s Selector sel = 6 n0 w6 k% r9 p
new Selector (proto.getClass (), "heatbugStep", false);- R9 }& Y- Z" f8 i% F( C8 V
actionForEach =' V: Q+ I: _+ \9 x
modelActions.createFActionForEachHomogeneous$call
5 b9 e) k e6 V7 B# D8 P (heatbugList,' T7 [& ^6 Y& v
new FCallImpl (this, proto, sel,
\& G" o d7 @7 H new FArgumentsImpl (this, sel)));0 _2 G9 @* b z( o. b, s1 [
} catch (Exception e) {9 n6 {) ]; L4 l3 N4 K' r
e.printStackTrace (System.err);. U% j1 t$ m d+ _6 M
}
h, N6 {1 v: b
$ i8 M1 U9 n! `3 W; @- {& J" n syncUpdateOrder ();
4 e$ z, F( i0 n' b& p" i2 t$ c4 o+ k% c8 H$ _$ i' f* f
try {$ N2 E+ j$ C# C. I
modelActions.createActionTo$message
' u+ n% v T( g' d( s! @, t3 x$ {2 M (heat, new Selector (heat.getClass (), "updateLattice", false));
2 [/ X' h# j9 O3 I9 U5 P$ {: G8 |8 I } catch (Exception e) {+ C+ n9 D7 K8 |2 i+ ]
System.err.println("Exception updateLattice: " + e.getMessage ());
* S ]4 y8 c/ }, S+ G2 {8 ? }6 T, @/ D4 C) R7 U; I
0 v- X0 K3 g' a/ N' \8 r- U! q // Then we create a schedule that executes the
. B/ M6 I& X: U; w // modelActions. modelActions is an ActionGroup, by itself it) C! a; l, ]9 }2 _
// has no notion of time. In order to have it executed in9 O0 a4 B% q( E( K% j8 ~
// time, we create a Schedule that says to use the6 i K& A8 [" Q( z$ {6 B
// modelActions ActionGroup at particular times. This( I# \! o( n3 S$ _
// schedule has a repeat interval of 1, it will loop every
- a p; s0 b; S' E& c* A% G4 p2 z // time step. The action is executed at time 0 relative to9 U) C9 @- q1 o" f5 h
// the beginning of the loop./ z: x" r, n/ h; V4 @; r
+ r" k* a/ n& v& ~( w4 A // This is a simple schedule, with only one action that is ?$ _% v& P" i/ m: W/ r
// just repeated every time. See jmousetrap for more o* X" ?6 G& _$ U
// complicated schedules.& x' u n" g2 U: ^, M( F1 V* s
, w6 v2 m/ }/ X' h9 z& {! r modelSchedule = new ScheduleImpl (getZone (), 1);6 R' n b1 L- r# \; O. b
modelSchedule.at$createAction (0, modelActions);
# c9 D$ G; J5 {$ L
2 }6 P- B' |- Q( c6 ` return this;# Z( A( Q; J5 x& o6 K/ j7 d
} |