HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& B8 d: a2 ?! i" L/ s3 ~, R( r( `2 _2 Z
public Object buildActions () {! r9 m* q2 R7 b) K% I
super.buildActions();
( I' z0 f0 ^8 h5 z2 o p 1 \" S! l1 G1 i: r
// Create the list of simulation actions. We put these in
1 s! w7 ^" Z" x% _% _- J s1 c8 g" @ // an action group, because we want these actions to be9 l, o/ e, v1 l ^; T) h
// executed in a specific order, but these steps should
% e! ]9 `) ]; e l // take no (simulated) time. The M(foo) means "The message* v& q& V D6 {
// called <foo>". You can send a message To a particular
, q7 J+ y: H+ D$ w+ k X // object, or ForEach object in a collection. w3 |7 `, O$ U( u" _+ O
6 I% e6 F; k4 p2 |5 h# K) g // Note we update the heatspace in two phases: first run5 u( n/ j6 z, W0 T) q
// diffusion, then run "updateWorld" to actually enact the
- q; }: V# G1 d; c // changes the heatbugs have made. The ordering here is" S; s1 u3 o$ t& i/ l5 X
// significant!
* y/ `/ F8 W- }4 Y5 ]
6 {3 M% G3 P+ Z, V; |1 z // Note also, that with the additional
) r& X: S( U c6 y* n7 R // `randomizeHeatbugUpdateOrder' Boolean flag we can
: t1 h' ~: I; D: x. Y% { // randomize the order in which the bugs actually run- v7 Q2 {9 b [- N O. R
// their step rule. This has the effect of removing any2 p$ m0 O+ x( R3 G- {3 `
// systematic bias in the iteration throught the heatbug
$ M! Z1 G6 |: [# f) q // list from timestep to timestep$ f' l2 i3 }3 m
2 ]8 N1 ]0 L! c$ s
// By default, all `createActionForEach' modelActions have q `! h1 \* r, [) l: ^
// a default order of `Sequential', which means that the
2 F: e3 V5 e& e" T // order of iteration through the `heatbugList' will be
1 \' S8 O: M9 R# ^+ I // identical (assuming the list order is not changed
; Y1 _0 g# V: X* }2 u# \3 k // indirectly by some other process).
1 Q' w, F( ^ d $ u, u& ]$ U2 L+ N
modelActions = new ActionGroupImpl (getZone ());, f, b# ?1 k0 l9 ?9 o- {' [
* y! ]0 }0 i2 z
try {+ k2 `2 ]2 Y$ G& d. _$ D" ^7 o
modelActions.createActionTo$message
9 f5 F) u: E6 i1 c. p3 A: T (heat, new Selector (heat.getClass (), "stepRule", false));
! T5 X1 Y8 Z" H# }6 Z5 L/ T } catch (Exception e) {
Y2 l' K- K7 u% s" F$ l" r, w1 w# L System.err.println ("Exception stepRule: " + e.getMessage ());! b1 |; v" b' e/ M
}, P3 J$ Q y; p+ e
' X7 P2 u$ o% z0 H try {
4 D5 Z2 q/ f+ y3 ?3 V% V) o Heatbug proto = (Heatbug) heatbugList.get (0);
4 M9 s, @) G* O3 ^ c) ~& m- \ Selector sel = 9 {6 B' ]3 M4 j+ E5 N
new Selector (proto.getClass (), "heatbugStep", false);4 M; ]9 W: l: |- k/ a' l1 z
actionForEach =5 [+ \7 ^. S( B2 R8 e
modelActions.createFActionForEachHomogeneous$call4 w8 T! ?& X* f" G+ u3 I; N
(heatbugList,
2 T, j, {) m+ ~$ B4 O ?# d new FCallImpl (this, proto, sel,8 Q9 m4 ^5 z' |" j/ q
new FArgumentsImpl (this, sel)));
?/ R. ~% G: J3 P4 d } catch (Exception e) {
. e. X* }7 n, b# n3 H e.printStackTrace (System.err);$ j9 `- v" y( z C; V' t& q0 D
}: C: c$ D% K# {' {' x
1 m+ D/ ^8 P! O( k
syncUpdateOrder ();3 m r1 K$ C" C3 L4 r. q% Y X
. f+ c# J8 W7 h4 J' h/ c) ]
try {; R& }: {; x1 e* p. j2 G* V& f _2 y
modelActions.createActionTo$message - s+ k' e0 [! A5 }8 _( @4 D
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 N0 s' ]" k' O$ ^5 V9 \0 T M& C, } } catch (Exception e) {& @2 D h5 |8 u' B; o2 ^: ?
System.err.println("Exception updateLattice: " + e.getMessage ());
$ y1 _9 ?2 W9 h6 g* A* r4 g }' z5 N5 P7 @& G" W; D) a$ O2 X3 w# W
$ ^1 H% \+ M6 R% ]3 y2 H // Then we create a schedule that executes the
7 @2 {; q3 z+ M7 Z2 ? // modelActions. modelActions is an ActionGroup, by itself it2 t) U- H8 F& N5 A8 {
// has no notion of time. In order to have it executed in
3 y4 ~2 l) _6 h/ g) V3 q: W9 D // time, we create a Schedule that says to use the' k6 L; i6 o5 D
// modelActions ActionGroup at particular times. This" u4 l3 D- Y' D0 F# f$ L1 t
// schedule has a repeat interval of 1, it will loop every
M9 I3 d: O* _3 V6 [ d // time step. The action is executed at time 0 relative to1 s+ u& Z3 T9 f( v
// the beginning of the loop.
) I4 m+ l$ X6 B: E1 `: w; a) [: }6 C" F+ N8 B/ c* N C
// This is a simple schedule, with only one action that is5 P) j1 T p5 T2 F/ u
// just repeated every time. See jmousetrap for more! K/ R, _+ n4 I% o$ B" r
// complicated schedules.% r* n4 ~( |; ^5 k3 S$ _
2 m+ h# f6 e" Q
modelSchedule = new ScheduleImpl (getZone (), 1);9 @ F, L6 K8 L& B5 ~1 X1 ]
modelSchedule.at$createAction (0, modelActions);- g: a* h7 X7 {; P6 f( O! w
: V) X& d1 A( E+ k return this;+ M# S+ m' u4 o9 t
} |