HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: x K0 Q1 S& i7 R
6 r2 }. U' q' J E7 R: t9 E h0 f$ F public Object buildActions () {
$ w4 k( m2 I/ E+ e super.buildActions();9 k# k1 \0 d3 i
. W, R/ }1 s Q; D) s) Y/ B% M // Create the list of simulation actions. We put these in
# W4 [; ]" y" u/ b" a" A" x S // an action group, because we want these actions to be: N6 i1 [0 V) \. M- q
// executed in a specific order, but these steps should7 e! g9 \$ v% @% V" k" c# w
// take no (simulated) time. The M(foo) means "The message) L+ o3 ]! P/ x) @: Q5 I" Y( w8 H
// called <foo>". You can send a message To a particular
% V* e8 \& s8 \- W5 g' x' P // object, or ForEach object in a collection.! d: ~" [) y u2 P
( B! ]0 v+ V; N" ]; a" h% K! s( f // Note we update the heatspace in two phases: first run' o0 M2 C% V; J, z6 {
// diffusion, then run "updateWorld" to actually enact the
3 E/ O9 z! x6 W0 d5 C r // changes the heatbugs have made. The ordering here is: b2 i) V3 S% a& g! b
// significant!
/ ], l: `* S9 Q# q z; v& J 4 t9 Y; D# v- @. R; F
// Note also, that with the additional
' q0 W' T. ]2 n7 s' _ // `randomizeHeatbugUpdateOrder' Boolean flag we can: C7 m5 E. n4 T, [% S" F8 y
// randomize the order in which the bugs actually run
+ ]0 A$ V& U2 _0 H/ k t // their step rule. This has the effect of removing any
# o$ H0 e2 r$ p0 q" J( u) q // systematic bias in the iteration throught the heatbug k5 r- M6 o- ?1 Z1 l! X% n
// list from timestep to timestep. x7 B2 v& ?+ u- I2 R% h
( v) k9 O) Z3 w( i! I
// By default, all `createActionForEach' modelActions have
( n9 }1 q: @- E+ }+ G9 k // a default order of `Sequential', which means that the; S! q- \# J0 I- j: s. r1 H2 G
// order of iteration through the `heatbugList' will be
k* L: N) | K% h3 y! `! W1 p2 N, T // identical (assuming the list order is not changed! d; |. r0 ]2 b$ J
// indirectly by some other process).! e$ F% B0 R. { U- Y# w, N- q! K
3 ~6 _- G5 E/ B! L6 U
modelActions = new ActionGroupImpl (getZone ());4 s+ w7 ?, {, r' u5 d
T5 Z6 C+ b1 d( u! ]; O7 P2 \! Z( f
try {
+ x1 ]# g/ o( O$ Y7 w modelActions.createActionTo$message& ~4 R% m/ K1 s1 d7 P7 C# h! W
(heat, new Selector (heat.getClass (), "stepRule", false));
3 @$ X1 o4 I& W0 r2 X! k- k& K } catch (Exception e) {0 u' Z ~& ]0 c& d
System.err.println ("Exception stepRule: " + e.getMessage ());8 | W& E; W3 h+ l/ @$ |) f n
}4 N8 J# r6 d- ~
4 q& ?# B C& k! B' `: a try {
) x" K' s. f+ q5 A8 |7 U Heatbug proto = (Heatbug) heatbugList.get (0);
/ F+ P, h4 B! U, T3 j1 y Selector sel = / r$ K' S% Z- |: c" V% F7 T
new Selector (proto.getClass (), "heatbugStep", false);
0 }; n v; U( o, X! | actionForEach =
4 i% H1 K8 }" T" D4 S modelActions.createFActionForEachHomogeneous$call6 B1 ~+ e$ i9 o: Z, f
(heatbugList,
" i3 M' v; L2 Z0 p; [) c# d new FCallImpl (this, proto, sel,4 b- \2 H3 G3 _# ]' {/ g# e# E5 o
new FArgumentsImpl (this, sel)));
" j7 A y3 c7 ~. B, X5 ~- k; [/ Z } catch (Exception e) {
' w& |* |* u9 W e.printStackTrace (System.err);( m1 _% Q0 s1 j) N- J" ~
}
6 a; A5 K5 {% N1 |9 R' N# Q+ ^ 3 f+ B! |, T4 D
syncUpdateOrder ();
& _: a8 a3 O+ {7 U! C( z% |2 x9 X6 R' b% Q6 \) ~
try {
* M* ~0 s! l' D modelActions.createActionTo$message
0 K. H( Q) q4 L4 e: r3 s (heat, new Selector (heat.getClass (), "updateLattice", false));2 Y: e" L0 ]2 t7 Z; P
} catch (Exception e) {
1 c. r4 k, Y; h8 o% h, e" ] System.err.println("Exception updateLattice: " + e.getMessage ());: |8 X H3 f. V8 b
}
- O6 M# [& r9 C: I$ q
~$ N! J# P# H* @ // Then we create a schedule that executes the
6 p& D. ]8 S7 l& T8 O0 o8 K // modelActions. modelActions is an ActionGroup, by itself it
1 g3 e. ?5 Z* p6 d$ s% p1 ^ J // has no notion of time. In order to have it executed in) b4 H1 X0 ]: C4 m" s. x; N4 d# L# q
// time, we create a Schedule that says to use the
/ p9 i; `& ?+ y6 l- Q/ v // modelActions ActionGroup at particular times. This& P. t8 s: ~& h9 W6 r
// schedule has a repeat interval of 1, it will loop every
" l- U0 B* |3 V/ ]8 l // time step. The action is executed at time 0 relative to
j6 d6 z7 \/ y" W3 j5 T // the beginning of the loop.1 ]3 C6 F6 D6 a- [8 |+ p6 `
+ S; m _! ?6 U
// This is a simple schedule, with only one action that is1 r1 g7 ]/ Z# [. ?
// just repeated every time. See jmousetrap for more7 r( p! ] n, ^* n# I" }0 ^
// complicated schedules.4 ^. _: M& [) @" |9 C& g7 O) [
( j5 I3 S& W4 f! u$ d# J7 R; W' M
modelSchedule = new ScheduleImpl (getZone (), 1);
, v$ E9 G5 d6 p8 O* }4 k modelSchedule.at$createAction (0, modelActions);
: M- K/ L; b. u" ]2 U
. s/ I* U5 Z1 S& \% F$ H return this;
% i; g. I- @: c- @" X } |