HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ [4 P) g; b" q
5 p& {( P- i7 k4 [& Y+ h public Object buildActions () {
3 s4 t/ {9 o1 R8 S3 [ super.buildActions();
! R. _9 f& I3 ~+ K, `) E . z5 |3 B1 P+ X4 D* ]
// Create the list of simulation actions. We put these in4 o, ] f! G2 w+ b3 S
// an action group, because we want these actions to be: s( b+ [- g) q' M+ N; i
// executed in a specific order, but these steps should
" Y3 _6 |; d# X% g! p, a I // take no (simulated) time. The M(foo) means "The message, t0 A$ |1 V- N3 z* f7 w- a/ B0 p
// called <foo>". You can send a message To a particular( v" q; v4 e2 z* E( F, }" ?1 J
// object, or ForEach object in a collection.
, X! V: [1 c e: \% D# W) C
$ c. @$ ^0 O9 b4 X* s // Note we update the heatspace in two phases: first run
6 N2 P m! q1 X _: r# L. I // diffusion, then run "updateWorld" to actually enact the
- ~/ V# i ?* `! u- t+ d; L4 I // changes the heatbugs have made. The ordering here is! K w: R; D1 H( ~) R* b) r1 T
// significant!- ?0 c0 R5 i! `
& w+ {$ f9 o6 m9 h
// Note also, that with the additional
& t( [+ { d; C5 y/ N0 z // `randomizeHeatbugUpdateOrder' Boolean flag we can0 I4 C2 E) D& { A" C
// randomize the order in which the bugs actually run
5 A# t# g$ G) O+ u+ x // their step rule. This has the effect of removing any3 a5 l+ k& W$ z7 s& ~
// systematic bias in the iteration throught the heatbug# M" u9 r; v% H! L7 `7 H4 P
// list from timestep to timestep8 R# v7 C: w x- z
% u* c" p6 g) @# h8 J2 o9 G // By default, all `createActionForEach' modelActions have8 y& Z7 r) V- w: Z/ z' ^
// a default order of `Sequential', which means that the" T* k4 m8 s1 x& j1 R* S3 j
// order of iteration through the `heatbugList' will be9 t3 r$ X4 |# Y' i
// identical (assuming the list order is not changed
1 f8 m2 B/ y7 U. A/ c: m: L // indirectly by some other process).$ {- ? `' G; Q7 m9 }% ?+ H
: F6 ^4 k4 G5 J. G/ k4 N2 l modelActions = new ActionGroupImpl (getZone ());* Z/ F$ ?6 w/ ]1 H/ W6 _
" i; y2 I& W N { try {, @; z* f$ M; @
modelActions.createActionTo$message6 M9 B$ ]8 n$ [ F* x+ a
(heat, new Selector (heat.getClass (), "stepRule", false));
/ [, a, s( d; U+ s1 Y9 C( M2 l } catch (Exception e) {# w& o! _8 p/ i; m5 I6 Z: A
System.err.println ("Exception stepRule: " + e.getMessage ());9 u4 h" {8 K% K# ~$ U
}4 E2 F: m* o8 p- o7 f+ {
5 a) H' [7 T) @: D try {
' y! x9 m9 [. I K& r' c/ \ Heatbug proto = (Heatbug) heatbugList.get (0);! t/ k4 H" w5 q% ^( V9 z
Selector sel =
4 F0 T5 {5 j, `/ b) U new Selector (proto.getClass (), "heatbugStep", false);7 m9 i8 P* U% _. c* p' y% |
actionForEach =
6 R' |3 i3 f/ u; i# C modelActions.createFActionForEachHomogeneous$call3 V l3 s" z5 z n" | U
(heatbugList,/ N( A1 ]# e- D
new FCallImpl (this, proto, sel,
/ L4 J, Y" z3 _ new FArgumentsImpl (this, sel)));5 y. Q( j9 i9 D" E/ ~0 a
} catch (Exception e) {) x K4 v- _! @. J/ v- d
e.printStackTrace (System.err);
4 e! o# ^+ G- Y: n+ G& i! g" ~* o& [& L }- }4 ^8 B+ [" k: u- R/ {; W( F
. t# c7 b0 }( A8 @5 i
syncUpdateOrder ();0 T7 J0 t. M2 u1 \& \3 ?; X+ e
2 z" i/ U- z% K* w. Q, t+ x
try {3 V0 d6 D: P9 y% c* X; o A5 _
modelActions.createActionTo$message - m8 S: q# e" r) o& c7 B
(heat, new Selector (heat.getClass (), "updateLattice", false));
K5 d) ]4 ?% V, b( |3 s } catch (Exception e) {% y, M% z$ U5 f z: v
System.err.println("Exception updateLattice: " + e.getMessage ());
* ~$ q% A2 j& t, u! Z3 f }) B0 S; f9 T J8 _
! k6 [; P, `+ m0 u* B
// Then we create a schedule that executes the- Q5 w3 X( `) L; Y, n1 J
// modelActions. modelActions is an ActionGroup, by itself it) \( U$ O6 }! G0 H5 D
// has no notion of time. In order to have it executed in: p; w y g0 l" b7 U f6 ?5 T W5 u
// time, we create a Schedule that says to use the" e+ O) J$ f; s& v2 `
// modelActions ActionGroup at particular times. This
1 P, F) B" ]9 G // schedule has a repeat interval of 1, it will loop every
' B1 [1 \* y0 O N% _9 S4 W // time step. The action is executed at time 0 relative to3 R6 r+ u4 B" c. F+ H& s' l5 k, F
// the beginning of the loop.
# B/ j) k* V. `, p2 l0 k
8 T0 ^2 E1 s0 Z0 d // This is a simple schedule, with only one action that is- H3 i1 s* h+ b
// just repeated every time. See jmousetrap for more
+ ^# q# j0 y- O' u% k // complicated schedules.% O; i% d; u& `, O/ n# g( s- G
/ d I8 w( |- u, T0 r
modelSchedule = new ScheduleImpl (getZone (), 1);& X# l0 g) }, N
modelSchedule.at$createAction (0, modelActions);& n S5 C! }: G. y- n
4 f! z" M6 x, `. l3 N
return this;( P5 a& d7 c( ]0 p4 e
} |