HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 k, Z' r2 m5 \
7 ]% U9 X6 b8 ? public Object buildActions () {% C5 ?9 z8 G+ b' g! g0 [% @
super.buildActions();- K! Z. u# ]3 E, A
* ]. u/ N: E+ i+ @ // Create the list of simulation actions. We put these in3 j% e* }, w) j5 l# I, ^% y
// an action group, because we want these actions to be5 _4 Z( J+ D6 E. V1 u C$ ?
// executed in a specific order, but these steps should
& E w2 N2 n8 l3 y/ |$ G // take no (simulated) time. The M(foo) means "The message" ]: _2 `; [* _4 L* B$ U, a# Z
// called <foo>". You can send a message To a particular
* D M0 Q0 d6 u; h // object, or ForEach object in a collection.
" A# O$ q' T, j. v( n& ]! D b! A' Y4 E3 `
// Note we update the heatspace in two phases: first run: H( s. B! J9 I+ ]- N- ^: Q
// diffusion, then run "updateWorld" to actually enact the
& v7 F" ~, r# y // changes the heatbugs have made. The ordering here is
2 b; x) O" p! v# l // significant!4 _5 b4 M4 Y' c
, x$ W7 y* w$ Y1 g/ m3 m/ a2 s
// Note also, that with the additional8 W/ r' P" p. X5 R5 \
// `randomizeHeatbugUpdateOrder' Boolean flag we can, o# ]! Z0 }- R |
// randomize the order in which the bugs actually run# W! z4 M+ S+ m J* `
// their step rule. This has the effect of removing any8 R f/ o: _$ r1 }7 ]8 @' ?. m
// systematic bias in the iteration throught the heatbug L5 q1 [+ o& U: X- _( n, |. m
// list from timestep to timestep) Z w4 p8 t9 o# K
8 p+ X" n3 l" ` // By default, all `createActionForEach' modelActions have
# H! u" I" I0 Y // a default order of `Sequential', which means that the
) l f8 f/ s p j5 p7 R // order of iteration through the `heatbugList' will be
# b5 u1 p U+ z6 E: W/ l, y" t // identical (assuming the list order is not changed
3 Q+ X i& Y5 D) l K Y; ~ // indirectly by some other process).
, ]# ~6 [+ R \- x, Y) Y- R7 {
# m4 U0 G$ I' s- `+ F modelActions = new ActionGroupImpl (getZone ());
; t+ U# E# v* e8 O
$ Z0 ]3 U: ~! i6 D5 k try {
9 j c( p8 L+ r8 \4 g( b: ^3 x modelActions.createActionTo$message
- H" x* m, d9 U6 t$ E (heat, new Selector (heat.getClass (), "stepRule", false));
$ r: E, C0 N/ a4 s } catch (Exception e) {) m) }- P! L, {, I
System.err.println ("Exception stepRule: " + e.getMessage ());4 g9 ] ~) b8 D' T ]
}
: d! E0 B; f9 {1 _- O. N3 X2 f6 E
# ^7 w* j9 p; b+ N' s6 } try {
* P; k6 i. H$ Q Heatbug proto = (Heatbug) heatbugList.get (0);8 B) [* c7 v% l
Selector sel =
/ |9 j. U6 h$ E% H8 n! ~3 u! d- B new Selector (proto.getClass (), "heatbugStep", false);3 f" x% M9 s* [8 G) Z2 _
actionForEach =
2 x/ x4 E+ E1 v6 w$ y. @- E modelActions.createFActionForEachHomogeneous$call
0 f( e: u* O# l, y (heatbugList,
5 w8 S+ m* o" w7 |$ ^* k new FCallImpl (this, proto, sel,
* I( O% p: Z( ?$ | S1 U+ {3 O4 L3 K new FArgumentsImpl (this, sel)));& J5 s7 r; c! G9 l' x' F! ^* [
} catch (Exception e) {; l7 ]- l0 f' o
e.printStackTrace (System.err);
3 Z7 q' ^3 p" l }
: I, I" q7 f, S1 Z/ ~ 1 T8 _8 p% o7 m" K/ M; X. g0 ~ S
syncUpdateOrder ();
% _2 w9 C% h0 b% ^
! t+ o1 K, ]$ {) a( L6 I. W1 { try {3 w+ I: Y# O" [
modelActions.createActionTo$message : F' Y" ?& c1 h. X# Z( t
(heat, new Selector (heat.getClass (), "updateLattice", false));# |9 z. p* b$ f; `; F$ g X
} catch (Exception e) {; r7 E6 V& d* D
System.err.println("Exception updateLattice: " + e.getMessage ());/ n, n* @: z- l
}
; B; M8 f+ H+ C9 p
- M7 g3 F9 t+ [! P& A& n5 ^ // Then we create a schedule that executes the( u# s4 {. U( M# W: o- b# r/ V8 s
// modelActions. modelActions is an ActionGroup, by itself it
4 L; C" [& A' w7 ~ // has no notion of time. In order to have it executed in
4 \- k- G$ j, v2 ]( P // time, we create a Schedule that says to use the
) V( L" ]! _" z G9 R // modelActions ActionGroup at particular times. This( R1 r& R1 C) J; u# F. ?2 |+ s! ^
// schedule has a repeat interval of 1, it will loop every
: z8 V, e0 {2 N5 Y9 I1 C9 C // time step. The action is executed at time 0 relative to
5 L% A# V' c+ p$ j4 s% e2 n& {( f // the beginning of the loop.
- g2 D* _7 j9 D
6 G% O% b1 H5 s1 T! M* b h // This is a simple schedule, with only one action that is, d, j+ Y; K( R, A2 G" s
// just repeated every time. See jmousetrap for more& O6 ?$ t4 }0 f
// complicated schedules.
$ V7 h0 w- r* O5 ], q
7 N* P B4 ^( _6 ~, [ modelSchedule = new ScheduleImpl (getZone (), 1);" I) I! X6 v! J" L+ S
modelSchedule.at$createAction (0, modelActions);
! z6 d0 a; ^, n4 H( g
% Z+ |! A k$ b+ K0 z return this;
2 z/ x0 S J u } |