HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" G$ ^/ w' z6 G* R" W/ S) R8 U* G8 {, Z6 a2 K
public Object buildActions () { D6 ]: W! J: Y6 C' [$ ]9 {
super.buildActions();
! M& j* h5 K& k1 S% ? x. Z: { ! @, R# @$ M4 T2 j0 Z2 p) W: Z3 V
// Create the list of simulation actions. We put these in
1 X/ W( M. v( Q' P o, o // an action group, because we want these actions to be4 G% { H7 f, t3 H
// executed in a specific order, but these steps should
( U& r2 M/ f8 M w# e# l // take no (simulated) time. The M(foo) means "The message
. F! z7 V2 p0 _+ k- R+ J) ? // called <foo>". You can send a message To a particular( P2 k1 ~ q/ E& F, Y
// object, or ForEach object in a collection.
* |' [: _) K3 R0 T0 n5 V4 r
4 W8 U- z5 W2 k$ c // Note we update the heatspace in two phases: first run. H; B0 j$ e9 R9 O
// diffusion, then run "updateWorld" to actually enact the
4 {( Z) C6 y H- I3 A // changes the heatbugs have made. The ordering here is2 u0 _+ I; d7 h, W9 n3 i
// significant!
5 n s. H7 o& H* b$ m& B
. N: S4 v* Q) E# L1 \ // Note also, that with the additional
' ~0 p9 Y/ T! Q/ G1 U7 _ // `randomizeHeatbugUpdateOrder' Boolean flag we can( p o% a; z' G" O$ ~0 _
// randomize the order in which the bugs actually run
( v% A" _9 v, D* h& O9 K // their step rule. This has the effect of removing any
3 `2 I# Q; o2 p& x _8 U // systematic bias in the iteration throught the heatbug
; f+ l. h8 _2 F // list from timestep to timestep4 X1 @; m! \8 z* ?+ P
: |, ?5 Z" y+ f }$ P; ^- i // By default, all `createActionForEach' modelActions have
, g" m. v0 Q7 R q // a default order of `Sequential', which means that the, N( B) d' }. l: w. u
// order of iteration through the `heatbugList' will be
* }7 J3 _& K8 l- F! C& k // identical (assuming the list order is not changed3 q6 Y3 c9 s# f) q8 a3 K
// indirectly by some other process).
. W, ?% l/ c9 k8 Q9 d) n
) n0 ]& q) \+ E0 @; m: [9 h; q modelActions = new ActionGroupImpl (getZone ());0 y2 l( K- T' \: [6 b- t5 @
4 O% n, t5 S; I6 s* h+ e: k% K
try {
H1 }; `, b" H% }2 Z modelActions.createActionTo$message7 [, x9 p8 @4 z4 _
(heat, new Selector (heat.getClass (), "stepRule", false));
! J- N0 Q9 g! U7 ~( J: n } catch (Exception e) {
% S$ p- y$ x* N& Q7 a- s h System.err.println ("Exception stepRule: " + e.getMessage ());& J- u5 h9 p- `
}3 h$ _7 o9 q% N0 F7 i
: }) J9 s. c) M, y/ U
try {) P5 C4 p# J9 H& ?. h6 \
Heatbug proto = (Heatbug) heatbugList.get (0);' K9 S) r$ E: ]6 c3 C$ k3 Q5 K
Selector sel =
3 u- O7 s/ `4 d h; E new Selector (proto.getClass (), "heatbugStep", false);
; m0 `; c. X* P: n/ O actionForEach =# o7 [2 F8 \# G4 M* g- X6 ^
modelActions.createFActionForEachHomogeneous$call
3 W( u5 `: A& i; W! @ Y1 c6 N (heatbugList,5 Q. q" N+ V9 N) N7 k! g
new FCallImpl (this, proto, sel,
. ]# D) m1 E* k' g- `+ K R' v new FArgumentsImpl (this, sel)));0 \" x \8 {+ H `8 c
} catch (Exception e) {' t" b1 }+ u, O' Y/ T3 v4 J. P
e.printStackTrace (System.err);
" c! t& Y! b/ Q0 O- y }6 g1 y# i% b0 ~
8 `& E2 F" N. w5 G: u$ ]
syncUpdateOrder ();
2 p8 l# H# h8 m) F5 J8 t( @! F) c- E5 p+ W' e3 x0 o
try {
9 q0 o. o0 o9 S* d9 L# S modelActions.createActionTo$message
4 V, H7 X% d" U9 ^: S) a4 F5 C) e5 c (heat, new Selector (heat.getClass (), "updateLattice", false));
$ ^9 A1 h* ~" D8 C+ l } catch (Exception e) {: X+ a% C5 t. j/ y) j
System.err.println("Exception updateLattice: " + e.getMessage ());/ F/ i( U6 [* Z1 R1 K$ B( Y# {
}4 R9 Y5 r- `: g( q9 [4 L
+ N3 C" q* S% o' U& b* a7 O) u // Then we create a schedule that executes the
7 A( ]2 x9 {3 M! R; V% r // modelActions. modelActions is an ActionGroup, by itself it, M! g6 S7 z" l' x9 y5 b
// has no notion of time. In order to have it executed in) U% w6 Z! T- N9 @
// time, we create a Schedule that says to use the# N% C) k6 p: u
// modelActions ActionGroup at particular times. This- C( E+ v7 ]2 j, E) d% F
// schedule has a repeat interval of 1, it will loop every
# I( L5 i8 S( Q$ H3 I5 X* I // time step. The action is executed at time 0 relative to# s( [1 f* V2 U
// the beginning of the loop." r9 h$ Y+ a6 g. B+ ~4 k
6 K, d2 F1 {6 y4 l( e* f
// This is a simple schedule, with only one action that is7 C# U" _0 H1 g4 g2 S
// just repeated every time. See jmousetrap for more/ n( G; K" ]( R& ?" k) ], M
// complicated schedules.
- J+ S3 Q% x" O- t3 i/ i
3 E+ @' H e; r( D9 K modelSchedule = new ScheduleImpl (getZone (), 1);0 q8 H2 Y+ F: N7 u a
modelSchedule.at$createAction (0, modelActions);0 M+ ~; y3 \: N' v
3 Y& B1 R5 H k8 ^ return this;
$ E4 r- h* z9 I% L7 s$ l, a: W/ Q } |