1 package org.simantics.charts;
3 import java.util.Collections;
6 import org.simantics.charts.editor.ChartData;
7 import org.simantics.charts.editor.ChartKeys;
8 import org.simantics.databoard.binding.error.BindingException;
9 import org.simantics.databoard.util.Bean;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.common.request.PossibleIndexRoot;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.variable.Variable;
15 import org.simantics.history.HistoryException;
16 import org.simantics.history.HistorySamplerItem;
17 import org.simantics.history.ItemManager;
18 import org.simantics.history.util.subscription.SamplingFormat;
19 import org.simantics.modeling.subscription.SubscriptionItem;
20 import org.simantics.modeling.subscription.SubscriptionItemQuery;
21 import org.simantics.project.IProject;
22 import org.simantics.simulation.experiment.IExperiment;
25 * Main facade for externally dealing with the trending system.
27 * @author Tuukka Lehtonen
28 * @author Antti Villberg
31 public final class Charts {
33 public static void resetChartEditorData(IProject project, Resource model, ChartData editorData) {
34 if (editorData != null) {
35 project.setHint(ChartKeys.chartSourceKey(model), editorData);
37 project.removeHint(ChartKeys.chartSourceKey(model));
41 public static HistorySamplerItem createHistorySamplerItem(ReadGraph graph, Variable run, Resource subscriptionItem) throws DatabaseException {
42 IExperiment exp = (IExperiment) run.getPropertyValue(graph, "iExperiment");
43 ITrendSupport support = exp.getService(ITrendSupport.class);
44 ChartData data = support.getChartData();
45 return createHistorySamplerItem(graph, subscriptionItem, data);
48 public static HistorySamplerItem createHistorySamplerItem(ReadGraph graph, Resource subscriptionItem, ChartData data) throws DatabaseException {
51 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(subscriptionItem));
52 if (indexRoot == null) {
53 throw new DatabaseException("There is no index root for " + subscriptionItem);
57 throw new DatabaseException("There is no chart data for " + subscriptionItem);
60 ItemManager im = new ItemManager(data.history.getItems());
62 SubscriptionItem i = graph.syncRequest(new SubscriptionItemQuery(subscriptionItem));
64 List<Bean> items = im.search("variableId", i.variableId);
65 Collections.sort(items, SamplingFormat.INTERVAL_COMPARATOR);
67 new DatabaseException("There is history item for " + i.variableId);
68 Bean config = items.get(0);
69 String historyId = (String) config.getFieldUnchecked("id");
71 return new HistorySamplerItem(data.collector, data.history, historyId, System.identityHashCode(data));
72 } catch (HistoryException e) {
73 throw new DatabaseException(e);
74 } catch (BindingException e) {
75 throw new DatabaseException(e);