HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 x6 }1 W {7 O+ E/ t3 J
! m6 D. r6 O5 S7 Q C b
public Object buildActions () {: u: I; C. U, b7 _3 j' Y M
super.buildActions();
) u1 u; D: Y# k
) C+ Y/ ~, I+ }0 e: M. S6 |2 ]. \ // Create the list of simulation actions. We put these in5 a8 x8 q7 r' g& H# W; q: Q
// an action group, because we want these actions to be2 s1 g! v( V# i$ ]# N5 n2 M N
// executed in a specific order, but these steps should" S. _3 m: O& h5 c0 I
// take no (simulated) time. The M(foo) means "The message
3 F s4 Y8 |& g5 k- l- } // called <foo>". You can send a message To a particular
" z/ |% ^* ^6 e; w% B* S. J6 D // object, or ForEach object in a collection.! _7 [( Q) l! ]; ^. g* ]
2 i6 c& R. l( I! X // Note we update the heatspace in two phases: first run
" r, P# q' B4 B& I // diffusion, then run "updateWorld" to actually enact the% b& B% \' D( ^' j9 p
// changes the heatbugs have made. The ordering here is5 X& U' G& J( B" b/ i5 q6 D# d
// significant!
# X T3 C9 ~( ~$ T6 D) r; W6 l+ J 6 B* D) j" @1 {
// Note also, that with the additional7 k( b0 Q, f' B* X2 k! Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
- @; x; w0 e7 w. [1 m: Q& o // randomize the order in which the bugs actually run' p' p! S! H3 k4 v& w
// their step rule. This has the effect of removing any% t# c# g* X9 y0 E+ l- c4 H
// systematic bias in the iteration throught the heatbug
' g) z" y7 @. ^* Y // list from timestep to timestep
6 y; D2 Y. x, Z2 D N1 J2 f
/ Y. T! T0 t% k3 x! ` // By default, all `createActionForEach' modelActions have
/ Z/ O& I) p7 B. h3 k! f& e: d // a default order of `Sequential', which means that the9 z9 o% F% ~2 G# l' v1 s' `6 j, }
// order of iteration through the `heatbugList' will be' f O! J8 y; y% F) K
// identical (assuming the list order is not changed1 J- K, K; C( E+ j! I
// indirectly by some other process).4 O; K) C( z1 l3 V! p8 f
9 L$ m( l5 Q* \ modelActions = new ActionGroupImpl (getZone ());5 R: Y6 P# e/ q* O, }4 f2 b+ x1 ?
$ ^; h+ k6 a0 B: M( o
try {- N9 G6 Q/ N, h' s8 u2 h9 l& k
modelActions.createActionTo$message
; }2 b. g6 B% H; u: q (heat, new Selector (heat.getClass (), "stepRule", false));
# E. z. `# i) Y, ]& r2 I } catch (Exception e) {4 |8 H) Q( U. E, E
System.err.println ("Exception stepRule: " + e.getMessage ());3 A2 u$ M; R7 k' v" B
}
- A( T% P g# X0 v0 Z/ V1 |) s
! {: k) y: q& E; Y! U. O1 h; W1 a try {
$ I( a- O2 } k5 ?/ G! p4 b Heatbug proto = (Heatbug) heatbugList.get (0);
~$ x+ z/ E2 h5 m Selector sel = ) E" W( X" X( \* _1 |8 e0 s% @9 u
new Selector (proto.getClass (), "heatbugStep", false);; p7 ^# q) ?- k- B" _3 `
actionForEach =/ ], U9 D: n; x
modelActions.createFActionForEachHomogeneous$call: l) ?. O/ {! T/ P3 s
(heatbugList,
6 k0 [8 e# Y X0 R. N: l new FCallImpl (this, proto, sel,7 F7 Y8 j. D1 S2 s
new FArgumentsImpl (this, sel)));# A, O7 Q: D2 F+ u) f% w$ W
} catch (Exception e) {
4 n; d0 l, A! n& p6 Z e.printStackTrace (System.err);
$ U" ^* ^2 t& ]" ~( S }
! s9 }, D7 j6 v, A* ? % {) f7 T4 l O3 O
syncUpdateOrder ();
' j; t$ t- _# V; Z0 r$ Q. D1 r1 z9 w% g5 ?% {9 I: T% M% J4 q) m
try {! [3 t; a7 ?4 z8 ]+ n
modelActions.createActionTo$message 2 T: } c/ h& ~/ O
(heat, new Selector (heat.getClass (), "updateLattice", false));0 z! F! W. ?0 ?0 y$ J, n& q3 j& A
} catch (Exception e) {
# n) h* z- [; ]0 C; |, K! T. C' c System.err.println("Exception updateLattice: " + e.getMessage ());
% z3 M+ |: _; X" m8 G$ G }. |# @( Y3 j) Y4 B1 f# y9 U1 d
: I3 e2 W/ x# F/ M1 J* |+ D# Q8 L
// Then we create a schedule that executes the, i6 K/ n% [1 R: x# {) l
// modelActions. modelActions is an ActionGroup, by itself it" R% q% |( A, `
// has no notion of time. In order to have it executed in9 u) M: j& c9 N7 O
// time, we create a Schedule that says to use the% G5 |; R1 m) t$ B6 t
// modelActions ActionGroup at particular times. This
& ]! @7 C1 S$ V; I( H8 Q4 t // schedule has a repeat interval of 1, it will loop every: |$ F' _- j! s$ C/ `$ u
// time step. The action is executed at time 0 relative to
9 Z9 S7 D, Z# Q0 w2 P // the beginning of the loop.
# z) S7 I( \) Z% P! D7 B: ]/ | Y: E* w3 l- k( v
// This is a simple schedule, with only one action that is
; ]* s0 Z$ R% ~+ G // just repeated every time. See jmousetrap for more
& u# v0 n5 e# q7 G9 l // complicated schedules.
6 R: r* A4 ?4 h
3 f3 [9 Y: k: {& O4 x; p* _) L modelSchedule = new ScheduleImpl (getZone (), 1);
) h7 t; l G- ]9 ~# g. | modelSchedule.at$createAction (0, modelActions);, X3 z! D0 f* U* v3 U
4 x k: @" @. D return this;! U- n5 |7 E1 n2 I. k% ]" h/ i. w
} |