HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) ~ [0 R, _7 ^+ M8 n' r: H c G; y$ d
public Object buildActions () {
9 }2 h( J( ~1 H super.buildActions();
7 V$ Q0 p5 [1 k8 C) v2 B
$ L$ [0 W4 b$ T5 u/ l7 U // Create the list of simulation actions. We put these in
8 Q" a8 |8 }, y // an action group, because we want these actions to be( m0 M- i3 ~9 x. x/ s4 g2 C; E
// executed in a specific order, but these steps should! n9 o' t3 K2 H* m! X( ^* w# u4 M
// take no (simulated) time. The M(foo) means "The message3 l2 ^$ F1 Q7 {- r
// called <foo>". You can send a message To a particular
9 w$ K1 { U1 I# d7 T ] // object, or ForEach object in a collection.
6 h) F3 Y/ }& ~+ A3 h; P 2 G8 m4 ~; t, v. X! `4 E2 G- L) }' {
// Note we update the heatspace in two phases: first run/ C) y8 U$ ~' u4 m, Q
// diffusion, then run "updateWorld" to actually enact the
3 V5 |8 |+ D+ v/ h% r8 g // changes the heatbugs have made. The ordering here is
6 G2 V' K+ O5 |2 ~5 y0 h // significant!
; h! y9 q$ d5 ?" ~' X- e2 `$ U) l% C
Z$ u9 s8 a! Y4 J8 j! [ // Note also, that with the additional
# c) |* }# O6 S0 _# @- r // `randomizeHeatbugUpdateOrder' Boolean flag we can% \/ W% U7 e& h3 g( l# M2 B
// randomize the order in which the bugs actually run" P* u( o0 i2 U. E/ w3 D
// their step rule. This has the effect of removing any( D. S- d$ O3 U! C. T
// systematic bias in the iteration throught the heatbug
4 Q. J, \6 g9 {: B9 ` // list from timestep to timestep( f- {7 q" w1 f' u0 A
) F8 V! t+ c$ E2 a; s r* A. t // By default, all `createActionForEach' modelActions have
. B7 V: h9 Y; l, l: j // a default order of `Sequential', which means that the; m( V3 \. j; D
// order of iteration through the `heatbugList' will be
/ X" F* i* t8 ]0 @* T // identical (assuming the list order is not changed
6 s2 k# r7 F( q' ^ o // indirectly by some other process).7 M. k+ J# K' n2 e5 k5 L
' z3 ]7 c, |4 S7 M modelActions = new ActionGroupImpl (getZone ());
! P( i" K2 t* n. G
% \, I5 e; ?: t) _& O try {; T0 b9 ]; E" a9 g9 j" k; `" K; D
modelActions.createActionTo$message
" ~: A1 L$ ?, ~$ K (heat, new Selector (heat.getClass (), "stepRule", false));, j' H' z8 z8 a K, F- d
} catch (Exception e) {- P& E: p2 Y# a
System.err.println ("Exception stepRule: " + e.getMessage ());5 m, ^3 c/ u( l
}' B% M1 e- L5 M I s
1 _* h1 X3 u5 D+ [" r3 P0 A, v' M8 D
try {) x7 I4 ?5 O( `" G# s( K
Heatbug proto = (Heatbug) heatbugList.get (0);
& C: w+ X* \' W Selector sel =
4 o5 v# W' Z1 A& z& ?# |5 ^ new Selector (proto.getClass (), "heatbugStep", false);
. F4 c: k1 o* v+ Z5 p+ T% t actionForEach =) s6 Q5 H% C4 f7 I
modelActions.createFActionForEachHomogeneous$call. T: z' m) b/ R; I5 p: v( }
(heatbugList,9 @' _) z; n7 ] n, ~
new FCallImpl (this, proto, sel,0 T: [: H, j3 a4 J
new FArgumentsImpl (this, sel)));5 ~' N& q& y7 E; |) s' z$ V4 I: Z7 y
} catch (Exception e) {* ~% O2 e0 k& _% ?& u& j9 N
e.printStackTrace (System.err);
# B" t4 q& _: f; k" b. Z }; c; p! K5 P9 b1 N6 n3 C
" k' Q6 X2 O! Q, y3 E. D i
syncUpdateOrder ();- x. J0 i0 l/ j) w! Z8 G# C
6 q c/ L/ I6 x0 y$ k6 K8 c9 J1 z8 g
try {
" Y, Y8 k9 k: _$ K, n5 T- U modelActions.createActionTo$message / j! d- |5 ?+ N$ @* F; K
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ S4 c: M8 O, P; X+ L7 j" | } catch (Exception e) {9 B- o4 o8 r( m, Q( K
System.err.println("Exception updateLattice: " + e.getMessage ());6 U( _5 ~9 g# y# n+ y" A; }2 I: [
}$ M6 S% y: q2 u' p @8 u
+ |' t7 S, F" u2 r/ R1 z4 X // Then we create a schedule that executes the
, n* C5 V- f- A; O7 r' w // modelActions. modelActions is an ActionGroup, by itself it
M& ~; j: I5 w, ] // has no notion of time. In order to have it executed in
, B; O) A- y0 E& E+ B // time, we create a Schedule that says to use the
2 O# I' y$ ]) y% ], ]- S) \1 I1 X // modelActions ActionGroup at particular times. This* S4 Z( U6 I/ K; A6 c
// schedule has a repeat interval of 1, it will loop every* m4 D# M7 T a7 [; u: ^
// time step. The action is executed at time 0 relative to
6 t; M+ a/ R5 J7 ]; @4 _ // the beginning of the loop.+ U( ^! x X1 q7 W, l
: T9 C, e) w4 r: b // This is a simple schedule, with only one action that is G; s) P' N! W: z3 H* s2 j
// just repeated every time. See jmousetrap for more
3 W( W. U, C1 w5 ~ |5 e // complicated schedules.: b m- S: _8 ~& p4 a7 O8 o$ {
6 l0 ]0 i" M) X, z- H modelSchedule = new ScheduleImpl (getZone (), 1);
7 q& E, G4 S9 W" L modelSchedule.at$createAction (0, modelActions);6 e; [) ~4 K1 c+ K2 E. K* n& I, _
/ K k% D* h% |6 `8 S# w
return this;# Y# l3 P8 A/ c. t$ _ u
} |