HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ O+ G+ y+ Q3 q) {
4 r1 O# N5 o0 h1 k+ D
public Object buildActions () {
/ I, k$ l4 I, A super.buildActions();6 J1 K) `2 A f5 _# a
4 ], N h/ P9 P; k" ?1 ` j) k // Create the list of simulation actions. We put these in( h* p, }" X4 P- Y6 u* [5 ~+ a
// an action group, because we want these actions to be7 T7 _* g* E$ G6 n5 J2 L/ `
// executed in a specific order, but these steps should/ w: L: u1 _! I {2 y5 d& e, }
// take no (simulated) time. The M(foo) means "The message* v; \% S( b/ c
// called <foo>". You can send a message To a particular
: I0 k& F1 }8 c# E% ]( P2 R: C // object, or ForEach object in a collection.# _9 M( r. ^* b# R3 `: _: V4 Y
1 Z+ q4 u! I* n6 c3 \
// Note we update the heatspace in two phases: first run
' p$ f: A+ R7 k Z/ A // diffusion, then run "updateWorld" to actually enact the
0 V# e/ t5 r9 E7 Z$ \- Q9 V( A // changes the heatbugs have made. The ordering here is
0 e& }' ]+ ]8 u$ Z // significant!
; D, A. h- x, B/ q# J# u
$ Y/ f; s, w- W# z1 ?' J' l // Note also, that with the additional
3 `: n$ I- R- J( M1 o! V // `randomizeHeatbugUpdateOrder' Boolean flag we can
( x" {9 A! r6 h) i+ O // randomize the order in which the bugs actually run
8 |+ V1 u6 W2 l // their step rule. This has the effect of removing any
1 _8 ^* ?; ~ g6 O% i ] // systematic bias in the iteration throught the heatbug
' Q0 n+ ]% } z4 m3 p // list from timestep to timestep' q5 o" f( E$ i+ j- V) |& @
1 H: C! o; w \; Q3 T // By default, all `createActionForEach' modelActions have* Z1 Z2 K' m$ B0 c% X
// a default order of `Sequential', which means that the
) C3 a$ _1 a& w- Y // order of iteration through the `heatbugList' will be
! `0 e }" H* C( G! O9 `( s // identical (assuming the list order is not changed
' W* M. w7 G/ i0 O- u, k. ] // indirectly by some other process).& i: \4 x% C, M
$ ^4 F% w5 j: `- u! z' c& A% f modelActions = new ActionGroupImpl (getZone ());
( o+ l! Y; R! t& c2 `2 a6 q3 w% j6 R: ~ H
try {4 [% g' W, X7 J5 V0 G( K# R1 s
modelActions.createActionTo$message
" w1 G9 Q4 q$ w9 F9 D( g, Y1 r1 I (heat, new Selector (heat.getClass (), "stepRule", false));
# c# y6 {2 t! S: e( Z" e6 X } catch (Exception e) {
2 D+ a" T" a! l" U System.err.println ("Exception stepRule: " + e.getMessage ());
8 e+ v6 r7 z7 H" J }
/ W5 I1 V b+ u! ?
q/ P2 \ X2 P9 r& {, d try {
9 V& s; {- Y" ^! l% o- }; V Heatbug proto = (Heatbug) heatbugList.get (0);1 m/ _. g# M5 U f
Selector sel = 3 Y6 H0 f3 M$ ]& i- Y
new Selector (proto.getClass (), "heatbugStep", false);! L- w' f4 [8 W2 e j
actionForEach =
; z4 C: p+ |2 v$ \/ v9 u modelActions.createFActionForEachHomogeneous$call4 ?( X) o; ^8 _ M+ |2 \
(heatbugList,
: O' e- T2 Q- }3 g6 [7 i new FCallImpl (this, proto, sel,
t) o: N0 f+ r0 |$ I new FArgumentsImpl (this, sel)));
/ Y) {' E; e. R$ Z: B } catch (Exception e) {, t% x& G5 j. P) h
e.printStackTrace (System.err); {4 Y% L0 N e$ D2 i
}/ C1 L& F% i3 k8 q( ?9 G u: V! y
* f7 M' A, i/ J syncUpdateOrder ();0 R# u( u" F- d' a) d
4 Y+ ?+ U1 Q( C1 k. N: ^9 o try {
8 y+ A. D1 F1 y5 L- [ modelActions.createActionTo$message / c6 ^0 r$ Q2 z4 Q# E& v; I
(heat, new Selector (heat.getClass (), "updateLattice", false));0 U, l/ L3 ?9 ]2 u; E( U1 }1 M# @
} catch (Exception e) {) m' ]1 }) O1 W1 I: m
System.err.println("Exception updateLattice: " + e.getMessage ());$ G& ~0 v& ^. |; `
}" R" g- ^. z, C& R7 z/ c$ l
' P: F4 ^0 H8 |) `! S6 | e
// Then we create a schedule that executes the2 P# q; S3 S# v. y* e1 X
// modelActions. modelActions is an ActionGroup, by itself it
1 T7 R. P! ?0 d/ S3 @, h // has no notion of time. In order to have it executed in6 ]2 x5 E9 [6 @) Y0 X7 g. v
// time, we create a Schedule that says to use the: z# s, M# C( M3 Q3 N& o. b4 m
// modelActions ActionGroup at particular times. This
- u: ^( p* i6 c // schedule has a repeat interval of 1, it will loop every
+ v! x$ k' R; n) d0 L( Z0 P, ? // time step. The action is executed at time 0 relative to
; r5 a9 I) u4 t/ X$ v9 [2 M# L // the beginning of the loop.
7 g$ ^. W- u6 P$ C, T+ {; J3 N2 f9 Y* t3 H$ d8 I
// This is a simple schedule, with only one action that is9 G. h8 ]8 Q0 J
// just repeated every time. See jmousetrap for more3 b, w w8 N' y0 S' n2 p$ W% S
// complicated schedules.$ p- G4 N- u5 ^7 `( f
5 ~% b$ b/ Y: ] modelSchedule = new ScheduleImpl (getZone (), 1);! M8 o( o' M* K, n# W
modelSchedule.at$createAction (0, modelActions);
% E3 Q0 l: t0 m% x* V" B& d ; B* A8 U% {. T; Q$ F* d! _& p
return this;% D% Z/ ]1 c, ?$ U
} |