HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* ]$ R7 r. @4 i5 p& l% w
( L; I0 C3 C+ S9 R+ L/ P+ b" m
public Object buildActions () {, { r/ f$ I4 c# {
super.buildActions();
& ?% o& C7 i! [! R. s % u" d2 T7 h, t1 y
// Create the list of simulation actions. We put these in
2 C/ ]5 f+ k+ g& h$ l r$ t+ s, e7 L // an action group, because we want these actions to be0 p: _5 C6 |; G1 V
// executed in a specific order, but these steps should2 S" }$ v V* k& i3 y }
// take no (simulated) time. The M(foo) means "The message
$ n& k6 G0 J! T* L2 \& r8 V // called <foo>". You can send a message To a particular
0 z( W0 o" i% E& ? // object, or ForEach object in a collection.1 o2 F' i* v* |8 o" X- x- `8 F
. [" p: w& o6 v! Z) b5 w2 P( M4 C
// Note we update the heatspace in two phases: first run
: N# V/ Z( w8 x4 k // diffusion, then run "updateWorld" to actually enact the! m, c; [ B& @( S9 g
// changes the heatbugs have made. The ordering here is& ?* @5 b+ b1 h& f. }/ X3 I/ E
// significant!
) I* h# {3 v) b ( b+ N J5 ^6 [+ \
// Note also, that with the additional( s4 p! x* [' t# |4 P/ B, r+ q1 H
// `randomizeHeatbugUpdateOrder' Boolean flag we can
' K+ @1 ]' y _0 ~3 w; Z$ H // randomize the order in which the bugs actually run9 V; U+ ]5 r$ h- }* s: X( M
// their step rule. This has the effect of removing any
# L! Y; ]) N# K0 M // systematic bias in the iteration throught the heatbug% j1 c7 b9 O' ?; d" j- K
// list from timestep to timestep
4 s( ^% `, q: j7 n! t& x
6 }- g8 `; j" P) H" j1 V; }4 S$ B // By default, all `createActionForEach' modelActions have; M7 y* @3 Y8 ~; r1 Q8 z' w L& l- f
// a default order of `Sequential', which means that the
! b; z1 A+ Y6 I* ^ // order of iteration through the `heatbugList' will be
* o' x* r% ]2 l" f2 w: `( F8 T1 @! X2 Z& C // identical (assuming the list order is not changed4 H; p% g0 B, p2 S' `
// indirectly by some other process).' I! V. X1 i6 g# l- X
8 m4 D0 o3 D$ S. j1 Y8 ?: L5 | modelActions = new ActionGroupImpl (getZone ());2 T z( B! p5 A! s/ N
. F0 k! Y; `1 l7 R- w/ F( @ try {
6 i1 f. t& ~% k' f modelActions.createActionTo$message; M) T6 c+ c1 q: E `0 f6 @1 Q
(heat, new Selector (heat.getClass (), "stepRule", false));
( i6 a" r; Z" c$ P } catch (Exception e) {
6 z3 w* n) P' j7 d2 `2 @; X System.err.println ("Exception stepRule: " + e.getMessage ());
! I, X: v" i+ Z: E, Y! H }
, t/ N, g; S! \( m9 t0 j3 u! j8 Z- N& V) ~; S, ^& o: e: z, x
try {
7 F) q% N% Q* u1 C5 V Heatbug proto = (Heatbug) heatbugList.get (0);
! b1 j, ^, C3 d) w! V Selector sel =
" L5 z6 I9 H# Z# n7 e5 |5 Z new Selector (proto.getClass (), "heatbugStep", false);1 e8 K7 H! n& S: Z
actionForEach =: S- k* Z' [+ q
modelActions.createFActionForEachHomogeneous$call
" t0 q: w, ?. Q9 p5 H. {5 p b (heatbugList,
; X* g/ U( \" a# }8 s new FCallImpl (this, proto, sel,' f n4 w- F% O' L! \
new FArgumentsImpl (this, sel)));7 B- ? w+ c& W: i7 B* H, q u
} catch (Exception e) {
) q2 R" V. U: h e.printStackTrace (System.err);! i; j7 U5 V4 C) n
}
, u, n4 [+ u" I" z7 V. G' G % N, }0 U4 V$ r# R( l
syncUpdateOrder ();5 T3 y# V: P" Y; v5 N( o
) `( d; y* G" }# k: G+ g! W try { ~2 I# f5 F c0 m4 l
modelActions.createActionTo$message 8 C u0 r$ Z- n) k9 ~
(heat, new Selector (heat.getClass (), "updateLattice", false));) e5 F, L& ?: W+ b) E. B( q* g
} catch (Exception e) {2 P; H! |8 n$ j% y! w; J! k
System.err.println("Exception updateLattice: " + e.getMessage ());
( l' N9 m9 @0 v0 Y }
# }* T& ?: e; L 3 r8 o# k; ~2 R. V% d
// Then we create a schedule that executes the
- P9 {: F: Q$ `5 G: B8 M2 u" K" m // modelActions. modelActions is an ActionGroup, by itself it
3 k+ L% r0 f$ B# P: _9 j% V0 K // has no notion of time. In order to have it executed in( _6 `; @4 K/ J
// time, we create a Schedule that says to use the; W0 q- m. t7 {, v' r% i/ C0 q
// modelActions ActionGroup at particular times. This5 N+ L8 l) x9 k+ F
// schedule has a repeat interval of 1, it will loop every0 v9 W8 H( F2 e; S, V; ^
// time step. The action is executed at time 0 relative to; x0 e, ~1 h) E+ N3 e
// the beginning of the loop.6 v; b$ h; J, w' G! _
4 j" I# a+ n# J4 u // This is a simple schedule, with only one action that is. r1 e% W X2 ~1 \5 r6 Q9 H# m) W
// just repeated every time. See jmousetrap for more8 O4 G3 i$ [8 w7 |' Z' x% q, {; d
// complicated schedules.+ n: R3 e2 u' k& m8 P3 v5 ~
. j- Z) @) P6 w$ G' x modelSchedule = new ScheduleImpl (getZone (), 1);6 N/ E3 n8 }2 s6 N1 \2 l- l; n0 [
modelSchedule.at$createAction (0, modelActions);
: C+ F4 K, u$ `. \5 W) E% f+ G
3 Q! m% n6 C2 @ return this;
& d* Z% u6 H. z } |