--- /dev/null
+/*******************************************************************************\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