HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 b4 j3 f7 f7 o- L
! w4 S6 W H( n) k$ X+ z$ j public Object buildActions () {
; C8 C- X( P9 ^- O$ ~ super.buildActions();
: J) s6 \+ p! E1 Z/ ] x7 P
5 M# ]1 M H% C' k# R1 Y6 L // Create the list of simulation actions. We put these in
: X& @) e! K: Z% ` // an action group, because we want these actions to be
* _% }: a: B2 @, ]) g // executed in a specific order, but these steps should1 g) h" m. ?/ u! P2 B% @% ?. t ?( s
// take no (simulated) time. The M(foo) means "The message; i, S2 }1 t& _( P/ s; I6 b
// called <foo>". You can send a message To a particular
7 Z. S9 n% G! n9 t% E0 \ // object, or ForEach object in a collection.5 G) y2 W, C9 `
- E# w; R3 N! S$ t0 z" P // Note we update the heatspace in two phases: first run* r, v% X; A3 t4 {) c: j
// diffusion, then run "updateWorld" to actually enact the
6 m; {9 s; j5 H. e4 e // changes the heatbugs have made. The ordering here is
% j. k, j# x- p // significant!4 j& w& I: B5 ?3 b3 \
8 w% V$ x9 P- T# W" N. h" d
// Note also, that with the additional
* H; u$ c7 x0 k2 F // `randomizeHeatbugUpdateOrder' Boolean flag we can
. G% f! i( \5 d1 N4 y2 {) F // randomize the order in which the bugs actually run) L5 g' G9 ?( ]
// their step rule. This has the effect of removing any! I% t& n+ \7 x& E
// systematic bias in the iteration throught the heatbug
2 z- A0 t' S1 N+ q // list from timestep to timestep) e: x* n- ?. j0 j! k2 s
6 A9 u( {: s# t, r
// By default, all `createActionForEach' modelActions have
' m) j3 d4 v& h // a default order of `Sequential', which means that the4 y5 F; g$ E' z' Q' V) e
// order of iteration through the `heatbugList' will be7 N9 W8 q6 u4 }2 S7 ~5 Z3 A. {- I
// identical (assuming the list order is not changed) j1 f& ~5 `/ P/ ~
// indirectly by some other process).3 g% ] h0 h, l& `! }
2 a7 \* |# ]& C& ]( J1 z8 G modelActions = new ActionGroupImpl (getZone ());
3 i1 f% I6 c- G
$ x( N, U6 ?2 P+ h9 W5 i9 o d- d try {$ p: m+ K- J" M9 C ]+ {" `
modelActions.createActionTo$message' W$ L. f" f( q( I! W( W
(heat, new Selector (heat.getClass (), "stepRule", false));, J. g2 e& g7 w/ W9 b& @
} catch (Exception e) {
; l' e3 b! }3 ?4 f9 ^5 M System.err.println ("Exception stepRule: " + e.getMessage ());3 Y& ]$ s1 k0 B- W7 x$ G
}/ D& M. P2 J/ J/ { o
2 ~) F9 |: l6 V4 O4 U try {
' z7 ~. d9 Q5 U Heatbug proto = (Heatbug) heatbugList.get (0);1 j7 Z- W0 R9 `
Selector sel = 3 J9 W3 {! p# y" l6 v5 e5 }+ [
new Selector (proto.getClass (), "heatbugStep", false);2 b& s" S8 B! L1 ]
actionForEach =* s: E) X6 |# Q8 m7 ?# C
modelActions.createFActionForEachHomogeneous$call; ]7 N" E1 O! _2 {' _0 ]
(heatbugList,. V- f. s7 t: |) s6 H
new FCallImpl (this, proto, sel,
, a, _& s, d$ w' n9 E- z. E new FArgumentsImpl (this, sel)));
+ w8 C" s1 Z. O2 o } catch (Exception e) {
/ l) o8 _4 P# N/ c e.printStackTrace (System.err);
. W0 K) P7 T+ {2 v! C$ o, K6 ~; ?' u }( x7 h$ Y( P6 V+ Z. O6 R' A
1 Y( s; V3 \9 |6 R0 \* S; R" w( x& V syncUpdateOrder ();# t" o: K. U+ y R. h/ Z& h/ ~' ], s
4 k; |/ ~4 P$ I4 |5 S; \ try {9 a( H4 J0 w# ^ Y& m* h6 m+ l
modelActions.createActionTo$message * _# e6 p% e3 t" r [) A/ O
(heat, new Selector (heat.getClass (), "updateLattice", false));
! w9 D: g3 @- t } catch (Exception e) {% n# T5 U( y' K- B, a
System.err.println("Exception updateLattice: " + e.getMessage ());5 V. D3 c8 S2 H( @
}
. s/ \1 J- u5 W- J5 B / j2 M" d1 y0 z! W" k
// Then we create a schedule that executes the" E, T3 s' H3 y( I& W) e
// modelActions. modelActions is an ActionGroup, by itself it
$ S! l$ {; U8 k7 W4 c$ h% x" ~: u // has no notion of time. In order to have it executed in
0 d9 u3 B) s2 ~' y/ ?- E1 _; t6 S // time, we create a Schedule that says to use the0 [, `3 U4 r0 q( y8 Z
// modelActions ActionGroup at particular times. This
4 n; a. h" R7 b9 r8 K8 ]# x // schedule has a repeat interval of 1, it will loop every% }. |6 k5 s# ]& g
// time step. The action is executed at time 0 relative to
! S, v: r. R* ^" U; C3 v( E // the beginning of the loop.
4 ^4 P+ x6 _$ \! L& I) n1 K p' _4 B: J( {$ u& Q/ p
// This is a simple schedule, with only one action that is/ e7 `. C4 N9 n. t0 b
// just repeated every time. See jmousetrap for more
0 q/ Z4 K* K7 E# S8 [$ F8 U5 D // complicated schedules.$ r% P4 M3 c0 [) F5 K5 `) n1 C
' v+ p$ h9 \ _6 I+ W
modelSchedule = new ScheduleImpl (getZone (), 1);% P% y: n O0 V1 v
modelSchedule.at$createAction (0, modelActions);3 e: D% X, V; [& D2 j
H } ~6 M! F7 {" _ p7 q return this;
9 N F2 v$ n, c( d8 D } |