From e37724c3c0038cd8a707f0a826f87fb4175a0fce Mon Sep 17 00:00:00 2001 From: miettinen Date: Fri, 14 Jun 2013 05:15:50 +0000 Subject: [PATCH] * Propose and allow only parameters for sensitivity analysis simulation. (refs #4319) * Rainbow icon to sensitivity chart (refs #4340) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27540 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../properties/VariableExistsValidator.java | 4 +- org.simantics.sysdyn.ui/adapters.xml | 2 +- org.simantics.sysdyn.ui/plugin.xml | 2 +- .../SensitivityAnalysisExperimentTab.java | 3 +- .../sysdyn/ui/trend/AllParametersOfModel.java | 132 ++++++++++++++++++ .../validation/ParameterExistsValidator.java | 50 +++++++ .../sysdyn/adapter/ValuesIndexVariable.java | 50 +++---- 7 files changed, 214 insertions(+), 29 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/AllParametersOfModel.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ParameterExistsValidator.java diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableExistsValidator.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableExistsValidator.java index aa2ecbc8..e8c8e03b 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableExistsValidator.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/VariableExistsValidator.java @@ -26,8 +26,8 @@ import org.simantics.utils.ui.AdaptionUtils; */ public class VariableExistsValidator implements IInputValidator, Widget { - private Collection variables; - private TrackedText text; + protected Collection variables; + protected TrackedText text; private boolean allowEmpty; @SuppressWarnings("unused") private boolean useLabels = false; diff --git a/org.simantics.sysdyn.ui/adapters.xml b/org.simantics.sysdyn.ui/adapters.xml index 51e3e4b1..68f7b5c4 100644 --- a/org.simantics.sysdyn.ui/adapters.xml +++ b/org.simantics.sysdyn.ui/adapters.xml @@ -10,5 +10,5 @@ VTT Technical Research Centre of Finland - initial API and implementation --> - none 0 fill 1 + none 0 fill 1 \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index df68d180..2dea0afb 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -816,7 +816,7 @@ diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/SensitivityAnalysisExperimentTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/SensitivityAnalysisExperimentTab.java index 2dcfc4ca..2bdaf8f4 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/SensitivityAnalysisExperimentTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/SensitivityAnalysisExperimentTab.java @@ -68,6 +68,7 @@ import org.simantics.sysdyn.ui.properties.widgets.factories.IntegerPropertyModif import org.simantics.sysdyn.ui.properties.widgets.sensitivity.DistributionPropertyWidget; import org.simantics.sysdyn.ui.properties.widgets.sensitivity.ParameterChildRule; import org.simantics.sysdyn.ui.properties.widgets.sensitivity.VariableNameModifier; +import org.simantics.sysdyn.ui.validation.ParameterExistsValidator; import org.simantics.ui.SimanticsUI; import org.simantics.utils.RunnableWithObject; import org.simantics.utils.datastructures.ArrayMap; @@ -221,7 +222,7 @@ public class SensitivityAnalysisExperimentTab extends LabelPropertyTabContributo variable.setTextFactory(new StringPropertyFactory(SysdynResource.URIs.SensitivityAnalysisExperiment_Parameter_variable)); variable.addModifyListener(new VariableNameModifier(variable.getWidget(), parameterSupport, SysdynResource.URIs.SensitivityAnalysisExperiment_Parameter_variable, SysdynResource.URIs.SensitivityAnalysisExperiment_Parameter_indexes)); variable.setColorProvider(new SysdynBasicColorProvider(new LocalResourceManager(JFaceResources.getResources(), variable.getWidget()))); - variable.setInputValidator(new VariableExistsValidator(parameterSupport, variable)); + variable.setInputValidator(new ParameterExistsValidator(parameterSupport, variable)); GridDataFactory.fillDefaults().grab(true, false).applyTo(variable.getWidget()); label = new Label(parameterProperties, SWT.NONE); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/AllParametersOfModel.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/AllParametersOfModel.java new file mode 100644 index 00000000..de84ecf5 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/AllParametersOfModel.java @@ -0,0 +1,132 @@ +package org.simantics.sysdyn.ui.trend; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.jfreechart.chart.properties.ChartVariable; +import org.simantics.jfreechart.chart.properties.IAllVariablesOfModel; +import org.simantics.layer0.Layer0; +import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.strings.AlphanumComparator; +import org.simantics.sysdyn.manager.SysdynModel; +import org.simantics.sysdyn.manager.SysdynModelManager; +import org.simantics.sysdyn.representation.IElement; +import org.simantics.sysdyn.representation.IndependentVariable; +import org.simantics.sysdyn.representation.Variability; + +/** + * Request for getting all variables of a model in a String array. Includes also + * variables inside modules. + * + * @author Teemu Lempinen + * @author Tuomas Miettinen + * + */ +public class AllParametersOfModel implements IAllVariablesOfModel { + + protected final Resource model; + + public AllParametersOfModel(Resource model) { + this.model = model; + } + + @Override + public Read> getVariablesQuery() { + return new VariableQuery(); + } + + + private class VariableQuery implements Read> { + @Override + public Collection perform(ReadGraph graph) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + SimulationResource simu = SimulationResource.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + + List result = new ArrayList(); + // Find the model of this resource + Resource model = AllParametersOfModel.this.model; + while(model != null && !graph.isInstanceOf(model, sr.SysdynModel)) + model = graph.getPossibleObject(model, l0.PartOf); + + if(model == null) + return result; + + // Find the models configuration + Resource conf = graph.getSingleObject(model, simu.HasConfiguration); + + // Recursively read all configurations and add items + ReadConfiguration(graph, conf, "", result); + + // Finally sort the results + Collections.sort(result, AlphanumComparator.CASE_INSENSITIVE_COMPARATOR); + return result; + } + } + + + + /** + * Read components in a configuration and recursively all module configurations + * + * @param graph ReadGraph + * @param configuration Resource to be read + * @param path Current path from base realization + * @param items Found variables + * @throws DatabaseException + */ + private void ReadConfiguration(ReadGraph graph, Resource configuration, String path, Collection items) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + + SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession()); + SysdynModel sm = sdm.getModel(graph, configuration); + try { + sm.update(graph); + } catch (DatabaseException e1) { + e1.printStackTrace(); + } + + String name; + for(Resource resource : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.IndependentVariable))) { + name = path + NameUtils.getSafeName(graph, resource); + IElement element = sm.getElement(resource); + if (element instanceof IndependentVariable) { + IndependentVariable variable = (IndependentVariable)element; + Variability variability = Variability.getVariability(variable, false, null); + if (variability == Variability.PARAMETER) { + items.add(new ChartVariable(name, name)); + } + } + } + + for(Resource module : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Module))) { + Resource instanceOf = graph.getPossibleObject(module, l0.InstanceOf); + Resource conf = graph.getPossibleObject(instanceOf, sr2.IsDefinedBy); + if(conf != null) { + String p = path + NameUtils.getSafeName(graph, module) + "."; + ReadConfiguration(graph, conf, p, items); + } + } + } + + @Override + public String getVariablesLabel(ReadGraph graph, String variableId) + throws DatabaseException { + return variableId.substring(1).replace('/', '.'); + } + +} + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ParameterExistsValidator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ParameterExistsValidator.java new file mode 100644 index 00000000..0fb44b63 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ParameterExistsValidator.java @@ -0,0 +1,50 @@ +package org.simantics.sysdyn.ui.validation; + +import java.util.Collection; + +import org.simantics.browsing.ui.swt.widgets.TrackedText; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.jfreechart.chart.properties.AllVariablesOfModel; +import org.simantics.jfreechart.chart.properties.ChartVariable; +import org.simantics.jfreechart.chart.properties.VariableExistsValidator; +import org.simantics.db.Resource; +import org.simantics.db.management.ISessionContext; +import org.simantics.db.procedure.Listener; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.ui.AdaptionUtils; + +public class ParameterExistsValidator extends VariableExistsValidator { + + public ParameterExistsValidator(WidgetSupport support, TrackedText text) { + super(support, text); + } + + @Override + public void setInput(ISessionContext context, Object input) { + final Resource resource = AdaptionUtils.adaptToSingle(input, Resource.class); + if(resource == null) + return; + + SimanticsUI.getSession().asyncRequest( + new AllVariablesOfModel(resource) + , new Listener>() { + + @Override + public void execute(Collection variables) { + ParameterExistsValidator.this.variables = variables; + } + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public boolean isDisposed() { + return ParameterExistsValidator.this.text.isDisposed(); + } + + }); + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValuesIndexVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValuesIndexVariable.java index 52218d04..687d5034 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValuesIndexVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValuesIndexVariable.java @@ -20,33 +20,35 @@ public class ValuesIndexVariable extends IndexVariable { @Override public double[][] getValue() { - Collection results = experiment.getActiveResults(); ArrayList variableNames = getVariableNamesWithIndexNumbers(); - double[][] result = new double[variableNames.size()][]; - for(int i = 0; i < variableNames.size(); i++) { - for(SysdynResult r : results) { - SysdynDataSet ds = r.getDataSet(variableNames.get(i)); - if(ds != null && ds.values != null) { - result[i] = new double[ds.values.length]; - for(int j = 0; j < ds.values.length; j++) { - result[i][j] = ds.values[j]; - } - - /* - * If this is the actual simulation result and experiment is game experiment, - * get the last value from experiment. It might be different from the result - * file, if it has been modified in current time step - */ - if(experiment instanceof SysdynGameExperiment && ds.result == null) { - double d = ((SysdynGameExperiment)experiment).getCurrentValue(variableNames.get(i)); - result[i][ds.values.length - 1] = d; - } - } else { - result[i] = new double[0]; - } - } + if (experiment != null) { + Collection results = experiment.getActiveResults(); + for(int i = 0; i < variableNames.size(); i++) { + for(SysdynResult r : results) { + SysdynDataSet ds = r.getDataSet(variableNames.get(i)); + if(ds != null && ds.values != null) { + result[i] = new double[ds.values.length]; + for(int j = 0; j < ds.values.length; j++) { + result[i][j] = ds.values[j]; + } + + /* + * If this is the actual simulation result and experiment is game experiment, + * get the last value from experiment. It might be different from the result + * file, if it has been modified in current time step + */ + if(experiment instanceof SysdynGameExperiment && ds.result == null) { + double d = ((SysdynGameExperiment)experiment).getCurrentValue(variableNames.get(i)); + result[i][ds.values.length - 1] = d; + } + + } else { + result[i] = new double[0]; + } + } + } } return result; } -- 2.47.1