HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; I1 J: r9 ]! Q/ V! w6 @3 B% c: y- \( q1 C6 L
public Object buildActions () {
$ K% g0 m; f: Y, d super.buildActions();6 G1 `9 L9 Q+ b( Z& t
4 }. n( d# V- o0 _/ g$ s7 `/ D // Create the list of simulation actions. We put these in
. M4 h. v3 a, m+ p // an action group, because we want these actions to be
+ P3 o( m" [' P6 _# c7 f, b // executed in a specific order, but these steps should
4 R& s; j# P* u. U. v2 M4 n // take no (simulated) time. The M(foo) means "The message' t E7 a- Z: a# Y" M
// called <foo>". You can send a message To a particular
! Q' h' c" w: q3 W* ` // object, or ForEach object in a collection.; i# o, ^9 ` D4 v6 X
4 N2 g/ }5 ?$ F# ^ // Note we update the heatspace in two phases: first run I' W% |/ P' _: ]/ Q# g6 [
// diffusion, then run "updateWorld" to actually enact the3 t G2 M, e( G8 I3 h, d9 G
// changes the heatbugs have made. The ordering here is
* U P, C# p/ P( y9 Q // significant!
3 |9 K5 @0 B6 C. [
2 C8 [0 n8 M0 O7 P // Note also, that with the additional
! x3 M8 P4 p4 _# r% D" I // `randomizeHeatbugUpdateOrder' Boolean flag we can
: n* r t! N5 u4 @1 t // randomize the order in which the bugs actually run
0 [. R8 ^- Q( d: S8 Y) r! Y" V6 J // their step rule. This has the effect of removing any: y7 h$ s) v0 ]6 ^- B/ s
// systematic bias in the iteration throught the heatbug
4 H% {1 g0 W( u4 _1 r6 G6 R // list from timestep to timestep2 G0 [+ A* z- D5 [/ ~
3 `) D- F3 s9 w# t. p/ Y
// By default, all `createActionForEach' modelActions have
& _4 M+ B- P0 m* y x // a default order of `Sequential', which means that the7 b4 B/ ^6 K( L2 R _$ ^8 ~9 ~' |
// order of iteration through the `heatbugList' will be
1 {- q% n8 D" P9 k E, i1 W9 p // identical (assuming the list order is not changed
* V5 f; C, u3 ` a // indirectly by some other process).- R, v, E" F* p8 K+ h
; e) O. F- A+ N0 C9 M
modelActions = new ActionGroupImpl (getZone ());$ Q3 s" n9 x& f
( `! J, ~& i) d9 E- n$ G# ]/ X/ h3 z
try {
5 t8 W \4 E) y, K& Y5 t" w modelActions.createActionTo$message
! j" T ^3 b" ]0 X) f/ f (heat, new Selector (heat.getClass (), "stepRule", false));
4 E; o. X/ }" m: A/ Y% F } catch (Exception e) {7 ^) G+ ?6 t/ r! {, L6 p2 z: A1 ~
System.err.println ("Exception stepRule: " + e.getMessage ());
# ?+ U6 u9 s! J' ] }
8 K0 M/ i. u1 p1 D$ H" n% v- x1 L
try {
$ q& M6 k) c1 p$ ~ Heatbug proto = (Heatbug) heatbugList.get (0);4 [. `: t2 ^' ]7 l$ ?
Selector sel =
1 x- F, f) Z+ U4 c new Selector (proto.getClass (), "heatbugStep", false);
1 h# l1 E5 s( V" l" `( r actionForEach =
9 q) h) m* c' F modelActions.createFActionForEachHomogeneous$call5 T1 n/ |$ i: I" [) O
(heatbugList,- w) Z0 }; L' D; A/ S6 f
new FCallImpl (this, proto, sel,
5 @ G5 e) _+ h, K6 J* [+ o9 j new FArgumentsImpl (this, sel)));7 B/ m4 k; a2 ^6 B2 H
} catch (Exception e) {
0 y( {) c2 n( k1 P e.printStackTrace (System.err);8 Y5 T, s8 V" V& ?8 G6 w
}
& a! _6 g" y" [/ l/ E: V
\9 y3 K. e' Q; ]; ^/ J syncUpdateOrder ();
3 `8 x ` o& c$ U, V% m* ?* |) q' j
: g9 W2 ]' U& ?* a3 u: q try {; f t: x( d+ {* w$ _8 i
modelActions.createActionTo$message
' o' Z& f! i" I$ y' i' q (heat, new Selector (heat.getClass (), "updateLattice", false));
7 o: B/ k* M$ D+ C6 g9 ~( b" Z } catch (Exception e) {4 u4 P/ A3 v, S/ }; h
System.err.println("Exception updateLattice: " + e.getMessage ());
1 g. ~3 Q3 y) G |/ U5 e% Y8 A o }8 n. w+ D5 I/ U: p5 U9 R
J4 M( t# y' j; O
// Then we create a schedule that executes the
* M& t- Z" T5 U8 L // modelActions. modelActions is an ActionGroup, by itself it5 o+ z0 m8 ^( O* I6 a$ Q
// has no notion of time. In order to have it executed in! J0 A2 X9 v- r( A5 ^- m
// time, we create a Schedule that says to use the. r0 w! F& J; r) g
// modelActions ActionGroup at particular times. This! ?! n) h2 B$ {# o ^$ ]
// schedule has a repeat interval of 1, it will loop every0 J' \; F9 m0 q
// time step. The action is executed at time 0 relative to
$ {; g! _& B( N: x // the beginning of the loop., I7 A7 w- C( S, _" m- g
# V2 g8 y7 `* a* b
// This is a simple schedule, with only one action that is
- [9 f5 ? K+ E$ G // just repeated every time. See jmousetrap for more
( e; x$ A# q9 `: e9 t8 M // complicated schedules.
# d3 H0 I0 b- n$ Z' P. d @: Q$ U8 B. { 8 {7 q* e. \3 ~+ x' p3 m- b
modelSchedule = new ScheduleImpl (getZone (), 1);
2 P6 N5 @, [* |$ E$ ^) U modelSchedule.at$createAction (0, modelActions);% d& g" X/ p( I: r* T1 i# j5 P3 J
( G% T& x8 [6 [8 p# r. Z; Y return this;
) S0 K( s2 X( b: V( V+ W5 q } |