HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 l' b) Z& W* p3 P% X+ S$ q V4 Z6 c* o/ l
public Object buildActions () {% I3 C( }6 \& q
super.buildActions();
: C3 B) N0 k; C' T0 U; o
- `- F6 _$ k$ A3 n- [ // Create the list of simulation actions. We put these in
* W8 [' H3 _7 O( Z4 @ // an action group, because we want these actions to be
8 K/ Q2 z! `# n) ]3 T+ V" R // executed in a specific order, but these steps should
/ i5 G7 K- Y8 i' T7 @7 |) u8 Q A& T // take no (simulated) time. The M(foo) means "The message7 I; M) F. ~) a
// called <foo>". You can send a message To a particular
. B4 `" z# ~& X( O8 K- _ // object, or ForEach object in a collection.2 i5 K2 y. x5 n f; z
& O% P9 U. u0 K- `9 n
// Note we update the heatspace in two phases: first run! ^- T$ E7 B+ l' i
// diffusion, then run "updateWorld" to actually enact the
# P% d& d4 f5 B0 C" U4 t // changes the heatbugs have made. The ordering here is9 W5 n2 Z" t" u6 C) n4 b
// significant!/ M* }1 X( }: J# h' p8 |( @
0 z- m0 e1 e2 [' T) ~ // Note also, that with the additional
1 }1 \0 s7 @" \) |. S- X" O9 \7 |) S // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ v5 t/ D* ~" }1 w) ~( a // randomize the order in which the bugs actually run
; x3 L/ \( T% _ Y // their step rule. This has the effect of removing any
( _: j& c) w1 f3 t9 J; t+ d // systematic bias in the iteration throught the heatbug+ Q7 u, v7 @- f
// list from timestep to timestep
$ A" e* Z$ ?# }! a
, Q5 V8 Y0 s' `( A* T( K // By default, all `createActionForEach' modelActions have- S' ^( `% M: ?, a' B
// a default order of `Sequential', which means that the+ a3 c& `+ G5 v9 U3 g. c/ g
// order of iteration through the `heatbugList' will be: J; Q5 S7 B/ _2 W3 m
// identical (assuming the list order is not changed
1 h4 S' ]7 A3 Q) f$ X // indirectly by some other process).+ d5 }- E9 O# d1 u" u
) }4 _0 w3 h* E! b1 h
modelActions = new ActionGroupImpl (getZone ());& Q& `9 h& R5 _! [+ m2 \# W, {+ q3 i
( A% f" p5 ~6 j& C1 ~, L" M ^# y2 v
try {
4 c! c5 g( B, C+ z: T1 g. J4 N modelActions.createActionTo$message8 R& \# _" m. L N
(heat, new Selector (heat.getClass (), "stepRule", false));
4 L. T7 E3 q5 |5 s7 Y } catch (Exception e) {
6 k, }, ~4 h: r- n9 o System.err.println ("Exception stepRule: " + e.getMessage ());
! ^6 [: c0 i) m2 H+ L }
0 b/ f; p; R1 t6 E+ ]- W5 d) q }# y# P- X& _+ I
try {
9 G$ E' Z% |1 L5 R! s3 ]7 @ Heatbug proto = (Heatbug) heatbugList.get (0);7 r# j; E ^1 p" ~9 F5 k- K
Selector sel = : P8 _ e8 s% r8 R
new Selector (proto.getClass (), "heatbugStep", false);
9 N. Q9 I0 d/ f* i; p0 m actionForEach =
8 M! M* v" h. c( _4 O/ g modelActions.createFActionForEachHomogeneous$call0 L. _0 P5 M3 l u5 M
(heatbugList,
( [, B6 Z0 a, J' \5 O U new FCallImpl (this, proto, sel,
7 c3 V8 m. V3 @# m8 ?' S% D new FArgumentsImpl (this, sel)));
3 L# w7 L. x8 A } catch (Exception e) {+ W2 X( e' t2 q8 C% R* p
e.printStackTrace (System.err);
/ X3 w/ ~1 s6 R+ U7 \8 T8 F8 ?) i9 S" m } q( }- D0 S' L' s9 ?4 `. R
. g& C6 K& q4 v" D9 z9 b+ Q' |! l8 O
syncUpdateOrder ();, m2 w1 m6 m: V* P
, o0 k: @( H- u. Q9 X g o try {
, d" `4 t; ~' l4 O modelActions.createActionTo$message
* C3 b: e1 G5 |# K" |% z' X/ ~. `5 K (heat, new Selector (heat.getClass (), "updateLattice", false));, y9 R$ K* _" [2 T% [
} catch (Exception e) {
+ f8 ~4 [8 A, t* `. s3 \& [ System.err.println("Exception updateLattice: " + e.getMessage ());
7 y2 }2 O# A% h7 Z$ B) I }
. B6 w& c) E# U X8 O! z! C% v/ D ( l& l; C9 B. t% V
// Then we create a schedule that executes the- F+ U+ q+ P) A3 E. F) m( A3 z
// modelActions. modelActions is an ActionGroup, by itself it
& Q( L3 s) r; _( y* Y' [ // has no notion of time. In order to have it executed in
" T H6 {5 X" A& | // time, we create a Schedule that says to use the- l( Q; m* D! c& d4 H' L. i- Z
// modelActions ActionGroup at particular times. This
0 i; j2 k# d) V, F" Y/ Y( c+ g // schedule has a repeat interval of 1, it will loop every2 v% J; q6 z# q7 p+ m1 k
// time step. The action is executed at time 0 relative to% _& R2 F4 }6 Q1 c" T, A
// the beginning of the loop.
6 [% k5 l3 D6 c$ L9 d& ^. t7 l" F; Y3 j) s
// This is a simple schedule, with only one action that is) a, X6 @ E5 ~
// just repeated every time. See jmousetrap for more
9 c3 z( E4 s/ F5 E // complicated schedules.4 k3 A5 Z$ I: V& C- g5 `( F! ^
& P! \: z4 p% s, V, ~! W! h* X modelSchedule = new ScheduleImpl (getZone (), 1);. ~1 V; K4 ?6 ]4 O8 N- X! P7 X
modelSchedule.at$createAction (0, modelActions);
- Y3 K1 t" }4 d1 _6 y7 [ $ a3 f+ i6 G g3 W1 x, ~
return this;7 Q h- Z% A) A1 f z) \6 I
} |