HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# O6 k4 x# y: [: ?, \$ g+ {) E# [, T' [3 \: y6 l# A d) J
public Object buildActions () {% p( l3 ?+ |; ]0 ~6 v e0 P3 K
super.buildActions();" K- b9 {0 U R7 C5 n# U8 e; O
+ d0 J$ t# n1 ~* V. t // Create the list of simulation actions. We put these in
+ \7 H8 u( `- x& V# z4 F // an action group, because we want these actions to be9 {' ?7 a" p0 _* d7 \! Q% q; W& T
// executed in a specific order, but these steps should7 D/ |% B& ]: A1 ?. R
// take no (simulated) time. The M(foo) means "The message, M4 y. N5 n4 ?! @" x
// called <foo>". You can send a message To a particular0 m* f6 O T) m' h& J7 F
// object, or ForEach object in a collection.
( d0 I" K, p) X" q& H* S/ _- z ' v& g, l8 x! P; ]9 i1 k
// Note we update the heatspace in two phases: first run% r3 y$ ? F+ _: l/ x
// diffusion, then run "updateWorld" to actually enact the
- j$ l0 S" r% o2 e" {: n' ? // changes the heatbugs have made. The ordering here is# X9 ~0 k( ], `/ d
// significant!
|, e/ B' e2 t6 s8 Q$ S
3 P G) ~: Q% t9 \, ~( ~ // Note also, that with the additional
; K, F( f& _. k( d: B // `randomizeHeatbugUpdateOrder' Boolean flag we can5 T6 Q* i7 T/ i# j' t
// randomize the order in which the bugs actually run
' I' M. j: R& R9 x! @8 \ // their step rule. This has the effect of removing any
" w. F3 ]8 b6 n- w' h. Y5 P5 B // systematic bias in the iteration throught the heatbug
T1 ]9 i% p f/ J4 d8 ^: X. R // list from timestep to timestep
" ^# D( T% E5 O% j" b0 ?! @
/ R3 K K4 Y0 J# V2 A // By default, all `createActionForEach' modelActions have
* P7 y0 E1 M5 j c0 e5 Y( V // a default order of `Sequential', which means that the! J0 @' L1 ~' [- f& J8 l
// order of iteration through the `heatbugList' will be
: X. B" [9 [6 \( ^, m6 j7 X* z // identical (assuming the list order is not changed8 N3 z6 Q$ y7 e- o
// indirectly by some other process).1 T/ k1 D3 N0 P
* I2 l' ]7 p6 d modelActions = new ActionGroupImpl (getZone ());
9 _& _, B, }* Y8 }( o
9 H6 m3 m) O) f; d8 g try {! X& Q0 d( C* t2 T" S* c ]
modelActions.createActionTo$message1 g4 \( ]/ ^/ ^
(heat, new Selector (heat.getClass (), "stepRule", false));
4 }' e% c0 e( y: u; X3 E+ c- Y5 ~ } catch (Exception e) {& y' ~' h# Q; q1 [/ a- g
System.err.println ("Exception stepRule: " + e.getMessage ());
0 _* Y; S ^% ?, C( h- _ }
% z: g5 f7 } d$ w5 D
% D' O+ C, h1 M" Y0 R try {
9 L* \& }- I" }1 E+ ^ Heatbug proto = (Heatbug) heatbugList.get (0);
) A9 m. Z2 ^7 `8 ? Selector sel = - s# R! h) E/ [* q* @% {
new Selector (proto.getClass (), "heatbugStep", false);
+ `4 g; G9 P; g4 _3 E9 j actionForEach =
; G% f0 `8 D y6 J1 ^9 b& v& \ modelActions.createFActionForEachHomogeneous$call; p0 O9 d6 q& V4 }2 v2 J
(heatbugList,6 C- }7 H8 g# @$ d
new FCallImpl (this, proto, sel,
' c$ }* @* Q! N; B6 S new FArgumentsImpl (this, sel)));- p) u5 Y6 {1 f. x
} catch (Exception e) {' e0 k7 O& M) a8 Z: u$ X
e.printStackTrace (System.err);. f2 ]! d4 F6 V
}
6 j$ Z+ F0 b' t% i% u* f 6 a- l ~) j2 a$ s* l6 h2 v9 ^
syncUpdateOrder ();( h% {' V6 l6 n4 M* |$ H! j$ r
; G5 s% q2 c" s9 `# {" e
try {9 W3 N) J) a9 u
modelActions.createActionTo$message . p1 o( ]9 A9 |6 A) V/ G
(heat, new Selector (heat.getClass (), "updateLattice", false));% S( O* ?, |- s
} catch (Exception e) {
3 h; u' O1 {# m9 q/ ~5 c System.err.println("Exception updateLattice: " + e.getMessage ()); \8 n G$ Z+ F' Y+ D, w* u) Q, P A
}
" G3 i7 C) z5 L8 {
7 {; T( A0 u' p4 ]: X5 j0 ^ // Then we create a schedule that executes the+ T8 y @# K$ d2 Q" g# [: F% Y
// modelActions. modelActions is an ActionGroup, by itself it
% s9 Y4 N/ B: q! ?& w7 N // has no notion of time. In order to have it executed in
; H9 K4 o9 x" O1 K [ // time, we create a Schedule that says to use the+ U& p9 q# Z* M7 g0 m. ^
// modelActions ActionGroup at particular times. This+ B; B/ @! C) n4 n) G4 l' d/ S: Q$ r* l
// schedule has a repeat interval of 1, it will loop every
. c3 T @) p2 E/ R* X" L# w1 B# U1 m9 ^ // time step. The action is executed at time 0 relative to# H: z$ L: @; i6 U& F" A3 K# l
// the beginning of the loop.
8 k' Q2 R I4 l- A, U
! D8 Y, O; q: |0 u( v& {" ` // This is a simple schedule, with only one action that is! k$ b. y9 v# C" E- O$ I. O
// just repeated every time. See jmousetrap for more; d. P9 D: y+ P! \9 I
// complicated schedules.* u5 A1 }- W" R9 k/ e }& X+ e) D" g
4 O: {& G! |5 Y0 \! c) I
modelSchedule = new ScheduleImpl (getZone (), 1);
6 G* @1 o1 k% l Q( [5 E( a& h0 Z# A modelSchedule.at$createAction (0, modelActions);
- P, i$ b+ q# b, M$ K& s 4 W" u) X3 @, a: g" U# N
return this;
& i4 K5 n6 o/ `; h& B6 |- j2 ` } |