HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 R& s* F Q5 w5 h% V2 V+ F
# N0 v: g# r* n* z
public Object buildActions () {: I+ H) r5 G, _& H' [: v( u
super.buildActions();
$ Y9 y4 u0 L% }, s' z/ E S* Q9 x3 Y( j* w) R
// Create the list of simulation actions. We put these in# v* n) G; D/ M0 }
// an action group, because we want these actions to be( R/ P j3 i% D* D5 L$ H
// executed in a specific order, but these steps should
5 c+ a2 [/ p* B6 d X" _/ g5 R# u // take no (simulated) time. The M(foo) means "The message& y' X% s$ V$ ^
// called <foo>". You can send a message To a particular3 [4 o' J& Q" v: \6 ~
// object, or ForEach object in a collection.
: G# V- N5 q0 Q* i& H: X5 j* u: H
' {8 A$ ?* J7 I/ J" B, h) p // Note we update the heatspace in two phases: first run
; s: S/ f Y& U8 o/ A // diffusion, then run "updateWorld" to actually enact the3 ]- {' h5 D3 ^. m
// changes the heatbugs have made. The ordering here is
2 S5 S7 A2 L1 g5 Q0 n& K // significant!
9 d& \% L0 Z* L* b, j& N5 p
( ?/ M8 j' O% ]# j: Z( Z% _# i // Note also, that with the additional7 u) S- r8 b3 w" q
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 q$ m) |% Q+ l; f // randomize the order in which the bugs actually run ~7 c0 e. X/ {5 @0 S
// their step rule. This has the effect of removing any: a1 c6 [" g& G" t7 L7 x7 a1 p0 ?
// systematic bias in the iteration throught the heatbug& v' w- d9 h6 n+ {% H2 P
// list from timestep to timestep6 \ ]; M! N, g* m% [
p I" S% ]* d
// By default, all `createActionForEach' modelActions have
, @. g# u5 X# j1 l$ | // a default order of `Sequential', which means that the
. J0 ?( n2 K0 W Q+ N // order of iteration through the `heatbugList' will be
4 u0 {/ k4 C3 ~# ~* v // identical (assuming the list order is not changed9 b7 N8 d. I5 q
// indirectly by some other process).' t* A! _; y; @1 d' U
# O: T# ^! V4 _4 |. z' M' D& p0 i modelActions = new ActionGroupImpl (getZone ());3 `9 b+ B( T% Q9 U7 W
6 V/ D) f) e! q% K5 z- p* C
try {
+ {9 g% B: m- P0 f. x9 S modelActions.createActionTo$message! K: ]1 t- y5 @$ p% ~) f
(heat, new Selector (heat.getClass (), "stepRule", false));6 m) [2 Z4 a7 w: |6 v$ u$ v7 | n
} catch (Exception e) {( a( X5 ]0 @; [8 i. Y3 f# _
System.err.println ("Exception stepRule: " + e.getMessage ());/ ]+ W# p8 ~* y0 R
}
+ }1 J* z2 c7 x \/ d" T/ T+ l& k4 I1 k3 B5 ^- o
try {/ ]" T( W6 B$ n6 [# Q& L
Heatbug proto = (Heatbug) heatbugList.get (0);
6 }" b1 W2 u* L7 B Selector sel =
$ c/ ]: V. ?" M* w) ]+ c new Selector (proto.getClass (), "heatbugStep", false);
- _9 {4 d/ P7 k1 a/ e% q1 ~ actionForEach =' h1 F2 J" o2 b" M* {9 }3 m" S; W# Z
modelActions.createFActionForEachHomogeneous$call9 L$ ^0 S% {+ ~4 ~' e G! m
(heatbugList,
( g( x/ A& m+ X2 f new FCallImpl (this, proto, sel,
7 E* R( ]( H6 H0 a$ |( t m new FArgumentsImpl (this, sel)));
, D8 [/ R1 W, N3 a- [4 V } catch (Exception e) {$ O* J f8 O0 k6 M
e.printStackTrace (System.err);" T# ^' x, `: z( O, P6 L: }
}3 M6 v0 N$ G9 A8 O* v8 @, p: m3 b! Y" s
$ N: g7 }+ }2 g9 K& |6 l& w# E' H0 e1 B
syncUpdateOrder ();
5 i' K" f# g+ U, [- [, J2 a) y! g0 Z. j! x1 E
try {
+ o. S) u( }5 I0 I% P8 L/ p modelActions.createActionTo$message
0 C. t& C/ ?+ K* L+ ` (heat, new Selector (heat.getClass (), "updateLattice", false));
6 u v/ N. m, S) ^0 M6 I } catch (Exception e) {3 p0 f, d( `1 p5 e; M# X3 b. L
System.err.println("Exception updateLattice: " + e.getMessage ());
8 ]2 Q) ]3 V4 p }) O4 S5 K5 `6 W. W- m; W
0 T( j1 B5 {; d& x+ O& j
// Then we create a schedule that executes the6 B, v& a! U0 b$ L6 j% Y
// modelActions. modelActions is an ActionGroup, by itself it
7 s+ D& Y* O) j' ]- w1 E" P // has no notion of time. In order to have it executed in
. R- O* i5 M d4 j2 K' [- u" } // time, we create a Schedule that says to use the' i, A' E7 @" c
// modelActions ActionGroup at particular times. This0 s' o2 [8 m) S' I+ d" g
// schedule has a repeat interval of 1, it will loop every
' [" w& E- p$ `0 W. f // time step. The action is executed at time 0 relative to7 Z& I# w8 n0 C8 _5 H, s
// the beginning of the loop.
) B4 \9 d( J6 k k
/ b+ m5 w1 h0 L // This is a simple schedule, with only one action that is- e3 J& o+ Y/ w* u* W, e
// just repeated every time. See jmousetrap for more& S* u3 h! x1 R U* V
// complicated schedules.% y( L& R2 k' s
; B# C' h/ B+ G& X modelSchedule = new ScheduleImpl (getZone (), 1);
) z! X- o3 L9 l8 P6 U" G, O, ]. S modelSchedule.at$createAction (0, modelActions);
: \3 ]+ \" V5 @ U( U( } 4 J/ i6 ^0 [: o4 U. k
return this;
/ ?8 P) f, D" A- [/ [: n+ p } |