HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 ?# F4 C( k3 v G; J8 A8 C
4 J# G/ D) B% z7 b$ e! F9 j public Object buildActions () {# |8 U3 m) R/ {7 k: L# `4 ~; T
super.buildActions();' ~5 Y# g0 {3 y4 T: p
- k A9 Z5 z" _. o/ }/ W* L // Create the list of simulation actions. We put these in
, g+ v# R. r+ |9 H& {3 Y0 N2 M // an action group, because we want these actions to be% I& l0 t, d" h
// executed in a specific order, but these steps should
2 z( J2 q6 S: t4 h: @+ S4 g1 g) R // take no (simulated) time. The M(foo) means "The message* e9 @: }+ O2 S, s
// called <foo>". You can send a message To a particular; A* s8 r# S3 t+ ?
// object, or ForEach object in a collection.
$ ~6 L% O- V6 P+ v1 t& J2 ^3 _: d
! i4 R$ [) i1 R // Note we update the heatspace in two phases: first run* M2 B8 b1 f: I! @
// diffusion, then run "updateWorld" to actually enact the: R e2 u( R0 E( {. _
// changes the heatbugs have made. The ordering here is3 j% W9 E4 o& S; Q1 A2 J, s! Q
// significant!
& J8 o7 R7 } V7 c6 V
9 \/ A, s' I& D) y2 }% t2 K0 \) _9 G // Note also, that with the additional
8 D8 M$ A2 M, m, o6 u; _; }$ B // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 M, N8 z/ f' j+ ? // randomize the order in which the bugs actually run
# i* i7 Z' w o7 K* _9 y! `9 _ // their step rule. This has the effect of removing any3 j3 a+ d6 h7 W9 \: o) R* o* M
// systematic bias in the iteration throught the heatbug! `2 Y; m5 ~0 `$ i
// list from timestep to timestep
5 I+ e; Q$ w! Y: m/ R! c$ p1 C
1 j( h5 O, m' D. A" `! J. R; s // By default, all `createActionForEach' modelActions have
2 L7 a$ S) D% s$ F // a default order of `Sequential', which means that the/ Y/ h" |6 Z. E' a% l
// order of iteration through the `heatbugList' will be
5 Q* u2 S3 o$ n2 m // identical (assuming the list order is not changed8 S w5 \4 z5 ^7 W. ]+ q9 ~4 [2 q
// indirectly by some other process).
$ ~9 t+ h/ S9 K+ P4 @ 6 I! `0 d5 n& W2 R8 f3 L
modelActions = new ActionGroupImpl (getZone ());3 H0 ?1 _" R- C, P/ K f
) i$ ?; G, z; } Y try {
- C1 Z) b6 N% @- a' d: c, r* ~ modelActions.createActionTo$message" j K; C/ C3 L: s# ` b+ k# u- l
(heat, new Selector (heat.getClass (), "stepRule", false));
1 r0 m+ @! B- ] Y } catch (Exception e) {
; i0 W+ Z2 W; p4 E4 r3 Y- ` System.err.println ("Exception stepRule: " + e.getMessage ());- O6 p8 ~. }6 M' [& I
}
8 j- g* T9 K) e/ `5 u& J9 Q( j* Q- i0 J( v7 \
try {+ U1 Y" t* g* s& {
Heatbug proto = (Heatbug) heatbugList.get (0); i% w2 p) L4 f# t5 ?( \
Selector sel = ( w9 i: h. W! ?* c# p' a; y
new Selector (proto.getClass (), "heatbugStep", false);
/ e1 x9 \6 V+ y6 A actionForEach = P7 r U# |6 t0 G; {' }' A
modelActions.createFActionForEachHomogeneous$call
0 S: T- h+ O" N: f7 G( q (heatbugList,
- Q9 S" J# y! X new FCallImpl (this, proto, sel,3 L# k0 _: ~( M- E, w& ^$ z9 y
new FArgumentsImpl (this, sel)));( ~; ~, N7 K) {& C; d9 m) U
} catch (Exception e) {
$ j+ _2 N% l9 f5 ? e.printStackTrace (System.err);& v, a$ A3 G8 w4 T5 \
}# x2 d, _9 D9 z/ b! B& f9 ?
$ h; Q' X p: d- z2 ^) l syncUpdateOrder ();
3 {+ C( P1 M P& {8 f2 r+ E: a6 x# f: _, N) \
try {$ a5 u, }2 q7 E1 I5 d
modelActions.createActionTo$message , I" x, M7 A% H; ^) |
(heat, new Selector (heat.getClass (), "updateLattice", false));0 L8 v# u8 [5 k0 ?6 |7 g
} catch (Exception e) {# B0 ]" x0 Z7 n+ y& F& P [ B2 h
System.err.println("Exception updateLattice: " + e.getMessage ());7 h( o; G) L6 j" i8 W* ~
}' Z/ L% Z0 s# o6 T% D
( ^* A3 ^+ {* z6 l9 E6 B. d+ j // Then we create a schedule that executes the
1 {3 a9 a3 g7 f // modelActions. modelActions is an ActionGroup, by itself it# U; ~2 |9 ^; a' l L- C( q6 x1 @! w
// has no notion of time. In order to have it executed in
, C( y" Y Z& e/ J5 N // time, we create a Schedule that says to use the" ?* S5 I+ b8 F
// modelActions ActionGroup at particular times. This# H0 D, W" w; ^' N# N
// schedule has a repeat interval of 1, it will loop every
9 _5 e/ q. l- o& N7 e // time step. The action is executed at time 0 relative to$ l* y# v, `" i, R- s( l# D1 L
// the beginning of the loop.
6 j7 u0 N+ k2 K; m- ]4 J9 {5 s2 a- `2 z3 \! G) d& R' o
// This is a simple schedule, with only one action that is% n# }! T( `' \# e
// just repeated every time. See jmousetrap for more
* a% H- c8 A# f' z& N2 b3 ] // complicated schedules.
4 g2 q* t& u# D) e+ U: X- W
$ l4 N7 b$ U+ `6 y6 U modelSchedule = new ScheduleImpl (getZone (), 1);2 D7 S, e5 p2 `. U
modelSchedule.at$createAction (0, modelActions);
* J y2 [% B: X, S+ k/ c1 Z1 Q! T; l
. v* I4 l% a; {( P# {6 |/ ^9 L return this;6 `9 @. b: A# g8 q7 i
} |