HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 a! ~) ?) S6 S& Z @
0 K& o9 A( B' @: b8 ^: E: Z public Object buildActions () {: w, L6 Z/ J! a$ n/ {
super.buildActions();
' ~; I' o5 N1 m
( h& G) w1 s, o2 Q% h2 ] // Create the list of simulation actions. We put these in
# n: o$ ~ P: x! ^" `" m // an action group, because we want these actions to be L% {* ?$ P f8 V* O
// executed in a specific order, but these steps should- @9 Q8 \/ H0 ~/ `7 k
// take no (simulated) time. The M(foo) means "The message
b5 A$ d" q9 N+ c" p // called <foo>". You can send a message To a particular
# `2 h- C; \, X // object, or ForEach object in a collection.
$ r" Z& p8 N( E/ ~' m8 v6 G
+ c K! u0 A7 |& O3 [+ \3 o; p8 J // Note we update the heatspace in two phases: first run7 g8 f& |' E5 O! t f8 a$ d& J
// diffusion, then run "updateWorld" to actually enact the5 W4 v" `& k- J% V, H* J; x: Q! Z
// changes the heatbugs have made. The ordering here is
0 H, Y+ t; _& x8 |4 \. R. t9 x# m // significant!
9 A: d+ j' w# G2 m- e5 B2 L
4 ?: w' X# k" {' J) T // Note also, that with the additional2 D: }! `9 z# i$ g1 y# I
// `randomizeHeatbugUpdateOrder' Boolean flag we can ~, I" w7 s: M5 U- k- d
// randomize the order in which the bugs actually run8 ^4 Z6 m* ?& g( N; v
// their step rule. This has the effect of removing any* g; c6 m' Q# ]9 B5 s
// systematic bias in the iteration throught the heatbug
y7 c0 N! f8 }- J. P/ ~4 } // list from timestep to timestep
* m m4 `. |& }$ \. T
2 X3 w7 A6 N& i- v+ X" p9 Y5 e. Q // By default, all `createActionForEach' modelActions have
% x2 A- v( I- h; [7 n+ S& N/ ? // a default order of `Sequential', which means that the4 u& r: b$ h" B+ q5 N) Z" G
// order of iteration through the `heatbugList' will be) D) J6 V* u7 K! B$ F
// identical (assuming the list order is not changed
4 s' H4 N/ B9 L9 S: E // indirectly by some other process).
! j, \/ d& X" {/ @( N# z
2 i1 p4 ^* |* b1 p modelActions = new ActionGroupImpl (getZone ());
. m& l/ Z# O' a; N# D- v& S0 P6 D5 Y3 _, _ ~: e
try {7 O* ~4 j& q( l$ B2 K8 p
modelActions.createActionTo$message
3 O8 P5 ^+ d/ }& O2 R z$ z0 m- U (heat, new Selector (heat.getClass (), "stepRule", false));. G4 `, d5 B) `/ P1 ?
} catch (Exception e) {
9 c+ O9 D. C, J# a0 R9 f System.err.println ("Exception stepRule: " + e.getMessage ());
) H* V+ k1 A) D5 K# g, R }
1 p% v6 L# O& v+ o! ] V2 [3 X+ |
try {
* g' ~) A1 p3 V7 l' E Heatbug proto = (Heatbug) heatbugList.get (0);
/ M4 ^+ ]& w3 q- f+ v2 Q. a6 X Selector sel =
1 a% g/ V1 H" l5 O+ k6 { new Selector (proto.getClass (), "heatbugStep", false);
7 n/ S0 k/ ?2 _ actionForEach =
- G5 T( K M6 _7 C2 w0 K& e6 Q modelActions.createFActionForEachHomogeneous$call
2 @5 H" [' g1 w (heatbugList,
+ L0 g5 x* t4 O4 Y' [6 @ new FCallImpl (this, proto, sel,) k( u; e N# U6 ]
new FArgumentsImpl (this, sel)));
( [: A' R5 Z+ n8 m } catch (Exception e) {
7 w# Q9 n, }; Q4 Z: w e.printStackTrace (System.err);2 v/ q5 ? c% {9 S: D- u! E+ b
}+ L9 w z" q. w# h
, h6 f6 P, R6 x2 O% D+ w2 |8 R syncUpdateOrder ();
) W# \/ n; y* H! [. V6 X" e- w1 l, d
try {* o2 u( N+ r6 U; \' E& o- s0 d1 \
modelActions.createActionTo$message / {/ r2 C5 K6 c0 O5 [
(heat, new Selector (heat.getClass (), "updateLattice", false));
- L) x# ~8 G4 F( x } catch (Exception e) {1 Y3 T! m! t3 R# ]5 A
System.err.println("Exception updateLattice: " + e.getMessage ());6 P: u7 T4 t& {8 w) Y
}7 f, o* Q, C' K: e# V9 M5 w2 Y2 J% ]
2 z( _) p: e2 R+ d7 R
// Then we create a schedule that executes the
7 x- V0 d* i p // modelActions. modelActions is an ActionGroup, by itself it- j8 J ?+ J9 n) O
// has no notion of time. In order to have it executed in
) @4 F7 M% o* z7 s# m& b // time, we create a Schedule that says to use the
" r6 p8 e5 Q4 h& m/ ~ // modelActions ActionGroup at particular times. This3 L& O3 G3 j+ b) Z
// schedule has a repeat interval of 1, it will loop every
0 F$ J u! |' g6 p5 F& K // time step. The action is executed at time 0 relative to
6 E( k1 b. w$ } // the beginning of the loop.* _) H$ }, X# Z9 H3 G) Z! B( |5 U
% F/ a) M, w- F+ A
// This is a simple schedule, with only one action that is! K, Q6 E) G7 x, M% b
// just repeated every time. See jmousetrap for more
2 q4 P6 t" a3 k$ H8 C9 ] // complicated schedules.
# w. _5 A9 p, C5 V
7 x/ k* X7 V1 W( I modelSchedule = new ScheduleImpl (getZone (), 1);+ Q9 N+ ?* g' [2 _& l
modelSchedule.at$createAction (0, modelActions);) M$ t0 h! [$ ?+ {
% W% h( [2 n- i+ ~$ ]
return this;
E: f3 [7 c3 H4 T } |