HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; }" h1 J$ h% F L6 A
# |! _/ n& y0 [9 U public Object buildActions () {, k. \( h- s2 d% S) ?# B; P
super.buildActions();" _: p$ E+ ~3 X% x! w1 @
6 P6 G3 h2 B: _, U/ A
// Create the list of simulation actions. We put these in: q7 g- ?* X5 W, A8 T* N
// an action group, because we want these actions to be: ]8 n% n9 @, ]) h( _
// executed in a specific order, but these steps should- \3 B( y3 F2 b$ u& B
// take no (simulated) time. The M(foo) means "The message- [) C ~2 I9 M/ [: J" Z
// called <foo>". You can send a message To a particular
) U( d0 E/ U X$ L // object, or ForEach object in a collection.2 M+ r. N: V. Q
2 L/ \; z' |9 p7 }* a- V1 L
// Note we update the heatspace in two phases: first run
& N! j" W5 P4 i: R2 W* {9 | // diffusion, then run "updateWorld" to actually enact the: j1 b0 ?* o6 R
// changes the heatbugs have made. The ordering here is
9 N8 R5 @ Y g/ m/ t% O // significant!
6 G: g( c ^$ w8 e" k; l 9 N; N, W2 d k Y; G. H
// Note also, that with the additional! W% m6 }: V. \; n) M
// `randomizeHeatbugUpdateOrder' Boolean flag we can- b) Y- G8 E, ^; f
// randomize the order in which the bugs actually run* j! {$ }5 l1 M( S! K6 ]
// their step rule. This has the effect of removing any; m& h+ W' H) C
// systematic bias in the iteration throught the heatbug8 G0 Z5 N3 a$ T' }
// list from timestep to timestep
8 B" g- Q: Q' Z
+ p4 N# _5 A# x9 s: Q5 I // By default, all `createActionForEach' modelActions have0 z% b& W+ @9 J8 h
// a default order of `Sequential', which means that the' w, E# X3 v# [' m) d. T
// order of iteration through the `heatbugList' will be: g- j9 \) \7 D
// identical (assuming the list order is not changed8 ~0 {& Q2 W1 H+ ]2 A# ~" p6 q1 {
// indirectly by some other process).
* ^9 i( b; h' e
7 l8 V, Z$ T( ] modelActions = new ActionGroupImpl (getZone ());6 p8 n/ g2 ^7 P, M! L! S3 B6 v% |
0 q+ G3 I6 t: R6 ^; P" t" G try {6 u/ a% n! N$ W& a; _" v
modelActions.createActionTo$message
, | b" |! ^3 V1 J! ]& x6 {9 G (heat, new Selector (heat.getClass (), "stepRule", false));
3 @9 y* Y) l7 r' N8 w9 l1 \5 A } catch (Exception e) {
2 R' O* V1 F. z. T5 ?3 ?5 C2 M System.err.println ("Exception stepRule: " + e.getMessage ());, P- j+ M8 U/ D+ }) x
}8 z8 n& D9 F. _1 G% l
* Y, L% o* A/ {) w try {
+ n7 s* J# r% }/ S4 R) D& k& R Heatbug proto = (Heatbug) heatbugList.get (0);
& A$ v1 |3 u, ]9 Z Selector sel =
& X8 d% O; Q. U$ C+ H8 e: V, K& N9 k new Selector (proto.getClass (), "heatbugStep", false);- L6 U. w) R$ w
actionForEach =
4 F3 W1 ]: K; x* a+ C2 R modelActions.createFActionForEachHomogeneous$call: E0 V" r$ C3 a i O2 P
(heatbugList,
! Z7 F) ]% [: F* Y, i; x* } new FCallImpl (this, proto, sel,
- y: X' G) Z# ~- d; H9 Q6 } new FArgumentsImpl (this, sel)));
" D G8 R* P$ W: C0 V# x2 b2 @! X } catch (Exception e) {5 J" l' @5 F8 {* U8 u8 W" ?- ?
e.printStackTrace (System.err);$ ?7 D4 \1 t% b" E' S; v, W! [( ]9 v
}
! I7 j3 n+ B6 R0 ]$ B9 Y5 q5 G: i4 i
# R: W2 ?: n7 t3 q$ V! S syncUpdateOrder ();
& ~& u M% ]9 W) @
/ z5 A& ^8 N* e+ `3 \ try {
; e4 T" ?$ p+ l9 P# c4 u modelActions.createActionTo$message . E/ q+ @4 o4 e$ c* A8 t
(heat, new Selector (heat.getClass (), "updateLattice", false));
J6 q/ r% P9 B$ w0 Q4 d9 N. u! t } catch (Exception e) {
# `6 o6 O6 Y; a! D# t System.err.println("Exception updateLattice: " + e.getMessage ());
' G6 u& W; O/ ^6 z% [ }( c; q$ Q% R! q
8 {: E, \, r& S" d8 g4 A0 R J' @5 z // Then we create a schedule that executes the
4 D) e" D: m) x5 P // modelActions. modelActions is an ActionGroup, by itself it
+ e3 }- g& x/ _! J/ W M$ I" V% s // has no notion of time. In order to have it executed in
% K) F) t @' |( L. C' g // time, we create a Schedule that says to use the3 ~2 x+ k: W! e# ?( o
// modelActions ActionGroup at particular times. This
2 t; h3 v( V" \( o // schedule has a repeat interval of 1, it will loop every' I# e3 M% e$ {' D
// time step. The action is executed at time 0 relative to2 b, f- s a( b" P2 P5 y
// the beginning of the loop.8 t( D* F# O0 k' {* g
: z0 r9 a7 b9 ~$ F5 O* E2 M2 I // This is a simple schedule, with only one action that is
9 I, q6 [ o/ B; z0 C6 ]$ } // just repeated every time. See jmousetrap for more; V; Z, _* W. y- f" b: B
// complicated schedules.
$ E3 \' T, R; @( P " @7 @3 T2 F/ j. R4 m# d* |
modelSchedule = new ScheduleImpl (getZone (), 1);
0 T' c. k5 b# z1 p modelSchedule.at$createAction (0, modelActions);5 y& A1 m( b8 S
! @$ W& H: C8 X; m3 T7 D return this;* _/ B/ I# y9 X& u6 `
} |