HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ j7 E6 j' }5 ?0 I0 x4 d9 X
3 E& n D5 T' x" I public Object buildActions () {
" F$ O% A. a+ d4 c super.buildActions();
( x: K. Z& D+ r# D1 ~
' f$ ?3 w7 U6 t3 N* x$ Y // Create the list of simulation actions. We put these in9 c8 x; i/ k. d/ Q( N7 X7 I6 Q
// an action group, because we want these actions to be* w& l c, [/ g+ |
// executed in a specific order, but these steps should
9 c5 b# ?' D# c // take no (simulated) time. The M(foo) means "The message: r2 x) U, q% Z9 O' }9 P' J' Q2 x
// called <foo>". You can send a message To a particular# Y, f3 Q# E( g7 j7 W
// object, or ForEach object in a collection.
5 t3 T! J1 s* o0 A6 Y H
: r$ h z, e# Y+ G- q, w& ` // Note we update the heatspace in two phases: first run
' y9 z8 u0 N7 i" j0 B R // diffusion, then run "updateWorld" to actually enact the' D$ @8 i( @% Z
// changes the heatbugs have made. The ordering here is
/ z z8 q. u8 }0 | // significant!
7 c- {5 b& O" q2 c2 u & j' Z6 i5 m z3 _+ w
// Note also, that with the additional
+ N9 Y1 E0 P0 m6 ^" Q // `randomizeHeatbugUpdateOrder' Boolean flag we can+ V2 @4 I4 H) N. J( V. a
// randomize the order in which the bugs actually run
$ n9 Z! ]5 S* W: }$ t" T4 t // their step rule. This has the effect of removing any
5 t! s4 j6 ~/ q5 \ // systematic bias in the iteration throught the heatbug/ _6 M7 [* \9 |0 V1 Y
// list from timestep to timestep, Z3 M* \& w) u' f2 S5 T1 ^
! {2 g6 t5 u' n // By default, all `createActionForEach' modelActions have
8 g0 ~$ S" f5 M* R1 w // a default order of `Sequential', which means that the3 m: R; M+ l4 e! }# l. x# I7 V
// order of iteration through the `heatbugList' will be" P/ p) G; `0 l6 k; G) z
// identical (assuming the list order is not changed
/ e2 L# w# l" ]7 C: Z$ f( v // indirectly by some other process).
2 G0 [( J. [/ M/ i$ y! i' h6 a
) ^( ^3 v; b, ^) Y5 }4 {: b8 d" x7 Y modelActions = new ActionGroupImpl (getZone ());$ s1 N& q& X# Y9 R
: z7 m2 E2 g5 \- W1 X+ a( y try {
$ u+ J9 J* v& O) E$ M6 F q modelActions.createActionTo$message. y9 P# @! v+ W1 V; Y: ?; C \, b! _
(heat, new Selector (heat.getClass (), "stepRule", false));
; M1 W- g, ^/ i7 y } catch (Exception e) {# l7 d0 r( p+ A7 b$ A: a2 J8 Z
System.err.println ("Exception stepRule: " + e.getMessage ());
$ l8 D5 ?7 u7 N5 F `9 O9 o }# o4 Y3 G8 e4 R& j3 }8 G
( A0 L/ {, T' j
try {& O+ S8 P9 D# O; b
Heatbug proto = (Heatbug) heatbugList.get (0);
6 A4 K u H/ Q j, F$ y5 Y% H Selector sel = p3 k1 H. i2 @% M8 d
new Selector (proto.getClass (), "heatbugStep", false);
% H2 f. s, |0 Y3 X+ Q7 Z! L# w actionForEach =
; z% `" b y9 F$ d& X modelActions.createFActionForEachHomogeneous$call! ~+ M/ E+ Y! l, w
(heatbugList,& M1 @8 {7 _1 g3 ~
new FCallImpl (this, proto, sel,
1 }) |4 d& a; @/ w new FArgumentsImpl (this, sel)));
/ T$ `2 o5 ?& o) e% i+ q4 b } catch (Exception e) {: {. b2 }. T) y/ O( A1 k( M
e.printStackTrace (System.err);
3 f" V+ b; \$ s. q }
# C4 A' }: a" ` ( H0 t+ V1 B+ @% W( Z+ P/ l
syncUpdateOrder ();
6 Q" d5 G# c. M. G1 W
$ g" Y2 b' |' G9 d! l% T( Y try {" V% j! f8 c& K6 q2 C
modelActions.createActionTo$message
+ o% ?% V" m3 A/ n (heat, new Selector (heat.getClass (), "updateLattice", false));2 A" W7 P9 `9 @7 M
} catch (Exception e) {8 O" {: a* {! u, M9 y
System.err.println("Exception updateLattice: " + e.getMessage ());
0 Q1 T8 [4 @9 k1 ?/ b }
6 J# g* L" o8 b( v! |$ c1 w( B( S
# m; t4 I. W) N+ m, o I' U3 S // Then we create a schedule that executes the
3 f% P& { h0 B* q$ ~/ ? // modelActions. modelActions is an ActionGroup, by itself it# D2 i1 z/ ?. w J" p- F7 @5 ~
// has no notion of time. In order to have it executed in+ r, H/ U3 \% z$ H$ g' u) o
// time, we create a Schedule that says to use the0 u4 l+ r* A- L( ]) c" M7 W; _
// modelActions ActionGroup at particular times. This
' d D6 b. W1 s; B6 a! V // schedule has a repeat interval of 1, it will loop every
, w2 ]" t4 r" e, m7 P // time step. The action is executed at time 0 relative to
; g. B9 q* a2 c, E; z // the beginning of the loop.
) C& s' H$ p$ u" B4 j, J, e4 m6 @: \( v5 y& B D
// This is a simple schedule, with only one action that is
! x$ x4 V( Y4 E, I // just repeated every time. See jmousetrap for more
$ @( T" `2 M( f" g4 I6 H7 Y // complicated schedules.
) \% Z% M' G! V+ R$ c ( v; ]& l# m! f1 `
modelSchedule = new ScheduleImpl (getZone (), 1);6 `! g2 h9 n6 Y6 c
modelSchedule.at$createAction (0, modelActions);
) q& H0 U$ i+ \3 s $ H! ]8 G& g+ _
return this;. I3 s8 z4 v1 K$ D& l6 V
} |