HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 @2 P+ K: d% L% S- }; G) |6 `: q* w4 j8 ^6 w9 F- m* u
public Object buildActions () {
3 ?! ]6 U/ c& z super.buildActions();
7 L) a9 Y$ n, y& V {9 B
- v `( }! I C- d5 ~ D // Create the list of simulation actions. We put these in
' H- v: O+ ^1 y // an action group, because we want these actions to be# @( o. x5 Q5 ]& P
// executed in a specific order, but these steps should1 y& }6 u8 F1 F% w
// take no (simulated) time. The M(foo) means "The message
( _& u; C" I6 c // called <foo>". You can send a message To a particular
u3 L2 t: F5 F! P& a3 { // object, or ForEach object in a collection.
, T# O5 h# [7 i3 k3 Y7 I ' p% S1 \! _# z5 a6 y
// Note we update the heatspace in two phases: first run \2 Q5 s# V, o% Z' K
// diffusion, then run "updateWorld" to actually enact the9 f$ c; c2 ?# b* y9 T% [7 U
// changes the heatbugs have made. The ordering here is
* ?, y5 N7 d- Z; b // significant!; S. P V& K8 i8 a
. W% R; |- S/ U4 L+ ]. f8 V+ \
// Note also, that with the additional! e: t' L- c4 c. F U- t) b
// `randomizeHeatbugUpdateOrder' Boolean flag we can3 C6 A8 a/ v* T; u6 X
// randomize the order in which the bugs actually run; S3 p) |5 {9 f
// their step rule. This has the effect of removing any4 M( b" z V; b H* P& l
// systematic bias in the iteration throught the heatbug
- w7 c1 u$ o. [0 w1 y // list from timestep to timestep4 p1 F; R) T, m
; d- T4 ^5 F6 w
// By default, all `createActionForEach' modelActions have& Q" U" p6 K J9 V& O d
// a default order of `Sequential', which means that the
: _, t- K6 O0 U5 G" O$ E4 L3 W& N // order of iteration through the `heatbugList' will be
# v0 a7 [9 h+ k. b // identical (assuming the list order is not changed) w) l. D$ c! Y! p: I
// indirectly by some other process).
" |: i( a! X! Z; ^; h$ n : C/ F2 h" q, E
modelActions = new ActionGroupImpl (getZone ());
: J+ p9 `3 H Q! |" ~5 q" Y2 ]' H5 P- r7 E: ^2 d5 E' N0 \
try {& h. B- V1 v3 F1 w5 }
modelActions.createActionTo$message: m! O7 o9 M) r$ T# \# `4 T
(heat, new Selector (heat.getClass (), "stepRule", false));
. c7 w4 H/ ] J' f; j$ C } catch (Exception e) {& T) F8 |% G$ Q" R: ?% V
System.err.println ("Exception stepRule: " + e.getMessage ());8 W+ _9 d- D1 A
}3 S/ |- j) B; _; M2 a
0 _( q+ x: X: Q4 L' n4 f
try {
( n6 |' {3 U, e. q! \ Heatbug proto = (Heatbug) heatbugList.get (0);+ J5 {2 T! }' A" P+ `" A
Selector sel =
7 H% g) h' V7 m, x3 N ` new Selector (proto.getClass (), "heatbugStep", false);, A8 T( k1 |/ K1 ~/ ~
actionForEach =
) o, v6 |# s4 X a( G modelActions.createFActionForEachHomogeneous$call
0 @) v# r$ i$ A% ~! {8 l (heatbugList,+ M# x2 h+ ?5 \/ _5 y; k
new FCallImpl (this, proto, sel,
7 |7 p6 O( p$ H# L, e: F% v7 i7 k new FArgumentsImpl (this, sel)));$ x! e+ O, ]# r4 {" A( V8 L
} catch (Exception e) {
1 A) a. r4 L: a e.printStackTrace (System.err);
. y$ u6 \- {3 J$ G9 g }
9 i; r6 T0 c: [1 `
4 W- H" ~" L$ C# M7 y syncUpdateOrder ();
& E" y, i1 c! F3 B' C2 u0 ^" y9 X5 a
try {
8 N3 Q, V9 Y1 `' F$ t' E modelActions.createActionTo$message
( ^( w8 `- I. V8 H) H/ x E/ E (heat, new Selector (heat.getClass (), "updateLattice", false)); ~5 v' `# \& Z
} catch (Exception e) {. ^( i' `1 @( d' X
System.err.println("Exception updateLattice: " + e.getMessage ());
1 I7 Z7 u* r# \8 F. N }& _. ~) {: u( I: z
" A) b" r, l- _ // Then we create a schedule that executes the
8 z% W& Y' t4 v H // modelActions. modelActions is an ActionGroup, by itself it# J: q& A& r& v
// has no notion of time. In order to have it executed in
$ E. }+ T6 y5 C9 x! e5 g // time, we create a Schedule that says to use the
( Q- @- _! O( ~- x4 ?8 h" A // modelActions ActionGroup at particular times. This, Q: i6 U! T, T- H; e
// schedule has a repeat interval of 1, it will loop every
j3 Y. h9 H+ T$ S1 O // time step. The action is executed at time 0 relative to
9 b* y4 `9 c1 O' f. { // the beginning of the loop.
. a' a7 d# _( b M- F( J R* t4 T4 |
// This is a simple schedule, with only one action that is# D5 R. i" N" ^+ k) }9 ^+ Q8 n: L' {
// just repeated every time. See jmousetrap for more+ h$ E9 A1 W3 y. v- d( v1 i1 i
// complicated schedules.
! ]. ~3 |7 ]& I) f% w3 ^6 N: l
2 J3 v9 C" U6 R. D& H' M, m& V modelSchedule = new ScheduleImpl (getZone (), 1);
9 g* {; z& t# ? P7 x# I modelSchedule.at$createAction (0, modelActions);! ?! ~: A5 G5 F# d! C
( T9 p1 l8 z6 L3 y return this;7 ~- N9 v& E* l6 l+ k( }" f
} |