1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.simulation.abstractsimengine.desmoj; |
5 | |
6 | import sun.reflect.generics.reflectiveObjects.NotImplementedException; |
7 | import de.uka.ipd.sdq.simulation.abstractsimengine.AbstractSimProcessDelegator; |
8 | import de.uka.ipd.sdq.simulation.abstractsimengine.processes.ProcessState; |
9 | import de.uka.ipd.sdq.simulation.abstractsimengine.processes.SimProcessThreadingStrategy; |
10 | import de.uka.ipd.sdq.simulation.abstractsimengine.processes.SimulatedProcess; |
11 | import desmoj.core.simulator.ExternalEvent; |
12 | import desmoj.core.simulator.TimeSpan; |
13 | |
14 | /** |
15 | * @author Steffen Becker |
16 | * @author Philipp Merkle |
17 | */ |
18 | public class DesmoJSimProcess extends SimulatedProcess { |
19 | |
20 | private AbstractSimProcessDelegator myAbstractProcess; |
21 | |
22 | private DesmoJModel model; |
23 | |
24 | /** |
25 | * Reference to the underlying experiment |
26 | */ |
27 | private final DesmoJExperiment experiment; |
28 | |
29 | public DesmoJSimProcess(AbstractSimProcessDelegator process, DesmoJModel model, String name) { |
30 | super(new SimProcessThreadingStrategy()); |
31 | this.myAbstractProcess = process; |
32 | this.model = model; |
33 | this.experiment = (DesmoJExperiment) myAbstractProcess.getModel().getSimulationControl(); |
34 | |
35 | startProcess(processStrategy); |
36 | } |
37 | |
38 | public void scheduleAt(double delay) { |
39 | if (!isTerminated()) { |
40 | if (this.myProcessState != ProcessState.SUSPENDED) { |
41 | throw new IllegalStateException("Tried to schedule thread which was not suspended [" |
42 | + this.myAbstractProcess.getId() + "]"); |
43 | } |
44 | |
45 | // Resume process immediately to force process cleanup |
46 | if (!simIsRunning()) { |
47 | resume(); |
48 | } |
49 | |
50 | // TODO set event name |
51 | new ExternalEvent(this.model, "TODO event name", false) { |
52 | @Override |
53 | public void eventRoutine() { |
54 | if (!isTerminated()) { |
55 | resume(); |
56 | } |
57 | } |
58 | }.schedule(new TimeSpan(delay)); |
59 | } |
60 | } |
61 | |
62 | private boolean simIsRunning() { |
63 | // do not use isRunning method here, since !isRunning != isStopped |
64 | return !experiment.getExperiment().isStopped(); |
65 | } |
66 | |
67 | @Override |
68 | protected AbstractSimProcessDelegator getAbstractProcess() { |
69 | return this.myAbstractProcess; |
70 | } |
71 | |
72 | @Override |
73 | public boolean isScheduled() { |
74 | throw new NotImplementedException(); |
75 | } |
76 | |
77 | @Override |
78 | public void reschedule(double delay) { |
79 | throw new NotImplementedException(); |
80 | } |
81 | |
82 | } |