HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, _, O8 ~$ A, ^. e4 ?1 [+ T0 N
! }5 m; P) v) B: u% K+ C
public Object buildActions () {
. s! c6 a2 p9 J super.buildActions();2 W# _9 m2 D1 Z) R& }, l
! f: D6 ^0 a- Q$ V) `+ f // Create the list of simulation actions. We put these in( @+ x; } m0 ~% S1 C
// an action group, because we want these actions to be# l8 [9 x4 H8 W9 R( K$ Q* W
// executed in a specific order, but these steps should1 o/ t9 z1 T4 m' S4 K
// take no (simulated) time. The M(foo) means "The message- X* F N( C' H. z, K* \
// called <foo>". You can send a message To a particular
5 C, c: n7 V% T1 P3 ?9 M7 H0 U% k // object, or ForEach object in a collection.
7 D# a* j! @: t t
) c/ o! A& b* M" u) z$ ~3 F) f // Note we update the heatspace in two phases: first run' Q, p u) }1 s, ?
// diffusion, then run "updateWorld" to actually enact the
9 u. ^# G# O; {. X/ K6 H // changes the heatbugs have made. The ordering here is0 D0 S" Q" g* |! U8 s! C
// significant!
4 K! ?1 K3 h+ D2 r/ f) M
6 e+ B: X( Z8 s4 _. |3 X1 j // Note also, that with the additional* N" d9 ^$ S. ~3 Q( N- L8 u
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( y0 b2 a$ u; U" i // randomize the order in which the bugs actually run8 L* g/ M8 S2 ?
// their step rule. This has the effect of removing any
) n" l6 r' U4 E4 s! V1 ` // systematic bias in the iteration throught the heatbug- j9 ` a' T8 ~; K1 c2 f
// list from timestep to timestep9 w* G, [1 M$ W7 q
6 Z! M# p9 F: B' r- W // By default, all `createActionForEach' modelActions have, W7 J _: w% k/ @7 d6 i
// a default order of `Sequential', which means that the
# i3 V [, R; v6 ?! v2 R // order of iteration through the `heatbugList' will be7 t$ [6 L9 f6 Z* t2 {; I
// identical (assuming the list order is not changed5 B3 H2 d1 Z A4 F8 k9 T
// indirectly by some other process).7 x: G, V( J2 R
& q3 {0 |5 b$ n& ^' Q modelActions = new ActionGroupImpl (getZone ());
9 b( U0 N) @5 O9 J
8 U1 K8 [" g9 e; v try {; {8 n; y' b; e& F: k5 X. G
modelActions.createActionTo$message
+ J9 T2 \6 x. `, s7 [) E (heat, new Selector (heat.getClass (), "stepRule", false));& T9 j" `9 U5 l1 _: z. ?+ G
} catch (Exception e) {3 v6 j" y+ c) s1 _2 f2 E
System.err.println ("Exception stepRule: " + e.getMessage ());- \" g6 d4 b' n6 d9 b% {7 M
}
6 y# S/ M+ I L- [; U8 X- S. B* K/ n9 y+ t! F
try {2 y/ i! V- y/ X4 A: G4 L# M
Heatbug proto = (Heatbug) heatbugList.get (0);
/ Q6 z) u5 n5 b7 D Selector sel =
; w' X" [. X7 X( h! ? new Selector (proto.getClass (), "heatbugStep", false);
$ H1 m5 W" _; ?+ w actionForEach =
, l+ j, l; f* m ? modelActions.createFActionForEachHomogeneous$call- h/ ?' R3 U$ {) W' B# `- O" o. p
(heatbugList,
; Y! x9 x# [( E5 O G; y3 `1 D new FCallImpl (this, proto, sel,: F6 q/ Y5 [7 {
new FArgumentsImpl (this, sel)));
0 R* n" t( Y$ y. m M+ L- _7 P } catch (Exception e) {& s1 A9 u0 U' Q0 ` [1 p- Q# V7 m
e.printStackTrace (System.err); e* @( O" Q0 V C
}/ f" R1 a3 ^ R9 C
9 ~% A( `# l. z0 S syncUpdateOrder ();% Y6 |; }5 W/ @# A$ @
: R. K) t+ M# M1 g9 ^8 I try {: I& ]7 O+ M3 K. x2 ]
modelActions.createActionTo$message ; ~8 ~; V6 d! T
(heat, new Selector (heat.getClass (), "updateLattice", false));
( a/ R, f( [& ?7 M0 | } catch (Exception e) {
8 f/ _1 e* E5 f+ r# | R4 k3 p- l System.err.println("Exception updateLattice: " + e.getMessage ());9 b- @& Y; x% g! Y& i
}
7 p/ n/ `/ _$ m' v l! g" q& W
$ h0 ?& c; v% Q/ w: r // Then we create a schedule that executes the
- L8 n# M$ ?) W) ?, K! T& k // modelActions. modelActions is an ActionGroup, by itself it( G" k$ y9 Z% e" }* O* h
// has no notion of time. In order to have it executed in
" R% b; Z8 U9 C, X+ }" U // time, we create a Schedule that says to use the
8 J5 I8 H8 k) f // modelActions ActionGroup at particular times. This$ W5 i7 l8 x: ~! E
// schedule has a repeat interval of 1, it will loop every& a- L% Z% B8 @6 R6 J( b
// time step. The action is executed at time 0 relative to6 U- f4 @& J6 g, b
// the beginning of the loop.
1 q- V' H0 J; r3 v1 J1 D* L$ W
// This is a simple schedule, with only one action that is
z6 w! a6 e0 u8 z/ s7 _: x // just repeated every time. See jmousetrap for more
8 @5 r7 C, W5 z // complicated schedules.+ P+ F5 D& r8 s( W) T' e
) I0 l5 V2 }6 ]2 `3 S) h9 m* C; J modelSchedule = new ScheduleImpl (getZone (), 1);
. `( _' X- l' t/ F! W7 Q, Y+ Z modelSchedule.at$createAction (0, modelActions);" V- x. K: Q: s. L$ S+ ?2 L
8 w; Q* X; R% u4 F+ J: y8 g return this; V. z6 [% x6 I: Z
} |