HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
{1 D3 {- C4 r; X \4 E. e6 ]
1 b/ _6 p, i6 D. G public Object buildActions () {
1 ^" X1 Y: Z* {! r3 ]; j, v; H( x- _ super.buildActions();$ [1 b2 d" K4 \! s! S( @
* C9 k/ N2 A' l) A: h
// Create the list of simulation actions. We put these in: x& i7 r8 j) }0 `2 F
// an action group, because we want these actions to be
) d( q; |! w" Z( N // executed in a specific order, but these steps should
|. Y/ Y& z' I5 [ // take no (simulated) time. The M(foo) means "The message
% n+ Y+ z0 o! q9 ` // called <foo>". You can send a message To a particular
" a1 ]5 L2 @% u! j // object, or ForEach object in a collection.
, O }% Z0 Q* A& i' V ) x# C0 _+ h5 A+ E1 t5 ?
// Note we update the heatspace in two phases: first run0 m% N8 Q3 L/ l, U, C2 }( U
// diffusion, then run "updateWorld" to actually enact the7 |& i( V0 y" q, X
// changes the heatbugs have made. The ordering here is
7 R0 r: J5 r# V" C) X$ s // significant!
4 s2 k% j1 G) p9 q& Y" Z) P+ z& n
' U2 k0 K8 T' b1 o // Note also, that with the additional" t5 _8 Q3 `+ H' ]' p6 T
// `randomizeHeatbugUpdateOrder' Boolean flag we can" Q! i4 j( U8 s# w: F9 b I; P
// randomize the order in which the bugs actually run
; m, A6 p& t8 d // their step rule. This has the effect of removing any
( A2 J* @5 R- k0 r- k& n7 ]! t // systematic bias in the iteration throught the heatbug
; s: n! h( a% m4 A // list from timestep to timestep
, h! Q9 q) C! s
0 L" i3 e; F6 `! n: d // By default, all `createActionForEach' modelActions have6 K# e+ s% o% |7 }
// a default order of `Sequential', which means that the
6 `/ r2 N, {7 `* c/ T7 y // order of iteration through the `heatbugList' will be
6 {; {/ h' B4 q8 t ?+ _) o // identical (assuming the list order is not changed
' U U3 {$ P( P" ~# [4 h // indirectly by some other process).
& Q O9 g, ?$ f2 Y, A* ^
! i( ?% ?8 `$ T/ U% A modelActions = new ActionGroupImpl (getZone ());
* ?+ @# H6 z% K6 G' }# f- r) O- @! m5 ]! ~, H6 Z- q
try {
) x, l+ |1 J* a" e) A, x, k, s1 n% | modelActions.createActionTo$message# D2 J; V6 z8 l8 F
(heat, new Selector (heat.getClass (), "stepRule", false));0 O; J' r5 Z3 F/ f( O G
} catch (Exception e) {: k' e2 G7 \3 Y$ [
System.err.println ("Exception stepRule: " + e.getMessage ());
3 Z1 p6 O1 c1 B4 G2 e! l( W+ L }2 }4 G6 H4 D8 E6 Y) w
2 `# S, z$ J5 F' M6 r* R7 z& x* G! r
try {! b, E$ `+ K# P8 {5 ?5 m
Heatbug proto = (Heatbug) heatbugList.get (0);
" ~& g: W8 x' M" L. F Selector sel =
/ n: D' f5 c# C( l new Selector (proto.getClass (), "heatbugStep", false);& L t" B2 Y' Z7 z2 @$ p7 _- |
actionForEach =
9 Z5 O, L8 j7 ]$ X" p' P modelActions.createFActionForEachHomogeneous$call
* J$ N' Q T( \% E2 t7 t7 M7 R (heatbugList,/ C+ i3 z- M0 F; l6 N
new FCallImpl (this, proto, sel,0 ^1 e h. T# ?4 E- ^0 A
new FArgumentsImpl (this, sel)));; [% r1 c. A& a9 k* q
} catch (Exception e) {( ], [( v- A& C+ J ]* h
e.printStackTrace (System.err);
+ Z" w' a( K( E) N; r- `5 n }+ Z- K: X# v J) o
) A" ?& {5 j% d4 x2 W/ O
syncUpdateOrder ();
. f y! z( V' {
# e A5 b9 \) l1 Q% L7 { try {! S" p2 {! d$ p. l6 M9 }& y
modelActions.createActionTo$message
S, g$ j" x0 ]: x0 \' D% Z (heat, new Selector (heat.getClass (), "updateLattice", false));
3 T7 y' x% X5 H } catch (Exception e) {8 g! _4 U0 f" J
System.err.println("Exception updateLattice: " + e.getMessage ());( i6 O5 `% Z$ a
}0 k! `& f, D3 U% ^! X) p/ G' y; g
9 _$ e8 ?1 y8 Q& ^ // Then we create a schedule that executes the* [0 w0 q1 \. F/ I: z4 R* q
// modelActions. modelActions is an ActionGroup, by itself it6 I6 J! q8 E+ K% D4 V
// has no notion of time. In order to have it executed in
8 O& ?4 O! [% s+ n9 _ // time, we create a Schedule that says to use the5 C; p7 Y1 p3 G S$ x9 H
// modelActions ActionGroup at particular times. This
, N1 ]* }0 N: H // schedule has a repeat interval of 1, it will loop every
: b* R) I) y# {8 r7 j // time step. The action is executed at time 0 relative to
! v7 Y2 O3 j4 v, s6 }" r // the beginning of the loop./ w+ ?* |+ X) t+ s
# t$ h5 Z) f" t- q1 \
// This is a simple schedule, with only one action that is8 H* v a2 P& @! p3 b5 R$ M
// just repeated every time. See jmousetrap for more
$ ~2 o" M4 f' T6 v // complicated schedules.
s4 x, u- l& E; w& D/ {# T
2 ?8 p% l. D6 U4 y modelSchedule = new ScheduleImpl (getZone (), 1);3 r% v! ]3 n- x: O! m* f4 t
modelSchedule.at$createAction (0, modelActions);% y3 `1 U& p8 l7 j5 K5 E' e. g
+ [ ?) m: E9 k0 y' j
return this;6 I3 p* }3 M+ t2 v
} |