HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 p7 J. f- t+ M$ J7 J$ t- A% E9 B5 b+ H0 X5 ]5 f3 J0 r
public Object buildActions () {
- X1 T' H( Z0 W* \5 V super.buildActions();
x1 A* K3 F/ X: U' x0 ~, m2 l
% H" W" ]' C( V. L) F6 n // Create the list of simulation actions. We put these in
5 w P) J8 `7 x, p6 \! |8 u // an action group, because we want these actions to be
! { d4 m: m V% ~* Q8 o) T // executed in a specific order, but these steps should+ i2 G" \# M5 c# |6 Y/ D/ V# {- O
// take no (simulated) time. The M(foo) means "The message. P/ q% j0 O8 I& Q) K* J8 R# w
// called <foo>". You can send a message To a particular( d" T( A2 @ C4 _& F
// object, or ForEach object in a collection.2 G. p8 d# h6 D- i
" `1 }2 Q4 z& ]) f$ }! D( \
// Note we update the heatspace in two phases: first run: Z3 O1 X& O9 q7 `( Q; S: Q* Y+ G8 x
// diffusion, then run "updateWorld" to actually enact the
8 A( w+ @* e& b0 y W) M5 { // changes the heatbugs have made. The ordering here is
+ w9 U f( G$ o8 Z6 y* |$ M. [ // significant!
. y9 o- K! `1 W* ] 8 \* Z* o1 |7 z/ [, U, X" N4 e
// Note also, that with the additional, S1 X0 {1 v6 t' U+ i# @
// `randomizeHeatbugUpdateOrder' Boolean flag we can# u# r# }( ~* \2 c/ B: }% R0 v
// randomize the order in which the bugs actually run8 Z. ]4 o" a" ~* K
// their step rule. This has the effect of removing any. T) c4 j6 y, f! x! O; B
// systematic bias in the iteration throught the heatbug
7 U0 L: s5 u3 [, @$ x // list from timestep to timestep
) v3 n! }- _4 v7 U0 Z 5 t; ~9 p* U& Q1 ] D* w
// By default, all `createActionForEach' modelActions have! G9 h+ y) }1 G' f0 d' S4 L
// a default order of `Sequential', which means that the3 I" M& Q: z3 z1 _
// order of iteration through the `heatbugList' will be
( J0 H" {+ d, m* }5 [7 ~ // identical (assuming the list order is not changed
# [8 K; o; l7 j! a! v( e // indirectly by some other process).6 y& G' W% }0 L" o
5 w, G L) i1 Y) W7 L
modelActions = new ActionGroupImpl (getZone ());8 l3 W1 @. l: J7 _4 z! `# y Z
! v0 l! J' x# ]9 H& Z- m7 f9 u6 G
try {
3 Z$ n' |" n- e7 l modelActions.createActionTo$message
# Z% y! z/ Z6 t s6 R% t (heat, new Selector (heat.getClass (), "stepRule", false));
, U" O- R+ l" G' q9 s } catch (Exception e) {+ Z$ q. I5 i& t+ v( x
System.err.println ("Exception stepRule: " + e.getMessage ());
0 G4 l* ~: E1 |% Z }. l2 b4 D( X* m4 ?8 h/ W+ g
% x( E8 U5 L" P' S try {
9 {5 b9 {. @5 x1 i% F, g# g5 G7 e Heatbug proto = (Heatbug) heatbugList.get (0);+ y4 R; N) o- ?
Selector sel = 2 R# Z- K4 K- k& a
new Selector (proto.getClass (), "heatbugStep", false);8 ^5 }5 n7 K* ?' h$ n( P4 [
actionForEach =
_ Y$ F( ^9 ~) e modelActions.createFActionForEachHomogeneous$call4 K# Z: }/ Y1 p
(heatbugList,6 @4 _# V8 u0 j5 t, P/ D# N
new FCallImpl (this, proto, sel," G! q3 j5 N/ H* ]) X. ^' p, `$ s
new FArgumentsImpl (this, sel)));
: b+ |) Y+ B3 n P* c% m } catch (Exception e) {
/ I+ [. ]. n/ O& z* N$ N e.printStackTrace (System.err);+ G3 ?) M8 H9 l3 V3 _1 n) L. S
}8 r) A' m: @1 R& m
/ y5 d9 L; d# B5 L E& j' T
syncUpdateOrder (); q5 W& f4 o' o2 C$ ?9 q H) d
6 n8 L7 m1 w5 L u try {+ o/ J* b* u. t+ t2 }4 L/ }
modelActions.createActionTo$message + K% n. _% l7 l. C' a. o+ F
(heat, new Selector (heat.getClass (), "updateLattice", false));4 f P& ~) `8 @+ p. {
} catch (Exception e) {6 g' J# w, S. u; k* e3 @7 f
System.err.println("Exception updateLattice: " + e.getMessage ());
+ }# x/ k& g; F2 V/ r2 V5 e }
! _7 a3 v$ i& ?' l5 U 3 S) _/ Q! V( ?6 x; @; x. T0 C
// Then we create a schedule that executes the
6 z9 ~2 a4 x" A& |2 D( L1 J2 K // modelActions. modelActions is an ActionGroup, by itself it
' c4 ]: q# A& `; t9 @# f // has no notion of time. In order to have it executed in, k- R) a3 S0 f2 N
// time, we create a Schedule that says to use the% |4 |4 i; E( w; K
// modelActions ActionGroup at particular times. This M! M3 ~3 D7 D" T, e& k4 i
// schedule has a repeat interval of 1, it will loop every
6 l6 l! V5 {+ I1 Z3 ?2 T. Q! u // time step. The action is executed at time 0 relative to% x% ^# X/ _0 R$ m0 K$ x G7 ]
// the beginning of the loop.
$ ]3 I3 }" b- x% o6 x$ Z* a, _* A2 C# s
// This is a simple schedule, with only one action that is
& r- E' \- V; ]$ d% F0 _5 Q // just repeated every time. See jmousetrap for more
0 S# `; D. f# Y2 e' N" Y // complicated schedules.
# h; A3 P5 L6 n% a; V ( r! l6 S4 h1 c1 X2 `0 f
modelSchedule = new ScheduleImpl (getZone (), 1);. `$ U. R7 | u+ D) [4 f% O
modelSchedule.at$createAction (0, modelActions);# W( i T' p* P# z+ b, f
) h# e* U0 W s# Q: c
return this;
; S4 Q5 z8 A$ z! U! c8 D4 A } |