HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 N" r: I7 `$ W5 n$ Z
' q0 Y$ i3 d; ^2 u7 \: t2 y public Object buildActions () {0 ]. ]8 g% T& a/ p; A) L( \
super.buildActions();: H0 G, _ x5 Y, D4 u7 d' T1 J: m
, F& z4 Y2 N" H7 f9 [% ~ // Create the list of simulation actions. We put these in
+ r) _! R! I4 i, L+ x* d+ w- P! m // an action group, because we want these actions to be
6 y) q+ o( {+ J // executed in a specific order, but these steps should
" W. d- I3 _! p; n5 [- X // take no (simulated) time. The M(foo) means "The message( L. I, K' C# |9 s A# e
// called <foo>". You can send a message To a particular5 ]; l0 ~: F2 k$ S
// object, or ForEach object in a collection.
) D! x; W! f6 F5 z' z" T , v6 \: ?, P& F; G3 x) v% f. v
// Note we update the heatspace in two phases: first run
: l, d. D( A. G- \0 W* h3 u // diffusion, then run "updateWorld" to actually enact the2 f3 Y* Z* w7 K
// changes the heatbugs have made. The ordering here is
; J5 g+ k: v2 K _) b // significant!) y, W: U: k. L8 n! g
3 Z1 s6 N# {' D( M. }2 J* B: F
// Note also, that with the additional& C( r; Z/ W* |5 u
// `randomizeHeatbugUpdateOrder' Boolean flag we can, W) T' W! \& G4 H6 S/ Q5 O0 J
// randomize the order in which the bugs actually run
" j7 r0 \0 t. @# K // their step rule. This has the effect of removing any
0 N/ H/ w9 a4 o% e+ v$ g& c/ @ // systematic bias in the iteration throught the heatbug
, @0 [1 w3 @& v // list from timestep to timestep8 F0 O. Q) Y1 V. p
3 ?) a* `0 ?8 B: n3 J // By default, all `createActionForEach' modelActions have4 M9 |" X) b' D/ `$ N1 k0 K- I
// a default order of `Sequential', which means that the; E) M! n$ G# O$ x; j: n* i& ^
// order of iteration through the `heatbugList' will be
0 Y9 G. X7 h- C9 r/ `. p9 g // identical (assuming the list order is not changed( J1 d+ i6 r, z+ k
// indirectly by some other process).
3 E4 ^& B% h- J1 f; Y* \- }
3 S% I) R. b: c8 O+ Y) S modelActions = new ActionGroupImpl (getZone ());7 v+ A& b4 H9 T% P+ C' o
: D. I. x9 C+ g2 F6 D) d try {% ?7 r7 s9 i/ e p$ D
modelActions.createActionTo$message6 g! S" R( ]2 O+ ~; y$ H
(heat, new Selector (heat.getClass (), "stepRule", false));" U* ^3 U( v' {, @3 R" u6 h
} catch (Exception e) {
1 S2 Y9 Z' ?+ x3 u* I9 \& i4 X0 q System.err.println ("Exception stepRule: " + e.getMessage ());
! w+ V f, A( v$ @- z1 [0 x) F }
0 p1 E t6 ]: V& N0 i0 Y* B$ S9 {, c( _6 i4 k( r
try {
+ D3 ^% ^" N+ q" `; W5 e% V Heatbug proto = (Heatbug) heatbugList.get (0);& T" U0 Y) G' g& D1 X7 L4 X
Selector sel =
: V. ?3 N; J1 Q new Selector (proto.getClass (), "heatbugStep", false);
: Y7 A/ Q. J/ U actionForEach =
' U2 t7 a5 K5 S$ c modelActions.createFActionForEachHomogeneous$call
" y" Y% c6 U9 U) ~- B (heatbugList,
2 V- T P C: d7 q6 H7 ? new FCallImpl (this, proto, sel,
6 q4 O3 d, B. R3 s! h8 a2 d new FArgumentsImpl (this, sel)));$ B" Y. N$ S- N3 I1 m% F3 L+ W
} catch (Exception e) {
/ l; H, ]/ H1 I* Y5 [ e.printStackTrace (System.err);
' s5 y/ e P _ D/ z3 k }
) I2 p; g* x, k' L
# r& Y! Z( ?0 P3 E: b syncUpdateOrder ();
+ b+ d$ e B+ ~' t% r$ H( Y5 _2 ]' _6 P, O
try {! J6 g E& F' j- |8 B' b
modelActions.createActionTo$message 1 j; x3 E- k" `! C7 C+ s% x
(heat, new Selector (heat.getClass (), "updateLattice", false));
* p- I' g& C S# G/ g( G' U- u0 _( w } catch (Exception e) {( j( n* G G+ V- {) I
System.err.println("Exception updateLattice: " + e.getMessage ());
9 V* }& Y/ ?, Y; Q1 L" C }
, N, v0 Z x! [6 d 5 G6 e- a+ a& M% |5 I' D0 ?5 T
// Then we create a schedule that executes the
" }7 E: h$ p x5 |1 Q- i) P // modelActions. modelActions is an ActionGroup, by itself it
2 z* [! ~3 @$ b // has no notion of time. In order to have it executed in
) w9 A; O6 X( \; c // time, we create a Schedule that says to use the
, W2 h& g+ u/ H4 R$ ~4 u // modelActions ActionGroup at particular times. This
& p' {! d5 ^7 O) X, r // schedule has a repeat interval of 1, it will loop every2 b% N' |9 t6 }/ s% ?
// time step. The action is executed at time 0 relative to
4 ]: [1 ~* O: B5 G0 o; v // the beginning of the loop.( ?4 ^/ w$ b* R. A+ L& ~
1 `5 a% k8 F" r- u X% T1 |0 z // This is a simple schedule, with only one action that is
3 y2 i3 |2 y/ j' a- ]$ i C2 T4 ] // just repeated every time. See jmousetrap for more0 P% L" y! d2 }1 Q1 h0 S8 \6 A
// complicated schedules.! }- X N4 @, L
" V$ F: R: ^" i: g/ `* R# o modelSchedule = new ScheduleImpl (getZone (), 1);2 s! D, C! Z7 Y; E
modelSchedule.at$createAction (0, modelActions);- ^# r3 c9 J2 R
- o6 A8 }2 H4 c F- ~$ v4 v return this;8 m" Y9 p" L+ t/ ~; }! A
} |