HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, m+ p1 X6 w( I6 R; @5 Z& T, r, q1 `
public Object buildActions () {6 y( h @$ F1 w" o' R
super.buildActions();
$ x/ y' h4 R0 [$ x2 S0 y 7 D, H" J; Z7 ^0 | Q9 L
// Create the list of simulation actions. We put these in
0 z: a: I& F ~/ }9 f // an action group, because we want these actions to be. m; J6 Z0 A6 ~6 J! m( E1 @
// executed in a specific order, but these steps should, p# Y, e% W8 c" Z4 x
// take no (simulated) time. The M(foo) means "The message
* K2 R4 B1 q) Y | // called <foo>". You can send a message To a particular7 U# I6 |. e0 d" C* {% G$ i; ~
// object, or ForEach object in a collection.
7 X9 O3 A9 Q4 q8 r; @: l
& @6 b6 K! ~/ |8 a5 w // Note we update the heatspace in two phases: first run3 Z8 C4 M, p+ X/ @" P, o
// diffusion, then run "updateWorld" to actually enact the' E$ @! D( P5 r
// changes the heatbugs have made. The ordering here is
& w8 t6 K m$ f8 o' ^* _ // significant!0 ?" Z2 r( \1 i* z4 ^4 Y
$ V0 z+ U$ E$ y5 k! q2 j. B // Note also, that with the additional
, ~) y" m- ]( F% E // `randomizeHeatbugUpdateOrder' Boolean flag we can3 f$ J2 y: n" y& ^- [. P8 Q
// randomize the order in which the bugs actually run$ ?5 r* U% z$ @8 D# B: G
// their step rule. This has the effect of removing any& H. V2 w$ x- _
// systematic bias in the iteration throught the heatbug @, r$ d$ N7 c1 w7 B2 o2 M& l
// list from timestep to timestep
6 m7 P' E( ~4 c- n: w) o
. U2 j# M8 U5 u z- r, u // By default, all `createActionForEach' modelActions have3 X$ ~% w. @3 k' n% s0 @
// a default order of `Sequential', which means that the7 X2 U, z6 b% j: A9 D, ~
// order of iteration through the `heatbugList' will be
9 B# i% B4 N- E) k8 U4 x // identical (assuming the list order is not changed
/ z* \; K/ y! b2 b0 q // indirectly by some other process).* `, ~% y$ R0 }" o* k6 q3 z; I0 N
' a& b$ z% }3 D
modelActions = new ActionGroupImpl (getZone ());+ r1 ^1 {. m2 s* b! q4 \ h: E* O
8 |5 b1 l a- n
try {: ^9 A5 Y' J5 B5 ?: U% s( {
modelActions.createActionTo$message
8 s0 ~0 U* m; B1 k+ p (heat, new Selector (heat.getClass (), "stepRule", false));# p0 B* A0 r8 p0 O! b+ t$ O
} catch (Exception e) {
, W# q! |( _3 S2 X7 j' D System.err.println ("Exception stepRule: " + e.getMessage ());
9 b! y: r/ f2 T% R+ L2 Z9 S8 q1 N }+ P* M8 S5 q- r! X
+ O8 p, |6 [+ ]7 F0 @$ d try {
1 S+ P, ?# f$ c( z' u6 N$ x Heatbug proto = (Heatbug) heatbugList.get (0);
0 Y, \) w( F9 u6 K& y7 D' ~ Selector sel =
! D% a; Z& s1 U. p) \ new Selector (proto.getClass (), "heatbugStep", false);
! i$ ]2 Y! `; O {. p3 ?0 L# [9 x4 \ actionForEach =( l2 c# o7 J2 [
modelActions.createFActionForEachHomogeneous$call
& C& v- W1 Z& w6 Q$ c2 |; [ (heatbugList,% q1 e4 Y# M6 J( H
new FCallImpl (this, proto, sel,
: z1 z' g; D3 h! a6 O4 j new FArgumentsImpl (this, sel)));! p& _( n( R$ z7 J% g! k9 e1 A0 e( T+ F
} catch (Exception e) {
" n# q H A" Z+ @; J: {0 ^ e.printStackTrace (System.err);
! l6 C6 f3 h4 b4 W }
6 ]2 }/ W* X/ {0 J w7 l% B I7 J % |$ M( b, C3 K8 C/ Y: v6 {
syncUpdateOrder ();# ]% {/ Y! _$ M4 X4 o
4 S; k# ?- I/ w try {* Z+ Y; R& j2 z& R
modelActions.createActionTo$message
6 w6 \: u% w; K (heat, new Selector (heat.getClass (), "updateLattice", false));
% V( r& |& y" W8 k3 O3 i" Y+ w; M } catch (Exception e) {9 G3 Q! L, U, b. {$ p
System.err.println("Exception updateLattice: " + e.getMessage ());
! n3 P8 e1 R( Q: m, Y6 | }6 ^ H3 G% J! M
# l6 H8 Z! J5 w4 p9 c( V( c: K. E( a% O // Then we create a schedule that executes the
+ c9 F. K* J5 W // modelActions. modelActions is an ActionGroup, by itself it
; {6 x2 _' U5 b // has no notion of time. In order to have it executed in
( N# J- A' E' E* G/ F // time, we create a Schedule that says to use the6 m& h0 j1 m5 C {8 T2 E9 V$ A* Z
// modelActions ActionGroup at particular times. This, p Z" g% w3 M( \( x9 T! A
// schedule has a repeat interval of 1, it will loop every; E4 [* g3 \9 N( G5 h
// time step. The action is executed at time 0 relative to4 E. @* x) u' \9 ]7 D
// the beginning of the loop.
+ c4 {1 i! \6 x: { J
( l7 V4 l1 g) q1 m* S // This is a simple schedule, with only one action that is, b: W2 O3 l7 G* W* n4 E6 ?9 S
// just repeated every time. See jmousetrap for more
" E7 x: Z, J0 t( e6 j& y // complicated schedules.1 `5 [" j& `) C6 \* ` I( M" Q) }
9 M4 A- S& n+ r! f7 D modelSchedule = new ScheduleImpl (getZone (), 1);0 j, \ H6 v" J
modelSchedule.at$createAction (0, modelActions);" @7 S) }1 q4 O+ C2 N8 l3 Y6 p7 U
; L0 F: \* | y' W' v( a$ C
return this;* A7 w6 M3 ?3 X7 j, V+ b. ~
} |