HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 M! p! @1 j% H
# ^2 O& ~" E. \6 G, U
public Object buildActions () {% P6 A' s! A: q
super.buildActions();
: a1 L* ]2 e# O" ^8 k7 @) x1 f+ F
% G, W0 p# Q( Q" I5 c // Create the list of simulation actions. We put these in
$ u& n) @2 y4 z0 n s+ X' T // an action group, because we want these actions to be8 @/ o4 f9 j B# e7 V* R5 y
// executed in a specific order, but these steps should
+ @/ t+ Z0 c* H9 p& ?6 M // take no (simulated) time. The M(foo) means "The message- Y- H9 M9 u5 I1 }3 l+ A# J
// called <foo>". You can send a message To a particular
9 t( _. d5 H: x8 }; {3 } // object, or ForEach object in a collection.
+ O+ Y1 f' @: |- u
6 b2 {5 a7 {) u // Note we update the heatspace in two phases: first run+ _3 T$ I3 D: |
// diffusion, then run "updateWorld" to actually enact the+ q) k8 x; p$ x- ]
// changes the heatbugs have made. The ordering here is
, x1 t7 A3 Y. r4 l. o // significant!
* W) j" h4 u! ?) `8 \( d w 9 b) `( ?. C, [* a$ ]' d5 b9 [! | D
// Note also, that with the additional9 w$ z. e. Y5 g" b
// `randomizeHeatbugUpdateOrder' Boolean flag we can
, m" X% g) O. ~3 z1 a, { d) v8 Q // randomize the order in which the bugs actually run
" Q2 O3 x0 n6 |9 ?2 A& x7 f4 q, h // their step rule. This has the effect of removing any% j h( A* x2 u8 h, |5 ]
// systematic bias in the iteration throught the heatbug
; e) {/ Q, _( S7 N // list from timestep to timestep/ ~, _! C( q+ i- z. G
1 {+ {; g4 M+ s" y- R8 m
// By default, all `createActionForEach' modelActions have
_5 g% R( z3 _; G( L+ n // a default order of `Sequential', which means that the! O6 q& m+ f' y- |2 I" H1 ?
// order of iteration through the `heatbugList' will be& h3 A' i0 t8 k1 }5 F3 k2 Z$ X
// identical (assuming the list order is not changed
% S# g8 Q6 W0 J // indirectly by some other process).
, o& X% u- j7 p& K& l
/ J4 H2 J# E8 A* g3 Q0 |; y modelActions = new ActionGroupImpl (getZone ());/ W7 T$ D! b) b
$ b5 F9 w) o( @) f' b0 l* d% k try {* U% y9 S! O H
modelActions.createActionTo$message
' I0 [. ~0 A" z- P3 \# n (heat, new Selector (heat.getClass (), "stepRule", false));: r6 z* j J( C) l5 t6 N: q* D# t
} catch (Exception e) {
0 {( t9 Y) ^( U; y+ _2 z0 ^ System.err.println ("Exception stepRule: " + e.getMessage ());
5 C) i) [- m1 d: g# t }
4 J0 |! `/ l/ M0 Q" j0 {8 z2 G' [' S9 }2 k$ r* R/ f' d
try {1 r3 ?2 u: }+ L
Heatbug proto = (Heatbug) heatbugList.get (0);' W1 B3 s+ n: r
Selector sel = : q% L' g/ x9 }$ ]/ t! J6 c
new Selector (proto.getClass (), "heatbugStep", false);
- q+ b' {7 J3 z r actionForEach =8 v8 l E' w3 p7 `4 M
modelActions.createFActionForEachHomogeneous$call. h! T% `3 ^+ @4 q
(heatbugList,
5 _7 W5 |% F3 R+ j new FCallImpl (this, proto, sel,, }( j! m2 O! ^9 t3 K7 \
new FArgumentsImpl (this, sel)));0 E: V$ V* _8 h m T+ X
} catch (Exception e) {& M6 d' V3 e" i9 r
e.printStackTrace (System.err);, U- t7 m8 E) z2 Z/ u
}+ a, a, V+ r& q' C; P# W7 [
4 q+ _* S2 p9 E( R syncUpdateOrder ();
. o/ T: |* E" _$ b* r
$ g+ K' h) ?1 K" z$ `# S try {
' a- w; R; e+ B- ^% k$ h modelActions.createActionTo$message
8 b3 R- T, b7 @& j% _3 v* F (heat, new Selector (heat.getClass (), "updateLattice", false));
: P* S" y' R C+ a2 x' w } catch (Exception e) {
6 a) Y3 j, q' J4 b, N System.err.println("Exception updateLattice: " + e.getMessage ());" ]9 p# ?- f7 _- ]) ?2 m# D
}2 P5 d' E7 k5 L
1 y1 X8 r7 h$ S5 `! c4 o4 u
// Then we create a schedule that executes the# i0 p* w& l1 @
// modelActions. modelActions is an ActionGroup, by itself it. D' o! v. O: i3 E& z* o* e
// has no notion of time. In order to have it executed in
# u* D4 C3 i( @5 |: V4 v. F // time, we create a Schedule that says to use the
4 H7 r6 A' @0 t; a; C5 t3 j // modelActions ActionGroup at particular times. This
! r0 J$ K- `' C. ~# i+ t5 H // schedule has a repeat interval of 1, it will loop every& _8 m4 d. A) C S* S, k
// time step. The action is executed at time 0 relative to
8 ]9 T/ D3 c) h* X) H+ i // the beginning of the loop.& [# X' n! a# |5 k2 t0 D
4 o, n+ L) m- R/ T n
// This is a simple schedule, with only one action that is
/ p# r% ?( }2 R' Y // just repeated every time. See jmousetrap for more
6 q' P- A9 H5 n9 { // complicated schedules.( N- g! `- i" |) F9 C: R
7 l P4 R$ W* D3 C modelSchedule = new ScheduleImpl (getZone (), 1);
7 @! Y: R: B4 i4 l4 d modelSchedule.at$createAction (0, modelActions);
' L3 E. b/ B) Y5 w0 { 2 N/ `3 ^1 i$ A: O* D. [' {: v
return this;
$ n k1 I7 p$ ~- u) C o6 N } |