HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* ^; \6 X6 s5 L* {0 ^9 X5 w- K
+ K Z1 d6 {: `8 ~- _$ v public Object buildActions () {# O* d5 E1 w$ \: u+ U5 ^( v6 `1 a7 V
super.buildActions();
U$ b! C) r4 h/ B- f
2 y6 f1 j x: X6 ^* J. t4 L1 V5 c // Create the list of simulation actions. We put these in
- _. A6 Z4 D7 o; d // an action group, because we want these actions to be
* q6 X& H) U. l* \2 \3 ` // executed in a specific order, but these steps should' K! J$ B% S: U. F6 y9 C) z
// take no (simulated) time. The M(foo) means "The message+ }6 m- ?) p R
// called <foo>". You can send a message To a particular
4 W2 K, x7 d" d5 c$ o X1 j* @ // object, or ForEach object in a collection.
/ [/ [! @# o, i p
% ?$ W7 \$ t+ H: `, V // Note we update the heatspace in two phases: first run, |" _7 C! m. e; L
// diffusion, then run "updateWorld" to actually enact the4 s$ U V* G6 M
// changes the heatbugs have made. The ordering here is
/ P8 [. z8 h, E6 P2 z // significant!6 C& }8 J) j& G Q- x4 x
( D8 R: R6 K! [% r // Note also, that with the additional) c' V# |% Q+ ?( ]! y% D
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# z% A/ g3 H2 E0 A% V+ f1 w // randomize the order in which the bugs actually run2 }" }9 W; _" u) @8 U) v
// their step rule. This has the effect of removing any
+ q8 l" j; w! g6 m& ]. R( [1 ~ // systematic bias in the iteration throught the heatbug
: v3 ~7 n: F/ ^9 ^4 Q$ C- I! [& c // list from timestep to timestep
( y- I# o+ [6 U7 z& f* @
" S/ t$ b0 }) i$ w7 N // By default, all `createActionForEach' modelActions have
- @7 ]9 [. i" Y- c1 Q // a default order of `Sequential', which means that the
2 ]; U1 J9 N! F& k6 x* S/ ^1 a // order of iteration through the `heatbugList' will be
4 k8 V( ~) Y' M // identical (assuming the list order is not changed1 M; L5 y, {* u2 I+ ], }5 ^) \
// indirectly by some other process).: B$ t! X: l$ T) x; k( H0 E
. l8 ?# g B0 k; F. g) k
modelActions = new ActionGroupImpl (getZone ());
5 U) @+ F; j8 j8 W% X k
! c( H. @% I4 b: \$ V try {
: w; x7 ?+ ?6 G! \- X! b modelActions.createActionTo$message
: G8 F. G/ A0 ^' G (heat, new Selector (heat.getClass (), "stepRule", false));
6 h8 K! c8 H5 H9 v( C& s } catch (Exception e) {
9 w: X5 M+ T; p4 g: o0 b System.err.println ("Exception stepRule: " + e.getMessage ());
; k: G7 i2 s' O) i( _ }
6 I6 L4 u2 H1 H6 k+ a: d9 E+ i( v
try { z* S( |; a3 J' a1 B$ }
Heatbug proto = (Heatbug) heatbugList.get (0);0 b( f$ P1 Y7 s& |, G# i
Selector sel =
1 w' J- v# s* T7 | new Selector (proto.getClass (), "heatbugStep", false);, k2 K1 [3 F+ }
actionForEach =: W! x! P8 N8 }* g/ [: g1 [5 a
modelActions.createFActionForEachHomogeneous$call
7 z$ }. }* A" h: ?% | (heatbugList,8 b" {, \/ Y3 K, z+ c; w' C" p0 B
new FCallImpl (this, proto, sel, H% b" L8 \9 Q* ~' \. b
new FArgumentsImpl (this, sel)));/ Z1 c3 h: x- ~' N
} catch (Exception e) {
( h) X/ M& a0 d8 M1 ? e.printStackTrace (System.err);
8 h! _- w' S+ l }8 C8 D! S. v2 j8 P+ W/ i& S! ?3 u1 }
; W8 Z: Q* Z' y syncUpdateOrder ();+ D% E p; @1 |3 w. J( n
F: J3 i/ B0 I& @& M6 n7 F
try {: D' D' o) O, G6 v$ L
modelActions.createActionTo$message / Q4 ]3 v N; Q0 q) s- f+ w
(heat, new Selector (heat.getClass (), "updateLattice", false));8 l! R. f' @/ F5 X" d; j/ b
} catch (Exception e) {3 j% i- s% N( p; P
System.err.println("Exception updateLattice: " + e.getMessage ());. g2 o9 H& R' I0 D! h6 L
}+ K. {1 z* c" j7 p
$ s" q, W/ a: x* S+ g( w
// Then we create a schedule that executes the
5 O$ b& V: h0 z4 h* w; j( D4 u // modelActions. modelActions is an ActionGroup, by itself it
2 F. L9 Y* p3 F4 ^3 m // has no notion of time. In order to have it executed in
! @1 t2 ]+ Z# l1 R% j // time, we create a Schedule that says to use the
: L/ U( o H; \* x/ t9 c+ b // modelActions ActionGroup at particular times. This
5 Q6 Z% w& ~% L5 K // schedule has a repeat interval of 1, it will loop every
5 [9 l8 D/ `8 O& i- p3 [: I // time step. The action is executed at time 0 relative to( P3 ]2 O& N0 U5 a
// the beginning of the loop.; i( e8 V% X* H/ E( d+ M
# h8 F4 m1 d) `( l- U // This is a simple schedule, with only one action that is
9 ~8 V9 _! Q9 m // just repeated every time. See jmousetrap for more
% d+ N9 i. A' e7 l" {: Y // complicated schedules.( g3 d) A$ n0 J
/ } e: `& ]8 y! m" ` modelSchedule = new ScheduleImpl (getZone (), 1);
! J. t% y+ U: H$ S; x8 N modelSchedule.at$createAction (0, modelActions);
& A% V5 |6 C/ I
# J: h; x2 M" E return this;
# T. o9 S% F* O$ T3 P3 { } |