source: java/main/src/main/java/com/framsticks/experiment/Experiment.java @ 105

Last change on this file since 105 was 105, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • import refactorization: move Tree, Path, etc.

from core to structure package

  • initial serialization implementation
  • improve PrimeExperiment? test
  • many organizational changes and convenience improvements

CHANGELOG:
Make registry in AbstractTree? final.

Move most classes from core to structure package.

Minor changes.

Switch names of Future and FutureHandler?.

Rename ExceptionResultHandler? to ExceptionHandler?.

Rename ExceptionHandler? to ExceptionDispatcherHandler?.

Fix bug in ParamCandidate? cache.

Add missing synchronization to the BufferedDispatcher?.

Develop @Serialized support.

Rework serialization further.

Add serialization/deserialization interface to ValueParam?.

Move getStorageType and isNumeric from Param down to params hierarchy.

Minor changes.

Improve param type induction.

Add TestSerializedClass? for testing new serialization.

Add info files gor GenePool? and Population.

Add standard.expt exemplary netfile.

Add type name field to PropertiesObject?.

Use PropertiesObject? for PropertiesAccess? instead of ordinary map.

Hide getFramsClass is several more places.

More unification accross FramsClass?, Access and Path.

Add ParamCollection?.

Simplify interface for getting params from FramsClass?, Access
or Path.

Make Access.call() interface variadic.

Add arguments(args) convenience wrapper around new Object[] {args}.

Upgrade to apache.commons.lang version 3.1

Minor improvement with Response constructors.

Develop proper result printing in ClientAtServer?.

Add experimentNetsave to PrimeExperiment?.

File size: 5.7 KB
Line 
1package com.framsticks.experiment;
2
3import java.util.Map;
4
5import org.apache.logging.log4j.Level;
6import org.apache.logging.log4j.Logger;
7import org.apache.logging.log4j.LogManager;
8
9import com.framsticks.params.EventListener;
10import com.framsticks.params.ParamFlags;
11import com.framsticks.params.SimpleUniqueList;
12import com.framsticks.params.annotations.AutoAppendAnnotation;
13import com.framsticks.params.annotations.FramsClassAnnotation;
14import com.framsticks.params.annotations.ParamAnnotation;
15import com.framsticks.params.types.ProcedureParam;
16import com.framsticks.remote.RemoteTree;
17import com.framsticks.structure.messages.ListChange;
18import com.framsticks.util.ExceptionHandler;
19import com.framsticks.util.FramsticksException;
20import com.framsticks.util.dispatching.AbstractJoinable;
21import com.framsticks.util.dispatching.BufferedDispatcher;
22import com.framsticks.util.dispatching.Dispatcher;
23import com.framsticks.util.dispatching.DispatcherSetable;
24import com.framsticks.util.dispatching.Dispatching;
25import com.framsticks.util.dispatching.Joinable;
26import com.framsticks.util.dispatching.JoinableCollection;
27import com.framsticks.util.dispatching.JoinableParent;
28import com.framsticks.util.dispatching.JoinableState;
29import com.framsticks.util.dispatching.RunAt;
30
31@FramsClassAnnotation
32public 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}
Note: See TracBrowser for help on using the repository browser.