HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( R' |3 k& ]% H/ ]3 ?* Z# W" `
. {4 F; G0 G) w: T$ j
public Object buildActions () {
6 B$ @) B f/ D! b& H; A3 A4 x super.buildActions();
% L* ~. M0 `# E/ i 8 ~! x% @" ]2 q! i7 A
// Create the list of simulation actions. We put these in
U. l! l; d* p6 O4 e+ i- K3 J2 e+ T( R // an action group, because we want these actions to be- \2 L: R1 X: f$ _1 l% n* x+ j7 e
// executed in a specific order, but these steps should* y% v" q+ P0 Z# d" J/ i
// take no (simulated) time. The M(foo) means "The message4 \7 z( n0 G- @2 M2 U
// called <foo>". You can send a message To a particular
' s) W4 c5 [2 q8 K- T$ |' B. j // object, or ForEach object in a collection.! c7 ~2 l- l) @: d/ O* [# X0 A. b# \' V
% |' [2 w9 `' [+ i) R // Note we update the heatspace in two phases: first run5 r; R/ `, T7 z) {
// diffusion, then run "updateWorld" to actually enact the
% y+ A* y; q6 i+ M* R" N // changes the heatbugs have made. The ordering here is# l* c% B4 h# T3 N( B
// significant!
8 z; F2 V/ G' [$ S: y( o1 B% W
" }7 H- F$ f% k* c( {) @ // Note also, that with the additional
" P" ]) h0 u/ Z9 Q5 _6 R' P5 j d // `randomizeHeatbugUpdateOrder' Boolean flag we can
. Q( D6 l' t2 j ^; N, B% | // randomize the order in which the bugs actually run
8 x% D6 f( i) k1 s9 a' ~$ [" P // their step rule. This has the effect of removing any
3 V* \ u4 V3 k1 b7 F+ L# |% ^ // systematic bias in the iteration throught the heatbug
9 e# T$ @& u. v7 O8 a4 G, M) P) b // list from timestep to timestep
. E0 S7 r* `4 J: V" c0 O
# w# I0 `* ]' x$ q# m' w0 ^" Q // By default, all `createActionForEach' modelActions have4 e" B& g9 P( D' r7 S9 p* G
// a default order of `Sequential', which means that the
! l3 v, s0 Q& f* o5 V4 O2 y& S1 t // order of iteration through the `heatbugList' will be
. R( x0 [0 x; w+ T& O // identical (assuming the list order is not changed$ `, F. W- d0 n: X' C
// indirectly by some other process).
: R; m2 G/ a' S/ Y5 a# ^ 9 B1 `4 d( D% F
modelActions = new ActionGroupImpl (getZone ());
4 ^' t' I% h4 [& B* l; \
$ }( i# |0 _# V9 F* i$ r try {
+ d; b' `8 ~4 m6 b' C- d modelActions.createActionTo$message
3 H" V$ V' O( b1 U z& B0 o (heat, new Selector (heat.getClass (), "stepRule", false));) r5 \) Y: B( {" q! o
} catch (Exception e) {
9 u4 U+ k* N# {$ H$ {$ u& v, a* l4 R System.err.println ("Exception stepRule: " + e.getMessage ());
& \3 g9 q: Y" k: ~+ z7 w5 q/ } }
: ~; A7 Z5 h$ V$ J* g! m- k+ K3 H( B; U X* p& D' F8 O
try {8 ]1 t) Y" V" G1 g
Heatbug proto = (Heatbug) heatbugList.get (0);5 y0 ~/ [8 y" X3 [
Selector sel = / f9 k0 O( [% Q0 }2 o* x3 Q( e- T# ?: a
new Selector (proto.getClass (), "heatbugStep", false);/ u5 q% G: a6 g7 s, k1 z4 Q, s
actionForEach =1 J( e# O% l* I: W& C1 ]
modelActions.createFActionForEachHomogeneous$call
* J# Q0 w! O" H! A! D7 u+ i% Z (heatbugList,' w9 }. ~/ F& l% h5 C d& I
new FCallImpl (this, proto, sel,
/ G& H, a/ ^( a$ \1 @% n3 y4 O new FArgumentsImpl (this, sel)));
7 e" t. \( k) W3 `" s0 |- a: t } catch (Exception e) {7 v4 b: y4 c1 ?; l; l2 ?1 e
e.printStackTrace (System.err);
0 G) J6 `+ _2 c( q- @9 t6 d) z }/ @$ j5 Y0 S4 c8 p
( b d I) F' W7 m8 Q% f syncUpdateOrder ();
, M7 {7 [) V4 p: H/ W; c# W
5 M0 o. c/ M5 ~9 n6 t try {
, \6 {9 `* [- I4 D2 @4 x+ D S modelActions.createActionTo$message
8 Q3 W+ l# i- }- ?: K) Q' C1 L (heat, new Selector (heat.getClass (), "updateLattice", false));
# P& ?1 y" ]5 F! M, r9 z } catch (Exception e) {
0 ~$ s, O0 k7 J System.err.println("Exception updateLattice: " + e.getMessage ());
^3 P! N8 w" d% E. q/ t/ U }
+ j* B ]: n7 B3 P3 e+ z( z6 d & m9 H* F" t2 L5 b$ F* V* `
// Then we create a schedule that executes the3 d, g+ ` }1 l- L, S ]# ], ~
// modelActions. modelActions is an ActionGroup, by itself it! o! P; q. b0 o; m
// has no notion of time. In order to have it executed in
: e/ X. x- W" w; m5 a# r // time, we create a Schedule that says to use the
# e4 g6 d4 }& a2 Z4 j l& C/ V' n // modelActions ActionGroup at particular times. This
% I5 b1 B# z5 e5 f; h( V // schedule has a repeat interval of 1, it will loop every
; n' _* [" \) i) [" G" Q1 |! e // time step. The action is executed at time 0 relative to1 ^( L2 }2 D- W, K( k
// the beginning of the loop.
& _$ x, {5 X( O2 I. E$ s- s' z/ Y( R% y3 y/ f
// This is a simple schedule, with only one action that is2 g1 W6 P3 z( c2 Z1 X
// just repeated every time. See jmousetrap for more
3 d% U% h* W, V" w4 \( c // complicated schedules.
. U! v; a2 Q8 B7 g+ E
+ S9 ~& x' r1 X+ R/ s* P2 S9 V; Z modelSchedule = new ScheduleImpl (getZone (), 1);
$ B# m/ c2 G# e% E. P modelSchedule.at$createAction (0, modelActions); K* v/ x. ^5 u7 F L. T! E& I
9 G+ @4 f+ S8 L8 p0 v4 W return this;8 k) K. |/ p" r7 z
} |