HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 a! _- ^+ A, G* `, A2 r, B
# G. R C. x+ D9 A, o. y public Object buildActions () {- b: w$ ]: f+ b3 n# q7 ]) i( E* t
super.buildActions();
& w1 E# Q; ?/ n/ M% z. A8 x6 N # P1 `4 ?3 R3 s4 Y8 Q* C
// Create the list of simulation actions. We put these in
2 B/ B7 h5 n3 p7 y // an action group, because we want these actions to be
' ?2 z" w+ Y! O. l; r7 Q, h // executed in a specific order, but these steps should3 x# U- x5 ]; `! f, o
// take no (simulated) time. The M(foo) means "The message
( e; o6 `9 p0 q$ Q // called <foo>". You can send a message To a particular0 R, ^- q Q& K% B* V7 R
// object, or ForEach object in a collection.
3 j4 \+ y- N; l
* R) Z% n5 q: l2 c! o9 ^ // Note we update the heatspace in two phases: first run. s+ [ ~# R$ A3 p; g5 i& {8 i5 ^
// diffusion, then run "updateWorld" to actually enact the
0 O2 H0 O& H/ A7 H. _2 x9 |" B // changes the heatbugs have made. The ordering here is/ Z! Q% |0 L* u/ b$ j: T: G
// significant!
9 y$ t9 U: w6 \. c; a . s9 A/ @2 r5 m5 @
// Note also, that with the additional' U Y: U2 p# d- m% L: F1 J) x
// `randomizeHeatbugUpdateOrder' Boolean flag we can
?. C) t% L+ R // randomize the order in which the bugs actually run' H! }% E$ O. m% U% s+ r4 a
// their step rule. This has the effect of removing any
2 ?3 \6 l' M5 r* X // systematic bias in the iteration throught the heatbug8 Z; C" l+ o! k2 A" b) U
// list from timestep to timestep0 F" p; q1 \7 f$ _' ^- T) m
# r. g/ \% Y4 x x // By default, all `createActionForEach' modelActions have. m, ]4 O. t% r
// a default order of `Sequential', which means that the
) a/ M& l" E! Y, P# {/ C // order of iteration through the `heatbugList' will be! W/ k7 [% x$ @5 n8 a1 q. D& [
// identical (assuming the list order is not changed: K3 }5 ]( y* G* e, a' M
// indirectly by some other process).
, G2 C0 b! F, X' ?$ r + H7 M% a- P6 c# \
modelActions = new ActionGroupImpl (getZone ());
/ {: h; D, T9 q) U1 M& X+ ]
! i( G+ c9 T0 C3 a7 | y4 ` try {
) c; X) J& E% R9 K( V/ C" O modelActions.createActionTo$message
! l) M; K, _5 R) r (heat, new Selector (heat.getClass (), "stepRule", false));
8 i: s0 M, f3 M6 ] } catch (Exception e) {) i* B3 y _1 P: w
System.err.println ("Exception stepRule: " + e.getMessage ());5 f9 C5 Y7 b2 ]9 q3 `4 R: w
}
7 V0 Q* I, S9 b5 c# _5 X- U3 ?: s/ W7 Z. }; D
try {7 L, j- W9 `; ?
Heatbug proto = (Heatbug) heatbugList.get (0);, a; U: s3 v3 P% z2 C
Selector sel = & G+ ~9 t0 a! n9 R
new Selector (proto.getClass (), "heatbugStep", false);6 g8 s8 h. ]1 N$ e& b0 T8 M
actionForEach =6 `% d/ x& \' A' C4 p4 {
modelActions.createFActionForEachHomogeneous$call
. k3 R5 K2 O; I8 x6 b6 R- Y( f$ y (heatbugList,% p( r9 G. H" d0 Y& j, E
new FCallImpl (this, proto, sel,: s% D; \2 X3 ~$ T1 `' Q
new FArgumentsImpl (this, sel)));6 \: A" ^7 G$ Q) }% s6 } D. f
} catch (Exception e) {4 r( q8 F% B H6 o& Y
e.printStackTrace (System.err);: n% J# A+ r# [. G0 Z/ t" L
}
3 q6 u" N6 H/ o. G: { j" m8 G6 y* V+ f8 D2 W3 G6 q, e
syncUpdateOrder ();/ [0 {: H& ?7 J, s" @1 \
* R6 |9 B6 E( O, @& ^ try {
, i5 I4 n3 ?& q* B, q modelActions.createActionTo$message
6 `# e0 {8 p9 q$ t' X (heat, new Selector (heat.getClass (), "updateLattice", false));
% O2 e9 [/ B! L } catch (Exception e) {
; }* X3 `' \ c( [ System.err.println("Exception updateLattice: " + e.getMessage ());
+ L5 {! V8 |, j$ ~$ y/ f# A }* L+ B7 I' e D3 T; D
' Q* f4 R- |/ j) m. L- M // Then we create a schedule that executes the R& t5 x+ I# `# J, m. P
// modelActions. modelActions is an ActionGroup, by itself it, h$ f0 }! _2 x0 [4 [$ Y
// has no notion of time. In order to have it executed in
m _7 x! @8 I- ]( `/ [9 u! H% K // time, we create a Schedule that says to use the
: t9 f! G* n* t7 {3 y // modelActions ActionGroup at particular times. This
9 N6 W) d* x2 X3 U) N // schedule has a repeat interval of 1, it will loop every# b6 E6 t) c+ x: E
// time step. The action is executed at time 0 relative to, o" Z. ~: K( }
// the beginning of the loop.7 ?% N7 c6 k; J9 g3 F+ X
@6 S( L. x. X1 X C7 I6 J( s // This is a simple schedule, with only one action that is, c- _# g( b! H' k6 V9 {; P
// just repeated every time. See jmousetrap for more
: l7 x1 ?% G1 B! N0 @( v // complicated schedules./ ~' e' L. r4 }& K
. {- e/ h* b/ |. C modelSchedule = new ScheduleImpl (getZone (), 1);8 A7 u) O: w; s, [5 G
modelSchedule.at$createAction (0, modelActions);/ G9 Y) h g$ l8 O i0 ^ Z4 W
" l7 [+ j; v- h2 b4 m* T! M
return this;0 ]% h: @1 [1 m1 @* O
} |