HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( p+ }* E7 h9 {: T) t Q, J* e
. O! S4 m; S9 c0 V2 w& E. p1 N public Object buildActions () {
# O2 Z& p: g2 M super.buildActions();
& z& V, q1 }% q
8 o0 S" O$ p, e. |4 U, Y // Create the list of simulation actions. We put these in
( L# K/ ]/ d: e+ s* |# ^ // an action group, because we want these actions to be8 S4 \9 Z6 Q1 k6 j3 E/ N
// executed in a specific order, but these steps should4 o; s |9 g ~1 e5 E0 X5 T
// take no (simulated) time. The M(foo) means "The message2 z+ V2 M3 G; Q0 J% D. [
// called <foo>". You can send a message To a particular7 R/ X! X9 D. W3 o( o; K2 U* c
// object, or ForEach object in a collection./ R5 R& A- P( X& ?& e& w0 S
6 B7 `* V) n8 M
// Note we update the heatspace in two phases: first run, {& j1 w/ Y: n6 P5 J" M5 z1 k% E# f
// diffusion, then run "updateWorld" to actually enact the, U" [8 u, x! `
// changes the heatbugs have made. The ordering here is
: h' [+ E. K; R% n1 A% A) w // significant!
0 D! [' C2 ^" a9 r) O9 [- Z- j
! h6 d: h- e4 e // Note also, that with the additional
9 s: p9 Z3 [4 j8 U // `randomizeHeatbugUpdateOrder' Boolean flag we can
; e9 D- E% h' w& s8 M {6 J( ]1 r: a // randomize the order in which the bugs actually run; X& n; j5 P" r
// their step rule. This has the effect of removing any1 C; J- j. v c$ b; B( x
// systematic bias in the iteration throught the heatbug
9 d, C- a) d @7 ~+ S! l. P // list from timestep to timestep* ?: k0 i9 s0 v# T0 B5 D/ m
+ Z, W5 X, J8 E, T, j
// By default, all `createActionForEach' modelActions have: m# n4 q& e' [" `! T* V2 N
// a default order of `Sequential', which means that the1 ^; ?( b, ?- Y3 B X8 B$ S
// order of iteration through the `heatbugList' will be" H! f$ J% d% u* l
// identical (assuming the list order is not changed1 c( q4 G- W ~1 J& H2 [+ k
// indirectly by some other process).1 a- _. x/ @; T& K! K4 S6 Q
; [) F, e3 J* j+ Z; y modelActions = new ActionGroupImpl (getZone ());- ~; J+ G$ C4 N3 p. o. q
' T% H& ]/ ?; D+ @# _ try {, {( d& d- l/ ?6 Z8 z2 r
modelActions.createActionTo$message, H, \2 a6 g$ p9 ^
(heat, new Selector (heat.getClass (), "stepRule", false)); d8 u, q8 G4 H! j$ Q' k, D
} catch (Exception e) {6 z& x! M' o" }6 i! P5 [ n
System.err.println ("Exception stepRule: " + e.getMessage ());4 ~1 y0 n$ R& y7 R
}
4 b9 k9 J9 ~( f( h& B% _3 M! ?2 \' F7 B
try {9 U& ~. b$ n; |$ c5 S) `- l
Heatbug proto = (Heatbug) heatbugList.get (0);! N! V! s( f. p. o
Selector sel =
6 u, P! S, K7 X5 }+ s; q6 \ new Selector (proto.getClass (), "heatbugStep", false);
( ]! A# f3 a; ?6 U3 I# \ actionForEach =
4 c1 `9 Q, `) M7 G6 V modelActions.createFActionForEachHomogeneous$call0 X( V/ Z# ~' l- v4 U2 ?+ W; w3 L
(heatbugList,
" l( b( U* Z0 K7 i P new FCallImpl (this, proto, sel,
0 k4 E4 e2 S9 w2 G% o- D0 e/ k* U new FArgumentsImpl (this, sel)));
; R' U n6 G, ]3 E' }0 w/ ~ } catch (Exception e) {; B+ A/ p, L: t/ I
e.printStackTrace (System.err);$ E7 R; e; ~2 t, D2 \ I
}' O; d d. t) K- T$ ^, v# w2 u- f
" \ b* D, H" K0 O2 ]0 j4 Y0 p
syncUpdateOrder ();5 g$ Y0 X/ W; V6 Q, u
1 e- Q* l' A. i7 R3 \
try {. `" \& L" n' U: K E
modelActions.createActionTo$message
& y/ r j7 o5 r8 q* ]/ g& E (heat, new Selector (heat.getClass (), "updateLattice", false));) m: H1 [* T" d) X
} catch (Exception e) {
% g3 \) c- t. Z% R0 i1 E5 b" C System.err.println("Exception updateLattice: " + e.getMessage ());
3 H S& H$ q" e+ W" F# R8 t8 q) b }* O, I& p) V, ?! X( G$ N: g/ {
/ t) m+ x5 K6 v! v // Then we create a schedule that executes the3 q& d1 s3 x$ F# h- \
// modelActions. modelActions is an ActionGroup, by itself it6 a& v4 B. D+ |( Q+ @
// has no notion of time. In order to have it executed in o. d5 V* C6 c9 p9 Y9 C
// time, we create a Schedule that says to use the
( S+ I" e6 J9 L // modelActions ActionGroup at particular times. This
5 l G2 E7 D$ a2 h* f3 S& M9 b // schedule has a repeat interval of 1, it will loop every; V' }9 ~) X, V9 O
// time step. The action is executed at time 0 relative to
% L1 p( w) r9 i4 a8 u // the beginning of the loop.
* L ?6 ^- F9 W
2 X0 _: Z: C5 C2 T // This is a simple schedule, with only one action that is4 k1 [" r; W6 a1 L! [6 K3 o+ b
// just repeated every time. See jmousetrap for more% L. a1 k' c: Q! _9 E( u
// complicated schedules.7 R/ x2 Z5 R2 K' a/ j& L. n( ]; X; z
( ?$ R( S- K" u0 d$ u1 ^
modelSchedule = new ScheduleImpl (getZone (), 1);5 P- P3 k, a8 e0 _, a
modelSchedule.at$createAction (0, modelActions);
% R' K$ L: J8 o! X& a- C7 N/ H1 S : d k$ p c9 B: A: d: h& l4 k
return this;
) @3 I! W t9 g- ]$ Z } |