HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 N4 `- `6 @' q g! y6 o' d
7 G$ x$ c5 k5 ]; p0 G3 U. P9 h public Object buildActions () {, l. C) C2 ?1 w9 ^% L
super.buildActions();
& {$ p1 {* R$ L9 M3 j1 R
/ R% t! c, n) b // Create the list of simulation actions. We put these in
" q8 a# h& e# {, m$ w: m // an action group, because we want these actions to be
/ N! A* _& U* V4 M) s6 E // executed in a specific order, but these steps should) q; d0 o9 G/ E- ^
// take no (simulated) time. The M(foo) means "The message; K/ K2 i4 l5 L4 c1 R* G5 r. C
// called <foo>". You can send a message To a particular
4 K. M5 y2 O7 j' j- |+ t // object, or ForEach object in a collection.( }3 d/ t1 O, x$ p8 E
$ g4 n2 ?$ w( i8 p# B
// Note we update the heatspace in two phases: first run
! Z% S4 V$ ^7 k/ f5 `% m // diffusion, then run "updateWorld" to actually enact the% F3 x1 R, c/ S0 Q, K
// changes the heatbugs have made. The ordering here is
9 z" R" |5 d' S5 }5 Z! m, W% C2 o // significant!
2 M: u, A3 r1 o! I5 f
- m9 @' _# {4 ?8 s$ J5 L4 I // Note also, that with the additional
# L& R7 V# K! J // `randomizeHeatbugUpdateOrder' Boolean flag we can
. p3 g B5 K: `0 w& O' O" A // randomize the order in which the bugs actually run
- z7 L/ B6 r7 V: i3 H // their step rule. This has the effect of removing any
^8 f6 h. Y7 y& v, z // systematic bias in the iteration throught the heatbug- H- x; I5 I O
// list from timestep to timestep
t; `6 [ X$ \9 J8 l+ k % y, n# F \% ]. E. ]( \4 P
// By default, all `createActionForEach' modelActions have+ [2 o0 S, d/ M& H# F6 G" }$ F
// a default order of `Sequential', which means that the
9 ^9 \% N8 b: R/ ^) W // order of iteration through the `heatbugList' will be" y+ g7 z. l! K* k q2 G, ~# N ]
// identical (assuming the list order is not changed2 C) _" G [, R
// indirectly by some other process).
; d3 @+ E& H) Z/ p- D* V. _" d ! O+ m! v9 ?/ i$ D6 ]% P& j
modelActions = new ActionGroupImpl (getZone ());; Q! U7 I' g* ~2 d( D" C/ q9 q
# k9 _/ Z R E try {9 j, a9 L3 \' y
modelActions.createActionTo$message
5 o% n) A S" g- R% i3 | (heat, new Selector (heat.getClass (), "stepRule", false));8 E X- J% k7 b4 K& n
} catch (Exception e) {- c1 y& p0 A' K0 {2 M5 g0 K# Q
System.err.println ("Exception stepRule: " + e.getMessage ());
) S7 p) G, c* E1 k4 |2 p( e7 T L0 @ }
8 }5 E A- [1 @' D/ {
0 b7 T$ T. e( {6 |, D( |7 t try {3 s( T5 G8 ~. v5 Y, i. d5 b+ o) E
Heatbug proto = (Heatbug) heatbugList.get (0);2 N2 J& t: x6 H5 t4 E5 c
Selector sel =
0 {* t; g# o ~" @ new Selector (proto.getClass (), "heatbugStep", false);
. l1 W j$ M/ E# U. L actionForEach =6 \! n8 k9 l; O. W5 R1 { r
modelActions.createFActionForEachHomogeneous$call% V* e5 S$ M) A! n
(heatbugList,; C$ |9 |+ P, ]' i
new FCallImpl (this, proto, sel,
/ |9 e7 d3 Y- ?& j/ A- G" v& T new FArgumentsImpl (this, sel))); v) T& F5 R* t, K4 M
} catch (Exception e) {
, V0 X, D# I ?5 e e.printStackTrace (System.err);" M) c# }, E- ?* }5 Y2 ?
}0 B7 H& D$ J! }! L& o2 j: F" J
. f D- T) w! Z1 H syncUpdateOrder ();
/ A3 k# m% G+ W& Z5 P c
3 r4 V+ ]: f5 ~0 S try {
# l7 X7 u' Z" j% k modelActions.createActionTo$message % q, r/ t5 x6 F2 Q% _$ j$ O" {
(heat, new Selector (heat.getClass (), "updateLattice", false));8 S7 N; f5 Z+ B, ?
} catch (Exception e) {
% U7 `7 o9 m6 t6 N6 _. G System.err.println("Exception updateLattice: " + e.getMessage ());9 {% P; _) u6 @& A' N
}$ G9 w( p7 G. b# ]/ X2 V! u1 R
5 T+ P: M( L5 ?$ x; `3 p
// Then we create a schedule that executes the
' _7 Z" l9 D8 A // modelActions. modelActions is an ActionGroup, by itself it" P. D3 N( |7 f
// has no notion of time. In order to have it executed in _ I1 R" _ y# ?6 z. e! X. P
// time, we create a Schedule that says to use the
$ F7 ?6 [) G" k5 w // modelActions ActionGroup at particular times. This5 _" m9 s6 z( {8 q& f6 ~
// schedule has a repeat interval of 1, it will loop every
4 D$ D2 O3 y) s8 n9 k4 ~ // time step. The action is executed at time 0 relative to9 O0 d' X( f7 Z* |, I
// the beginning of the loop.( _; P+ ]3 C/ \# d6 ~
$ [5 s) _& f6 N) n) u
// This is a simple schedule, with only one action that is7 i! A+ x( S _ E
// just repeated every time. See jmousetrap for more" C3 N3 H5 y1 d
// complicated schedules.1 b* f4 D z) G) f; y4 f2 O
5 G. @4 E( [2 [/ x1 j
modelSchedule = new ScheduleImpl (getZone (), 1);
. U- A% X$ _' X3 @0 N5 A! N" J modelSchedule.at$createAction (0, modelActions);4 H' g7 D k; ^8 L9 V0 r5 H6 a9 h
7 `5 u) Q p& h9 h return this;) }/ Y5 U+ k/ ?( l) `
} |