HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* b0 P/ @' _: c3 d. a: @* X
; p* N4 H& k F public Object buildActions () {
# l0 }3 ]3 T$ l+ I super.buildActions();
7 T# U+ j; I5 u
# Y* ^' \! ^; }0 H: Q // Create the list of simulation actions. We put these in
7 x8 |6 {, `" X$ A! Y, y+ a: V& E9 N // an action group, because we want these actions to be7 i0 G) |0 i) @3 x
// executed in a specific order, but these steps should9 D3 I4 i2 H& r9 n2 Y( w
// take no (simulated) time. The M(foo) means "The message
2 f' [ a- O! F1 O // called <foo>". You can send a message To a particular
# ~ o5 S* H m. p8 G( Z2 ] // object, or ForEach object in a collection.& l& r/ g/ P( G' p3 o" M; \
, ]: T) ]; p; x: x4 `3 V // Note we update the heatspace in two phases: first run# N- O( ~) Y$ o; A% y G% N
// diffusion, then run "updateWorld" to actually enact the9 d/ w+ `/ U1 h
// changes the heatbugs have made. The ordering here is
5 @! i" N. U! F1 |- d // significant!
2 F) M% n/ V, E
3 n2 M% Y* W N2 E D* ?$ N, P // Note also, that with the additional
! U. a/ }% V, o4 } // `randomizeHeatbugUpdateOrder' Boolean flag we can
. @1 F+ o. g. J // randomize the order in which the bugs actually run1 S" s! G, D$ [. r+ x: F8 Z; a9 j0 f
// their step rule. This has the effect of removing any
3 N. e) X# K {& Z // systematic bias in the iteration throught the heatbug- V E9 t7 y* z8 B
// list from timestep to timestep
+ j: C9 j8 o9 i: D! ]+ q
: E' k- o1 D4 [ // By default, all `createActionForEach' modelActions have
) `% u" S0 @! p4 b' M. C1 ^ // a default order of `Sequential', which means that the
$ h' u- v/ C4 \. Y. j // order of iteration through the `heatbugList' will be6 h/ u& ]9 i: a: p& V
// identical (assuming the list order is not changed
2 }/ X$ x0 L( Q) q: ? // indirectly by some other process).
3 Y3 l3 {& \% t) p6 T- V! Z 0 |( u7 V, a- w+ h# ?$ ^% h
modelActions = new ActionGroupImpl (getZone ());6 T% y& q# e4 f* a. t
! p6 @' D! N; L. {0 ?1 d* c
try {
# y; l" [/ L4 Q modelActions.createActionTo$message1 h" U+ C4 B8 H ?% E% e
(heat, new Selector (heat.getClass (), "stepRule", false));! u" W c7 o% q1 b
} catch (Exception e) {! Z! r) t! I! L& ]& s( S# G
System.err.println ("Exception stepRule: " + e.getMessage ());' z0 c5 W# i( _9 d* H& q
}
. u) E* j' y3 l; w! ]6 r& y* ^$ A& @; f0 |; s" m
try {. c- G" e9 v. S, e# ]
Heatbug proto = (Heatbug) heatbugList.get (0);
! N. w y' {, x6 M \ V N Selector sel =
/ r: f/ q2 \: p) L/ b! k% @% l! c new Selector (proto.getClass (), "heatbugStep", false);6 ~+ o2 b* F! e. I
actionForEach =
0 V1 j* f) [/ M modelActions.createFActionForEachHomogeneous$call" q0 V5 P2 x# X( d2 s/ I2 r
(heatbugList,
8 o1 a: ~/ @: J/ R new FCallImpl (this, proto, sel,
0 \, g+ s7 x6 L: L3 A3 O new FArgumentsImpl (this, sel)));
( |# o, V" L" {# i1 D( h+ A) I } catch (Exception e) {( E5 F& M( ^; S" m+ b$ X
e.printStackTrace (System.err);7 N; Y% t8 }! X, V! P
}( x$ g. P1 Q1 D/ V$ E, X
: X" t( J% g* x U$ v2 a8 t
syncUpdateOrder ();- ?, P: F: b" Y% T
* l. ~: i' o: T# V try {# b4 B% S/ N% D& p$ Q2 u: N
modelActions.createActionTo$message
* G$ }2 a3 o$ |/ z! p# V (heat, new Selector (heat.getClass (), "updateLattice", false));
- o6 Y7 t0 C. Q- T; i: F5 X+ t } catch (Exception e) {& d; N8 |8 H+ i6 E3 T1 w+ n% h
System.err.println("Exception updateLattice: " + e.getMessage ());+ i' @8 d$ K! c1 [6 Y
}
2 E! R. y$ n# \, w
3 E! j5 i* j# s$ L% V1 j, V // Then we create a schedule that executes the1 V( l! _% e, x1 M2 w" x
// modelActions. modelActions is an ActionGroup, by itself it
) ~6 n+ f+ h8 A" K( S" G // has no notion of time. In order to have it executed in
1 T$ X/ A" P" S* K // time, we create a Schedule that says to use the3 [) w |* h% N( V. n; B
// modelActions ActionGroup at particular times. This, p$ ~5 {6 u0 ], }
// schedule has a repeat interval of 1, it will loop every
) c5 q3 S( V4 U+ V8 [! J // time step. The action is executed at time 0 relative to
! K( W$ ]7 y& `# y4 q# H // the beginning of the loop.
0 p# ?& K6 I8 B" f' s$ X) Q4 k( Y* v' @, e9 E! i
// This is a simple schedule, with only one action that is( y2 X, N( l- Y1 @6 [
// just repeated every time. See jmousetrap for more
, c+ N+ R" Y3 C* c7 Z1 O. G/ t // complicated schedules.! i3 e; Z4 z2 u8 l
& h! l: u# d8 ]' [7 t8 C c
modelSchedule = new ScheduleImpl (getZone (), 1);
8 h5 x& q* l( J% L) z" u* O6 \$ ^ modelSchedule.at$createAction (0, modelActions);
* B+ X/ M7 f* P: n
: s6 b% h4 o# W return this;6 K" D3 P2 d Z2 p8 b7 E) m
} |