HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( {6 m1 n; ^, T$ u' l0 h% r& D1 P, u. j* [% c
public Object buildActions () {5 Q1 w7 P8 a0 \* x! Y+ T5 I2 c
super.buildActions();
7 \/ A6 s, n9 @/ L: P
, l3 T, F7 t4 j2 w // Create the list of simulation actions. We put these in
! T9 |: U/ K5 w, ?1 Q4 j9 v // an action group, because we want these actions to be
4 j {! F6 S, } // executed in a specific order, but these steps should
$ b: y& ^8 P) ~2 {- {1 s. `; g // take no (simulated) time. The M(foo) means "The message7 P7 ^/ U! J% F. i* I
// called <foo>". You can send a message To a particular
, y$ {+ n# Z5 n6 h // object, or ForEach object in a collection.& Z) x3 Y/ `, L i# d+ M
- j z) h' Y# c& e4 F
// Note we update the heatspace in two phases: first run0 }% j* [( ^4 i; x0 ?2 f b
// diffusion, then run "updateWorld" to actually enact the
; u4 z$ `1 i8 b7 h2 x* N // changes the heatbugs have made. The ordering here is' q' Y/ O W/ B7 G, l% n- \
// significant!
: X8 t& N+ R& ]+ Y2 d3 ^/ f * o% n: w9 e0 {5 ]' a
// Note also, that with the additional
: Z' B' N' z" R: a* N3 F8 I // `randomizeHeatbugUpdateOrder' Boolean flag we can$ A) [( d" [8 _2 i
// randomize the order in which the bugs actually run
6 r9 t: t' p! p, K // their step rule. This has the effect of removing any
4 _' d# n9 ]) }) ^& ? N // systematic bias in the iteration throught the heatbug
- E! P5 D1 m7 c2 U5 Z! G. U9 D$ H // list from timestep to timestep5 V) L* S- ^& n7 {% X
/ k& T- k, M7 T y // By default, all `createActionForEach' modelActions have9 ]9 F" i* {6 ^& o
// a default order of `Sequential', which means that the i* b9 c1 f7 W, b3 V; @ |
// order of iteration through the `heatbugList' will be
' n" y" q' l- f! m" ? // identical (assuming the list order is not changed- b6 l) {% n! O, [( V
// indirectly by some other process).9 q1 o0 ^2 `" |8 M
5 {3 y; q% w8 b modelActions = new ActionGroupImpl (getZone ());
" E, Y0 ?8 y) M! [1 w/ ^9 \: b" {2 Y- _/ q# k
try {
8 N4 c# a# S+ _" J7 D L modelActions.createActionTo$message
1 `$ `/ c' L7 Y3 N g6 f( f (heat, new Selector (heat.getClass (), "stepRule", false));' S' ^7 M5 E* t0 D/ c1 M; I! w! z
} catch (Exception e) {6 ~& M5 w* e& s5 N- ~0 j
System.err.println ("Exception stepRule: " + e.getMessage ());
* ?# O- N7 \) w- _ }' w9 E2 r: H+ W5 V; C9 p! X
9 y+ B- A+ E" `& M4 H- _2 t try {
. z B5 R/ X# C/ R Heatbug proto = (Heatbug) heatbugList.get (0);& l3 K# a9 h; r+ ~0 `" w
Selector sel =
4 k' q. G. N$ P: }4 L/ j new Selector (proto.getClass (), "heatbugStep", false);
$ w7 E2 c$ \( A) Y) i" i actionForEach =
* q0 i* Q, a3 J' k' [ modelActions.createFActionForEachHomogeneous$call7 K# ^5 Z- Q& i$ | c
(heatbugList,
2 c' W& A: n3 H# ]$ ] b new FCallImpl (this, proto, sel,
' S" h9 |" J9 B/ z new FArgumentsImpl (this, sel)));5 J( I. d8 j$ S: Y4 P
} catch (Exception e) {+ }. ]& L, t, [/ C
e.printStackTrace (System.err);7 x1 P# R2 E% I
}
# a L4 z& X; P7 u) k0 D: k$ x , ^ X7 f7 S( h+ `
syncUpdateOrder ();. g; R' W3 o. D1 A0 c% }* n! y
P5 ]! A: ~8 c" R h" Z
try {7 O) N- ]% w( `4 S; s
modelActions.createActionTo$message " {9 |# Z) u8 g- o+ h5 @8 i
(heat, new Selector (heat.getClass (), "updateLattice", false));
" i' C! E+ x0 I* K2 K% {( j2 K } catch (Exception e) {0 _5 O# Y9 @0 S$ [2 `' i/ P9 `
System.err.println("Exception updateLattice: " + e.getMessage ());: H* }0 F7 v. ]
}0 N" ^4 ^2 V( t
3 i- H* `4 T k
// Then we create a schedule that executes the3 h$ o$ f" W' o$ @
// modelActions. modelActions is an ActionGroup, by itself it
, K9 k, I' ^ b) K1 r% m1 V // has no notion of time. In order to have it executed in
9 D6 _( ]# v/ u+ W: F+ P* }7 J // time, we create a Schedule that says to use the9 J' Z1 S3 \4 P/ w% v7 s, r" j
// modelActions ActionGroup at particular times. This) }1 P2 {0 c( U: v- N) y: \% @' U! x
// schedule has a repeat interval of 1, it will loop every
$ r0 M1 D h, N( r9 W }4 r- i% H // time step. The action is executed at time 0 relative to
5 ]* p& v& @, a5 Q2 P* l // the beginning of the loop.! O0 C# G: \ @7 c- ]( m; }+ y6 u: p
; O6 u8 r4 h8 g
// This is a simple schedule, with only one action that is
- W0 B/ l' c. N Z // just repeated every time. See jmousetrap for more
9 X4 } y9 M6 ?( E // complicated schedules.
0 }/ V! Q" S! J- q3 ~7 e ) m U! ?: \4 e$ p
modelSchedule = new ScheduleImpl (getZone (), 1);
+ L0 Q9 c/ o; I, A modelSchedule.at$createAction (0, modelActions);9 u% D2 z4 C: x/ t
/ D( t# _: M" H) M f6 E/ ] return this;; D3 ?3 ]# `! ?+ e8 x4 V; B
} |