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