From: miettinen Date: Fri, 30 Aug 2013 05:41:24 +0000 (+0000) Subject: Save sensitivity analysis results (refs #4243). X-Git-Tag: 1.8.1~251 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=fcaea668ae2fb4a7319f4d37c3f505a077c139cc;p=simantics%2Fsysdyn.git Save sensitivity analysis results (refs #4243). A few little fixes. git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27794 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index c032924c..93d42bb8 100644 Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ diff --git a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph index 6b4860ab..12cf9621 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -327,7 +327,8 @@ SYSDYN.SysdynModelicaFunction.Output -- SYSDYN.Experiment.result --> SYSDYN.Result -- SYSDYN.Experiment.resultSet --> SYSDYN.ResultSet -- SYSDYN.Result.time --> L0.Long -- SYSDYN.HistoryDataset.columns --> L0.Boolean -- SYSDYN.HistoryDataset.sheet --> SHEET.Spreadsheet -- SYSDYN.Experiment.result --> SYSDYN.Result @@ -898,6 +898,22 @@ + + + + + + + + + + + + @@ -1571,6 +1596,10 @@ args="org.simantics.sysdyn.ui.browser.nodes.SimulationResultNode" property="org.simantics.sysdyn.ui.nodeClass"> + + @@ -1840,6 +1869,10 @@ class="org.simantics.sysdyn.ui.browser.contributions.SimulationResult" preference="2.0"> + + @@ -2007,6 +2040,10 @@ class="org.simantics.sysdyn.ui.browser.contributions.SimulationResultLabeler" preference="2.0"> + + @@ -2163,6 +2200,10 @@ class="org.simantics.sysdyn.ui.browser.contributions.SimulationResultImager" preference="2.0"> + + @@ -2202,6 +2243,10 @@ class="org.simantics.sysdyn.ui.browser.contributions.SimulationResultDecorator" preference="2.0"> + + @@ -2230,6 +2275,11 @@ name="Activate result in charts" priority="250.0"> + + results = graph.getObjects(resultSet, sr.Experiment_result); + for (Resource result : results) { + if (graph.hasStatement(result, sr.Result_showResult)) { + resultShown = true; + break; + } + } + for (Resource result : results) { + if (resultShown) { + graph.denyStatement(result, sr.Result_showResult, result); + } else { + graph.claim(result, sr.Result_showResult, result); + } + } + } + } + }); + e.consume(); + } + + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java index 98fa0ce0..802a846a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java @@ -82,7 +82,7 @@ public class AbstractNodeImager extends ImagerContributor else if(graph.isInstanceOf(node.data, sr.GameExperiment)) image = "icons/time_go.png"; else if(graph.isInstanceOf(node.data, sr.SensitivityAnalysisExperiment)) - image = "icons/rainbow.png"; + image = "icons/time_rainbow.png"; else image = "icons/time.png"; } else if (node instanceof InputNode) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResult.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResult.java index dfa9897a..f7bd86f0 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResult.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResult.java @@ -27,19 +27,22 @@ import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.browser.nodes.ExperimentNode; import org.simantics.sysdyn.ui.browser.nodes.HistoryDataNode; +import org.simantics.sysdyn.ui.browser.nodes.SimulationResultNode; +import org.simantics.sysdyn.ui.browser.nodes.SimulationResultSetNode; -public class SimulationResult extends ViewpointContributor { +public class SimulationResult extends ViewpointContributor> { - @SuppressWarnings("unchecked") @Override - public Collection getContribution(ReadGraph graph, ExperimentNode experiment) throws DatabaseException { + public Collection getContribution(ReadGraph graph, AbstractNode node) throws DatabaseException { ArrayList> result = new ArrayList>(); SysdynResource sr = SysdynResource.getInstance(graph); - for(final Resource r : graph.syncRequest(new ObjectsWithType(experiment.data, sr.Experiment_result, sr.Result))) { + if (!(node instanceof ExperimentNode || node instanceof SimulationResultSetNode)) + return result; + for(final Resource r : graph.syncRequest(new ObjectsWithType(node.data, sr.Experiment_result, sr.Result))) { if(graph.isInstanceOf(r, sr.HistoryDataset)) { - result.add(graph.adapt(r, AbstractNode.class)); - } else { result.add(new HistoryDataNode(r)); + } else { + result.add(new SimulationResultNode(r)); String resultPath = (String)graph.getPossibleRelatedValue(r, sr.Result_resultFile); File file = new File(resultPath); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultDecorator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultDecorator.java index 442e91a0..3676eba1 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultDecorator.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultDecorator.java @@ -35,7 +35,14 @@ public class SimulationResultDecorator extends LabelDecoratorContributor F decorateFont(F font, String column, int itemIndex) { + return (F) ((FontDescriptor) font).withStyle(SWT.NORMAL); + } + }; } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultSet.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultSet.java new file mode 100644 index 00000000..dfbe8811 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultSet.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2010, 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.sysdyn.ui.browser.contributions; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.browser.nodes.ExperimentNode; +import org.simantics.sysdyn.ui.browser.nodes.SimulationResultSetNode; + +public class SimulationResultSet extends ViewpointContributor { + + @Override + public Collection getContribution(ReadGraph graph, ExperimentNode experiment) throws DatabaseException { + ArrayList results = new ArrayList(); + SysdynResource sr = SysdynResource.getInstance(graph); + for(final Resource r : graph.syncRequest(new ObjectsWithType(experiment.data, sr.Experiment_resultSet, sr.ResultSet))) { + results.add(new SimulationResultSetNode(r)); + } + return results; + } + + @Override + public String getViewpointId() { + return "Standard"; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultSetDecorator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultSetDecorator.java new file mode 100644 index 00000000..2491ee51 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultSetDecorator.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2010, 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.sysdyn.ui.browser.contributions; + +import org.eclipse.jface.resource.FontDescriptor; +import org.eclipse.swt.SWT; +import org.simantics.browsing.ui.content.LabelDecorator; +import org.simantics.browsing.ui.graph.contributor.labeler.LabelDecoratorContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.browser.nodes.SimulationResultSetNode; + +public class SimulationResultSetDecorator extends LabelDecoratorContributor{ + + @Override + public LabelDecorator getDecorator(ReadGraph graph, SimulationResultSetNode resultSet) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + for (Resource result : graph.getObjects(resultSet.data, sr.Experiment_result)) { + if (graph.hasStatement(result, sr.Result_showResult)) { + return new LabelDecorator.Stub() { + + @SuppressWarnings("unchecked") + @Override + public F decorateFont(F font, String column, int itemIndex) { + return (F) ((FontDescriptor) font).withStyle(SWT.BOLD); + } + }; + } + } + return new LabelDecorator.Stub() { + + @SuppressWarnings("unchecked") + @Override + public F decorateFont(F font, String column, int itemIndex) { + return (F) ((FontDescriptor) font).withStyle(SWT.NORMAL); + } + }; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultSetImager.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultSetImager.java new file mode 100644 index 00000000..88c73806 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultSetImager.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2010, 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.sysdyn.ui.browser.contributions; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.simantics.browsing.ui.swt.ImagerContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.Activator; +import org.simantics.sysdyn.ui.browser.nodes.SimulationResultSetNode; + +public class SimulationResultSetImager extends ImagerContributor{ + + @Override + public ImageDescriptor getDescriptor(ReadGraph graph, SimulationResultSetNode resultSet) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + for (Resource result : graph.getObjects(resultSet.data, sr.Experiment_result)) { + if (graph.hasStatement(result, sr.Result_showResult)) { + return ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/chart_bar_3.png")); + } + } + return ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/chart_bar_3_blackAndWhite.png")); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultSetLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultSetLabeler.java new file mode 100644 index 00000000..16c42593 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SimulationResultSetLabeler.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2010, 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.sysdyn.ui.browser.contributions; + +import org.simantics.browsing.ui.graph.contributor.labeler.LabelerContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.ui.browser.nodes.SimulationResultSetNode; + +public class SimulationResultSetLabeler extends LabelerContributor{ + + @Override + public String getLabel(ReadGraph graph, SimulationResultSetNode resultSet) throws DatabaseException { + String name = graph.getPossibleRelatedValue(resultSet.data, Layer0.getInstance(graph).HasLabel); + return name == null ? "Experiment (no name)" : name; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ExperimentNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ExperimentNode.java index d272900f..deeb8dd7 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ExperimentNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ExperimentNode.java @@ -77,6 +77,10 @@ public class ExperimentNode extends AbstractNode implements IDoubleCli if(results != null) for(Resource result : results) SimulationResultNode.unlinkResult(graph, result); + Collection resultSets = graph.getObjects(data, SysdynResource.getInstance(graph).Experiment_resultSet); + if(resultSets != null) + for(Resource resultSet : resultSets) + SimulationResultSetNode.unlinkResultSet(graph, resultSet); RemoverUtil.remove(graph, data); } }); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SimulationResultSetNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SimulationResultSetNode.java new file mode 100644 index 00000000..194e1c9e --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SimulationResultSetNode.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (c) 2010, 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.sysdyn.ui.browser.nodes; + +import org.simantics.browsing.ui.common.node.AbstractNode; +import org.simantics.browsing.ui.common.node.DeleteException; +import org.simantics.browsing.ui.common.node.IDeletableNode; +import org.simantics.browsing.ui.common.node.IDoubleClickableNode; +import org.simantics.browsing.ui.common.node.IModifiableNode; +import org.simantics.browsing.ui.content.Labeler.Modifier; +import org.simantics.browsing.ui.graph.impl.LabelModifier; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.CancelTransactionException; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.RemoverUtil; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.handlers.ToggleResultSetActivation; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.ui.ExceptionUtils; + +public class SimulationResultSetNode extends AbstractNode implements IDoubleClickableNode, IDeletableNode, IModifiableNode { + + public SimulationResultSetNode(Resource resource) { + super(resource); + } + + + @Override + public Modifier getModifier(String columnId) { + LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data) { + @Override + public String isValid(String label) { + if (label.isEmpty()) + return "Empty label not allowed"; + return null; + } + }; + return modifier; + } + + + @Override + public void delete() throws DeleteException { + try { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException, CancelTransactionException { + unlinkResultSet(graph, data); + } + }); + } catch (DatabaseException e) { + ExceptionUtils.logAndShowError(e); + } + + } + + + public static void unlinkResultSet(WriteGraph graph, Resource resultSet) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + for (Resource result : graph.syncRequest(new ObjectsWithType(resultSet, sr.Experiment_result, sr.Result))) { + SimulationResultNode.unlinkResult(graph, result); + } + RemoverUtil.remove(graph, resultSet); + } + + @Override + public boolean handleDoubleClick() { + Resource[] resources = {data}; + ToggleResultSetActivation.toggleActivation(resources); + return true; + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ToggleResultSetActivation.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ToggleResultSetActivation.java new file mode 100644 index 00000000..a667a8c0 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ToggleResultSetActivation.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2010, 2011 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.sysdyn.ui.handlers; + +import java.util.Collection; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.utils.ResourceAdaptionUtils; + +public class ToggleResultSetActivation extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + ISelection sel = HandlerUtil.getCurrentSelection(event); + Resource[] resources = ResourceAdaptionUtils.toResources(sel); + if (resources.length == 0) + return null; + + toggleActivation(resources); + + return null; + } + + public static void toggleActivation(final Resource[] resources) { + try { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + for(Resource r : resources) { + // If there is at least one result which shown, clear them all. + // If not one result is shown, show them all. + boolean resultShown = false; + Collection results = graph.getObjects(r, sr.Experiment_result); + for (Resource result : results) { + if (graph.hasStatement(result, sr.Result_showResult)) { + resultShown = true; + break; + } + } + for (Resource result : results) { + if (resultShown) { + graph.denyStatement(result, sr.Result_showResult, result); + } else { + graph.claim(result, sr.Result_showResult, result); + } + } + } + } + + }); + } catch (DatabaseException e) { + + } + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java index 5fd9af0b..5f00db8b 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java @@ -229,6 +229,14 @@ public class SysdynProject extends AbstractProjectFeature { if(result != null) resultPaths.put(resultFile, result); } } + for(Resource resultSet : graph.getObjects(experiment, sr.Experiment_resultSet)) { + for(Resource result : graph.getObjects(resultSet, sr.Experiment_result)) { + if(!graph.isInstanceOf(result, sr.HistoryDataset)) { + String resultFile = (String)graph.getPossibleRelatedValue(result, sr.Result_resultFile); + if(result != null) resultPaths.put(resultFile, result); + } + } + } } } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/ActiveResults.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/ActiveResults.java index 5e1f3fa6..6ae1b035 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/ActiveResults.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/ActiveResults.java @@ -54,6 +54,15 @@ public class ActiveResults implements Read>{ result.add(r); } } + Collection experimentResultSets = graph.getObjects(experiment, SR.Experiment_resultSet); + for(Resource resultSet : experimentResultSets) { + experimentResults = graph.getObjects(resultSet, SR.Experiment_result); + for(Resource r : experimentResults) { + if(graph.hasStatement(r, SR.Result_showResult)) { + result.add(r); + } + } + } } return result; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultJob.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultJob.java index 7b0ea6ba..caf06b3d 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultJob.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultJob.java @@ -56,7 +56,7 @@ public class SaveResultJob extends Job { protected IStatus run(IProgressMonitor monitor) { this.monitor = monitor; int resultItemsNumber = sysdynResult.numberOfVariables(); - monitor.beginTask("Save result", resultItemsNumber * 2); + monitor.beginTask("Save result", resultItemsNumber * 2 + 1); try { // Create result file file = session.syncRequest(new Read() { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultSetJob.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultSetJob.java new file mode 100644 index 00000000..21385549 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SaveResultSetJob.java @@ -0,0 +1,153 @@ +/******************************************************************************* + * Copyright (c) 2010, 2013 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.sysdyn.manager; + +import java.io.File; +import java.util.ArrayList; +import java.util.UUID; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.request.WriteResultRequest; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.sysdyn.SysdynResource; + +/** + * Class for saving simulation result sets. + * @author Tuomas Miettinen + * + */ +public class SaveResultSetJob extends Job { + + private final ArrayList sysdynResults; + private final SysdynExperiment experiment; + private final Session session; + private IProgressMonitor monitor; + private File file; + + public SaveResultSetJob(final SysdynExperiment experiment, Session session, final ArrayList results) { + super("Save Results"); + this.experiment = experiment; + this.sysdynResults = results; + this.session = session; + } + + @Override + protected IStatus run(IProgressMonitor monitor) { + this.monitor = monitor; + if (sysdynResults == null || sysdynResults.size() == 0) + return Status.CANCEL_STATUS; + int resultItemsNumber = sysdynResults.get(0).numberOfVariables() * sysdynResults.size(); + monitor.beginTask("Save results", resultItemsNumber * 2 + 2); + + try { + // Create a ResultSet, under which the individual results are stored, and + // add it under the experiment within the model browser. + + final Resource resultSetResource = session.syncRequest(new WriteResultRequest() { + + @Override + public Resource perform(WriteGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + Resource model = graph.getSingleObject(experiment.getResource(), l0.PartOf); + String name = NameUtils.findFreshName(graph, "ResultSet", model, l0.ConsistsOf, "%s%d"); + + SysdynResource sr = SysdynResource.getInstance(graph); + Resource res = GraphUtils.create2(graph, sr.ResultSet, + l0.HasLabel, name, + l0.HasName, name, + l0.PartOf, model, + sr.Result_time, System.currentTimeMillis()); + graph.claim(experiment.getResource(), sr.Experiment_resultSet, res); + return res; + } + }); + monitor.worked(1); + + // Save the individual results + for (int i = 0; i < sysdynResults.size(); ++i) { + MemoryResult result = sysdynResults.get(i); + final String name = new Integer(i+1).toString(); + // Create result file + file = session.syncRequest(new Read() { + + @Override + public File perform(ReadGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + Resource model = graph.getSingleObject(experiment.getResource(), l0.PartOf); + Resource project = graph.getSingleObject(model, l0.PartOf); + String projectName = graph.getPossibleRelatedValue(project, l0.HasName); + File root = new File(Platform.getLocation().toOSString(), "www.simantics.org"); + if(!root.isDirectory()) root.mkdir(); + File projectRoot = new File(root, projectName); + if(!projectRoot.isDirectory()) projectRoot.mkdir(); + File file = new File( projectRoot, UUID.randomUUID().toString() + ".dbb"); + return file; + } + }); + + // Start the saving operation + Thread saveThread = experiment.getSaveThread(result, file, SaveResultSetJob.this.monitor); + saveThread.run(); + + // Add the result under the ResultSet within the model browser. + session.syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + String resultSetName = graph.getRelatedValue(resultSetResource, l0.HasName); + + SysdynResource sr = SysdynResource.getInstance(graph); + Resource res = GraphUtils.create2(graph, sr.Result, + l0.HasLabel, name, + l0.HasName, resultSetName + "#" + name, + sr.Result_resultFile, file.getAbsolutePath(), + sr.Result_time, System.currentTimeMillis()); + graph.claim(resultSetResource, sr.Experiment_result, res); + } + }); + + } + + } catch (DatabaseException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + monitor.done(); + return Status.OK_STATUS; + } + + @Override + public boolean belongsTo(Object family) { + return "SaveResultSetJob".equals(family); + } + + @Override + protected void canceling() { + + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynSensitivityAnalysisExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynSensitivityAnalysisExperiment.java index 182f0430..c543e95d 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynSensitivityAnalysisExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynSensitivityAnalysisExperiment.java @@ -322,8 +322,8 @@ public class SysdynSensitivityAnalysisExperiment extends SysdynExperiment { public void saveState() { if(results == null || !(results instanceof ArrayList)) return; - //SaveResultJob saveResultJob = new SaveResultJob(SysdynSensitivityAnalysisExperiment.this, session, results); - //saveResultJob.schedule(); + SaveResultSetJob saveResultSetJob = new SaveResultSetJob((SysdynExperiment)this, session, results); + saveResultSetJob.schedule(); } @Override