HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ F: b3 f6 A9 r% [; J! L# V9 {0 i* e" \+ {
public Object buildActions () {, v; M7 Q' j2 F% U" Q6 i7 p! O! g
super.buildActions();
& A0 Q- }6 a1 k7 F$ f0 I7 B5 k' g
5 C. N6 _% G2 c' [5 r7 P! E: L // Create the list of simulation actions. We put these in' b# d; Q4 {: x b
// an action group, because we want these actions to be p u2 i/ K( U0 o9 t
// executed in a specific order, but these steps should2 {7 a8 c c# j" E
// take no (simulated) time. The M(foo) means "The message
0 M/ Y# [+ w* ]8 ^3 e8 f // called <foo>". You can send a message To a particular8 ]7 a, V; k( M% _ u; Z* E, s& [
// object, or ForEach object in a collection.
/ y5 e" e! Z) }3 r- w* S
1 z1 ] S v- w( a) u // Note we update the heatspace in two phases: first run
8 Y; w. s5 E5 Z // diffusion, then run "updateWorld" to actually enact the
+ W" d1 |- Y( O9 A // changes the heatbugs have made. The ordering here is- d3 `8 e# ]0 l+ y0 [1 _8 ?, F
// significant!' Y/ t" F6 W' {$ H& a0 o
. ^) D6 g& D% g& C
// Note also, that with the additional5 Y" p/ W7 l9 J: c4 V+ Y7 v7 F. ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can: {/ v* V. N$ e6 V2 x9 i7 u
// randomize the order in which the bugs actually run+ ]5 }1 T0 a6 y& H# D
// their step rule. This has the effect of removing any; h' y+ G0 e6 }7 P
// systematic bias in the iteration throught the heatbug& q6 q7 o3 X* Q4 z! O% Y
// list from timestep to timestep
& |6 J# H" D7 o. Z
% n2 k& t( x+ z2 i) ]0 u // By default, all `createActionForEach' modelActions have
; A' W$ H1 o: @1 a- Y- K/ k // a default order of `Sequential', which means that the
+ H- T2 x; E+ V2 o // order of iteration through the `heatbugList' will be
7 ]( ?+ u) r9 K9 s( A+ t) G& J // identical (assuming the list order is not changed
# F) d$ I2 T8 f5 \# f // indirectly by some other process).* T/ _; f3 v) y3 B
- B H* n$ w0 | _ modelActions = new ActionGroupImpl (getZone ());$ C8 |- f8 s! J( F3 d4 q! R
7 U" H% x' u; I3 _6 `% I
try {" y: x: S( \1 M3 D$ ?
modelActions.createActionTo$message
. Z6 }% _: x8 J4 ~3 K' V0 m7 ` (heat, new Selector (heat.getClass (), "stepRule", false));
) h6 ^! A# S. q' u } catch (Exception e) {: I M( L- E2 K) q* w- M1 Y
System.err.println ("Exception stepRule: " + e.getMessage ());) b- w" m5 X0 k4 e0 V7 X1 O( M
}
" q( ]$ t4 X+ B$ Y" ?! N' Y0 y
7 c" b9 a: V4 f# ^" k) Q try {6 n# H( R: Q. l9 G
Heatbug proto = (Heatbug) heatbugList.get (0);
1 a F. r6 J; }& |, T' A Selector sel = 3 K& J: e! i- m/ F, B
new Selector (proto.getClass (), "heatbugStep", false);
p0 f) M4 M5 ^ actionForEach =
- E# S) Q4 ?( m" W+ n; Q. {2 O modelActions.createFActionForEachHomogeneous$call
. R8 H) N! c' Q- l/ ? (heatbugList,
9 I7 J) l n' d* k" i" |4 n new FCallImpl (this, proto, sel,0 J/ M0 k4 N$ W+ j* P5 y }" V
new FArgumentsImpl (this, sel)));2 P1 ]6 _+ s$ u
} catch (Exception e) {
U. {# e \/ }, p0 S7 i. W% z e.printStackTrace (System.err);
# s6 ~: y8 L2 u( w5 ~/ T } r5 g; A- }+ F' d q
2 y1 O, T( ^6 j/ d8 S8 {* Q
syncUpdateOrder ();0 N% T) O2 n; A$ p4 @ C
4 g3 _+ e2 g. h. D" z% v0 k
try {, a( s: }3 ?" Y. B; g7 u
modelActions.createActionTo$message
! g6 y+ Y- F% x3 y" n (heat, new Selector (heat.getClass (), "updateLattice", false));/ K# m" c+ t% d' {4 p3 _
} catch (Exception e) {' V) ?" J& g$ o4 @
System.err.println("Exception updateLattice: " + e.getMessage ());
# S W& C0 q' o; A4 s }& x! |! k' a* h* W8 D
9 S. C" d2 F) }' s6 x+ r
// Then we create a schedule that executes the1 p3 g2 q% L. m" d! k; P
// modelActions. modelActions is an ActionGroup, by itself it3 N7 G D, i; Y& j$ E& D
// has no notion of time. In order to have it executed in0 V3 b4 a' U* @% v* h# J
// time, we create a Schedule that says to use the5 S# j" k' v7 l- f2 D" n" e
// modelActions ActionGroup at particular times. This; x ?+ [2 F& J" A+ M$ V
// schedule has a repeat interval of 1, it will loop every3 h) h! x) a3 L" j: W
// time step. The action is executed at time 0 relative to
8 \. A* z, O' i2 Z, B* X // the beginning of the loop. l4 p& B' H4 G' n8 t5 G
' V# I; b- `5 a0 B$ g5 b
// This is a simple schedule, with only one action that is4 m5 v ~, p* D/ V" D8 V: R
// just repeated every time. See jmousetrap for more
+ R4 I* r& w2 I4 ]6 k T3 K- f$ ^ // complicated schedules.
. d9 Q. s4 P0 A. ~
+ l- v* a3 M6 z' f" [7 B modelSchedule = new ScheduleImpl (getZone (), 1);
; M ]' B! h3 b5 S' \9 o6 U modelSchedule.at$createAction (0, modelActions);
3 {+ [6 v; y) d3 e
, N# h i/ ?3 e7 g- v( B- P return this;
% l; C& {* e- s: T; b" z7 S1 _$ r7 c } |