HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. N F* H& w) t
9 q( N3 K3 r% V% A. L* h public Object buildActions () {
/ h* ^/ c. @/ M* e; q9 K super.buildActions();1 g$ x5 B; n* f! G
2 z. Y7 c" N: D
// Create the list of simulation actions. We put these in
% R3 I, d# V, ~$ Z' i* _) ^& V // an action group, because we want these actions to be
5 A% \: ^3 Q* h // executed in a specific order, but these steps should$ e i: J8 J r% e! s7 s; x' _
// take no (simulated) time. The M(foo) means "The message
' G& Q3 n- p' l' `# a // called <foo>". You can send a message To a particular$ f9 Y/ i! k/ D- y- c9 Z: d
// object, or ForEach object in a collection. w1 w$ ~7 {: k8 |* n# R8 s/ h4 g
% N: A1 E6 m7 u2 l0 l! | // Note we update the heatspace in two phases: first run
4 t7 r# @! c4 x6 \' p; J // diffusion, then run "updateWorld" to actually enact the8 L3 W6 G; r. m# b# t
// changes the heatbugs have made. The ordering here is
8 T% z4 b9 ]% o6 Z9 O" G // significant!/ V' ?! x' w3 s% V
8 @5 I2 `* C; r* V+ U' h: m
// Note also, that with the additional; b. k3 b8 n( K! y6 N
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ O: o/ W4 I1 X& w" l8 M
// randomize the order in which the bugs actually run4 x8 G, o. X: b
// their step rule. This has the effect of removing any7 g$ ?: w" G4 J& F3 j) k( [7 i
// systematic bias in the iteration throught the heatbug
9 }, T% S$ u! h C2 K // list from timestep to timestep- C! ~, E1 w9 P$ ^8 N
; O! E, k9 j6 f$ s. Y$ V
// By default, all `createActionForEach' modelActions have' T q0 T3 W0 i6 W" j1 b
// a default order of `Sequential', which means that the
( a Z6 Z2 ?. {. ~ // order of iteration through the `heatbugList' will be
" w. `6 q Y. f; y1 k/ _+ O5 k // identical (assuming the list order is not changed2 s. J3 y8 j7 ]0 X7 p
// indirectly by some other process).1 t, Q! K/ T) w
" n1 @. @' v' i' s$ G+ I4 t9 ?4 _+ r modelActions = new ActionGroupImpl (getZone ());
; W: h, t# @* B/ p# T1 d3 c4 q8 ?6 k# @- h
try {& C0 Z. n1 v% P, h* }& ]7 c
modelActions.createActionTo$message& s) i2 A& g8 B" j0 v& G P9 l
(heat, new Selector (heat.getClass (), "stepRule", false));8 `* \* g+ k3 r
} catch (Exception e) {$ a; V( u# E' E$ l8 c& J' K; p
System.err.println ("Exception stepRule: " + e.getMessage ());
% u4 E7 |7 r0 Z3 a }
" L( L* A- a* \
4 |5 x% Q: H5 c p try {
1 m1 K2 k7 x- }1 G+ t" A* h1 R% f Heatbug proto = (Heatbug) heatbugList.get (0);* F6 T9 Q5 }% l5 H3 }5 z5 w2 I
Selector sel = ) ]- X0 y5 N* @/ J- M
new Selector (proto.getClass (), "heatbugStep", false);3 x% j0 V: ~. @" j& j9 q3 B, I& j G
actionForEach =6 t4 ?# N) Z$ C4 @5 L, |% T" I
modelActions.createFActionForEachHomogeneous$call
x$ ?( Y5 a1 Q; s% Q+ t K (heatbugList,7 V- O) ?3 Y- B9 {/ t X- O
new FCallImpl (this, proto, sel,) |: r) x' ~2 l. L# @( \$ _
new FArgumentsImpl (this, sel)));
* `! D2 L! C$ w/ M, F1 o } catch (Exception e) {
) C/ H% L* C" [) r8 P, Z' F4 d7 G2 A$ a e.printStackTrace (System.err);
" L+ E7 K; M# X }7 M; m2 \( {9 r$ n* ^5 u
. v, x+ K' A3 D: V% ]6 v syncUpdateOrder ();1 }+ P0 a r% d) `
6 O3 h6 u# K, k6 k# ` try {
- K/ v0 c( R( ^" M7 W2 ]7 ~' i modelActions.createActionTo$message 5 q) O$ w* `- y+ K0 K
(heat, new Selector (heat.getClass (), "updateLattice", false));
. E; t2 d, F3 k; [( U- Q+ q } catch (Exception e) {1 l" x, X4 [) ~
System.err.println("Exception updateLattice: " + e.getMessage ());
1 r( |6 o# N" K } F0 m; c5 f/ d4 z
8 S& r& E: | z$ _ ] // Then we create a schedule that executes the
' q( _6 J$ n. B3 e; | // modelActions. modelActions is an ActionGroup, by itself it, N9 g& F+ R8 y% l+ ?
// has no notion of time. In order to have it executed in& T/ M) w2 I- ~' Z1 q
// time, we create a Schedule that says to use the
3 \9 w3 J+ }+ p // modelActions ActionGroup at particular times. This
{* a6 I* S. O; D: M, X // schedule has a repeat interval of 1, it will loop every5 t3 i) O2 b- S9 e
// time step. The action is executed at time 0 relative to
- F# Y8 J* {2 @5 h7 ]: D9 i // the beginning of the loop.
2 E; a! C1 [7 y
% F$ y$ [! ]9 }+ D' F6 l // This is a simple schedule, with only one action that is H/ e; e e+ d
// just repeated every time. See jmousetrap for more+ i) L0 d0 j J' h! d' K6 f
// complicated schedules.2 A. N) M" O, T( A( n* L* s, |) h: G
( A+ p8 l/ b/ d" ~ modelSchedule = new ScheduleImpl (getZone (), 1);
% K. b3 B2 d+ s. E% t modelSchedule.at$createAction (0, modelActions);, u7 E5 D/ j+ B3 l0 t* P5 V
" f. L$ V( k8 U4 c1 |
return this;
0 O4 i0 M4 J% i } |