HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% V! Q* m9 O- A8 A2 N7 c
0 u8 T x1 n8 ~' A public Object buildActions () {
" F8 Q: k( n$ e( B! f" l- n2 t; r2 a; q super.buildActions();' _/ S1 e) Z; Q& q% V
8 ]4 u% f" I" S+ C9 P! m // Create the list of simulation actions. We put these in! j% _' n( Z0 N5 o
// an action group, because we want these actions to be
6 ~4 @# f2 l+ w" [( ~/ ~% J // executed in a specific order, but these steps should
3 a7 N3 V+ e4 D$ ^" {4 c // take no (simulated) time. The M(foo) means "The message
# R7 a, L+ {2 ?" B2 A // called <foo>". You can send a message To a particular
8 S8 h# v0 G, d" H" T5 u. A // object, or ForEach object in a collection.1 C/ p' |, E( G# R0 j" o
. S, I' O0 n" @ // Note we update the heatspace in two phases: first run+ B R0 [" Z* z, G
// diffusion, then run "updateWorld" to actually enact the
7 u( i: d! N8 h/ N9 v // changes the heatbugs have made. The ordering here is) i8 p# \! n% Y
// significant!
* _% K I* B3 W! T+ V 6 ~- M1 _5 v8 O" M. x5 o: n; T) |7 Y
// Note also, that with the additional
( D) R s/ [; _ // `randomizeHeatbugUpdateOrder' Boolean flag we can
0 T9 ?( @& l7 q1 w) J/ ` // randomize the order in which the bugs actually run+ {# f/ \7 H3 }# c2 w5 V
// their step rule. This has the effect of removing any
% M9 m5 s. M8 a' S9 V0 g1 r // systematic bias in the iteration throught the heatbug
! I6 r- B! W. o+ Y9 Q // list from timestep to timestep2 l; a8 v- `+ W
- x8 b5 ^% |2 A% f/ j, u // By default, all `createActionForEach' modelActions have
z2 U2 {6 u$ ]- p+ u5 W // a default order of `Sequential', which means that the6 v8 y; T! ?, M$ H/ o
// order of iteration through the `heatbugList' will be
& T' D- M$ J, g/ y$ L! Q // identical (assuming the list order is not changed
) |6 t+ }* f4 X3 ?7 T/ x // indirectly by some other process).
$ y0 Q/ ]# P/ S$ v: E4 l% f ; R/ x) h+ |% Z0 N, t- p6 @
modelActions = new ActionGroupImpl (getZone ());" ^7 R# E" F: T! Z- }, x! o) j
. x, {+ Y% ]! J+ V. N try {
: d4 R5 y9 P4 z8 j modelActions.createActionTo$message
0 G% G8 |, ^+ L (heat, new Selector (heat.getClass (), "stepRule", false));
6 t! o$ [6 A% z$ s' p( Z } catch (Exception e) {, ]! z6 V; m$ U
System.err.println ("Exception stepRule: " + e.getMessage ());- H) \& @! i7 G8 Y+ v1 q+ f
}# V$ t8 ~+ ~. l# h
( |/ g* S( J. p, q3 R
try {
% J1 i$ ~! D( o- O Heatbug proto = (Heatbug) heatbugList.get (0);
! @1 H P) W" {) o( g# L Selector sel =
( P" q9 P" C+ p6 P. B0 D0 s new Selector (proto.getClass (), "heatbugStep", false);
" } ?7 d, d' D- S% D, [, i+ W actionForEach =9 Z: {6 N- i3 |
modelActions.createFActionForEachHomogeneous$call
, }2 `# o" k5 D (heatbugList,
! q/ A. C/ G% c( ~( K new FCallImpl (this, proto, sel,
. b6 \, y- o0 h( p5 W new FArgumentsImpl (this, sel)));+ e- t8 d& a8 j
} catch (Exception e) {6 A* B0 b9 B- S0 y: C0 |- `; `, {* L
e.printStackTrace (System.err);& f1 q) i3 t' ?/ f- c( k/ E6 N
}
9 i8 ]" Z1 S3 v" S4 A5 g4 W# m " p5 o; n( F$ ~$ R. V' Q7 P
syncUpdateOrder ();' Q, j" r$ s' ^1 j# s
- u b* x8 r F( Y
try {
% v" C& o% ~5 G' | modelActions.createActionTo$message ! q4 q' m) V; m4 F& O
(heat, new Selector (heat.getClass (), "updateLattice", false));
3 z/ Z$ R( [- c. u1 g } catch (Exception e) {, v9 U' F! v; N$ G2 A0 s1 r% L
System.err.println("Exception updateLattice: " + e.getMessage ());
2 C7 x1 V9 w- q5 L6 }! r }7 k$ j9 D/ ?5 z) _0 [; c1 c
5 C& |) ?- I/ a9 v! B5 \' E6 ^$ e
// Then we create a schedule that executes the
1 B V; o3 K, w4 {! M0 _* v // modelActions. modelActions is an ActionGroup, by itself it
! p) r, Z. N, C$ T+ H. I' b$ Y // has no notion of time. In order to have it executed in
9 a* Z: [" v' A: g1 Y* ^1 r, d% ~; x // time, we create a Schedule that says to use the* H K3 K7 K3 e) q
// modelActions ActionGroup at particular times. This% z/ \4 y* ]9 ?; p& @$ `/ q
// schedule has a repeat interval of 1, it will loop every
- r- v! v# G# q+ {; w# \0 V- Z // time step. The action is executed at time 0 relative to
% D8 o, r1 p- I- g8 z // the beginning of the loop.
5 M- O# u% z- V2 Q
# {" N8 g$ z" A3 e. s: O! E+ L // This is a simple schedule, with only one action that is
9 y, w0 b' h/ A9 Q$ h& Z& I' e // just repeated every time. See jmousetrap for more
+ O4 D* f* d$ d, e( r, m/ B: Q) Y/ W // complicated schedules.; t2 ? s7 ^! B" L) m Q" A6 M
$ o- n% i/ G4 |1 f2 W p
modelSchedule = new ScheduleImpl (getZone (), 1);' B. {3 ?0 \3 C9 n0 L0 J' ?
modelSchedule.at$createAction (0, modelActions);
) B0 y9 L8 V( t, |4 h7 I
# E4 \% z5 p7 `5 } return this;
8 k1 g7 r$ X4 P/ H } |