HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
F& {0 |" w; A% N
( S! |; w7 h) p a% |# T public Object buildActions () {
' T' K- W& i" X. Q. ` super.buildActions();
3 m+ ~& ~' r5 b6 T& c2 M
) c% B5 V8 y8 I4 { // Create the list of simulation actions. We put these in
6 @; T/ e" T: t7 s2 z0 h5 a // an action group, because we want these actions to be
& N2 M. c$ h8 D" E1 d // executed in a specific order, but these steps should2 @3 b7 z9 \; u0 q; a- ?
// take no (simulated) time. The M(foo) means "The message" w8 @ v; m M& S8 J
// called <foo>". You can send a message To a particular, i/ J a5 c' K
// object, or ForEach object in a collection.
& d" Q$ O: L% z& U1 u
7 Q! M( i, U1 ]* Z6 `0 _3 T // Note we update the heatspace in two phases: first run
3 d- q* @4 U P- H7 V // diffusion, then run "updateWorld" to actually enact the
) T8 p8 P' V/ A# W: J6 y // changes the heatbugs have made. The ordering here is5 m5 O" f! `2 y- W& k( c5 W
// significant!
+ L& C) r& u1 `- y 3 t# h% e, p' G9 ^. M, G( o, h
// Note also, that with the additional% c- j5 v: J- }2 I
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ ^+ b/ L/ ^6 U3 D# \5 i8 _ // randomize the order in which the bugs actually run
' r' U4 Y& v8 ~ N+ Z // their step rule. This has the effect of removing any
M- z7 K- _* \% g5 Q/ _ // systematic bias in the iteration throught the heatbug
: `( o$ r+ E7 [' m7 e: e. t0 n // list from timestep to timestep
- o) m# z( n9 k
6 d6 _% N8 ^( j' Q // By default, all `createActionForEach' modelActions have# v% ~4 U0 E( d* m; L; x% U
// a default order of `Sequential', which means that the
8 t2 G8 X/ d. d$ u% i+ Z" h- ?- L( R1 m // order of iteration through the `heatbugList' will be* C* `! _9 H4 l
// identical (assuming the list order is not changed8 U. w* T% ~: s. m' N3 E: Z. x5 X
// indirectly by some other process).
0 G" g6 F7 w. k4 A& o Y
" t2 _) l& k k/ Q, [ modelActions = new ActionGroupImpl (getZone ());! E* d4 g4 k8 ?1 [ M
6 S* { q# a$ k& n0 f3 J( t* o8 } try {
7 Z0 y7 @8 W/ [# L( I modelActions.createActionTo$message$ d0 ]0 ?, h# G& ]
(heat, new Selector (heat.getClass (), "stepRule", false));
# x8 v5 y# x, S* t$ k! k } catch (Exception e) {* V' I3 ?: j: ]3 U. f% ~0 V4 R0 i
System.err.println ("Exception stepRule: " + e.getMessage ());- i* ?8 o) r" n7 K+ t
}
- P& H C3 C" v5 T( n9 [
! S# w3 G9 q8 X% Q0 R try {( e& c3 v. ^. t: D/ v k
Heatbug proto = (Heatbug) heatbugList.get (0);& s- y$ o9 H- H0 \
Selector sel =
P; _( h7 w, u& Z8 G3 j8 q new Selector (proto.getClass (), "heatbugStep", false);
, n. o) I# E, q2 f; K. ]/ ~ actionForEach =
, n, ~! m9 \5 m3 v; M2 G modelActions.createFActionForEachHomogeneous$call3 ]0 U% l' B, u2 Y6 k; _
(heatbugList,
$ D" D7 [4 f% u! a" W& Z new FCallImpl (this, proto, sel,
- M! L A0 k5 C new FArgumentsImpl (this, sel)));4 s$ B" I* }/ H. [
} catch (Exception e) {0 G. X: }7 H( h+ c5 C7 z
e.printStackTrace (System.err);
. N9 E+ v' ?1 \) V }
( e! `! L( l% \) }) b
" k+ A2 D+ h& p8 L, @1 e* C syncUpdateOrder ();
: a8 Z$ ]0 K* Z I( a7 T" A W4 V2 I' ]
try {) e. p$ ?4 y: B1 M4 K" S; f
modelActions.createActionTo$message : `7 \2 o2 } G* }5 T5 \
(heat, new Selector (heat.getClass (), "updateLattice", false));# o6 e0 Y, d1 u$ z5 X
} catch (Exception e) {$ I6 B/ A* q( {7 A; d1 m
System.err.println("Exception updateLattice: " + e.getMessage ());) I0 R5 m/ a0 {( o+ ?3 s. p8 O" S R
}8 z# I W2 S- ?; ~
/ ~0 U# b5 [6 Q4 Z9 c! b& y8 P
// Then we create a schedule that executes the6 }, Q8 p3 L; \; j: X3 p
// modelActions. modelActions is an ActionGroup, by itself it+ M. f5 C. Q# ]) h P2 [ v
// has no notion of time. In order to have it executed in3 n9 z% H; Q$ Y8 a
// time, we create a Schedule that says to use the
9 H; x' ]* k4 _ // modelActions ActionGroup at particular times. This8 t& }0 v% Z; S! t. [2 j! U4 O" V% k
// schedule has a repeat interval of 1, it will loop every
8 b( n' {7 u- L0 t$ M! o7 o$ W4 k // time step. The action is executed at time 0 relative to
4 T4 I3 g8 |- s& Y7 o8 l6 L" R // the beginning of the loop.
" j+ U% B. H3 ?. `1 A3 J( F3 f5 B0 z/ t# p, x- W5 R! P/ [
// This is a simple schedule, with only one action that is5 U1 k$ F4 t" n4 r
// just repeated every time. See jmousetrap for more2 }1 m% o8 k6 k; | [3 T1 J5 d% p. `
// complicated schedules., g1 B0 R# \# `8 C% t1 G" _! Y$ U }
: ?) Q4 W% D8 _6 O L1 S modelSchedule = new ScheduleImpl (getZone (), 1);
: e6 \! Y2 D2 L7 b( q5 X modelSchedule.at$createAction (0, modelActions);! }8 ^' D, ]8 `2 U% k3 C5 l3 d
. T9 Z. _" i8 |+ Y- ~ return this;( g$ V9 ?% c" G/ T) z3 H0 ~( X+ Y
} |