HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 h" J! o' [3 |$ Z( f# U8 t6 I- u5 Y
public Object buildActions () {
/ B4 d% ^! F% p9 j! ] super.buildActions();
# `$ w8 r7 e9 d' J7 ? ; G( R' A) r" e. E7 R
// Create the list of simulation actions. We put these in$ L# d9 h! r3 x" f+ {" u, ]8 ?
// an action group, because we want these actions to be
6 B b1 K7 s: h, I4 t // executed in a specific order, but these steps should
1 S. ^( r% i: ]# ]0 a1 A4 [- l // take no (simulated) time. The M(foo) means "The message
0 n7 r6 w9 Q" z" l A& K: x* N // called <foo>". You can send a message To a particular$ `" [" R: ^, Z K9 g3 c/ z' |
// object, or ForEach object in a collection.
; `+ L' X, c k
% w5 S% h. K/ { K3 ]( r D // Note we update the heatspace in two phases: first run
' {3 y: P0 n) }, Y; |1 m* S // diffusion, then run "updateWorld" to actually enact the$ S2 c E _: d( _% k f/ B6 j
// changes the heatbugs have made. The ordering here is5 }7 D; k+ t( O9 W& E
// significant!: K- t A3 H: m* T, J( T
# a. l' W0 X t' \. r$ p // Note also, that with the additional
. h+ D3 ^$ ]4 c) @ // `randomizeHeatbugUpdateOrder' Boolean flag we can
* H! W$ G5 m0 K) B& d+ V1 Q // randomize the order in which the bugs actually run9 X; T" n. o- @' ^* q' c& ]$ p8 k3 j2 E
// their step rule. This has the effect of removing any
7 J, F9 C" Q! c7 } // systematic bias in the iteration throught the heatbug
* z3 y7 n5 K" }- f+ F _ // list from timestep to timestep
+ G. A3 z1 [ t' ]% H7 }
5 X% `0 g: H7 `3 w" `; z // By default, all `createActionForEach' modelActions have
% Q; U3 `. w- y$ W // a default order of `Sequential', which means that the
+ O/ f& h/ Q/ }/ ^) h0 l: L // order of iteration through the `heatbugList' will be/ _7 ^* { E; F" ^; }( _2 c
// identical (assuming the list order is not changed8 b* ]1 m0 ^7 ~: z
// indirectly by some other process).
; Z/ `. Y2 K5 z1 S. ` S: h " |, K' ^" M9 E# Q* k
modelActions = new ActionGroupImpl (getZone ());: k+ p7 @" b3 G, r
$ v( G1 c- k4 L% ~ A try {
( [7 H% x/ h+ H* X modelActions.createActionTo$message
* a9 f( O- l* ^' Z0 d; f (heat, new Selector (heat.getClass (), "stepRule", false));
1 ^/ [# Q0 q c2 v } catch (Exception e) {
3 h# ~3 U+ }: P* E* {( y R System.err.println ("Exception stepRule: " + e.getMessage ());+ T5 z- Z1 @1 D: `: M9 _( ^
}( Q! |1 D7 n' r7 B' b
/ M* C/ E" u/ w7 t: U" d* O try {4 {' \" R: g$ S* n5 a* F+ `5 q
Heatbug proto = (Heatbug) heatbugList.get (0);6 f Q5 E' X! M: H
Selector sel =
% d: p$ W+ Y: R. Y0 I new Selector (proto.getClass (), "heatbugStep", false);4 W; R( Y0 g* o# p9 p% z" P! t. T
actionForEach =! U- k* P* n- f3 ]' u; W7 b( E
modelActions.createFActionForEachHomogeneous$call
% Y! n3 z/ H0 ~9 c# j (heatbugList,
+ n& V1 w3 z* Z new FCallImpl (this, proto, sel,
) ~2 ]8 p. R8 O0 [! [ new FArgumentsImpl (this, sel)));
/ Q7 V+ n5 D( C } catch (Exception e) {
0 |# _. [6 c5 W# p( J' @+ O e.printStackTrace (System.err);* L4 y' T' Z& `
}
/ B# m$ s3 }8 {* { * ?" }" }) _/ ?, w7 O8 U8 x3 f4 S1 A
syncUpdateOrder ();; s! T4 q( M7 I% C
1 _9 p% M# U% C- w5 Y1 N/ J) r
try { w3 x# K* z; Z! {2 b' Y+ }9 [
modelActions.createActionTo$message
# [& x- C6 H5 { (heat, new Selector (heat.getClass (), "updateLattice", false));! r' p& |6 I) s1 E4 B9 x3 J
} catch (Exception e) {6 p5 R1 F3 Z; X$ w
System.err.println("Exception updateLattice: " + e.getMessage ());
: t% p I* ~# {6 Q$ G }
6 c. g2 {- ~! W v0 U
! `! ~/ D/ Z+ H" [" i4 a; ` // Then we create a schedule that executes the
& T9 Y* B" K# O. H. M // modelActions. modelActions is an ActionGroup, by itself it: M* ]8 b. o. k0 t5 j
// has no notion of time. In order to have it executed in
. z9 H9 y9 K1 ^6 A ^4 ` // time, we create a Schedule that says to use the
n6 @# }* p: B I' _2 D9 s // modelActions ActionGroup at particular times. This9 o4 F$ I% G/ j: {. L& v
// schedule has a repeat interval of 1, it will loop every
- H9 P4 z; q! D( R0 w. Q% v // time step. The action is executed at time 0 relative to( e# t+ l- Q4 i8 Y, }) R! l
// the beginning of the loop.
. X1 J, j% n7 | s
9 P- ~# d& D4 h7 }1 f0 ~ // This is a simple schedule, with only one action that is/ r5 n+ S) o4 c- W' i, {4 e
// just repeated every time. See jmousetrap for more2 o: F. h2 d* o6 s" @
// complicated schedules.
4 N" V0 ], n& V , X& w' N% x$ ~) K: I2 @
modelSchedule = new ScheduleImpl (getZone (), 1);( R) N# x' Y& B- ^' ?/ {; ~
modelSchedule.at$createAction (0, modelActions);: Y. ~ r! V" u- T+ m
) w% {2 |( Z- u% @
return this;/ K5 V! q8 ?% ?+ r
} |