HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# X& @; n1 W9 P% B2 u4 x$ b# U3 W& ?2 b7 m: ~9 e9 S
public Object buildActions () {( u4 q o$ }& G
super.buildActions();! L' g2 T t" p+ d
* A0 P8 _9 S6 f7 R- ^9 T // Create the list of simulation actions. We put these in' o; i# z, Y7 K% d
// an action group, because we want these actions to be
' P! r! ?; H- e: M: ^, l% I6 j // executed in a specific order, but these steps should- t. {1 Q2 _+ K6 I
// take no (simulated) time. The M(foo) means "The message
- e7 J& s! N3 U7 a+ F, i // called <foo>". You can send a message To a particular$ G' E: _+ D. j% U3 a" p0 v+ w
// object, or ForEach object in a collection.9 M+ l- g' D0 f' h! q a. K
( V, @0 V% x; }# m" f // Note we update the heatspace in two phases: first run" m! B8 w0 L+ N
// diffusion, then run "updateWorld" to actually enact the
+ Z$ S; i) }" Y# L2 T2 B7 }4 ? // changes the heatbugs have made. The ordering here is
1 P: l; @3 e2 l1 f" O" ? // significant!
1 [% P: G* G5 f% h: | 4 y( j T+ S8 T3 H" G' U
// Note also, that with the additional9 ~- y1 Q* |' K" b, I4 w
// `randomizeHeatbugUpdateOrder' Boolean flag we can
+ Q; R) D0 W. X) r8 f* e$ | // randomize the order in which the bugs actually run: `( K+ ~6 W3 o [; J
// their step rule. This has the effect of removing any
9 Q8 U1 n& |9 k' L% n // systematic bias in the iteration throught the heatbug
9 w4 Q% n( u1 {# C // list from timestep to timestep, _/ l! m6 C1 I2 T; @/ Q
) A/ E* O- g+ u! j8 {0 d! d$ [ // By default, all `createActionForEach' modelActions have* z( c9 b* ` x
// a default order of `Sequential', which means that the- y; w8 K$ ?9 E! x
// order of iteration through the `heatbugList' will be
) p; m0 i$ g3 `7 Q // identical (assuming the list order is not changed/ X) S5 ~# H6 R' s
// indirectly by some other process).
" N' h8 x: |! J( T `6 N4 t / q$ V% D( ?/ j8 R m& g$ M& O
modelActions = new ActionGroupImpl (getZone ());8 R$ W1 l* h: e$ P3 y1 [
. _9 w j' n0 K try {
! B2 q; D3 e$ \% }$ Y" v3 X6 ^ modelActions.createActionTo$message
' s: {4 _* v, S/ J: [ (heat, new Selector (heat.getClass (), "stepRule", false));
: n! ^6 h; q/ |& V+ n } catch (Exception e) {+ s$ i" u9 B0 f9 }$ q( k% c* P
System.err.println ("Exception stepRule: " + e.getMessage ());6 N* x: `, ~0 |' c
}- b" F: a, C- S4 j9 ~3 Q) Y
% H8 E, L4 [& t) x, R try {
: {/ U4 ~% M) ~7 D Heatbug proto = (Heatbug) heatbugList.get (0);4 {( J, o, ^) {. j
Selector sel =
; P3 i3 K/ ], m new Selector (proto.getClass (), "heatbugStep", false);, K# d0 b# T. Y) m: L, D; ^
actionForEach =* E" Z2 i) H* E
modelActions.createFActionForEachHomogeneous$call; q* s) H, `4 ?* @" r5 R
(heatbugList,! b7 |9 x6 p& M" T
new FCallImpl (this, proto, sel,
, j+ @! Z0 n! O9 c; v) ^, o0 s new FArgumentsImpl (this, sel)));: k8 L6 h: J5 k! a$ c" w1 f
} catch (Exception e) {
1 p! i) @4 ^7 k% ~/ l e.printStackTrace (System.err);
! r) V6 H7 ^8 ^, d }
# Z5 Z$ V0 u: Q" b & m2 ]: y- b/ [ H" }% Z
syncUpdateOrder ();% h& O( [" K" k
9 h2 F+ L2 u% X( a try {7 I! s2 l: c7 G9 s" U
modelActions.createActionTo$message 8 v& }6 L( Q! L3 X. i) f
(heat, new Selector (heat.getClass (), "updateLattice", false));
) x X" b }$ _6 d! t, N; | } catch (Exception e) {8 J1 \( H% `! n$ B0 F
System.err.println("Exception updateLattice: " + e.getMessage ());
9 W6 ]$ M3 j: w6 Q9 A& ~7 S }+ L7 _) r1 n% r
9 Q- e+ Z0 g, l; p' w# o8 H' O // Then we create a schedule that executes the$ s2 v& m* Y$ u8 u1 L' r# F+ x
// modelActions. modelActions is an ActionGroup, by itself it
6 a# d& I! K- I2 X% Q // has no notion of time. In order to have it executed in% t; d" `2 {7 a f' ?
// time, we create a Schedule that says to use the) @" H3 e5 S" ^; u. _' X0 V
// modelActions ActionGroup at particular times. This6 y- d# V$ |3 \) T1 e* y
// schedule has a repeat interval of 1, it will loop every
: J2 Q4 q' @/ D // time step. The action is executed at time 0 relative to r+ U% m' V! g/ H
// the beginning of the loop. q8 b- Q9 G5 {
& z8 x7 @* F- }) v
// This is a simple schedule, with only one action that is$ G* k7 r. _6 c2 c
// just repeated every time. See jmousetrap for more
) P$ K2 C7 y2 M( I$ A5 t: a% F // complicated schedules.6 J3 c9 G' N8 A3 J& j# A7 O
. X0 D( f2 y% Y6 w* y; k
modelSchedule = new ScheduleImpl (getZone (), 1);
! j& J* g* R! k# S4 T3 g* Y$ N modelSchedule.at$createAction (0, modelActions);
9 ~4 g6 Q$ }( s/ X& J8 V 8 c- O8 E I, j
return this;1 B9 _; Z3 v; f) N, d- r
} |