HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: K. R9 R3 C1 A# @" P
# ^/ i6 K3 `9 _% G7 } public Object buildActions () {
# A9 ~+ l4 X- c" M( k/ T super.buildActions();
5 c1 n0 u, s5 e) Z 0 z8 ~7 S5 P3 h: @+ ~5 u# B# L
// Create the list of simulation actions. We put these in6 J; w( e/ d5 I2 x( I
// an action group, because we want these actions to be5 T: C5 L# R$ T* E5 Z ^
// executed in a specific order, but these steps should5 p/ R) R1 L. b2 R% v2 d' \
// take no (simulated) time. The M(foo) means "The message/ U- |6 y8 ~: T1 w! B
// called <foo>". You can send a message To a particular
. j' b" Z3 F9 Z( ^5 N // object, or ForEach object in a collection.1 Z% r, w/ X Z
' q% C. l7 k7 \ N5 i
// Note we update the heatspace in two phases: first run% a2 i7 X4 r, ?6 d' k
// diffusion, then run "updateWorld" to actually enact the8 z0 ~' i; c( i6 Z: w7 I) F% o% V
// changes the heatbugs have made. The ordering here is
+ q* g: {- z0 L! z // significant!) V7 g: w9 c- a. t/ e, `/ g1 a
+ S0 F1 q! t5 m t& X // Note also, that with the additional$ e* ?( b+ f1 D h
// `randomizeHeatbugUpdateOrder' Boolean flag we can
) W0 a7 A5 ?2 w; T. i0 q+ n' i // randomize the order in which the bugs actually run
8 ?' ~! W: \) {3 O& N // their step rule. This has the effect of removing any
- Y( F5 ?$ b1 _; D" B // systematic bias in the iteration throught the heatbug" u& N' n# y+ [' t* e! \+ Y
// list from timestep to timestep
) G2 f7 d1 {5 `5 @1 @) G2 F 4 B$ G- I3 C; D4 m' q
// By default, all `createActionForEach' modelActions have, s( y V' K! b( Z4 O8 S S
// a default order of `Sequential', which means that the3 H+ f( u* g' H! @8 K
// order of iteration through the `heatbugList' will be
2 x3 v$ i& r* i( x // identical (assuming the list order is not changed
1 e, m4 M! g" Z3 Q // indirectly by some other process).
# X- X8 [. K1 N# {$ n1 u/ b7 G, J7 `
" r f G }+ R9 V- h modelActions = new ActionGroupImpl (getZone ());' a* r/ p& ^( e* p ]) u
: B8 X7 P w0 l# R& `4 O% ?- ?
try {6 g+ P V; Q# U1 F+ o( w, l
modelActions.createActionTo$message
! C0 q n5 W- P) b (heat, new Selector (heat.getClass (), "stepRule", false));
: n7 D# l; A) h# }4 y: z: j } catch (Exception e) {
H7 x+ {* R, q; {9 x System.err.println ("Exception stepRule: " + e.getMessage ());: G5 A. ^/ w/ c" L( y
}
. b0 q4 [5 P; R# _4 E7 J- M" I l$ ]" A' v+ c0 T
try {% Y8 |- y, x+ z! X7 {
Heatbug proto = (Heatbug) heatbugList.get (0);4 t8 H, ~& {. ~! H9 R) Q$ Q
Selector sel =
# P- U& L9 s+ K# Z; A- s# G) ^ new Selector (proto.getClass (), "heatbugStep", false);
' g: y6 V4 I6 }. a/ |- o actionForEach =
) k( r* y7 q% d. D modelActions.createFActionForEachHomogeneous$call
! U0 m4 o/ k4 z. p- a (heatbugList,
3 ~& C# ?% O2 s, E0 ~ new FCallImpl (this, proto, sel,
: I# c; i1 L/ {0 O new FArgumentsImpl (this, sel)));
/ h3 b' H2 b" ~" o _ } catch (Exception e) {6 ?0 O/ H! |8 v6 Z, ?$ g
e.printStackTrace (System.err);7 Q; @. K8 Q! j { J/ N* t
}
" s* j) w7 B& s5 ?9 n2 Q6 p
1 j6 X+ q4 {! a( R% {8 P D* V syncUpdateOrder ();1 x1 }0 N6 [+ c5 c% ` K- o
$ `0 Z; k. ~1 {: l. @( R6 @0 N try {/ ]" \: h9 r) w) c
modelActions.createActionTo$message - i2 p( N0 k2 ~! |8 X0 K2 @7 P( u
(heat, new Selector (heat.getClass (), "updateLattice", false));' D7 t0 z$ `* K, ?, Z# |
} catch (Exception e) {
0 Q8 R% {( i0 r8 ^1 ?; H% E* c System.err.println("Exception updateLattice: " + e.getMessage ());! A1 w/ g' r7 \4 Q- h! g5 r
}1 G! |1 Z% f3 S% Z2 N, H4 D3 s
2 R. L' X) P4 E& P7 z" | // Then we create a schedule that executes the
, v. D0 h# J3 s+ h6 k // modelActions. modelActions is an ActionGroup, by itself it
: B. N: M7 ~2 {3 r4 o, N // has no notion of time. In order to have it executed in
/ K; P0 R6 w- X& K' r: V, S1 a! | // time, we create a Schedule that says to use the
; ~% h3 E/ U) @5 k/ l3 Q, ^8 w, M // modelActions ActionGroup at particular times. This5 [3 I' p9 i: i
// schedule has a repeat interval of 1, it will loop every* T& n! k$ Z& p1 I9 u1 v
// time step. The action is executed at time 0 relative to } l5 H" g! e: E- [1 v, J2 B
// the beginning of the loop.
2 L1 ?6 L' H9 M0 O2 v/ n4 f1 @, w6 G4 I
// This is a simple schedule, with only one action that is
* W }) E: V9 f7 G3 X1 J }: ` // just repeated every time. See jmousetrap for more
/ C. O% ^5 U9 Z6 L7 g+ b4 b // complicated schedules.
& Y! i: i/ W4 {: n
" B: k! z" [; K. o, | modelSchedule = new ScheduleImpl (getZone (), 1);% {6 `+ S: n5 r9 _7 ]6 H Y& q3 W
modelSchedule.at$createAction (0, modelActions);
3 p: U' Y4 r6 ], E
- M/ m$ ?3 l7 g) j5 w* u; b7 F return this;8 X7 F4 {6 o, k+ Y
} |