HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; o. ]& ~7 N3 d8 b) e
, G S) G# k1 K$ U& H5 [; \6 R public Object buildActions () {
. G w9 e/ L9 h( ^9 l$ j super.buildActions();6 @- ^6 t8 y6 R: ^9 D
% B) N7 j/ ^4 p/ j
// Create the list of simulation actions. We put these in
, V4 F8 _% S. q6 W# X7 m$ E! ] // an action group, because we want these actions to be
) {# q4 ^! @) n( T. O // executed in a specific order, but these steps should T+ r7 ~) C. t1 Q
// take no (simulated) time. The M(foo) means "The message, K( M4 b* I* X! _
// called <foo>". You can send a message To a particular. V. q' H" C6 z/ {9 n; w: k1 n
// object, or ForEach object in a collection.
, Q4 i4 D1 Q" A2 U+ ^9 H$ R J! E7 n# p$ {/ R9 A. A- _
// Note we update the heatspace in two phases: first run$ T5 K# D! V) \( s! I: j b9 r
// diffusion, then run "updateWorld" to actually enact the" b m4 d+ D+ n5 [. \+ [" v
// changes the heatbugs have made. The ordering here is
% d+ b7 G% f; D* R/ ~# K& c3 C // significant!2 H0 O* R. x. X9 ~4 p
8 F: i) M* g6 p5 A& K/ g7 q( ? // Note also, that with the additional5 d. N, D8 j& G
// `randomizeHeatbugUpdateOrder' Boolean flag we can/ N7 |; Z A, {. V
// randomize the order in which the bugs actually run
0 H0 Z) J9 K5 e3 \, k/ q- l // their step rule. This has the effect of removing any4 f3 e# h0 n) T( j( A/ i h
// systematic bias in the iteration throught the heatbug& z* k& O- y( z
// list from timestep to timestep6 N; K1 t- o5 V/ ~8 k0 v% s7 N3 x
+ |- L& f, ^% v1 W( [* J4 S9 T( h // By default, all `createActionForEach' modelActions have3 n w; b0 u$ [
// a default order of `Sequential', which means that the
) m+ V3 O; H- e0 z6 T" ] l // order of iteration through the `heatbugList' will be
: m. F7 H% e+ Z$ g# u6 q // identical (assuming the list order is not changed
; w& ?: x6 Y) p/ ]2 c* M3 t' N // indirectly by some other process).2 H8 g+ n& k' I: x' A7 d
) l; B* W. o; ?2 w; |1 }- h
modelActions = new ActionGroupImpl (getZone ());. a6 X3 D- D8 T
6 V1 w* e# x* H* k( V$ W1 X try {
& T, G2 N2 q J. W" c4 a modelActions.createActionTo$message
. j' T+ d, X2 |% s ] (heat, new Selector (heat.getClass (), "stepRule", false));& Q8 D6 V! _+ j7 C: E* E" u: b$ o
} catch (Exception e) {
- I/ N+ h& n& s- V4 k- R System.err.println ("Exception stepRule: " + e.getMessage ());2 q: |2 n% p2 B% n
}
4 c! x! P3 q% {
: @' R; `4 l" I; v2 r( v try {
* |7 W3 P/ U8 t# h Heatbug proto = (Heatbug) heatbugList.get (0);
6 ]# ^" N2 d* _7 u! n Selector sel = * ]* _0 b3 U/ A9 e
new Selector (proto.getClass (), "heatbugStep", false);
* ?! F0 B! q [* v) @9 ? actionForEach =/ v {! K. j- s! X5 ? c
modelActions.createFActionForEachHomogeneous$call
G- g8 x+ s( Q' i4 _. W" i (heatbugList,) [' D W/ \2 @6 J' |3 G
new FCallImpl (this, proto, sel,% P" @1 s) |2 a! l6 I7 K
new FArgumentsImpl (this, sel)));
4 G" @ C8 c3 C. a5 H/ B8 [6 ` } catch (Exception e) {
/ _9 G+ g$ S: j' l% k e.printStackTrace (System.err);$ y+ T! m. S- _ T- d, ^
}
* ~: Q0 i5 D! ?/ A' E ) C- R( n0 k% U* N& F, @
syncUpdateOrder (); ]6 i1 @$ y" I8 A" e1 z2 ?
" F$ @0 _2 Q+ @ try {$ n4 K- r1 h3 y- @* ^( ?2 X
modelActions.createActionTo$message ; A0 ~( g" W( u0 N0 h
(heat, new Selector (heat.getClass (), "updateLattice", false));( p9 M. P, B4 e/ D6 _+ {0 e b) F: e
} catch (Exception e) {: z5 d5 O* ~1 q4 H
System.err.println("Exception updateLattice: " + e.getMessage ());
+ |) Q' u3 N: u* T) W! V/ T }
$ R& d; m" ]1 D# c6 U & ^0 x* a* I" [ w4 `9 z; r) {
// Then we create a schedule that executes the2 Q, i! B% ^* `& F) g
// modelActions. modelActions is an ActionGroup, by itself it
! _2 }( k. P1 K% q# ~) W m // has no notion of time. In order to have it executed in0 r6 J' b6 q& M( ~
// time, we create a Schedule that says to use the2 ]! k/ N1 _6 x
// modelActions ActionGroup at particular times. This3 Q& ~8 V, y3 g% k$ E
// schedule has a repeat interval of 1, it will loop every+ U+ F6 |. C7 T8 q% G2 P
// time step. The action is executed at time 0 relative to
% K1 K [7 R; t6 k- g4 e // the beginning of the loop.( p b) x8 D. y8 L
8 D0 B. E u J' ]* ?. i // This is a simple schedule, with only one action that is
) n( @- }2 r6 t/ G // just repeated every time. See jmousetrap for more$ R3 R7 i9 C) K1 T7 h' d# S* q
// complicated schedules.
* s M# {% l0 [$ `+ ]3 L) p, g
# h1 e6 X2 v( P9 n) ?- y$ K modelSchedule = new ScheduleImpl (getZone (), 1);
( f! z8 b" N7 a modelSchedule.at$createAction (0, modelActions);& B$ G+ c4 n4 p
9 j+ i% m* W' o, o# V5 P return this;
; m# E2 k3 O k. I } |