HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 j& [2 U' q C, j {' p& H
" a) p) k9 \2 R; h Y4 C public Object buildActions () {
+ V: n" d2 ?2 }" x" E$ }5 C4 m* H1 _" d super.buildActions();
' V2 u# Y3 x% ^
' T4 J7 E* Q0 ~5 R' j* d+ I0 s // Create the list of simulation actions. We put these in& A( I; M- v! n \0 T N6 @
// an action group, because we want these actions to be
( t* v- p; ^/ b5 F) d3 X/ D // executed in a specific order, but these steps should
% h- {4 R/ g6 L1 B8 a( [: v. { // take no (simulated) time. The M(foo) means "The message
/ n- y: s! i& u" x; B Y6 W // called <foo>". You can send a message To a particular1 X( F$ d4 C1 k8 _
// object, or ForEach object in a collection.9 G) I+ k* D5 G/ q y
; j1 `4 n3 E: N& ] h; Z- H // Note we update the heatspace in two phases: first run# O/ ^' m0 T+ B2 g0 n2 O
// diffusion, then run "updateWorld" to actually enact the
# ]$ X+ d( a: |4 T, M7 s& ` // changes the heatbugs have made. The ordering here is
& s* |8 t* I" P% `" G // significant!
! r. Y O3 D% ], l
( t, F0 y6 ~* ^- i2 k // Note also, that with the additional
' g1 K J. t& U // `randomizeHeatbugUpdateOrder' Boolean flag we can* p7 B2 h6 m f6 y. H
// randomize the order in which the bugs actually run
. C& u0 l9 A8 H3 X // their step rule. This has the effect of removing any. v" w% S9 Z/ y1 _4 o
// systematic bias in the iteration throught the heatbug) @9 I/ z* d9 M- h! [% \
// list from timestep to timestep$ m! V H, q( l! y
6 k. O8 r1 \% ?& y6 U
// By default, all `createActionForEach' modelActions have9 m- K! O' R9 L; {( U, e2 S! G' j
// a default order of `Sequential', which means that the
) r$ M: D0 t/ g s$ I& ~3 k // order of iteration through the `heatbugList' will be7 S c: I; R5 p3 l! o
// identical (assuming the list order is not changed0 y) J1 T- z% ]) N
// indirectly by some other process).! R$ [9 @ x* @' ?2 {1 f" p
+ N' \) L& t/ |8 _
modelActions = new ActionGroupImpl (getZone ());) v6 U7 v7 V1 ^# c1 k& z6 f: X8 y6 C
1 l2 w* P: h \$ e5 a$ g `
try {; Y/ |4 C$ p5 e# P3 Z- D
modelActions.createActionTo$message
, P7 v* Q* U {( ?0 L3 Q (heat, new Selector (heat.getClass (), "stepRule", false));; s3 [& Z- d& U5 M; C
} catch (Exception e) {4 q* b& X* S! q& y
System.err.println ("Exception stepRule: " + e.getMessage ());( H) @$ l. U3 _ s$ T0 P5 Y. U
}2 j# T. i5 e8 P5 b8 _ d
% J, M6 Z6 |" q4 y5 Q; [3 s; r
try {
, P/ E) I! x8 D* F' C8 Q Heatbug proto = (Heatbug) heatbugList.get (0);
8 Q2 S, _- ]5 P- r1 J Selector sel =
& h# D: T! v( P! R new Selector (proto.getClass (), "heatbugStep", false);+ X* X8 Z9 `! g7 |; \" l
actionForEach = i$ ]# r, Y o* O) V( F
modelActions.createFActionForEachHomogeneous$call& [' [. \ W% D Y5 D& C. G+ x
(heatbugList,8 W- N0 B0 [- S1 }; i, b7 I2 ]
new FCallImpl (this, proto, sel,
$ C& I- J, [: y- n% R new FArgumentsImpl (this, sel)));
@5 D1 w0 F% C3 S8 [ J! ~ } catch (Exception e) {6 l0 @) s5 d8 v0 {4 [, U
e.printStackTrace (System.err);
' u8 r- `- n0 R }' [: w" Z$ n0 M* \- _
8 e& D/ O4 w. y# @ syncUpdateOrder ();
5 g- ?, X, g" ?" h3 M! z) e3 v- y) ^& |5 s T% o2 B, r* N
try {1 I/ W9 R- f: A* V. C. e
modelActions.createActionTo$message
6 k0 N# J8 U# x! X7 M8 V (heat, new Selector (heat.getClass (), "updateLattice", false));
! ]3 u' q& D: ^* F7 f$ I } catch (Exception e) {: |3 k% _1 m$ _7 p$ j
System.err.println("Exception updateLattice: " + e.getMessage ());
1 r$ l( G9 ?& m, M6 h }
0 H# [1 O1 S& S( c& x/ l, b2 e 5 T, ^1 ^- H6 x5 x( G9 X
// Then we create a schedule that executes the6 Q0 ]) v- P6 ^
// modelActions. modelActions is an ActionGroup, by itself it
- B% G6 W0 b# G8 `( D+ i // has no notion of time. In order to have it executed in
# W* D; D' A4 F, U+ Y, B9 t // time, we create a Schedule that says to use the- d; c0 P( Q+ a
// modelActions ActionGroup at particular times. This8 ?7 y3 C5 Q" V; ?- X
// schedule has a repeat interval of 1, it will loop every
/ Y8 r" v% S' r2 ]1 q" ~ // time step. The action is executed at time 0 relative to) d& d1 l: @0 G; }" C* x! u
// the beginning of the loop.
# N+ ]4 }; t( Z- d5 b$ E3 L) q3 `0 D- X
// This is a simple schedule, with only one action that is1 M2 m U8 V: O) p- |
// just repeated every time. See jmousetrap for more
- F3 X8 ?7 r c& [/ g5 I7 k$ P // complicated schedules.* g6 ^1 Q- Y& n' O: Y, {
: _ G, t. `# i* F! ?* u modelSchedule = new ScheduleImpl (getZone (), 1);
1 ~# p3 z# W; {6 _. m' Z: k modelSchedule.at$createAction (0, modelActions); n* f% i' x; o
3 i; p: C, n4 u& r) e0 v, b return this;; X0 r" A) l4 R8 g
} |