HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 R) x% Y% v6 U% \7 b) n6 e. o/ h9 b7 a! k
public Object buildActions () {/ P& ~& k3 V$ {. W) |) Q$ a% {
super.buildActions();6 d% ]2 g3 c P8 i u5 T
# @: Z$ }( t' s! n& e // Create the list of simulation actions. We put these in3 Y( b9 [" M# B9 y! k
// an action group, because we want these actions to be! z- ]; p/ s2 k3 I% X8 C! X& e' o
// executed in a specific order, but these steps should; ^8 a# _0 W2 V( m, |
// take no (simulated) time. The M(foo) means "The message: J" B' E3 Q, j9 T
// called <foo>". You can send a message To a particular
. A0 b) g: @% x // object, or ForEach object in a collection.
5 ^0 }& ` V) W$ `; O
# r5 c; t9 P$ ?5 T3 j4 f // Note we update the heatspace in two phases: first run
4 h1 T& H+ i. r4 x# L' \* N: c // diffusion, then run "updateWorld" to actually enact the
: s* E1 p, V5 v1 a" c // changes the heatbugs have made. The ordering here is& p. a) U$ U4 X+ z. I/ o8 D
// significant!
' u1 `$ a7 v3 |
* n6 p% w* ?$ R; Y$ m+ `0 k- E4 ^, e // Note also, that with the additional9 ^' E' s# Y' _8 v: S* m5 T, D4 K, S
// `randomizeHeatbugUpdateOrder' Boolean flag we can4 I% `5 B% E4 o0 C- M; {2 ^# x& b6 n
// randomize the order in which the bugs actually run: v+ [& i1 \; |4 Y$ i8 @3 A
// their step rule. This has the effect of removing any+ N# a* ~* Y7 n! ]8 L
// systematic bias in the iteration throught the heatbug
" F7 P8 ?+ _2 d# g, X: z // list from timestep to timestep
, t, W4 c8 }+ m, K; N5 c2 t9 e 9 v+ S, }3 P8 i, F3 J+ |) {
// By default, all `createActionForEach' modelActions have
' f4 f: b7 S5 c$ Q6 G // a default order of `Sequential', which means that the
8 s( @! Y; ]. Z, x5 ~5 r // order of iteration through the `heatbugList' will be5 |) O- b! y% w3 j5 u0 q6 O% H
// identical (assuming the list order is not changed
/ l: J# g/ Q8 B: K% P // indirectly by some other process).
4 T/ F+ q# P- W6 Q
* X( \$ v' x% Z8 t0 S8 ]2 E modelActions = new ActionGroupImpl (getZone ());6 \& N" Q4 v! O9 e1 ^
' C; n: s! H+ U% d0 o try {; z+ Q5 y H$ b2 X) ]
modelActions.createActionTo$message( U1 {) g& g: M) Z; @/ _
(heat, new Selector (heat.getClass (), "stepRule", false));
# p* A3 ?. g# T8 ?4 o5 \( d } catch (Exception e) {6 r2 l9 |3 i) D; F' p+ S- V0 m
System.err.println ("Exception stepRule: " + e.getMessage ());
) y' [0 K& e6 W" i( W8 w0 Y }7 N ]8 z, t r9 l s2 D3 y
: P C' n, s# j0 H- m
try {# }% r" l2 \6 R) @* |9 w
Heatbug proto = (Heatbug) heatbugList.get (0);
! b9 h) i8 a4 P; j Selector sel =
: k) z8 o/ ]" ]3 f1 j1 f' U new Selector (proto.getClass (), "heatbugStep", false);
3 [/ T$ @. a7 I% W& i# ? actionForEach =" m4 e9 a7 \( a0 ?. K
modelActions.createFActionForEachHomogeneous$call6 H' m7 s5 X% ^
(heatbugList,$ e- p- r I" g: F6 r
new FCallImpl (this, proto, sel,
. X% c/ [& }/ h, E% D: f4 d# T new FArgumentsImpl (this, sel)));9 K9 ^: v* q. R5 I; w3 N3 b/ }# q
} catch (Exception e) {2 T8 `# V" A6 b! q
e.printStackTrace (System.err);7 E+ G% W& O7 K, a7 y
}
. E9 B! X& Z3 T, n5 R0 x
$ I. n2 P1 a6 ?# O( a syncUpdateOrder ();% [$ ~% q- g. N" p+ a! h: d: C5 s( _
# v! W. {6 U2 p+ Q" |3 [ try {
! [/ R& Z5 d1 o% _$ {/ |7 p2 ^ modelActions.createActionTo$message
t% d7 z0 [. M' @ (heat, new Selector (heat.getClass (), "updateLattice", false));+ ~) w: _% {) Z' r6 f( W
} catch (Exception e) {6 }& C) M: l) c, \. B
System.err.println("Exception updateLattice: " + e.getMessage ());
0 x* N" w7 p) p- c }4 D+ N5 ]8 A& z6 o
* [5 R9 B3 N4 D- C! I% r d // Then we create a schedule that executes the
/ B' t& e# G2 I1 Q // modelActions. modelActions is an ActionGroup, by itself it
% V9 z. k* {, Y6 S! E // has no notion of time. In order to have it executed in* n3 E; X5 R3 Z9 A2 n; F
// time, we create a Schedule that says to use the
/ A% T, A5 u ?. l& h7 S7 F: H1 ]7 u" h // modelActions ActionGroup at particular times. This% q" M. a. j: h( \
// schedule has a repeat interval of 1, it will loop every% A9 `' t o+ w: ?; W7 @! p+ F! w- |
// time step. The action is executed at time 0 relative to- }+ t0 w+ e! @4 V$ |3 J9 {- D
// the beginning of the loop.
6 @+ m- S# x/ \! x2 z
: H. t8 A! @: z( Q // This is a simple schedule, with only one action that is! b% m7 g& }3 x+ c
// just repeated every time. See jmousetrap for more
! ?. v7 T7 `; p$ x6 ? // complicated schedules.
$ k! D2 t7 ?7 b " O' w, d* l% a" A# U' E/ z' k
modelSchedule = new ScheduleImpl (getZone (), 1);
: \6 U, C5 ^2 |$ y7 Z: l1 p7 P6 ] modelSchedule.at$createAction (0, modelActions);
- \" @0 V; V+ h4 X" a' L
" @9 R% f4 t( m: ] return this;- a6 l) {- }8 b& M/ l
} |