HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 z( o; b0 c5 R; R1 d1 z& C, x, z$ n- N/ E# B6 H3 P
public Object buildActions () {
5 b, n. j( I3 z, i. _6 e/ k super.buildActions();
% v. S+ o% H5 {! I: \5 Y' _) a 4 \8 D0 z9 Y7 ]1 L: M, h
// Create the list of simulation actions. We put these in8 B4 Q1 S) r" }$ {8 K1 Z. ^
// an action group, because we want these actions to be6 |- _8 j6 y, T; @, B& m3 X
// executed in a specific order, but these steps should/ a5 ]; \* o' V
// take no (simulated) time. The M(foo) means "The message6 L6 p" [% ?3 V W8 c
// called <foo>". You can send a message To a particular6 {8 A2 H; a2 s! }1 o1 |/ ~# q( u
// object, or ForEach object in a collection.
# E# @+ v7 d1 J' a7 L- W " U5 W0 z' r; H4 `% ^6 |
// Note we update the heatspace in two phases: first run q" ~, S0 n6 E' D6 b: U L5 t, d
// diffusion, then run "updateWorld" to actually enact the
" X1 E4 o h1 V" `: q0 G, Q // changes the heatbugs have made. The ordering here is* R8 u6 N9 @8 m3 X% c. |) h
// significant!7 B5 q: U3 e! T' h) a
& P' P. _ o5 y/ f* }
// Note also, that with the additional1 i' q/ y& U% C
// `randomizeHeatbugUpdateOrder' Boolean flag we can8 B: W, i/ V% @
// randomize the order in which the bugs actually run9 j% D" M; g3 A" R. c; e5 X
// their step rule. This has the effect of removing any( m+ L) d* o6 g; W
// systematic bias in the iteration throught the heatbug
, }4 o+ v: v+ X5 `, p6 y e // list from timestep to timestep' D" B% g) v* f5 V
9 V( o- X' O* M/ n# K! V3 A
// By default, all `createActionForEach' modelActions have2 | ]* E) R1 j( c
// a default order of `Sequential', which means that the
& s( m/ ^1 C' V. z [* C7 j5 W5 X // order of iteration through the `heatbugList' will be
1 i. U, d6 S, l5 | // identical (assuming the list order is not changed
" |/ L+ z h# V5 c8 g$ k // indirectly by some other process).
; U8 {* p0 ? G + p, d# A8 ~* g% `) E: b1 O
modelActions = new ActionGroupImpl (getZone ());2 _# Q! B$ s* _4 }5 F
9 ]2 u* R6 Z! i. q% O. c7 c$ ?
try {0 I4 u' ~% m$ ?& u! r
modelActions.createActionTo$message0 r: r: l* H9 O- E3 d' W" L
(heat, new Selector (heat.getClass (), "stepRule", false));' E) n- e& u! F' Z$ t' `
} catch (Exception e) {4 j; Z& \* _! S
System.err.println ("Exception stepRule: " + e.getMessage ());# v! k1 E4 y) |/ j9 ]" {
}
0 u; u- P+ t9 y( i$ _. z, w# {$ A; p/ R9 y. q
try {
7 i3 \9 z( U0 {; ?2 y$ k Heatbug proto = (Heatbug) heatbugList.get (0);
/ s w. S4 a( q: J! u5 g& \* N Selector sel = * [: D6 h% ]: d$ G5 A0 {5 T
new Selector (proto.getClass (), "heatbugStep", false);
P- u D( q C9 X _ actionForEach =0 \( v% X: j: u4 }1 a4 t, U
modelActions.createFActionForEachHomogeneous$call, P1 Y# b7 Q6 K& W
(heatbugList,5 d: H3 j! U2 Q4 k
new FCallImpl (this, proto, sel,: ?; o/ N S: V
new FArgumentsImpl (this, sel)));7 H( _* t O1 K
} catch (Exception e) {
2 d$ D6 _# {, t e.printStackTrace (System.err);$ W/ c# j, G3 S( q% O; p5 f) e- ^1 n3 m
}
$ P; c; T/ P$ y# I9 e ' }) F; m9 x; X4 p. ?
syncUpdateOrder ();3 Q; ]5 Q. u2 P9 H9 b( c; s
. N2 p- T6 j. J" x! W: n- E o try {
, a% m4 _% \ j5 P) f modelActions.createActionTo$message - ~* Q/ s6 D; B5 w- a3 }: N
(heat, new Selector (heat.getClass (), "updateLattice", false));
0 K" t! }, R& W7 | } catch (Exception e) {
- n6 k. v* t. W+ P8 E; T7 g System.err.println("Exception updateLattice: " + e.getMessage ());3 W" u. z! j/ [4 a* m
}
0 P$ G+ n) d' N" w ( T: N0 b5 I" t6 ]
// Then we create a schedule that executes the1 X, Z) U3 a1 k
// modelActions. modelActions is an ActionGroup, by itself it
! K. f/ v0 V/ \, L! ^2 Q( x // has no notion of time. In order to have it executed in
3 q, y0 X. S8 u' h' [ // time, we create a Schedule that says to use the
& f( \$ f: Z" Y, o0 X // modelActions ActionGroup at particular times. This
' l' S d" V( n# Q1 X( v7 [ // schedule has a repeat interval of 1, it will loop every7 J! L }. T) J& r! F4 o/ o4 G5 N
// time step. The action is executed at time 0 relative to1 E0 \2 c) P/ c
// the beginning of the loop.
6 r6 k. F4 V8 z/ t/ o0 N7 ]; V5 D$ S& }$ M5 H+ `
// This is a simple schedule, with only one action that is5 n; M' V) Z( K, Q3 n% z Y! X' n
// just repeated every time. See jmousetrap for more: d' y- g. l+ O& W, W& Y1 z
// complicated schedules. T% h; c9 G& W2 K
& v1 [8 C. Y0 a* A9 t- K
modelSchedule = new ScheduleImpl (getZone (), 1);
6 l" P- o' k5 t% u) ]3 x modelSchedule.at$createAction (0, modelActions);
( k3 ]9 B" \+ N9 G, e8 @6 ^2 y # c5 n! m, g5 U1 f( `
return this;
) r( h/ U' |, R% R& Q' J9 f. B* E } |