HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 n: u! v u# ?* @7 v7 L# \6 j
; I, ]' y' J: C! v0 q& o' S- n
public Object buildActions () {
# l& V+ Y3 V5 f2 C super.buildActions();9 G* s$ n- i' o: ?2 [5 w9 P, L! n+ N
* v$ @! m# x4 Y% J: K$ B
// Create the list of simulation actions. We put these in
' f1 L5 B/ c) H, h: ] // an action group, because we want these actions to be. a% ]& r) s6 X) v
// executed in a specific order, but these steps should$ u/ K u0 [7 l* d( x2 k7 F- o
// take no (simulated) time. The M(foo) means "The message- C, }4 x: u, e: ~
// called <foo>". You can send a message To a particular! O2 m7 a2 H5 h/ t) B
// object, or ForEach object in a collection.
! L2 N# ]) K/ @ g* i( v
1 _4 j* q9 E! m- [) y // Note we update the heatspace in two phases: first run% Q+ e1 U S4 U m0 U3 Z7 [% F
// diffusion, then run "updateWorld" to actually enact the
5 U# v- E% u [. u& J // changes the heatbugs have made. The ordering here is
4 T' M" w/ X* [. C4 Q // significant!+ r9 M, R! S, G' {7 `
6 ]9 T0 B( M0 e3 G& r4 Q // Note also, that with the additional
$ R7 G; U$ f+ Q // `randomizeHeatbugUpdateOrder' Boolean flag we can+ G, x! {" U3 R: j, z; ^( L7 x) H5 A X
// randomize the order in which the bugs actually run
" z' O0 r& h3 V) x // their step rule. This has the effect of removing any
8 a: t7 f {$ n+ f // systematic bias in the iteration throught the heatbug
+ c" g& r; A1 I5 u2 R, A" Q // list from timestep to timestep* k+ B9 n( f' a7 l& g$ U/ o1 ^
) R5 |" D4 x0 O' T4 C
// By default, all `createActionForEach' modelActions have# {# ^9 H9 \7 ~, i _
// a default order of `Sequential', which means that the* _5 P$ x' N: {+ P
// order of iteration through the `heatbugList' will be
) M7 P1 @2 D0 F* r# `2 V- Y1 G- @ // identical (assuming the list order is not changed# W) W5 Z0 {! {
// indirectly by some other process).. |4 d, X1 \& m% k3 l" q
3 c0 D; k8 z B. W modelActions = new ActionGroupImpl (getZone ());' J8 e$ n* ?- k! \9 v+ n
1 Y' a0 t" U, O
try {
6 `) @8 x9 @4 l& `$ _ modelActions.createActionTo$message
1 N- ?# N0 M; n& |( A' x- a: o. N (heat, new Selector (heat.getClass (), "stepRule", false));; i) j: `& n4 K4 _2 V. |
} catch (Exception e) {3 ?$ c7 n5 M3 q6 g
System.err.println ("Exception stepRule: " + e.getMessage ());
, p7 g/ B1 v8 \; G5 d* H9 K }
) b* X% a4 s; D& e0 V1 G
[3 B2 ^* r5 h9 J1 H$ v j try {- H0 Q) Y( g" k+ [
Heatbug proto = (Heatbug) heatbugList.get (0);, W# u& |/ Y0 \
Selector sel = / |/ q7 @8 v* E" K* J. w
new Selector (proto.getClass (), "heatbugStep", false); k4 W* U9 |. }* y2 r
actionForEach =
; J4 r) B5 F9 x2 E: u% D/ A modelActions.createFActionForEachHomogeneous$call) I! ^/ X$ X8 s$ `' Z
(heatbugList,
{6 a2 e9 ]. `. w new FCallImpl (this, proto, sel,
9 o+ v: q) Y2 F new FArgumentsImpl (this, sel)));( N! S9 A; u5 l
} catch (Exception e) { M/ h- V. J9 m) Z
e.printStackTrace (System.err);
9 C; L4 o+ V* V" j3 Q# w* P. E } \+ a- R4 H \. Z
x: Q0 [# O7 s6 h
syncUpdateOrder ();2 [ z! {% R9 A6 f5 p2 g
& F7 e0 g9 |" \! E" P try {
& y a" f9 W E( \1 ]% F3 {6 R modelActions.createActionTo$message
& t# h4 n; S' B# x2 y (heat, new Selector (heat.getClass (), "updateLattice", false));
+ X% G- G7 R! g n7 D5 x5 ? } catch (Exception e) {
# O6 u& G& @+ w: z$ u System.err.println("Exception updateLattice: " + e.getMessage ());6 F9 I# G4 [) _ q/ x- l
}& d, K' K7 m0 d. J) m
0 X4 ^5 X) T; `8 I U
// Then we create a schedule that executes the
. U* Z8 k8 J2 Q/ o // modelActions. modelActions is an ActionGroup, by itself it
% d! N- [7 C/ X // has no notion of time. In order to have it executed in
9 {; ~5 Y4 I4 v4 P/ w // time, we create a Schedule that says to use the
9 V- f; x$ a$ \. F2 I; A% z // modelActions ActionGroup at particular times. This
) |: R. G8 U2 h8 m, \ // schedule has a repeat interval of 1, it will loop every1 Y+ c& w' V1 z9 w3 J
// time step. The action is executed at time 0 relative to8 H! i' z3 z1 I0 T, ~* ]* C; ]8 ?5 p
// the beginning of the loop.7 Y4 t- N* X6 `) M
; K' P2 g5 @# I, U* H: J: t7 m
// This is a simple schedule, with only one action that is
- k+ t8 Q) z7 y5 |; _6 p" W! r" G% P // just repeated every time. See jmousetrap for more; M5 ~% R" T+ Z) {1 m! q! s2 `/ E: C
// complicated schedules.) q ]' Q V/ n2 Q; G! a* n
+ i# O: u/ o: m T7 P8 u modelSchedule = new ScheduleImpl (getZone (), 1);5 Z! h/ N! B3 E
modelSchedule.at$createAction (0, modelActions);7 A( x9 c& i9 e& Y' t5 m+ K
$ S: V1 C$ _9 e5 A- u4 ^ return this;( [, s) | K# Z
} |