HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
n2 n2 r; T5 l7 X! T# w9 \' @2 w
public Object buildActions () {
$ C {/ Z# }3 j7 c super.buildActions();
J/ \( t. N F( E' G$ {6 x
- I) a- a$ M( N5 O D; X // Create the list of simulation actions. We put these in- x: z# j' ^( g/ i: T6 i1 s
// an action group, because we want these actions to be
0 Q& ~/ T% c/ w9 [7 E' l. ]5 \. v // executed in a specific order, but these steps should, ?: o/ b3 z- p
// take no (simulated) time. The M(foo) means "The message$ \3 ~9 ?' w: f/ P
// called <foo>". You can send a message To a particular
! Z+ B7 k+ o% ~) p C! e2 V // object, or ForEach object in a collection.4 X7 W u( X# ~
! [5 z8 Y4 X1 w" ?
// Note we update the heatspace in two phases: first run
3 {! k4 j8 }; R* M4 b. {% z% b5 r // diffusion, then run "updateWorld" to actually enact the
5 w5 \( }* e% @ // changes the heatbugs have made. The ordering here is1 y6 b- }# m e+ |( V8 H" Q
// significant!
6 m4 t* E3 }0 d" w @3 w# y* H+ u , r$ u6 G7 D- u! E
// Note also, that with the additional/ O7 f: M1 S5 _9 n4 G
// `randomizeHeatbugUpdateOrder' Boolean flag we can& J3 \# K7 b- S$ m% u4 E& ^
// randomize the order in which the bugs actually run
& q7 w/ W6 @9 s& \8 L! z. P$ R // their step rule. This has the effect of removing any& [2 z8 J, ?! P+ n
// systematic bias in the iteration throught the heatbug0 }. O# o' b K5 x2 Y
// list from timestep to timestep
& V; i/ L! c! m4 [
- T0 C# Z2 h! w$ E! t. X8 I+ S0 I6 j1 i // By default, all `createActionForEach' modelActions have
- T) p6 H0 n6 k% m1 `; _- D0 Y // a default order of `Sequential', which means that the- r) N8 p: d$ F5 \, X# u% P
// order of iteration through the `heatbugList' will be
* R/ w9 w0 R: {; e8 ]3 @, ~ // identical (assuming the list order is not changed
: s2 x6 {9 v+ p9 F* \ // indirectly by some other process).
/ L* c8 Z- R$ B7 _: d - Q* w" z& J) I6 \! G' l6 |
modelActions = new ActionGroupImpl (getZone ());
( ?2 v: C; a5 V# S. t3 l% s( j! ?8 i, f6 v# b
try {5 s/ P* I* x8 _9 i- k
modelActions.createActionTo$message, h k0 n* T9 v5 G3 n' y4 V" e) ^
(heat, new Selector (heat.getClass (), "stepRule", false));
e$ O6 |( w) b } catch (Exception e) {% z6 X: u8 X2 e, Q
System.err.println ("Exception stepRule: " + e.getMessage ());
( U v+ f$ e9 C8 X3 q }
; T B1 a4 p$ v0 A' }' x' ?% N: U' N# ]4 W
try {" [: L, }2 L& _+ w% _$ p
Heatbug proto = (Heatbug) heatbugList.get (0);
/ T5 V$ I- I" m9 H' L6 L( s Selector sel = & [" ^, J4 q$ O6 H7 A
new Selector (proto.getClass (), "heatbugStep", false);/ w+ m+ C6 l/ f- i
actionForEach =
* D# k3 `5 R+ \# z modelActions.createFActionForEachHomogeneous$call& z$ Z7 ?" G: r/ k3 c$ s* h3 T
(heatbugList,
: j: ^; K3 D7 N3 F: v7 w/ K new FCallImpl (this, proto, sel,
6 `+ S+ c% q8 D' @% d new FArgumentsImpl (this, sel)));
: {, _3 V; s' ]& ^ } catch (Exception e) {
i- p) E% x9 p7 v. `2 |& j. k, D' J3 b* W e.printStackTrace (System.err);9 q" b r" d" ^# F; {/ \4 @
}# Z4 @; f9 j. z3 h" e
% E* K" v: l* M' V; |
syncUpdateOrder ();
! d( j& E' M6 _. u: ^& v( I) Q
* a8 n# Q' o7 _ U4 z3 s try {
1 F+ s+ I! o" ]* F0 w0 s modelActions.createActionTo$message
6 `. U" F" e( G& H/ b5 j (heat, new Selector (heat.getClass (), "updateLattice", false));
* E. ~* h* q' j0 I- x% u, y" |& | } catch (Exception e) {
' g' I' w ?. V) E, a% o System.err.println("Exception updateLattice: " + e.getMessage ());7 j, u: ]* }3 l. x5 z3 E% R8 I
}9 j2 o* L2 @9 m8 R% }' Z
+ }) q0 T- L) \ // Then we create a schedule that executes the: ]! U: G/ }$ a' q) t/ Y
// modelActions. modelActions is an ActionGroup, by itself it, R5 ?/ x2 ]/ e+ O
// has no notion of time. In order to have it executed in8 _% E% \7 R5 Z K+ u
// time, we create a Schedule that says to use the
3 M1 k1 F1 H |( L // modelActions ActionGroup at particular times. This
F' H% @+ ~& ~& t2 j3 w // schedule has a repeat interval of 1, it will loop every1 h! r7 U, P7 b" M5 {% y( C
// time step. The action is executed at time 0 relative to
0 y5 p$ o7 j! |. ~$ W; | // the beginning of the loop.$ _4 Y/ ] e: k+ N$ r+ ^ ^
6 @: _. H) f+ L3 l4 _
// This is a simple schedule, with only one action that is0 B. C" ^. A* \: _
// just repeated every time. See jmousetrap for more5 x7 Y8 G$ D3 R: c, D
// complicated schedules.- G" V+ }8 z1 M3 Z7 N
4 A5 W. I5 z1 W1 |* c0 q modelSchedule = new ScheduleImpl (getZone (), 1);
" m- l" J$ ^' O, I modelSchedule.at$createAction (0, modelActions);: R) I g* C+ y& G% g6 Z
+ L( x9 @- M% @" R8 ?* y6 U return this;9 u8 j: }. }. E r- Q8 \+ @
} |