HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# g; ]4 b! I, d8 D1 D+ `, G. r, x! k3 s- s- Q- ^3 v$ y/ J
public Object buildActions () {6 ]. Y/ f. l8 N- _( C4 U
super.buildActions();: N6 p8 }) E! N4 j. C
( \4 @5 o' q6 }
// Create the list of simulation actions. We put these in" w4 ?3 B% S% i% ?$ v
// an action group, because we want these actions to be
2 v3 f+ j3 r! {9 T) e4 } // executed in a specific order, but these steps should
+ j* |( k% K) ] // take no (simulated) time. The M(foo) means "The message* U* e; L6 }2 ]
// called <foo>". You can send a message To a particular
4 C- E4 k6 c' d' [- ]2 b // object, or ForEach object in a collection." M3 c$ d6 O! G- O, T/ p
: A' M: X( \! W; R1 E3 [. n* ? // Note we update the heatspace in two phases: first run
( z3 w y: _, }8 }! W9 a, s8 o // diffusion, then run "updateWorld" to actually enact the) i7 S7 ~/ y8 |: q' U2 V3 \
// changes the heatbugs have made. The ordering here is6 k! } M+ ~( Q$ w. \
// significant!* Y- \ F+ ~" v V" ^# H+ Y- j
. }5 b; Z, T! F& s; L& f! T // Note also, that with the additional4 G# d: m3 G4 S4 Y6 R; a% I, p: p# \
// `randomizeHeatbugUpdateOrder' Boolean flag we can
0 g; ?' ?/ ?% ] // randomize the order in which the bugs actually run
4 Z$ m) |$ T* E, o2 d // their step rule. This has the effect of removing any, w n# p' Q, U2 L+ e$ C
// systematic bias in the iteration throught the heatbug
) S4 o, C) A7 K4 A8 H // list from timestep to timestep
. o; t7 N3 W, a5 b. c. A9 M
# ~" O, s$ W8 \6 |% y! ?# I8 [ // By default, all `createActionForEach' modelActions have
/ Z% W9 O5 Z( b& ^7 N // a default order of `Sequential', which means that the% b! ?3 c+ g6 s0 o+ J1 u
// order of iteration through the `heatbugList' will be
: L& p) Y( p4 X: t3 Z% P2 ? // identical (assuming the list order is not changed
8 {1 R. B, w8 b5 W8 F% U! N! Z // indirectly by some other process).
, l5 h' D: O5 z. }
1 B$ x4 N7 ]3 ~4 b$ m7 n: J4 R* z$ s3 ~ modelActions = new ActionGroupImpl (getZone ());! z3 s4 }: ]* Q
, v1 d u0 l& u, X
try {
' ?8 G0 ]% G; u" \2 N modelActions.createActionTo$message
3 A0 V" T; z! d, d u6 ]# | (heat, new Selector (heat.getClass (), "stepRule", false));/ F5 A5 _0 A! n* Y2 n
} catch (Exception e) {- h8 A# A5 O0 P$ A: O; B+ M
System.err.println ("Exception stepRule: " + e.getMessage ()); m! ]5 ]4 ^" C Z( i9 m& J
}
E0 k4 L- x2 h T( t7 m ^" ~+ x8 u& T
try {5 K. J, o3 P+ U; `& o" \) u
Heatbug proto = (Heatbug) heatbugList.get (0); o; j* ^! r# m+ |, E: ]
Selector sel = ' J/ A$ @) s+ I* Y3 J+ d t+ G
new Selector (proto.getClass (), "heatbugStep", false);- G; o8 i3 k$ C6 {* Q) r' y' T
actionForEach =
$ R$ M, O" Z! h3 n3 G6 T( L. [/ ] modelActions.createFActionForEachHomogeneous$call. j7 I# b: Q- \# u/ n3 t4 p6 q
(heatbugList,
5 u0 h8 f2 z: } new FCallImpl (this, proto, sel,
! t+ V E* y/ B! }' y! b0 z2 E new FArgumentsImpl (this, sel)));
0 @$ _# Y% t |2 j } catch (Exception e) {9 B. E4 W7 o2 |/ m
e.printStackTrace (System.err);
8 }6 C( p. z& V# n& U }) b: X$ Y$ z: Y8 n+ a
9 M# @! A |+ e& c+ s syncUpdateOrder ();
' f: O* w* w2 C9 j- r1 b1 V( J5 n( N) p/ M( E( X: N9 s# D
try {) q+ O" K0 V1 _8 h' Z& F6 W9 X
modelActions.createActionTo$message
( p3 I% C! b1 s& T (heat, new Selector (heat.getClass (), "updateLattice", false));+ _5 B- `" p- c e9 O' ^0 q
} catch (Exception e) {
3 |, R) B; v G' {& T. e System.err.println("Exception updateLattice: " + e.getMessage ());
" E$ L: }" Q$ M- x1 f4 t }& [1 S% q. m, i) U+ U: R. d
E- s: f1 U. G9 c // Then we create a schedule that executes the6 ~$ W$ w+ {" V5 m; G3 Q. m8 x/ ^
// modelActions. modelActions is an ActionGroup, by itself it
$ |8 [9 j( T1 R, d // has no notion of time. In order to have it executed in Q' g0 o: A& y. p" |) E
// time, we create a Schedule that says to use the4 `( r0 Y) F; t0 y6 i' s
// modelActions ActionGroup at particular times. This& d+ X9 u2 W2 A6 K, t% v
// schedule has a repeat interval of 1, it will loop every9 [! G& T% X" O+ D s9 q
// time step. The action is executed at time 0 relative to+ s. p J+ b, r
// the beginning of the loop.
! z5 ]- t1 Y/ O7 M3 ?1 I9 y' p# T4 ~, {- l' ^% N
// This is a simple schedule, with only one action that is1 f. f, Q5 S4 u% U2 ~: {
// just repeated every time. See jmousetrap for more* b, { ]3 v* ~& {1 R$ Q0 P
// complicated schedules.
3 ~: }2 o P0 d% K: S
. ?; g0 O/ D% F' ~( v0 Z modelSchedule = new ScheduleImpl (getZone (), 1);
% l0 C' n2 R2 t! t modelSchedule.at$createAction (0, modelActions);
- K L- T& M) a x- r3 Z# d . x' u1 [8 W" w# U6 O/ L1 R
return this;$ O: Q( l$ T1 b2 C! q, K$ n
} |