HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ l9 O2 P) }6 ~6 Q. T$ ?$ _
0 N- X, N, r8 o public Object buildActions () {
+ K- [7 e. J& R super.buildActions();) S* Q( m9 P8 R6 C) p
4 `* H9 Y1 M I1 y, }# e8 R // Create the list of simulation actions. We put these in- ?2 t a4 T5 ~9 r
// an action group, because we want these actions to be# S9 W+ @* z' Z% |5 ?
// executed in a specific order, but these steps should
8 ~& J4 q1 X8 U) y2 }9 j // take no (simulated) time. The M(foo) means "The message
, I/ h. y, A) Z$ _1 J3 U // called <foo>". You can send a message To a particular
3 A/ I$ a& C' X) y5 i- Y. u // object, or ForEach object in a collection.
1 q% x. O; c$ J! `# Q/ B
7 {6 }1 M4 v) I6 f; c2 F // Note we update the heatspace in two phases: first run
9 y3 u1 d. _2 R // diffusion, then run "updateWorld" to actually enact the
* o. q5 l) g) U // changes the heatbugs have made. The ordering here is6 a0 r2 @- \! ~( D
// significant!
/ w1 }2 Y- p2 u6 Q* ^3 y 5 B `6 Q. s7 x: H4 j' B6 {& x
// Note also, that with the additional/ P! n* |- B" ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 p3 {4 }/ F0 a7 }4 ]
// randomize the order in which the bugs actually run
M' `% \* z# o: l- A // their step rule. This has the effect of removing any
8 Q8 W# ^7 S8 E& o! B a/ F: p // systematic bias in the iteration throught the heatbug, p5 c3 Z+ ^3 T5 V; t
// list from timestep to timestep
0 J* T9 u! w7 ^1 u
* k: t& h% F+ _, @ Z // By default, all `createActionForEach' modelActions have8 I1 O, x& P" B
// a default order of `Sequential', which means that the: U( x: h4 j# {* R, w9 C
// order of iteration through the `heatbugList' will be
`; |& W0 {5 R3 l$ V6 ? // identical (assuming the list order is not changed
9 v; ]4 n: P9 V3 q // indirectly by some other process).
r" Y4 [. x, W) h d- l( y% o! `& h
modelActions = new ActionGroupImpl (getZone ());
1 S) D3 B" u2 f. N4 B2 {/ K6 f1 w- M7 R. W
try {
5 _) p3 Z2 w9 z9 _* ]% T modelActions.createActionTo$message
' G; c% ~- l. R. S (heat, new Selector (heat.getClass (), "stepRule", false));
4 G- _6 j" \5 _: U7 m9 |, n$ _ } catch (Exception e) {
* u# T/ d, q# B2 I& l0 A! S. J7 M System.err.println ("Exception stepRule: " + e.getMessage ());- O+ A) R" K% |" @1 O
}6 M' C0 O/ v0 U5 |9 D
& w/ B' b( j1 b& j6 V
try {/ C& ^ j, {* p A/ e
Heatbug proto = (Heatbug) heatbugList.get (0);
! `6 q/ G# b7 S7 z T; X# }. I Selector sel = . S+ S5 \+ B& W) {1 }5 }& H' E
new Selector (proto.getClass (), "heatbugStep", false);
7 t7 ?) {6 n; F: V7 y% S# { o actionForEach =. x7 _8 A% O* j
modelActions.createFActionForEachHomogeneous$call; y* ^4 r8 [# D4 L% l4 U
(heatbugList,
$ m, q' W, p3 J# s. D8 |- I: j new FCallImpl (this, proto, sel,& r7 A' d' d1 H1 \6 i }
new FArgumentsImpl (this, sel)));
8 X0 I( `1 N- X/ L+ v" r& P } catch (Exception e) {
! i1 ^% s' `9 N" J8 P: g" D8 R V e.printStackTrace (System.err);9 U) i- i) R, |7 V2 E( V% N
}' s/ t6 A! g$ E/ k/ ]; v6 G
+ {' @3 p; J, ?, V" L: \" ] syncUpdateOrder ();$ o# K5 S: J- [: c! q3 f
) ^2 y) ~- V! L7 u. _0 \
try {+ X# |; L/ W, `0 R8 ^" m
modelActions.createActionTo$message 1 n1 C$ J8 }. e6 r7 |( _5 e0 B
(heat, new Selector (heat.getClass (), "updateLattice", false));4 M# ^$ g" [( @, g
} catch (Exception e) {
/ P8 p0 _) M* p System.err.println("Exception updateLattice: " + e.getMessage ());' A* C) w' u" f' d
}1 M( v6 J' S8 l _: A/ W% @
A4 z3 M2 q7 x i4 L- _ i4 l // Then we create a schedule that executes the0 ~3 K. D4 S4 o# C1 Z/ ]; C6 U
// modelActions. modelActions is an ActionGroup, by itself it6 W4 B: d @, g7 E# ?8 Z2 U
// has no notion of time. In order to have it executed in& P7 G; j" v7 F& N) G& T
// time, we create a Schedule that says to use the
2 _* E |' y( u! ^" I8 f // modelActions ActionGroup at particular times. This4 F8 _& m& b- _1 ?
// schedule has a repeat interval of 1, it will loop every9 ?, R" B s: z
// time step. The action is executed at time 0 relative to
6 f7 \ }# o6 p! |5 ?. g // the beginning of the loop.
( y- N* ~. x$ w4 {4 h4 r7 T
1 p1 c+ `0 k) ^ // This is a simple schedule, with only one action that is2 r9 S* v; x) r" A( `
// just repeated every time. See jmousetrap for more- a) P8 _& f% @3 g
// complicated schedules.
! N3 M) w( Z; U: G5 V( D " F3 X* v n2 ^9 J: R5 n
modelSchedule = new ScheduleImpl (getZone (), 1);
/ u" F0 K' M3 c: t+ c; o modelSchedule.at$createAction (0, modelActions);
8 K! \, x- L+ c k. [! X- R9 y2 x 1 ~8 i4 I I2 P- j5 A7 X6 U0 _% {
return this;9 r4 T) v$ Z* C8 J, `; R% P7 H# s
} |