HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' O& ^. {1 R8 l7 K, T8 z. b
9 z9 v4 L% q7 j5 U* b6 B9 C& a% P
public Object buildActions () {
1 [# n# E" C4 p- p3 s super.buildActions();9 Z8 @. i* F0 g; ~1 g2 l
: K8 e3 l. M; c( G2 s7 s // Create the list of simulation actions. We put these in
6 ?5 e, g# D8 ~& ]& I // an action group, because we want these actions to be
4 B0 F& B( @. p a // executed in a specific order, but these steps should3 b& f# [6 y) h" y/ u J3 C
// take no (simulated) time. The M(foo) means "The message
& V8 t( Q) ]" [# `$ ~5 z, M9 U // called <foo>". You can send a message To a particular
/ m2 K1 o) E/ g# ?2 G* ~0 D& w4 O6 d% b // object, or ForEach object in a collection.
4 a% H! ~- B( O2 f% ]
5 P2 F2 P/ d9 G/ k ]$ h // Note we update the heatspace in two phases: first run
* ]7 K7 W8 S! w+ X0 g // diffusion, then run "updateWorld" to actually enact the9 k) E' f( Z+ m+ J' q I! z
// changes the heatbugs have made. The ordering here is! v$ n6 G% U9 I, \# |; Q; J
// significant!1 d" C3 C9 U: `0 Q9 h0 }2 W5 j
" q4 k) e( Z6 `" m5 C2 w& p
// Note also, that with the additional
7 ?6 U5 C# l( z& y4 V3 z // `randomizeHeatbugUpdateOrder' Boolean flag we can/ r5 D, L$ c$ y( M- C
// randomize the order in which the bugs actually run4 [3 S* k4 M, V$ _) u
// their step rule. This has the effect of removing any
/ a& X" @. {8 @4 x; b // systematic bias in the iteration throught the heatbug5 p" k- f& d( f5 ]
// list from timestep to timestep
/ R5 G% r/ H+ |$ y
6 H0 C& z9 [. _. h // By default, all `createActionForEach' modelActions have* b9 W. d" _4 r( B6 u+ }8 ?1 ]0 G
// a default order of `Sequential', which means that the
' m: f, U7 e- B, p3 M) ^ // order of iteration through the `heatbugList' will be
5 h9 ?& D9 J) F: _: T; b: s- a // identical (assuming the list order is not changed
/ s# t* S7 ?* d" T // indirectly by some other process).
8 ]* }; u' }& J) L. ~ Y$ r, P4 ?8 o6 e
! }2 f( q+ X' R modelActions = new ActionGroupImpl (getZone ());/ g9 N% R/ \; ^' i) i# E
( x; J. X5 H5 x+ ~4 o- {
try {
~; c# k9 e6 \ N modelActions.createActionTo$message
+ `( P2 h) |1 {- B6 s, Y1 f (heat, new Selector (heat.getClass (), "stepRule", false));( T, @4 G0 s9 i$ X0 I$ m2 v
} catch (Exception e) {( G8 L1 x* q& F
System.err.println ("Exception stepRule: " + e.getMessage ());. t0 G9 v# }4 o0 g
}: Z! N: z1 p! q! V) Y( @) q
% Y9 J. }. s1 T8 \
try {
7 U6 A9 X) C( l3 z. q8 V Heatbug proto = (Heatbug) heatbugList.get (0);; ~7 O% p3 C1 h: U3 B: C
Selector sel =
1 l/ w, B8 t' Y( e# j new Selector (proto.getClass (), "heatbugStep", false);* j+ y& ~* @0 A' q# i3 e
actionForEach =
" j3 q9 D0 r- `- @. t7 M modelActions.createFActionForEachHomogeneous$call
' y& ~; A0 s y; Y% ] (heatbugList,+ t. Q6 D2 U$ }2 ~7 g6 V" z
new FCallImpl (this, proto, sel,- c3 A- A. U5 V
new FArgumentsImpl (this, sel)));
# f5 O$ @! z% P" G$ Z3 L/ C: W } catch (Exception e) {
5 U' A5 a7 M6 o9 q1 o: V S e.printStackTrace (System.err);6 I/ \5 ^3 j& G4 a2 E
}4 _3 Q; r% q ~' I5 i J
7 \& E3 |' L# p8 o/ p syncUpdateOrder ();& W1 b& D+ L% Q N" u
) ~8 J! A8 ^: c1 L4 |. M7 V, T
try {+ W& Q4 g* B: P" T3 X
modelActions.createActionTo$message
: p: j+ n J+ i& r7 u (heat, new Selector (heat.getClass (), "updateLattice", false));
# a$ Y& A6 E9 W } catch (Exception e) {+ K+ G! p0 @/ A7 Y5 L6 n
System.err.println("Exception updateLattice: " + e.getMessage ());
5 a' z6 J; _* m }, @$ K- v1 s" U% x( G _) q
0 j7 a: y6 Z5 G" K // Then we create a schedule that executes the, P$ L1 z2 u& U9 {
// modelActions. modelActions is an ActionGroup, by itself it
% R0 y% e: _0 D: m; C // has no notion of time. In order to have it executed in
$ }% b8 m" x2 s4 X // time, we create a Schedule that says to use the5 n1 ~7 o" I$ |" V
// modelActions ActionGroup at particular times. This
6 f+ Z* Y( W' [+ \8 F3 B // schedule has a repeat interval of 1, it will loop every3 x3 F2 h5 ~! H. y Z- @
// time step. The action is executed at time 0 relative to
- l: e* c1 K/ k // the beginning of the loop.' ?" y1 g( U* T9 [" a) u
( e. b7 F) t7 k
// This is a simple schedule, with only one action that is# S% T! k. T0 |" ]# a; k9 D+ {
// just repeated every time. See jmousetrap for more) ], |# U1 C& Y2 F" S* n j9 H
// complicated schedules.
. a# D a _9 m: q9 N; @
: L2 j; ` O# ]" B' W6 c modelSchedule = new ScheduleImpl (getZone (), 1);
8 E; L4 W0 v9 p8 w+ A j modelSchedule.at$createAction (0, modelActions);
% t! Z: F0 y) [0 j
0 `6 t% G8 @3 l; J, J; D2 N# y return this; B+ r4 v2 A0 y* H' S' Z
} |