HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 p0 V1 E! }2 ~- G, B) i. v/ w8 A! \
: [5 @! U F' E# { public Object buildActions () {
5 V9 }2 l% l$ ~ z- a super.buildActions();
( ?8 ^( T0 [4 j1 c 6 F8 _$ e: A2 t
// Create the list of simulation actions. We put these in
/ T* g( ?6 a. z1 ?& p // an action group, because we want these actions to be
% U, Z5 A. j, E7 o6 { c: {) e& H( F // executed in a specific order, but these steps should$ h! S7 _, X8 H5 Q S! `) r& I
// take no (simulated) time. The M(foo) means "The message
/ W( l: A) v4 p: ] // called <foo>". You can send a message To a particular
9 g, l; ~7 f1 N j. f // object, or ForEach object in a collection.
* g! K4 B8 J% T+ E 0 y6 w% A4 n; Q- [) \* y
// Note we update the heatspace in two phases: first run+ f {$ O3 |% y9 o7 n5 T. @
// diffusion, then run "updateWorld" to actually enact the
: G9 Z4 C+ h/ H0 s+ V6 H // changes the heatbugs have made. The ordering here is0 W/ v, e0 v& Q& C4 j6 u
// significant!3 I: r. ^* a$ O! ?( [
n7 S# o5 N' M // Note also, that with the additional( k1 c2 _0 k) w6 c! ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. h' ?5 u: {8 K V6 d' S // randomize the order in which the bugs actually run( V" h% K: @1 s6 \
// their step rule. This has the effect of removing any
+ a" }9 J- b' c // systematic bias in the iteration throught the heatbug
2 U1 K0 J3 N: U1 M // list from timestep to timestep
7 H' F8 L; n# ^, l0 {1 o H( R g ! h# ~. l3 u" p% _
// By default, all `createActionForEach' modelActions have9 Z. c' _ I# V. J2 ]4 g( k6 v
// a default order of `Sequential', which means that the
' s7 j- A" p4 o# `6 L& \, ]5 { // order of iteration through the `heatbugList' will be- T; {/ h4 w# V: I; m, t
// identical (assuming the list order is not changed& E) }/ c) w+ b3 y! [2 v
// indirectly by some other process).
8 N9 M1 @, C+ N8 p0 Z! m; X
- i; n$ c/ K8 k1 O. p& b modelActions = new ActionGroupImpl (getZone ());4 s( j1 u0 q; t0 U) {* t1 l# x
* @/ n+ `( e( h3 m try {
9 S) C: w; e2 @9 | modelActions.createActionTo$message' H- t$ a! U ]& M3 ?4 ^0 s
(heat, new Selector (heat.getClass (), "stepRule", false));
3 D/ g' p4 ]" k, H. ~ } catch (Exception e) {7 [, k _; g# o% i9 o A
System.err.println ("Exception stepRule: " + e.getMessage ());+ X* _4 _( ~0 x6 ^: g5 r; [6 v; \
}+ R# Q6 w& D8 [, |4 ?- x# R$ I
4 ^& } s# n* K; T5 [ try {/ ~' H/ n7 _' A) w4 `& L$ N6 d
Heatbug proto = (Heatbug) heatbugList.get (0);
* o/ H8 B, s3 @3 \& [$ D& M Selector sel =
C* W8 h7 x0 A- T) R) l- E6 y1 ] new Selector (proto.getClass (), "heatbugStep", false);
2 ^; Y; v5 ^, A3 ?1 U4 b actionForEach =
9 ~- o Y% d. _ modelActions.createFActionForEachHomogeneous$call1 W" F5 k: _# P5 t
(heatbugList,
1 o7 x% V# r9 y% w# E* O' h new FCallImpl (this, proto, sel,5 P$ Y1 {* j* d: c5 ~7 c
new FArgumentsImpl (this, sel)));
7 ~3 w( S0 y7 x0 S }# @* o# t } catch (Exception e) {
8 n! W+ X5 c1 V# T e.printStackTrace (System.err);
" c7 t/ A. y) `4 I+ N4 t$ o }
8 n3 l% V2 h( ?2 v * p: {0 E6 H3 v/ ^' D6 s. S+ r! [
syncUpdateOrder ();: k9 `3 {- ~" K, K. M! _* V f
& z, L/ o7 R; c$ r4 ^" T0 m try {1 h3 @+ H2 B; H) @! y$ B6 }
modelActions.createActionTo$message
( i4 i! [; @9 z7 u7 t; X9 y (heat, new Selector (heat.getClass (), "updateLattice", false));
2 j1 f! B' g2 x1 {) x. b% e4 h* R } catch (Exception e) {* q7 _% u; \- h: S5 Q4 K
System.err.println("Exception updateLattice: " + e.getMessage ());5 l6 S8 ]2 h- d
}
5 L, F4 a5 i% {1 j; e) t3 m+ [; w ; y' Y# w) ` A ?
// Then we create a schedule that executes the/ A o2 o* O. ^: V- I' H* m( S
// modelActions. modelActions is an ActionGroup, by itself it- c) O9 T1 s8 D7 V; l S- L# c
// has no notion of time. In order to have it executed in
, O' `- D0 g* |. H- e, c1 A- A2 m4 A" i // time, we create a Schedule that says to use the6 K$ G7 ?% U* e
// modelActions ActionGroup at particular times. This
8 j; I# h# U% o* E$ N3 B9 _ // schedule has a repeat interval of 1, it will loop every
* Q' ~4 f4 q+ c$ j! h1 a) \ // time step. The action is executed at time 0 relative to
# N1 Z7 f2 ?2 ~ // the beginning of the loop.6 @$ U: x. U& d: ~. B1 D' ^6 I
) a/ k# }8 C# j- y/ L% A
// This is a simple schedule, with only one action that is" v; n2 ]" m5 ]
// just repeated every time. See jmousetrap for more* c/ V$ r/ f1 S/ [& ?6 r7 Q1 O i
// complicated schedules.
0 P5 m) M0 q G+ t1 i+ Y $ Z, H% W) r) X" o
modelSchedule = new ScheduleImpl (getZone (), 1);
! n9 N0 O: s' L! a$ U2 b modelSchedule.at$createAction (0, modelActions);+ U3 S& r0 ?! r2 D q
8 Q$ `7 \* \. `" F6 K, l2 P
return this;& H) d2 ^' r) L1 k
} |