1 | package com.framsticks.experiment; |
---|
2 | |
---|
3 | import java.util.Map; |
---|
4 | |
---|
5 | import org.apache.logging.log4j.Level; |
---|
6 | import org.apache.logging.log4j.Logger; |
---|
7 | import org.apache.logging.log4j.LogManager; |
---|
8 | |
---|
9 | import com.framsticks.params.EventListener; |
---|
10 | import com.framsticks.params.ParamFlags; |
---|
11 | import com.framsticks.params.SimpleUniqueList; |
---|
12 | import com.framsticks.params.annotations.AutoAppendAnnotation; |
---|
13 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
14 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
15 | import com.framsticks.params.types.ProcedureParam; |
---|
16 | import com.framsticks.remote.RemoteTree; |
---|
17 | import com.framsticks.structure.messages.ListChange; |
---|
18 | import com.framsticks.util.ExceptionHandler; |
---|
19 | import com.framsticks.util.FramsticksException; |
---|
20 | import com.framsticks.util.dispatching.AbstractJoinable; |
---|
21 | import com.framsticks.util.dispatching.BufferedDispatcher; |
---|
22 | import com.framsticks.util.dispatching.Dispatcher; |
---|
23 | import com.framsticks.util.dispatching.DispatcherSetable; |
---|
24 | import com.framsticks.util.dispatching.Dispatching; |
---|
25 | import com.framsticks.util.dispatching.Joinable; |
---|
26 | import com.framsticks.util.dispatching.JoinableCollection; |
---|
27 | import com.framsticks.util.dispatching.JoinableParent; |
---|
28 | import com.framsticks.util.dispatching.JoinableState; |
---|
29 | import com.framsticks.util.dispatching.RunAt; |
---|
30 | |
---|
31 | @FramsClassAnnotation |
---|
32 | public class Experiment extends AbstractJoinable implements Dispatcher<Experiment>, DispatcherSetable<Experiment>, JoinableParent, ExceptionHandler { |
---|
33 | private static final Logger log = LogManager.getLogger(Experiment.class); |
---|
34 | |
---|
35 | protected final JoinableCollection<Simulator> simulatorAsJoinables = new JoinableCollection<Simulator>().setObservableName("simulators"); |
---|
36 | |
---|
37 | protected final JoinableCollection<RemoteTree> simulatorCandidates = new JoinableCollection<RemoteTree>().setObservableName("candidates"); |
---|
38 | |
---|
39 | protected final SimpleUniqueList<Simulator> simulators = new SimpleUniqueList<>(Simulator.class, 's'); |
---|
40 | |
---|
41 | protected final SimpleUniqueList<Simulator> oldSimulators = new SimpleUniqueList<>(Simulator.class, 's'); |
---|
42 | |
---|
43 | protected final BufferedDispatcher<Experiment> bufferedDispatcher = new BufferedDispatcher<>(this); |
---|
44 | |
---|
45 | protected String expdef; |
---|
46 | |
---|
47 | |
---|
48 | /** |
---|
49 | * |
---|
50 | */ |
---|
51 | public Experiment() { |
---|
52 | super(); |
---|
53 | bufferedDispatcher.setBuffer(false); |
---|
54 | |
---|
55 | Dispatching.dispatchLog(this, log, Level.DEBUG, "first task"); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * @return the simulatorCandidates |
---|
60 | */ |
---|
61 | public JoinableCollection<RemoteTree> getSimulatorCandidates() { |
---|
62 | return simulatorCandidates; |
---|
63 | } |
---|
64 | |
---|
65 | @ParamAnnotation |
---|
66 | public Map<String, Simulator> getSimulators() { |
---|
67 | return simulators.getView(); |
---|
68 | } |
---|
69 | |
---|
70 | @ParamAnnotation(id = "old_simulators") |
---|
71 | public Map<String, Simulator> getOldSimulators() { |
---|
72 | return oldSimulators.getView(); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * @return the dispatcher |
---|
77 | */ |
---|
78 | @Override |
---|
79 | public Dispatcher<Experiment> getDispatcher() { |
---|
80 | return bufferedDispatcher; |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * @param dispatcher the dispatcher to set |
---|
85 | */ |
---|
86 | @Override |
---|
87 | public void setDispatcher(Dispatcher<Experiment> dispatcher) { |
---|
88 | bufferedDispatcher.setTargetDispatcher(dispatcher); |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * @return the expdef |
---|
93 | */ |
---|
94 | @ParamAnnotation(flags = ParamFlags.USERREADONLY) |
---|
95 | public String getExpdef() { |
---|
96 | return expdef; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * @param expdef the expdef to set |
---|
101 | */ |
---|
102 | @ParamAnnotation |
---|
103 | public void setExpdef(String expdef) { |
---|
104 | this.expdef = expdef; |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | @ParamAnnotation(id = "simulators_changed") |
---|
109 | public void addSimulatorsListener(EventListener<ListChange> listener) { |
---|
110 | simulators.addListener(listener); |
---|
111 | } |
---|
112 | |
---|
113 | @ParamAnnotation(id = "simulators_changed") |
---|
114 | public void removeSimulatorsListener(EventListener<ListChange> listener) { |
---|
115 | simulators.removeListener(listener); |
---|
116 | } |
---|
117 | |
---|
118 | @AutoAppendAnnotation |
---|
119 | public void addSimulator(Simulator simulator) { |
---|
120 | simulators.add(simulator); |
---|
121 | simulatorAsJoinables.add(simulator); |
---|
122 | simulators.fireChildrenChange(simulator, ListChange.Action.Modify, "ready"); |
---|
123 | |
---|
124 | } |
---|
125 | |
---|
126 | protected void removeSimulator(Simulator simulator) { |
---|
127 | simulatorAsJoinables.remove(simulator); |
---|
128 | simulators.remove(simulator); |
---|
129 | oldSimulators.add(simulator); |
---|
130 | } |
---|
131 | |
---|
132 | @ParamAnnotation(id = "old_simulators_changed") |
---|
133 | public void addOldSimulatorsListener(EventListener<ListChange> listener) { |
---|
134 | oldSimulators.addListener(listener); |
---|
135 | } |
---|
136 | |
---|
137 | @ParamAnnotation(id = "old_simulators_changed") |
---|
138 | public void removeOldSimulatorsListener(EventListener<ListChange> listener) { |
---|
139 | oldSimulators.removeListener(listener); |
---|
140 | } |
---|
141 | |
---|
142 | @Override |
---|
143 | public String getName() { |
---|
144 | return "experiment"; |
---|
145 | } |
---|
146 | |
---|
147 | @Override |
---|
148 | public void childChangedState(Joinable joinable, JoinableState state) { |
---|
149 | proceedToState(state); |
---|
150 | } |
---|
151 | |
---|
152 | @Override |
---|
153 | protected void joinableStart() { |
---|
154 | bufferedDispatcher.createThreadIfNeeded(); |
---|
155 | Dispatching.use(bufferedDispatcher, this); |
---|
156 | |
---|
157 | Dispatching.use(simulatorAsJoinables, this); |
---|
158 | Dispatching.use(simulatorCandidates, this); |
---|
159 | } |
---|
160 | |
---|
161 | @Override |
---|
162 | protected void joinableInterrupt() { |
---|
163 | |
---|
164 | Dispatching.drop(simulatorAsJoinables, this); |
---|
165 | Dispatching.drop(simulatorCandidates, this); |
---|
166 | |
---|
167 | finishJoinable(); |
---|
168 | } |
---|
169 | |
---|
170 | @Override |
---|
171 | protected void joinableFinish() { |
---|
172 | log.debug("finishing experiment {}", this); |
---|
173 | } |
---|
174 | |
---|
175 | @Override |
---|
176 | protected void joinableJoin() throws InterruptedException { |
---|
177 | Dispatching.drop(bufferedDispatcher, this); |
---|
178 | |
---|
179 | Dispatching.join(simulatorAsJoinables); |
---|
180 | Dispatching.join(simulatorCandidates); |
---|
181 | Dispatching.join(bufferedDispatcher.getTargetDispatcher()); |
---|
182 | } |
---|
183 | |
---|
184 | @Override |
---|
185 | public void handle(FramsticksException exception) { |
---|
186 | log.error("caught exception: ", exception); |
---|
187 | } |
---|
188 | |
---|
189 | @Override |
---|
190 | public boolean isActive() { |
---|
191 | return bufferedDispatcher.isActive(); |
---|
192 | } |
---|
193 | |
---|
194 | @Override |
---|
195 | public void dispatch(RunAt<? extends Experiment> runnable) { |
---|
196 | bufferedDispatcher.dispatch(runnable); |
---|
197 | } |
---|
198 | |
---|
199 | @ParamAnnotation(paramType = ProcedureParam.class) |
---|
200 | public void connectToSimulator(String address) { |
---|
201 | SimulatorConnector connector = new SimulatorConnector(); |
---|
202 | connector.setAddress(address); |
---|
203 | connector.attachTo(this); |
---|
204 | } |
---|
205 | |
---|
206 | } |
---|