From ba2e05f5f8c8cf23d852d4a94e52bac27113a06f Mon Sep 17 00:00:00 2001 From: jkauttio Date: Fri, 5 Jun 2015 13:57:59 +0000 Subject: [PATCH] Implement basic model and variable access SCL functions refs #5895 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@31395 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn/scl/Simantics/Sysdyn.scl | 7 ++ .../org/simantics/sysdyn/utils/SCLUtils.java | 65 ++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/org.simantics.sysdyn/scl/Simantics/Sysdyn.scl b/org.simantics.sysdyn/scl/Simantics/Sysdyn.scl index f5bb87b9..b5e5e669 100644 --- a/org.simantics.sysdyn/scl/Simantics/Sysdyn.scl +++ b/org.simantics.sysdyn/scl/Simantics/Sysdyn.scl @@ -109,4 +109,11 @@ importJava "org.simantics.sysdyn.manager.SysdynExperiments" where deleteIC :: Variable -> String -> () setPublishResults :: Variable -> Boolean -> () + +importJava "org.simantics.sysdyn.utils.SCLUtils" where + + getModel :: String -> Resource + getModels :: () -> [Resource] + getVariable :: Resource -> String -> Resource + getVariables :: Resource -> [Resource] \ No newline at end of file diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/SCLUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/SCLUtils.java index ad84d27b..fc23e20c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/SCLUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/SCLUtils.java @@ -1,10 +1,73 @@ package org.simantics.sysdyn.utils; +import java.util.ArrayList; +import java.util.List; + +import org.simantics.Simantics; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.sysdyn.SysdynResource; public class SCLUtils { - public static Resource getModel(String name) { + public static Resource getModel(ReadGraph graph, String modelName) throws DatabaseException { + return getResourceWithName(graph, getModels(graph), modelName); + } + + public static List getModels(ReadGraph graph) throws DatabaseException { + return getChildrenWithType(graph, Simantics.getProjectResource(), SysdynResource.getInstance(graph).SysdynModel); + } + + public static Resource getVariable(ReadGraph graph, Resource model, String variableName) throws DatabaseException { + return getResourceWithName(graph, getVariables(graph, model), variableName); + } + + public static List getVariables(ReadGraph graph, Resource model) throws DatabaseException { + Resource configuration = graph.getSingleObject(model, SimulationResource.getInstance(graph).HasConfiguration); + // TODO: make sure IndependentVariable is the correct type to use here + return getChildrenWithType(graph, configuration, SysdynResource.getInstance(graph).IndependentVariable); + } + + public static String getExpression(ReadGraph graph, Resource variable) throws DatabaseException { + return null; + } + + public static void setExpression(WriteGraph graph, Resource variable, String equation) throws DatabaseException { + return; + } + + // TODO: there must be a (better) standard way to do these things! + + private static List getChildrenWithType(ReadGraph graph, Resource parent, Resource type) + throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + + List results = new ArrayList(); + + for (Resource child : graph.getObjects(parent, l0.ConsistsOf)) { + if (graph.isInstanceOf(child, type)) { + results.add(child); + } + } + + return results; + } + + private static Resource getResourceWithName(ReadGraph graph, List resources, String name) + throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + + for (Resource resource : resources) { + if (name.equals(graph.getPossibleRelatedValue(resource, l0.HasName, Bindings.STRING))) { + return resource; + } + } + return null; } -- 2.47.1