HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 M5 Q5 f" G7 K: [8 A6 I6 ?& X
8 m- R/ }6 V5 u' q: T5 O/ y: f! u
public Object buildActions () {
! S, F" b: |! g2 H) O% r8 I. I super.buildActions();, d* J! h, }4 k! |" p1 r
. v1 W' Q+ g' q7 I; _7 k2 c! y' F+ V. y
// Create the list of simulation actions. We put these in
$ x3 p4 m* z, I9 w; Q! }4 i- \/ Y // an action group, because we want these actions to be5 \8 g/ s- [" ^3 G$ F3 m
// executed in a specific order, but these steps should, v2 w/ |: u6 D O% G
// take no (simulated) time. The M(foo) means "The message
4 W o! \. {* I, p9 G2 @ // called <foo>". You can send a message To a particular% v0 z F3 y: f* z6 A& f( Y
// object, or ForEach object in a collection.# I H: R: y) D
: d) i6 u7 |" U( d& o# ~
// Note we update the heatspace in two phases: first run
7 h7 M4 j7 g) e1 I+ z4 u // diffusion, then run "updateWorld" to actually enact the
4 i5 M' v. S4 g( M$ w- V // changes the heatbugs have made. The ordering here is7 }# R( h0 D0 V: F" d7 n& E
// significant!
5 W0 s. [& e+ {" g; f
3 @' E& S d# Y // Note also, that with the additional
% ?& B0 H4 q4 X6 T // `randomizeHeatbugUpdateOrder' Boolean flag we can
7 y" g4 {8 F! c% ^$ w // randomize the order in which the bugs actually run
: f$ T0 i" Y& ] G2 n // their step rule. This has the effect of removing any# I/ `4 K% V2 C5 l6 Q
// systematic bias in the iteration throught the heatbug/ Y8 [) E7 @& d# R8 G% w# r
// list from timestep to timestep
$ j7 N: Y0 G. @
" S) F% ~# R& u // By default, all `createActionForEach' modelActions have
6 S9 h9 ]7 T# k7 \/ B& m0 ` // a default order of `Sequential', which means that the( l% k" c2 N; I4 m: k. O' y
// order of iteration through the `heatbugList' will be$ }8 Q5 t- \) [4 }, q1 b
// identical (assuming the list order is not changed, A; L; G7 l! v1 h- [2 C
// indirectly by some other process).6 f, q* \+ } R
! Y+ k- G2 W6 G# N @ modelActions = new ActionGroupImpl (getZone ());
2 z' }- r9 q5 h* ?/ X6 y* Q+ G9 ?) `& J8 `: V( T8 n, ], \
try {
" n n8 k1 H. }5 K5 V6 J modelActions.createActionTo$message
" i3 O$ O8 z2 \5 S, O* n8 g7 p (heat, new Selector (heat.getClass (), "stepRule", false));* O1 ?% D# }' k; }
} catch (Exception e) {- r' I- l8 L5 S' X3 W* ^
System.err.println ("Exception stepRule: " + e.getMessage ());8 q" q8 v3 z5 L" k
}
0 q- i! u% Z; I8 g& `# l7 {4 [( f4 v0 y) n# {: S
try {
3 J1 o. H) G( q* i, D" S: ?! l Heatbug proto = (Heatbug) heatbugList.get (0);
) P2 E7 u8 W4 b" G: }1 Q Selector sel =
9 f0 |) O, m8 T8 u& g5 N( L. j new Selector (proto.getClass (), "heatbugStep", false);
% U! w3 \- G! Q2 ~( `8 I$ r actionForEach =
3 W; l; H+ \# x; ~& J modelActions.createFActionForEachHomogeneous$call
6 ^4 j& |9 w4 M1 k0 E (heatbugList,
' [5 M" l# @# B% C% s. x: I+ p& _ new FCallImpl (this, proto, sel,) P! g. |* T( H0 s0 B' @9 N
new FArgumentsImpl (this, sel)));
4 o6 N6 q8 b* i, c$ e8 _" j } catch (Exception e) { V8 w: f# A4 S1 x9 Y) M
e.printStackTrace (System.err);
, d. M6 h# @2 R. a) ~9 l }% e0 t" }& O8 J
8 L4 A9 S8 B7 v+ g* b
syncUpdateOrder ();, W) \; {" ~1 J% O& y' o M' A
' d% A% P) I P [ try {1 h/ u; s: D& B4 |) Z
modelActions.createActionTo$message
2 N. n/ f( L/ q: @, Y$ m (heat, new Selector (heat.getClass (), "updateLattice", false));
2 d: {2 h5 k8 E1 W) j! A } catch (Exception e) {/ Y p; a! o6 F5 m, D# J! g
System.err.println("Exception updateLattice: " + e.getMessage ());
6 F' @. c; N H4 T# j% J }
( `8 s/ W7 e4 d8 e, G + ~' f2 u5 U4 }# m% P" O4 y+ N
// Then we create a schedule that executes the' G2 _3 n) M; I, N4 k5 s) i
// modelActions. modelActions is an ActionGroup, by itself it
, a9 K! |" ~5 `* `5 j& t$ i9 K // has no notion of time. In order to have it executed in9 P# S) Y5 q1 k- G
// time, we create a Schedule that says to use the3 l+ i+ {) i0 \0 j6 I. J5 x$ ?
// modelActions ActionGroup at particular times. This
+ i2 S; b9 _$ ?5 E // schedule has a repeat interval of 1, it will loop every
/ c5 Q; z& Y# k( w. j3 w // time step. The action is executed at time 0 relative to
1 P7 p! }8 M+ R' g // the beginning of the loop.2 ~7 n+ u3 s! L; j$ s
; w5 M. H+ e! k: G
// This is a simple schedule, with only one action that is4 I' ?. Z# s( b% r
// just repeated every time. See jmousetrap for more4 V# @6 ?. o r. q4 C: X
// complicated schedules.2 U. S% h! p) @2 q1 M0 Y. E3 {
6 _! R) m) z' [* [7 y
modelSchedule = new ScheduleImpl (getZone (), 1);6 L# k: F% A4 F
modelSchedule.at$createAction (0, modelActions);
/ J2 H ]1 o j. |: p, l0 s 4 [3 t5 ^2 r$ K# \% `
return this;
U2 y, A2 q% a" G# k# t* M } |