HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 C5 M# z5 y; E' i. A& F/ p# P" d& y
public Object buildActions () {
$ @" E4 K- K! ~. F super.buildActions();2 o( e& V, w& ~+ z
4 Q+ A8 M- R% P // Create the list of simulation actions. We put these in
0 P! p1 ]8 e& ~% ?' X0 S' W q // an action group, because we want these actions to be# z0 g D4 Q( I" j0 L% O$ S
// executed in a specific order, but these steps should/ v$ A' t1 ]# V, J/ U) y
// take no (simulated) time. The M(foo) means "The message, Y7 q. \$ L0 _& I% Y& c
// called <foo>". You can send a message To a particular. t1 H9 {8 D9 ~2 S/ | ]
// object, or ForEach object in a collection.& j- N, m: D: `
9 s1 r5 `3 [7 p" _# c0 ~
// Note we update the heatspace in two phases: first run
: O1 n* t7 I. [+ m8 |6 { // diffusion, then run "updateWorld" to actually enact the! E- W4 w* w8 _6 F8 U" n
// changes the heatbugs have made. The ordering here is
0 M1 b* w% S( q8 s // significant!
9 e2 _6 l8 J; ^4 {" ?& ?7 {& y ! L9 x1 | H3 }& v
// Note also, that with the additional( ?0 K y5 o+ n# z7 j$ \: w
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ j, {1 s# f& s* E& r // randomize the order in which the bugs actually run
# T9 V6 l* R5 a7 `% B' y* l // their step rule. This has the effect of removing any
/ z, |* V- a- u% ]6 f, { // systematic bias in the iteration throught the heatbug
2 E4 [) ?, h* F6 O" I1 u1 C# [ // list from timestep to timestep
2 H3 B" f* F: l* y8 W( b: ?* F
& K% a3 @ l/ ?8 d // By default, all `createActionForEach' modelActions have# ]# N0 h, k8 \
// a default order of `Sequential', which means that the7 [0 m. u1 a! l4 g# N
// order of iteration through the `heatbugList' will be& k5 d j' r- O
// identical (assuming the list order is not changed5 }3 j4 H+ M/ }! z& V
// indirectly by some other process).5 Y) e8 h5 \ g1 Z( u! E" S' k
) {: t8 C. X& b, J, [/ E1 f- U: j modelActions = new ActionGroupImpl (getZone ());8 z( H4 @3 H- d
. x9 {' z5 Q5 G( K$ G try {
0 m) `* m; I* l/ k6 y7 S N& _ modelActions.createActionTo$message" [5 ^' A C% _3 O
(heat, new Selector (heat.getClass (), "stepRule", false));
5 O" K8 g. e4 j. g; N } catch (Exception e) {
# |& S9 R k" s! H; L5 m( c, { System.err.println ("Exception stepRule: " + e.getMessage ());+ q4 i2 {/ v$ k6 L: m
}1 L; l0 k) Q' n/ s' u6 w j
5 P) E2 ^6 p2 z+ Z0 q; E0 w try {. J8 C$ f0 W' w# ?* t; Z
Heatbug proto = (Heatbug) heatbugList.get (0);8 v+ [7 O/ o/ I1 u9 a) M
Selector sel =
: R$ |5 }* H+ \ new Selector (proto.getClass (), "heatbugStep", false);
9 h7 Y9 F; W! @ actionForEach =3 d5 c, L2 m& X, v* \; a' ^
modelActions.createFActionForEachHomogeneous$call& E3 G" }( D9 j0 u8 K5 R$ H' @
(heatbugList,
3 [7 p; X) s# s) R, x; k5 o new FCallImpl (this, proto, sel,, `( I% G( Y) C5 z3 r
new FArgumentsImpl (this, sel)));! B4 s0 R. Y, [" R
} catch (Exception e) {% b+ D# |3 k, f( e' P( O
e.printStackTrace (System.err);5 Z! ^3 M# n. \( p6 L
}: r8 J- k3 P+ _2 _0 |
; m5 ^7 p* X4 I$ q8 [) ?
syncUpdateOrder ();
; w* I& n1 F3 y" I6 n
8 h7 n& z3 ]4 g, d; m w' ?3 ?/ [0 D) K try {
( @9 z7 |; R: m" u O9 m" v* {, } modelActions.createActionTo$message # m( }. Y: ]9 ^9 t, n
(heat, new Selector (heat.getClass (), "updateLattice", false));: b: P3 x, }( j( o/ g
} catch (Exception e) {5 P! G1 n) i9 \. G9 H$ C2 h6 e2 h
System.err.println("Exception updateLattice: " + e.getMessage ());
0 X* B. Y, e; i( n! G& s }; N4 {1 \. s) R5 q6 K% T7 L
& G( w' z p) x% h5 M1 F
// Then we create a schedule that executes the
7 U; ^! F% g% \$ z* [. y // modelActions. modelActions is an ActionGroup, by itself it# m1 z" F/ U% D
// has no notion of time. In order to have it executed in
# {. y1 V7 P4 |' w( H2 W& _& r, W // time, we create a Schedule that says to use the8 l: h7 D# k6 T# v2 j" ^& |
// modelActions ActionGroup at particular times. This
8 [$ @2 p; C# h5 @: I7 E3 E$ U7 s // schedule has a repeat interval of 1, it will loop every: T- Q2 ]' c, ~3 |
// time step. The action is executed at time 0 relative to) ]6 g& Y. j- n5 ?
// the beginning of the loop.. K# Q9 r! O& o# s S/ r( p
9 _& F e0 p+ f4 b, w# m# g. k) X // This is a simple schedule, with only one action that is
2 l) k' b+ O9 Z: Q6 a // just repeated every time. See jmousetrap for more e: ^- l/ O5 j E$ a' K0 C
// complicated schedules.
. {- l. U Q) ~7 s" w
/ K$ R% u J3 E- h* J* n( o+ j modelSchedule = new ScheduleImpl (getZone (), 1);
- i h, {- J0 _2 Y3 F5 d5 J. ? modelSchedule.at$createAction (0, modelActions); p0 {- k2 d% y
; b! \! |( @( X" i; P. L T return this;
! M1 \" t" _8 y5 g } |