HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
- m: l( S2 u2 q# K6 N5 ~
2 G% O: |; S( b; \/ V5 A7 @ public Object buildActions () {
, e0 o" C( H! Q: b" M5 `6 w: K; u super.buildActions();4 V4 q+ s7 A9 }8 O
* k+ U/ g3 X2 N // Create the list of simulation actions. We put these in
# r! D h! h' z# ~. h# j // an action group, because we want these actions to be: x9 M. z4 s8 u
// executed in a specific order, but these steps should( _) r# P% {8 Y, v, M0 G
// take no (simulated) time. The M(foo) means "The message
1 H+ [6 j! h. E // called <foo>". You can send a message To a particular
' C1 v2 v2 \! j2 F // object, or ForEach object in a collection.6 |+ @! E$ O- x
' ^1 C2 P$ g. T! F# v
// Note we update the heatspace in two phases: first run0 v% ^+ E+ {: T7 H" p
// diffusion, then run "updateWorld" to actually enact the+ ?8 o' B2 R& K4 M
// changes the heatbugs have made. The ordering here is' c3 q3 V$ ]; B
// significant!% ?( Q6 _ ?& H. e
6 w! n0 X9 F* H/ M" f* [6 g
// Note also, that with the additional
$ O _6 `) I- p, J5 q5 O% V" Y2 ^0 w# F1 t // `randomizeHeatbugUpdateOrder' Boolean flag we can1 m2 B. O7 `& W$ o, g/ A
// randomize the order in which the bugs actually run
& B8 T. l5 {* z8 P // their step rule. This has the effect of removing any
+ m; r9 t2 @$ c9 r' Q // systematic bias in the iteration throught the heatbug
4 p7 i/ ]" @" M // list from timestep to timestep2 V" R2 y3 r. U4 H
9 R6 a6 _: a9 Q/ S/ \; n+ {# \3 I // By default, all `createActionForEach' modelActions have0 r7 ?- E3 B7 y L s3 _! R9 {6 k9 G0 d
// a default order of `Sequential', which means that the4 C& F+ ~+ u" N2 a
// order of iteration through the `heatbugList' will be ]3 U& \! ^# Q0 O% A/ W
// identical (assuming the list order is not changed3 m- X3 M9 O6 p
// indirectly by some other process).
/ L. L/ o% P. H m, D+ L+ g6 }1 \% J 5 u( S& c5 A* a+ Y3 ~) n* O
modelActions = new ActionGroupImpl (getZone ());
1 R- P7 g+ w, O% c/ T% i/ s; w8 ]8 G# { p4 I) B
try {3 q% S9 \+ Z$ c0 |
modelActions.createActionTo$message9 c3 e+ s+ a" E- \& _
(heat, new Selector (heat.getClass (), "stepRule", false));
5 q) o7 ~) F w2 P n* X. X } catch (Exception e) {
6 }) k6 e9 c! J' t7 K$ Q3 U# m System.err.println ("Exception stepRule: " + e.getMessage ());
( G$ ~2 k# ~. D9 P1 y1 w* V+ X J* E9 r }& l9 |1 e, N6 C7 s
+ \ x ]) }% t1 R2 M+ T/ Z2 |3 E' A0 \/ B try {0 Y, K. ], ^/ P) u% b6 t- i+ S
Heatbug proto = (Heatbug) heatbugList.get (0);6 X8 J: M; b. [3 d7 F
Selector sel = 6 D, o2 V! p, |6 u) p+ F$ X( Z
new Selector (proto.getClass (), "heatbugStep", false);1 ~; ^ z" ]9 [; f( d g- f
actionForEach =
) M$ C6 V+ s. E) S modelActions.createFActionForEachHomogeneous$call
/ |/ B0 F! V' @: Q$ u (heatbugList,
. A7 F1 b% g& ~$ | new FCallImpl (this, proto, sel,. E: d7 P9 F5 h! q7 R
new FArgumentsImpl (this, sel)));
- z; M: [: C2 O } catch (Exception e) {$ i' }7 @/ J5 Y$ J) ?/ Q! R
e.printStackTrace (System.err);
6 i% U' h4 [0 s- H7 F }/ Q& Z/ V ]0 q) L4 p- B9 H2 M* X1 @
. R( r0 ]6 g. S. {( T" Z syncUpdateOrder ();" q* h7 }) e# ?; @# p& `
* L4 V+ B8 o* x, y5 [/ s7 V
try {( U% s* h- d$ w: T/ S
modelActions.createActionTo$message ' L4 n. X3 Z' K
(heat, new Selector (heat.getClass (), "updateLattice", false));
6 g/ H5 x8 m' u4 I } catch (Exception e) {( Y5 @5 E2 t) j) h" X
System.err.println("Exception updateLattice: " + e.getMessage ());) g+ L5 L& k1 a" g3 A3 X T0 Z( W
}8 `2 S& B: O& U( |' m
- @1 v3 d) S5 L' Q9 q // Then we create a schedule that executes the
- R7 M# {% t- E/ ^- n+ C9 l& M // modelActions. modelActions is an ActionGroup, by itself it% R# ]4 ]7 c# V$ q0 C
// has no notion of time. In order to have it executed in
% P0 y5 v, o7 p- n+ T) B% |5 d; C // time, we create a Schedule that says to use the
; ]# @1 a/ u) k$ ~0 Z // modelActions ActionGroup at particular times. This; L* x5 _& d: m
// schedule has a repeat interval of 1, it will loop every- N( `) A: E: s* s; i
// time step. The action is executed at time 0 relative to% N! N/ e. q m& J6 v
// the beginning of the loop.$ i* s9 b# ?) M1 K* B
8 r% W2 q5 Y5 ~ u P
// This is a simple schedule, with only one action that is
* A+ s" J. j; |' Z( p) `& J8 x0 l // just repeated every time. See jmousetrap for more
1 _7 `4 H0 g" t, Y% ]% e; g // complicated schedules.
; @6 q! J0 t3 @5 O 6 }- e" b |7 W* Z0 i* K
modelSchedule = new ScheduleImpl (getZone (), 1);4 y+ T! W' ?2 N& x: _" m) o
modelSchedule.at$createAction (0, modelActions);
6 }+ q2 L. z+ z. N$ V1 t' U + W8 {3 O \, F6 Q, a
return this;4 x9 ~7 v2 F7 a2 y/ f. I! m" |
} |