1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.simulation.abstractsimengine.ssj; |
5 | |
6 | import org.apache.log4j.Logger; |
7 | |
8 | import sun.reflect.generics.reflectiveObjects.NotImplementedException; |
9 | import umontreal.iro.lecuyer.simevents.Event; |
10 | import umontreal.iro.lecuyer.simevents.Simulator; |
11 | import de.uka.ipd.sdq.simulation.abstractsimengine.AbstractSimProcessDelegator; |
12 | import de.uka.ipd.sdq.simulation.abstractsimengine.processes.ProcessState; |
13 | import de.uka.ipd.sdq.simulation.abstractsimengine.processes.SimProcessThreadingStrategy; |
14 | import de.uka.ipd.sdq.simulation.abstractsimengine.processes.SimulatedProcess; |
15 | |
16 | /** |
17 | * Simulation Process implementation for SSJ |
18 | * |
19 | * @author Snowball, Michael H. & Jens (bugfixing, refactorings, cleanup) |
20 | */ |
21 | public class SSJSimProcess extends SimulatedProcess { |
22 | |
23 | public static final Logger logger = Logger.getLogger(SSJSimProcess.class); |
24 | |
25 | /** |
26 | * Underlying abstract simulation process |
27 | */ |
28 | public final AbstractSimProcessDelegator myAbstractProcess; |
29 | |
30 | /** |
31 | * Reference to the actual and concrete simulation engine |
32 | */ |
33 | private final Simulator sim; |
34 | |
35 | /** |
36 | * Reference to the underlying SSJ experiment |
37 | */ |
38 | private final SSJExperiment ssjExperiment; |
39 | |
40 | /** |
41 | * Constructor |
42 | * @param myProcess Underlying abstract sim process |
43 | * @param name an ID |
44 | */ |
45 | public SSJSimProcess(AbstractSimProcessDelegator myProcess, String name) { |
46 | super(new SimProcessThreadingStrategy()); |
47 | this.myAbstractProcess = myProcess; |
48 | this.ssjExperiment = (SSJExperiment) myAbstractProcess.getModel().getSimulationControl(); |
49 | this.sim = ssjExperiment.getSimulator(); |
50 | |
51 | startProcess(processStrategy); |
52 | } |
53 | |
54 | /* (non-Javadoc) |
55 | * @see de.uka.ipd.sdq.simulation.abstractsimengine.ISimProcess#scheduleAt(double) |
56 | */ |
57 | public void scheduleAt(double delay) { |
58 | if (!isTerminated()) { |
59 | if (this.myProcessState != ProcessState.SUSPENDED) { |
60 | throw new IllegalStateException("Tried to schedule thread which was not suspended [" |
61 | + this.myAbstractProcess.getId() + "]"); |
62 | } |
63 | |
64 | // Resume process immediately to force process cleanup |
65 | if (!simIsRunning()) { |
66 | resume(); |
67 | } |
68 | |
69 | new Event(sim) { |
70 | @Override |
71 | public void actions() { |
72 | if (!isTerminated()) { |
73 | resume(); |
74 | } |
75 | } |
76 | }.schedule(delay); |
77 | } |
78 | } |
79 | |
80 | private boolean simIsRunning() { |
81 | return !ssjExperiment.getSimulator().isStopped(); |
82 | } |
83 | |
84 | @Override |
85 | protected AbstractSimProcessDelegator getAbstractProcess() { |
86 | return this.myAbstractProcess; |
87 | } |
88 | |
89 | @Override |
90 | public boolean isScheduled() { |
91 | throw new NotImplementedException(); |
92 | } |
93 | |
94 | @Override |
95 | public void reschedule(double delay) { |
96 | throw new NotImplementedException(); |
97 | } |
98 | |
99 | } |