-/*******************************************************************************\r
- * Copyright (c) 2012 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.charts;\r
-\r
-import java.io.File;\r
-import java.io.FileNotFoundException;\r
-import java.io.IOException;\r
-\r
-import org.eclipse.core.runtime.IPath;\r
-import org.eclipse.core.runtime.Platform;\r
-import org.simantics.Simantics;\r
-import org.simantics.charts.editor.ChartData;\r
-import org.simantics.databoard.util.URIUtil;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.ValidationException;\r
-import org.simantics.db.layer0.request.PossibleModel;\r
-import org.simantics.history.History;\r
-import org.simantics.history.HistoryException;\r
-import org.simantics.history.HistoryManager;\r
-import org.simantics.modeling.subscription.CollectSubscriptions;\r
-import org.simantics.modeling.subscription.ModelHistoryCollector;\r
-import org.simantics.project.IProject;\r
-import org.simantics.simulation.experiment.IDynamicExperiment;\r
-import org.simantics.utils.FileUtils;\r
-\r
-/**\r
- * This class contains utilities for initializing history collection and makes\r
- * trending of subscribed dynamic experiment data possible.\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public class TrendSupport {\r
-\r
- // Input\r
- private IDynamicExperiment experiment;\r
- private String workspaceDataDirectoryName;\r
-\r
- // Internals\r
- private ModelHistoryCollector historyCollector;\r
- private ChartData chartData;\r
-\r
- public TrendSupport(IDynamicExperiment experiment, String workspaceDataDirectoryName) {\r
- this.experiment = experiment;\r
- this.workspaceDataDirectoryName = workspaceDataDirectoryName;\r
- }\r
-\r
- public void initializeHistoryCollection(ReadGraph graph) throws DatabaseException {\r
- final IProject project = Simantics.peekProject();\r
- if (project == null)\r
- return;\r
-\r
- try {\r
- File workarea = getExperimentDirectory(graph, experiment.getResource(), true, "result-" + experiment.getIdentifier());\r
- final HistoryManager history = History.openFileHistory(workarea);\r
-\r
- historyCollector = ModelHistoryCollector.createCollector(\r
- experiment,\r
- history,\r
- Activator.getDefault().getLog(),\r
- () -> {\r
- CollectSubscriptions cs = new CollectSubscriptions(TrendSupport.this.experiment, 0.1);\r
- return cs;\r
- }, \r
- () -> {\r
- // Reset chart data every time subscriptions change\r
- // to make sure that trend editor react.\r
- if (chartData != null)\r
- Charts.resetChartEditorData(project, experiment.getModel(), chartData);\r
- });\r
-\r
- historyCollector.initialize(graph, 0, true);\r
-\r
- // Initialize chart source to support trend viewing\r
- HistoryManager historyManager = History.openFileHistory(historyCollector.getWorkarea());\r
- chartData = new ChartData(experiment.getModel(), null,\r
- experiment, experiment.getDatasource(), historyManager,\r
- historyCollector.getCollector());\r
- Charts.resetChartEditorData(project, experiment.getModel(), chartData);\r
- } catch (IOException e) {\r
- throw new DatabaseException(e);\r
- } catch (HistoryException e) {\r
- throw new DatabaseException(e);\r
- } catch (InterruptedException e) {\r
- throw new DatabaseException(e);\r
- }\r
- }\r
-\r
- public void dispose() {\r
- if (historyCollector != null) {\r
- historyCollector.dispose();\r
- historyCollector = null;\r
- }\r
- if (chartData != null) {\r
- final IProject project = Simantics.peekProject();\r
- if (project != null)\r
- Charts.resetChartEditorData(project, experiment.getModel(), null);\r
- chartData = null;\r
- }\r
- }\r
-\r
- /**\r
- * @param g\r
- * @param experiment\r
- * @param ensureExistence\r
- * @param subdirs\r
- * @return\r
- * @throws DatabaseException\r
- * @throws IOException\r
- */\r
- protected File getExperimentDirectory(ReadGraph g, Resource experiment, boolean ensureExistence, String... subdirs) throws DatabaseException, IOException {\r
- Resource model = g.syncRequest(new PossibleModel(experiment));\r
- if (model == null)\r
- throw new ValidationException(\r
- "Experiment '" + NameUtils.getSafeName(g, experiment)\r
- + "' is not part of any model. Has the experiment been removed already? Database may also be corrupted?");\r
-\r
- String[] dirs = new String[3 + subdirs.length];\r
- dirs[0] = workspaceDataDirectoryName;\r
- dirs[1] = "model-" + model.getResourceId();\r
- dirs[2] = "experiment-" + experiment.getResourceId();\r
- System.arraycopy(subdirs, 0, dirs, 3, subdirs.length);\r
-\r
- File experimentPath = getWorkspacePath(false, dirs);\r
- if (ensureExistence)\r
- ensureDirectoryExistence(experimentPath);\r
-\r
- return experimentPath;\r
- }\r
-\r
- /**\r
- * @param dir the directory that needs to be created if it does not exist\r
- * @return the requested directory if created or deemed existing\r
- * @throws IOException if the requested directory cannot be created\r
- */\r
- protected static File ensureDirectoryExistence(File dir) throws IOException {\r
- if (dir.exists() && !dir.isDirectory())\r
- FileUtils.deleteAll(dir);\r
-\r
- dir.mkdirs();\r
- if (!dir.exists() || !dir.isDirectory())\r
- throw new FileNotFoundException("Could not create directory '" + dir + "'. Out of disk space?");\r
-\r
- return dir;\r
- }\r
-\r
- /**\r
- * @param escapeNames <code>true</code> to run each path segment through\r
- * {@link URIUtil#encodeFilename(String)}\r
- * @param relativeSegments path segments to append to the workspace root\r
- * path\r
- * @return the designated path within the workspace\r
- */\r
- protected static File getWorkspacePath(boolean escapeNames, String... relativeSegments) {\r
- IPath finalPath = Platform.getLocation();\r
- for (int i = 0; i < relativeSegments.length; ++i)\r
- finalPath = finalPath.append(escapeNames ? URIUtil.encodeFilename(relativeSegments[i]) : relativeSegments[i]);\r
-\r
- return finalPath.toFile();\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.charts;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Platform;
+import org.simantics.Simantics;
+import org.simantics.charts.editor.ChartData;
+import org.simantics.databoard.util.URIUtil;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ValidationException;
+import org.simantics.db.layer0.request.PossibleModel;
+import org.simantics.history.History;
+import org.simantics.history.HistoryException;
+import org.simantics.history.HistoryManager;
+import org.simantics.modeling.subscription.CollectSubscriptions;
+import org.simantics.modeling.subscription.ModelHistoryCollector;
+import org.simantics.project.IProject;
+import org.simantics.simulation.experiment.IDynamicExperiment;
+import org.simantics.utils.FileUtils;
+
+/**
+ * This class contains utilities for initializing history collection and makes
+ * trending of subscribed dynamic experiment data possible.
+ *
+ * @author Tuukka Lehtonen
+ */
+public class TrendSupport implements ITrendSupport {
+
+ // Input
+ private IDynamicExperiment experiment;
+ private String workspaceDataDirectoryName;
+
+ // Internals
+ private ModelHistoryCollector historyCollector;
+ private ChartData chartData;
+
+ public TrendSupport(IDynamicExperiment experiment, String workspaceDataDirectoryName) {
+ this.experiment = experiment;
+ this.workspaceDataDirectoryName = workspaceDataDirectoryName;
+ }
+
+ public void initializeHistoryCollection(ReadGraph graph) throws DatabaseException {
+ final IProject project = Simantics.peekProject();
+ if (project == null)
+ return;
+
+ try {
+ File workarea = getExperimentDirectory(graph, experiment.getResource(), true, "result-" + experiment.getIdentifier());
+ final HistoryManager history = History.openFileHistory(workarea);
+
+ historyCollector = ModelHistoryCollector.createCollector(
+ experiment,
+ history,
+ Activator.getDefault().getLog(),
+ () -> {
+ CollectSubscriptions cs = new CollectSubscriptions(TrendSupport.this.experiment, 0.1);
+ return cs;
+ },
+ () -> {
+ // Reset chart data every time subscriptions change
+ // to make sure that trend editor react.
+ if (chartData != null)
+ Charts.resetChartEditorData(project, experiment.getModel(), chartData);
+ });
+
+ historyCollector.initialize(graph, 0, true);
+
+ // Initialize chart source to support trend viewing
+ HistoryManager historyManager = History.openFileHistory(historyCollector.getWorkarea());
+ chartData = new ChartData(experiment.getModel(), null,
+ experiment, experiment.getDatasource(), historyManager,
+ historyCollector.getCollector());
+ Charts.resetChartEditorData(project, experiment.getModel(), chartData);
+ } catch (IOException e) {
+ throw new DatabaseException(e);
+ } catch (HistoryException e) {
+ throw new DatabaseException(e);
+ } catch (InterruptedException e) {
+ throw new DatabaseException(e);
+ }
+ }
+
+ public void dispose() {
+ if (historyCollector != null) {
+ historyCollector.dispose();
+ historyCollector = null;
+ }
+ if (chartData != null) {
+ final IProject project = Simantics.peekProject();
+ if (project != null)
+ Charts.resetChartEditorData(project, experiment.getModel(), null);
+ chartData = null;
+ }
+ }
+
+ /**
+ * @param g
+ * @param experiment
+ * @param ensureExistence
+ * @param subdirs
+ * @return
+ * @throws DatabaseException
+ * @throws IOException
+ */
+ protected File getExperimentDirectory(ReadGraph g, Resource experiment, boolean ensureExistence, String... subdirs) throws DatabaseException, IOException {
+ Resource model = g.syncRequest(new PossibleModel(experiment));
+ if (model == null)
+ throw new ValidationException(
+ "Experiment '" + NameUtils.getSafeName(g, experiment)
+ + "' is not part of any model. Has the experiment been removed already? Database may also be corrupted?");
+
+ String[] dirs = new String[3 + subdirs.length];
+ dirs[0] = workspaceDataDirectoryName;
+ dirs[1] = "model-" + model.getResourceId();
+ dirs[2] = "experiment-" + experiment.getResourceId();
+ System.arraycopy(subdirs, 0, dirs, 3, subdirs.length);
+
+ File experimentPath = getWorkspacePath(false, dirs);
+ if (ensureExistence)
+ ensureDirectoryExistence(experimentPath);
+
+ return experimentPath;
+ }
+
+ /**
+ * @param dir the directory that needs to be created if it does not exist
+ * @return the requested directory if created or deemed existing
+ * @throws IOException if the requested directory cannot be created
+ */
+ protected static File ensureDirectoryExistence(File dir) throws IOException {
+ if (dir.exists() && !dir.isDirectory())
+ FileUtils.deleteAll(dir);
+
+ dir.mkdirs();
+ if (!dir.exists() || !dir.isDirectory())
+ throw new FileNotFoundException("Could not create directory '" + dir + "'. Out of disk space?");
+
+ return dir;
+ }
+
+ /**
+ * @param escapeNames <code>true</code> to run each path segment through
+ * {@link URIUtil#encodeFilename(String)}
+ * @param relativeSegments path segments to append to the workspace root
+ * path
+ * @return the designated path within the workspace
+ */
+ protected static File getWorkspacePath(boolean escapeNames, String... relativeSegments) {
+ IPath finalPath = Platform.getLocation();
+ for (int i = 0; i < relativeSegments.length; ++i)
+ finalPath = finalPath.append(escapeNames ? URIUtil.encodeFilename(relativeSegments[i]) : relativeSegments[i]);
+
+ return finalPath.toFile();
+ }
+
+ @Override
+ public void setChartData(ReadGraph graph) throws DatabaseException {
+ }
+
+ @Override
+ public ChartData getChartData() {
+ return chartData;
+ }
+
+}