HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. w, l* l* m1 o3 X+ q0 v" [
j( ?8 i0 B: J( y: m public Object buildActions () {% R: D8 O2 i) A3 k+ D
super.buildActions();) k9 C: ^: K! Q' [
# X6 I B4 g# G0 L& l7 k/ Z' e) }
// Create the list of simulation actions. We put these in
/ u: c2 ^' p, v; D+ m( |) @ W: x // an action group, because we want these actions to be/ x* M4 B8 L5 J
// executed in a specific order, but these steps should3 B$ S6 m0 X3 \$ _$ I
// take no (simulated) time. The M(foo) means "The message
% L9 G! J) ?% }/ x // called <foo>". You can send a message To a particular; O# r; |2 ~' X1 `! L$ S. e0 ]
// object, or ForEach object in a collection.; a P4 N* D' q+ l
- |8 U2 v# ]* q8 ^$ @1 \- L8 V // Note we update the heatspace in two phases: first run
( N& u9 i, D6 `& e8 M. L // diffusion, then run "updateWorld" to actually enact the) |: o6 o( h+ K5 L7 ^
// changes the heatbugs have made. The ordering here is
# y* j: E3 e7 Y* D" M1 z // significant!
& {; H% M" j5 X, `; l + s, ^, P' k* ]- K, N# o( e
// Note also, that with the additional, U% q+ a2 L" ?6 z5 E
// `randomizeHeatbugUpdateOrder' Boolean flag we can
9 T$ ]$ I/ ?% \4 A // randomize the order in which the bugs actually run
7 ~, P+ b4 a2 k" n( s7 d4 K& _- Q // their step rule. This has the effect of removing any
) U) `7 y' p# ?( O // systematic bias in the iteration throught the heatbug
! m! T2 {: b$ M; T6 o ? // list from timestep to timestep# j9 W1 @) g* M6 c
4 ]4 G3 o A; A. m H
// By default, all `createActionForEach' modelActions have8 l5 O' N! C& W1 V) l. n; j; [
// a default order of `Sequential', which means that the$ ?8 N9 c" L) d
// order of iteration through the `heatbugList' will be
0 N2 f; @+ A* Q7 r // identical (assuming the list order is not changed# Q r0 t6 W g% ?7 G; p, M
// indirectly by some other process).
* p# z( F: V X 7 _5 O8 ~% A# u- K
modelActions = new ActionGroupImpl (getZone ());
5 A- ^1 D/ ]$ t; y+ r6 ~; o, s- U" A. n0 e( D$ ^
try {2 m: @- y% Z2 x" r1 n5 s
modelActions.createActionTo$message0 k Q" f/ j0 y7 M) Q' J& t8 T
(heat, new Selector (heat.getClass (), "stepRule", false));# h1 F: C1 S, d- e' \, _% ?
} catch (Exception e) {$ X# v' P4 d2 K( S
System.err.println ("Exception stepRule: " + e.getMessage ());* M; d4 h+ V: w& O+ |! m( g
}
7 w) ~4 Y6 V* Z* b: R ^6 t$ u: W. o; A2 \
try {
9 u: s$ t4 c7 a) g' p- _ Heatbug proto = (Heatbug) heatbugList.get (0);
( W, O# @6 ~+ h0 n9 i! h, Q Selector sel = 4 g4 ?- W: E, t* D$ }( j( [4 j8 [
new Selector (proto.getClass (), "heatbugStep", false);
- O5 U) O D" i' G- y actionForEach =8 ~. q( X S) d6 e+ D8 M" T
modelActions.createFActionForEachHomogeneous$call2 z" ]1 f/ I% o* b+ Z! i0 w
(heatbugList,
! ~, H; m% F9 s3 k( a ]: ~! V new FCallImpl (this, proto, sel,
\4 y. T+ U: t, X" h0 s! } new FArgumentsImpl (this, sel)));# q. q+ Y4 I7 a, h, V8 s: ~
} catch (Exception e) {/ w( C: }, U+ u4 |: s
e.printStackTrace (System.err);* V! E6 V) W1 M2 l* |
}9 }" ]* o0 p- Q9 \. S
+ o8 {* K+ k+ Q4 r1 k; H- ~! \! |; L syncUpdateOrder ();5 x6 G* w) L$ C& Q: p4 W
3 j7 U W4 C$ ~0 z _$ \6 l try { W' z3 X3 X% V- ~! U) Y; Q
modelActions.createActionTo$message ( O4 M- j! E `1 n
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 z& a- r6 e8 H3 q3 v } catch (Exception e) {, C7 P( K0 N1 {8 ^' r2 V/ \
System.err.println("Exception updateLattice: " + e.getMessage ());
* R; I0 c$ Q+ r0 Y" w. C+ n# { }
% s( u# }2 C' W4 @. \ ) d! z" Y+ Y6 x7 x+ ~; ?
// Then we create a schedule that executes the( m- c! ?1 L3 `
// modelActions. modelActions is an ActionGroup, by itself it
- O, L$ h6 `3 ~: d8 H$ | // has no notion of time. In order to have it executed in
; P1 B, @, S0 ^; j6 e& e // time, we create a Schedule that says to use the
% N5 Z/ j4 n8 e8 F6 s' D* G // modelActions ActionGroup at particular times. This
( z: i9 e; O) R( C // schedule has a repeat interval of 1, it will loop every$ j: T7 |9 B3 B0 E1 [* A. b9 ]) m
// time step. The action is executed at time 0 relative to
, v/ B% o& r6 [ // the beginning of the loop.$ o- J1 x6 K- Q1 g# h. `) {6 { H T) c
3 |2 b& _1 @/ ^5 m // This is a simple schedule, with only one action that is
; \+ g' A% U' E4 E. c7 ~" m // just repeated every time. See jmousetrap for more
/ H" [$ A3 k- X& K" B, n$ _ // complicated schedules.
4 Z. d. W4 _6 [' t* ] 4 i& M! Z0 ]7 b7 W
modelSchedule = new ScheduleImpl (getZone (), 1);
* I; b# l$ q1 ~& O o modelSchedule.at$createAction (0, modelActions);. W) @$ I. g" k% }/ {# m& I3 y
% Z. X3 _3 i, u6 s$ [* \
return this;
. Z! x- N0 b5 z } |