HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ B( j4 S, S5 P' D7 I# U
& z; i- r9 S' p
public Object buildActions () {
1 @ J2 M/ G8 \% ^ Z; F super.buildActions();
3 p1 X" d/ W- y" V, i" _% M/ Q 9 C* {4 R1 a: M; i5 h, `
// Create the list of simulation actions. We put these in
" A9 o' a, B& L2 X' t# n // an action group, because we want these actions to be
: |" X. q. v* i! Q% [- U/ Y // executed in a specific order, but these steps should J8 D' | y3 D( u/ P# A
// take no (simulated) time. The M(foo) means "The message
8 c X9 C' k5 } // called <foo>". You can send a message To a particular& u7 i- i7 L& q p, |) a
// object, or ForEach object in a collection.
2 G2 K& N7 g# p1 m7 \7 K
9 @! {" K; v8 |& p* x; S6 W$ j$ j // Note we update the heatspace in two phases: first run' S6 o% e) s6 w4 {; E
// diffusion, then run "updateWorld" to actually enact the
/ d; s) s6 {. j, P. {7 g // changes the heatbugs have made. The ordering here is, ?2 P# |* |" @: i. N
// significant!. @, X+ w5 m! H( q
- P% N+ b) K! j3 {% O // Note also, that with the additional( O9 |) _/ t/ v1 s" O# s+ L
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 n: w! p1 _$ v, A5 q: c$ }
// randomize the order in which the bugs actually run6 c: J( r( N9 |5 p% W
// their step rule. This has the effect of removing any
. g' w7 W# |- D0 e& T0 G7 c# y // systematic bias in the iteration throught the heatbug% l* x. u% ^8 }9 n" Z
// list from timestep to timestep
& x2 v1 P+ S: |5 h% @1 ` $ h% _% U0 N+ O4 j5 V# [% u
// By default, all `createActionForEach' modelActions have1 z& T; _- p i3 v7 ]
// a default order of `Sequential', which means that the
, F/ V& x; v. u9 k // order of iteration through the `heatbugList' will be5 ~: t$ ?3 @0 [% y8 _! R
// identical (assuming the list order is not changed) G- t0 p6 r& `8 x6 h T: Y
// indirectly by some other process).
3 b, U& U4 X7 E9 d# h/ z! S4 S
6 l( O& v8 ` K h modelActions = new ActionGroupImpl (getZone ());
- U; ^& H, \( M8 U2 j5 d+ e# |9 E5 P7 `, ^" ]% U2 X
try {
$ o. t3 p4 I& |6 z# P8 K- r4 f6 d, r modelActions.createActionTo$message
+ k" z0 ~& X# w (heat, new Selector (heat.getClass (), "stepRule", false));
2 m; j6 T: N* ?( B/ Q) z } catch (Exception e) {
' D4 @$ q) G/ {# ]2 l6 ]+ S, G. T& K System.err.println ("Exception stepRule: " + e.getMessage ());8 f0 u5 E5 z! m) F
}
/ x8 P/ x' d5 v" n7 S! |9 ~' _ P' \ z
try {6 ^4 F* b& f1 Z# H" T1 S
Heatbug proto = (Heatbug) heatbugList.get (0);: \' O" C, s9 P5 `5 O# n4 b
Selector sel =
, [3 V% a' n, |# l2 g8 O+ D- G' j+ M new Selector (proto.getClass (), "heatbugStep", false);) J; V3 O# Z9 J! N! E, I3 ?5 w
actionForEach =! U0 E. B) Q( w- t" z( n( l
modelActions.createFActionForEachHomogeneous$call0 |/ p+ P& K- B& V) n$ w3 y
(heatbugList,
5 P6 r% E6 ^, _* g: B0 ]2 C new FCallImpl (this, proto, sel,
! t; X, k8 V, U new FArgumentsImpl (this, sel)));- P: e! X* n& g4 @! ?* A
} catch (Exception e) {! f6 ~, `) D: @
e.printStackTrace (System.err);0 i0 D9 a W0 M8 I/ K
}6 n) e u( A) J L9 l8 y
" }" r7 {% `8 I3 ~$ l
syncUpdateOrder ();
( i! o. H6 _9 |$ G* `+ u9 s- G8 ~ V' A4 J6 Q6 y; @- g
try {: i3 w1 ~' l1 b; ^. z
modelActions.createActionTo$message
4 H9 `, k0 I+ B$ F& ^/ {) T (heat, new Selector (heat.getClass (), "updateLattice", false));
/ `5 {/ }8 k" w } catch (Exception e) {
3 L; J/ p( ?) z$ M System.err.println("Exception updateLattice: " + e.getMessage ());
. H$ x2 j0 d# U( k5 L }
2 V7 A# m' V* w: Y3 L
4 K, {9 r- g! C) w. o; Q! C( X // Then we create a schedule that executes the
# a/ w) M- a( i1 H6 Z( c+ _ // modelActions. modelActions is an ActionGroup, by itself it
( F! C$ U& h X. r+ ? a // has no notion of time. In order to have it executed in8 b3 D W2 S4 h4 ^
// time, we create a Schedule that says to use the
( r. \2 Y! B& X* E# A // modelActions ActionGroup at particular times. This9 q* @ z( {! E3 [2 l# {
// schedule has a repeat interval of 1, it will loop every
# p, q$ H1 d, @1 i3 _7 K7 ], H0 K // time step. The action is executed at time 0 relative to" }; l& A* f/ Y6 _ n! O6 ?
// the beginning of the loop.1 K4 o# ^. D) _. y+ t
( w1 i! n, V' V
// This is a simple schedule, with only one action that is
8 A- C# N$ g1 k! C1 q3 [* x // just repeated every time. See jmousetrap for more6 M o/ \& r2 Q, \
// complicated schedules.- c- a; j" t& @, D
! A8 N( G+ ^6 }% z
modelSchedule = new ScheduleImpl (getZone (), 1);" Z# o7 K9 K3 e b) Y# [) ~$ `! v
modelSchedule.at$createAction (0, modelActions);
# a- t( f( b! j( J# F& Q! R
* q# `# z; ]! {; u! s" T return this;! k9 R7 @! L Y# ]2 o" C
} |