HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ Y4 N5 ~+ ]; X1 F% ^ d& c
# }/ c1 ?! P O7 @ public Object buildActions () {
7 E( p4 G* p/ n( s super.buildActions();5 g/ A9 s a' {& p. G% k7 }
; i* F/ w/ Y; x# x) P // Create the list of simulation actions. We put these in& A; z$ M% v$ _9 R
// an action group, because we want these actions to be
9 c3 I2 [; s2 D2 L& [ // executed in a specific order, but these steps should; v0 z. N9 O/ \5 j( n. V
// take no (simulated) time. The M(foo) means "The message
% X& q4 A G+ w/ [1 L+ ~9 r9 s( w // called <foo>". You can send a message To a particular% ~$ p8 J0 e1 F7 C! I7 ^6 l( K
// object, or ForEach object in a collection.
6 ]* Q4 _5 z: q9 ?
# }2 g9 P i3 o // Note we update the heatspace in two phases: first run
# n& i% \" c' u& e, @, z9 ? // diffusion, then run "updateWorld" to actually enact the
6 x3 |, B1 J$ {8 z // changes the heatbugs have made. The ordering here is* E; l# F8 _" U. z/ D( U6 t* t
// significant!% C/ r! W+ b' v# z4 P! C
6 O' V# F5 o- t: ?6 ^- r* g+ v // Note also, that with the additional
, K4 X: S1 H' @' j: l // `randomizeHeatbugUpdateOrder' Boolean flag we can8 z: x1 w' V$ v" X" J% [
// randomize the order in which the bugs actually run
$ a4 f" f9 k& N( U& ]3 N# i' I // their step rule. This has the effect of removing any
% l7 H/ s0 [' W) F; K! [8 U& H // systematic bias in the iteration throught the heatbug
4 W# ?0 p1 ~: `1 P5 h // list from timestep to timestep, j$ r$ U* r8 _, Q9 j2 D
% `* n" m5 B5 M9 |1 f0 Z
// By default, all `createActionForEach' modelActions have) z' s0 A |* J, ?1 Q! L
// a default order of `Sequential', which means that the
% @- ` }$ I3 V2 a8 |5 N // order of iteration through the `heatbugList' will be
$ R w C2 D' u0 C" T2 o // identical (assuming the list order is not changed
v; a* q) [$ r# m // indirectly by some other process).
; h" z1 ?0 V1 b! ?, U
8 I2 S% f% _5 j modelActions = new ActionGroupImpl (getZone ());( g, W- L- R& T- g3 _) z/ ^' |
" u4 p# q' M2 \6 g2 w try {
- e! G, Y Q( U2 \2 j modelActions.createActionTo$message
* P& [5 J1 r( _" O6 r0 S- o (heat, new Selector (heat.getClass (), "stepRule", false));7 M5 ? Y; v- h: R% P
} catch (Exception e) {
" W" ]& {: g7 m# _2 [8 S. l System.err.println ("Exception stepRule: " + e.getMessage ());
6 ^. j' { K; V0 o }) B( ?, |+ ^7 z( g( ~/ j: ~# A
0 V3 r8 z* \5 l5 S4 ?, Y0 q; H" T try {
0 Y* i8 E% g7 Z Heatbug proto = (Heatbug) heatbugList.get (0);9 s2 C/ q- O0 R" j% [
Selector sel =
0 _: j- W+ I# c0 d' o5 R' q new Selector (proto.getClass (), "heatbugStep", false);" a9 Q2 y% D/ M7 K
actionForEach =
7 R8 j, N2 [" K( v& K modelActions.createFActionForEachHomogeneous$call
) z5 V& j3 ^* d# H (heatbugList,
4 ^# i$ N" s' u( H Z; I new FCallImpl (this, proto, sel,
$ l$ I8 `5 z1 M new FArgumentsImpl (this, sel)));* Z- u7 z) s8 B$ ~1 R2 ~- v
} catch (Exception e) {8 w: C& ]7 G( p; t) \4 W/ u8 I8 ^
e.printStackTrace (System.err);7 L j$ {: a8 y% R* X( N
}
% u, _9 A. @3 |( \% B ' j# t5 Z7 i# I
syncUpdateOrder ();
& l" b9 A) o, {0 z% T) n( k: k" p5 _! A
try {2 U+ f1 [* I( F/ ?$ @
modelActions.createActionTo$message ; J/ y6 ^! _: ^8 X
(heat, new Selector (heat.getClass (), "updateLattice", false));
$ ?$ t5 C. r. g" x+ I* ^ } catch (Exception e) {. }+ G; w- G N6 A7 H0 B7 d) I
System.err.println("Exception updateLattice: " + e.getMessage ());$ n0 n' m3 F* t1 D7 Y' @1 m
}5 u- t8 l+ z' W
5 ~2 G; |: b8 ?5 J% l" r6 `+ p // Then we create a schedule that executes the! Y4 _0 G; @7 r: m
// modelActions. modelActions is an ActionGroup, by itself it) t+ k) ]1 ]5 C: z
// has no notion of time. In order to have it executed in4 \+ ^/ Y3 o7 S8 F$ [
// time, we create a Schedule that says to use the
' T0 ]9 v. ]3 s8 \, v# c. |0 ?$ y // modelActions ActionGroup at particular times. This8 A' N9 u9 g7 C. Q1 A8 x% s2 D
// schedule has a repeat interval of 1, it will loop every: f/ T: }2 x# x. X. ^
// time step. The action is executed at time 0 relative to1 f, A# r- t* I6 U) | p; g& M) Z
// the beginning of the loop.
- {, p2 a" S" E! J: B& {$ g6 J' x+ p+ f0 z& Y; C+ }
// This is a simple schedule, with only one action that is
- f% ]3 Z! x+ {6 F0 L9 x, U: D; V // just repeated every time. See jmousetrap for more* H% C) }0 e/ f$ t5 E
// complicated schedules.% |) o3 z+ {: J
9 ~; r, \- u, H/ v8 e( G6 p modelSchedule = new ScheduleImpl (getZone (), 1);4 U8 ]! M- N) H2 i6 i
modelSchedule.at$createAction (0, modelActions);* N7 ]3 y7 _3 ~$ E; D& O, ?8 T, o
& m4 j0 A5 @5 x+ f; P% d return this;
8 [1 P( n) ]6 T7 k2 }6 }' s* g } |