HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* q, E# }+ w7 ^6 m+ D3 p( }
! P; _' X/ o2 G c$ X) j2 \6 n, }
public Object buildActions () {
* L& p3 W+ w8 `: n' e1 E- [! F super.buildActions();6 d, f6 R2 N6 A& I
( O7 F5 l# r U9 n; E. H3 P
// Create the list of simulation actions. We put these in' A9 ?: [( M1 Y5 Z) _% N4 ]4 U
// an action group, because we want these actions to be0 a' X2 G: J. ^. Z
// executed in a specific order, but these steps should' r- g3 P, N7 I. q
// take no (simulated) time. The M(foo) means "The message
# j: `1 O8 t0 L! |4 V, }5 S. q! T // called <foo>". You can send a message To a particular
: i. p/ ]2 c0 G6 O; A* _0 a* W // object, or ForEach object in a collection.
4 `2 N9 J3 g7 D9 v2 K % t8 ? b$ y( i: A. ?) m" f( x4 s
// Note we update the heatspace in two phases: first run
' u5 K" R6 V: L0 [+ f // diffusion, then run "updateWorld" to actually enact the& E1 i( L7 o7 X" E
// changes the heatbugs have made. The ordering here is2 p0 a( ?/ W* J$ [& y5 p
// significant!
0 e3 ^1 x* o$ K) Y* j9 p ! x, |7 P$ t) o
// Note also, that with the additional
' t2 [' h9 j i( M& u H3 i // `randomizeHeatbugUpdateOrder' Boolean flag we can
, p+ q! Q8 V: w$ l) O$ ^ // randomize the order in which the bugs actually run0 I2 s7 E* C1 s, Z
// their step rule. This has the effect of removing any) N7 K9 W( D" m: g' c/ o5 M8 s8 s
// systematic bias in the iteration throught the heatbug
7 L3 _0 i8 c8 { // list from timestep to timestep
$ P* O% ^2 r: ]8 `$ m) D8 q7 i" I# i
& D, q1 ~% L H/ \* J; C // By default, all `createActionForEach' modelActions have! M* B9 ~9 k& E; c8 W6 Q# N w
// a default order of `Sequential', which means that the
8 [. T4 {1 C1 [ // order of iteration through the `heatbugList' will be' D7 t6 M; c* g$ d2 Q! K6 a6 z
// identical (assuming the list order is not changed# C6 `* K# o. a) @
// indirectly by some other process).& ]( c7 x! c& h4 R
0 W/ o, R! G( r* @& f
modelActions = new ActionGroupImpl (getZone ());4 g) O+ w/ X6 }7 n
' z# y' i3 o$ }3 ]
try {6 v$ X2 E& S: L8 Z6 t
modelActions.createActionTo$message
. Q8 p7 I% k* _4 s; z& c( F! B (heat, new Selector (heat.getClass (), "stepRule", false));
$ m2 C: |2 R% T( e! t } catch (Exception e) {
$ p& X. j8 X: V% v' h System.err.println ("Exception stepRule: " + e.getMessage ());
2 N( T3 `! T# Z+ g4 Y- Q; O }+ D5 i9 z; r. z8 O7 y
+ l. z1 N# Q5 d! g; A9 N5 G try {0 ?8 _) g. V' D# ^( ?; G
Heatbug proto = (Heatbug) heatbugList.get (0);
7 h ^( P! ] y2 L D Selector sel =
' W/ l- H3 h& s' ? new Selector (proto.getClass (), "heatbugStep", false);9 [+ X6 B& U4 X2 k2 I3 O
actionForEach =
. ^- B3 O# f# T7 }2 P8 e2 z modelActions.createFActionForEachHomogeneous$call2 S1 b7 _& I9 j' u; H& {& j
(heatbugList,
3 \, \, k3 _1 O: h new FCallImpl (this, proto, sel," R9 H3 j! `% [& r1 Q
new FArgumentsImpl (this, sel)));
& i' A( l" P p6 \% S1 O } catch (Exception e) {; y) m9 m8 j0 Q" L g2 C6 ]
e.printStackTrace (System.err);
; z8 l( X: w2 D }6 L# ] b/ F0 k3 q2 o# h. B( U$ h
# `$ J3 z8 X: j- h, C& C4 L) Y syncUpdateOrder ();2 _8 C% v8 f2 l. l, X, G1 l
! \- f, i8 B% F6 C try {
7 q4 \6 l0 f# { modelActions.createActionTo$message
* c1 ?' i' k4 }/ Z' i& R (heat, new Selector (heat.getClass (), "updateLattice", false));
+ S; r E. a$ J# [- l0 g( Z } catch (Exception e) {
" y9 {( ?" _2 _+ T0 a, f System.err.println("Exception updateLattice: " + e.getMessage ());
/ U8 {3 ?% R" O }
/ u6 @+ U7 t0 n; L, ?: u" @ . J ~& z1 L" F
// Then we create a schedule that executes the3 s- s. s' |! Z8 S1 y
// modelActions. modelActions is an ActionGroup, by itself it7 w4 w# L U# h( o
// has no notion of time. In order to have it executed in8 j, u7 c0 v" T+ F; ^
// time, we create a Schedule that says to use the* S) Y! F% x) d# M- {7 e: x( f: P# K
// modelActions ActionGroup at particular times. This( v; N8 U. V- E
// schedule has a repeat interval of 1, it will loop every
) l* n# n. |2 f! e0 s$ n // time step. The action is executed at time 0 relative to
p9 q c5 x! I# f1 v+ ~3 ]$ W // the beginning of the loop.
+ P' r, x6 \' X/ `1 c2 G- f- D
2 A" ~4 Z4 D! L+ i! [- [ // This is a simple schedule, with only one action that is
! J- Y8 }3 M8 I2 H // just repeated every time. See jmousetrap for more$ ^. R# u; ^: ?8 K! U* \8 f
// complicated schedules.
# u- Z7 ]+ \# A2 ^8 m, v! e1 |$ L ) }4 M0 }( x/ g H) B1 R4 `4 @
modelSchedule = new ScheduleImpl (getZone (), 1);
F+ M$ ]1 ~5 n, o: H modelSchedule.at$createAction (0, modelActions);
; n9 t B3 [7 Q' M 1 Y/ x/ l# g. R+ w1 P+ p
return this;
6 \5 h) L8 ~1 U8 C } |