HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( e0 C0 k$ W* Z9 q. x4 @1 M( e$ n+ V8 ^3 v
public Object buildActions () {2 u8 P& [1 o' v: O- |
super.buildActions();
$ S( _- L) ]8 P' U! m
0 P& x; i$ `5 A3 U. I" v: G7 t // Create the list of simulation actions. We put these in
+ U; @" A. Q% l% D: O) D // an action group, because we want these actions to be! ?' z4 x1 j7 z" K
// executed in a specific order, but these steps should
$ @/ _1 U1 E( p! K# o: F0 k // take no (simulated) time. The M(foo) means "The message3 g" Q' I l! W
// called <foo>". You can send a message To a particular
) u& M- s6 g0 ?: U9 N8 c, m" | // object, or ForEach object in a collection.! D7 C, y% f! H: ?2 G
( T8 s. X6 K' n$ }/ Y
// Note we update the heatspace in two phases: first run% i7 |* L7 v. P @1 H" U0 N8 `
// diffusion, then run "updateWorld" to actually enact the
5 f' M, s$ o8 x3 J // changes the heatbugs have made. The ordering here is5 t2 ]0 ^. M+ ]3 E& ]' z# f
// significant!6 t- X1 }" \$ C% }1 b/ w% y1 U1 H
% s5 i s& p# }. n/ T- A: e( P // Note also, that with the additional
6 `+ K& _3 q" o6 w% J5 w9 g3 f // `randomizeHeatbugUpdateOrder' Boolean flag we can
{1 a9 a2 r J+ b+ P // randomize the order in which the bugs actually run8 r7 E; s. P. D) H& D2 o4 O
// their step rule. This has the effect of removing any
% f; T! U( y1 ~, | // systematic bias in the iteration throught the heatbug! J; i B$ }3 d D# |
// list from timestep to timestep
* N* c6 y, ^+ M, d$ a/ b# k5 ~ ( O, k) P9 t: C" ~! @5 B& K/ v9 [
// By default, all `createActionForEach' modelActions have
* t$ |7 O( t% r. p5 ]. @" m // a default order of `Sequential', which means that the0 ]& u, C1 t6 Y" q
// order of iteration through the `heatbugList' will be* T J8 i+ {8 r2 `% J( X4 l, _ j5 l
// identical (assuming the list order is not changed, U, m ?( K8 L% F t7 v
// indirectly by some other process).
9 m$ n/ t# e8 q 9 _7 V ^" r! w
modelActions = new ActionGroupImpl (getZone ());$ ?7 X& p/ I+ V L5 w, K. i+ F
! C" g+ O& m5 ]6 F4 D, I7 L: Z% F
try {
: L T" z8 B% o& P7 j; n modelActions.createActionTo$message
' `; q) F! a' I0 B (heat, new Selector (heat.getClass (), "stepRule", false));
: N, k: y1 }! V/ V' i/ U } catch (Exception e) {
& R3 G% ?. [6 p# E" K System.err.println ("Exception stepRule: " + e.getMessage ());
8 n! o+ ~8 p6 C8 @: s. a& Z$ A" ~ }# A3 J7 C. b9 [1 D# G
* n' I5 P6 ?% k try {" D5 }! z. V+ r0 r' U8 G2 Y
Heatbug proto = (Heatbug) heatbugList.get (0);
# v2 O5 r4 N; y2 i5 N, {& Q Selector sel =
; J/ G' P5 V2 L new Selector (proto.getClass (), "heatbugStep", false);0 b$ p+ \7 y% H9 A3 d9 y, \
actionForEach =: ?! B% G* j9 L' Q) h
modelActions.createFActionForEachHomogeneous$call
. w8 q4 J$ {# r- I (heatbugList,
; i r V1 W% l9 T0 r: |% _ new FCallImpl (this, proto, sel,
2 M, k% o' k$ r1 M new FArgumentsImpl (this, sel)));) m- m. E0 X/ l9 ^
} catch (Exception e) {
1 h* q$ |5 V& O* e. G e.printStackTrace (System.err);$ v3 ?; \2 Z9 I' k7 ^
}& F8 n E' c# K- r' b0 Q
. A8 V/ F) [, j( s( u: s
syncUpdateOrder ();
7 z/ L1 q: _" a9 h" P' [( L. `0 Q/ y' D7 n$ S
try {* E1 o! x6 k; o1 S4 L i: w1 {) H
modelActions.createActionTo$message
+ X; E1 l: ]" X (heat, new Selector (heat.getClass (), "updateLattice", false));
) [; E) n; p9 O% w5 l* ~5 L4 c } catch (Exception e) {
2 U; E+ W0 f. i& c System.err.println("Exception updateLattice: " + e.getMessage ());, F c: Y. {% O5 A- i$ p( X
}, f/ P1 A+ M5 z" C& E9 m0 x. `
7 O, A* _3 ?6 j0 o# P3 h5 H+ ]
// Then we create a schedule that executes the
& S' w4 `3 t0 F // modelActions. modelActions is an ActionGroup, by itself it- A8 l- x, O5 t5 ]9 R
// has no notion of time. In order to have it executed in
$ Y6 \: ?& v/ i // time, we create a Schedule that says to use the! B/ K8 |: C6 [! }1 e0 t+ L
// modelActions ActionGroup at particular times. This. L, t; r3 j0 R! [
// schedule has a repeat interval of 1, it will loop every
1 k* Z$ k0 c+ ]& k3 j; e, n // time step. The action is executed at time 0 relative to! {+ M _' h7 X$ z7 E
// the beginning of the loop.
) f# I2 E8 t( U4 ~9 [- u1 g. s. ]' }% s
// This is a simple schedule, with only one action that is
2 }" g, p4 r/ D! T' Z/ I+ E // just repeated every time. See jmousetrap for more) Z0 k9 B0 a' ` i( y
// complicated schedules.
# i; ]! c/ H! G* _$ G/ K3 H e/ V+ _/ P6 _: u! W. B
modelSchedule = new ScheduleImpl (getZone (), 1);
* r# f' p& {; K9 y3 [ modelSchedule.at$createAction (0, modelActions);
, i( }7 R7 k; Z* a5 g" k
& n; L& V4 ^( O- _4 s return this;: v* e: W9 n) u$ ^
} |