HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ A, m5 U' G7 Z( @6 b
/ y" X% D; J7 B. P: Z9 g
public Object buildActions () {
: J6 q& B7 E9 q0 Q7 {' t, A) J super.buildActions();
4 j1 p4 @& r3 a9 ? 8 o, h4 {3 W/ t& ]% z/ t1 t
// Create the list of simulation actions. We put these in; l' ~2 D& m# `" ~
// an action group, because we want these actions to be
# i1 f9 ]% L7 E- w. ] // executed in a specific order, but these steps should
, Z" ^) v: e+ J, U% c5 z' ? ?/ Q // take no (simulated) time. The M(foo) means "The message
6 p* @9 @! \; |! Q // called <foo>". You can send a message To a particular! Q3 X0 Z+ W Q% g4 l
// object, or ForEach object in a collection.8 `: j8 N { C2 j' i7 X
8 e! d/ u9 ]/ A7 @ // Note we update the heatspace in two phases: first run
1 I! s+ j% ]- R7 e // diffusion, then run "updateWorld" to actually enact the
/ J$ _9 D5 U7 R, c" s y // changes the heatbugs have made. The ordering here is$ |/ t! W1 N. _' V! B; r5 j% d
// significant!
: M/ _9 |/ ]2 m) T# L , }7 j9 A& s+ b3 [4 c, w
// Note also, that with the additional
1 J1 h. x6 h+ ^* T* t0 i // `randomizeHeatbugUpdateOrder' Boolean flag we can7 M! B9 A6 h8 V$ p, V1 }
// randomize the order in which the bugs actually run& W3 \4 X/ a& X# U
// their step rule. This has the effect of removing any
# u# @7 [$ g5 q) \, C" t0 Q' ^* i4 ] // systematic bias in the iteration throught the heatbug3 a+ _8 Y* Q7 z+ A, D _
// list from timestep to timestep9 _: B- s( _2 M# ~' N" ?8 t$ P3 j
) }! n5 Z7 h* U0 m! z
// By default, all `createActionForEach' modelActions have4 a. c/ }( F( z4 W& U
// a default order of `Sequential', which means that the$ w+ H0 ^5 H+ u+ u: ?% ]
// order of iteration through the `heatbugList' will be9 f, w' p- n' J' s j* d4 W
// identical (assuming the list order is not changed
, n9 Q4 O0 A6 P* O // indirectly by some other process).) s6 W( F0 R& {: d, R9 t
5 C& [9 l4 i6 }! h, ~- J s
modelActions = new ActionGroupImpl (getZone ());/ C1 K3 \+ E% Q! T
. [4 r3 C( c+ Y! T: t try {
) m2 `$ d1 Q% [! o) W5 f( O) w$ E modelActions.createActionTo$message4 }* l. `0 y: i0 |' N6 _9 H
(heat, new Selector (heat.getClass (), "stepRule", false));
! @$ E2 U, i e/ o/ ]# _ } catch (Exception e) {* a& J& v& m8 C9 R7 s d
System.err.println ("Exception stepRule: " + e.getMessage ());
i1 i$ n7 N: X/ |% \, [9 E/ J, n }5 R6 ]* P% e0 x. ~9 Y9 b& Y; @( [- S* ?3 F
( z+ E" q) s8 F
try {& e5 l& Q) H1 p, {# I" G* L, Z
Heatbug proto = (Heatbug) heatbugList.get (0);
+ Y e2 ?4 ?' E2 O! U3 L2 p( ` Selector sel =
% ^3 s9 B: Z9 Z1 Y2 K( o new Selector (proto.getClass (), "heatbugStep", false);- L g0 M* M! Z# q% t* D9 u
actionForEach =: L+ n3 t/ {" j/ I. f9 A
modelActions.createFActionForEachHomogeneous$call
$ `9 y4 E' b' w8 d8 u. U* m1 M |$ T* l6 W (heatbugList,: Z* S5 X5 i7 R' k6 w) A
new FCallImpl (this, proto, sel,
) g1 C" E1 F. {3 u3 y' P* G$ l new FArgumentsImpl (this, sel)));
( E9 P0 H" Q4 _' e( n8 i7 b } catch (Exception e) {% T$ ~7 A( r5 `/ J7 @- {* \
e.printStackTrace (System.err);
; O. w$ h( c- Q7 D9 E) a }! F0 Z5 E' U4 A" x2 g
F& ~6 Y6 b% {$ K9 x
syncUpdateOrder ();* x) K3 x# t# o! c/ ^- \: {
, R, _" W3 ?6 g, `4 S6 u! b
try {: f+ s3 N& v5 {, ?1 @: ]2 T
modelActions.createActionTo$message
5 G, q' c. w) X9 T, L o( r (heat, new Selector (heat.getClass (), "updateLattice", false));
# U$ _0 K' g8 Q. Q) e } catch (Exception e) {- g. u) z5 S5 A F5 ]
System.err.println("Exception updateLattice: " + e.getMessage ());
# Y% [4 s+ f1 m4 g4 `9 r }
" X$ r; u- t9 E z
8 m f6 ^) {: _ // Then we create a schedule that executes the
8 w8 l' e# v% V0 B // modelActions. modelActions is an ActionGroup, by itself it
! Z4 y7 w$ E3 G& R4 V6 G8 s- u // has no notion of time. In order to have it executed in$ D9 U. Z2 \7 r0 Y" h4 d
// time, we create a Schedule that says to use the w/ H: U0 M9 w r# I
// modelActions ActionGroup at particular times. This
, X% L8 y8 p( E0 s7 S // schedule has a repeat interval of 1, it will loop every4 ]- x2 n# |; f" a: x1 U4 Z% y
// time step. The action is executed at time 0 relative to
9 t5 |" n9 r% n# {% A+ w0 i/ Y // the beginning of the loop.6 J8 i$ e" C7 n; H5 Z* b
: ?. U$ i0 L, A1 r ^+ }' r5 {* l1 y // This is a simple schedule, with only one action that is" Q8 P: y, J% c) o
// just repeated every time. See jmousetrap for more
" X, ? A! \: M: q6 ? // complicated schedules.
! F% B E" V t4 A6 C ~, b) f2 B, A
modelSchedule = new ScheduleImpl (getZone (), 1);! O% X9 E5 s4 n
modelSchedule.at$createAction (0, modelActions);! s% c5 W7 D1 S
8 [$ V6 D# y/ O/ Q% [) C( n return this;
: j2 J7 F: Q5 g& p2 H2 e8 z7 o } |