HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
|0 k5 t) k. _1 `% \) t7 W; i- d7 y# }5 M* i5 F8 H9 y: f
public Object buildActions () {
# i( Z9 Z& n8 f& y- v super.buildActions();
1 b/ }7 `! W0 i. [- D, e
, k! _; ]/ }' h' \3 }8 |) { // Create the list of simulation actions. We put these in1 Z/ S) ^. i, j! A* k' E0 @$ S
// an action group, because we want these actions to be9 i$ _) i+ c2 Y3 F* X! g7 h2 `$ n
// executed in a specific order, but these steps should; d6 E9 K6 x. R/ _) e3 o
// take no (simulated) time. The M(foo) means "The message' t1 A5 o2 w7 p! V+ C0 v$ \
// called <foo>". You can send a message To a particular
r$ L8 l" K1 F! k, V // object, or ForEach object in a collection.
/ Y$ U* z% k( [; E
) ]+ |9 s5 m; F4 ]+ I // Note we update the heatspace in two phases: first run
! n# _7 |) r+ F6 N( \! x$ ~ // diffusion, then run "updateWorld" to actually enact the
2 k) E: B* R+ B! R+ O // changes the heatbugs have made. The ordering here is, G$ d1 F: C2 R& Q5 N
// significant!
; Q# ?7 X: I { f+ h
1 s6 V; `- j0 B( O# I- H // Note also, that with the additional
- W n: L$ m* W5 e" v // `randomizeHeatbugUpdateOrder' Boolean flag we can
+ x' |2 Z; v+ S. t. I // randomize the order in which the bugs actually run
' x% R% }2 i9 z! N6 L2 L3 { // their step rule. This has the effect of removing any
9 B$ s8 p5 _3 n, ]* Z5 t // systematic bias in the iteration throught the heatbug
, S; Z* y# ]1 R5 Z1 @) e6 D. _ // list from timestep to timestep
: s/ ^7 w- y: ]5 s9 z 1 i+ w6 f+ I' I6 q9 c h7 X' R
// By default, all `createActionForEach' modelActions have
& V3 @: g+ G. ` // a default order of `Sequential', which means that the. ^6 n8 {( S- _* r4 F7 O
// order of iteration through the `heatbugList' will be
1 x/ n7 m! _0 W3 @) _7 o // identical (assuming the list order is not changed
* i4 Z( F9 c/ k* C1 y: E G8 s // indirectly by some other process).
) A& Q9 V, t8 Q9 @# k
* ]) B; p9 N( e$ p/ F2 s modelActions = new ActionGroupImpl (getZone ());
$ T: Z U2 v# o3 G. @3 \) U/ |* L! a8 _ h; ?% G" R" E
try {
" o# r8 b% X1 g1 x3 {: ?; l modelActions.createActionTo$message# q8 h2 @1 O8 J @
(heat, new Selector (heat.getClass (), "stepRule", false));
) j: `& L' [+ V# F8 a( N } catch (Exception e) {+ S! e% U+ ~$ f: M k( G; x6 _
System.err.println ("Exception stepRule: " + e.getMessage ());
, ?) m. z) s" ~0 K! h5 {7 k }
8 a% z; H" l6 `5 v$ _/ i& |+ v5 x/ p# E$ M" g! `4 T
try {8 J+ E2 p. n; @8 B& }$ [- Z
Heatbug proto = (Heatbug) heatbugList.get (0);
! s1 q6 b. u9 `. v( F. `- i# e Selector sel =
, u2 J) U6 l* s1 o1 i% Y V new Selector (proto.getClass (), "heatbugStep", false);# ]7 ^( T! [- m( k0 _
actionForEach =
, O @! {; b* {7 |( [ modelActions.createFActionForEachHomogeneous$call' B: O& y1 b- @( f
(heatbugList,
0 }5 g0 y' f( f* x8 v new FCallImpl (this, proto, sel,( r6 b* m3 o: S: o
new FArgumentsImpl (this, sel)));
4 s, j4 l7 ?1 J* D" s' ? } catch (Exception e) {
* u$ Z# m% `' p" { e.printStackTrace (System.err);% @1 ` x1 L6 E @8 ]; M' K) O
}
R9 n F- p# t
3 {9 y& D( v( I# B; ~1 I syncUpdateOrder (); A5 \6 o/ ~+ ~
( P4 J2 W2 p+ p8 m7 ^
try {7 w1 i n' q `# r1 o
modelActions.createActionTo$message + T% S+ ~; P0 H/ \- s
(heat, new Selector (heat.getClass (), "updateLattice", false));
/ Q" i N" [2 {1 s1 _# t } catch (Exception e) {- x+ O6 I2 Z9 t, ~
System.err.println("Exception updateLattice: " + e.getMessage ());6 s2 h$ G$ Z$ x
}
: [$ A( T) Y1 r3 e0 `# o
! \, S4 ~- G# c ^ // Then we create a schedule that executes the* u# F3 J: ^0 i+ i
// modelActions. modelActions is an ActionGroup, by itself it* X: g8 W- Q+ n
// has no notion of time. In order to have it executed in
9 @' `5 L) a6 h& Z- o // time, we create a Schedule that says to use the$ g" [: h' \" Y1 T. b1 [
// modelActions ActionGroup at particular times. This. I$ o. D: [& h/ r; ?4 Y
// schedule has a repeat interval of 1, it will loop every0 _% L3 ]" ]: |" I
// time step. The action is executed at time 0 relative to2 k8 j' b: J) P" w+ {
// the beginning of the loop.
6 b- ~+ c# y5 d: f6 F, h* R0 t8 T" |* A0 b
// This is a simple schedule, with only one action that is- `8 n" L. |& N a+ i; k" G
// just repeated every time. See jmousetrap for more' U# L. n% E) L j: {/ Q
// complicated schedules.8 a& Q4 x* t* X' t6 H
7 C% v- s+ V% X! ~* N; W modelSchedule = new ScheduleImpl (getZone (), 1);
" v( r1 b! X- L" E( N modelSchedule.at$createAction (0, modelActions);
3 F- T9 A4 @1 F2 d" r; ?- I7 \% [# h : S" V! s* V0 K0 ]. U1 N! z7 {% Z/ {. {
return this;
+ R/ S- q8 V' f7 e } |