HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 S, J" p6 V3 t- h" L
1 p/ G: i1 K3 I- J4 ]
public Object buildActions () {
, U. l; w0 j* Q7 n5 _5 k+ N6 S super.buildActions();
- f* f( c6 E! q- Y, B
. J) u: V1 e! ]4 s# k6 B // Create the list of simulation actions. We put these in1 w6 D2 x8 R; R( ]; v% Z
// an action group, because we want these actions to be
0 F1 ^; t/ h5 Z // executed in a specific order, but these steps should
' E2 Y" w, c A: g# N- M // take no (simulated) time. The M(foo) means "The message
" N( h& E7 U, G! L/ f% y0 k6 r // called <foo>". You can send a message To a particular
|: Q% K* B. M // object, or ForEach object in a collection.4 O: `+ H" d: X9 l3 P
. R5 @0 [* F- ]) D5 x" t
// Note we update the heatspace in two phases: first run
% i5 U- e% K) \4 Z0 a9 U% F // diffusion, then run "updateWorld" to actually enact the
5 m* f# z/ X' U* x* F // changes the heatbugs have made. The ordering here is3 V0 |, `4 b6 l) \3 o
// significant!
& ^3 d8 G( m8 o# j& l6 L# g& m 9 N! l! u$ {/ T
// Note also, that with the additional
' W" M; [7 h1 @# i3 c! d8 p // `randomizeHeatbugUpdateOrder' Boolean flag we can
! ?% z& {9 u& `8 a6 _& z* ? // randomize the order in which the bugs actually run
' o% \/ }' H( F( K // their step rule. This has the effect of removing any+ K ^ w) a: \( G Q! ^
// systematic bias in the iteration throught the heatbug) p. n% m9 Y5 p; R: Y8 Q
// list from timestep to timestep
3 M1 M% o) T# |2 R) h: b2 ^1 i5 r 9 B, J! H8 T! F) b
// By default, all `createActionForEach' modelActions have
% l9 c4 N* m3 d& b // a default order of `Sequential', which means that the
3 d5 C1 @! v h // order of iteration through the `heatbugList' will be( t, f- [+ Y; g( A
// identical (assuming the list order is not changed
; z6 o3 T3 D! x1 l: P4 e // indirectly by some other process)., z1 T9 X$ ?( N9 P5 E
5 [3 p0 E5 r9 t3 U- W modelActions = new ActionGroupImpl (getZone ());
8 _3 P# V6 L! W& @* u- G, c' y) W* g- c# k S
try {8 F( R; X) U! l$ F6 f/ l w$ [4 m
modelActions.createActionTo$message% M+ t* w+ ~; c7 T' Y
(heat, new Selector (heat.getClass (), "stepRule", false));- X: W1 _$ Z4 \% q7 l
} catch (Exception e) {
) W4 @3 T/ v3 _+ { System.err.println ("Exception stepRule: " + e.getMessage ());
% E6 }) O# P3 t3 b+ N }
# m. I7 g; H9 h; e3 G g6 n4 H( x; l+ X" B
try {" E8 ~4 F" e) L8 r# ~
Heatbug proto = (Heatbug) heatbugList.get (0);5 |7 V" n. Y( {- Q6 h7 \
Selector sel = & q1 a; B& s ]8 D8 P) a6 y
new Selector (proto.getClass (), "heatbugStep", false);- a! o7 Z0 \/ H
actionForEach =
) ]6 ?( S" s6 d& ^ modelActions.createFActionForEachHomogeneous$call1 j" q/ N5 A2 B2 i
(heatbugList,
0 B9 T: h: Z9 j- G& [* ~ new FCallImpl (this, proto, sel,
8 I+ T C0 O3 t! t% k2 O new FArgumentsImpl (this, sel)));- G; S2 o0 r8 [6 V3 `
} catch (Exception e) {, D; X+ ]8 F: Y5 y* j
e.printStackTrace (System.err);
. `% a6 z1 A% g( D8 u! I }
0 A) ^- i0 v! C1 F: i
* D# h7 I6 w' Y syncUpdateOrder ();
, ^4 i1 {! t3 M5 Q: }
' O: X( p" e$ Y6 v$ [( Q. B1 ~ try {! g: E5 Q& m. u: i( h' e( a
modelActions.createActionTo$message * A0 ?$ B+ k3 g6 y1 a
(heat, new Selector (heat.getClass (), "updateLattice", false));& A" }' C I% @6 v
} catch (Exception e) {
, `: F4 { E- H System.err.println("Exception updateLattice: " + e.getMessage ());
/ A5 A. o5 p3 H- b9 q9 L" V }
5 m8 j/ B, _6 n6 a4 {: j4 ] # A, y, m6 Q; r! U) Y3 B
// Then we create a schedule that executes the
5 K% ?3 R0 h5 d( w // modelActions. modelActions is an ActionGroup, by itself it
8 F" z4 l5 ]+ q/ T, g // has no notion of time. In order to have it executed in
9 N3 X. Z- E" k" T; i, t. _0 O // time, we create a Schedule that says to use the6 V9 C8 F; D/ v
// modelActions ActionGroup at particular times. This$ u- l, M# c% ?$ c7 N% m# X( u
// schedule has a repeat interval of 1, it will loop every
5 J; z& s B. j' T" g // time step. The action is executed at time 0 relative to
" O' v1 P3 V/ {2 N0 ? // the beginning of the loop.
% x- X8 o, @' A1 S& w* U8 L; g; ^
// This is a simple schedule, with only one action that is C4 B4 Y, k3 X% Q1 `1 y
// just repeated every time. See jmousetrap for more, T: g/ D: S2 k! g5 t! i
// complicated schedules.9 o5 B4 v0 M: a* R f5 h' [
, [# u# D3 R7 Y. W+ x( f modelSchedule = new ScheduleImpl (getZone (), 1);% P9 ?4 Q* K2 u5 O7 W+ @
modelSchedule.at$createAction (0, modelActions);4 }: g, G; _! L8 F; N
7 ~; x$ p2 P: ^ F7 U1 t, M% x" V' J
return this;0 ]0 ~4 t' R* ^: B) ?# F+ y3 j
} |