HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 Z8 V- \' m1 r( C, H& B" E. c: q9 y2 P* h* m) U
public Object buildActions () {& H3 d' @4 V# N
super.buildActions();4 g6 g9 |: a% v4 ~ m, o
% d1 F4 U5 j; X$ F
// Create the list of simulation actions. We put these in; @6 [& P3 I5 \: |
// an action group, because we want these actions to be
+ O8 o; w' o/ t% K // executed in a specific order, but these steps should9 i- B4 }8 }1 c3 r# }
// take no (simulated) time. The M(foo) means "The message0 J$ S( w* `# t6 K( e8 [, k% e
// called <foo>". You can send a message To a particular
0 z8 j5 ?4 g- N. x8 T& _ // object, or ForEach object in a collection.
T' l8 u6 D- o$ l" G ' s q- [- f, ~: j3 U' h
// Note we update the heatspace in two phases: first run- W2 i/ B, n! D. n* C) A4 L# @
// diffusion, then run "updateWorld" to actually enact the! b3 g* t/ a% i/ B# s6 e4 w5 p
// changes the heatbugs have made. The ordering here is; j! |" Q O) E. e, \2 s# m
// significant!2 @: D: F5 w& v$ J* n3 k8 U
6 K$ B3 s+ [1 n$ H
// Note also, that with the additional% h. ]$ t! ~, x1 [5 {% N
// `randomizeHeatbugUpdateOrder' Boolean flag we can
' E+ @, ^5 Q# V // randomize the order in which the bugs actually run
8 f/ t7 x- c) E4 C% ~ // their step rule. This has the effect of removing any3 |& k' N8 j! A9 c2 V$ D
// systematic bias in the iteration throught the heatbug0 M) `6 C. o: H) y6 G" ~+ Z! p
// list from timestep to timestep% |) V E9 M" x9 s
; `( O' o' ]0 \* v0 A
// By default, all `createActionForEach' modelActions have: i; o/ h& {1 W+ u5 `
// a default order of `Sequential', which means that the
/ I5 W: i' R( i- C( v8 R // order of iteration through the `heatbugList' will be
5 ^9 C2 G4 m U- f: N& L4 z // identical (assuming the list order is not changed2 i4 }% g' w' h+ Z3 ]
// indirectly by some other process).4 C4 @7 `( G8 w' _& K' e
: k8 }8 W6 }6 s) H3 Q( B& L
modelActions = new ActionGroupImpl (getZone ());: Q4 s# d( A, c- n: x
/ G5 V& ?, J' l' q* t! s try {
) L; q# F) F$ e7 O. y0 b. l$ F" \5 K modelActions.createActionTo$message/ V* E) [: q" a3 P* x
(heat, new Selector (heat.getClass (), "stepRule", false));& g& X" y' m6 g
} catch (Exception e) {% ?2 p+ p4 ?( V# |: b, \9 k" K
System.err.println ("Exception stepRule: " + e.getMessage ());7 S9 E1 {" ?9 X0 l) G5 F3 {$ d# Y
}% F- u s7 e4 I) G5 b9 n) E
# y; N/ {/ [+ C9 I' z
try {* H7 q; U1 ]3 l* k
Heatbug proto = (Heatbug) heatbugList.get (0);) ?% X; w, z5 ]: \
Selector sel =
1 ^+ I7 e) V l5 r% _3 S4 [ new Selector (proto.getClass (), "heatbugStep", false);6 L6 v& j$ f: r8 U8 D4 m
actionForEach =
7 |( [1 {# J7 R b- R' \ modelActions.createFActionForEachHomogeneous$call
7 ~7 u5 r1 I: u" V' h0 E, c (heatbugList,( k" G3 i, r, q; D" G( W% @
new FCallImpl (this, proto, sel,/ X6 @! K, E% t0 \( r9 |4 M* V
new FArgumentsImpl (this, sel)));
/ R p0 h- O# L } catch (Exception e) {3 ?- }8 j6 S+ ?8 b: A7 U
e.printStackTrace (System.err);4 @! j& ^; Q. v3 R1 U: G
}: m- }# j4 K, e) x5 Q; S/ q
; `8 z( v, \1 Y! y% _ O& Z2 l
syncUpdateOrder ();0 I! Z6 _$ t3 e3 r
' q7 @8 ?. n. h- ~0 o7 N) T. f( x* K
try {
$ m- |0 ^+ H4 M1 |* `' a modelActions.createActionTo$message . a8 _ u# l/ l2 D+ G
(heat, new Selector (heat.getClass (), "updateLattice", false));
- n+ ^, h- f) X2 I( P/ E } catch (Exception e) {7 G2 l/ A: i5 x# @ _! x! j, J
System.err.println("Exception updateLattice: " + e.getMessage ());1 f& r6 v" d0 Y5 H0 D- z6 z
}
: G! s7 A0 r/ S& I! J: @$ N5 c0 }
& @% G( ~3 ?; P/ Z // Then we create a schedule that executes the1 p; T# F8 n! [% y9 M g
// modelActions. modelActions is an ActionGroup, by itself it
! c$ t" f3 m- z6 X // has no notion of time. In order to have it executed in
! e: R" w, I+ x1 b" `7 n4 o x // time, we create a Schedule that says to use the$ M- T- `% ?3 E/ p8 E% i( W/ W2 c
// modelActions ActionGroup at particular times. This1 n, V! |2 N+ l7 E
// schedule has a repeat interval of 1, it will loop every6 r* T1 }9 T# }$ u
// time step. The action is executed at time 0 relative to! r: m1 F- l4 B' m$ k& e
// the beginning of the loop.
/ N2 \) C1 @ L& g
+ r; T- S" P+ `! N5 P1 G // This is a simple schedule, with only one action that is
3 Q( s3 S1 U, K( e$ I8 d9 t // just repeated every time. See jmousetrap for more8 [/ k' |. N3 }/ p. t
// complicated schedules.5 w$ \8 q/ c* }" [
3 s$ a) s' m: Z0 g modelSchedule = new ScheduleImpl (getZone (), 1);
- S& E" w& z* g/ A modelSchedule.at$createAction (0, modelActions);) v) i. o$ ?0 Z- g1 M7 w
- j( [# L3 a& s" E+ O. T return this;
, m8 q& h) ?9 P" x% k } |