1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.charts.editor;
14 import java.util.concurrent.atomic.AtomicInteger;
16 import org.simantics.databoard.annotations.Optional;
17 import org.simantics.db.Resource;
18 import org.simantics.history.Collector;
19 import org.simantics.history.HistoryManager;
20 import org.simantics.simulation.data.Datasource;
21 import org.simantics.simulation.experiment.IExperiment;
24 * @author Tuukka Lehtonen
26 public final class ChartData {
29 * The model for which this ChartData is valid, i.e. the model that contains
30 * {@link #run} if {@link #run} is non<code>non-null</code>
33 public Resource model;
36 * If just reading data from file system, not the database, this may be
43 * Used for giving the chart access to the active experiment state.
46 public IExperiment experiment;
49 * May be null if the chart data is not connected to a running experiment.
52 public Datasource datasource;
55 public HistoryManager history;
58 * Optional collector, Chart uses this to flush data right before drawing it
61 public Collector collector;
64 * This (shared instance) is used to track the amount of users of this ChartData
65 * instance. The same instance may end up in many ChartData instances through
66 * {@link #readFrom(ChartData)}.
68 public AtomicInteger refCount = new AtomicInteger();
70 public ChartData(Resource model, Resource run, IExperiment experiment, Datasource datasource, HistoryManager history, Collector collector) {
73 this.experiment = experiment;
74 this.datasource = datasource;
75 this.history = history;
76 this.collector = collector;
79 public void readFrom(ChartData other) {
83 this.experiment = null;
84 this.datasource = null;
86 this.collector = null;
89 this.model = other.model;
91 this.experiment = other.experiment;
92 this.datasource = other.datasource;
93 this.history = other.history;
94 this.collector = other.collector;
95 this.refCount = other.refCount;
100 * Dispose by closing history
102 public void dispose() {
107 if (history != null) {
113 public int reference() {
114 AtomicInteger i = refCount;
117 int result = i.incrementAndGet();
118 //System.out.println(this + ": reference: " + (result-1) + " -> " + result + " (" + System.identityHashCode(refCount) + ")");
122 public int dereference() {
123 AtomicInteger i = refCount;
126 int result = i.decrementAndGet();
127 //System.out.println(this + ": dereference: " + (result+1) + " -> " + result + " (" + System.identityHashCode(refCount) + ")");
136 public void waitUntilNotReferenced() throws InterruptedException {
137 AtomicInteger i = refCount;
141 while (i.get() > 0) {