HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 r. q" I" Z9 k; @4 d/ f
; O ~5 ]+ A8 C# K: j! D% b
public Object buildActions () {# f7 V* V- h4 v9 ^
super.buildActions();
) }& M0 F* c+ I o( e' `6 q2 y* I7 d J! \
// Create the list of simulation actions. We put these in
# [$ n6 G: y; Q: A, G // an action group, because we want these actions to be
0 P' O, V. A0 n // executed in a specific order, but these steps should
, @6 ?3 y+ s \ // take no (simulated) time. The M(foo) means "The message) H) Q |: C3 m O9 O
// called <foo>". You can send a message To a particular1 b2 u+ J9 D- u
// object, or ForEach object in a collection.) [+ v2 n/ P2 r7 t- T# ~
0 b2 I( u% r6 ?( o8 I: _$ N) Y // Note we update the heatspace in two phases: first run. M8 v7 {/ |$ @. M6 j
// diffusion, then run "updateWorld" to actually enact the4 Y& s1 |8 ^: B
// changes the heatbugs have made. The ordering here is8 b/ }2 f; w- j1 `4 b9 {( _# f7 z
// significant!# \3 [: ]' V6 K/ b4 l
8 d$ X7 L$ S/ A- @5 |! j: C! ]) R; w* g; z // Note also, that with the additional
) K* b7 k9 u: `! w+ k e: t // `randomizeHeatbugUpdateOrder' Boolean flag we can) k( X5 ?: ^% v1 U: q# O* Z$ C
// randomize the order in which the bugs actually run
7 \3 B; O2 p) C* S4 o0 w; e // their step rule. This has the effect of removing any
% o" c7 E% B9 C$ e& F/ X: p% z3 l: T // systematic bias in the iteration throught the heatbug
: d' ^- U5 c, c // list from timestep to timestep6 P& a7 N0 F6 U; P
& Y( j8 t' Q9 A# i: A
// By default, all `createActionForEach' modelActions have
) \( H5 E6 o+ n. F // a default order of `Sequential', which means that the
P! ~) g& r) C& l; C // order of iteration through the `heatbugList' will be
8 z: W" ~' h2 m: b$ S3 k q- A# m // identical (assuming the list order is not changed- V* }1 s' b. S+ h2 B8 i
// indirectly by some other process).9 Q0 G n! a4 t' y' B6 o: a% G
; p/ A2 j( g( Y3 I7 K5 k4 t! g" L
modelActions = new ActionGroupImpl (getZone ());
4 W @% d+ Q" B d. Q6 l8 y- D! ^6 X! X& K( T
try {" }8 _3 U3 ~0 [3 I) V0 L7 Z) @
modelActions.createActionTo$message
" Z# Y( m5 e0 I5 m; ^& d (heat, new Selector (heat.getClass (), "stepRule", false));
+ U) j. G1 K. L3 p! n' S3 x } catch (Exception e) {" R' |' J: q0 `9 Y/ b' X& v4 J
System.err.println ("Exception stepRule: " + e.getMessage ());2 H/ S! y5 @' a. W/ e& ~' B
}. e+ g6 K* z U$ z* D: o1 f2 U; m
- R( }& w! \. b( }; E5 X try { \2 ], a$ I& ~5 t8 c" ^* n8 }* M! Y
Heatbug proto = (Heatbug) heatbugList.get (0);
7 o% m, R7 r+ ]6 \7 W! } Selector sel = ) l( ?! W* O9 y1 _) E% t4 t$ h
new Selector (proto.getClass (), "heatbugStep", false); r8 L+ s3 K8 t+ Y" V
actionForEach =
$ F4 a& f" A( c. ^ modelActions.createFActionForEachHomogeneous$call& `9 b/ f3 U6 A d" `5 C
(heatbugList,& _0 }' `7 b7 H7 e# W% ]' S( f" t
new FCallImpl (this, proto, sel,
4 u$ G* m% q. r0 w7 ]* c% A7 X new FArgumentsImpl (this, sel)));
9 ^$ ^5 k6 u+ {2 T4 c' c } catch (Exception e) {2 N3 i& N4 C$ s
e.printStackTrace (System.err);
! z' p/ l/ N; M }
+ G1 v( g5 Z7 w/ E0 @ 2 F/ e* ^) f$ e
syncUpdateOrder ();* ~5 Y0 l4 \- e3 f# J. N. o
) j! U2 h5 X' h
try {6 a) `' Z D! J4 L
modelActions.createActionTo$message
. k4 Q1 v) h8 V: u8 ^+ M& W! ~5 @ (heat, new Selector (heat.getClass (), "updateLattice", false));' `! \- ^2 Y' g4 W. ~3 s" `
} catch (Exception e) {1 B2 q$ t0 G Y% A2 u2 ~" v
System.err.println("Exception updateLattice: " + e.getMessage ());8 B7 h$ i( Q. t" r5 Q" }4 Q ~9 Z O
}6 u+ f8 V( l$ K( G- p- W, V$ A
/ o; C( I+ }, }2 j9 Q* x' E# W$ O( H
// Then we create a schedule that executes the
4 n. `- F0 b9 L$ o5 ]# w. A // modelActions. modelActions is an ActionGroup, by itself it, K: ~* A `1 G% e
// has no notion of time. In order to have it executed in: @5 }' w) e& J% k; y: l! [1 `
// time, we create a Schedule that says to use the, X. F' f# |3 X( b
// modelActions ActionGroup at particular times. This
& z" G' U/ I O0 m // schedule has a repeat interval of 1, it will loop every
+ L6 C9 L& `7 l // time step. The action is executed at time 0 relative to
3 ]7 Q4 V5 S' E! a' b // the beginning of the loop.
1 `* ?; D, P1 J
; n; A0 Q0 L# h: m1 L0 ?8 W // This is a simple schedule, with only one action that is" M8 `9 s1 b' u0 `1 T4 u& T
// just repeated every time. See jmousetrap for more
& C, c! V- k! _! f // complicated schedules.% U2 t* ~* t N# F- J, H7 a
& m% J: D1 K. A9 A
modelSchedule = new ScheduleImpl (getZone (), 1);
- F/ |$ v+ A Z b" X O" ` modelSchedule.at$createAction (0, modelActions);
+ C& O) d/ ^+ F5 I4 s M
+ p& u& {$ D# T$ w0 C0 v return this;
# T0 H4 Y9 B9 R7 e+ O } |