HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- }0 O, b4 `# r
4 Q7 M" j" J V public Object buildActions () {
! J5 i; z' {. h* o. V$ q: C9 w0 r7 D super.buildActions();
- l+ D% j& A. Y5 b0 B4 @0 Z
3 V0 E3 L& H. m P/ H6 I7 F5 c // Create the list of simulation actions. We put these in1 @: a# s F! Z: j6 I
// an action group, because we want these actions to be$ R0 |1 L" c5 a$ e
// executed in a specific order, but these steps should
4 N3 |) o* s; [- ] // take no (simulated) time. The M(foo) means "The message/ Z7 R3 x9 F) ^& P/ A4 J4 Y! T
// called <foo>". You can send a message To a particular3 C9 r, [. f- Q' \/ w
// object, or ForEach object in a collection.8 M0 R, p6 c. |) w
$ l7 G$ d( D8 V/ f! p2 x
// Note we update the heatspace in two phases: first run/ _& V4 @. v- E& u- w1 T
// diffusion, then run "updateWorld" to actually enact the
5 T9 C8 t& o- E8 G3 d# u4 M ^/ p // changes the heatbugs have made. The ordering here is
5 [2 R" {) h& ^, n // significant!
# ~) K" E. ^( Z# C) `7 `1 [& e
& f( ^% U( M) S+ C# L+ K; p // Note also, that with the additional
" ]% v" L- q/ l! x2 Y6 O" A: x# \ // `randomizeHeatbugUpdateOrder' Boolean flag we can
8 h* C' x r1 Z5 H5 q // randomize the order in which the bugs actually run& N+ G+ b! m2 j- R. h( ` M
// their step rule. This has the effect of removing any
% \! I% R4 A0 T" o6 T/ R // systematic bias in the iteration throught the heatbug! [3 h! t/ z W, C4 w* e
// list from timestep to timestep) k1 \- I& d/ ~8 C4 N
# {" j+ K; a. L5 U& S3 ?* A" M // By default, all `createActionForEach' modelActions have( h3 a5 N! f$ m. J% V; O0 c
// a default order of `Sequential', which means that the0 c9 c6 A3 n9 o F" p1 t' D
// order of iteration through the `heatbugList' will be# `( \* U' d" m! ^3 t8 Z
// identical (assuming the list order is not changed: Y( m9 V7 W. |* t# t5 O
// indirectly by some other process).
7 K- Z9 @: r: t* e
, S" \/ u/ V0 s modelActions = new ActionGroupImpl (getZone ());4 c* R* m8 E8 s- n* `) g& f4 D' d5 |/ D
$ l" u# Z: E4 ?6 D1 ^1 n try {
~# ^7 Y# |, q- L modelActions.createActionTo$message
$ Q& v( G7 x% y& k8 a k% W (heat, new Selector (heat.getClass (), "stepRule", false));
# B( c2 P$ S1 W3 L T( a } catch (Exception e) {
, l+ y, s! u) t$ z System.err.println ("Exception stepRule: " + e.getMessage ());" o q- x+ T" a9 e+ ?
}
% v% J$ d. B {# M' V# t+ U3 C
try {
6 ^( B) X3 `+ ]+ T! c8 p: ^% h Heatbug proto = (Heatbug) heatbugList.get (0);+ n i' t: t; a/ _
Selector sel =
# h8 n+ a* A/ U3 T# |: S9 M new Selector (proto.getClass (), "heatbugStep", false);
* W4 U! k0 V4 Q- e x actionForEach =3 V, I) a3 b7 A9 h# e2 n. K4 m
modelActions.createFActionForEachHomogeneous$call
9 A/ m( M- {' K# G! f S F (heatbugList,' ^$ M/ D M4 }: a8 j9 F
new FCallImpl (this, proto, sel,
! G( H- x; o9 W) o, k1 j( } new FArgumentsImpl (this, sel)));, l- ]% L* B; b
} catch (Exception e) {
4 k, \# [1 F8 a- E" G& H5 v7 j e.printStackTrace (System.err);
7 g' w! f; [$ v% P4 L" _ }. f" I; P, O' |& s3 e t, @5 B1 B/ Q
/ ^: D7 B" L: t% f3 D T- H
syncUpdateOrder ();! h! A& F% Z+ H& U( e9 n# B
' F2 _1 [' p6 P0 L# [9 D( y
try {
/ M+ ?: E7 I6 R( m6 j) U- P modelActions.createActionTo$message
0 u5 z$ ?# H8 I7 S) v5 u% r (heat, new Selector (heat.getClass (), "updateLattice", false));1 J. @- `2 S( a1 e
} catch (Exception e) {2 v7 N, P _# g( s! P; @
System.err.println("Exception updateLattice: " + e.getMessage ());+ ~5 a2 F, B+ a6 A/ U/ z# J) n
}+ x2 u- Q$ O% C3 i; P
% _3 a* g: ~! a // Then we create a schedule that executes the) e9 F6 M |6 B4 N
// modelActions. modelActions is an ActionGroup, by itself it
6 W# W) @/ x- H/ P$ B // has no notion of time. In order to have it executed in
4 E# `7 g, {+ l6 I+ V // time, we create a Schedule that says to use the( _# U- v% Z) t& q
// modelActions ActionGroup at particular times. This
/ _6 ?" N! M( e7 l8 p4 [0 Z // schedule has a repeat interval of 1, it will loop every8 |" P. a3 d7 \+ G" J: P3 }
// time step. The action is executed at time 0 relative to$ p9 Z, o( M6 B( [0 h# `
// the beginning of the loop.
! k+ j7 Y4 ^ x# ?/ c: E
0 q$ l# w/ x3 O5 ]. s8 @9 ` // This is a simple schedule, with only one action that is
; H6 R8 {$ X: S# N // just repeated every time. See jmousetrap for more$ f; x2 h9 ~( E/ U
// complicated schedules.. V2 F7 p0 ~9 k+ ~1 k' j
1 h. I+ u T# U+ H
modelSchedule = new ScheduleImpl (getZone (), 1);
6 a4 C9 D ^- d, f1 F modelSchedule.at$createAction (0, modelActions);0 X4 M: Q3 Y0 E) I( T- v6 _% o
7 T$ k+ y1 _. ^3 H8 E return this;* ]# p* F4 H( U. ^3 `& U1 h, O
} |