HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. g$ P o% w/ B
" p& ?: ?% e+ y! ~. e/ D
public Object buildActions () {
0 z- | }+ d0 N) C super.buildActions();7 ], P& S& z( R7 a& v, ?+ j
8 A. L3 Y/ m6 K2 ~ // Create the list of simulation actions. We put these in+ B/ L r# k s
// an action group, because we want these actions to be2 p' f, G( c$ j/ T/ d
// executed in a specific order, but these steps should# ?5 V/ y: J) t9 p9 C
// take no (simulated) time. The M(foo) means "The message; ?0 F4 `4 B( s A* C% {' F
// called <foo>". You can send a message To a particular& T3 ^& m3 n* s3 t! Y9 A+ T
// object, or ForEach object in a collection.
: N$ D3 K6 M& S$ b8 |& i- u
) H5 }* H I( X# r! z // Note we update the heatspace in two phases: first run
! |& g7 u# C5 P2 A // diffusion, then run "updateWorld" to actually enact the
! [3 w8 j7 F. Y; K& X9 a# I // changes the heatbugs have made. The ordering here is. A/ A5 g/ o9 b1 q
// significant!3 y/ H8 o1 @7 v6 e4 Y* g$ K
% m$ g- T/ ~ v# g // Note also, that with the additional6 Y. w1 B: U7 r9 f: R# o' e: A [
// `randomizeHeatbugUpdateOrder' Boolean flag we can
' J& Q4 o- ?" R% M$ J // randomize the order in which the bugs actually run d& ?' E6 d, v1 }5 J
// their step rule. This has the effect of removing any% p( V: t8 [! M5 w/ W/ u
// systematic bias in the iteration throught the heatbug' V' J! D& r+ O3 S
// list from timestep to timestep
6 a8 D6 t* j7 r1 M
# r: p$ t! U: w+ I) k% ? // By default, all `createActionForEach' modelActions have
% e7 C* U$ j' G- |! C, M // a default order of `Sequential', which means that the) b0 X9 s, `) f7 {3 u& }1 b
// order of iteration through the `heatbugList' will be
1 ? }6 {. c5 P/ L! j& |4 P* \8 z9 }8 X // identical (assuming the list order is not changed% R" m- C1 d+ \9 s
// indirectly by some other process).
7 q- _3 q! r3 F8 k1 s" t! G
6 J( U: u- M! B/ b5 e! a8 o modelActions = new ActionGroupImpl (getZone ());
8 u! D8 M# B- v, B: S: ~1 t% a
; R8 F! ?9 @) S# L try {5 b5 |( g3 F8 {
modelActions.createActionTo$message
( P. h7 Y' l: w" }7 m" }( Q (heat, new Selector (heat.getClass (), "stepRule", false));) K0 g) `; T4 E' s/ u0 s2 Y
} catch (Exception e) {: f1 S: v. {" B
System.err.println ("Exception stepRule: " + e.getMessage ());8 t5 ^; F% t9 s8 w
}
: @) c$ w7 Y& j7 R& i. d S* D
+ i3 S3 c0 s3 y: Y T2 }9 A. N try {
+ G- Q( k+ _, k: x0 O/ B* } Heatbug proto = (Heatbug) heatbugList.get (0);- O5 y O5 o+ Q$ N% n; ` a% t9 t: I
Selector sel = , [1 w" _# P. B- g
new Selector (proto.getClass (), "heatbugStep", false);) M5 H/ G8 v8 Z! h7 a6 A: s7 a
actionForEach =% p7 i* Z2 h3 S5 ~2 }* W
modelActions.createFActionForEachHomogeneous$call
2 r. A0 }5 G) u (heatbugList,
& F, N, _" o" o5 k& z new FCallImpl (this, proto, sel,. C% @1 @% \4 t+ x+ i2 ^
new FArgumentsImpl (this, sel)));
* I( F1 P/ g0 z+ Y } catch (Exception e) {3 p. P7 e6 P9 J/ V4 K
e.printStackTrace (System.err);, H6 H" A- U7 Z% k, b' E
}
& M T. U P6 R. }- V- Q5 f( `! L* ` + x0 u4 l4 \) c
syncUpdateOrder ();
. Y, F# x2 J* x% H6 B0 R5 x" z. D9 K) o: k) x% B
try {. u$ C8 I: U/ m9 ]" F3 r( w4 e% \
modelActions.createActionTo$message
- M! ?* E4 s. g" S9 }: Z% u (heat, new Selector (heat.getClass (), "updateLattice", false));% s) F1 N: o2 q1 ]* z
} catch (Exception e) {2 l3 I+ f$ H+ ~8 I$ ]
System.err.println("Exception updateLattice: " + e.getMessage ());- |' n7 I2 W; D v
}, T# b. z) m; i# b; V6 h
1 H7 Z1 A6 r& [3 f5 K8 _ // Then we create a schedule that executes the
0 Y* j E; b" F& L# U9 D+ C // modelActions. modelActions is an ActionGroup, by itself it! g8 T; ^2 L2 R) q" {2 ~
// has no notion of time. In order to have it executed in( [- A5 }# L" B- x5 H3 q2 r
// time, we create a Schedule that says to use the/ a- P) ] E0 W' s. m6 r9 r
// modelActions ActionGroup at particular times. This, ^& B! t5 R7 \( a4 d* _1 ~# c
// schedule has a repeat interval of 1, it will loop every
4 ~9 W" U3 ~1 j Y0 `8 g // time step. The action is executed at time 0 relative to0 `4 Q7 @$ O. B) I" ^
// the beginning of the loop." k5 O* w( K1 p7 g
& H$ U% u1 ?5 ]
// This is a simple schedule, with only one action that is9 M# n* e* t, [: G- f0 \, M
// just repeated every time. See jmousetrap for more% F, q% j6 l$ u' y- C' L' O2 P6 R
// complicated schedules." g- d& @- H6 S: D$ q1 G( E
3 O) w/ p; n8 l: Y) ?( e' C modelSchedule = new ScheduleImpl (getZone (), 1);
/ v. G9 l! Y& s% ~4 A" f modelSchedule.at$createAction (0, modelActions);) p6 S4 J& q( ]$ [8 f
6 H. F3 T1 N( ^7 E$ V# C; d9 H
return this;
6 ]+ Z' a3 ~) ?* G/ G } |