From c968d4b0ed22ec34431485899f8eeab61b8e26aa Mon Sep 17 00:00:00 2001 From: lempinen Date: Mon, 2 Aug 2010 10:28:50 +0000 Subject: [PATCH] Save and show results git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16898 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/elements2/InputFactory.java | 8 +- .../sysdyn/ui/project/HistoryVariable.java | 2 +- .../simantics/sysdyn/ui/trend/TrendView.java | 82 ++++++++++++++----- .../sysdyn/manager/SysdynExperiment.java | 14 +--- .../simantics/sysdyn/manager/SysdynModel.java | 14 +++- .../sysdyn/manager/SysdynResult.java | 45 +++++----- 6 files changed, 107 insertions(+), 58 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java index dfc9cfef..d59bc3b3 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/InputFactory.java @@ -118,11 +118,11 @@ public class InputFactory extends SysdynElementFactory { refersTo = graph.getSingleObject(dep, sr.HasTail); referenceName = (String) graph.getPossibleRelatedValue(refersTo, b.HasName); moduleRepresentation = graph.getPossibleResource(variable.substring(0, variable.lastIndexOf("/"))); - Resource parent = graph.getSingleObject(moduleRepresentation, b.PartOf); - module = graph.getSingleObject(moduleRepresentation, b.Represents); + Resource parent = graph.getPossibleObject(moduleRepresentation, b.PartOf); String parentURI = graph.getURI(parent); String modelURI = diagram.getHint((DiagramModelHints.KEY_DIAGRAM_MODEL_URI)); - if(parentURI.equals(modelURI)) + module = graph.getPossibleObject(moduleRepresentation, b.Represents); + if(parentURI.equals(modelURI) || module == null) moduleName = ""; else { moduleName = (String) graph.getPossibleRelatedValue(module, b.HasName); @@ -133,7 +133,7 @@ public class InputFactory extends SysdynElementFactory { } String inputReference = null; - if (module != null && refersTo != null) { + if (moduleName != null && refersTo != null) { inputReference = moduleName + "." + referenceName; } if (inputReference == null) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/HistoryVariable.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/HistoryVariable.java index af2a129a..b686274d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/HistoryVariable.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/HistoryVariable.java @@ -30,7 +30,7 @@ public class HistoryVariable extends ResourceVariable { Resource model = Variables.getModel(graph, this); Resource configuration = graph.getPossibleObject(model, b.HasConfiguration); SysdynModel sm = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configuration); - SysdynResult sr = new SysdynResult(sm.getSimulationResult()); + SysdynResult sr = sm.getSysdynResult(); String rvi = Variables.getRVI(graph, this); System.out.println("HistoryVariable rvi='" + rvi + "'"); rvi = rvi.substring(1).replace("/", "."); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java index 6b034f5f..b9179b93 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java @@ -13,6 +13,7 @@ package org.simantics.sysdyn.ui.trend; import java.awt.Frame; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Set; @@ -32,10 +33,6 @@ import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.AbstractXYDataset; -import org.simantics.databoard.Bindings; -import org.simantics.databoard.accessor.Accessor; -import org.simantics.databoard.accessor.error.AccessorException; -import org.simantics.databoard.binding.error.RuntimeBindingConstructionException; import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; @@ -49,6 +46,9 @@ import org.simantics.diagram.stubs.DiagramResource; import org.simantics.modeling.ModelingUtils; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.manager.SysdynDataSet; +import org.simantics.sysdyn.manager.SysdynModel; +import org.simantics.sysdyn.manager.SysdynModelManager; +import org.simantics.sysdyn.manager.SysdynResult; import org.simantics.ui.SimanticsUI; import org.simantics.utils.ui.ISelectionUtils; import org.simantics.utils.ui.jface.ActiveSelectionProvider; @@ -138,10 +138,10 @@ public class TrendView extends ViewPart { return; if(selection instanceof IStructuredSelection) { Set ress = ISelectionUtils.filterSetSelection(selection, Resource.class); - List runtimes = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_VARIABLE_RESOURCE, Resource.class); - if(runtimes.isEmpty()) - return; - Resource runtime = runtimes.get(0); + List runtimes = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_VARIABLE_RESOURCE, Resource.class); + if(runtimes.isEmpty()) + return; + Resource runtime = runtimes.get(0); setSelection(ress.toArray(Resource.NONE), runtime); } } @@ -159,7 +159,7 @@ public class TrendView extends ViewPart { } public void updateView() { - + Session session = SimanticsUI.peekSession(); if (session == null) return; @@ -170,11 +170,14 @@ public class TrendView extends ViewPart { final ArrayList datasets = new ArrayList(); for(Resource resource : resources) { - SysdynDataSet set = load(g, resource, runtime); - if(set == null) { - return; - } - datasets.add(set); +// SysdynDataSet set = load(g, resource, runtime); +// if(set == null) { +// return; +// } +// datasets.add(set); + Collection activeDataSets = loadAllActive(g, resource, runtime); + if(activeDataSets != null && !activeDataSets.isEmpty()) + datasets.addAll(activeDataSets); } SwingUtilities.invokeLater(new Runnable() { @@ -190,27 +193,28 @@ public class TrendView extends ViewPart { }); } + /* protected SysdynDataSet load(ReadGraph g, Resource element, Resource runtime) throws DatabaseException { Builtins b = g.getBuiltins(); - + SysdynResource sr = SysdynResource.getInstance(g); DiagramResource dr = DiagramResource.getInstance(g); - + if(runtime == null) return null; Resource resource = ModelingUtils.getPossibleElementCorrespondendence(g, element); if(resource == null || !g.isInstanceOf(resource, sr.Variable)) return null; String variableURI = g.getPossibleRelatedValue(runtime, dr.HasVariable); - + System.out.println("load dataset for '"+ g.getPossibleRelatedValue(resource, b.HasName) + "'in composite '" + variableURI + "'"); Variable compositeVariable = Variables.getVariable(g, variableURI); Variable item = compositeVariable.browsePossible(g, resource); if(item == null) return null; - + System.out.println("load dataset for '" + item.getURI(g) + "'"); - + Accessor accessor = item.getInterface(g, Accessor.class); if(accessor == null) return null; try { @@ -223,6 +227,7 @@ public class TrendView extends ViewPart { return null; } + */ @Override public void setFocus() { @@ -230,4 +235,43 @@ public class TrendView extends ViewPart { panel.requestFocus(); } + protected Collection loadAllActive(ReadGraph g, Resource element, Resource runtime) throws DatabaseException { + + ArrayList dataSets = new ArrayList(); + Builtins b = g.getBuiltins(); + + SysdynResource sr = SysdynResource.getInstance(g); + + DiagramResource dr = DiagramResource.getInstance(g); + + if(runtime == null) return null; + Resource resource = ModelingUtils.getPossibleElementCorrespondendence(g, element); + if(resource == null || !g.isInstanceOf(resource, sr.Variable)) return null; + + String variableURI = g.getPossibleRelatedValue(runtime, dr.HasVariable); + + System.out.println("load dataset for '"+ g.getPossibleRelatedValue(resource, b.HasName) + "'in composite '" + variableURI + "'"); + Variable compositeVariable = Variables.getVariable(g, variableURI); + Variable item = compositeVariable.browsePossible(g, resource); + if(item == null) return null; + + String rvi = Variables.getRVI(g, item); + System.out.println("HistoryVariable rvi='" + rvi + "'"); + + System.out.println("load dataset for '" + item.getURI(g) + "'"); + + Resource model = Variables.getModel(g, item); + Resource configuration = g.getPossibleObject(model, b.HasConfiguration); + SysdynModel sm = SysdynModelManager.getInstance(g.getSession()).getModel(g, configuration); + + Collection activeResults = sm.getActiveResults(g); + for(SysdynResult sysdynResult : activeResults) { + SysdynDataSet sds = sysdynResult.getDataSet(rvi.substring(1).replace("/", ".")); + if(sds != null) + dataSets.add(sds); + } + return dataSets; + + } + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java index 2b2a3149..efb17d00 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynExperiment.java @@ -32,7 +32,6 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { Runnable modificationListener; Resource experiment; SysdynModel sysdynModel; - SysdynResult result; public SysdynExperiment(Resource experiment, Resource model) { super(model); @@ -54,7 +53,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { public void saveState() { // TODO Auto-generated method stub System.out.println("saveState"); - if(sysdynModel.getSimulationResult() == null) return; + if(sysdynModel.getSysdynResult() == null) return; try { session.syncRequest(new WriteRequest() { @@ -77,7 +76,7 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { b.PartOf, model, sr.HasResultFile, file.getAbsolutePath()); graph.claim(experiment, sr.HasResult, res); - result.saveToFile(file); + sysdynModel.getSysdynResult().saveToFile(file); } }); session.getService(LifecycleSupport.class).save(); @@ -158,7 +157,6 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { System.out.println("init"); changeState(ExperimentState.STOPPED); this.session = g.getSession(); - result = new SysdynResult(); session.asyncRequest(new ReadRequest() { @@ -166,14 +164,6 @@ public class SysdynExperiment extends Experiment implements IDynamicExperiment { public void run(ReadGraph graph) throws DatabaseException { final Resource configuration = graph.getPossibleObject(model, graph.getBuiltins().HasConfiguration); sysdynModel = SysdynModelManager.getInstance(session).getModel(graph, configuration); - sysdynModel.addResultListener(new Runnable() { - - @Override - public void run() { - result.setResult(sysdynModel.getSimulationResult()); - } - }); - toggleActivation(graph, true); } }); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java index 3bc82721..dfb8d396 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -55,6 +55,7 @@ public class SysdynModel implements IMappingListener, IModel { Configuration configuration; Set modules = new HashSet(); SimulationResult result; + SysdynResult sysdynResult; CopyOnWriteArrayList modificationListeners = new CopyOnWriteArrayList(); @@ -107,6 +108,8 @@ public class SysdynModel implements IMappingListener, IModel { } catch(DatabaseException e) { e.printStackTrace(); } + sysdynResult = new SysdynResult(); + sysdynResult.setResult(new SimulationResult()); } @@ -134,7 +137,8 @@ public class SysdynModel implements IMappingListener, IModel { writer.toString(), monitor, configuration.startTime, - configuration.stopTime); + configuration.stopTime); + sysdynResult.setResult(result); } catch(Exception e) { e.printStackTrace(); result = null; @@ -162,6 +166,10 @@ public class SysdynModel implements IMappingListener, IModel { public SimulationResult getSimulationResult() { return result; } + + public SysdynResult getSysdynResult() { + return sysdynResult; + } public void addResultListener(Runnable listener) { synchronized(resultListeners) { @@ -259,8 +267,8 @@ public class SysdynModel implements IMappingListener, IModel { e.printStackTrace(); } - if(getSimulationResult() != null) - results.add(0, new SysdynResult(getSimulationResult())); + if(getSysdynResult() != null) + results.add(0, getSysdynResult() ); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java index bc38975e..85efa71a 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynResult.java @@ -32,7 +32,7 @@ import org.simantics.modelica.data.DataSet; import org.simantics.modelica.data.SimulationResult; public class SysdynResult { - + RecordingSession recordingSession; RecordAccessor accessor; String resultName; @@ -47,14 +47,13 @@ public class SysdynResult { } public SysdynResult(String result, String path) { - System.out.println("sysdynresult " + result + " " + path); this.resultName = result; File file = new File(path); try { BinaryVariant bv = (BinaryVariant)Accessors.openAccessor(file); BinaryRecord br = bv.getValueAccessor(); accessor = br; - } catch (AccessorConstructionException e) { + } catch (AccessorConstructionException e) { e.printStackTrace(); } } @@ -72,13 +71,13 @@ public class SysdynResult { try { Binding stringBinding = StringBindingDefault.INSTANCE; Binding recordBinding = Bindings.getBinding( Recording.class ); - + recordingSession = new RecordingSession(); recordingSession.recordings = new TreeMap(); MutableVariant timeVariableId = new MutableVariant(); timeVariableId.setValue(stringBinding, "timeVariableId"); recordingSession.timeVariableId = timeVariableId; - + for(DataSet ds : result.getVariableDataSets()){ @@ -110,6 +109,25 @@ public class SysdynResult { } catch (Exception e) { e.printStackTrace(); } + + if(recordingSession == null) return; + + + try { + Binding recordBinding = Bindings.getBinding( RecordingSession.class ); + if(accessor == null) { + accessor = (JavaRecord)Accessors.getAccessor(recordBinding, recordingSession); + } else { + accessor.setValue(recordBinding, recordingSession); + } + } catch (AccessorConstructionException e) { + e.printStackTrace(); + } catch (BindingConstructionException e) { + e.printStackTrace(); + } catch (AccessorException e) { + e.printStackTrace(); + } + } public void saveToFile(File file) { @@ -126,7 +144,7 @@ public class SysdynResult { } catch (BindingException e) { e.printStackTrace(); } - + } @@ -176,19 +194,8 @@ public class SysdynResult { public static class NodeId { public String nodeId; } - + public SysdynDataSet getDataSet(String variable) { - if(accessor == null && recordingSession != null) { - try { - Binding recordBinding = Bindings.getBinding( RecordingSession.class ); - accessor = (JavaRecord)Accessors.getAccessor(recordBinding, recordingSession); - } catch (AccessorConstructionException e) { - e.printStackTrace(); - } catch (BindingConstructionException e) { - e.printStackTrace(); - } - } - if(accessor != null) { MapAccessor ma; try { @@ -209,7 +216,7 @@ public class SysdynResult { VariantAccessor va = ma.getValueAccessor( MutableVariantBinding.INSTANCE, tmpVariant); Accessor valueAccessor = va.getValueAccessor(); rec = (Recording)valueAccessor.getValue( Bindings.getBinding(Recording.class)); - + va = ma.getValueAccessor( MutableVariantBinding.INSTANCE, tmpVariant2); valueAccessor = va.getValueAccessor(); time = (Recording)valueAccessor.getValue( Bindings.getBinding(Recording.class)); -- 2.47.1