HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
S0 Z9 o% I- [- I% z# G7 L- ]5 f: x
public Object buildActions () {6 Q1 y! j( j/ Q# \
super.buildActions(); b4 a1 F3 b2 X+ N
/ p: V; t7 v: S, x8 ?6 z D( ]* b // Create the list of simulation actions. We put these in5 j) K; A& u S$ c7 Y
// an action group, because we want these actions to be# ~3 U9 B3 g- O* p# c( Q# W3 }
// executed in a specific order, but these steps should* w. J& M( [' l1 y
// take no (simulated) time. The M(foo) means "The message$ B2 p% P8 l8 }8 L# |
// called <foo>". You can send a message To a particular
) w3 B; I: l4 ` // object, or ForEach object in a collection.3 P5 I) ^+ s5 g: C3 T( u
; t R$ I* t+ M1 A3 N8 Z2 K. B# n // Note we update the heatspace in two phases: first run# x- J$ t* g5 d8 l1 s$ G
// diffusion, then run "updateWorld" to actually enact the7 h! \- c) b5 @+ `# E a$ J
// changes the heatbugs have made. The ordering here is" W$ H6 ?7 c" G( j. B- D3 K9 u% G
// significant!, U, Z1 c5 `9 C) Y2 O0 O4 b
! `( q, o- G7 k, ]9 {3 t# _/ u
// Note also, that with the additional
4 ? f& y4 h% i" U# c1 A% D" i& t // `randomizeHeatbugUpdateOrder' Boolean flag we can) f- S7 J; A$ L3 A
// randomize the order in which the bugs actually run6 z! m& u& I7 Q8 g) t! X6 ?: \! P
// their step rule. This has the effect of removing any
6 {& C: D7 @3 f/ \% [. k. `- i5 o# A // systematic bias in the iteration throught the heatbug; J/ C' p5 i1 w2 X' ^7 v6 E
// list from timestep to timestep
) u+ x2 m( T- s( X/ i/ r
3 @0 `& r( O( D. G- a: ]$ r0 \8 h // By default, all `createActionForEach' modelActions have v8 c* R- q! C
// a default order of `Sequential', which means that the
- U2 {8 U$ U' d9 T/ X // order of iteration through the `heatbugList' will be3 S9 O9 U1 }. X) e$ s
// identical (assuming the list order is not changed
. Z9 [3 l4 K2 q% O) k0 f // indirectly by some other process).
/ V) G. g l' k8 j3 @/ D) k
' g' i Q$ T5 b modelActions = new ActionGroupImpl (getZone ());6 {/ o6 c1 E9 R0 s; L# v* D4 L9 D- {
( F5 V1 d1 m0 f9 g3 k2 P try {9 u8 M- m' p5 G/ f9 b
modelActions.createActionTo$message
& P2 l7 R3 c* q6 |% R9 {# ~4 q: @ (heat, new Selector (heat.getClass (), "stepRule", false));
0 K% m* [ u# _' _: n# h% X } catch (Exception e) {: n/ i7 y: y% ~ B0 g( w9 _
System.err.println ("Exception stepRule: " + e.getMessage ());
* o8 x4 m& i" g }) k6 C) ?1 g1 W% [% X" ^
/ f4 l2 E1 A" s4 t
try {9 B+ R5 v" A5 \9 K
Heatbug proto = (Heatbug) heatbugList.get (0);
% v+ P O: g3 l7 _9 i B! h Selector sel = * _) S# e. b2 @0 ^4 c2 s
new Selector (proto.getClass (), "heatbugStep", false);
9 M& R: P+ ^2 C- Y actionForEach =- O1 {: P& _3 Q! y) ?' K" I$ g8 j
modelActions.createFActionForEachHomogeneous$call
% ^3 R# {3 X7 H0 Y2 g6 m2 e* _" z (heatbugList,( h r( j+ x+ \, x5 d$ ]* K- N$ g
new FCallImpl (this, proto, sel,- N( d9 S9 `2 t* B3 u" {% g
new FArgumentsImpl (this, sel)));; \6 P) H8 t0 J2 g
} catch (Exception e) {
9 ?- W5 x. h1 R3 | e.printStackTrace (System.err);9 r/ A. t; k& V# e& o; K! z5 ^
}+ V* `% Z# q( Q" Y* p
6 n) h7 c! d p9 Q# T syncUpdateOrder ();
" \1 k/ y; q& |4 k! `; w2 o; T: H& k) c$ P! B L y) R
try {! ~& D2 X. O) F* R/ u" H1 P
modelActions.createActionTo$message
) ~5 p C% z. ~3 } ] (heat, new Selector (heat.getClass (), "updateLattice", false));0 e @4 k z! h2 V4 K7 I! U' L
} catch (Exception e) {
7 T, ]; d! w& o# d* A4 ?% p System.err.println("Exception updateLattice: " + e.getMessage ());
" ~; p. H. J1 _9 }# c }
! R6 z& W# r4 ]# E b$ H0 M 1 p7 t. G- [6 r( {
// Then we create a schedule that executes the) A5 S2 e! C: U5 M6 }; }
// modelActions. modelActions is an ActionGroup, by itself it
2 Y- }8 d3 {* T; M, q3 p5 a' y# M // has no notion of time. In order to have it executed in8 K# q3 h, N2 Y& z! Y) `
// time, we create a Schedule that says to use the
6 Z. W5 U9 r: V; {. ]; S // modelActions ActionGroup at particular times. This
# B4 h. A6 Z4 f/ p/ W* m# z7 p // schedule has a repeat interval of 1, it will loop every
; e2 V# z4 W* [* x2 s/ b // time step. The action is executed at time 0 relative to
" [! W0 f& T) x. u& Y$ _ // the beginning of the loop.
1 ?( B* _8 e* U2 U- \
! q: B$ R' D0 F0 w/ T7 y // This is a simple schedule, with only one action that is
, S4 [: q9 \8 K1 I j // just repeated every time. See jmousetrap for more
- v; x: W. R l+ K7 d // complicated schedules.
) X& B' v# r" u- N1 N
2 @/ g$ Q+ E: T7 ]. ] modelSchedule = new ScheduleImpl (getZone (), 1);
3 F; O/ E1 ]1 J2 g modelSchedule.at$createAction (0, modelActions);
% e1 r2 K7 {+ ]4 d8 `+ n 2 {4 ^ a! l5 h6 {
return this;9 z% b& |1 x- _7 c
} |