HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 N& k0 Y6 ?# f: n4 G1 k2 F
- x, f9 }2 D* {% o
public Object buildActions () {
8 x. t' v, A1 N; g( t7 [ super.buildActions();
9 O( U0 K( ^7 V4 ]0 S3 j; j" _ - X5 u9 g3 |% }- U/ a2 b! b
// Create the list of simulation actions. We put these in
) x6 m& a6 I9 v, G: z8 n // an action group, because we want these actions to be
2 W& U, q6 Z7 h, Q8 E9 d4 Y // executed in a specific order, but these steps should, ^% a( W, k/ c2 r7 `9 F2 H
// take no (simulated) time. The M(foo) means "The message' E# z! b6 y9 _3 s
// called <foo>". You can send a message To a particular, E! Q5 Y3 X3 Q3 {7 U/ X
// object, or ForEach object in a collection.
$ K- {+ k7 u0 V+ Q9 e' C
+ X. U( F7 }' d' G4 X. O% x // Note we update the heatspace in two phases: first run
, l1 a# Y5 T$ B // diffusion, then run "updateWorld" to actually enact the U1 U7 `9 _! q* j
// changes the heatbugs have made. The ordering here is! u- W) |0 ^* V' V& e! b
// significant!
" g- s" r" u7 y4 m8 z 9 R5 W) W8 R- v3 ?
// Note also, that with the additional
8 a7 {3 P6 X& M9 H% e. p3 M // `randomizeHeatbugUpdateOrder' Boolean flag we can
& p8 L) m" p5 [! E# y* z // randomize the order in which the bugs actually run) E9 D2 P4 q" p
// their step rule. This has the effect of removing any8 h: S6 d) V& k! x; ]
// systematic bias in the iteration throught the heatbug- S8 T7 _2 O: N0 h4 s' [5 p: z8 [
// list from timestep to timestep7 ~/ P& r" X$ R
+ b$ h6 B; j0 n! F! U // By default, all `createActionForEach' modelActions have7 d( G, E; t9 A- ^
// a default order of `Sequential', which means that the! y5 n+ ]! M. ?/ O
// order of iteration through the `heatbugList' will be# ~! \, t8 c- h* N
// identical (assuming the list order is not changed
- y$ n' l: }8 e4 n0 G // indirectly by some other process).9 x8 v G' r; l- m: |+ P- z% t; \
! z" p s1 E% B' D' {! F modelActions = new ActionGroupImpl (getZone ());
' F6 K$ ~& [/ S3 u: E$ [) J0 c$ V. ^$ [) [
try {
' q* j3 f* d0 y( v modelActions.createActionTo$message# Z: w- h' k% L+ g, E. D* G) q9 d/ G E
(heat, new Selector (heat.getClass (), "stepRule", false)); P: E0 b; a8 _! l( h( v
} catch (Exception e) {
1 H, N1 d1 r! s$ ?, }& k System.err.println ("Exception stepRule: " + e.getMessage ());
, Y: j1 G$ U' ]% z4 e5 r" ` }" G7 J( {& c2 T P. w
* r" f5 B7 L% G
try {
+ h; M0 x2 K3 e: w8 I Heatbug proto = (Heatbug) heatbugList.get (0);4 s, E1 D7 @; h) N: @
Selector sel =
" k& H# A1 g0 f5 {. m. Z# t. m new Selector (proto.getClass (), "heatbugStep", false);: Y" B4 w1 ?) J; z4 T4 y" N
actionForEach =2 R' H$ x! r& t: B/ U% a5 [
modelActions.createFActionForEachHomogeneous$call
9 z: ^2 Z: \# Y: J (heatbugList,
# X" Z" c4 O% j0 Z1 Q2 d4 l new FCallImpl (this, proto, sel,
) S* F" H* b; ^4 X8 b new FArgumentsImpl (this, sel)));
3 O6 \1 t: z# {: N7 l1 ^+ c } catch (Exception e) {
u: I' I/ Y. N: F. s# h e.printStackTrace (System.err);
, s+ Q! Z' Z# b" @7 }$ D }
, E y) N- A1 P 1 k( c5 g/ D [
syncUpdateOrder ();. X" _4 r- N$ p2 h& ~. a
; p6 \' X' d4 f) \* X4 ?% F: O5 } try {
$ N* I; Q* ]6 X9 w+ k modelActions.createActionTo$message
, ^7 s0 E2 N5 t/ Q5 S2 I. [ Z. T (heat, new Selector (heat.getClass (), "updateLattice", false));
3 J! V% Z5 o* r/ B1 S# q/ P* T } catch (Exception e) {
( M+ ^. r' E) U6 u5 Z) r# b System.err.println("Exception updateLattice: " + e.getMessage ());4 ^! U5 a9 Y" z ^; B$ s
}4 y7 @" E1 A# }. \
2 B$ T& k2 o$ ^5 N9 D, D' X // Then we create a schedule that executes the
" E6 k9 N5 W, C+ p // modelActions. modelActions is an ActionGroup, by itself it
- z h) c( _7 Z) ^% U // has no notion of time. In order to have it executed in' ^& l& `* e3 W) ]$ o( X( G* o+ t `
// time, we create a Schedule that says to use the5 p9 [7 g5 c0 R% M/ n
// modelActions ActionGroup at particular times. This
4 }& x: k, Y0 r8 J // schedule has a repeat interval of 1, it will loop every. F$ g4 y: j4 L+ N9 U( W
// time step. The action is executed at time 0 relative to
6 n% s' S8 x* m$ D2 l/ ^( A7 C // the beginning of the loop." M# X" _; s+ j4 ?. A
% ^- n2 S) ^/ u6 n$ R0 b$ W // This is a simple schedule, with only one action that is$ M- B, o" d; k8 s4 A
// just repeated every time. See jmousetrap for more
3 w. ]" P1 ~* @3 Y7 G ~5 N+ K // complicated schedules.! {2 |' \+ v( Y/ L3 U
4 h- Q3 l% A, F% w G
modelSchedule = new ScheduleImpl (getZone (), 1);
2 A* v! C7 {7 Q G. G% _ modelSchedule.at$createAction (0, modelActions);* }' i/ i O) \# } n* K; \' R
) U& p$ E2 |: {3 F
return this;
+ L- r* T2 f. m: x } |