HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ \& c/ F0 Z% o! q+ k( d, j9 U3 @( \. ]8 m! a+ C m! K' A& b+ A
public Object buildActions () {1 b* Y: E! ]$ D9 o) ~
super.buildActions();+ e2 x' x! p# T5 m; T
( L: r2 d/ K5 T
// Create the list of simulation actions. We put these in y; `9 _. V; P# P8 U
// an action group, because we want these actions to be
7 g' S7 @% l% d9 R5 i! m' t // executed in a specific order, but these steps should
: u! Y* X8 S* ?/ S' d: n7 z // take no (simulated) time. The M(foo) means "The message
4 M; X- s, k; }! k# @# E. m4 e1 U // called <foo>". You can send a message To a particular8 ~# G' Z4 Q; v1 U
// object, or ForEach object in a collection.6 o) d3 Z5 T7 l
, m' [: Y# @) J // Note we update the heatspace in two phases: first run
% ^) t. L' x# U4 _+ p* Q // diffusion, then run "updateWorld" to actually enact the: ], E% E0 K" K, y4 E
// changes the heatbugs have made. The ordering here is
; e2 }" U0 I ^( ` // significant!6 [9 g1 y, G" z4 s
# q/ ^) t9 T2 @" ~; l6 q& F; w
// Note also, that with the additional% U, M2 I O( [2 N& w
// `randomizeHeatbugUpdateOrder' Boolean flag we can! \9 b. w) c; T6 ]# V' ?
// randomize the order in which the bugs actually run
9 Z W1 B* _9 z) S9 C // their step rule. This has the effect of removing any
2 D; T, N& ?; ~& @ // systematic bias in the iteration throught the heatbug
1 @; y& x2 s1 J7 n5 ~2 E# t // list from timestep to timestep
) c3 o& t4 `; k: `' g2 i# I* G % d4 z: g5 c1 E: {
// By default, all `createActionForEach' modelActions have2 t( I8 G2 e+ I: D; A" ~( j
// a default order of `Sequential', which means that the' j8 D' J- d" L2 B* ^
// order of iteration through the `heatbugList' will be5 [' e8 t; w- C. O
// identical (assuming the list order is not changed
% B \* H5 j! r D // indirectly by some other process).
. @( @5 F G7 j1 E# `7 e& L 2 z! R0 S2 @9 o5 O# O3 C
modelActions = new ActionGroupImpl (getZone ());1 G( [+ _* m z# v
( v& w v7 J8 n7 w0 g# Q try {
F# q4 F/ |* [# _- _& ?+ {3 ^ modelActions.createActionTo$message
4 K& x+ m% ?0 K (heat, new Selector (heat.getClass (), "stepRule", false));5 i# M- I. Z! W O
} catch (Exception e) {
. {2 V8 i* Y' C! `+ e, e+ z9 C. s System.err.println ("Exception stepRule: " + e.getMessage ());
. C6 c, p9 ` L5 }- ] }5 Q0 d" r- @7 f$ _4 e Z' s
8 a) U1 j. s. d: ?5 M5 H" J
try {
, U% H3 d& t2 e9 N Heatbug proto = (Heatbug) heatbugList.get (0);
7 S9 B8 J; P. S" H Selector sel = 7 X2 ^5 }+ i) e" k4 ^3 p
new Selector (proto.getClass (), "heatbugStep", false);% N# }, n& J+ ?+ Z5 x
actionForEach =
: H" m* J2 C3 h) F modelActions.createFActionForEachHomogeneous$call& d2 l2 E, j+ f" k& ^9 U
(heatbugList,' M' [4 O5 ?% k8 q
new FCallImpl (this, proto, sel,
; h0 Y. N/ S6 M0 ]0 ?: v new FArgumentsImpl (this, sel)));8 a q: x9 U& q, g4 i5 z
} catch (Exception e) {
5 }; w7 K' V+ J" [/ m3 d) X$ C e.printStackTrace (System.err);2 n- T" @! `! e4 S1 J% d
}/ V7 V8 K$ P+ P; X$ P% {& d/ A
4 p* t! L! d7 t% j' U syncUpdateOrder ();9 @, r2 @* w8 a; ^( n# ?, g
5 J- Y% d+ |* Q% }/ C, k* }1 L try {2 D3 J0 K6 \) p
modelActions.createActionTo$message
. p" }2 m) S/ M& [8 ?" h (heat, new Selector (heat.getClass (), "updateLattice", false));* E" k5 T4 n: c. z$ P1 ^
} catch (Exception e) {
9 p/ U5 Q& p8 m5 i6 ^5 z/ t A System.err.println("Exception updateLattice: " + e.getMessage ());3 p7 Z% I6 }( a) o( k: E) q0 s9 ]
}+ F6 A: k- q6 N; W( `
4 C0 H! V' u! O6 V3 s# M: B
// Then we create a schedule that executes the
- O2 f) ^% ~1 _' D5 O // modelActions. modelActions is an ActionGroup, by itself it
1 F. b9 t$ B+ Z. P# u // has no notion of time. In order to have it executed in6 r2 K" y/ ]! ]& s
// time, we create a Schedule that says to use the' x5 g. c E% g) F) ^# m: ^3 ]
// modelActions ActionGroup at particular times. This( {, c5 h. i! C7 G/ G4 G
// schedule has a repeat interval of 1, it will loop every
" Q* O5 F8 d4 o; p8 c // time step. The action is executed at time 0 relative to
& k p% ^4 m2 y# Q1 Q // the beginning of the loop.( i1 E. ~6 G5 k
% O; y- L2 d1 [/ J M" D // This is a simple schedule, with only one action that is; V0 ], ^: l @' p+ a" o' Z- d
// just repeated every time. See jmousetrap for more5 |& g2 V* a( d6 d, e& x, x
// complicated schedules.
+ Z3 N2 u' y! Z$ | K: }. ]7 U . W2 }6 Z, E4 n& s0 [# q
modelSchedule = new ScheduleImpl (getZone (), 1);
. Y" B/ D# @. w( \+ R5 g modelSchedule.at$createAction (0, modelActions);/ {2 K% b5 D6 G( J2 o
5 R$ l8 ]+ Q2 w: g* l return this;
0 b# o* w/ u4 t/ m } |