HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# x8 S9 S( Y7 A1 w& z
, a6 z) {+ l$ Y! T z+ a# t8 ^ public Object buildActions () {
1 W z# F( z$ q# t) { super.buildActions();
6 i) p+ w' z; e/ O' T# I- w' d
# z4 ^/ A0 h r* r0 K/ } // Create the list of simulation actions. We put these in. J {6 q% _# [
// an action group, because we want these actions to be. B* |- v, G8 G6 v! C: y' r
// executed in a specific order, but these steps should$ {+ N5 k% C Q1 }& A# _
// take no (simulated) time. The M(foo) means "The message9 Y# T) [# i7 p! i8 ?+ s2 @& Z
// called <foo>". You can send a message To a particular v. w9 q0 [/ B7 U6 \+ L* x
// object, or ForEach object in a collection.
: x4 X- Y# J8 m4 A
/ S/ A5 }: h5 q) V8 g# s& i // Note we update the heatspace in two phases: first run: n _& V" J& R8 G
// diffusion, then run "updateWorld" to actually enact the% d! Z- ~3 \% Y. u2 P) s1 Z2 A
// changes the heatbugs have made. The ordering here is' b" X5 z/ w) q, D* z' o
// significant!
! M, d6 `% ]" C
( K4 p& A' b+ U; A0 m; R // Note also, that with the additional
' A7 E- e# O/ V // `randomizeHeatbugUpdateOrder' Boolean flag we can0 C- o" L( o, G- V3 d
// randomize the order in which the bugs actually run
9 ^7 K# w! k# t1 A: k& y. W // their step rule. This has the effect of removing any8 m5 b% H% q6 ]3 {! p2 F
// systematic bias in the iteration throught the heatbug
- p8 A# n4 |6 Z // list from timestep to timestep
' O8 ~& g; x# `0 u ' b& x$ k( @: k8 d% N+ {3 u2 w, i
// By default, all `createActionForEach' modelActions have
! u8 }6 C* {- H7 x% a // a default order of `Sequential', which means that the
$ ^1 t# z# x' V+ i; y // order of iteration through the `heatbugList' will be! M- x1 R8 y$ s. C6 U7 [6 k
// identical (assuming the list order is not changed3 P2 e* _1 ~" o
// indirectly by some other process).( ^( U2 n& B/ y. G: |% u
: r& u# R, y1 |, _/ U1 K
modelActions = new ActionGroupImpl (getZone ());3 a* o& a: L {0 h' B
& n( z( E2 b2 f7 k# h1 d6 A, u6 }
try {
6 y6 `5 ~$ i' R g modelActions.createActionTo$message/ B4 x. S0 [& T4 x. ~7 O6 k, M
(heat, new Selector (heat.getClass (), "stepRule", false));
& n) Y5 y- p" U1 X! Y& D) t } catch (Exception e) {* B/ Z( f5 B4 s4 D( C- H6 {" o
System.err.println ("Exception stepRule: " + e.getMessage ());& F+ L/ O" P$ r& {$ M+ M+ X
}
0 V0 u* w( ^ l- m6 C$ l( W& u8 Z3 w! k8 @( a
try {: U* a! L2 Q- J; |
Heatbug proto = (Heatbug) heatbugList.get (0);/ v3 [( H# y( Y; t+ A2 `4 I
Selector sel = + W( _# T! Z/ G9 b0 s
new Selector (proto.getClass (), "heatbugStep", false);
! H4 ^; D, M0 ]: y9 c* m actionForEach =
( Q( g3 d& O }: Y0 ~4 T modelActions.createFActionForEachHomogeneous$call2 Q% J* c0 u% F9 T0 ], I' I
(heatbugList,
6 m$ F- ^0 a6 _( v; t+ ^" h new FCallImpl (this, proto, sel,) f$ g4 T& z( D% f s% S
new FArgumentsImpl (this, sel)));
. Q, M- j* b0 Y! d } catch (Exception e) {
0 }9 a# E* w5 `2 B# F e.printStackTrace (System.err);( b' A- k0 |" H8 l8 T. }
}
" X+ V3 l! W& v) ] 9 e9 w) v" f* n0 _+ R
syncUpdateOrder ();
3 k( Y' e2 d2 T7 _/ ^+ t
& h. v% E3 L( _ }5 i try {# m5 G, d; J( D! [
modelActions.createActionTo$message
4 k2 N' _7 u3 L" ^, J" H; H! I (heat, new Selector (heat.getClass (), "updateLattice", false));
( U8 V( \7 B" e* V2 w, g M6 m } catch (Exception e) {
' C/ v3 u4 I2 J0 g9 d System.err.println("Exception updateLattice: " + e.getMessage ());3 K' h: m# N* Q1 Q
}2 C6 y$ W4 d1 ?2 D- a# @* p
9 F% l0 ~* ?# s/ _
// Then we create a schedule that executes the. }% n* Q9 ?5 p7 R7 t) O8 R8 m
// modelActions. modelActions is an ActionGroup, by itself it, L, d7 ^9 E! ^3 f$ W$ x
// has no notion of time. In order to have it executed in6 x- O) g/ ~" R$ c! r
// time, we create a Schedule that says to use the
8 M. |. K) H/ r; P& v+ b // modelActions ActionGroup at particular times. This, G5 R8 [% T- g `+ S/ Q( Q. D/ m
// schedule has a repeat interval of 1, it will loop every
6 r! r3 d( t- U9 l // time step. The action is executed at time 0 relative to
6 E! i' m- s& P // the beginning of the loop.( I+ D0 `( t; v3 f
% M( H0 I) a; v4 N. v% ^+ E1 y
// This is a simple schedule, with only one action that is
2 o6 R W# H; E; g4 ]" q A // just repeated every time. See jmousetrap for more
; U2 d M8 c- C* h6 F v // complicated schedules.0 @8 ~; Z8 D+ ]1 I
! B% L4 t$ g. F8 o' x( p: C
modelSchedule = new ScheduleImpl (getZone (), 1);
/ t1 L8 P& N, I3 f$ p' R. u0 [2 @ modelSchedule.at$createAction (0, modelActions);9 O0 _: U' m* B9 r3 o$ \
2 ~. ]+ B2 L+ K$ c$ ~' J% j+ ~4 R return this;
q. w/ ^; q/ F& _. t8 r# x8 r } |