HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( g; A6 S' c8 m, G$ X
! p. l9 x4 Y5 n$ p4 B) }/ {+ U
public Object buildActions () {
& o4 v. q/ r! w$ ^+ g( Y# d- ] super.buildActions();
9 M+ A% ^& D2 O9 N( n4 [
/ j' P! J! D- ]- U9 ?% H // Create the list of simulation actions. We put these in
2 Z9 v1 M* ]$ [8 `& ~ x // an action group, because we want these actions to be* ?" C* I" `: f& ?6 o
// executed in a specific order, but these steps should
. E# Q! ?+ C9 g- T/ b // take no (simulated) time. The M(foo) means "The message9 G% ^7 r' V N7 ^: L! M
// called <foo>". You can send a message To a particular% X6 O$ x# ?* |9 o
// object, or ForEach object in a collection.
8 F. h3 S6 G) J3 A1 ]) `# D
4 b9 f$ E% r0 a // Note we update the heatspace in two phases: first run+ Q2 `, V1 X" W; h
// diffusion, then run "updateWorld" to actually enact the% ?& B7 I( J( y: ~
// changes the heatbugs have made. The ordering here is
- V) |$ e. D& y/ g* h // significant!0 w* z+ [( O2 V+ d) g7 e4 B( f1 C
2 m4 u; W) t- S \3 }0 a
// Note also, that with the additional ?! G1 v& r) M, z
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ t5 P7 a+ i9 v- J
// randomize the order in which the bugs actually run5 j4 X: [) b9 W \: X
// their step rule. This has the effect of removing any
3 ]) j3 J- N, M8 }; f, l2 {! s // systematic bias in the iteration throught the heatbug- v5 x6 [- ~0 m; X
// list from timestep to timestep' G3 e9 L6 P; x+ s1 n
! j. i2 X: S1 }" ]
// By default, all `createActionForEach' modelActions have
. r* H+ g$ i/ _" O# A5 c // a default order of `Sequential', which means that the0 f2 |1 b( I+ V5 _& `1 k
// order of iteration through the `heatbugList' will be
2 Z, {& s* Y( J/ w2 p+ M# h i // identical (assuming the list order is not changed# v Q2 `1 `7 Z7 J0 M; F
// indirectly by some other process).
" w3 z5 i. |% q7 w' v ! P, G" u3 e1 O( r2 I4 v1 }, \! h
modelActions = new ActionGroupImpl (getZone ());
2 T9 |( d- v6 }! c' @ E6 m9 E6 i% p8 ~
try {
/ T$ M& p; E$ T6 n6 @ _ modelActions.createActionTo$message
3 F- S; ~+ A1 ^$ m* E' f (heat, new Selector (heat.getClass (), "stepRule", false));
4 y. |/ y7 \: M. o ]# x3 y } catch (Exception e) {( j' A4 t' {& Q' z, P
System.err.println ("Exception stepRule: " + e.getMessage ());
2 |9 S) P. s4 ], v }
+ l0 ~4 w0 k4 g; n1 R3 }" k! A. K% O' e9 F0 P8 B+ p; T0 [
try {
& H7 Z0 }# j) H/ z& `8 K; ?5 C Heatbug proto = (Heatbug) heatbugList.get (0);
6 |2 w+ u% ^" ^" E6 o8 j Selector sel =
4 R4 M: z7 e8 b* F/ |. y- K new Selector (proto.getClass (), "heatbugStep", false);
& L) d4 L0 K& I0 V4 t% G actionForEach =
' a8 J8 I' [( B2 k/ P5 j6 m modelActions.createFActionForEachHomogeneous$call
* W" o; r4 J2 D& V! U (heatbugList,
5 u) v3 z* C: W) L4 J4 r; M. F& c+ C new FCallImpl (this, proto, sel,
4 t1 k2 X: H9 d# x4 K( \$ x new FArgumentsImpl (this, sel)));# F' I! k. Y/ b. l: C
} catch (Exception e) {
1 \" w; L$ G" c) @ e.printStackTrace (System.err);
- w- d d8 u7 |. j [- }4 ^ }4 i5 T2 i3 n+ R% j7 F9 w$ V. v
7 V9 g [/ d( A# a5 h syncUpdateOrder ();
% r" O& C/ I- y, ^$ s: L, Z# R: [3 E; x
& L' B. Q* P/ w7 v try {" y* i# _. a4 _. q6 F' F
modelActions.createActionTo$message & b; O1 G; M) |: d. O5 b
(heat, new Selector (heat.getClass (), "updateLattice", false));
( f: O7 S5 x7 w5 J+ } } catch (Exception e) {
7 L8 k8 b; S5 G2 `) N System.err.println("Exception updateLattice: " + e.getMessage ());5 Q& L7 v8 Y* n0 _
}
( ]9 z, d+ s. `9 u; L: l F- d 9 f9 o$ @6 b; b: j& D
// Then we create a schedule that executes the1 p4 J1 \5 O; x# @
// modelActions. modelActions is an ActionGroup, by itself it$ Z* h1 K- U. M
// has no notion of time. In order to have it executed in, s$ j2 \8 A' s) f
// time, we create a Schedule that says to use the3 |, o, F/ N ?
// modelActions ActionGroup at particular times. This: I+ w( L, O ^! H: ~6 m8 Y
// schedule has a repeat interval of 1, it will loop every
3 ~) s1 s9 b& R/ Q. @* L0 j // time step. The action is executed at time 0 relative to
) p( H* i! v U% b1 i7 r // the beginning of the loop.
9 C7 T* L8 d! X& ]
f. t( j+ I9 e4 y* r2 X; W // This is a simple schedule, with only one action that is
& x& k0 e" f/ G: `7 @8 `& [ // just repeated every time. See jmousetrap for more
7 ^7 {+ l" I/ N& d // complicated schedules.
8 O+ h+ \ e0 u" X2 f6 P * Z) Y% y8 F: @' t# ~8 }
modelSchedule = new ScheduleImpl (getZone (), 1);8 i- a2 D& ~$ z1 r+ h$ F. F
modelSchedule.at$createAction (0, modelActions);
0 u# C8 i7 T: @$ u/ U9 {
/ \" X$ ~; a$ x1 j4 v2 f3 L& c return this;
0 x1 j1 A Y: i# e' F' b! U } |