HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ G! X; Z* _* J, {3 j6 C" X% G# e
& V5 o) ]" w& J6 l7 ^# p public Object buildActions () {
' I, D" Z/ h* N2 @9 I! z super.buildActions();
. x: L+ O* _* t" E ) k w ~1 w4 Z0 w8 q
// Create the list of simulation actions. We put these in
! a# q |! c+ n4 i; U9 K // an action group, because we want these actions to be
$ K; F& M8 _ S // executed in a specific order, but these steps should
0 p5 X. D+ y! j. H+ Q // take no (simulated) time. The M(foo) means "The message
5 V7 X: z3 b( q/ ]( F // called <foo>". You can send a message To a particular
0 c6 Q- z3 F5 D1 Y // object, or ForEach object in a collection.0 S# a/ n0 g+ c& C* w
4 _" N: B) a% F, {! { // Note we update the heatspace in two phases: first run
# J3 W) v5 `0 J5 B7 c4 { // diffusion, then run "updateWorld" to actually enact the
& ?$ \& ^! ?6 t4 V @ // changes the heatbugs have made. The ordering here is
* V5 p0 @& [! W7 x+ e* k // significant!
+ i f# |$ Y! I, f! k
/ r) {7 {6 u- ~- l // Note also, that with the additional
" }& ^! J. E! _, Q6 B // `randomizeHeatbugUpdateOrder' Boolean flag we can4 C& Y, l. W) @! v
// randomize the order in which the bugs actually run
. D' b/ w; e+ O' b6 r- s0 k // their step rule. This has the effect of removing any
0 ]0 e+ I, [1 g* Y' F0 o9 w // systematic bias in the iteration throught the heatbug8 w% x5 ]: d9 M
// list from timestep to timestep
2 d- ~. s' B5 T! Z# J9 D " N/ h9 T% f1 j. Z
// By default, all `createActionForEach' modelActions have
( T$ u. L0 J* R // a default order of `Sequential', which means that the
2 i7 }5 }* O$ O' S // order of iteration through the `heatbugList' will be0 Q2 F. d5 Q' q" X# V' ~
// identical (assuming the list order is not changed' s2 k% N8 D- C1 h; W
// indirectly by some other process).
6 t3 t9 h+ e& G1 j+ c; V/ V
: m: J( V# ~( A7 U) I8 b modelActions = new ActionGroupImpl (getZone ());
& R! Z% G4 b+ `1 v6 L7 p( x5 l1 i5 w& \
try {
0 t% _# F0 w+ L5 Z( c- b modelActions.createActionTo$message
' I, |- x2 j& E. H9 A (heat, new Selector (heat.getClass (), "stepRule", false)); q' L; @9 L$ P+ \
} catch (Exception e) {' x, |6 K* m/ L. T* P4 a2 l9 W
System.err.println ("Exception stepRule: " + e.getMessage ());6 P7 t3 G( p8 x5 t# ~6 ]2 i) y7 ]1 s2 _
} h; P. A; F) i0 e: F& t
- q9 l; J% g8 K. l7 C. a) p' ^ try {; g& e& q3 h3 _% O5 Y$ v0 s1 W8 N
Heatbug proto = (Heatbug) heatbugList.get (0);+ m9 t" E( U9 D& L3 M) [
Selector sel = 7 o7 L/ I% I. I/ G3 e9 G
new Selector (proto.getClass (), "heatbugStep", false);
& r$ k, T7 I, w; x actionForEach =
" X8 o8 Y+ d2 ~/ e- o modelActions.createFActionForEachHomogeneous$call2 D! q) ^& g+ I/ g8 Y- e& w7 Y
(heatbugList,! n/ m, _6 _: `% ~" t: q9 Z
new FCallImpl (this, proto, sel,! K4 j+ |& R5 d; G$ U
new FArgumentsImpl (this, sel)));% H# H& C% A5 }, E4 ~. i" |& e
} catch (Exception e) {7 S9 p% c. L6 K$ o( A# k; W
e.printStackTrace (System.err);: T# I6 u- X6 P' ~3 U
}
& e6 O5 i$ s' x/ \$ d' m' u8 m ) M3 A4 k# q q( |
syncUpdateOrder ();
0 X0 |! L' z8 `$ h; Q( H
/ S+ S. i' O9 r3 @+ I$ e try {
8 d, v- @( ^' V# W modelActions.createActionTo$message # v+ c$ Q! x: k6 O8 f
(heat, new Selector (heat.getClass (), "updateLattice", false));. H/ V* D4 B( D) b) M
} catch (Exception e) {
1 }; @$ X! G. W7 s System.err.println("Exception updateLattice: " + e.getMessage ());
& T* a# ]& a% \: t; e0 s3 d } X/ W: |$ }* F P7 J* n% `/ j
: I# I7 F" s5 X% u0 d/ @
// Then we create a schedule that executes the
/ t( w9 S$ W7 m0 i. `/ c // modelActions. modelActions is an ActionGroup, by itself it5 H& n4 q3 g( e3 J s0 w* |
// has no notion of time. In order to have it executed in
1 x: K) K- d6 z+ m8 q // time, we create a Schedule that says to use the
' k+ }# n. o$ {* W% G6 ` // modelActions ActionGroup at particular times. This
0 L6 e. P8 _8 L v9 ~! A // schedule has a repeat interval of 1, it will loop every
4 ]2 R! g' e1 n& i5 @; c! A/ K // time step. The action is executed at time 0 relative to. e4 d# T$ k+ K. p3 [/ p
// the beginning of the loop." F a+ }. @ a1 l7 S+ s
* x* q9 @- d8 s5 R. ^6 R7 p$ E" a
// This is a simple schedule, with only one action that is
- n. x0 ^5 P3 ~4 d K5 a // just repeated every time. See jmousetrap for more+ {. c8 O; N* Y% R
// complicated schedules.
8 H( r7 K0 v3 C / q; |. P! e& A+ B- l2 D
modelSchedule = new ScheduleImpl (getZone (), 1);
, n+ z6 M5 A9 z. @& i7 O L2 \ modelSchedule.at$createAction (0, modelActions);
; p" L4 R) u/ t- j9 z
+ T2 Z3 X) D, B+ d$ I return this;; @' ?- q+ n5 l3 B6 i1 H( ^" y: b
} |