41125d0a0ad2f086cb90201c05f15c432d8bebcc
[simantics/platform.git] / bundles / org.simantics.simulation / src / org / simantics / simulation / experiment / Experiment.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.simulation.experiment;
13
14 import java.util.UUID;
15 import java.util.concurrent.locks.Lock;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.simantics.databoard.accessor.Accessor;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.procedure.Listener;
23 import org.simantics.db.request.ExternalRead;
24 import org.simantics.utils.datastructures.ListenerList;
25
26 public abstract class Experiment implements IExperiment {
27
28     protected Resource experiment;
29     protected Resource model;
30     protected ListenerList<IExperimentListener> listeners =
31         new ListenerList<IExperimentListener>(IExperimentListener.class);
32     protected ListenerList<IExperimentStatusListener> statusListeners =
33         new ListenerList<IExperimentStatusListener>(IExperimentStatusListener.class);
34     protected volatile ExperimentState state = ExperimentState.INITIALIZING;
35     protected String identifier;
36
37     public Experiment(Resource model) {
38         this(null, model, UUID.randomUUID().toString());
39     }
40
41     public Experiment(Resource model, String identifier) {
42         this(null, model, identifier);
43     }
44
45     public Experiment(Resource experiment, Resource model) {
46         this(experiment, model, UUID.randomUUID().toString());
47     }
48
49     public Experiment(Resource experiment, Resource model, String identifier) {
50         this.experiment = experiment;
51         this.model = model;
52         this.identifier = identifier;
53     }
54     
55     @Override
56     public <T> T getService(Class<T> clazz) {
57         return null;
58     }
59
60     @Override
61     public ExperimentState getState() {
62         if(state == null)
63                 System.err.println("nullstate");
64         return state;
65     }
66     
67     @Override
68     public ExperimentState getState(ReadGraph graph) throws DatabaseException {
69         return graph.syncRequest(EXPERIMENT_STATE_READ);
70     }
71
72     public void changeState(ExperimentState newState) {
73         if (state == ExperimentState.DISPOSED) {
74             if (newState != ExperimentState.DISPOSED) {
75                 // TODO: this might be worth logging as an error.
76             }
77             return;
78         }
79         if(newState != state) {
80             state = newState;
81             localStateChange();
82             for(IExperimentListener listener : listeners.getListeners())
83                 listener.stateChanged(newState);
84         }
85     }
86
87     protected void localStateChange() {
88     }
89
90     @Override
91     public void addListener(IExperimentListener listener) {
92         listeners.add(listener);
93         listener.stateChanged(state);
94     }
95
96     @Override
97     public Resource getResource() {
98         return experiment;
99     }
100
101     @Override
102     public Resource getModel() {
103         return model;
104     }
105
106     @Override
107     public void removeListener(IExperimentListener listener) {
108         listeners.remove(listener);
109     }
110
111     @Override
112     public void addStatusListener(IExperimentStatusListener listener) {
113         statusListeners.add(listener);
114     }
115
116     @Override
117     public void removeStatusListener(IExperimentStatusListener listener) {
118         statusListeners.remove(listener);
119     }
120
121     @Override
122     public void shutdown(IProgressMonitor monitor) {
123         changeState(ExperimentState.DISPOSED);
124     }
125
126     @Override
127     public Lock getDatasourceLock() {
128         throw new UnsupportedOperationException("deprecated operation");
129     }
130
131     @Override
132     public Accessor getAccessor() {
133         return null;
134     }
135
136     @Override
137     public String getIdentifier() {
138         return identifier;
139     }
140
141     static class ExperimentStateRead implements ExternalRead<ExperimentState>, Runnable {
142         
143         final private Experiment experiment;
144         private Listener<ExperimentState> listener = null;
145         
146         ExperimentStateRead(Experiment experiment) {
147                 this.experiment = experiment;
148         }
149
150         @Override
151         public void register(ReadGraph graph, final Listener<ExperimentState> procedure) {
152             //System.out.println("IcTrackerRequest.register: " + procedure);
153             listener = procedure;
154             procedure.execute(experiment.state);
155         }
156
157         @Override
158         public void unregistered() {
159             //System.out.println("IcTrackerRequest.unregister: " + listener);
160             listener = null;
161         }
162
163         @Override
164         public void run() {
165             Listener<ExperimentState> l = listener;
166             //System.out.println("IcTrackerRequest.run: " + l);
167             if (l != null)
168                 l.execute(experiment.state);
169         }
170     }
171
172     private ExperimentStateRead EXPERIMENT_STATE_READ = new ExperimentStateRead(this);
173     
174 }