HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) C5 Q$ N% n; l6 P" Y* G
. s" M- n9 ?; }* v. F/ x1 z+ s public Object buildActions () {
8 g7 z( R0 M8 w$ A$ G7 G/ I super.buildActions();
5 ?6 y; v8 B% X! a2 ?
5 b( U3 T, y; ~9 J# H // Create the list of simulation actions. We put these in2 f8 Y, }& u% m+ ?* B% [0 |
// an action group, because we want these actions to be' ~: ?: a# a) s( {! x
// executed in a specific order, but these steps should
& Y& j! F. h x. k // take no (simulated) time. The M(foo) means "The message4 P }; ^3 ]! ^3 w
// called <foo>". You can send a message To a particular9 O0 e+ i7 a* J, t
// object, or ForEach object in a collection.3 a& j0 z! f+ A8 D
6 \% W6 N5 R. e) l: y# o% y' o // Note we update the heatspace in two phases: first run7 p, o, r3 J9 D, x7 O: M
// diffusion, then run "updateWorld" to actually enact the
, \8 {4 X& x3 k8 ^& y$ A" B& {6 f* C // changes the heatbugs have made. The ordering here is/ s: T! e& M2 w' i |& ~
// significant!
3 U: ~" L5 _" F2 \ / I* a/ G7 s1 \1 V: Q- E2 l4 u7 B1 T
// Note also, that with the additional
% u, c2 @ i) S4 e3 ?% K // `randomizeHeatbugUpdateOrder' Boolean flag we can0 l- l3 N+ T( P8 k
// randomize the order in which the bugs actually run
( u' s6 M7 P0 a // their step rule. This has the effect of removing any
! g( Y9 r: e0 v( f+ Z% y // systematic bias in the iteration throught the heatbug# ]- f1 V9 ^2 Y& C! S: j4 l
// list from timestep to timestep
( B6 a9 s0 i3 b5 |# V5 N 1 H/ |* \8 Q5 @" r
// By default, all `createActionForEach' modelActions have
1 h: Z' V* C- p% F( w1 B. j. j4 |* N3 X // a default order of `Sequential', which means that the
8 M! q9 o7 i8 P // order of iteration through the `heatbugList' will be
2 t- j; o* \6 f: f8 c% K$ M // identical (assuming the list order is not changed# Q+ u/ }' a" q. d
// indirectly by some other process).' t8 h* P& J: y/ i+ v% ^
) T) N) a- R/ @# {# j; U" y5 c modelActions = new ActionGroupImpl (getZone ());
# B* A* } j( d8 i8 P3 G2 ]: h4 P( Q3 R9 J/ H
try {8 b- O4 O" ^ d1 }" {1 m5 i! |
modelActions.createActionTo$message9 H$ _6 B; D; g9 v: W' d
(heat, new Selector (heat.getClass (), "stepRule", false));; _' u3 o! F4 f# v0 m+ J, m s, B/ k
} catch (Exception e) {
2 ^- e9 o2 {0 P( a0 O9 b* t8 t System.err.println ("Exception stepRule: " + e.getMessage ());
& Z# W3 ?* i1 J) @) C' G# _$ T }
6 n/ Z# h- x' x/ ~3 g' |0 l* J: O3 b5 L+ ]: J n! F) m. `- N5 w
try {/ Z. \1 c1 p. _1 \; N6 F
Heatbug proto = (Heatbug) heatbugList.get (0);
3 t' c2 Z& Z, d5 z3 J& R Selector sel = 2 m4 F# c* [' D9 B
new Selector (proto.getClass (), "heatbugStep", false);$ X7 q5 K# [7 T3 e
actionForEach =3 ]! Z) H1 a3 {# R! Z
modelActions.createFActionForEachHomogeneous$call/ p/ j' T3 }5 s' g
(heatbugList,
+ ?# {8 D- J; n% x; Y7 v% ? new FCallImpl (this, proto, sel,
1 g q# o7 C' U' G: w new FArgumentsImpl (this, sel)));5 A8 v5 N4 }0 `3 `' V p5 X3 I
} catch (Exception e) {: x" |/ j2 [& Q) r
e.printStackTrace (System.err);% Z, U0 c/ i" u! A
}
. \! B6 F: H9 u/ }* A
0 ?, w! a, T0 L0 q2 @2 t3 { syncUpdateOrder ();
4 l1 q% u- ^8 _/ w8 s
3 A2 J) L! |" P. I# V try {
$ o" n3 l6 [, O1 ^3 { modelActions.createActionTo$message 1 u S9 M, b+ D
(heat, new Selector (heat.getClass (), "updateLattice", false));
* w( F ]4 `# B+ ?& ? } catch (Exception e) {- s& t, M% {8 z1 E9 r, t: X$ ?' ~
System.err.println("Exception updateLattice: " + e.getMessage ());, @& E% E8 o, o3 R6 a
}2 _. \4 z, j# U4 m& p" |
% e9 n d. U! U* o, f3 L$ D
// Then we create a schedule that executes the {, t& n( O6 X4 S$ {
// modelActions. modelActions is an ActionGroup, by itself it0 k' V/ t" ?$ I" z
// has no notion of time. In order to have it executed in. h! ^6 [) B* g) n+ v' |
// time, we create a Schedule that says to use the
8 e0 p4 [1 `* V* D# ]6 v // modelActions ActionGroup at particular times. This
/ D9 V2 b: ?- q+ K // schedule has a repeat interval of 1, it will loop every( e2 p6 h' E' z: C' w
// time step. The action is executed at time 0 relative to
7 K3 b' s9 a, N1 @ // the beginning of the loop.; V t+ f; d& \% s9 U' O
1 d$ W' {0 J+ n8 A# _ // This is a simple schedule, with only one action that is
1 O. I2 w5 R, |0 t3 [ // just repeated every time. See jmousetrap for more
( l4 ]" Y) m) u // complicated schedules.
& v9 ]; {& f8 {% Q : i# j- l! I- t$ v) _
modelSchedule = new ScheduleImpl (getZone (), 1);
0 i$ t( n( h) u modelSchedule.at$createAction (0, modelActions);
3 J9 P" \- T8 \% Q1 I. }) t! D: v, | `! L. k& j J- C4 ?8 {
return this;
! Y8 K3 ^( S8 G& Q/ c/ L2 z, n6 ?# h8 N } |