HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 D0 C. h# B( n1 R# P
' V6 e5 T2 R5 \ public Object buildActions () {' E( `8 f9 N: ?; K! G* M- U
super.buildActions();
* b7 h& o# x2 R5 e7 |! v ; G3 R" f# t- F/ u# C! M7 X
// Create the list of simulation actions. We put these in
/ k& |+ f4 X; z // an action group, because we want these actions to be% _ V6 _/ m' U; F4 f; U% o# ?: [
// executed in a specific order, but these steps should
, U& Y! O% @% k // take no (simulated) time. The M(foo) means "The message
" o/ M" o8 j2 c // called <foo>". You can send a message To a particular
) B) o- u: U3 U: k2 e5 F // object, or ForEach object in a collection.
1 h6 }( m. @) Q- Q1 X
3 K3 P) B7 ?% p0 m // Note we update the heatspace in two phases: first run
! F z/ T$ z3 I0 X // diffusion, then run "updateWorld" to actually enact the: v5 m S6 }, Y. L& }
// changes the heatbugs have made. The ordering here is
2 ~, J. |6 M1 L2 r; V' H // significant!
1 \9 s) ^3 t3 O' x G
% X0 y$ P- r$ O3 L: F3 h0 k // Note also, that with the additional
& y/ d& g4 I7 c% K7 z // `randomizeHeatbugUpdateOrder' Boolean flag we can! j3 e3 q( j( \9 ], s$ B) V
// randomize the order in which the bugs actually run2 o# z' G/ l! [. U+ f3 Q5 E1 A
// their step rule. This has the effect of removing any& D3 m* m, ~6 j0 n
// systematic bias in the iteration throught the heatbug
9 z5 J% S V2 O3 e( A0 g7 e% i2 L& i // list from timestep to timestep8 z4 ~0 g1 i6 S' d6 L) z
/ t) s8 F- s9 r% { // By default, all `createActionForEach' modelActions have4 i8 }' z- e3 R/ N
// a default order of `Sequential', which means that the6 Q! C, q( o) k" ]1 \2 }! q
// order of iteration through the `heatbugList' will be7 f4 `8 s0 {" F, X
// identical (assuming the list order is not changed
+ v7 g( Y4 N, j. a9 Y& _3 Q& T. F4 j // indirectly by some other process).( Q! [' q7 ~2 ]8 }4 W- K+ d
0 e* y$ u1 g. t/ l
modelActions = new ActionGroupImpl (getZone ());
7 c$ h1 B0 o1 q! q' K9 j* R
9 k! C/ U) {6 x" U1 i- ? try {5 y6 S& o! G3 A4 F0 E f1 r8 T
modelActions.createActionTo$message
4 N/ ^' `+ v! Y' k& Y8 `" _0 E (heat, new Selector (heat.getClass (), "stepRule", false));* c- s* u' W8 {8 y% L
} catch (Exception e) {4 C+ S+ o' `# P5 H- P
System.err.println ("Exception stepRule: " + e.getMessage ());
! U- Y! x5 Z0 ~& \ }
[1 Y6 \( ~. l9 Z5 t" o1 z; Q" G# X# F* |7 z/ x2 B
try {, B9 u2 I: U9 B1 N
Heatbug proto = (Heatbug) heatbugList.get (0); g% U( V: b0 j% r
Selector sel =
* I" r6 p1 f1 ^+ g7 s* D new Selector (proto.getClass (), "heatbugStep", false);7 F6 }* A7 Y$ ~: D& C5 j7 x) i
actionForEach =' F7 o* x) z& ]; R# [
modelActions.createFActionForEachHomogeneous$call
' o1 D4 H8 ^* T, l (heatbugList,
% n7 h H S" f( V: U8 h4 g new FCallImpl (this, proto, sel,
5 [, |- Z/ Y; G! g% c6 K! J8 h e new FArgumentsImpl (this, sel)));3 @6 ?- ^, S6 s: E5 w
} catch (Exception e) {# W, S0 U4 ?' r9 `) d
e.printStackTrace (System.err);
. o0 S0 n- t5 {/ _ H( i' [4 K$ H& T }$ p r6 @* ~) ^
/ D4 d3 r+ N/ n3 J4 ^5 Q8 q
syncUpdateOrder ();
1 ^2 W1 c1 O/ m: _, w$ O+ E" A( b! @1 }' c: Y
try {
, r2 D2 J6 J8 j8 e3 \ modelActions.createActionTo$message 5 B9 m0 H! P b4 k+ Z; I
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 k- i5 t" [* W/ @2 ? } catch (Exception e) {1 G$ B% w, ]9 z3 t- Q
System.err.println("Exception updateLattice: " + e.getMessage ());
9 Q# _: v4 D" Z, @ }& A. n7 W; K! `1 r% C# K0 V6 ?
& w% C) Q! f4 k" @ // Then we create a schedule that executes the/ w2 Q$ Q! P1 I$ m# z
// modelActions. modelActions is an ActionGroup, by itself it
/ D2 l- ~6 `/ B2 S3 I // has no notion of time. In order to have it executed in9 L6 \: O6 q* o2 G8 G* f8 ?0 h _
// time, we create a Schedule that says to use the
# o* w7 }- a7 M // modelActions ActionGroup at particular times. This
T9 H T) \, \" v // schedule has a repeat interval of 1, it will loop every0 ` y( k4 c( r
// time step. The action is executed at time 0 relative to1 `/ Q8 j" \6 l$ s" a* r! G; p& ]$ k/ k
// the beginning of the loop.
0 D4 l: {6 W4 N w; m& `
4 U0 K. }) x7 t- z* ]3 a& l // This is a simple schedule, with only one action that is, x: s* b [6 L& ]! v- s8 _
// just repeated every time. See jmousetrap for more- j1 t) Q8 Y! Z! L% G& G
// complicated schedules.
9 J1 l0 F2 w7 l. j& E# s6 [( a. G 0 z7 ?% f* {8 e `% a1 o$ z
modelSchedule = new ScheduleImpl (getZone (), 1);
( ~7 ~ x" {+ m. W modelSchedule.at$createAction (0, modelActions);0 W/ u$ N" h& z
, `& K3 d/ C) f2 W& }3 X
return this;, O6 E5 ^4 i2 G4 }
} |