HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( {$ g9 K* Z1 p- w3 o0 e) ^7 N# j5 L; X# W @/ P
public Object buildActions () {
* Q! Y0 S0 w! Z6 C/ P super.buildActions();/ E( `, w/ k4 O7 O/ C7 j B& S; D, ~
) b+ ?& N9 a( Y& t- T
// Create the list of simulation actions. We put these in
- }* F+ t3 Q9 E, N( l$ `2 j // an action group, because we want these actions to be
6 J0 ^+ q3 K0 B8 z* r5 z // executed in a specific order, but these steps should
/ g1 B8 o+ ~$ L8 ] // take no (simulated) time. The M(foo) means "The message8 H! I; P; t8 j0 x$ `
// called <foo>". You can send a message To a particular
! I3 |9 I8 X) O& }5 G9 y // object, or ForEach object in a collection.9 d b" {- b% u$ R
& k/ n- q8 R4 V; s# Q
// Note we update the heatspace in two phases: first run2 [0 y3 h8 |" U& V, u- f
// diffusion, then run "updateWorld" to actually enact the
. V9 ~, M0 W+ |4 i$ z // changes the heatbugs have made. The ordering here is
. t. A2 h' k/ ] // significant!: M2 K) N$ q7 S% k, h& t, }, e
" Y8 `* P9 J8 G: I+ Z
// Note also, that with the additional
5 W$ f. E7 @' ~# I // `randomizeHeatbugUpdateOrder' Boolean flag we can) D5 e7 L" J# S, h
// randomize the order in which the bugs actually run5 y- ]5 h/ M. w9 A6 K+ Y8 y& K& c0 b
// their step rule. This has the effect of removing any) G I$ j5 _3 b" u0 X
// systematic bias in the iteration throught the heatbug9 h9 U, A/ t2 J8 z
// list from timestep to timestep$ D0 P c1 W- H9 s! h
, Z* K* ]/ ]* X$ f: A& `* ~ // By default, all `createActionForEach' modelActions have
% e+ C2 K& M0 `7 J* z9 i3 e // a default order of `Sequential', which means that the
$ V. f Z% J0 @1 E // order of iteration through the `heatbugList' will be
2 E& P1 ^/ U8 a, f7 G' u // identical (assuming the list order is not changed/ W. T* W* e' C& P; X
// indirectly by some other process).
) ~2 M$ M1 p; e( }/ \+ J
1 D: L7 C! |" J) g( ~ modelActions = new ActionGroupImpl (getZone ());; W- d6 i# R* N9 N8 }
6 {' N+ R- T& I6 z9 u try {
: ^* f8 k; O' V" o$ R modelActions.createActionTo$message
+ S1 \* K5 n) P (heat, new Selector (heat.getClass (), "stepRule", false));
* n2 F! g. n! S0 K6 k9 L2 U# t" h; p! P- O } catch (Exception e) {: ~2 F( p" I6 j
System.err.println ("Exception stepRule: " + e.getMessage ());+ L7 n! q6 i& s
}
- l) i9 F8 M$ x M' t! }9 r
1 K/ c5 f+ f! t5 b7 g try {! w* p7 Z- ], F* k! E
Heatbug proto = (Heatbug) heatbugList.get (0);
5 w( C' \! |) }( V5 H Selector sel =
8 D# y5 s/ r. h# f* W% W2 [9 b new Selector (proto.getClass (), "heatbugStep", false);
7 x5 f" V6 W/ \+ T' V2 { actionForEach =
$ F; ^" E: V# B& L modelActions.createFActionForEachHomogeneous$call
6 u5 N3 h ~+ j5 c (heatbugList,$ `; r3 g: x1 t" R
new FCallImpl (this, proto, sel,
" q" W s& U* u8 F& ^& {# S7 i# F0 H new FArgumentsImpl (this, sel)));3 x' |0 B# K% g9 r: c8 D3 D
} catch (Exception e) {+ F; L& k+ q4 D: H
e.printStackTrace (System.err);, Y; {; |& m% x1 j& `
}# c4 n7 b/ ^ e0 L
8 v# _/ [. |- f* e3 Q
syncUpdateOrder ();$ S$ B6 E: n7 G4 R5 L2 f( b: u: u; m
+ l! O0 {# b5 D! P8 n try {
% V) |2 {' c+ A, r" l0 i0 a modelActions.createActionTo$message 5 B7 h, ^: R2 K4 I8 G
(heat, new Selector (heat.getClass (), "updateLattice", false));
! L2 @9 v8 h8 `8 @* v } catch (Exception e) { L- Q3 f9 e$ r* V; I( X
System.err.println("Exception updateLattice: " + e.getMessage ());
/ c* p6 E% `$ X0 h+ l1 ^ }9 ]* u7 W0 R9 B! S$ v+ p+ ?' |) ]
/ H& K; I; d/ K! d! \- ?; n6 `- [3 z' m // Then we create a schedule that executes the
( U9 A) `8 G$ }5 R2 g // modelActions. modelActions is an ActionGroup, by itself it
/ V, X9 t' }& r& s4 c0 i* {( h- V // has no notion of time. In order to have it executed in1 {+ \/ g* a2 h3 s$ X) C
// time, we create a Schedule that says to use the
7 E9 B4 {4 ~* i // modelActions ActionGroup at particular times. This
1 Q+ k! C- x2 ?9 ]4 h' a. l+ n // schedule has a repeat interval of 1, it will loop every }6 |, m: }. C: w2 w( c/ ?
// time step. The action is executed at time 0 relative to/ q }! A; V$ w6 u
// the beginning of the loop.
* B# V7 n M9 p& n
$ D: H7 v' v7 D4 [ // This is a simple schedule, with only one action that is
9 p4 d D# F" m ] // just repeated every time. See jmousetrap for more$ L: m4 T- W4 ]" H
// complicated schedules.; d" C0 h" {0 j
+ k( k$ B' q# w- c modelSchedule = new ScheduleImpl (getZone (), 1);$ c4 M$ G, x; G# `; G' s" Q
modelSchedule.at$createAction (0, modelActions);' l% u3 |" s p& J( i/ G
0 N* b+ p" L+ L/ r& Y5 [* z0 \, ]
return this;* Z* T- R1 y) ?0 L
} |