HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: @5 s1 s) |. Q2 |8 _
: y$ `1 Q6 q5 y5 o public Object buildActions () {0 S' l7 ^8 U* T" d& O
super.buildActions();; O, d& ^1 ~) Z/ e+ `4 s0 M
% R$ E! B% D& D. K // Create the list of simulation actions. We put these in* {2 I z4 C8 |4 E# g, [
// an action group, because we want these actions to be
/ w. ]% f9 N+ a! r3 ?+ n' @. g // executed in a specific order, but these steps should6 b% z ?7 W/ b' w& e1 q' x/ O9 r
// take no (simulated) time. The M(foo) means "The message0 ?, Q" \6 z; x- D
// called <foo>". You can send a message To a particular
+ r# A: ~9 O7 N p; s% X // object, or ForEach object in a collection.+ q2 m5 g# c5 S- U! p
8 h2 b* ]6 `; l* O- d8 G
// Note we update the heatspace in two phases: first run5 M8 G: x2 b0 n x% d
// diffusion, then run "updateWorld" to actually enact the+ S" x# F2 E8 t% I6 a, J
// changes the heatbugs have made. The ordering here is6 k( O' N4 b# u3 c9 j* W/ O
// significant!, Q; Q7 }3 F6 f5 G2 L7 ]/ R
# \/ N2 Y+ V1 @ // Note also, that with the additional& R: t6 H5 e: Y! Z+ {8 B
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 I: q7 P$ B( K9 \/ B$ K$ d // randomize the order in which the bugs actually run- ]7 C0 x) r9 J* P6 D- g- b
// their step rule. This has the effect of removing any* N6 j3 t- }$ o- s+ v2 V
// systematic bias in the iteration throught the heatbug3 e4 Z- g' B0 n5 q
// list from timestep to timestep
9 y; b1 @: a) ?' G' B; @; `' d
: s( |2 O. t( ]. `2 Z // By default, all `createActionForEach' modelActions have0 a" J: i3 L2 c' e: P
// a default order of `Sequential', which means that the$ k; e2 e+ i: U" }) x6 Y
// order of iteration through the `heatbugList' will be) ]% z8 n1 l% A% B# G4 Y- C* p
// identical (assuming the list order is not changed# b: x( Q0 x4 f
// indirectly by some other process).1 y6 b: c% g5 r ]+ [* ~0 S
* e5 _4 T& }$ w1 U- N. U modelActions = new ActionGroupImpl (getZone ());3 X- z9 T5 h9 Y/ K2 K" O8 w
; Y1 R: U" A6 x/ c! J9 X: ^) H
try {4 C- ^% _) O5 q0 Y
modelActions.createActionTo$message
# I3 t+ ^/ ~8 ^5 | (heat, new Selector (heat.getClass (), "stepRule", false));: {! v) J! R g7 J; j$ U0 ^
} catch (Exception e) {
2 n5 m: t* M- ^, A$ q9 a: C( E System.err.println ("Exception stepRule: " + e.getMessage ());( ~0 r: I3 N# t' ^, A9 }, ]$ h( k, O
}6 B9 A) p1 H1 m9 B
& Y; N* S, d7 a1 ?) r
try {% \9 G) R+ d) ~& s( I3 S) {" b
Heatbug proto = (Heatbug) heatbugList.get (0);* R) S2 g/ x' r+ |+ P% F& k/ q0 V2 e
Selector sel =
: e. I' n6 {2 M+ K$ X2 c# B new Selector (proto.getClass (), "heatbugStep", false);5 _/ J* @2 M: w$ L8 e: b
actionForEach =
6 a) u$ [+ L! ~2 N) r- J! ]' w modelActions.createFActionForEachHomogeneous$call
' R1 e. }& e% d/ P (heatbugList,
1 U7 [8 w: p) b) x/ L( S: } new FCallImpl (this, proto, sel,
) f5 E( A* y9 } new FArgumentsImpl (this, sel)));
) _' ?# P; p0 F& H } catch (Exception e) {
" d( P3 R9 g! Q e.printStackTrace (System.err);! D9 o, B: ?1 t* M1 k8 t# }! u
}8 D. ^8 c: {( V+ {
8 p8 g$ {2 o/ u syncUpdateOrder ();
, ~3 S; E& T# V! H# X- v! c9 R
+ @9 @: J% F$ L$ g+ p! J$ ^8 s try {
2 ^3 o/ Z9 C+ E: T, g modelActions.createActionTo$message 9 e1 D/ V( f E9 y
(heat, new Selector (heat.getClass (), "updateLattice", false));, F* A/ k4 y/ Z- N
} catch (Exception e) {
# l8 P# I1 {, | q {& M+ U6 T System.err.println("Exception updateLattice: " + e.getMessage ());$ m0 O- Q+ b& p. u; C* W
}
( c& ?& y. {; z J
% A! b' D7 W% ^5 G+ ~ // Then we create a schedule that executes the6 r2 Z5 B/ Y0 `. J( q4 _0 D
// modelActions. modelActions is an ActionGroup, by itself it
0 \6 ?& X, j9 D0 }" m* V* i. w // has no notion of time. In order to have it executed in
, a! o4 w2 ~7 X. E9 v. e& n // time, we create a Schedule that says to use the+ n6 I4 }4 H3 }
// modelActions ActionGroup at particular times. This# ~1 o6 j9 Z1 x+ M
// schedule has a repeat interval of 1, it will loop every
; R8 F0 P, v! Z // time step. The action is executed at time 0 relative to# J$ n4 @, q0 P |
// the beginning of the loop.: t. Z4 n! U5 d' y- v3 u
\9 u2 G: z5 E5 e: }1 x
// This is a simple schedule, with only one action that is4 K: K& y7 u7 p4 w7 i$ T Q
// just repeated every time. See jmousetrap for more. f6 x8 q8 J1 [6 D9 f
// complicated schedules.
" {, k3 Z; w9 t0 N' U2 I
. ~# z$ i) u4 ]. `. ]7 l) r modelSchedule = new ScheduleImpl (getZone (), 1);5 c, Q; v3 S' K6 z* \# z5 |1 A. C9 G
modelSchedule.at$createAction (0, modelActions);
& t1 x4 A: w/ a" z1 R1 \8 E
0 c# x& ^' X0 m& L/ X return this;" J5 f U* y+ p+ e( f* f
} |