HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 A. b) J9 _3 ?1 B. m3 |7 a* L {5 l- L
% y! H3 ~8 C# j, o* o public Object buildActions () {
, D7 U/ i, O% D# C0 {% }7 a* W super.buildActions();
3 f1 e, {/ G6 j: O" Z
' R! }" U. U6 Y5 R3 p; ` // Create the list of simulation actions. We put these in
; L- k. w6 _1 ]0 d // an action group, because we want these actions to be
; A- L4 M! ?1 D o: M; O // executed in a specific order, but these steps should9 i. x( a6 q& }1 b7 O( @6 x7 G
// take no (simulated) time. The M(foo) means "The message- o1 n+ x' X1 Y6 C0 n$ y
// called <foo>". You can send a message To a particular. R! p; E9 D8 _( ~! K
// object, or ForEach object in a collection.
Z3 h: A. x9 {% W1 Z1 Y % e$ V* o$ \6 Y3 g$ r( \' r
// Note we update the heatspace in two phases: first run
! W1 w: ?9 h7 u$ I- S6 n$ c // diffusion, then run "updateWorld" to actually enact the
& c; j% U7 w6 N7 k // changes the heatbugs have made. The ordering here is0 O& D) Q I; @3 E
// significant!
. N8 U; i' B% M# V9 O% b9 @
b1 ]6 \* O0 v# e // Note also, that with the additional, @0 O5 F' e. w
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 J1 Y# z; `2 I) U' u# m/ O // randomize the order in which the bugs actually run
6 p0 ?! v% x' v! a2 a7 Y) \& B // their step rule. This has the effect of removing any% y. U8 }) W; ]" y
// systematic bias in the iteration throught the heatbug
% G4 J; k* _# V4 M$ y4 ^ // list from timestep to timestep) E- d: a) E0 o( t$ x
4 S0 ]9 j; Q; w5 C, l4 B
// By default, all `createActionForEach' modelActions have
$ R+ {9 S$ C% e& S3 U; n- q2 I; e r // a default order of `Sequential', which means that the
# @9 e: S0 t) O) { // order of iteration through the `heatbugList' will be0 K0 L& ?& m% n: B2 ^' b0 E
// identical (assuming the list order is not changed
+ v. I N! E8 @# w5 f# f6 l- S8 T // indirectly by some other process).
$ a$ O/ h- \6 ?9 b
, P2 W1 l6 X- D; w" K) g modelActions = new ActionGroupImpl (getZone ());2 r. M. }) Z) A' K) p5 o
3 n# }1 F' [) X" z, u( e try {- X5 Y6 F7 ?0 G
modelActions.createActionTo$message0 N: @* a+ t" n
(heat, new Selector (heat.getClass (), "stepRule", false));
7 s" a# n- L* z) g% r } catch (Exception e) {3 W6 X4 O. n: g* |. ^# s- ^
System.err.println ("Exception stepRule: " + e.getMessage ());
$ O! _5 _; e' X) ~: P! I3 ?! |7 i | }
3 D( ^2 t. S$ M2 P; J% k1 H" Y# w+ q$ v7 S E- G$ B! j$ G# S' N; n. f
try {$ a; l$ A! Z) O$ Z' ]
Heatbug proto = (Heatbug) heatbugList.get (0);6 i1 e- F) d# [/ i; s! G( u# h' B
Selector sel =
2 |" [3 ^. N C& u' ^, l& y new Selector (proto.getClass (), "heatbugStep", false);
! `0 E2 Q& e3 i7 q" I8 W actionForEach =
. {# j( R% h1 S# M8 C2 v; K# T modelActions.createFActionForEachHomogeneous$call! M" p. ]% T% P) J
(heatbugList,7 b1 M9 v1 D. n1 W! [ m" S
new FCallImpl (this, proto, sel,9 F. Z' h$ Y: T; ]9 p; u; {: v
new FArgumentsImpl (this, sel)));! j9 q$ T/ G& W, U
} catch (Exception e) {
( t' p* r- N! t& z9 \$ S e.printStackTrace (System.err);3 e. V& h( R' j) a W
}7 J9 ^0 ^/ m& Y$ V6 w
t; n" e1 B$ l4 A, Q
syncUpdateOrder ();
8 ]$ z) | S! h9 Z4 A7 a3 K- |+ w% J2 H& v7 D& m) K
try {
4 e- I$ J% ~$ @ modelActions.createActionTo$message 7 f0 r5 b) [4 u" A7 X
(heat, new Selector (heat.getClass (), "updateLattice", false));$ y6 F% l0 K5 x2 N/ W8 ^: ~
} catch (Exception e) {) I* ?" v9 N, h7 u
System.err.println("Exception updateLattice: " + e.getMessage ());/ h0 K+ O2 M- R
}
/ y/ l( h+ j, O 3 q! `! [: W/ z. M& \, A" W
// Then we create a schedule that executes the
. p/ n3 T) m# ]. S% ~1 V // modelActions. modelActions is an ActionGroup, by itself it
' @ X3 l- r/ Y" Y/ W // has no notion of time. In order to have it executed in
) L7 i6 ~* l) I6 S% C e( K$ g: q // time, we create a Schedule that says to use the
' `4 w8 a9 B: U6 \ // modelActions ActionGroup at particular times. This
1 a" ]% J' `6 c1 G; O // schedule has a repeat interval of 1, it will loop every
1 U( G( n2 ~8 k) p // time step. The action is executed at time 0 relative to. p1 W7 d0 g, p8 D
// the beginning of the loop.
; E3 x2 m# i' z/ o; m# b/ j. V3 Q0 E1 l9 ^( k, Y5 Y* ~
// This is a simple schedule, with only one action that is
& ]5 [- k6 q: W. H- H5 H // just repeated every time. See jmousetrap for more/ ~7 T, Z" T) \2 _5 R! b% ]& i
// complicated schedules.
2 w7 l& S0 T2 P; a C- y8 R* V
# ]2 I* x6 g1 M) S3 i5 \ modelSchedule = new ScheduleImpl (getZone (), 1);
' e o4 |* |* t& e& C modelSchedule.at$createAction (0, modelActions);* E/ h8 Z$ q2 {, N
8 a# k* @- ?; B* I; L; P
return this;7 Z+ F, F6 \5 n7 r7 x
} |