HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% ~( S0 ?5 l+ {) I
( f' V5 I% T5 v: V4 ]$ y9 n3 h; E public Object buildActions () {
0 u+ N/ i d* v R8 M1 b1 Z% c super.buildActions();* R) J5 q% E5 }3 y
2 w6 D! I, {6 z2 V // Create the list of simulation actions. We put these in1 u* V0 f% @0 L# r' Z, N4 ]
// an action group, because we want these actions to be
* n7 Y o9 q5 g* k! {, Z5 X! ? // executed in a specific order, but these steps should
. H' r7 v0 p& {& X8 u // take no (simulated) time. The M(foo) means "The message
2 c9 V7 |+ s: ^+ f5 U1 q# w0 W // called <foo>". You can send a message To a particular
* J- O* B) B* C$ D7 u // object, or ForEach object in a collection.
/ [- c4 C$ e# N& D) J; q# ^. u ' I' W# L' [# B7 |) @* D% H. n
// Note we update the heatspace in two phases: first run% q0 p6 q1 @0 B/ D3 e% ~( E. Q0 K
// diffusion, then run "updateWorld" to actually enact the& L( u4 O! V s4 i% ^: }
// changes the heatbugs have made. The ordering here is
" ?4 i. _4 {- n. O // significant!
7 r( y. ?! \: y2 c. c0 j
$ t+ O0 X% |2 I; ]5 H% T // Note also, that with the additional
, i! O/ Z7 l9 p! ]/ W // `randomizeHeatbugUpdateOrder' Boolean flag we can
& I7 E! O; f# Y/ q$ B! @8 y, G // randomize the order in which the bugs actually run
9 s; \! |8 a. V% Z8 z1 u% {, x // their step rule. This has the effect of removing any
: g( g8 U5 V6 x% ]9 b; O: B // systematic bias in the iteration throught the heatbug
3 d9 w( I0 u$ T" C' y9 V/ m // list from timestep to timestep0 I' C1 ^$ Y7 w9 _" R( K( V: w$ x
, A% y6 a" ^+ Z // By default, all `createActionForEach' modelActions have! }( Q q4 T* W! [( Z/ u7 O$ Y$ o) f0 C
// a default order of `Sequential', which means that the
0 a6 C/ B$ s+ J- f4 ` // order of iteration through the `heatbugList' will be% S1 t5 E5 C" D( g! Y
// identical (assuming the list order is not changed
0 B. U; J$ m) e // indirectly by some other process).
' p4 w! H; i; o. u% P% I7 | : o9 s; f. G# E+ s4 t
modelActions = new ActionGroupImpl (getZone ());& O8 s- H4 h/ C
, I( N9 l3 K, t try {
5 e, H* ?$ ^" Y$ i, V( C, `7 \+ l modelActions.createActionTo$message
7 O- P2 f2 ^8 O* F+ |) X/ d (heat, new Selector (heat.getClass (), "stepRule", false));1 y3 w4 F2 r# m+ r
} catch (Exception e) {# w) r: U8 J; g8 O- o
System.err.println ("Exception stepRule: " + e.getMessage ());
! ]4 a& U+ Q) |0 d% I }" X0 W8 A1 c8 m
# ^6 q: d$ y$ Y0 V7 |+ D try {+ C \; ?4 w* ]3 H( c
Heatbug proto = (Heatbug) heatbugList.get (0);
0 q6 w7 s9 N/ y0 L+ @ Selector sel =
4 p; W2 c$ n* k% E5 E new Selector (proto.getClass (), "heatbugStep", false); U% q# H4 F) ~& Z4 x9 k
actionForEach =) b& }9 s/ A, i5 E& ~
modelActions.createFActionForEachHomogeneous$call% [! n" M" J7 y/ U& V8 i
(heatbugList,. [# n3 |3 l- V% k) y( \
new FCallImpl (this, proto, sel,, t: m# f' v' |( T+ S/ m. m9 ]
new FArgumentsImpl (this, sel)));
, r0 o, ^7 a* t& g! ^. k1 |# X } catch (Exception e) {2 [3 L: L6 A" H
e.printStackTrace (System.err);
/ C$ s0 o6 j- E) z- v }
8 F: I' V4 [6 C7 ~5 ]9 ]
@. K$ F4 Q. ?: {1 u syncUpdateOrder (); ^" y$ l. b `: @+ d. h
. P' Z2 C/ c4 {
try {
2 n q( |! {1 e4 z modelActions.createActionTo$message & }2 Y$ @9 x0 \( C; k9 y
(heat, new Selector (heat.getClass (), "updateLattice", false));" V) V9 t- _3 u+ ?
} catch (Exception e) {
. ^9 @ ?0 \0 O( h! M System.err.println("Exception updateLattice: " + e.getMessage ());% S- X$ z% e, l: s8 m$ s! u
}
: U2 S! k8 N$ c+ s: b1 ] w! @' f 6 m! d) w8 ^! B; ]! Y8 {6 N! Q6 v' l* q
// Then we create a schedule that executes the# z {5 a" S: S' ^
// modelActions. modelActions is an ActionGroup, by itself it2 z' q+ ]% E4 v A; |7 g' P/ l
// has no notion of time. In order to have it executed in. l3 E# W5 u: p; d, o( D* O
// time, we create a Schedule that says to use the8 Z% X; E5 V" M1 u3 Y$ w( D9 v
// modelActions ActionGroup at particular times. This
( S8 j* t2 L9 E$ ?5 O. K // schedule has a repeat interval of 1, it will loop every
/ l2 ~% {% P6 s! z) R: U [4 r // time step. The action is executed at time 0 relative to- j' ?# g+ S2 w o% I) k, E
// the beginning of the loop.
7 _* t1 V. V# r% x9 m3 `
: W K0 q% E) q9 R& s$ I // This is a simple schedule, with only one action that is" m+ j/ W; }5 \9 z' k- S2 d
// just repeated every time. See jmousetrap for more1 C+ E" U: r3 y1 L3 y
// complicated schedules.
: _: G! \! g3 Y 7 b4 x( n" @* T! r9 Z" v+ j
modelSchedule = new ScheduleImpl (getZone (), 1);
& N6 s& I J& @ modelSchedule.at$createAction (0, modelActions);
) a, J z2 Q6 q 8 u v' i5 n. a6 M0 y& y
return this;
- a* a( k7 Z7 r: `0 s1 U( I5 S } |