HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 l1 J+ k8 j: O+ F+ o% {: J6 l8 q) P0 V
public Object buildActions () {9 p( |$ v8 t$ x, s9 C9 z* B# P
super.buildActions();
* P3 s& a: M3 V1 S; n K0 I
- f# U0 D$ b9 C# Z$ W // Create the list of simulation actions. We put these in ~9 m: P, F/ N
// an action group, because we want these actions to be' s; ~, O5 @/ U4 {1 X
// executed in a specific order, but these steps should2 c; t7 L {3 V6 w" @+ V) W
// take no (simulated) time. The M(foo) means "The message
0 |' y" X# k# f // called <foo>". You can send a message To a particular
7 T) F P" |; g# ~* V5 c, w9 n // object, or ForEach object in a collection.7 D' d/ ~5 |( K8 Z4 r' W% W$ {2 ^
' j& L2 \* D( o // Note we update the heatspace in two phases: first run- {. k8 |# R/ A M8 D
// diffusion, then run "updateWorld" to actually enact the4 ]8 J5 e! m2 W2 {% S, v9 i c
// changes the heatbugs have made. The ordering here is! H" ]# _+ C$ e3 W$ ?; S( L, I& K, O
// significant!- f! `! [% a7 p' n
* {) O K6 {) q! } W // Note also, that with the additional
2 ?6 r+ B( H7 a3 H // `randomizeHeatbugUpdateOrder' Boolean flag we can% U; R D: p' J' q# n& }
// randomize the order in which the bugs actually run! _% B: `' M# ?5 f$ Z6 N: Y& @
// their step rule. This has the effect of removing any
5 d$ R& B1 {3 o // systematic bias in the iteration throught the heatbug+ {* f1 w$ v) w* H* q
// list from timestep to timestep. O2 G, d& Y" u( w: x, C9 y
4 m" X5 @5 |7 D: ]0 p // By default, all `createActionForEach' modelActions have6 U) I! e$ E7 a2 T' E; Q- W
// a default order of `Sequential', which means that the
, z9 n- J0 b1 Q2 s // order of iteration through the `heatbugList' will be4 j: V! R0 J8 x9 R; U" E- k7 w
// identical (assuming the list order is not changed- ^1 e' F( U3 p; |0 v2 s) I: F5 _
// indirectly by some other process).
9 u5 a& a! x$ n' i7 N- D. m
8 ~5 Y: M; N" D9 b$ E" ~0 H modelActions = new ActionGroupImpl (getZone ());% H8 J4 {" `# V- t. C
( r' f2 P" q5 }- I1 g* d* I
try {
! P4 ~; I4 m0 x! m5 L. J- U modelActions.createActionTo$message
( q! J! Y0 i/ }6 ~5 j3 X" u6 K9 b% W (heat, new Selector (heat.getClass (), "stepRule", false));: R* L, n0 T+ n i& E
} catch (Exception e) {
( K& |4 l8 K. d: Z System.err.println ("Exception stepRule: " + e.getMessage ());
* a9 V- j; J) e! e# h* u }, g' p- F7 j! v: x7 s, }9 J: ]
; \7 }* H7 M4 L- E) A' R) m! S6 p try {$ T9 L: e) n# j' J! v
Heatbug proto = (Heatbug) heatbugList.get (0);
7 H3 J' ]/ s2 a; L/ p Selector sel = 2 G- U6 t7 j; v4 o1 h, U
new Selector (proto.getClass (), "heatbugStep", false);* T8 ^! S- }; j. u; }0 v
actionForEach =
$ z- e# `7 f( d- Z. s8 P modelActions.createFActionForEachHomogeneous$call9 R: S4 ]" Q* I! i4 u4 z& ] j+ ^; R
(heatbugList,
8 {9 b+ o4 d8 _, P new FCallImpl (this, proto, sel,/ \& D! E8 D: K" y
new FArgumentsImpl (this, sel)));1 g" v8 q. q/ M F6 G `- k
} catch (Exception e) {
6 F- L9 k& j E4 x% Q e.printStackTrace (System.err);5 y: J. D& p) s% @( @6 |( ~
}
( n% g% c7 z6 Y0 S; Q ) R; h% N7 |$ I0 i
syncUpdateOrder ();
8 @8 V1 R. q' v$ R6 ?2 k1 ~4 P4 q* y* h
try {1 `* @/ ~4 A+ v
modelActions.createActionTo$message
* I* |3 b. y' J. e (heat, new Selector (heat.getClass (), "updateLattice", false));
' q/ c4 ^% k/ H$ Q* | } catch (Exception e) {
+ ^6 [; ~ M/ @) t' z6 o System.err.println("Exception updateLattice: " + e.getMessage ());
0 u3 a7 C2 J6 {7 ^' f! {0 f }
0 ^9 z" P5 \) e6 e
3 i D# l# ^( I3 D8 t5 E! x // Then we create a schedule that executes the
4 c) e5 k- z; X0 `) f // modelActions. modelActions is an ActionGroup, by itself it
: L, h0 {8 y/ [9 R // has no notion of time. In order to have it executed in
! ? l" A9 \3 |+ N+ b3 `1 r // time, we create a Schedule that says to use the9 J; s$ i' Y$ _5 A
// modelActions ActionGroup at particular times. This; o- G' a4 _' P0 ~2 v( O" x
// schedule has a repeat interval of 1, it will loop every' X+ M; h: o7 P- F; E+ J$ X( N) k
// time step. The action is executed at time 0 relative to
8 v7 O& \5 `" k8 i // the beginning of the loop.
" x: }! U6 ?2 G* S$ L
: V' W9 E, K; R) U6 j8 x: v9 L // This is a simple schedule, with only one action that is1 _9 N% [+ x! p
// just repeated every time. See jmousetrap for more c: k) M+ A/ p+ B
// complicated schedules.
# k6 W' K. }0 E5 M3 b % o% W* Q1 |4 d x- ]6 y3 ?1 f
modelSchedule = new ScheduleImpl (getZone (), 1);1 k& z" p$ L! I) z2 M- `
modelSchedule.at$createAction (0, modelActions);
" q; y: L& ] J2 x : i" r" d) g/ Y) M# C2 a8 X1 F
return this;
( C# I7 u+ D( E( @" z( C" z! D } |