HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 o" X) v- R0 j
* a8 z) V; n% e, k5 f# W public Object buildActions () {8 T$ P u9 F+ J0 |' w R
super.buildActions();
: J. N, [8 ?# j7 t : B& j- j& P$ _& _/ I8 i
// Create the list of simulation actions. We put these in
5 y& n7 d& F* O) B // an action group, because we want these actions to be; V& S; W! v. r6 E2 |; n
// executed in a specific order, but these steps should
( ]8 n- U/ O3 Z1 h- m( U // take no (simulated) time. The M(foo) means "The message
$ w5 [4 {8 f) F6 ]: C4 R" ~ // called <foo>". You can send a message To a particular
/ v! G: _+ n4 N/ M# F4 y // object, or ForEach object in a collection.
/ Z- u$ q3 s( u7 B( w0 P
* [+ \3 y4 J4 I$ C$ o // Note we update the heatspace in two phases: first run: N9 l) w: Q g
// diffusion, then run "updateWorld" to actually enact the
* A& R" S7 |- L, | // changes the heatbugs have made. The ordering here is
/ m& N2 S. {7 }) o1 V // significant!
0 C) v- K; E3 x. x6 d# ^ & Y2 E* N3 K/ V) m* o0 S5 L. b) z
// Note also, that with the additional
* x4 l% z" M" z8 z: T2 u6 A // `randomizeHeatbugUpdateOrder' Boolean flag we can* ]8 J/ e6 g& q0 J. a
// randomize the order in which the bugs actually run
, t4 i `$ y2 m0 s2 _# n1 ~ // their step rule. This has the effect of removing any8 b( p D+ f- s5 c; U$ ^4 i! x
// systematic bias in the iteration throught the heatbug
3 [! @; ?4 s9 M" ] // list from timestep to timestep
{! }7 I, @% s, {/ |( ^
6 c: O1 r: W9 H: J' {% } // By default, all `createActionForEach' modelActions have
! I3 F) J/ p* G- w! ~! s F2 X5 T // a default order of `Sequential', which means that the" o n5 Y" Z$ {+ h
// order of iteration through the `heatbugList' will be
3 ~( I' Y0 O9 I // identical (assuming the list order is not changed
4 }4 t- ^, I9 O$ F* Q/ W // indirectly by some other process).
! i: z" w& D" F5 f % z( I. S" S- q4 z
modelActions = new ActionGroupImpl (getZone ());( V1 A, b) e7 v# ]! s
! n; Y+ x6 x! k/ \4 r+ b8 ` try {
; {: }) i* ?& }, u' t% C+ t modelActions.createActionTo$message- X2 i' W& f. R" }4 Z. [- E) R, C
(heat, new Selector (heat.getClass (), "stepRule", false));9 f3 t1 N* }# t5 b) p
} catch (Exception e) {
7 ] F- f$ ?3 C- n! l p System.err.println ("Exception stepRule: " + e.getMessage ());9 e6 H. n, g' }
}5 Q6 B+ e4 B: Y7 Y' E
% c# n/ H. S0 C2 i try {
- f& d3 N5 |* A3 X, c( T5 K Heatbug proto = (Heatbug) heatbugList.get (0);+ G. @$ W* C9 G: i: H
Selector sel =
) O% o# L; \3 G. D5 c; J new Selector (proto.getClass (), "heatbugStep", false);
% I+ P9 V# U; g7 i actionForEach =! K" U6 {* }% L% F5 |
modelActions.createFActionForEachHomogeneous$call# l' C% ~4 g/ q8 L5 F4 m Q4 E2 B
(heatbugList,2 H% W1 m# k6 h, B1 d7 d1 D9 Y! k
new FCallImpl (this, proto, sel,2 q2 }4 ^) R8 D) X9 y
new FArgumentsImpl (this, sel)));) _6 V- W) s) y# [1 C
} catch (Exception e) {
/ x, @1 d; u/ W* @ e.printStackTrace (System.err);6 [4 ^, M$ {% A& h! O
}
1 M, `7 l7 l) O7 K \. S6 D
) H& x. W+ @+ E Z! @, C$ E! O syncUpdateOrder ();* t$ R# G, |5 u
+ l$ J6 a: m. {; o7 p
try {
- w1 h6 R1 F% U modelActions.createActionTo$message , R# `' ]. c. w; |9 g" j" `/ H* K
(heat, new Selector (heat.getClass (), "updateLattice", false));
: o- F% f; ^- Z( T } catch (Exception e) {3 R5 o$ m1 s# f: Y5 k2 z% ^1 Z
System.err.println("Exception updateLattice: " + e.getMessage ());- I6 W7 J6 }8 R7 z
}. N6 Q- ] n( p+ w' l+ s2 A
! M' ~/ p. L6 y; K' v& f% D
// Then we create a schedule that executes the. {; z/ L- Q% F$ ] m: |" ~
// modelActions. modelActions is an ActionGroup, by itself it1 a) z& t" N1 M
// has no notion of time. In order to have it executed in
7 Y' D) o: @( {4 @1 l' c // time, we create a Schedule that says to use the
" m7 k4 i0 v( L, ]) ]9 o // modelActions ActionGroup at particular times. This0 H: c7 ~3 S# W8 A5 D8 J7 I
// schedule has a repeat interval of 1, it will loop every
3 Y6 o; w" R# R9 { // time step. The action is executed at time 0 relative to& Y0 `- B! }, R0 N+ L M
// the beginning of the loop.
; T* E. a: J! t2 W: y% G0 D% ?0 c; h) \8 J- l. s9 i
// This is a simple schedule, with only one action that is
, @* _, L0 O" |" R // just repeated every time. See jmousetrap for more
+ n4 _; x% i( J% u2 \ // complicated schedules.; e* o& ?3 D+ O& z
4 q' B) s$ w; S8 k! K+ Z modelSchedule = new ScheduleImpl (getZone (), 1);0 ?$ ?% ?* L3 U0 e& v
modelSchedule.at$createAction (0, modelActions);
, k0 C, _- {) A- _4 T
r* Z" b( N$ C return this;' u" F- f+ F+ `
} |