From: villberg Date: Mon, 17 Mar 2014 14:02:27 +0000 (+0000) Subject: Initial support. Added simulation tests for models in rootFiles X-Git-Tag: 1.8.1~118 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=996fb15eb3d240cdb7e1ca1aedfbda5b46b16014;p=simantics%2Fsysdyn.git Initial support. Added simulation tests for models in rootFiles refs #4766 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29141 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.tests/META-INF/MANIFEST.MF b/org.simantics.sysdyn.tests/META-INF/MANIFEST.MF index 62fbacb3..ff6e3bc5 100644 --- a/org.simantics.sysdyn.tests/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn.tests/META-INF/MANIFEST.MF @@ -5,7 +5,8 @@ Bundle-SymbolicName: org.simantics.sysdyn.tests Bundle-Version: 1.0.0.qualifier Bundle-Activator: org.simantics.sysdyn.tests.Activator Require-Bundle: org.simantics.modelica;bundle-version="1.0.0", - org.junit;bundle-version="4.8.2" + org.junit;bundle-version="4.8.2", + org.simantics.db.testing;bundle-version="1.0.0" Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Import-Package: org.osgi.framework diff --git a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scl/SCLTestScripts.java b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scl/SCLTestScripts.java new file mode 100644 index 00000000..40755c9c --- /dev/null +++ b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scl/SCLTestScripts.java @@ -0,0 +1,21 @@ +package org.simantics.sysdyn.tests.scl; + +import junit.runner.Version; + +import org.junit.Test; +import org.simantics.db.testing.base.ScriptTestBase; + +public class SCLTestScripts extends ScriptTestBase { + + public SCLTestScripts() { + super("../scripts"); + System.out.println("JUnit version is: " + Version.id()); + } + + @Test public void ImportModel_DiscoveryAndExploitationOfResources() { test(300); } + @Test public void ImportModel_GlobalCarbonCycle() { test(300); } + @Test public void ImportModel_PredatorAndLimitedPrey() { test(300); } + @Test public void ImportModel_ResourceExploitationUseRecycling() { test(300); } + @Test public void ImportModel_TouristsEnvironmentsAndHotelFacilities() { test(300); } + +} \ No newline at end of file diff --git a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scl/SCLTests.java b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scl/SCLTests.java new file mode 100644 index 00000000..db9b75cc --- /dev/null +++ b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scl/SCLTests.java @@ -0,0 +1,24 @@ +package org.simantics.sysdyn.tests.scl; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Suite.SuiteClasses; +import org.simantics.db.testing.common.RegressionSuiteRunner; + +@RunWith(RegressionSuiteRunner.class) +@SuiteClasses({SCLTestScripts.class}) + +public class SCLTests { + + @BeforeClass + public static void setUp() { + + } + + @Test + public static void asd() { + System.out.println("asd"); + } + +} diff --git a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_DiscoveryAndExploitationOfResources.sts b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_DiscoveryAndExploitationOfResources.sts new file mode 100644 index 00000000..e7e584f0 --- /dev/null +++ b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_DiscoveryAndExploitationOfResources.sts @@ -0,0 +1,8 @@ +>import "File" +>import "Simantics/Misc" +>import "Simantics/Testing" +>import "Simantics/Sysdyn" +>m = importModel $ (getSystemProperty "scl.test.path") + "/../../../../../../../org.simantics.sysdyn.feature/rootFiles/sampleModels/Discovery And Exploitation Of Resources.sysdyn" +>exp = fromJust $ possibleResourceChild m "Experiment" +>id = activateExperiment exp +>run id diff --git a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_GlobalCarbonCycle.sts b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_GlobalCarbonCycle.sts new file mode 100644 index 00000000..d731bc91 --- /dev/null +++ b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_GlobalCarbonCycle.sts @@ -0,0 +1,10 @@ +>import "File" +>import "Simantics/Misc" +>import "Simantics/Testing" +>import "Simantics/Sysdyn" +>m = importModel $ (getSystemProperty "scl.test.path") + "/../../../../../../../org.simantics.sysdyn.feature/rootFiles/sampleModels/Global Carbon Cycle.sysdyn" +>exp = fromJust $ possibleResourceChild m "Experiment" +>id = activateExperiment exp +>run id +>withinEpsilon (lastValue exp id "/Carbon In Biosphere") 2710.604 0.001 +True \ No newline at end of file diff --git a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_PredatorAndLimitedPrey.sts b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_PredatorAndLimitedPrey.sts new file mode 100644 index 00000000..4f754f6e --- /dev/null +++ b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_PredatorAndLimitedPrey.sts @@ -0,0 +1,8 @@ +>import "File" +>import "Simantics/Misc" +>import "Simantics/Testing" +>import "Simantics/Sysdyn" +>m = importModel $ (getSystemProperty "scl.test.path") + "/../../../../../../../org.simantics.sysdyn.feature/rootFiles/sampleModels/Predator And Limited Prey.sysdyn" +>exp = fromJust $ possibleResourceChild m "Experiment" +>id = activateExperiment exp +>run id diff --git a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_ResourceExploitationUseRecycling.sts b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_ResourceExploitationUseRecycling.sts new file mode 100644 index 00000000..dca003ed --- /dev/null +++ b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_ResourceExploitationUseRecycling.sts @@ -0,0 +1,8 @@ +>import "File" +>import "Simantics/Misc" +>import "Simantics/Testing" +>import "Simantics/Sysdyn" +>m = importModel $ (getSystemProperty "scl.test.path") + "/../../../../../../../org.simantics.sysdyn.feature/rootFiles/sampleModels/Resource Exploitation Use Recycling.sysdyn" +>exp = fromJust $ possibleResourceChild m "Experiment" +>id = activateExperiment exp +>run id diff --git a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_TouristsEnvironmentsAndHotelFacilities.sts b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_TouristsEnvironmentsAndHotelFacilities.sts new file mode 100644 index 00000000..f77a497b --- /dev/null +++ b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts/ImportModel_TouristsEnvironmentsAndHotelFacilities.sts @@ -0,0 +1,8 @@ +>import "File" +>import "Simantics/Misc" +>import "Simantics/Testing" +>import "Simantics/Sysdyn" +>m = importModel $ (getSystemProperty "scl.test.path") + "/../../../../../../../org.simantics.sysdyn.feature/rootFiles/sampleModels/Tourists Environments And Hotel Facilities.sysdyn" +>exp = fromJust $ possibleResourceChild m "Experiment" +>id = activateExperiment exp +>run id 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 41b8aaa6..6756bb5e 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 @@ -49,6 +49,7 @@ import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.editor.SysdynEditorNamingService; import org.simantics.ui.workbench.IEditorNamingService; import org.simantics.utils.FileUtils; +import org.simantics.utils.ui.BundleUtils; public class SysdynProject extends AbstractProjectFeature { private static final String DEFAULT_PERSPECTIVE = "org.simantics.sysdyn.ui.perspective"; @@ -58,6 +59,17 @@ public class SysdynProject extends AbstractProjectFeature { final IProject project = getProject(); final Session session = getSession(); + try { + File base = BundleUtils.findFile("org.simantics.sysdyn.ui", ""); + File f = new File(base, "../org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/scripts"); + if(f.exists()) { + System.setProperty("scl.test.path", f.getAbsolutePath()); + } + } catch (Exception e) { + + } + + getProjectElement().setHint(ProjectKeys.DEFAULT_PERSPECTIVE, DEFAULT_PERSPECTIVE); // Multi for simupedia use diff --git a/org.simantics.sysdyn/scl/Sysdyn.scl b/org.simantics.sysdyn/scl/Sysdyn.scl index 21010c10..b32a845a 100644 --- a/org.simantics.sysdyn/scl/Sysdyn.scl +++ b/org.simantics.sysdyn/scl/Sysdyn.scl @@ -1,3 +1,4 @@ +include "UI/Progress" include "Simantics/DB" include "Simantics/Variables" @@ -84,8 +85,15 @@ importJava "org.simantics.sysdyn.utils.DocumentationUtils" where formatDate :: Long -> String @JavaName importModel - importModel :: String -> () + importModel :: String -> Resource @JavaName isParameter isParameter :: Variable -> Boolean + + lastValue :: Resource -> String -> String -> Double + +importJava "org.simantics.sysdyn.manager.SysdynExperiments" where + + activateExperiment :: Resource -> String + run :: String -> () \ No newline at end of file diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariable.java index 9834ff5d..71ea423e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/ValueIndexVariable.java @@ -24,10 +24,12 @@ public class ValueIndexVariable extends IndexVariable { super(graph, parent, indexes); } + public static double[] UNRESOLVED = new double[0]; + @Override public double[] getValue() { if(experiment == null) - return new double[0]; + return UNRESOLVED; Collection results = experiment.getActiveResults(); ArrayList variableNames = getVariableNamesWithIndexNumbers(); @@ -43,7 +45,7 @@ public class ValueIndexVariable extends IndexVariable { if(ds != null && ds.values != null && ds.values.length > 0) { result[i] = ds.values[ds.values.length-1]; } else { - result[i] = 0; + return UNRESOLVED; } } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java index 296c0ddd..b3e39609 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java @@ -6,21 +6,30 @@ import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; +import java.util.concurrent.TimeUnit; import org.eclipse.core.runtime.NullProgressMonitor; +import org.simantics.Simantics; import org.simantics.databoard.Bindings; import org.simantics.databoard.util.Base64; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.ObjectsWithSupertype; +import org.simantics.db.common.request.UniqueRead; +import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.ListUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.EvaluatingListener; +import org.simantics.db.layer0.util.EvaluatingListener.Criterion; +import org.simantics.db.layer0.util.EvaluatingListener.Evaluation; import org.simantics.db.layer0.variable.Variables; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; +import org.simantics.scl.runtime.function.FunctionImpl1; import org.simantics.simulation.ontology.SimulationResource; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.adapter.ValueIndexVariable; import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.manager.SysdynModelManager; import org.simantics.sysdyn.representation.Book; @@ -33,6 +42,7 @@ import org.simantics.sysdyn.representation.Stock; import org.simantics.sysdyn.representation.Variability; import org.simantics.sysdyn.representation.Variable; import org.simantics.sysdyn.utils.imports.ImportUtils; +import org.simantics.utils.DataContainer; public class DocumentationUtils { @@ -469,8 +479,18 @@ public class DocumentationUtils { * * @param path */ - public static void importModel(String path) { - ImportUtils.importModelFile(path, new NullProgressMonitor()); + public static Resource importModel(String path) { + final DataContainer m = new DataContainer(); + ImportUtils.importModelFile(path, new NullProgressMonitor(), new FunctionImpl1() { + + @Override + public WriteRequest apply(Resource model) { + m.set(model); + return null; + } + + }); + return m.get(); } public static boolean isParameter(ReadGraph graph, org.simantics.db.layer0.variable.Variable variable) throws DatabaseException { @@ -492,4 +512,43 @@ public class DocumentationUtils { Resource expression = expressions.get(0); return graph.isInstanceOf(expression, SR.ParameterExpression); } + + public static double lastValue(final Resource experiment, final String runId, final String path) throws DatabaseException { + + try { + double[] values = EvaluatingListener.trySyncRequest(Simantics.getSession(), + + new UniqueRead(){ + @SuppressWarnings("unchecked") + @Override + public double[] perform(ReadGraph graph) throws DatabaseException{ + + org.simantics.db.layer0.variable.Variable var = Variables.getVariable(graph, experiment); + if(var == null) return null; + org.simantics.db.layer0.variable.Variable run = var.getPossibleChild(graph, runId); + if(run == null) return null; + org.simantics.db.layer0.variable.Variable v = run.browsePossible(graph, path + "#value#"); + if(v == null) return null; + return v.getPossibleValue(graph); + + } + }, + new Criterion() { + + @Override + public Evaluation evaluate(double[] result) { + if(result == null) return Evaluation.DISCARD; + return (result != ValueIndexVariable.UNRESOLVED) ? Evaluation.ACCEPT : Evaluation.IGNORE; + } + + }, + 15, TimeUnit.SECONDS); + return values[0]; + } catch (InterruptedException e) { + e.printStackTrace(); + return Double.NaN; + } + + } + }