HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! l. T0 D* e- O7 P% J. {3 s
: M7 j/ B, U9 ^0 ?" ^ public Object buildActions () {
- a! \( x0 A' s6 Y. t super.buildActions();
* ~0 E: l ]: J; d/ z( X2 d
+ c' Y; K9 P. M0 C) S // Create the list of simulation actions. We put these in
7 H- Y/ p( u7 p( i f3 j, r // an action group, because we want these actions to be
( W* o, r! k3 E) R3 u. @ // executed in a specific order, but these steps should
! I! h* o% ]- ?; ]4 B6 m // take no (simulated) time. The M(foo) means "The message
- F9 y( k0 f* u6 w- g0 D // called <foo>". You can send a message To a particular! |& J; Q, p6 o
// object, or ForEach object in a collection.! z5 m, o( @' x. Y4 ~
0 `( Z; {; P/ c. F0 q
// Note we update the heatspace in two phases: first run
! R" G5 ^" |& j9 m. f9 ? // diffusion, then run "updateWorld" to actually enact the: A N1 s. j0 M- r
// changes the heatbugs have made. The ordering here is8 l9 k1 c7 J5 B- ^) `7 S: x
// significant!
/ \9 ^. h: d! n ) s! E4 l! B" M) y4 {
// Note also, that with the additional
7 b5 }0 O7 V) \ // `randomizeHeatbugUpdateOrder' Boolean flag we can
E* `. _0 Z+ K7 B) ^, [/ S // randomize the order in which the bugs actually run
; ?* D$ b" N0 Y4 T // their step rule. This has the effect of removing any
5 q: ?( F- r+ u4 `! @2 w // systematic bias in the iteration throught the heatbug5 [+ T& [8 N" l- G. i
// list from timestep to timestep& u. O# ] a5 r
1 c( X: R- z9 b( b8 h // By default, all `createActionForEach' modelActions have0 b2 {1 X6 ]! ~( d. z- H" b k7 k- ~
// a default order of `Sequential', which means that the. [, P2 q1 A, J. c$ E( r6 u
// order of iteration through the `heatbugList' will be
$ V. p# A _% Q0 M% Y6 ? // identical (assuming the list order is not changed
+ ^7 N/ [; F" w h* D // indirectly by some other process).5 c& S( k+ ?% x ?1 u0 u
& i& T5 h( J7 b7 h( r9 b
modelActions = new ActionGroupImpl (getZone ());/ e% l6 j( s. U* ^
) C; [, ~5 f6 V
try {. y9 t, v& i& c, n1 {0 c' c$ p
modelActions.createActionTo$message
- X3 G3 f0 I! }3 C (heat, new Selector (heat.getClass (), "stepRule", false));
$ X g' G( a. e5 z' q( h5 b- U } catch (Exception e) {1 j# }$ a- o+ G/ R* ~7 A6 t
System.err.println ("Exception stepRule: " + e.getMessage ());2 k. [* S( T; B- p3 a) k
}
& E0 B3 ~* s4 x8 [, V0 L2 y: {+ }" l* {. J$ _# C1 X* A; Y
try {. k3 U( S$ p, x3 u0 m! ?7 n
Heatbug proto = (Heatbug) heatbugList.get (0);2 V0 A* O0 Y. D- ]9 l: m# {+ C1 L
Selector sel =
8 K& v* i8 C" }; n new Selector (proto.getClass (), "heatbugStep", false);6 w; v8 x3 K0 y* Y3 [
actionForEach =
' m7 q' G9 f: M# A: u modelActions.createFActionForEachHomogeneous$call
: E$ [, @9 M2 [ (heatbugList,2 ?( c8 x4 U* F5 E$ w# ^, k0 @
new FCallImpl (this, proto, sel,3 N! p: j% d; k4 Q' J0 a8 q) T
new FArgumentsImpl (this, sel)));, y1 [0 w: ]/ Q* y/ j" @
} catch (Exception e) {5 O& K* t8 G$ Q2 J
e.printStackTrace (System.err);
) s F! q) S+ Q* @; T }, q* @% T: U4 W; K' L' V& e6 Q
0 h# E; D7 `4 J; R
syncUpdateOrder ();
! H( z, J# y l8 ]
8 N: e U& r4 X8 a2 [7 \ try {( i+ |9 ~* ^/ z
modelActions.createActionTo$message
7 q& i+ g( ~1 M6 q v (heat, new Selector (heat.getClass (), "updateLattice", false));
2 a' [: _9 E Z+ e } catch (Exception e) {
, J6 Q4 [9 a6 F System.err.println("Exception updateLattice: " + e.getMessage ());
: V$ G( r5 E; ~# W/ G( X c- R }
& o# L' s8 P5 ]3 S! I. `0 ]. u6 u
* K$ z4 d, L; a+ V5 B: z B q! I // Then we create a schedule that executes the
3 { h: \" b" o( @7 z // modelActions. modelActions is an ActionGroup, by itself it
5 Q' ~6 S$ p. x0 [ // has no notion of time. In order to have it executed in
( e' r" a9 q! p4 J0 L1 t // time, we create a Schedule that says to use the
! z3 I0 R/ F+ Y4 R // modelActions ActionGroup at particular times. This& z; U5 B4 j6 G1 `
// schedule has a repeat interval of 1, it will loop every$ t7 Y4 u- \# q! m. S8 T6 _
// time step. The action is executed at time 0 relative to
, [7 P+ ?9 T& |7 q9 T: C8 C // the beginning of the loop.
7 c$ o' }$ b% o& e& H
8 r% I7 h4 z! W5 }0 i" Y9 R // This is a simple schedule, with only one action that is1 v" i- Y: S& V+ J6 V0 U8 m
// just repeated every time. See jmousetrap for more! ]" Q+ @3 D6 V1 b1 x6 v% d
// complicated schedules.4 k! P( e2 k+ @& \" A/ R% H( A
) t: Z4 J7 S$ c5 f1 m5 p- W8 Q
modelSchedule = new ScheduleImpl (getZone (), 1);, w! d6 Z& ~1 ^- H
modelSchedule.at$createAction (0, modelActions);) C" H J) \+ e6 s8 W2 C" |8 a
# {9 q) ]* p% h4 G+ g
return this;; `/ y) N" f1 ?6 D' x
} |