HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 ?- K4 W3 l4 S* w+ V" w0 \( n# p% y- v' o t
public Object buildActions () {
5 r8 U! p! \2 M& _3 d$ R super.buildActions();
1 e" n! q% H L+ r2 q
6 I4 O8 t9 D$ R // Create the list of simulation actions. We put these in
$ `2 v' j) P' @3 b3 ?- P7 F& F // an action group, because we want these actions to be- K$ o9 G+ G8 p, v
// executed in a specific order, but these steps should4 b6 l8 ~& o6 ?7 m+ s. N
// take no (simulated) time. The M(foo) means "The message
8 F+ N, `8 M' L9 Y+ f; ]7 c // called <foo>". You can send a message To a particular
% p( U; a! u3 a9 D( S' ? // object, or ForEach object in a collection.
7 Y4 C9 D6 n' F# I8 f! E
& A" n2 v$ }! Z // Note we update the heatspace in two phases: first run
& c* j. ^' D l5 }. [4 y$ S, k* u // diffusion, then run "updateWorld" to actually enact the: Q* ]. H; M& g& X
// changes the heatbugs have made. The ordering here is; u( R& U' x+ O- E. s
// significant! f/ I# y$ D( L5 P3 U, A. ^
+ X3 V' C$ k5 K8 F* n // Note also, that with the additional
: U3 L( r* e/ R- O- t% ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can
, J( @' Z/ P" n/ O$ I4 Z/ f: w# { // randomize the order in which the bugs actually run
# F( o0 b9 S% B4 \ // their step rule. This has the effect of removing any0 }6 b3 K; `6 d. L ~$ l) C
// systematic bias in the iteration throught the heatbug
' b: [( L' } x* k2 r+ ^ // list from timestep to timestep9 S, V. w( o5 F
: S/ C% Y, O3 e# Z" } // By default, all `createActionForEach' modelActions have
- Q1 ~& g& M. ]- M4 E' f* e$ i+ w i0 g; Q // a default order of `Sequential', which means that the
" w$ `: Y' e, c5 A // order of iteration through the `heatbugList' will be
) O _0 D( Z% C: e+ I/ ~ // identical (assuming the list order is not changed* ]: B5 L* f$ @7 r9 b
// indirectly by some other process).9 r# t) L/ b3 C" t9 ]6 K% B9 l3 z
2 g& ^# N5 d @# k: ]2 o
modelActions = new ActionGroupImpl (getZone ());
a! F6 x; m# R3 Y0 Q. w2 _- C' @2 d& N. ]1 H
try {/ O$ Z. Y8 U; V, A0 {( ?; q0 |
modelActions.createActionTo$message! M' }9 q/ u" g1 a- `
(heat, new Selector (heat.getClass (), "stepRule", false));
7 D3 L3 ?& N: H$ ?4 u' ? X9 ~, ? } catch (Exception e) {& Z8 g. x# N4 g1 z& ?- C
System.err.println ("Exception stepRule: " + e.getMessage ());- w' Z, A& [/ h, @2 M$ z' d
}
* W- U0 z# u/ K4 X* \' M A$ G2 c3 `! R" g6 H
try {+ T% f/ [5 B8 Q; e9 Q
Heatbug proto = (Heatbug) heatbugList.get (0);% q( @* I* n7 q% g
Selector sel = ' V5 r8 k) x; G0 x: ^) q
new Selector (proto.getClass (), "heatbugStep", false);
( j- ?) r5 X3 c; l. p" z& d. `( y actionForEach =! Y& J5 @: E X% s9 J
modelActions.createFActionForEachHomogeneous$call8 Z+ ]5 j% I& C, @# ~- F: w* }' l
(heatbugList,
2 N& c& R W8 F/ t/ W new FCallImpl (this, proto, sel,' }, r" X; ?7 C4 w
new FArgumentsImpl (this, sel)));$ H$ k# H4 v8 M. M
} catch (Exception e) {
5 b* o- |& c/ K0 W e.printStackTrace (System.err);
3 ^ o, R' a/ |& ^& t& q3 ` }
1 S, M$ P- D3 Y, U 3 M1 I, h: P) `
syncUpdateOrder ();. U: e& `7 u; M) D! m
) g/ {, j$ T9 e& U2 X% K
try {
1 @' c1 [) C# |/ s1 S9 w modelActions.createActionTo$message ; W+ r8 O5 b) z, P* Y% ~! E
(heat, new Selector (heat.getClass (), "updateLattice", false));; U6 H; g! }' R$ }6 c' i* `) X
} catch (Exception e) {
. @1 a4 U( ~; O* C System.err.println("Exception updateLattice: " + e.getMessage ());6 @ X: P0 \8 a5 R$ g: R7 m
}' ~/ y/ m% W# x7 o0 f
% {7 t9 n/ Y$ `; s! a
// Then we create a schedule that executes the
; I4 Z7 l9 X+ B p4 H: b' X0 ^5 s // modelActions. modelActions is an ActionGroup, by itself it
/ B) r. i/ ?/ s& ]: @ // has no notion of time. In order to have it executed in
$ o. C; h6 u* t3 X% h) F // time, we create a Schedule that says to use the
, `: Q2 m& P# U) s7 O j // modelActions ActionGroup at particular times. This
) p' N4 H% A2 Q( r1 c) o. y* U // schedule has a repeat interval of 1, it will loop every, a: ~4 E& t! j9 I" S% f
// time step. The action is executed at time 0 relative to
; s+ i* C, [8 m; P // the beginning of the loop.
: S, P" v0 ~- \. e8 o: s+ ^! F
# o' ` Q0 }5 L% ~, {- V // This is a simple schedule, with only one action that is
7 u4 z# S0 N& S( Y8 M. b" } // just repeated every time. See jmousetrap for more
% I% C2 T h( a) T // complicated schedules.! s! l. `# o+ @- X9 K( T
) S, U2 K1 f5 i* ~. m4 Q- q& ` modelSchedule = new ScheduleImpl (getZone (), 1);
$ k, z9 R, ?$ E: v modelSchedule.at$createAction (0, modelActions);
/ f7 Q2 M) G7 ^4 S1 W # {" a! U) X- q3 P4 b+ m) R5 c; k/ n
return this;3 O/ C- {$ |! y$ V5 F
} |