HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 e1 x0 ?: f$ u1 v5 S- c/ W
# w& L; u5 F; r# L public Object buildActions () {7 Q8 [) Y! n, k- k4 {- s5 H
super.buildActions();
- s5 J; }3 g0 R4 d9 h 1 w; a6 ~; i. j& T( k4 k1 Z
// Create the list of simulation actions. We put these in
2 N- E% [/ q# G% a9 L. ? // an action group, because we want these actions to be
8 `0 f) N B5 C' W. v // executed in a specific order, but these steps should/ q3 ?8 }, C1 T4 Y5 t& c
// take no (simulated) time. The M(foo) means "The message, @. i- {8 j w2 h, q
// called <foo>". You can send a message To a particular
6 d! ~( L& J2 ]* c // object, or ForEach object in a collection./ O4 \+ N0 U6 x7 z6 K
$ ^9 z' q- M" p* R- b$ v // Note we update the heatspace in two phases: first run
2 Z9 n7 K* Y4 i7 }8 f& | // diffusion, then run "updateWorld" to actually enact the
' S3 D2 T `# V, ?( M4 s& ^2 E3 o5 R // changes the heatbugs have made. The ordering here is
* C/ l2 L- f* J5 ^/ E. s // significant!
' u1 p3 ?% |6 K% U7 j9 E) K C/ r7 r Y % z' f. s v; A# B; o7 ~
// Note also, that with the additional. A# X" I7 x9 f; n( y* w) C
// `randomizeHeatbugUpdateOrder' Boolean flag we can
D# |! F7 ^& ~8 T5 @/ H // randomize the order in which the bugs actually run
+ H6 t8 x% L1 N( [& D" O( ? // their step rule. This has the effect of removing any* o* n! I/ E/ t" L
// systematic bias in the iteration throught the heatbug- V4 y5 Q% P; x1 B
// list from timestep to timestep7 W5 r# ?! ?4 ?$ _( h
( `3 Q% h7 a' Q7 i) T // By default, all `createActionForEach' modelActions have* b5 }& h, R9 l+ @& z
// a default order of `Sequential', which means that the8 x N% n' D0 ^1 e% |7 {3 C3 X, D
// order of iteration through the `heatbugList' will be2 h$ T3 ?7 Q- o1 H8 L! h
// identical (assuming the list order is not changed
7 B W1 [9 k! r% s // indirectly by some other process).
% |0 F6 W5 W3 p* l& U
) }$ d6 E& D# k# \ modelActions = new ActionGroupImpl (getZone ());/ p3 m z3 m$ l0 M# l, a, w
6 F2 ^* y/ f$ b& ~+ ]8 G try {
/ b3 f+ g: x8 J w, k7 } modelActions.createActionTo$message/ F) |! \6 h+ J2 I( c [
(heat, new Selector (heat.getClass (), "stepRule", false));2 _' t" ?4 F4 k/ t& X
} catch (Exception e) {
7 g/ f6 c$ |7 Z4 J4 e- J System.err.println ("Exception stepRule: " + e.getMessage ());
' L; X: @7 k k9 S! e- u X* N9 ^ }
# @$ F/ L/ b; j$ |$ h# g0 ?2 D& [# e2 X" _) w3 j: {
try {
) A3 v9 l% r5 d" [2 X/ S9 V Heatbug proto = (Heatbug) heatbugList.get (0);' D6 p5 G4 G% r8 B/ w) {
Selector sel = ) [* _7 `2 E; \# q. K
new Selector (proto.getClass (), "heatbugStep", false);' K7 w7 _' I- U! U9 r9 F( n
actionForEach =
9 ^& t7 S0 Q. p8 f- e4 } modelActions.createFActionForEachHomogeneous$call
- k2 e7 c, k+ q! D5 D% v- b (heatbugList,# _' n+ d* p4 y M) W4 b) p3 g5 x
new FCallImpl (this, proto, sel,* v" g6 P# X4 e
new FArgumentsImpl (this, sel)));
' [# u4 C4 @/ G. b. F } catch (Exception e) {
( ]: r3 G$ O) {0 J+ Z8 L" O4 a2 @, B- O e.printStackTrace (System.err);+ A- h& O3 ]9 `4 e5 B
}
9 `# z" B9 n5 g' l3 L2 P * M" v! O1 i, ^ \1 Z, o/ J. R
syncUpdateOrder ();
E2 s8 \( o- F* z
* S5 w j! p( i& x; f1 |0 b" ` try {
2 d: g& C) B4 c8 k3 \( v modelActions.createActionTo$message ! ]* Y; m! I8 [! @0 j
(heat, new Selector (heat.getClass (), "updateLattice", false));
1 L" I( j( R, Q% X } catch (Exception e) {
+ @* {# H% ~- g: y, d System.err.println("Exception updateLattice: " + e.getMessage ());
4 P1 U" j* `( u7 S }
3 o$ q! e8 m. E+ R # \: L6 Y! v8 Z2 g& ?: w
// Then we create a schedule that executes the
, `* g9 D: t) m // modelActions. modelActions is an ActionGroup, by itself it
8 X. f0 y) J% t- ? // has no notion of time. In order to have it executed in
R9 ^ g; x* \; M, g* f" [+ Y6 d // time, we create a Schedule that says to use the
9 A! z1 ]5 e+ U Z // modelActions ActionGroup at particular times. This: X7 V. q+ W9 k9 H0 \
// schedule has a repeat interval of 1, it will loop every
3 ^% {1 a8 j' t6 ~/ s // time step. The action is executed at time 0 relative to
! {' w4 B0 v! B0 _1 k8 o. [ // the beginning of the loop.0 ]4 x/ j Y/ {8 u' V+ l
- p' x6 q/ L! N' x* H
// This is a simple schedule, with only one action that is3 _! K) }# ^- Y$ R' d$ O, z
// just repeated every time. See jmousetrap for more" d% |) K! ]* E8 t% h1 k) z |
// complicated schedules.0 [- y/ K V6 \& t+ _, B
9 t" t. K( ?9 _% K' X modelSchedule = new ScheduleImpl (getZone (), 1);* N5 ~7 z: n9 J& H
modelSchedule.at$createAction (0, modelActions);$ P8 R' I# m" N- B5 Y
3 h1 y( x0 Z( ]( ?- j4 k' d' ]( Q return this;
0 a& R+ t4 [& P! o2 ]; P1 E! s( Y } |