HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 \) P2 n+ s" Y: V0 N5 B9 Q o
8 S3 ~9 D6 X0 V4 d% } public Object buildActions () {' @5 g- M9 H3 j
super.buildActions();& _, c6 `1 }2 Z# S5 C$ j4 q8 \
- ~( k; u T" T% p2 ~) X // Create the list of simulation actions. We put these in
# i- M* V+ ^7 b. k$ W- Q // an action group, because we want these actions to be
$ y; z3 k% E. T6 x0 }, Z; | // executed in a specific order, but these steps should
. }+ }4 U9 Z% X# ?2 I( H! x // take no (simulated) time. The M(foo) means "The message" O2 T' | t9 U+ ]! b
// called <foo>". You can send a message To a particular
: ?* j" k! R. h- B // object, or ForEach object in a collection.* x5 _% e; R8 @; J
' S! S4 [2 A+ u# l1 p, h; w // Note we update the heatspace in two phases: first run
' {& q& U1 `, Z% ^9 _( k6 R // diffusion, then run "updateWorld" to actually enact the
1 j0 X; H- Z$ ^ // changes the heatbugs have made. The ordering here is; z2 l+ O" j5 ~6 b: C R7 C
// significant!1 k, e( r) U' ]; z$ D5 B$ U, U
3 f- @5 {, o c- F
// Note also, that with the additional
. C3 P* P J5 ~- W$ @ // `randomizeHeatbugUpdateOrder' Boolean flag we can
0 {( u+ W7 o5 ~) l9 u // randomize the order in which the bugs actually run
* b' I; d( }, m$ [$ L // their step rule. This has the effect of removing any7 t1 H( E G3 i3 A1 m/ D, }
// systematic bias in the iteration throught the heatbug
& E2 e+ ~5 e. D! ] // list from timestep to timestep- R+ t# h/ b% V) I* A
Y- X# S# \9 O8 C2 J
// By default, all `createActionForEach' modelActions have/ r! W7 s. c: O z; {9 q
// a default order of `Sequential', which means that the8 c0 v: J" ~$ t+ T' B6 Y
// order of iteration through the `heatbugList' will be) P$ b( B" H! _+ x. s; E
// identical (assuming the list order is not changed
+ J: u, ]# u+ k \ I- v // indirectly by some other process)./ n) d6 \, h3 F2 H: M6 z
1 Q9 Q$ b- q! I6 Z1 H2 y7 r modelActions = new ActionGroupImpl (getZone ());$ n$ n) f5 J% Y& H$ d( P) M5 n
3 ?* X, f# ? k' M( Q1 e
try {
$ p. K& F9 ?" F7 l( i$ f6 V modelActions.createActionTo$message
" N6 ^( W; _4 @( r (heat, new Selector (heat.getClass (), "stepRule", false));; ^% ]; z1 Z" E9 J8 h- z
} catch (Exception e) {
7 ? v- e. s& k* x System.err.println ("Exception stepRule: " + e.getMessage ());& U( L" ^! b+ W/ g
}
% i `8 L' ?- l3 I0 a6 p, ]6 j8 j
; p- L: J0 W' c+ K try {
- E! m4 ~; @- f1 b, [$ x7 G Heatbug proto = (Heatbug) heatbugList.get (0);
1 A& u7 L4 R4 c7 T; p" R0 Q0 `/ q& c6 r Selector sel =
$ B% q5 r' A ?1 D- ^5 O# [6 ? new Selector (proto.getClass (), "heatbugStep", false);: B, w% B2 B- V: P% N
actionForEach =
4 p! q, K: |4 u* a: S5 ?; j( x modelActions.createFActionForEachHomogeneous$call
* M, e; o7 G% q (heatbugList,1 R6 a! w7 l$ b! r! I, |3 m+ G
new FCallImpl (this, proto, sel,
3 u' E. ^3 ]# n0 R new FArgumentsImpl (this, sel)));: w: I5 G: w6 d8 U; R2 d1 J2 |: J
} catch (Exception e) {
2 l' G* L) d- ~; M( M6 R3 o2 P e.printStackTrace (System.err);
4 E o% _4 E3 K8 D3 _ }, T" G. u$ H; D7 S5 J) _0 I
4 x! s/ T) I, s
syncUpdateOrder ();: ~+ h$ m5 `0 f( ~
6 P8 u x$ y, V' Q/ X% K' O try {
1 t: R! O; q q8 B! d modelActions.createActionTo$message
7 H2 u' v4 h$ X& l( I (heat, new Selector (heat.getClass (), "updateLattice", false));
% W5 \6 E1 Y J1 E' |) A7 h } catch (Exception e) {
0 Z8 \+ v' ?! H/ |! R6 z System.err.println("Exception updateLattice: " + e.getMessage ());+ z4 I8 A- R" o
}1 ]9 C& h7 v% S0 Y( m6 F
& L7 K# c3 ^9 u+ f2 u- F // Then we create a schedule that executes the
2 ~: }5 s) P" }0 t& Q // modelActions. modelActions is an ActionGroup, by itself it
8 ^+ W3 N1 P( z5 o- F7 B ~ // has no notion of time. In order to have it executed in. L( W+ [: h8 t" F
// time, we create a Schedule that says to use the
! w( V3 q! D+ H- k! C. x // modelActions ActionGroup at particular times. This4 W6 C8 c7 Y8 R
// schedule has a repeat interval of 1, it will loop every
. \9 ?$ ^) e2 @- g1 b0 w& S9 [ // time step. The action is executed at time 0 relative to
9 k8 j4 u: T* q4 y/ D4 g // the beginning of the loop.
' r% }, {, y6 k" z, Q( Q6 C. u! m* U9 e9 t6 z
// This is a simple schedule, with only one action that is3 F* F8 m2 @* ~0 Q5 u3 v1 c( _- p
// just repeated every time. See jmousetrap for more; s* P- I, ^& ^/ W* ~; s$ ^
// complicated schedules.
+ z& c/ {4 E0 z9 \
8 ~9 ?& b; t' z z( z, a modelSchedule = new ScheduleImpl (getZone (), 1);
& {& a8 ]" J" ^5 a modelSchedule.at$createAction (0, modelActions);
- z/ V! V& N* F V) a3 ^, F @" b
$ D" X: I# a) [ y7 h7 Q( v return this;! z3 f; s' t x8 W: Y1 A
} |