HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: Y1 N" P7 _. L3 F2 @ y% P
) v! Q; @" \& v public Object buildActions () {
4 I/ A% j' A3 Y% w$ c3 Y9 b$ J super.buildActions();% T* A1 v6 b5 w9 o% M
: a' w) S1 g5 J // Create the list of simulation actions. We put these in
8 ?3 d' S3 _, T$ H6 Z* i2 { // an action group, because we want these actions to be1 F: a2 D/ l* O" T; }
// executed in a specific order, but these steps should- i6 \" d2 M8 V4 y4 b y' m
// take no (simulated) time. The M(foo) means "The message
3 k. o0 `+ X# B& w+ |0 t // called <foo>". You can send a message To a particular( M4 ]* f7 j* K4 u
// object, or ForEach object in a collection.
9 Z; U' m2 C4 U5 x; h" S/ c
# S' {" x" w8 d8 j // Note we update the heatspace in two phases: first run
; k! B) y" [" D7 T' W/ p: Z // diffusion, then run "updateWorld" to actually enact the
l* h$ `" ^/ z // changes the heatbugs have made. The ordering here is
/ j0 o* p4 j/ A7 F // significant!
9 K: q8 N- M* `- p9 n# { ; I7 H: P& `% o
// Note also, that with the additional; H5 i& b+ w: ?- \+ F2 o
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# d2 d2 X, Z4 a1 b // randomize the order in which the bugs actually run, n( W r4 M d; y
// their step rule. This has the effect of removing any8 @ R. s% J: H+ K' X; c% M8 ^
// systematic bias in the iteration throught the heatbug, \: H r D$ ?, q5 T+ m
// list from timestep to timestep# m( p3 m) J U1 X, @* K4 j
, |; v1 U y @# w. a8 J( W // By default, all `createActionForEach' modelActions have5 T% j4 l) }% g. u! Z
// a default order of `Sequential', which means that the3 n, Q9 S" ~5 C
// order of iteration through the `heatbugList' will be
2 ~1 D, X1 t4 r2 D$ j. R // identical (assuming the list order is not changed
5 J5 N1 m+ V" o5 V // indirectly by some other process).5 X. J1 I+ N$ v9 B3 m' \: Z
8 k$ _ [$ X) L modelActions = new ActionGroupImpl (getZone ());
1 |! T& T9 o! x* p3 K
G( V$ V6 ?( J# t, X& R try {
6 H) G: \3 p. k9 p modelActions.createActionTo$message
! ?1 g( D, |, ^0 @2 ?$ ? (heat, new Selector (heat.getClass (), "stepRule", false));6 l, P' ?, e- [) V( e$ y. M- N
} catch (Exception e) {
6 F% S1 G+ L/ d3 l0 e" \ System.err.println ("Exception stepRule: " + e.getMessage ());
! r6 B# a4 \/ e }
1 V1 w9 M( l5 a$ i& I' w4 U7 q
. A" `2 m! } u try {
1 M% X4 b6 I: Q, X Heatbug proto = (Heatbug) heatbugList.get (0);
' P- E5 E% d8 X. C: f+ { Selector sel = W( d$ L6 o. D3 H$ _1 t6 z( K
new Selector (proto.getClass (), "heatbugStep", false);9 i; J- M9 c9 m& J% ~
actionForEach =
: M! ^* }+ S i. P; m9 C8 E modelActions.createFActionForEachHomogeneous$call) Y$ t/ ]; x: ]# D- z N
(heatbugList,
6 t% A4 \4 h Q- X* f: v' Y new FCallImpl (this, proto, sel,
) a: o, \1 D0 x, [1 `0 r new FArgumentsImpl (this, sel)));
8 K9 w# c8 Y3 z/ b5 c5 y. n } catch (Exception e) {
0 g8 \% r9 P; v3 I* k W& B: { e.printStackTrace (System.err);
# U0 |3 p- i( x$ A- k9 k, x }+ h, F4 K# t5 `* W3 Y1 [4 l2 D' ^
; B2 V* `# [ f6 x$ ^$ L' v+ T Y
syncUpdateOrder ();! [/ C) ^/ d8 E, J( W
1 A, A% J' R0 L2 H8 g" ~, p' V
try {2 z) U) _. z0 H/ v$ q
modelActions.createActionTo$message
4 E( {1 ^: u" k (heat, new Selector (heat.getClass (), "updateLattice", false));; v3 f# \& P" ]9 o" ]: \; `9 R
} catch (Exception e) {( J- V. }+ x! Z2 s5 y
System.err.println("Exception updateLattice: " + e.getMessage ());
8 k) B1 A4 x6 k2 \ }
% @# g0 @: z1 g; ]6 T% ^
% {, ]9 h/ Q7 h: B // Then we create a schedule that executes the9 S: e* T6 y0 V/ f+ C0 ]" `: \
// modelActions. modelActions is an ActionGroup, by itself it
/ W* c2 h* }" w5 @! O" P1 O! x a // has no notion of time. In order to have it executed in
2 K: x8 I" E. v // time, we create a Schedule that says to use the
: [# T: z5 j4 @; e5 P( l // modelActions ActionGroup at particular times. This* h& d- P" C$ ~' h6 ] w
// schedule has a repeat interval of 1, it will loop every) ~ ]0 v- W6 W' d
// time step. The action is executed at time 0 relative to, R) G& v, K- m F4 w
// the beginning of the loop.( t H$ Y% G; f9 O
% v6 L [4 o. j$ o: M
// This is a simple schedule, with only one action that is7 n S+ C" c' h, ? k
// just repeated every time. See jmousetrap for more
2 _0 u- O7 n \) R: o3 k // complicated schedules.
5 ?4 J- O7 o" M, l7 i" i/ I , b0 U1 n' d. t6 w+ q! G
modelSchedule = new ScheduleImpl (getZone (), 1);
/ t$ t: d6 _4 U o0 p2 J6 k! q e& O modelSchedule.at$createAction (0, modelActions);
7 _, ]: t8 O& ` " q( o3 ?0 B( D6 W1 A8 P2 \8 i: u
return this;% v4 V4 [4 o/ p5 b/ T! M
} |