HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) Y, W5 F* }- I4 F4 s% G
+ k+ V0 @% r% o public Object buildActions () {
; H- d3 g$ R9 X) }6 _& O. a/ d super.buildActions();
& E! N" g7 K0 y) j5 J
: O; x1 |% V6 J8 \& B# @# X0 ] // Create the list of simulation actions. We put these in9 w( l9 u w. X$ @, Z# l
// an action group, because we want these actions to be: c7 }7 u, x4 U# P* p0 o. D
// executed in a specific order, but these steps should- r( z* k1 Q% [1 n5 i
// take no (simulated) time. The M(foo) means "The message& {; k. z5 a+ g* g
// called <foo>". You can send a message To a particular
* f. t+ X3 M) f" o8 W# b3 l* _) k // object, or ForEach object in a collection.
4 \- W4 e" ?# @7 I$ ~2 f5 d7 U 2 F. {& E( e, `$ g
// Note we update the heatspace in two phases: first run+ o$ o6 L: Q* b) b) Y6 b
// diffusion, then run "updateWorld" to actually enact the# K0 T7 u1 R7 ?) N1 X( ]
// changes the heatbugs have made. The ordering here is
I4 D/ Q6 @, V5 y2 `" m" n // significant!
( U, p9 @; A0 C* n/ Q- j" U& K$ c- I
8 b2 u: F4 |: u$ O) c // Note also, that with the additional
; q. I* W& w( b. U' D% x1 ` // `randomizeHeatbugUpdateOrder' Boolean flag we can" E, l$ _$ T( a8 X
// randomize the order in which the bugs actually run
5 K% Z% F$ g, ` // their step rule. This has the effect of removing any! k" H9 z* d9 u0 m# A. \
// systematic bias in the iteration throught the heatbug
. y, G: _) P# s k/ x* v // list from timestep to timestep
6 Z6 V/ [) F. M* y4 w3 B7 T# Q
/ f8 y$ h/ ]7 T9 k9 v // By default, all `createActionForEach' modelActions have9 U) w) _9 L, o* Z4 X
// a default order of `Sequential', which means that the5 P$ _) O* }- u0 ~9 ?& E
// order of iteration through the `heatbugList' will be: N" ~ x9 P7 o" s# `$ D, Q2 C
// identical (assuming the list order is not changed
1 q/ [; c1 x3 i U' { [; X" W // indirectly by some other process).
1 \/ V6 `* g( H5 R3 s3 `+ |0 y , x% e: M. I5 T$ g1 {" K
modelActions = new ActionGroupImpl (getZone ());
. y; h4 Y7 Z/ O! q8 c5 s; w* C6 @7 y5 j2 ~' X) s
try {
/ R+ P$ _5 V9 b- s V* ] modelActions.createActionTo$message
+ s! m& D/ H$ J& F% t2 {7 |% ]! D (heat, new Selector (heat.getClass (), "stepRule", false));: w% f0 n/ |" A
} catch (Exception e) {9 F( @4 e# p( b9 I& l
System.err.println ("Exception stepRule: " + e.getMessage ());
+ A3 I( I$ Y( H }
1 r4 c' j# S8 y h8 A2 x+ L8 y" k4 K8 H5 M" d1 k$ g
try {3 {0 i. M2 y' _ \( u
Heatbug proto = (Heatbug) heatbugList.get (0);
L, d& f4 t A; l0 |/ V8 A: x0 S# K% Z Selector sel = % W& h2 V6 H* N% Q- Q
new Selector (proto.getClass (), "heatbugStep", false);
; t* T7 Q7 K" o: L: j actionForEach =
% _- P- ?5 q* T5 I% G modelActions.createFActionForEachHomogeneous$call
+ z5 Q6 |8 Z% ~) L. Q- ~+ d0 @ (heatbugList," U% B& w, B! c/ K" k1 R
new FCallImpl (this, proto, sel,- L: C0 y0 x- ~" H V* u# t
new FArgumentsImpl (this, sel)));
2 B8 W7 H3 z) e) b9 i/ F% ~ } catch (Exception e) {" Y) S3 d1 C$ k4 b$ g$ b1 K
e.printStackTrace (System.err);
; l5 }- ^' q6 C* e }! Q6 s) S( @/ M& I1 Q! b1 M% W( f
3 R0 J& c, |3 B/ J3 Y! K8 L: E
syncUpdateOrder ();8 p0 F a, f$ R* B
/ u) p7 O4 Q- U try {" I4 F2 V3 ?5 E1 R) |8 k0 R- U
modelActions.createActionTo$message 3 L% U O$ T/ }# ?, O
(heat, new Selector (heat.getClass (), "updateLattice", false));/ ]; z: x& s( b' Q( {
} catch (Exception e) {
0 [, S- _' u4 f* F0 P- `0 { System.err.println("Exception updateLattice: " + e.getMessage ());# x1 ~0 ]9 \; j' n5 E+ Z% f
}
. L! M0 `9 Q) @. t! o5 f
) C: a# N f3 m8 A- ~ // Then we create a schedule that executes the
/ ^/ k2 D* r2 y4 ~9 u# U2 w // modelActions. modelActions is an ActionGroup, by itself it
% ]3 x8 \- U Y& U4 W- P* T$ R // has no notion of time. In order to have it executed in
% e# L S0 Z- ?' b3 N // time, we create a Schedule that says to use the3 k! V1 i" M: a8 w& D2 q; u
// modelActions ActionGroup at particular times. This9 X9 s) p; l, U" X2 H
// schedule has a repeat interval of 1, it will loop every
) L/ P% I2 o# a4 b" O( G) L // time step. The action is executed at time 0 relative to
5 V/ t/ ~ s1 Y; ~9 x! g* l9 U1 I8 Q // the beginning of the loop.1 v `1 _5 n n! W3 P# j
5 X& m1 H5 y& s$ l. Q% u9 Y+ y // This is a simple schedule, with only one action that is
o3 t8 N, u. o // just repeated every time. See jmousetrap for more. t" a; C6 q. `) A5 l) ?
// complicated schedules.
/ |4 g2 {" f$ I, \; H! q7 R# a
! K4 o" E/ p3 W, k modelSchedule = new ScheduleImpl (getZone (), 1);2 G6 h# R$ j% v. |" A7 m/ e6 [( H
modelSchedule.at$createAction (0, modelActions);4 H0 f# r- {5 X7 W
% c1 @2 i% Q3 Y% ~5 s$ l
return this;1 O H4 p9 t# h+ S9 { j! U7 n
} |