HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- }/ L6 s, o3 ?* E% T: a- Q j; o, M& D' g' t
public Object buildActions () {# X R1 U0 i8 h2 x" ]4 B% `
super.buildActions();
- Q/ b* ?" C; b
$ R# n; K+ k) J& Y# I; I# O3 L // Create the list of simulation actions. We put these in1 R7 }: C0 v1 I$ X& ]1 m; J7 Q" Z
// an action group, because we want these actions to be, f! s; i; m0 J& p8 i. A
// executed in a specific order, but these steps should
9 Q! Y4 _6 W% \2 w+ o // take no (simulated) time. The M(foo) means "The message, V; q. K5 P% g ?1 n* Y
// called <foo>". You can send a message To a particular
/ F6 V# t/ }5 L$ u; I8 p1 r& [ // object, or ForEach object in a collection.
$ D* ^* \1 M) P! e7 | * R4 p& }, o( F; \, e
// Note we update the heatspace in two phases: first run
: M, l: ]% ?' n. U8 M // diffusion, then run "updateWorld" to actually enact the
# m* ^- x- S! J# u! ^, d // changes the heatbugs have made. The ordering here is
. M) Z; ?: v* J& r // significant!- `( m1 ]/ ~6 c
/ w: K" E$ Z' M
// Note also, that with the additional \4 {8 ?5 m6 ^8 |
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ |- N0 |* x7 Y
// randomize the order in which the bugs actually run
% ^! i/ L( O4 w! v# G5 ` // their step rule. This has the effect of removing any0 {8 y5 w' {0 D# y
// systematic bias in the iteration throught the heatbug
7 U! q: w( `% E // list from timestep to timestep
9 T3 ^1 o! ]9 X" t# x3 a H $ [& Q' Y: Z1 F5 }
// By default, all `createActionForEach' modelActions have/ h* ~ y4 g3 t' [3 O
// a default order of `Sequential', which means that the! q$ }) k8 t/ n$ M! @# X
// order of iteration through the `heatbugList' will be
0 z( e2 g; H# o0 m# Y // identical (assuming the list order is not changed
7 ?4 K0 J7 M9 n* ^) h" r* d // indirectly by some other process).# n" V4 @$ G6 k1 w0 s
6 z* w1 `! p& @$ ]4 ^9 v modelActions = new ActionGroupImpl (getZone ());
, e1 ?7 R+ g9 H! x; D
" ]6 n: W+ u+ [9 i& j P1 c ?5 [ try {
/ q, |+ L% b6 }+ F modelActions.createActionTo$message
7 y- P+ ^ q) j& f (heat, new Selector (heat.getClass (), "stepRule", false));
9 Z+ v. L: p- ^3 \ } catch (Exception e) {9 s, Y2 l; M1 O. v7 B9 o* @4 Y
System.err.println ("Exception stepRule: " + e.getMessage ());
0 @/ F2 t! K7 x! ?- s' Y }
9 A$ ?* a; _0 K; f
2 o& m% `2 R8 G- M+ O try {
( \, [6 y+ ]# z$ l Heatbug proto = (Heatbug) heatbugList.get (0);# J6 F: S9 H/ @- L S K
Selector sel = 7 O; Y; @4 m; ?7 y" N! e; u
new Selector (proto.getClass (), "heatbugStep", false);
: Y# |2 w5 G& U- C$ a2 F8 X, e) x actionForEach =
1 [3 v @$ [9 B/ u/ `" m modelActions.createFActionForEachHomogeneous$call
' M' K# }: @+ O) @' _7 \ (heatbugList,
$ \# {5 L) N9 a/ F new FCallImpl (this, proto, sel,, l& b) F1 w- V% p+ i/ d+ a
new FArgumentsImpl (this, sel))); v6 j: ^3 z6 ]$ K
} catch (Exception e) {
" q2 }* e0 }* K7 D3 B e.printStackTrace (System.err);2 d' l) i6 b. G5 G
}: @7 W j4 {5 b7 X
& n. ?$ S- r( D* P1 q5 D syncUpdateOrder ();& J8 U- J; ?6 y! ^. s3 A
9 v' } v: N% e) m. v5 e9 P* ?
try {% G; v# m6 g$ L( \
modelActions.createActionTo$message 4 Q3 z! J. F( U0 ~0 @, k) ?
(heat, new Selector (heat.getClass (), "updateLattice", false));1 }! f/ z+ y( h, I: {
} catch (Exception e) {7 k' l4 x) i1 t G( L6 y
System.err.println("Exception updateLattice: " + e.getMessage ());
1 B! j) ~$ P m3 e8 |6 r }
8 y7 C9 r1 h- U2 v: F3 |
! v k* j8 F$ i( Y" W9 W2 V( \& q! R9 q // Then we create a schedule that executes the
2 f0 s( w# p3 K // modelActions. modelActions is an ActionGroup, by itself it
$ \6 w6 Z, V) e1 T // has no notion of time. In order to have it executed in
( w: M2 _4 {% \9 r% S- B( H! G1 ~$ H6 \ // time, we create a Schedule that says to use the
9 T3 w# y/ {/ T7 U* Q // modelActions ActionGroup at particular times. This
) ]4 U3 b+ X* }$ [% Q# B; `" f7 H // schedule has a repeat interval of 1, it will loop every
: ~% L$ f2 B( P& E // time step. The action is executed at time 0 relative to
- @7 H9 b9 `7 @2 E% C% v# \ // the beginning of the loop. [' a% f# J Z9 Y( {
( o: U% A$ D4 w, Z& R // This is a simple schedule, with only one action that is4 C8 c# V$ J) k7 Q/ g7 ^
// just repeated every time. See jmousetrap for more
7 R4 I1 ?% W0 r // complicated schedules.
% d( H9 p: z8 |" C- k, y
; Z, Q: S; {6 O0 f8 Z5 ^- B$ { modelSchedule = new ScheduleImpl (getZone (), 1);% J( x: L7 H* @
modelSchedule.at$createAction (0, modelActions);- V6 ]2 G! _. ]2 V0 ~
* W/ v' W* K( z' C! `: L# C7 R
return this;
7 ?0 B3 w, Q# J' n' {/ w3 N( o+ I& D } |