HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ \- |% \" Z# w4 ^5 K! \: @( L$ W+ P4 n1 V/ p* d. c% k2 Y
public Object buildActions () {
9 b `# f. d+ L F4 b super.buildActions();
7 h. r5 @9 J8 S, _ ; K6 E" o- M) W3 ^( h/ A, n5 B* l
// Create the list of simulation actions. We put these in
3 w# P+ d: r9 k, n4 D% p! J- Q0 ` // an action group, because we want these actions to be8 n- `! \6 H; n R$ c+ Y
// executed in a specific order, but these steps should% s. O# g y' t8 S4 U
// take no (simulated) time. The M(foo) means "The message0 j1 g) r8 v) e/ v& l. A
// called <foo>". You can send a message To a particular
" j; e! O7 W' \# Z- h* q7 n // object, or ForEach object in a collection.
0 ~' N0 i# B- [
- a: O0 Q7 M, _ F // Note we update the heatspace in two phases: first run
6 f+ B- N, ^: V // diffusion, then run "updateWorld" to actually enact the% r1 o- c- n1 t0 ? A; G4 [) ^
// changes the heatbugs have made. The ordering here is# k1 U# ^0 Y# U
// significant!3 }" ^. S! S7 o/ l" j$ e/ w$ r; e9 {
! D, H/ L9 T* Y // Note also, that with the additional
% P w H7 g) r7 h% X- i+ i // `randomizeHeatbugUpdateOrder' Boolean flag we can
! a6 z7 D ~* E5 B6 c( B, `# [! K/ W // randomize the order in which the bugs actually run
9 J" R/ z8 G G# ? // their step rule. This has the effect of removing any
/ N6 a( f+ ~: ^, X1 N // systematic bias in the iteration throught the heatbug" ~! t _* W2 o
// list from timestep to timestep
8 s3 f, j! [' p& ~3 ]
8 N! i- \+ ?9 d. H // By default, all `createActionForEach' modelActions have
; \) \1 h6 a" m0 i% H+ J // a default order of `Sequential', which means that the) V0 N/ p* b0 j' c& X R# \7 ?
// order of iteration through the `heatbugList' will be( V# k$ w1 |7 Q3 F$ v3 ^
// identical (assuming the list order is not changed, n4 ^# Y$ r& k
// indirectly by some other process).! b% w. [0 `+ {0 K2 T, l- M+ \
@, W+ d7 F7 e) E7 Z
modelActions = new ActionGroupImpl (getZone ()); c. y) ~8 L9 [
# D* H# v( Z& n% @- ]' s" f6 @: N try {
* y2 r6 ~9 t b! X" h modelActions.createActionTo$message' P0 K! U2 i; V
(heat, new Selector (heat.getClass (), "stepRule", false));
; @* C% z5 b6 g2 M6 r } catch (Exception e) {. Q9 Z8 R8 x' c- L- C+ \$ z
System.err.println ("Exception stepRule: " + e.getMessage ());) R1 \4 }; Z0 k, M5 P
}
& i6 W$ ?8 ]6 Y2 y1 E
, S8 Q. P0 O$ W( I$ L( G9 j try {9 [1 Q X. x) S+ ?4 K
Heatbug proto = (Heatbug) heatbugList.get (0);: N2 }- n& L$ A# G( c; \# ~) K
Selector sel =
$ ~5 h! ^8 v8 T$ ?5 M new Selector (proto.getClass (), "heatbugStep", false);
' \1 O+ q9 o$ a0 o" V actionForEach =
2 p! ^* h. U* j( M modelActions.createFActionForEachHomogeneous$call1 d. b* j# B2 ~0 n3 e, K
(heatbugList,
9 c4 g3 p! P3 \1 [/ `# D$ Z k new FCallImpl (this, proto, sel,
) a3 _+ L( n1 W5 k new FArgumentsImpl (this, sel)));# @. u5 ^/ b5 Y! [
} catch (Exception e) {0 {9 y2 j; B/ x0 B4 S- W
e.printStackTrace (System.err);! h* t3 Y; V; w6 z( `
}3 x* W/ \5 y( d
g4 O2 x( K8 c G1 M @% [1 L
syncUpdateOrder ();
$ t, _0 ^1 [' X4 w; ?3 b' c8 ]$ |0 i% S3 ?: Q& j# i( U
try {: {# @8 t8 Q2 N5 z# n6 w
modelActions.createActionTo$message - P4 r0 O9 b. h9 e% B
(heat, new Selector (heat.getClass (), "updateLattice", false));5 O# ?2 L8 [3 l7 v, B5 m
} catch (Exception e) {: _$ C* ^1 T- E
System.err.println("Exception updateLattice: " + e.getMessage ());
5 b! `& s( A5 o }5 m. M! I( q& h- R5 a
4 v- I- e/ s% N* k$ A# p4 q& _
// Then we create a schedule that executes the" L3 x5 N$ C1 S$ G+ t" o# \: K
// modelActions. modelActions is an ActionGroup, by itself it7 T1 F- Y% C) c9 ]8 K$ ~, m
// has no notion of time. In order to have it executed in Y5 |" R* @9 }. y; G6 C. M m* j
// time, we create a Schedule that says to use the
) P: [9 D. _4 {! f# E // modelActions ActionGroup at particular times. This' d8 \4 O$ K& A1 o! H
// schedule has a repeat interval of 1, it will loop every2 ?/ z! E, j% d5 A7 e
// time step. The action is executed at time 0 relative to
8 j. S" {8 f5 x. U7 x% E) o/ S& V // the beginning of the loop.3 [" _2 q/ A% F8 }9 B. l; ]: s9 ]5 C
/ V1 T3 S& N; M9 Y! \% z2 N // This is a simple schedule, with only one action that is
' i2 _$ p7 B' v5 n' |. w0 } // just repeated every time. See jmousetrap for more v$ k0 E$ U: g* E R4 I
// complicated schedules.
5 N! V, Y2 B3 w' X- a i7 \8 P 6 N5 U' r( h3 O1 u! K0 h, R, D
modelSchedule = new ScheduleImpl (getZone (), 1);
9 f' T7 ~# H9 s. b modelSchedule.at$createAction (0, modelActions);4 w# J8 j4 Q e) @
1 N/ x* a7 f% P+ c5 [% o. n( _ return this;
. G/ J. J7 u- R } |