EXPERIMENT_STATE_READ needs to be triggered on state change
[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             EXPERIMENT_STATE_READ.run();
85         }
86     }
87
88     protected void localStateChange() {
89     }
90
91     @Override
92     public void addListener(IExperimentListener listener) {
93         listeners.add(listener);
94         listener.stateChanged(state);
95     }
96
97     @Override
98     public Resource getResource() {
99         return experiment;
100     }
101
102     @Override
103     public Resource getModel() {
104         return model;
105     }
106
107     @Override
108     public void removeListener(IExperimentListener listener) {
109         listeners.remove(listener);
110     }
111
112     @Override
113     public void addStatusListener(IExperimentStatusListener listener) {
114         statusListeners.add(listener);
115     }
116
117     @Override
118     public void removeStatusListener(IExperimentStatusListener listener) {
119         statusListeners.remove(listener);
120     }
121
122     @Override
123     public void shutdown(IProgressMonitor monitor) {
124         changeState(ExperimentState.DISPOSED);
125     }
126
127     @Override
128     public Lock getDatasourceLock() {
129         throw new UnsupportedOperationException("deprecated operation");
130     }
131
132     @Override
133     public Accessor getAccessor() {
134         return null;
135     }
136
137     @Override
138     public String getIdentifier() {
139         return identifier;
140     }
141
142     static class ExperimentStateRead implements ExternalRead<ExperimentState>, Runnable {
143         
144         final private Experiment experiment;
145         private Listener<ExperimentState> listener = null;
146         
147         ExperimentStateRead(Experiment experiment) {
148                 this.experiment = experiment;
149         }
150
151         @Override
152         public void register(ReadGraph graph, final Listener<ExperimentState> procedure) {
153             //System.out.println("IcTrackerRequest.register: " + procedure);
154             listener = procedure;
155             procedure.execute(experiment.state);
156         }
157
158         @Override
159         public void unregistered() {
160             //System.out.println("IcTrackerRequest.unregister: " + listener);
161             listener = null;
162         }
163
164         @Override
165         public void run() {
166             Listener<ExperimentState> l = listener;
167             //System.out.println("IcTrackerRequest.run: " + l);
168             if (l != null)
169                 l.execute(experiment.state);
170         }
171     }
172
173     private ExperimentStateRead EXPERIMENT_STATE_READ = new ExperimentStateRead(this);
174     
175 }