HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 u L, }7 j1 I6 S7 G5 v2 C
5 l3 G! h1 }7 |3 g* h& H" r, P: ^ public Object buildActions () {' _: e1 x" G6 f
super.buildActions();
% q+ @! D$ w" x+ G
2 O1 _8 F6 z5 ^) l/ |. U' c/ z! ?4 W5 | // Create the list of simulation actions. We put these in; Y4 o" l }6 n- `- A9 @( y8 u
// an action group, because we want these actions to be6 q: y. Y- }) D4 T
// executed in a specific order, but these steps should
3 ]$ `' m! v2 `! x( L // take no (simulated) time. The M(foo) means "The message
. k2 D7 A* g1 L8 E9 D" @ // called <foo>". You can send a message To a particular) l5 \% f- `2 u6 b; f: A8 f6 k% @
// object, or ForEach object in a collection.
7 J" x5 R6 U% m , |& b4 O0 |4 b X; Z5 h* d. {! q/ c
// Note we update the heatspace in two phases: first run. J7 ]' q' E/ h5 h& P V
// diffusion, then run "updateWorld" to actually enact the' R, Z) j+ C! R
// changes the heatbugs have made. The ordering here is6 u- b5 d" `, l) }% G2 ~
// significant!
' c+ Y9 p8 B5 S) C% w * R: y+ b3 b( p8 Q
// Note also, that with the additional
* ^; m; F/ M! ?/ r O" C4 j; o: @ // `randomizeHeatbugUpdateOrder' Boolean flag we can- W/ s. {6 I: k% T
// randomize the order in which the bugs actually run
( f1 P! ^9 _1 I& r9 E // their step rule. This has the effect of removing any
3 }' z& D6 K5 b- p // systematic bias in the iteration throught the heatbug
4 d% m+ x/ A% u5 M // list from timestep to timestep
# M9 O& w) d0 f3 J
- T$ }9 e5 Z2 \( Y // By default, all `createActionForEach' modelActions have
: z7 i S1 O. n5 q" L$ ` // a default order of `Sequential', which means that the0 ^2 c/ U. K) l- h- \
// order of iteration through the `heatbugList' will be
. I: Q' h4 x. [- X // identical (assuming the list order is not changed9 U: J$ D2 O7 j
// indirectly by some other process).
, S& a. w/ Y9 S& D0 m : M- f+ g2 A% S0 t6 C
modelActions = new ActionGroupImpl (getZone ());
) G0 I9 ^) e) C" ]% |$ A6 E& q
8 L* J4 f$ l, Z% w `( B- x try {
% ~& i+ M6 f. t. n5 d0 [* P modelActions.createActionTo$message8 e" `; \( T2 ~! h7 b/ M% d Z
(heat, new Selector (heat.getClass (), "stepRule", false));! Z5 A7 r4 |: [2 q4 U
} catch (Exception e) {
8 P: A- P% X4 l- |& f System.err.println ("Exception stepRule: " + e.getMessage ());9 o- I+ L( c: q" ?$ C" a1 P
}
1 x6 o0 ^5 `. d
3 s. n. P: |3 [$ o/ o, n, m5 O9 L try {* d% {2 h0 _! D4 h5 H
Heatbug proto = (Heatbug) heatbugList.get (0);! F; x/ f7 f# ~" `* v7 x' K, }
Selector sel = ) F0 @1 W. `9 ]& A5 D/ n% t* v- |
new Selector (proto.getClass (), "heatbugStep", false);. U+ h" `" i/ N
actionForEach =, ~0 }3 m4 k s) @
modelActions.createFActionForEachHomogeneous$call
5 D1 a. V& o3 C: @ (heatbugList,
+ _6 t& |2 I: w3 V5 j2 X8 I new FCallImpl (this, proto, sel,# b3 Y+ T8 T0 W
new FArgumentsImpl (this, sel)));- @ N3 D3 e. z
} catch (Exception e) {
5 c# x1 K# }% Z# a) j e.printStackTrace (System.err);$ a- @8 H& F4 h+ o- {7 j6 K; q
}
, Z0 t$ `* J( Z/ b6 u
/ Y4 n- V. I7 z; @5 A6 A syncUpdateOrder ();
& I- x: w( w6 |% M/ G3 r7 b( E3 C
|3 V6 M9 w' m3 {, f2 l try {
. ]# g$ K- T+ Z v* n( v6 E9 P2 @ modelActions.createActionTo$message
2 \ _, {3 E' c! k @9 i1 n q (heat, new Selector (heat.getClass (), "updateLattice", false));
% P3 G8 b" \2 g5 b8 L& U& c } catch (Exception e) {
* C' R. H' y f: G* F H! Y7 H) O- C System.err.println("Exception updateLattice: " + e.getMessage ());& B' s7 n j& x9 ?5 k* l
}
7 k& W, ?8 l* i # W7 b. N2 v' V9 J0 S+ K1 u8 Y5 I
// Then we create a schedule that executes the5 ^# o( Q I, A; \
// modelActions. modelActions is an ActionGroup, by itself it( F* J; _$ e9 j8 z
// has no notion of time. In order to have it executed in4 N) X" P) r3 ?, f% A O5 U
// time, we create a Schedule that says to use the
. R+ o& {3 B& a* O% c // modelActions ActionGroup at particular times. This
" p5 m# q3 @" Z% v3 g // schedule has a repeat interval of 1, it will loop every% ]# ]9 K1 M6 G L) ?* ~
// time step. The action is executed at time 0 relative to
) |8 s9 F% |+ X& c // the beginning of the loop.8 m, ^$ _" H% [6 s4 W4 w. |2 V8 c
$ b: X9 t) x! T/ M0 W X // This is a simple schedule, with only one action that is
# O3 T$ S4 D2 P+ O# R // just repeated every time. See jmousetrap for more
. F, o C7 K `- X/ `+ E& R // complicated schedules.- D* `1 x. a% U1 V9 @
' b* s4 [3 n8 u) C
modelSchedule = new ScheduleImpl (getZone (), 1);" `+ p* Z* K0 o8 |1 f; p
modelSchedule.at$createAction (0, modelActions);& _8 a1 @( O8 `
+ l# m4 o. B, r% n' X$ y( b return this;
c$ I+ |/ u* B+ E3 L( n3 W X } |