HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. x- l% V! Z# `# D% [
t+ y3 m# U4 A
public Object buildActions () {: h' K0 d1 ~0 g6 M* w
super.buildActions();
6 o/ y% i. s8 N0 R. P/ }
- ~4 @1 P5 a, a& ^3 J // Create the list of simulation actions. We put these in
+ g: p$ I5 o$ B" D# C4 t // an action group, because we want these actions to be% W5 O, @3 n7 I( C
// executed in a specific order, but these steps should
9 S' O8 u3 k( @! M0 J1 h p // take no (simulated) time. The M(foo) means "The message
d M: ^5 l1 i' e7 g- y( b // called <foo>". You can send a message To a particular
) y& e+ ?- }6 K+ p h0 [. \6 R! h // object, or ForEach object in a collection.% O4 H, x$ w' U6 V1 { L% t1 J
1 k& A" `8 }/ P // Note we update the heatspace in two phases: first run
/ t2 Q1 {0 n4 ^7 R' K, W // diffusion, then run "updateWorld" to actually enact the* a; b8 S4 b e$ X9 \" t# b
// changes the heatbugs have made. The ordering here is
& l$ ]6 j, B' G9 a) A$ u. Z5 W3 u# Z // significant!; X/ H, e: ^, f. B7 l' }7 N
% g7 J& k5 v, d" e8 @* {) w
// Note also, that with the additional& Y; ]+ {7 f) Z; t9 _
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* k% e) e H' }8 f2 e" w7 E // randomize the order in which the bugs actually run
. U, y4 N$ d/ M! `: J // their step rule. This has the effect of removing any! S& m; k o; A& O$ |
// systematic bias in the iteration throught the heatbug2 b+ g/ p/ E: v1 K! C# v
// list from timestep to timestep* X4 i: J$ G3 _# Z3 B" s
; d' l& j- l$ H1 ?. { // By default, all `createActionForEach' modelActions have
+ P. r2 F4 T* B2 S1 V+ w // a default order of `Sequential', which means that the: w9 F* S' r p; z- S
// order of iteration through the `heatbugList' will be+ b& i: R9 Q, J' f7 j5 r* y
// identical (assuming the list order is not changed9 V& ?% y8 w% T& Q# N
// indirectly by some other process).# O/ e2 H. D: d4 N* g" t& w- y
( M7 [! P& l& y0 N* A
modelActions = new ActionGroupImpl (getZone ());) G. h+ c. q0 _+ v8 j( R- p
" R* z. {9 Y% r: L j2 _ try {
5 P; m# h' B+ F3 z modelActions.createActionTo$message
4 h1 m, x4 p5 [. g) a (heat, new Selector (heat.getClass (), "stepRule", false));8 P4 t* y. g+ s8 g5 W
} catch (Exception e) {
' Z Z' X, z9 b8 t; `4 \* p System.err.println ("Exception stepRule: " + e.getMessage ());1 ]. J- u* ?# h3 |, |* R1 E
}
' S* {3 I7 y0 M0 S0 `+ k3 x
2 L7 b3 c( l2 }% |; m# S try {
7 T1 T& }8 c2 j' [ Heatbug proto = (Heatbug) heatbugList.get (0);
( w! v4 w! e8 E' g; K/ W Selector sel =
Q' ^ d2 E5 ^& m; D- J6 e6 q+ i new Selector (proto.getClass (), "heatbugStep", false);
8 }7 h3 Z7 t ]/ u! ^ actionForEach =8 \% |% e. h" l; Q! O! |. K; }( n9 X
modelActions.createFActionForEachHomogeneous$call; ^1 B' u2 d6 Y- ?- J
(heatbugList,2 h1 M% P2 w t8 B% v. W# H
new FCallImpl (this, proto, sel,
+ a4 |* K- o, i# h new FArgumentsImpl (this, sel)));
4 ?0 X) l, P! W+ q6 P' b1 V } catch (Exception e) {
: N0 L$ w& o% M, ?8 W, O2 E! T/ ] e.printStackTrace (System.err);
8 l% Z9 n; u# ^/ ]9 y& k% N' G }
1 x: w8 X7 p. k/ j$ G) E( X! T+ Z 6 D; `1 t# s, `, p8 [) V& l3 F, O! Y
syncUpdateOrder ();5 B$ |/ c/ D `) \/ e) \# m
* H h8 {! G ^- t8 J$ n try {
' j# W+ E; J- ^* X; s' a, ? modelActions.createActionTo$message ( {# S* w4 j& J+ W+ H( v$ b/ i) j
(heat, new Selector (heat.getClass (), "updateLattice", false));
, n2 M! y% l7 @$ S5 k } catch (Exception e) {
0 m# K+ P- {) {! h* e System.err.println("Exception updateLattice: " + e.getMessage ());) A! ~( j* N# m4 N" \
}
# F0 t3 p- e B9 ?1 I ) N5 A. E& {2 }+ k
// Then we create a schedule that executes the( {7 z8 C2 K7 n% p' ^% h; o
// modelActions. modelActions is an ActionGroup, by itself it- Y2 x* A K5 [$ b4 z# I# k
// has no notion of time. In order to have it executed in
1 X2 L# @" v! D E // time, we create a Schedule that says to use the
9 a$ F/ s* ^# |8 r // modelActions ActionGroup at particular times. This4 U$ w' V" e: b) t. ?0 ~0 @
// schedule has a repeat interval of 1, it will loop every; m- I3 x! \5 b7 x1 i: i z; K
// time step. The action is executed at time 0 relative to2 W3 }5 k3 c( {" s5 w) T
// the beginning of the loop.1 Q& t+ _0 p9 ~. U
9 c5 `" Q* k! c, g6 h/ k t1 F // This is a simple schedule, with only one action that is* t* u, a3 B' v/ P, E
// just repeated every time. See jmousetrap for more
- d- S C4 Z9 @ // complicated schedules.9 p' S) ]! O, ]5 s% j
" X8 o( u( E3 \7 ^ modelSchedule = new ScheduleImpl (getZone (), 1);, x$ j5 t) V5 m: m$ i- D7 C1 y
modelSchedule.at$createAction (0, modelActions);% q0 \/ |$ [& I+ ^) H @- m; m s# Y
- J1 @- v+ d+ i1 e- i- L' r return this;
$ i r& Q. m7 f7 K$ ~ } |