HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 l$ B3 a; u3 U6 O# ]7 P7 A: H0 ]. ^8 e
public Object buildActions () {. `9 U0 n( ], m* I6 V) h( t& Z3 ~7 ~
super.buildActions();9 e4 s5 M: c( B# G/ J
, \1 R5 _" R6 |; X9 g // Create the list of simulation actions. We put these in
. K r. U/ X1 b$ j6 ^, v$ f+ ~8 ~ // an action group, because we want these actions to be
1 {# k" n8 m% D8 J4 z; @; L* j // executed in a specific order, but these steps should
. \& b' E ]" z- J // take no (simulated) time. The M(foo) means "The message2 q9 C9 Z% R. H$ u2 x0 }3 E+ E; E6 z
// called <foo>". You can send a message To a particular8 W1 ~. I' W8 f$ M
// object, or ForEach object in a collection.
; {( @9 ~% b; E8 G1 x 6 q# [* E# p5 R5 v2 Q
// Note we update the heatspace in two phases: first run) t% a q) ~" ~4 n* ?, e# @
// diffusion, then run "updateWorld" to actually enact the* e% ~/ J% _( s
// changes the heatbugs have made. The ordering here is
/ t) m" p- W# A; Z // significant!9 h: Q. m4 ^3 Z: t0 N6 a
9 [1 ]8 F- }5 b# t1 y$ @" I // Note also, that with the additional
; _' X3 A" p: ]" F+ R6 G5 |2 e // `randomizeHeatbugUpdateOrder' Boolean flag we can' [3 l. W4 R6 ]7 v8 Y
// randomize the order in which the bugs actually run. ^9 c8 l2 o5 F, G {3 X! g
// their step rule. This has the effect of removing any- l0 P) _7 Z7 V+ y% `% P
// systematic bias in the iteration throught the heatbug1 e! {/ [6 w4 m( R- V
// list from timestep to timestep
8 B- x1 b) x/ x% { 2 b3 v q1 P; W& F( s
// By default, all `createActionForEach' modelActions have v, `& V) d6 l$ |1 `- j
// a default order of `Sequential', which means that the1 t% p x7 @+ C
// order of iteration through the `heatbugList' will be
N! _1 i8 a( Q' N" r/ E! K' J // identical (assuming the list order is not changed
" c6 l% _0 x1 g0 Y8 |0 l ^ // indirectly by some other process).. V. N0 A! W1 Y) w( j
" n* z% L6 u* G- ]4 T modelActions = new ActionGroupImpl (getZone ());3 T" N. w) o3 @+ r$ c. O, T6 g; l
/ ~/ u" H/ S7 p1 R* b1 P4 E" a8 J try {1 ~1 y1 q- d2 E4 k
modelActions.createActionTo$message
1 I5 ~! o) A* o9 N+ O) U2 M (heat, new Selector (heat.getClass (), "stepRule", false));; _: v" j6 s9 s1 {$ Z
} catch (Exception e) {
7 S* l- t. O+ J8 F: v- f, {' u System.err.println ("Exception stepRule: " + e.getMessage ());( C# Y3 I \- b- K
}
& ?, c7 f/ u! M, i8 w- m9 a
! J0 B2 G/ h* ?, M try {: T, D- H! ?) |# T4 o
Heatbug proto = (Heatbug) heatbugList.get (0);
+ p! _2 ?. l G! W+ {* X3 e; Q+ j Selector sel =
1 B) l5 U8 J+ c7 k1 y new Selector (proto.getClass (), "heatbugStep", false);# U! ^! o A. E+ s* e
actionForEach =) q+ S& i! N( W' ?3 i
modelActions.createFActionForEachHomogeneous$call
9 m/ s! z& W+ I (heatbugList,# A2 R ]2 o! l& p" I
new FCallImpl (this, proto, sel,5 H6 T2 `( v# F! ^2 H
new FArgumentsImpl (this, sel)));
& ^3 h0 B4 r" w9 r. {; H5 m } catch (Exception e) {! ~+ b- \+ c+ x/ B0 D6 r$ ?
e.printStackTrace (System.err);
% b$ m. U/ o- M, E }
1 _5 F4 b- u0 H6 ^: B" A2 a
, T* D4 J4 p$ n7 B9 I* r8 |- h* a syncUpdateOrder ();
1 t0 P# S6 e2 s( s" c
6 e2 i( w- Z/ {* c- Y+ Q6 I try {
- k8 [$ V$ R* R1 Q8 e% W modelActions.createActionTo$message ( v& h, H: {4 ?$ l1 T& }1 B+ q9 ^
(heat, new Selector (heat.getClass (), "updateLattice", false));8 j' ]! ?8 @% u, Q2 m
} catch (Exception e) {6 ^, ~6 r" A9 S$ j) m% F
System.err.println("Exception updateLattice: " + e.getMessage ());
/ ~: E0 `& M% l- |4 i% r' h }
/ f# h4 [4 T* o& s # s+ h$ D0 F) i9 S7 x
// Then we create a schedule that executes the6 J) h0 Y# Z4 @5 ]
// modelActions. modelActions is an ActionGroup, by itself it
" F: ^3 F, O, H' u // has no notion of time. In order to have it executed in
$ \: ~9 I% O$ Y // time, we create a Schedule that says to use the
Q( |. f0 ?; o- z- x // modelActions ActionGroup at particular times. This5 D) Z' i9 @& X& D
// schedule has a repeat interval of 1, it will loop every$ N- n Q# g2 S2 z+ j" L0 H0 O4 U
// time step. The action is executed at time 0 relative to
1 i2 {& v! j/ n5 d // the beginning of the loop.% {; c9 n# x- B- |2 b; f
$ x7 \9 y3 v5 p0 t ?
// This is a simple schedule, with only one action that is
1 a- Y8 x( c& x // just repeated every time. See jmousetrap for more6 L4 a7 ^; u1 q0 B; u$ V
// complicated schedules." H" Z/ O' d, m( n* m( Q
/ Q0 V& Y9 {9 F$ J, ~ modelSchedule = new ScheduleImpl (getZone (), 1);' B- t2 h: Y) Q6 ?! @) u
modelSchedule.at$createAction (0, modelActions);
/ t6 u! b+ O3 E: q1 i$ o" _
* Z X1 e$ u! L1 g" {% e return this;) ?* U1 H( ?& T; z4 I
} |