HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 k% D" f( X9 |4 P! m6 T- j
% W: b0 W. J6 X
public Object buildActions () {3 c( p9 A% g# D0 }% c8 C4 Y1 i
super.buildActions();
: i" O7 J. N& G& N+ s9 {
! G- G% f: o* x# a+ g // Create the list of simulation actions. We put these in* D; E ~5 c7 @/ r0 d
// an action group, because we want these actions to be
* d# i( e, Q3 n3 z // executed in a specific order, but these steps should
* ~$ H8 h# `( c$ i4 S3 h/ O! p // take no (simulated) time. The M(foo) means "The message
5 N" B2 p9 @) O, V // called <foo>". You can send a message To a particular% h, v) _/ ~8 I5 W( L- `, e* d
// object, or ForEach object in a collection.
% Q& K, w9 F# { x0 L8 [" |8 g1 W 1 r6 W# |2 D( s, }( r+ U
// Note we update the heatspace in two phases: first run
- N+ M& d/ z* e // diffusion, then run "updateWorld" to actually enact the0 A1 ^8 {+ \' N# O" j
// changes the heatbugs have made. The ordering here is( a% K3 [3 Z* d$ N' H* ~5 f ~ P
// significant!
! d+ l4 O: A* F: z1 e- S ) w! w% r+ z( ~/ B) R9 A
// Note also, that with the additional
* E8 O: l, i/ Z5 R, w: s // `randomizeHeatbugUpdateOrder' Boolean flag we can4 G& n" ]) U' u) A( X& [
// randomize the order in which the bugs actually run, W" _3 d( B- D
// their step rule. This has the effect of removing any
+ V2 _$ D0 o6 k! V4 p" H, ^' i* u // systematic bias in the iteration throught the heatbug
5 ?1 C2 T- B6 {: Q$ n2 N // list from timestep to timestep3 P- \# n b5 S/ I, ^! q; H
. Z' O" }" D0 D
// By default, all `createActionForEach' modelActions have% Y2 z! }; O ^" L9 U: x
// a default order of `Sequential', which means that the* t Q% C6 d. U# {' U) E4 B8 y" b
// order of iteration through the `heatbugList' will be/ o8 H) F2 v6 y3 i: Z9 T
// identical (assuming the list order is not changed
q8 Z5 ^* b/ n- t1 G+ J: ` // indirectly by some other process).3 b: D% v" N; H0 C" S+ B, x
, g( y8 `/ [+ R7 { n) B/ J modelActions = new ActionGroupImpl (getZone ());
, `7 l E' w+ c9 H9 |0 q O" X
, z' b! W4 a: f try {) ^6 D( M$ m- _. y, P# u
modelActions.createActionTo$message& g8 X( K# i; l
(heat, new Selector (heat.getClass (), "stepRule", false));
1 [9 w* k1 q: m9 J I2 ^) W# X } catch (Exception e) {! x! v: P U) m/ b0 g# G3 ?! }
System.err.println ("Exception stepRule: " + e.getMessage ());
; P# ^: }" g0 t; _6 I$ t* o' x }2 p2 p# B" q, x2 F, ?9 X
! E) X1 A2 M! x/ E7 C try {
! s3 ?0 z- S& S C8 f, o8 }( U! u Heatbug proto = (Heatbug) heatbugList.get (0);2 N3 G+ u4 _. Q/ L$ Z; p& a0 }
Selector sel = & K l7 r+ D5 V# L* A) k& x$ Y1 u, U
new Selector (proto.getClass (), "heatbugStep", false);
+ Y- y: q. e/ B5 e! F+ S actionForEach =
) x$ Q8 B. \, n) u modelActions.createFActionForEachHomogeneous$call4 E# e A: j' S a. c+ G
(heatbugList,
3 k" w; g; b5 Z0 x new FCallImpl (this, proto, sel,
( |! G# g1 ] s u# y new FArgumentsImpl (this, sel)));
3 i- }" I; z g9 J5 B8 r } catch (Exception e) {# Q8 A! E0 O) R, }
e.printStackTrace (System.err);4 E) H4 Q2 ?. f7 u4 d
}
; f$ B6 ?& M: o$ H/ ^ 2 }: w8 a$ ? u" M. O
syncUpdateOrder ();
" y/ A& }6 I6 g' x j2 m0 Z! }) ^, a, `0 r: d9 Q
try {* l5 Y4 P8 b) P/ Q( e, |1 g4 K
modelActions.createActionTo$message ! [6 M0 \% P4 ~1 k
(heat, new Selector (heat.getClass (), "updateLattice", false));
; |+ B! S+ J$ p6 ^2 ? } catch (Exception e) {
6 M; R% s2 K( c9 |) E8 t3 ] System.err.println("Exception updateLattice: " + e.getMessage ());
, x4 j5 L C4 L$ x7 P! `. v }
& {" y2 ]/ x( k8 q " T' y* [( ?" `7 D
// Then we create a schedule that executes the
- i. F" R8 V' P% K+ k1 j // modelActions. modelActions is an ActionGroup, by itself it
% L+ i' V5 u/ \/ \/ B7 i8 Y // has no notion of time. In order to have it executed in
- p8 C/ r8 d, k6 q |# D // time, we create a Schedule that says to use the
" h8 S- e- h E8 E* }/ l! J2 @2 M2 U // modelActions ActionGroup at particular times. This! i/ F; ]" f1 A; Z! N5 }' B* g
// schedule has a repeat interval of 1, it will loop every
# h) t# E) _+ x5 D! F+ j6 d // time step. The action is executed at time 0 relative to
4 T+ s- U$ C' m: r4 V // the beginning of the loop.; k' z! _6 _. u5 F
' q7 K8 f( }/ b
// This is a simple schedule, with only one action that is
/ }6 e' |" s2 ?4 \5 U( P7 U, ~( c // just repeated every time. See jmousetrap for more
: ^1 Y; i8 x' d8 C5 ~. o E8 y // complicated schedules.
% {, r( a" w2 m1 R/ |% P 9 i% R. [ V" e. [8 T
modelSchedule = new ScheduleImpl (getZone (), 1);; \1 B) T5 W6 X0 B- n2 K+ n# |
modelSchedule.at$createAction (0, modelActions);
. \- S2 Z7 d7 [& j9 \
1 H4 ^. F7 q4 Z" H4 o' ` return this;
' o: q8 d7 W, e5 w% v2 v } |