HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 z+ M2 `0 h/ _8 v) |, d% g4 z
/ w- z8 Z8 R4 _3 m3 h8 L public Object buildActions () {1 W0 q3 ~# |5 U* b" V% U+ ?9 O
super.buildActions();
/ K5 [7 G% d! M2 |
* q5 D8 W% ]$ W; I+ ? R; E9 E( [ // Create the list of simulation actions. We put these in* @/ C! O4 f2 \' ~. N
// an action group, because we want these actions to be
R/ x5 Q( u2 {: u; m8 L // executed in a specific order, but these steps should$ {3 f% X, j, @8 c1 V9 T
// take no (simulated) time. The M(foo) means "The message' ~0 o2 L K8 k* O. Q
// called <foo>". You can send a message To a particular
3 k. p* S' z" p% K. H$ ~' i2 \5 K4 I5 N" I // object, or ForEach object in a collection.
( s* `/ g/ }: X2 Q H, b . V+ q* M- C1 v9 c7 ?) x/ x
// Note we update the heatspace in two phases: first run3 u: @ W$ d+ W9 A; M, Y
// diffusion, then run "updateWorld" to actually enact the6 I0 {8 Y( B7 P, i; |, g% [
// changes the heatbugs have made. The ordering here is
: D3 D* a e2 h, J // significant!
2 A9 O# \, {1 R4 [/ M
: K- Y) T6 Q( l- M, @" b // Note also, that with the additional
! R& _3 p7 l" E- y$ ^; b- |9 M; E // `randomizeHeatbugUpdateOrder' Boolean flag we can$ w2 o8 E/ l9 P1 v. Y, a* [7 _
// randomize the order in which the bugs actually run, @) }+ h1 ]. g/ k& L
// their step rule. This has the effect of removing any
1 ]& E" o ]2 U4 P5 G* z9 v4 M! z // systematic bias in the iteration throught the heatbug$ ] G0 g$ T4 E! x' t4 w, t; ~- A
// list from timestep to timestep
' r% M4 w( G1 b$ j ! M: I U- e2 C7 K: O
// By default, all `createActionForEach' modelActions have% A! v1 _+ B: z7 H& L" `
// a default order of `Sequential', which means that the
! y+ A3 T. d7 i4 \5 Y // order of iteration through the `heatbugList' will be& l0 j. G: D9 h+ ^- h. x
// identical (assuming the list order is not changed
8 r& ~ q G7 p; v5 z // indirectly by some other process).1 r: Q- _9 p+ _4 G" m' V) j
# S; I, d% @; w2 Z modelActions = new ActionGroupImpl (getZone ());
e; o4 h% s0 H
# V, n2 {3 q; t Q# q try {
" b O& `7 c) _3 F3 s0 } modelActions.createActionTo$message
* y( {5 R, \# e) P# T (heat, new Selector (heat.getClass (), "stepRule", false));1 ~5 [" n3 S* `. j3 w6 B! G7 e
} catch (Exception e) {1 o# o- x4 R5 p% a2 e) D: y
System.err.println ("Exception stepRule: " + e.getMessage ());+ D$ P0 x4 {( L5 j
}7 o! M- Y! B5 U
8 F6 M5 A6 n2 I2 [2 `6 s3 v6 w try {: l7 H9 V6 a) c h# D' m% D
Heatbug proto = (Heatbug) heatbugList.get (0);
8 X9 c, @0 b9 v3 i5 q7 Z Selector sel = ) L( C. @, H7 k+ Y9 p9 N
new Selector (proto.getClass (), "heatbugStep", false);
& r# _0 F6 Z# g9 v* Y9 I. W9 ?3 t actionForEach =1 D2 h/ j* ]! c3 _: A3 C
modelActions.createFActionForEachHomogeneous$call' K J y* @/ h$ @
(heatbugList,
! w) k8 N/ r# S) E+ k; `8 z new FCallImpl (this, proto, sel,
, a2 M+ q* V4 i- z9 i% A& ?( W0 ~ new FArgumentsImpl (this, sel)));
. x" I4 a6 n. Q1 l; K; Y! N3 l$ _/ K/ t: C } catch (Exception e) {
% u- ^6 O4 U+ c6 w9 g e.printStackTrace (System.err);* K: m" ^& t9 b0 G* f
}
+ {: {; C& m# q( Y2 g: Y
3 h' n" `( x9 M6 ~ syncUpdateOrder ();
; [7 D* [% B H% j* ]* C7 @0 k7 t6 c
9 {& v/ ]+ d% E4 }. \3 ? try {
; N) @" X2 g5 L; }+ F modelActions.createActionTo$message
0 A: X/ }. ]7 h (heat, new Selector (heat.getClass (), "updateLattice", false));
3 L6 o, U8 g8 N% g } catch (Exception e) {2 S7 r5 Q2 q% O. V
System.err.println("Exception updateLattice: " + e.getMessage ());5 f) \1 Y* P* y' y* b* ?
}; x& v8 E6 K& k! W' g0 X* F. ?
_1 n7 ~+ s, S) x% P
// Then we create a schedule that executes the( _' h% s2 A, |1 q
// modelActions. modelActions is an ActionGroup, by itself it, O- ?0 \; T3 H
// has no notion of time. In order to have it executed in
% O% {6 T$ r" J& f* ^ // time, we create a Schedule that says to use the
8 g) d) |8 S, ^ // modelActions ActionGroup at particular times. This( f6 n( N' r8 t A
// schedule has a repeat interval of 1, it will loop every) E0 g1 X( |+ Q. @0 b% p+ W+ C; V
// time step. The action is executed at time 0 relative to
3 l2 d2 x5 |/ y. E! P4 b // the beginning of the loop.3 Y0 D0 c3 V0 t0 Z5 w( f/ Y
3 Q% A. a4 @+ a) u+ P0 ~0 B5 r7 z // This is a simple schedule, with only one action that is3 T0 f. \8 e2 C0 u* I, S
// just repeated every time. See jmousetrap for more
/ O+ ?( Q! n! S5 j! l. P' g // complicated schedules.
7 X) `& J/ s$ t6 {5 Z/ Y& N8 k
" ~: ^' G7 G! ~( x/ \ modelSchedule = new ScheduleImpl (getZone (), 1);
- v8 r. ` s+ i" D- V modelSchedule.at$createAction (0, modelActions);
, N! D( `2 [! @* c2 Q6 |
& n3 ^9 ?% F2 A0 S4 C return this;
$ O6 W8 E6 q" D1 f L' F } |