HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ d- Z; X4 z- `$ B3 U. S- _; t+ ]; T
7 c5 j* W6 W- g' k' ] public Object buildActions () {
' T- t3 c; c! s& U! K super.buildActions();
3 o+ Q6 B" C, e1 Y9 g, ~/ a) Q 1 t. m7 J4 m& j% \
// Create the list of simulation actions. We put these in
& i( R* |# Y3 m6 f* Q5 m // an action group, because we want these actions to be
% [. {0 U% M- O // executed in a specific order, but these steps should' O# g, ^# V" e7 m" V7 ~
// take no (simulated) time. The M(foo) means "The message0 q5 b, T% z3 a! o8 z. f
// called <foo>". You can send a message To a particular
7 N& Q7 j# @2 ~6 E# y$ r // object, or ForEach object in a collection.
1 V( ?" d% ^$ k 0 k1 |1 H/ ]) E C) C c: z
// Note we update the heatspace in two phases: first run3 w) m S8 K! R- f8 A* P n! ?8 r% D
// diffusion, then run "updateWorld" to actually enact the, T+ D% L0 _0 c$ E6 T- p
// changes the heatbugs have made. The ordering here is& a8 s/ K& |+ P! Y0 q0 f3 i! q7 ? [
// significant!
- I, \6 @, l% M2 k; k4 e- t 1 x' D5 i5 t9 A' O3 l4 D, S
// Note also, that with the additional2 k- l2 q) Z, K# c
// `randomizeHeatbugUpdateOrder' Boolean flag we can
: c( u$ H# M( J/ | i // randomize the order in which the bugs actually run
! _9 S) g+ {3 l2 M/ i; L' H8 Q // their step rule. This has the effect of removing any
3 ^ y9 E5 X o3 Y/ F. i // systematic bias in the iteration throught the heatbug
+ ^6 H0 m4 O2 }, {% l% U% z // list from timestep to timestep* h& Z6 k- i8 ~) J. W" ~
: ?8 W0 K! m1 w5 G: m! d
// By default, all `createActionForEach' modelActions have
0 O, o1 i8 d% [ // a default order of `Sequential', which means that the( U& ?" U( `" U6 t7 M$ q4 ]
// order of iteration through the `heatbugList' will be1 Z! H9 ~+ X: u/ T. p
// identical (assuming the list order is not changed. R/ n$ D+ k# R/ u: { ~3 M1 n7 ?
// indirectly by some other process).
: a$ \0 E* m' [; k
% p0 I% a! w" E0 r( _ modelActions = new ActionGroupImpl (getZone ());
# S) ]. c0 m" U( V- p
" U8 }" M; j3 n1 f9 ], Z try {2 L, g: s- Q' ^) R) e
modelActions.createActionTo$message: ~* Q# G2 h! p4 k2 v
(heat, new Selector (heat.getClass (), "stepRule", false));9 ?& S) C3 X3 h
} catch (Exception e) {
5 ^9 c, I6 x B, u/ b System.err.println ("Exception stepRule: " + e.getMessage ());" Z$ R: J' N7 _/ T9 ?
}2 e P" _- }7 h G6 Y0 |
7 \7 e3 C# ?! W' G" o0 l
try {
- v$ ^& \+ h( [" B4 [' y Heatbug proto = (Heatbug) heatbugList.get (0);7 R/ [) P' C2 o2 a, L8 z
Selector sel =
2 Q' w& ?9 X3 ]6 _ new Selector (proto.getClass (), "heatbugStep", false);0 b5 y; G/ N% ~+ v' z |% X
actionForEach =
' W) e+ G/ p- K. I4 g modelActions.createFActionForEachHomogeneous$call
1 Y) M" M3 H0 P* k- P (heatbugList,' W' I6 d3 y; }/ E: b; Q: S
new FCallImpl (this, proto, sel,
* ]1 l# Y* }6 z1 ^8 r new FArgumentsImpl (this, sel)));
: U1 h( ?! \& d+ o } catch (Exception e) {% C; d# F% X" M2 f9 b
e.printStackTrace (System.err);% s6 J$ ]- s T5 j+ L$ n$ m
}
$ s; _: o& z& a* f+ U& u( K $ X, B7 _4 o7 z- W
syncUpdateOrder ();' g: x% _% ^/ F; O
# [: V/ `( ^ v. [9 Y1 j
try {
& o, F$ k9 e( S- l P; g, p modelActions.createActionTo$message
$ K+ E9 J1 }7 s, x1 a (heat, new Selector (heat.getClass (), "updateLattice", false));
" E: v4 _( a# Z2 @% O } catch (Exception e) {
. O9 R* e* A0 v. r' g System.err.println("Exception updateLattice: " + e.getMessage ());5 g, ^2 z2 K! {
}, m5 ~( s' ^0 T3 |
% u! F8 N; F8 ~& Y) H7 y // Then we create a schedule that executes the r7 B" E. {' N
// modelActions. modelActions is an ActionGroup, by itself it
9 J$ t# m& p4 C* V$ c+ x% o9 c% F // has no notion of time. In order to have it executed in
) B6 D5 D9 @$ x1 r s) Q5 g // time, we create a Schedule that says to use the
# B3 q, p3 R7 S" s // modelActions ActionGroup at particular times. This
# B1 m4 Y1 @5 n+ Z7 a) I9 X C // schedule has a repeat interval of 1, it will loop every
/ Q. B* l0 u% G4 Z( h0 r8 i // time step. The action is executed at time 0 relative to
. n3 ^: H/ G: ], B# a/ ]" A // the beginning of the loop.
. F2 @, S# d! l" d8 ~" b
2 W5 W4 M2 x. d' k( a // This is a simple schedule, with only one action that is
4 Q' G" G j. ~) F& @: V$ G // just repeated every time. See jmousetrap for more
# `. D+ I9 x$ P1 T* z! n // complicated schedules.
7 C+ b* I5 C9 I: L2 ?/ n
J+ h- X2 `* j# R* g modelSchedule = new ScheduleImpl (getZone (), 1);. z; g2 O5 l$ `: R' p
modelSchedule.at$createAction (0, modelActions);* J& c8 \9 D+ V( c! L+ { q2 {
- J% _2 `) _: w) ^. S" a
return this;
. a+ ?3 h8 P+ j } |