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 model = graph.syncRequest(new PossibleIndexRoot(subscriptionItem));
53 throw new DatabaseException("There is no model for " + subscriptionItem);
56 ItemManager im = new ItemManager(data.history.getItems());
58 SubscriptionItem i = graph.syncRequest(new SubscriptionItemQuery(subscriptionItem));
60 List<Bean> items = im.search("variableId", i.variableId);
61 Collections.sort(items, SamplingFormat.INTERVAL_COMPARATOR);
63 new DatabaseException("There is history item for " + i.variableId);
64 Bean config = items.get(0);
65 String historyId = (String) config.getFieldUnchecked("id");
67 return new HistorySamplerItem(data.collector, data.history, historyId, System.identityHashCode(data));
68 } catch (HistoryException e) {
69 throw new DatabaseException(e);
70 } catch (BindingException e) {
71 throw new DatabaseException(e);