HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% z) q: l( L$ }& A5 ^, {) k, I" a
% m$ B; W3 q& H, L& U+ b7 b public Object buildActions () {
$ j, p& ]1 U+ Y super.buildActions();
. `9 k; a3 p2 S- q ) ~; G! Y8 m f! U1 h
// Create the list of simulation actions. We put these in% D! I* q. ]$ i# P: A+ {
// an action group, because we want these actions to be
9 r; K8 W; x) {! ^: x // executed in a specific order, but these steps should% F- O$ T) T6 f4 q3 T1 C6 E
// take no (simulated) time. The M(foo) means "The message
/ x4 w4 o# N% a3 ~' Z& J // called <foo>". You can send a message To a particular
& k# l; c. V t& v* q) b+ B // object, or ForEach object in a collection.
. b, @9 I/ r) Y+ ]3 w! m1 r
6 H. R0 m. H* h! K _, } // Note we update the heatspace in two phases: first run
( l1 J; V; O9 z6 R: L' M // diffusion, then run "updateWorld" to actually enact the
% Z. k% S0 S& E' e8 Q // changes the heatbugs have made. The ordering here is
! I8 _3 O+ |) D5 @ // significant!3 ?0 i& G+ _8 M1 {
9 d" k$ i* _. y$ c% A% h0 I( p // Note also, that with the additional5 P2 t7 D4 X( u. s
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# s s6 Y1 O( k, t // randomize the order in which the bugs actually run
' [( t N; a P4 q) F p // their step rule. This has the effect of removing any
1 M3 }2 C) E6 R1 ~3 k* d0 l+ o // systematic bias in the iteration throught the heatbug5 j% j$ I5 s, q8 x% G" H3 Y! X( t
// list from timestep to timestep
5 u& y$ d! w& l9 _1 b + u. l+ P9 W2 f3 x+ ]
// By default, all `createActionForEach' modelActions have" x, Z3 n( R( ~5 l6 C# Y% i
// a default order of `Sequential', which means that the
# x6 `2 n$ N' `7 ]( R! _ // order of iteration through the `heatbugList' will be
) N$ W& E9 d- @( K8 V/ V // identical (assuming the list order is not changed+ c+ k6 V& [6 _
// indirectly by some other process).
& ~$ m8 a/ D; a+ f 2 r- h8 x1 [6 U/ X
modelActions = new ActionGroupImpl (getZone ());
- ]6 X, H- {' M0 n( a' v
q( d! v, I8 I2 j) R& u try {
6 ~1 N- E. s' t7 I: ~$ f# I modelActions.createActionTo$message
. V: K9 z ~- q& R0 o (heat, new Selector (heat.getClass (), "stepRule", false));
8 c$ F- T+ p7 ~& x/ t& C } catch (Exception e) {
$ w" M: J, M$ P! _' ?, S7 | System.err.println ("Exception stepRule: " + e.getMessage ());+ u" ~2 W$ P, p: X7 j
}: J: J( W8 I, e' }0 Z) g
$ W4 Z3 t) B& r# V9 ~ ~; U ^ try { Y8 j# _! T& J6 g6 H, O# u
Heatbug proto = (Heatbug) heatbugList.get (0);
& c2 M8 O( u' o" j& c Selector sel = 0 \; }- x( G* M2 c. z
new Selector (proto.getClass (), "heatbugStep", false);7 K& e1 z: Y2 q) Z
actionForEach =; \. H! F, l3 r' C4 A9 x+ d
modelActions.createFActionForEachHomogeneous$call
2 k; E2 F( I9 y: h7 g (heatbugList,
. |$ H' v, h" b/ M new FCallImpl (this, proto, sel,
) o' p0 g9 z6 H d new FArgumentsImpl (this, sel)));& o: G6 o8 r; m
} catch (Exception e) {
) E: K1 g/ l, M( v e.printStackTrace (System.err);
( t/ A! T1 Y) u/ e8 d# N) P }8 x; E' \) y5 ?3 `# W
$ Y& D' m1 P8 d% N/ c! n+ j9 P. i) e
syncUpdateOrder ();
/ S; m" y/ Y4 k8 A m- ?& n
: C: b/ t% Q( i# h u try {4 z4 ]( i9 d: j
modelActions.createActionTo$message z& m3 r# \' Y! \" j" Z1 F
(heat, new Selector (heat.getClass (), "updateLattice", false));" q0 O1 Z% n9 T( n1 b( r
} catch (Exception e) {" w7 Z& [4 W0 w& p
System.err.println("Exception updateLattice: " + e.getMessage ());: ^$ ^6 h, E; f+ Z6 I# x' V2 C( Z
}
A8 w- \3 D( b3 }9 d* e # J( a$ C) T* F5 c& C" B7 l- H/ x; h' j/ x
// Then we create a schedule that executes the
6 c/ @, `/ p: B8 b/ b a0 n. k& z9 @ // modelActions. modelActions is an ActionGroup, by itself it: q0 j7 n/ s. ~& J% p' J, t
// has no notion of time. In order to have it executed in# o9 V' f( u* B9 z# k1 x
// time, we create a Schedule that says to use the
- `: Y) {6 t" J2 t* z; w // modelActions ActionGroup at particular times. This
% T4 S0 {7 [6 g1 A) M3 U/ n6 J // schedule has a repeat interval of 1, it will loop every
2 Y0 p9 g1 a. e" ^! D // time step. The action is executed at time 0 relative to
w" N# q4 X; h1 @ // the beginning of the loop.1 V) B2 v7 s k$ s' ~
" O( X. c/ W; L4 H" y/ Y // This is a simple schedule, with only one action that is
9 a1 @% a; |+ i; N // just repeated every time. See jmousetrap for more4 n% o t0 |; ^3 l$ K! ]
// complicated schedules.8 L3 `+ u, B& A
7 `3 |+ c% |9 H8 C1 M" D
modelSchedule = new ScheduleImpl (getZone (), 1);5 b$ D4 r8 S6 x3 P8 C8 S7 C
modelSchedule.at$createAction (0, modelActions);
! `+ R# R! d2 Y m+ g" j
, ^& ]/ X Z$ N. ^9 Z) p. ? return this;8 j! o3 E; q: k& ]8 T: Q; k* Y
} |