HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# G0 D' x, \- |: t& N' e; W' V# l2 _7 {7 l/ v+ z
public Object buildActions () {
0 {+ P) ?/ u) L* [4 U4 N+ Q) R# ^ super.buildActions();
. Z# o% C. d6 T* Y l9 N, j1 b) L
- U4 `7 M4 {, I) X5 v; T7 _! m // Create the list of simulation actions. We put these in2 v8 A5 g' s) n' x t6 e
// an action group, because we want these actions to be
- [: C$ L) \' r' h# X2 l // executed in a specific order, but these steps should
5 P" H' a" z; y" t8 P( r // take no (simulated) time. The M(foo) means "The message7 v; O- i6 t6 \
// called <foo>". You can send a message To a particular
6 m' V1 [( a8 F/ H // object, or ForEach object in a collection.6 z5 b7 o! i- m# J9 v$ Y, z. h
& f: M% F/ @$ v" F
// Note we update the heatspace in two phases: first run- p1 }8 p# H' z' g& Z
// diffusion, then run "updateWorld" to actually enact the
6 n! \: z* A$ p // changes the heatbugs have made. The ordering here is
& X6 {6 Y* n" G2 s // significant!) @* M+ X( h4 e& Y( e
; Y! n# `" i$ }' @0 G# o
// Note also, that with the additional
- ?: T: |$ x2 e+ A s) }! k // `randomizeHeatbugUpdateOrder' Boolean flag we can
1 o, f/ ?' |7 ]! T& y& S7 W% U // randomize the order in which the bugs actually run
9 X! ]9 s% |4 [* d7 S' }, v // their step rule. This has the effect of removing any/ Y/ } W t3 D
// systematic bias in the iteration throught the heatbug ~1 p6 Y& o& v) p
// list from timestep to timestep
8 ?; H T; A+ @) [: ]$ o; u
/ O' }3 }, D' r* m // By default, all `createActionForEach' modelActions have
( ^/ i1 X7 E+ V // a default order of `Sequential', which means that the) W% }# U+ k4 t, W
// order of iteration through the `heatbugList' will be; ?- ~' C# _" i: A
// identical (assuming the list order is not changed& t" H9 W' V5 V [" x
// indirectly by some other process).
2 D \/ [. ^! f% p# }# G
$ R/ F* G: A/ r6 J" x- z) ]) y modelActions = new ActionGroupImpl (getZone ());$ M8 _8 ?& v3 x* B' @9 L3 Y& q4 p
9 I* y9 U0 l. n* O9 V
try {: |$ r7 @( R; N5 X4 V$ S
modelActions.createActionTo$message
3 T8 |/ d6 }2 u8 s( r2 f* }7 R (heat, new Selector (heat.getClass (), "stepRule", false));
: H6 |* l! O3 X5 H3 Q$ Z } catch (Exception e) {
" O9 s, Y0 X1 W: Y$ \3 F% N System.err.println ("Exception stepRule: " + e.getMessage ()); m, q# E/ p3 s# Z; `* [
}
+ T% G4 k0 v: u2 E6 o, `
* i$ T" i4 v! u try {
. D) ?5 g" K' J+ g4 P1 ]+ ^ Heatbug proto = (Heatbug) heatbugList.get (0);' l9 o% S6 r p4 F
Selector sel =
# U' g+ [( a8 ~5 c$ c0 J new Selector (proto.getClass (), "heatbugStep", false);
9 n- T! _) \' B actionForEach =
& {4 B( R* u2 R, G( b: Q9 D modelActions.createFActionForEachHomogeneous$call
+ l8 [; |+ _# d+ L (heatbugList,1 D; \1 ^6 {7 _9 B& ^' ?2 u+ r+ l- M
new FCallImpl (this, proto, sel,; N# Y: {% ~1 Q. p& z* v0 l
new FArgumentsImpl (this, sel)));
; E9 {5 W# V8 k3 R0 r- Q } catch (Exception e) {! \$ _3 q, z% M$ K0 O: b+ ^
e.printStackTrace (System.err);. q* H2 k3 J+ l7 a$ q
}% w# G* i4 |+ J) ]! q7 b
, {8 \4 R6 X* [4 ~ Z( X) S4 \- ?% O syncUpdateOrder ();
& i) y* s Y' s! b# K
6 o# r7 d8 U+ M9 W$ D9 i: W try {: | f3 V" E( P3 B2 F
modelActions.createActionTo$message / N( `5 k2 M( M) Z, u
(heat, new Selector (heat.getClass (), "updateLattice", false));
" n7 a+ \$ w7 ~& ^5 g2 ^! }+ w6 Y# |# ^ } catch (Exception e) {; ~+ Z+ z( o/ ~: @1 g4 C/ h- D4 Y
System.err.println("Exception updateLattice: " + e.getMessage ());
) K4 T V& o9 R& v0 D5 O }
. I4 j8 v& a; ~) b
. `. L8 U6 k* o- | // Then we create a schedule that executes the
2 K: A# L6 f5 [7 w( J; a // modelActions. modelActions is an ActionGroup, by itself it
1 ~' k- m$ C* b# f1 g: G8 r7 ~ // has no notion of time. In order to have it executed in
" k, | k" V% _: I( m // time, we create a Schedule that says to use the8 c: s" K# \2 @2 v' ` \8 p3 n) f
// modelActions ActionGroup at particular times. This( k* R1 m/ I! U: s" P" `$ J1 `
// schedule has a repeat interval of 1, it will loop every9 w+ z" Y2 {! O9 ]) i
// time step. The action is executed at time 0 relative to1 b m' u% O, g! P U- l0 L
// the beginning of the loop.
9 |( O) f5 f6 d' b" }* C
5 f) J0 _8 I1 e5 T/ }0 a" p // This is a simple schedule, with only one action that is: K# n, c, L1 G |3 w/ ^' f, C
// just repeated every time. See jmousetrap for more
) c! v, `9 T" T // complicated schedules.
; H) q" u1 T( [1 G$ A - w# R( r4 t9 i l: N# e
modelSchedule = new ScheduleImpl (getZone (), 1);
: g+ X# ~$ x$ S7 v! R modelSchedule.at$createAction (0, modelActions);, r& ], O2 h9 A* [9 i/ n1 `" L0 r
5 I% B9 R% z3 Q1 @
return this;
" V8 I4 E0 ^0 O* C } |