HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 X: s; \# C% `/ e6 @7 i6 b4 c9 c- J4 k- M; ]
public Object buildActions () {4 L* n9 i# x( Q
super.buildActions(); m/ [) N% @7 I8 o! }9 u8 k
) |: q: {2 q8 H# e) {
// Create the list of simulation actions. We put these in4 ?+ Q8 E. {& x5 A
// an action group, because we want these actions to be0 l/ t M1 B, P3 K3 `: R% k
// executed in a specific order, but these steps should- h+ B- L4 F& Y
// take no (simulated) time. The M(foo) means "The message7 [. {1 M8 X8 f9 A$ d3 z0 a+ N
// called <foo>". You can send a message To a particular" s! q: @ J% I
// object, or ForEach object in a collection.3 {: @0 s, o1 f4 e' e# X* r
3 M( d4 _. z$ O) r1 M) G* g3 J5 M% L // Note we update the heatspace in two phases: first run, E0 N: Q8 r& g
// diffusion, then run "updateWorld" to actually enact the H) I0 ?: k1 H7 i4 \
// changes the heatbugs have made. The ordering here is8 Z/ `) J }, q/ S$ I0 S; T
// significant!
& \3 q$ s* J4 @8 Z w& R, r u# W 4 T q% T/ B( f# _: J
// Note also, that with the additional+ Q: l! f" `+ E6 G2 u/ L. [
// `randomizeHeatbugUpdateOrder' Boolean flag we can" X, a/ B9 x9 F8 F5 V1 l: p6 D @/ q6 ~
// randomize the order in which the bugs actually run
% n# j2 o/ j, f# z" @4 R- u // their step rule. This has the effect of removing any
% v3 s; o- W* J' u( y3 O // systematic bias in the iteration throught the heatbug
. w1 K( S+ k2 E9 P5 v // list from timestep to timestep
9 a& l, K/ f" y+ F y1 D6 a . {" m- h3 F- y9 B% J" Z
// By default, all `createActionForEach' modelActions have
% a( ~2 A# Q( t& ~+ e- {' F9 r // a default order of `Sequential', which means that the, J0 `+ e1 c) G' u8 c) j
// order of iteration through the `heatbugList' will be
/ S$ q* @' }+ m* w$ z // identical (assuming the list order is not changed' d, T3 E# @! ^' G3 m
// indirectly by some other process).: Y6 o* P1 ?% V" F& M! e
- X' k5 I0 Z" O/ M/ a modelActions = new ActionGroupImpl (getZone ());0 L9 a- C) d1 H# f
5 M; z |6 ]) T5 L, S try {
% T/ C+ S3 q8 |/ y. E; v* f modelActions.createActionTo$message7 m9 _- p+ N% K4 O
(heat, new Selector (heat.getClass (), "stepRule", false));
- E0 _4 N$ }. t1 P+ c } catch (Exception e) {2 ?& l) F! W2 r+ J6 ]
System.err.println ("Exception stepRule: " + e.getMessage ());
3 v6 F& L$ ]! M6 G' b1 F. L9 n }
5 r7 U4 p9 o5 P. ]9 p2 H6 F. [# _ J( N6 _0 G# a
try {+ f g" ^! T0 x2 w
Heatbug proto = (Heatbug) heatbugList.get (0);% p J, S& j: f5 v9 G" X
Selector sel = 9 N- |$ G) K0 d0 n- \
new Selector (proto.getClass (), "heatbugStep", false);
5 |4 q( D0 O, x. W( j actionForEach =$ X( Y" d: z- X1 F1 J1 U
modelActions.createFActionForEachHomogeneous$call
- t" p- @5 f0 d" ^- y; U (heatbugList,; r9 A+ K6 g6 w! J4 _7 V( U
new FCallImpl (this, proto, sel,
& y8 v7 L7 Q$ [5 K& ~$ I new FArgumentsImpl (this, sel)));
2 s' s7 w' m( L _ } catch (Exception e) {
9 A5 M4 K# o/ u5 p$ g/ ` e.printStackTrace (System.err);) A% ]5 P7 q9 o9 }2 u+ y' D3 g6 |- w
}: }: }- n- N& k( v& v. C8 h$ K- Q; h
m' u" c) X1 ?0 B1 f
syncUpdateOrder ();
* c- s' b* L2 K! \6 z+ E. |5 X4 l Z% n2 G; h9 z) \" I! N
try {
7 e/ Y: p4 g0 O* ~. d% E$ } modelActions.createActionTo$message
' v1 c1 \. Y. ], T( @5 c (heat, new Selector (heat.getClass (), "updateLattice", false));/ V% \ w+ C- N- e8 g* B I
} catch (Exception e) {
( S# k( Q2 V' k7 y) @- z System.err.println("Exception updateLattice: " + e.getMessage ());" ~" A. `' @8 |2 G
}
9 r( I1 K2 ?! @" p! O1 h 5 l& m( T3 b! M ^
// Then we create a schedule that executes the# B- N7 u0 X1 V1 R7 B6 I2 r l7 Y
// modelActions. modelActions is an ActionGroup, by itself it3 ^& M3 |3 G8 k0 T! |0 Z8 C" W8 k3 Q
// has no notion of time. In order to have it executed in
+ K: f/ }/ L- t // time, we create a Schedule that says to use the
0 T! \1 u. J/ S // modelActions ActionGroup at particular times. This. J& d! ~- ]/ _3 P8 p9 f4 z9 b
// schedule has a repeat interval of 1, it will loop every
! R: G1 Z$ p- m& i k( ~/ ? // time step. The action is executed at time 0 relative to1 b: D! @# {1 F. J2 ^0 T: z& I( P
// the beginning of the loop./ d4 o h. e) t; M N" [$ Y/ } L
4 @/ v% y1 ?5 x( s3 y& V) r // This is a simple schedule, with only one action that is
4 z, K$ q7 `8 c0 o) L // just repeated every time. See jmousetrap for more
" l: I3 D: R( [5 z: A // complicated schedules.
4 `, U5 ~1 w. e; O% L0 N- r
& u# ~" S7 ]& m' u: Q b6 |8 \ modelSchedule = new ScheduleImpl (getZone (), 1);* c8 H1 N1 }7 g7 G5 z
modelSchedule.at$createAction (0, modelActions);! ^6 U0 @2 i/ c" _) R
$ P' _ e6 h1 f# S return this;: z6 o7 A7 e, t. \
} |