HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 U! g% F. f0 C( `" ]( |
6 D9 m4 ^, l8 c' E5 `
public Object buildActions () {
9 h; E! H1 {( x" F* V# Q( I4 Z' a super.buildActions();
' ?* M+ G& l* ?- [; _; i! V- ?) S
4 ]0 U6 k7 H- N" G // Create the list of simulation actions. We put these in( |4 o0 a# J. L1 @, _1 {
// an action group, because we want these actions to be6 _. m% w3 W( j- J' t- d
// executed in a specific order, but these steps should
- x. a$ K3 ~$ k7 o! S2 x // take no (simulated) time. The M(foo) means "The message
, s8 w! X& Y8 O. ]9 C // called <foo>". You can send a message To a particular
" [# f: R) p/ E6 @5 t# T. ] // object, or ForEach object in a collection.6 ~$ H0 ^- a8 w" F
9 I4 c# d; b- x' `" n$ K
// Note we update the heatspace in two phases: first run c" e3 w9 N; `, l+ v+ [
// diffusion, then run "updateWorld" to actually enact the
4 k/ ]/ [3 H" ?! o1 U // changes the heatbugs have made. The ordering here is9 g, R' m1 a2 N" M6 v* _% N
// significant!
) R& r# @! R! A H: [3 Y7 k1 F + R6 Q7 x- _# s9 s2 }8 c3 j
// Note also, that with the additional, V5 _8 ~' X# r! U$ l2 c
// `randomizeHeatbugUpdateOrder' Boolean flag we can5 u% K* G6 h. L$ ]. y+ V! u
// randomize the order in which the bugs actually run
% U" R* ~' r- ]. u- i) b // their step rule. This has the effect of removing any3 a" X' n& x. T* Z
// systematic bias in the iteration throught the heatbug3 B3 {7 u- M9 ?4 ^
// list from timestep to timestep
+ ^% ~: I8 o- w0 O5 }
% |4 B% X! I0 ?1 c/ h1 N // By default, all `createActionForEach' modelActions have e" k0 [+ X5 y9 e
// a default order of `Sequential', which means that the
3 I: G2 ^% Y! b1 C) }( Q // order of iteration through the `heatbugList' will be! N" p' A* [1 S6 [# k) d
// identical (assuming the list order is not changed
# Q# B. B! `2 _ // indirectly by some other process).
* Z2 t, B, f( j! ? ( [ o g) p0 p8 k. w7 j. o, u2 c
modelActions = new ActionGroupImpl (getZone ());
2 p7 F& f' u- e+ r
- O: f7 P1 L5 T5 m: W$ g try {
* ?. W" M- D; I; F6 O Q9 _ modelActions.createActionTo$message
2 ?2 ?8 N$ c" p, c6 o5 g) R! W# @ (heat, new Selector (heat.getClass (), "stepRule", false));
% M( A& I& d, \2 r5 M; B } catch (Exception e) {( c6 y4 d/ {( o" }4 q; @5 a, J
System.err.println ("Exception stepRule: " + e.getMessage ());
* c- u' ^7 W5 S: V8 x8 E0 T' m Y% O }9 l5 W+ T: U: J/ M; ]0 Q/ A
7 @: X5 g' R3 e! Y! @0 n4 q' R5 v
try {
/ s3 v8 M/ K! V5 T& o6 n# S Heatbug proto = (Heatbug) heatbugList.get (0);+ D( K2 v1 c9 k6 M- J/ z
Selector sel = 1 r; }4 k$ F' W; q2 l. I- _
new Selector (proto.getClass (), "heatbugStep", false);- o8 P Y6 R( p& o4 n
actionForEach =
! N0 i; @0 q/ L modelActions.createFActionForEachHomogeneous$call7 A9 }. W# P1 G* o) u
(heatbugList,) `! x0 X& n5 q! y# I6 L' N) O
new FCallImpl (this, proto, sel,
. K. G/ I: C( Z: n7 L new FArgumentsImpl (this, sel)));7 u% ~3 \. ~' W- G- v
} catch (Exception e) {
$ ]0 h) G) O5 H: f1 m3 ` e.printStackTrace (System.err);( R- H- o4 U9 A6 F F) F/ Y) ~
}
! r# G4 \' c+ G1 z
+ Z1 ~4 O4 b$ I3 X" K syncUpdateOrder ();
* h( d# C8 ?* C" E/ `; E! N5 u0 n& v9 m7 a4 c, g
try {
: V3 ?& N3 a7 r: ]! Y# _ modelActions.createActionTo$message . K, r% ?! k+ U1 T$ ]9 W
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 h8 a' C, M+ A5 V h } catch (Exception e) {
# a( |2 F) Y9 b+ b, J3 Z6 y System.err.println("Exception updateLattice: " + e.getMessage ());& Q2 b, Y5 l0 w/ t2 Y4 k
}
/ P5 C- I8 V1 i z# Z2 Z
. Y8 H* d* ]4 y- W6 ^& p // Then we create a schedule that executes the% {5 p% c* u" `
// modelActions. modelActions is an ActionGroup, by itself it2 q7 t* |: ~: k# h0 p
// has no notion of time. In order to have it executed in3 l) q/ A; V2 W, s o. I' a& F4 V; y
// time, we create a Schedule that says to use the
. j. T6 p( `6 E9 L5 R // modelActions ActionGroup at particular times. This3 s# J& a) m* ?# R$ _
// schedule has a repeat interval of 1, it will loop every
, {( O$ S2 E- d2 ~. \ // time step. The action is executed at time 0 relative to% P- x& `7 m' g! o
// the beginning of the loop.
% k! O+ {& I! {; ^
* X( w0 ~. ^' z$ o // This is a simple schedule, with only one action that is
* x( x: X. F4 c3 N // just repeated every time. See jmousetrap for more
! X/ N: N" Y2 { // complicated schedules." S7 ]' L7 D6 G* Q6 }' m' D# {/ F
{6 O" x8 Z S/ V, |! T; T9 D modelSchedule = new ScheduleImpl (getZone (), 1);
' C1 a( M h( {0 U6 y K' o) A modelSchedule.at$createAction (0, modelActions);
A& ? n* T2 ~ 9 B% b' e# {. G
return this;
8 \- K% i# |7 D2 U+ `5 K% { } |