HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' H; J4 E, I3 ~( p* \
. j: _( R% f- D- z1 o
public Object buildActions () {* S+ B& M% f4 b; ? L5 \- j
super.buildActions();9 I6 O% K6 k' s' p, _$ }' `3 @7 P
H) ?4 R2 X# L6 _
// Create the list of simulation actions. We put these in+ R! U4 y6 t. g! n! _) W0 E
// an action group, because we want these actions to be/ G+ E) u" I# q! x9 f6 M
// executed in a specific order, but these steps should
3 h7 B k8 [( ^5 M$ a) u; e // take no (simulated) time. The M(foo) means "The message
. d% F6 c% z S7 E9 W' S$ [8 v3 f // called <foo>". You can send a message To a particular
( ]$ T. a) t: @' [ // object, or ForEach object in a collection.
) R( P2 T8 E4 A( w, D: @1 { : f9 X' I, M9 Q# x& k' P0 `5 _- _
// Note we update the heatspace in two phases: first run
6 G9 g, |& D) S. @ // diffusion, then run "updateWorld" to actually enact the
3 S3 D4 D' P6 b* @/ Y& w1 O. M/ j // changes the heatbugs have made. The ordering here is; ^, t$ e/ V. d7 h2 V! [4 V/ }0 Q9 K
// significant!, I; P- n$ C; t# @3 W
. ?6 w# c/ B+ |) ~( F0 W // Note also, that with the additional, @% ^ X$ ]& R" T
// `randomizeHeatbugUpdateOrder' Boolean flag we can) z3 b" p, C) | o1 Z* a7 Y
// randomize the order in which the bugs actually run
, g5 {, w; g' L. @2 a7 Q // their step rule. This has the effect of removing any
# N9 I0 L9 _; e* J6 C // systematic bias in the iteration throught the heatbug
+ u+ Y8 b1 w$ a4 d2 g7 G // list from timestep to timestep3 a: G5 `5 k, A0 J& P
7 G" Q4 H1 S7 b! g% r; ^ // By default, all `createActionForEach' modelActions have
5 N2 }* u5 I; ~' \) n- Z // a default order of `Sequential', which means that the
' @; }. X. A2 e I6 P9 I // order of iteration through the `heatbugList' will be3 D/ k; e$ Z! Q+ S" R2 }
// identical (assuming the list order is not changed
4 F' ^- X3 E4 T% |+ @5 K // indirectly by some other process).
( h2 p0 i: {7 G0 b' o, W, w* b & a) J9 K! o) _( D' H1 f. j
modelActions = new ActionGroupImpl (getZone ());
# f4 C6 ]: e8 u) D, n+ n/ v7 G' y I0 M" F, g$ \+ h& F$ r; r
try {+ D9 X- a' S" T
modelActions.createActionTo$message( }7 e, d0 ~7 C4 ~2 X0 t5 P
(heat, new Selector (heat.getClass (), "stepRule", false));
3 c7 a3 v, E, @. W3 x7 b' {9 v } catch (Exception e) {
1 O/ I7 w2 y! Z5 a+ X9 w- q System.err.println ("Exception stepRule: " + e.getMessage ());
5 ~, r: f! Q7 M% J E, ?7 \' P }
$ ^9 {% Z% W& N
0 d" U! a( }4 N V- ? try {
4 Z: @2 z$ K. p6 b* D Heatbug proto = (Heatbug) heatbugList.get (0);' R, Q T: y1 W, \
Selector sel = " O: s: h4 B+ L. Y0 ]
new Selector (proto.getClass (), "heatbugStep", false);' e% h9 y# ?( o
actionForEach =
/ p6 i/ m7 j p! m T: ]! h modelActions.createFActionForEachHomogeneous$call, T, k& _5 H7 o. D) R' H! v
(heatbugList,6 u n3 Q5 M* |8 Y
new FCallImpl (this, proto, sel,9 y+ `8 g" q1 l8 O7 ?
new FArgumentsImpl (this, sel)));% g; |% p0 p: _. l% Y
} catch (Exception e) {
# z: a7 j/ D" m5 y6 V8 ` e.printStackTrace (System.err);; K& O% q; W3 T3 q) _3 {
}
X" X: C, y& U- S2 O. ~
) q5 H0 V$ Z+ w" l2 n8 x7 f& ` syncUpdateOrder ();7 V9 j" s% ~) i4 y$ e' T( U& m
w0 U! Z# h' j" q
try {7 O$ e6 u" ~9 `6 ?/ i( \' l
modelActions.createActionTo$message # O/ v* X. A1 `7 D* X
(heat, new Selector (heat.getClass (), "updateLattice", false));+ J& k% F/ k. i, J
} catch (Exception e) {
' | k0 k8 G# |. i' o' X! s System.err.println("Exception updateLattice: " + e.getMessage ());
. M/ E9 t' P( \2 P }' H( Y5 P# g X/ o7 y% V
0 R9 z" D1 N* H0 T3 V7 G // Then we create a schedule that executes the" Z8 O. f6 x. N6 v
// modelActions. modelActions is an ActionGroup, by itself it8 z3 Y( M' O; d$ V5 G$ d. P$ ?
// has no notion of time. In order to have it executed in
3 e2 F8 d5 Z( C8 X. m/ } // time, we create a Schedule that says to use the
/ `. B4 m1 b5 `4 l( `; J$ a // modelActions ActionGroup at particular times. This: w) P7 M/ k* ^3 u
// schedule has a repeat interval of 1, it will loop every
) v$ g) z9 V/ S i, r // time step. The action is executed at time 0 relative to
- H, s/ o- v' }9 z$ c3 ? // the beginning of the loop.
" X9 P; J( C7 z) t, j, Q, X+ P. Z# o9 u
// This is a simple schedule, with only one action that is8 g9 V! V8 \1 ^& R; d/ V
// just repeated every time. See jmousetrap for more. L. z. @5 {4 z
// complicated schedules.
' [" q7 L V" C8 F% A3 P & M7 z5 i2 o' @
modelSchedule = new ScheduleImpl (getZone (), 1);
% v @; x: B6 ~& P modelSchedule.at$createAction (0, modelActions);& y$ Q! q$ [$ T! e$ V
& M: }9 z+ ?# p0 R return this;
/ d' ?5 c: H$ v1 n# P. x$ E } |