HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 u7 o& c( R" F& I4 @# O7 d) N$ x
1 s6 p( y; d9 ^7 }6 y public Object buildActions () {
; n9 {* {3 z7 M super.buildActions();
. X( x4 I# x5 q! u Z' T' L7 X & U9 C% ?% `" z/ {- w
// Create the list of simulation actions. We put these in
5 V: A4 Q! F4 e) L/ i // an action group, because we want these actions to be' D- V- o: O+ j: v& g- c3 O& l
// executed in a specific order, but these steps should! p$ r: i; ]+ I, n2 J
// take no (simulated) time. The M(foo) means "The message" J# C& d1 }. E* S
// called <foo>". You can send a message To a particular6 e2 t$ E# X4 D x) c& G) w
// object, or ForEach object in a collection.7 N" A+ i& J0 w# C
; B% V7 k! L+ O
// Note we update the heatspace in two phases: first run
/ |' ~/ t4 j6 W4 r) z4 j // diffusion, then run "updateWorld" to actually enact the7 _& @, Z6 p9 a/ v
// changes the heatbugs have made. The ordering here is
% a$ K8 f' y5 B1 n2 b* b$ e // significant!
& S! Z! F& ]( ^3 ^8 r8 Z+ H
4 C! P; T+ g! I9 i$ o' ~ // Note also, that with the additional! Z- u) Q" o% M; F$ K& M6 W
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ S9 D& ~0 I9 n N0 `2 f; M7 o9 ?3 y // randomize the order in which the bugs actually run
6 g$ S1 m" ~" `7 k // their step rule. This has the effect of removing any
$ V" M6 \, B* I0 G9 T // systematic bias in the iteration throught the heatbug
% }' o4 l' X- h0 F% V // list from timestep to timestep- l; D; j$ @+ |0 k" D* I
+ b- U* j1 F R0 c: C6 o- Y // By default, all `createActionForEach' modelActions have
, s- ~ R$ O- t5 z3 S/ I p // a default order of `Sequential', which means that the
8 s& c6 J$ ^! s, m& }8 @ // order of iteration through the `heatbugList' will be
' \4 z- B9 p( |$ A/ g" i // identical (assuming the list order is not changed; L% L' K' u2 {0 X
// indirectly by some other process).
! Q1 |$ x8 P4 O1 r3 y : T& L1 B' S; W7 u9 r3 D/ }+ p
modelActions = new ActionGroupImpl (getZone ());
, N. |2 Y/ j) q x. X4 Z3 r- h# \5 _& i+ z, \
try {" L! L8 c0 c1 S# \5 _
modelActions.createActionTo$message. L! U9 q; a+ {% d1 q
(heat, new Selector (heat.getClass (), "stepRule", false));
0 z( X+ e5 `5 S9 b } catch (Exception e) {
: L% W! m+ T8 C" a9 Z& [; F7 W, T System.err.println ("Exception stepRule: " + e.getMessage ());
6 w8 U9 v) S$ V o5 U" K }
~3 q. B. D/ h' a" z$ P
8 ?2 m: w6 |: J- S( `# c try {( ^' x3 b* l, c' m) `
Heatbug proto = (Heatbug) heatbugList.get (0);4 U7 r; k, N" ?3 V
Selector sel =
( `! _0 Q1 R! d! w9 t/ o* q new Selector (proto.getClass (), "heatbugStep", false);, s5 G" S7 x$ h. K' E3 U; O
actionForEach =7 J7 a8 F0 f9 P$ N# u/ y+ M
modelActions.createFActionForEachHomogeneous$call8 T" w6 ^9 {" f! m
(heatbugList,7 n3 d0 N" m) A1 }- {/ o5 o
new FCallImpl (this, proto, sel,5 V! {2 ~. B4 [$ v
new FArgumentsImpl (this, sel)));
3 W; P2 I6 `" o [' Q4 n# {) \! k } catch (Exception e) {8 T1 Q7 c8 g$ W
e.printStackTrace (System.err);$ g% @2 B: d9 @, |; A2 e
}4 w. Q! J" W# q I& v
% {( N4 T4 K- y, @
syncUpdateOrder ();( I! P. \. g7 ~2 g; V4 J! O
; U0 t0 L2 A: |3 r& S
try {
: k2 x: U& C% P; B; Q/ B: V modelActions.createActionTo$message $ s9 |1 K0 C7 h3 k5 H! r! a
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 D* K( D8 J4 d1 P } catch (Exception e) {
. L) m3 a9 S8 [ System.err.println("Exception updateLattice: " + e.getMessage ());) ?9 H2 l& S' y5 f: @( }4 z
}7 S1 j, Y, M3 l; X& ]# [7 x" Y7 X
% r0 D9 e) U- N" Q
// Then we create a schedule that executes the
1 G$ E0 a7 p: \+ ?0 G // modelActions. modelActions is an ActionGroup, by itself it
" u! B. ~5 [( l- r( s& i, U4 U* ` // has no notion of time. In order to have it executed in
5 V* F1 R% N2 ?: R. r // time, we create a Schedule that says to use the
; D9 y6 [8 D- c5 h* m$ \ // modelActions ActionGroup at particular times. This
2 Y5 O/ \2 q" |4 D // schedule has a repeat interval of 1, it will loop every3 D8 N: a" _2 m1 M5 O
// time step. The action is executed at time 0 relative to. V) L9 C. d; E
// the beginning of the loop. J" L3 o) s; K& @, L9 {; R" i- ]0 F
; n: l, m' \2 `* E0 W // This is a simple schedule, with only one action that is
/ D+ U9 p' Z+ v6 m // just repeated every time. See jmousetrap for more
. E( `8 V5 h4 Y0 I // complicated schedules." v& K* P9 g9 D) ~$ C& `$ u- @
1 r3 k. }) d- j$ q modelSchedule = new ScheduleImpl (getZone (), 1);
, K P* g N! G& d modelSchedule.at$createAction (0, modelActions);
M' x; E, R5 ~7 r1 y9 B/ F0 b. B
9 ^+ v O5 d$ h Z return this;
7 V* v$ U& k1 a } |