HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* V) p6 O2 R; i! m: [ d
2 ]% n7 e& b5 V1 O! K
public Object buildActions () {
" {; m" Z8 M$ E- o$ L super.buildActions();
- K Q: a$ [ E' n) o8 [3 g
* s4 F# y" N! h0 Y9 m, W1 {' f$ o8 r S // Create the list of simulation actions. We put these in
# C! S* Z9 }2 [& ?' r: I& ] // an action group, because we want these actions to be
% Z7 z$ r* \# U8 d // executed in a specific order, but these steps should
; M" Q! o; c: g! l4 k // take no (simulated) time. The M(foo) means "The message+ ?5 e2 {9 _& Q7 ]9 _+ t2 n
// called <foo>". You can send a message To a particular% W3 \9 \% U) D% d$ V
// object, or ForEach object in a collection.. t+ c6 V. M6 J* W5 b/ g6 A
4 m8 T9 k8 a2 J, F; b // Note we update the heatspace in two phases: first run
$ T% `4 f( o& X! H% P; A // diffusion, then run "updateWorld" to actually enact the
0 n4 E, ]- b$ O& | // changes the heatbugs have made. The ordering here is
1 j7 [5 o& B4 n. x0 l6 x: i6 u: m // significant! a3 E' E5 e/ x
' ~4 b7 ]0 A$ E3 x5 \+ ? // Note also, that with the additional
5 f6 Z, Q; a, p7 o // `randomizeHeatbugUpdateOrder' Boolean flag we can- B2 J. T$ H4 ^ X
// randomize the order in which the bugs actually run
. b4 @8 Q9 x; C9 ]) Q/ A // their step rule. This has the effect of removing any% P( I0 u3 q% d) Q2 G6 x- {% t, a
// systematic bias in the iteration throught the heatbug- A% v: y7 J' P4 A2 p
// list from timestep to timestep) d9 S" k9 e# U+ U- ]% u
8 S$ o% k6 i$ @* M3 ~5 m. h
// By default, all `createActionForEach' modelActions have% I& c5 H P# V( v7 m; x4 E
// a default order of `Sequential', which means that the
1 K' q. |6 \' v% u // order of iteration through the `heatbugList' will be9 h2 m9 W! [4 V5 Q* ~! D7 a
// identical (assuming the list order is not changed
# S$ N$ [$ {$ p* |: I // indirectly by some other process).
/ {5 X9 y; H3 X) ]6 w0 L; z: D ) r: C: u# X" X. i* G$ v3 B
modelActions = new ActionGroupImpl (getZone ());0 k" x4 T6 t# V6 _% C
9 {0 o* y& b# x4 P3 w2 V9 p try {7 ^# q, s8 j' Z
modelActions.createActionTo$message
1 n1 N' U6 z( t; z7 I0 { (heat, new Selector (heat.getClass (), "stepRule", false));
2 t7 z3 x1 f' |, Q, t" m } catch (Exception e) {
( x H6 t1 t( R, y( D1 b+ R* W$ w System.err.println ("Exception stepRule: " + e.getMessage ());+ l' k/ c) d( t4 g
}
4 K* Z7 m# k! M$ x3 H# R& ~3 ^! o+ k4 K9 W2 I* p: N" Z# b7 P! \& p) h
try {/ m8 P$ b6 A4 i9 I2 Y
Heatbug proto = (Heatbug) heatbugList.get (0);: O5 f+ j8 C) u3 i$ ?" ?
Selector sel =
" v- T) h! K0 b5 h new Selector (proto.getClass (), "heatbugStep", false);
2 y: G4 x7 h+ B6 u actionForEach =
- B2 S, O. a* u8 T4 O0 k8 `: } modelActions.createFActionForEachHomogeneous$call- z/ `/ \: G: V- K
(heatbugList,
: n, B9 ^4 J d$ p5 K% H& F new FCallImpl (this, proto, sel,4 E2 I+ N' W9 F
new FArgumentsImpl (this, sel)));
8 q% \4 A0 r# j$ |5 k, z } catch (Exception e) {, w- p5 x! a& {, V; {) p4 l- d
e.printStackTrace (System.err);% O0 x2 |. v6 I5 g5 ~( o
}
" t0 P8 r' `+ M2 t2 B
8 J( ^) B* S6 p: I# ^$ h( [. K# r syncUpdateOrder (); b% |" P4 @8 {/ T9 x' F. T
: N7 B' b) n1 N3 T* i; A try {
/ ?+ r$ R$ H- s/ Y3 r modelActions.createActionTo$message
; ~ v Z0 j, f- T- e: i, B (heat, new Selector (heat.getClass (), "updateLattice", false));
7 y, Q& u; v+ H+ }- |+ ^ } catch (Exception e) {
+ I$ P/ X) A+ }) ?+ F% Z System.err.println("Exception updateLattice: " + e.getMessage ());
$ I: m& ]3 s8 |1 ^' B }& h# n1 m8 B, k( H% [0 J; ~
* y1 I- E- W9 i# N# _# v
// Then we create a schedule that executes the" W" Y! A# m9 a0 J! N. R4 k, F
// modelActions. modelActions is an ActionGroup, by itself it
! _! s6 ^& t/ x" a( t // has no notion of time. In order to have it executed in
: n/ o- ^7 }9 c/ ? // time, we create a Schedule that says to use the
4 I' X m p8 _- W // modelActions ActionGroup at particular times. This
! t) W/ {/ N, B% R' Z/ l0 W // schedule has a repeat interval of 1, it will loop every
( c' v; H; |9 e; K5 j3 ^ // time step. The action is executed at time 0 relative to
3 Z' t0 Q5 d+ l$ r8 M% H- e // the beginning of the loop.1 W! k! O1 M: O
A' W: q& C8 v2 |, t" s, P
// This is a simple schedule, with only one action that is
, A5 m/ K" l' r: v // just repeated every time. See jmousetrap for more0 ~" }+ Y$ T) B+ x) V+ v; ~
// complicated schedules.
7 A+ ^* \3 m9 X. j/ y ' [& S$ S4 H/ |9 a, I$ B2 l
modelSchedule = new ScheduleImpl (getZone (), 1);
3 P, @ [# h5 B4 U# E9 | modelSchedule.at$createAction (0, modelActions);4 F: T+ r& x: i% |) [5 {
Y% N- Z3 W- H* K& ~, Y7 f return this;( ?1 \ Y; {. @3 s1 ], l* P
} |