1 package org.simantics.charts;
3 import java.util.Collections;
4 import java.util.Comparator;
7 import org.simantics.charts.editor.ChartData;
8 import org.simantics.charts.editor.ChartKeys;
9 import org.simantics.databoard.binding.error.BindingException;
10 import org.simantics.databoard.util.Bean;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.common.request.PossibleIndexRoot;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.variable.Variable;
16 import org.simantics.history.HistoryException;
17 import org.simantics.history.HistorySamplerItem;
18 import org.simantics.history.HistorySamplerItem2;
19 import org.simantics.history.ItemManager;
20 import org.simantics.history.HistorySamplerItem2.LevelItem;
21 import org.simantics.history.util.subscription.SamplingFormat;
22 import org.simantics.modeling.subscription.SubscriptionItem;
23 import org.simantics.modeling.subscription.SubscriptionItemQuery;
24 import org.simantics.project.IProject;
25 import org.simantics.simulation.experiment.IExperiment;
28 * Main facade for externally dealing with the trending system.
30 * @author Tuukka Lehtonen
31 * @author Antti Villberg
34 public final class Charts {
36 public static void resetChartEditorData(IProject project, Resource model, ChartData editorData) {
37 if (editorData != null) {
38 project.setHint(ChartKeys.chartSourceKey(model), editorData);
40 project.removeHint(ChartKeys.chartSourceKey(model));
44 public static HistorySamplerItem createHistorySamplerItem(ReadGraph graph, Variable run, Resource subscriptionItem) throws DatabaseException {
45 IExperiment exp = (IExperiment) run.getPropertyValue(graph, "iExperiment");
46 ITrendSupport support = exp.getService(ITrendSupport.class);
47 ChartData data = support.getChartData();
48 return createHistorySamplerItem(graph, subscriptionItem, data);
51 public static HistorySamplerItem createHistorySamplerItem(ReadGraph graph, Resource subscriptionItem, ChartData data) throws DatabaseException {
54 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(subscriptionItem));
55 if (indexRoot == null) {
56 throw new DatabaseException("There is no index root for " + subscriptionItem);
60 throw new DatabaseException("There is no chart data for " + subscriptionItem);
63 ItemManager im = new ItemManager(data.history.getItems());
65 SubscriptionItem i = graph.syncRequest(new SubscriptionItemQuery(subscriptionItem));
67 List<Bean> items = im.search("variableId", i.variableId);
68 Collections.sort(items, SamplingFormat.INTERVAL_COMPARATOR);
70 throw new DatabaseException("There is history item for " + i.variableId);
71 Bean config = items.get(0);
72 String historyId = (String) config.getFieldUnchecked("id");
74 return new HistorySamplerItem(data.collector, data.history, historyId, System.identityHashCode(data));
75 } catch (HistoryException e) {
76 throw new DatabaseException(e);
77 } catch (BindingException e) {
78 throw new DatabaseException(e);
82 public static HistorySamplerItem2 createHistorySamplerItem2(ReadGraph graph, Variable run, Resource subscriptionItem) throws DatabaseException {
83 IExperiment exp = (IExperiment) run.getPropertyValue(graph, "iExperiment");
84 ITrendSupport support = exp.getService(ITrendSupport.class);
85 ChartData data = support.getChartData();
86 return data != null ? createHistorySamplerItem2(graph, subscriptionItem, data) : null;
89 public static HistorySamplerItem2 createHistorySamplerItem2(ReadGraph graph, Resource subscriptionItem, ChartData data) throws DatabaseException {
91 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(subscriptionItem));
92 if (indexRoot == null)
93 throw new DatabaseException("There is no index root for " + subscriptionItem);
95 throw new DatabaseException("There is no chart data for " + subscriptionItem);
97 SubscriptionItem i = graph.syncRequest(new SubscriptionItemQuery(subscriptionItem));
99 // TODO: this is super-inefficient!
100 ItemManager im = new ItemManager(data.history.getItems());
102 List<Bean> items = im.search("variableId", i.variableId);
104 throw new DatabaseException("There is history item for " + i.variableId);
106 LevelItem[] historyItems = items.stream().map(Charts::toLevelItem).sorted(ASCENDING_INTERVAL_ORDER).toArray(LevelItem[]::new);
108 return new HistorySamplerItem2(data.collector, data.history, historyItems, System.identityHashCode(data));
109 } catch (HistoryException e) {
110 throw new DatabaseException(e);
111 } catch (BindingException e) {
112 throw new DatabaseException(e);
116 private static LevelItem toLevelItem(Bean bean) {
117 String id = (String) bean.getFieldUnchecked("id");
118 double interval = (Double) bean.getFieldUnchecked("interval");
119 return new LevelItem(id, interval);
122 public final static Comparator<LevelItem> ASCENDING_INTERVAL_ORDER =
123 (o1, o2) -> SamplingFormat.compareSamplingInterval(o1.samplingInterval, o2.samplingInterval);