HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 T. B0 c! W: N& |5 O2 W) j7 W" l/ A- |# q: ]9 W& e
public Object buildActions () {3 V' |0 [- i. v# }; V3 h! e
super.buildActions();
0 u3 x9 k5 F {, J4 Q* O4 R' s3 [
! i! t* O H7 T% _; V* | // Create the list of simulation actions. We put these in
: d1 D0 z3 r9 J4 g // an action group, because we want these actions to be0 W8 q" d, u S" ~7 N' B
// executed in a specific order, but these steps should
: y3 [5 l3 r( i9 }0 \( N$ t3 P // take no (simulated) time. The M(foo) means "The message/ b$ v; y- R! _" v: P# [
// called <foo>". You can send a message To a particular. @ ?: k1 i: K ?' a' P8 l
// object, or ForEach object in a collection." d2 I/ f& n, Z( x
( w1 c: d( I! f
// Note we update the heatspace in two phases: first run
9 ^+ _8 P0 z' L // diffusion, then run "updateWorld" to actually enact the
0 f( T) e1 q& Q S0 O8 X // changes the heatbugs have made. The ordering here is0 t$ y1 d E/ m
// significant!
& ^$ W4 w* S8 S0 s! D e H; @" Y1 _4 R* Y0 H+ U& `
// Note also, that with the additional4 U; U4 I3 ]' ~! ~+ v& d5 z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 o& x- e& ~. D6 }! D4 L // randomize the order in which the bugs actually run1 y/ z% [8 J+ J
// their step rule. This has the effect of removing any4 D+ ^8 P. ^2 y7 v0 E# p# p- A
// systematic bias in the iteration throught the heatbug% m4 m, |& b) C# \, e
// list from timestep to timestep/ t" ]: k. c0 Q4 D* g
, ], V5 `' g- V9 \* z* W5 V // By default, all `createActionForEach' modelActions have
- ^4 Z" C" C* E7 t7 D1 T4 e // a default order of `Sequential', which means that the
; y+ A" D" _: y, L& s5 r$ D3 @: q4 c/ @ // order of iteration through the `heatbugList' will be3 o9 P. e$ Y, P
// identical (assuming the list order is not changed# y, G6 o' d8 @5 l( R& C
// indirectly by some other process).: E' p, o! k& ^1 p6 H7 {
/ w( {' T' @5 ?% l- s. P% Q' o modelActions = new ActionGroupImpl (getZone ());0 x. f1 R0 [/ _) q
0 @0 s3 E3 G6 x9 u: a9 J try {+ ]& M" M2 l3 V- V4 I
modelActions.createActionTo$message5 v) M E2 m% L% \- V0 A
(heat, new Selector (heat.getClass (), "stepRule", false));. g2 l7 ^- x) C2 W' T: x' D
} catch (Exception e) {; i% T4 m2 f9 X
System.err.println ("Exception stepRule: " + e.getMessage ());
1 P/ m0 H5 T6 m }
9 J1 }! D1 @, t& D% Z; C c
; w# a$ e9 _; f try {
k6 y. k( h; @9 }. S+ ?. k Heatbug proto = (Heatbug) heatbugList.get (0);
! i% F2 c6 \2 p& K( H8 C Selector sel = 8 g5 @+ x0 I6 ^; U5 G; `
new Selector (proto.getClass (), "heatbugStep", false);4 a l, |" d- G' ~) D- ~: f. T- A- c
actionForEach =
7 q; ~: W$ e9 j, R! r/ u% t" ~! [ modelActions.createFActionForEachHomogeneous$call
. q7 n1 N4 H7 e- f; O+ W (heatbugList,
( b+ P/ R8 e7 k9 I) g# [ new FCallImpl (this, proto, sel,0 O& a$ I4 R. h& q) h) g/ n5 U! f4 D
new FArgumentsImpl (this, sel)));# s: g8 P1 r+ Z4 i1 n5 V
} catch (Exception e) {" b$ g. n4 o( e( q+ |2 b5 m
e.printStackTrace (System.err);% P9 [) ?7 }8 c- A6 Q$ s; J
}' l! L& H, R i
, j' Q1 |/ U: s' }) p$ e: z* J
syncUpdateOrder ();! V1 V* a o8 T2 W# w1 q# d
" ]5 p& x7 h; ~
try {2 \6 _! _- Z! p8 o
modelActions.createActionTo$message
& U4 @4 h6 D2 T7 [+ l1 J7 \$ ] (heat, new Selector (heat.getClass (), "updateLattice", false));1 ~6 z- ~ A3 r# v
} catch (Exception e) {
& ~$ I" V; S( g% \7 c System.err.println("Exception updateLattice: " + e.getMessage ());
' F. w7 |" d# s/ h }
: W) q/ \5 f" o
+ T& q( p5 c( b' U // Then we create a schedule that executes the+ f8 T3 c- V" {) f; T
// modelActions. modelActions is an ActionGroup, by itself it2 V5 j ^9 I4 t! Z- A+ |$ C# j3 n; k
// has no notion of time. In order to have it executed in
, J& h$ e: ]5 |* [8 G3 d5 m" F# s5 J // time, we create a Schedule that says to use the4 K/ N: N3 S' a5 X/ F( e
// modelActions ActionGroup at particular times. This
/ U7 z5 O8 @4 n" R! Z // schedule has a repeat interval of 1, it will loop every
2 w1 \. l$ ?( H6 b // time step. The action is executed at time 0 relative to& X: H# F7 k! Q" \. E
// the beginning of the loop.0 y% r) e4 G! j% ^) \
4 o8 x# Y p ^3 r7 e
// This is a simple schedule, with only one action that is
$ M, L5 ]0 W' n. a$ C! x // just repeated every time. See jmousetrap for more+ _2 A$ T7 I+ ]! G# t+ B
// complicated schedules.
5 E4 @% J/ |+ j# k: c * ]& W: D, b0 Q
modelSchedule = new ScheduleImpl (getZone (), 1);
' _( \2 r3 h8 _ modelSchedule.at$createAction (0, modelActions);
% k' E& \3 c! Y$ B7 Q9 Q4 x) I# X
/ }+ d6 f' W4 \5 n3 i! W2 ]- ? return this;. m" e7 J" x/ i6 E L# S
} |