HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' k# W7 v3 a7 A& p. q9 Q8 l
7 T% {+ V8 f$ i! G0 V# p* d public Object buildActions () {
; U( h& X; Y, F n0 } super.buildActions();, `; V9 f3 i4 c* Z r: A
4 @! F( H! E g3 D f. a
// Create the list of simulation actions. We put these in+ }* j* \3 h9 }" z/ e1 V% K
// an action group, because we want these actions to be
, v7 E" J, F1 |5 E' |* [; m5 ? // executed in a specific order, but these steps should
I- O* H5 Z7 b$ }2 A // take no (simulated) time. The M(foo) means "The message5 X% m5 s, ~& X0 a0 w
// called <foo>". You can send a message To a particular
% T. ?* o1 c. f1 Y7 ~$ b // object, or ForEach object in a collection.% B0 @2 p& n' u$ [& i( n
x( f6 i: [: T% p( Z& `
// Note we update the heatspace in two phases: first run
; }7 g7 u( W2 E6 C6 a // diffusion, then run "updateWorld" to actually enact the
0 e# z0 N9 q" P$ k4 d // changes the heatbugs have made. The ordering here is k1 o9 m7 E9 j
// significant!' E) x: M. Q2 b, ^( i* a8 B& H
+ V( N% i3 {6 u( l) E: J // Note also, that with the additional
6 ]* ~3 L2 |1 Z# m" I% I4 j, A // `randomizeHeatbugUpdateOrder' Boolean flag we can2 W6 E; Z4 E- ?6 E' f1 \4 c! T
// randomize the order in which the bugs actually run
) Y. g5 m' O7 M9 } // their step rule. This has the effect of removing any
2 w1 G' L8 Y, u* t) \ // systematic bias in the iteration throught the heatbug
2 d5 a) |3 i5 y" k, { // list from timestep to timestep" d( u) N2 m5 f# X
/ `' ^" v* R. t5 c$ A // By default, all `createActionForEach' modelActions have
) U1 U2 X7 \/ D. N, Z+ K // a default order of `Sequential', which means that the# y- ~5 A) J- z0 U6 X- D
// order of iteration through the `heatbugList' will be* Z3 y4 s" I! x, _
// identical (assuming the list order is not changed
6 I C! h. [" E: |! m; k // indirectly by some other process).4 P- _% x& \6 `, U/ ?
6 W6 b+ ~2 W3 z) R& Z2 h modelActions = new ActionGroupImpl (getZone ());
- V* `/ n2 n+ ?1 G/ x3 R3 H% `* N' _; r" I) V8 I/ E
try {8 f# T5 c% F9 [2 n
modelActions.createActionTo$message
6 K! J" L! ?! L& ^5 w& \0 k) r (heat, new Selector (heat.getClass (), "stepRule", false));
: }6 Q4 w$ N& L3 q& R } catch (Exception e) {
- U W) D0 e5 U5 z System.err.println ("Exception stepRule: " + e.getMessage ()); y( \8 q. C& X9 r2 ~2 ]
}, ^- o% w. n& u: B0 J/ z* o
: n* G/ \6 _! x" \* i
try {
" ^6 k0 l {% d- W: N+ N Heatbug proto = (Heatbug) heatbugList.get (0);
5 Z2 M4 M. y; ? Selector sel = ; v5 R$ k4 M& O2 e& K3 D# h/ W' Q
new Selector (proto.getClass (), "heatbugStep", false);
! ^3 J' y2 ~: k f9 i5 F' H0 I actionForEach =
6 y& t: `" {6 R( C) A1 v) U modelActions.createFActionForEachHomogeneous$call4 M: s* G& N- M4 R
(heatbugList,& _5 U3 p) D: M8 n
new FCallImpl (this, proto, sel,
( G: O. W1 t* ^! O, M6 { new FArgumentsImpl (this, sel)));5 ^+ `4 U; P" k o6 m
} catch (Exception e) {
0 w. c# Y; J% _% f5 h6 `6 g8 A5 Z e.printStackTrace (System.err);
, s* a$ G7 `! t' A4 ]" h }9 _' V* U7 [3 D; Z( S
6 V& E" [ Q8 p. _ syncUpdateOrder ();$ j" c! c- D; t4 b; i1 ]& s
6 o' L- f) b, o R try {
$ I; k1 X C5 d* H& r1 p- U2 V3 d modelActions.createActionTo$message ) u8 W- J% g- i" b9 J0 a& c* F7 }
(heat, new Selector (heat.getClass (), "updateLattice", false));
% W- u: s5 r% t$ ` } catch (Exception e) {
) b" o* ?% r/ I# Q3 T* s System.err.println("Exception updateLattice: " + e.getMessage ());
1 j5 K' k7 W3 K0 ?, I }
( k' g! g( f7 {* J! @( {: a: U" q8 f - Q( U+ l- J, n x% M: B' A
// Then we create a schedule that executes the
' M6 ~( l( B) d; p6 |) ] // modelActions. modelActions is an ActionGroup, by itself it
( f: F! r$ G3 e, x4 n% T& R // has no notion of time. In order to have it executed in# j5 k9 k0 h% _3 @
// time, we create a Schedule that says to use the
8 {! [) q% n) B // modelActions ActionGroup at particular times. This
: s2 z; R' Q# t // schedule has a repeat interval of 1, it will loop every5 U, h0 b* y5 f7 N7 }/ B
// time step. The action is executed at time 0 relative to9 K* t8 }: N$ t$ X
// the beginning of the loop.7 ?' v2 f0 I: @1 U0 `: L8 u" v
/ {4 f) A0 ^% A' b% X0 Z% F/ B5 Q5 A. J+ l
// This is a simple schedule, with only one action that is
B6 ~8 \/ R: e) _" t; v E // just repeated every time. See jmousetrap for more& n- ~7 n' h/ |0 `' b
// complicated schedules.
R1 {, U; x. _2 E! b/ | 0 N- T, L% i3 {, N/ R5 K
modelSchedule = new ScheduleImpl (getZone (), 1);& |; }5 x6 X) F8 a- }4 r
modelSchedule.at$createAction (0, modelActions);
- c8 H" ]; b! R
8 n( d8 Q" Y0 a* g+ m+ P& S3 n return this;* N5 z- W6 G' x
} |