HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ E8 ^# M) `0 |( e7 i* z
: b, j* P) H1 P$ q: p2 v2 F public Object buildActions () {+ k! X* Q( ?* i4 F7 n, p
super.buildActions();
4 Q! u% O( `7 D2 E; J; }* o 1 Z6 U$ R1 `( l
// Create the list of simulation actions. We put these in
7 H4 e: W4 a% s4 R0 w, P- l2 n- B // an action group, because we want these actions to be
t3 S. \: l+ P L/ g // executed in a specific order, but these steps should9 o3 ?& w8 A" \+ r5 O, |3 A
// take no (simulated) time. The M(foo) means "The message
: R0 Q; D( N( ]. [+ a# T8 p6 x // called <foo>". You can send a message To a particular
4 z* I3 e! R* G d // object, or ForEach object in a collection.2 | Z& K* F+ X+ K6 T
+ A' [, {+ l u J: u; }6 Z // Note we update the heatspace in two phases: first run
% i1 D5 ?: l* i6 C0 d' ` `7 _ i // diffusion, then run "updateWorld" to actually enact the
7 }* Q- _# P0 s0 }0 O }# h6 C // changes the heatbugs have made. The ordering here is& p! }" x) V. N% Z4 F
// significant!
% ^/ a4 N7 l' g! h
: r# D" N# x. e2 g* N+ J2 K // Note also, that with the additional
t# D$ A! b) Q0 |& w3 s0 ?# \ // `randomizeHeatbugUpdateOrder' Boolean flag we can, m$ w% c) Z% B0 V
// randomize the order in which the bugs actually run* M) n, U3 Y3 ], w- C
// their step rule. This has the effect of removing any
$ ~' h4 i9 U7 k+ Y8 h3 u // systematic bias in the iteration throught the heatbug
4 j/ }# c% M" r5 ^- L // list from timestep to timestep
6 x4 |2 M. Q! w* _# }5 ^" p- t
% W0 x2 ?$ G% V- o2 S // By default, all `createActionForEach' modelActions have1 a- n5 H$ d/ A U/ ^- P0 i
// a default order of `Sequential', which means that the) S& o+ F0 T( N. L$ h' }
// order of iteration through the `heatbugList' will be
+ B. p! E# d% V' f& K" | // identical (assuming the list order is not changed
: ~$ `) O0 z" i // indirectly by some other process).
f" O% Y9 N; ]
+ j' l( E G8 H+ |. t2 v modelActions = new ActionGroupImpl (getZone ());
4 w8 x9 u# R1 x* B7 O5 C' E' A4 k. |- O) P# H$ |* |7 D
try {& b" h# o/ f+ u
modelActions.createActionTo$message
4 |; R2 P2 k ] @ T (heat, new Selector (heat.getClass (), "stepRule", false));
" x( q1 c$ v' F. _% b } catch (Exception e) {4 W) y3 ~/ z6 q$ F# d
System.err.println ("Exception stepRule: " + e.getMessage ());% g% f8 h; O* P' v+ a
}
* h" f s( m+ \& [2 \4 H8 H3 H/ Q+ y- o& Z
try {
& |9 a! R0 L3 c8 l; P1 } Heatbug proto = (Heatbug) heatbugList.get (0);
/ Y2 z; S D- S3 Q Selector sel = . q* Q. V* h0 y8 V1 X" F
new Selector (proto.getClass (), "heatbugStep", false);
$ B, `; j9 K8 o" e: H; f actionForEach =
" C7 h8 d& J% c3 ]) ~ modelActions.createFActionForEachHomogeneous$call* v Q- \2 A" |$ W- Y
(heatbugList,
x$ {( O6 j+ M; I) Z7 @3 i new FCallImpl (this, proto, sel," h/ w# k( w5 p# P5 f% z' r' b$ n
new FArgumentsImpl (this, sel)));) ]$ A3 O* @% }: [3 ^
} catch (Exception e) {
( u( y" D/ \8 B; h w) K! { e.printStackTrace (System.err);0 K" J( N9 @0 h: W( ]4 \: G
}
' G& R) d Q1 t7 J1 s0 J6 ~ ( G- w: b; t7 t! ]; |1 |* X. V
syncUpdateOrder ();: m7 K% h D @4 T+ n( Y, S
6 q# z5 D. b v7 M4 u% Y I
try {
: `2 ^4 G& u0 h3 T& k1 g. n/ P4 x; C modelActions.createActionTo$message
. N. \# J, p/ i6 p (heat, new Selector (heat.getClass (), "updateLattice", false));
" I1 e4 m# F. I, U$ o- K } catch (Exception e) {
2 Y, l b* S0 G4 g* j; P System.err.println("Exception updateLattice: " + e.getMessage ());
% n( G; `8 \, `, ^- H1 I }9 w, D1 A: I9 \0 F
& i+ `7 Y0 T, R3 w
// Then we create a schedule that executes the+ \+ ?9 O8 b) l8 A
// modelActions. modelActions is an ActionGroup, by itself it; k; I1 N4 J# g+ Q8 S5 o T& @
// has no notion of time. In order to have it executed in" Q6 |' P9 E* t2 Z/ Q
// time, we create a Schedule that says to use the
9 b. `* b/ X" o% T- X) A3 D' Q // modelActions ActionGroup at particular times. This+ _' B5 b4 S. }6 }" Z* O* A
// schedule has a repeat interval of 1, it will loop every7 Z6 V; F, U* _' c
// time step. The action is executed at time 0 relative to; @4 t/ L, d# U. A! P# q
// the beginning of the loop.
/ ]* }9 C/ u r/ o. M8 ^* T6 M
6 D. m! {% A1 i9 L2 T. [) L* @) A // This is a simple schedule, with only one action that is5 V" g! a9 C" d' ^% M# G0 q
// just repeated every time. See jmousetrap for more& i- J- |% I* |' R
// complicated schedules.
, D0 a$ {- T) C& b2 S$ } ! X) t! S' v) y8 z
modelSchedule = new ScheduleImpl (getZone (), 1);5 h. \1 \: c# |% j$ l% p
modelSchedule.at$createAction (0, modelActions);
5 L% S* R, J" U& M
9 F$ ^1 R' k+ H0 A* G! U return this;
3 R F; x: ^. }7 Z& P } |