From: lempinen Date: Wed, 27 Apr 2011 09:47:03 +0000 (+0000) Subject: Support some basic Vensim functions X-Git-Tag: simantics-1.4RC1~37 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=a9fe2b5e2f385cb0556ed6fbb0f3758e8f685ac4;p=simantics%2Fsysdyn.git Support some basic Vensim functions git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20586 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index 8f0c57d8..2456f494 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/VensimFunctions.pgraph b/org.simantics.sysdyn.ontology/graph/VensimFunctions.pgraph new file mode 100644 index 00000000..18c19556 --- /dev/null +++ b/org.simantics.sysdyn.ontology/graph/VensimFunctions.pgraph @@ -0,0 +1,199 @@ + +L0 = +SYSDYN = + +VF = : SYSDYN.SysdynModelicaFunctionLibrary + +/* +VF.DELAYFIXED : SYSDYN.SysdynModelicaFunction + L0.HasDescription """DELAY FIXED( inputVar , dtime , init ) + -> delay(inputVar, dtime) + init -value is ignored.""" + SYSDYN.HasModelicaFunctionCode """ input Real inputVar; + input Real dtime; + input Real init; + input Real t = time; + output Real z; +protected + parameter Real parameterDTime = dtime; +algorithm + z := if time < parameterDTime then init else delay(inputVar, parameterDTime);""" +*/ + +VF.IFTHENELSE : SYSDYN.SysdynModelicaFunction + L0.HasDescription """IF THEN ELSE( cond , ontrue , onfalse ) + -> if cond then ontrue else onfalse""" + SYSDYN.HasModelicaFunctionCode """ input Boolean cond; + input Real ontrue; + input Real onfalse; + output Real z; +algorithm + z := if cond then ontrue else onfalse;""" + +VF.MAX : SYSDYN.SysdynModelicaFunction + L0.HasDescription """MAX(a, b) + Returns the larger of a and b.""" + SYSDYN.HasModelicaFunctionCode """ input Real a; + input Real b; + output Real z; +algorithm + z := if a > b then a else b;""" + +VF.MIN : SYSDYN.SysdynModelicaFunction + L0.HasDescription """MIN(a, b) + Returns the smaller of a and b.""" + SYSDYN.HasModelicaFunctionCode """ input Real a; + input Real b; + output Real z; +algorithm + z := if a < b then a else b;""" + +VF.XIDZ : SYSDYN.SysdynModelicaFunction + L0.HasDescription """XIDZ(a, b, x) + x if divided by zero, a/b otherwise.""" + SYSDYN.HasModelicaFunctionCode """ input Real a; + input Real b; + input Real x; + output Real z; +algorithm + z := xidz(a, b, x);""" + +VF.ZIDZ : SYSDYN.SysdynModelicaFunction + L0.HasDescription """XIDZ(a, b) + Zero if divided by zero, a/b otherwise.""" + SYSDYN.HasModelicaFunctionCode """ input Real a; + input Real b; + output Real z; +algorithm + z := zidz(a, b);""" + +VF.ABS : SYSDYN.SysdynModelicaFunction + L0.HasDescription """ABS(x) + Returns the absolute value of x.""" + SYSDYN.HasModelicaFunctionCode """ input Real x; + output Real z; +algorithm + z := abs(x);""" + +VF.SQRT : SYSDYN.SysdynModelicaFunction + L0.HasDescription """SQRT(x) + Returns the square root of x.""" + SYSDYN.HasModelicaFunctionCode """ input Real x; + output Real z; +algorithm + z := sqrt(x);""" + +VF.MODULO : SYSDYN.SysdynModelicaFunction + L0.HasDescription """MODULO(a, b) + Returns the remainder when a is divided by b. """ + SYSDYN.HasModelicaFunctionCode """ input Real a; + input Real b; + output Real z; +algorithm + z := mod(a, b);""" + +VF.PULSE : SYSDYN.SysdynModelicaFunction + L0.HasDescription """PULSE(start, width) + Returns 1.0 starting at time start and lasting for interval width. 0.0 is returned at other times.""" + SYSDYN.HasModelicaFunctionCode """ input Real start; + input Real width; + input Real t = time; + output Real z; +algorithm + z := if t >= start and (t - start) <= width then 1.0 else 0.0;""" + +VF.RAMP : SYSDYN.SysdynModelicaFunction + L0.HasDescription """RAMP(slope, startTime, endTime) + Returns 0 until the start time and then slopes upward until end time and then holds constant.""" + SYSDYN.HasModelicaFunctionCode """ input Real slope; + input Real startTime; + input Real endTime; + input Real t = time; + output Real z; +algorithm + z := + if t > startTime then + if t < endTime then + slope * (t - startTime) + else + slope * (endTime - startTime) + else + 0;""" + +VF.STEP : SYSDYN.SysdynModelicaFunction + L0.HasDescription """STEP(height, stepTime) + Returns 0.0 until the step time and then returns height.""" + SYSDYN.HasModelicaFunctionCode """ input Real height; + input Real stepTime; + input Real t = time; + output Real z; +algorithm + z := if t >= stepTime then height else 0.0;""" + +/* Continuous built-in common mathematical functions */ + +VF.EXP : SYSDYN.SysdynModelicaFunction + L0.HasDescription """ + """ + SYSDYN.HasModelicaFunctionCode """ input Real x; + output Real z; +algorithm + z := exp(x);""" + +VF.SIN : SYSDYN.SysdynModelicaFunction + L0.HasDescription """SIN(x) + Returns the sine of x.""" + SYSDYN.HasModelicaFunctionCode """ input Real x; + output Real z; +algorithm + z := sin(x);""" + +VF.SINH : SYSDYN.SysdynModelicaFunction + L0.HasDescription """SINH(x) + Returns the hyperbolic sine of x.""" + SYSDYN.HasModelicaFunctionCode """ input Real x; + output Real z; +algorithm + z := sinh(x);""" + +VF.COS : SYSDYN.SysdynModelicaFunction + L0.HasDescription """COS(x) + Returns the cosine of x.""" + SYSDYN.HasModelicaFunctionCode """ input Real x; + output Real z; +algorithm + z := cos(x);""" + +VF.COSH : SYSDYN.SysdynModelicaFunction + L0.HasDescription """COSH(x) + Returns the hyperbolic cosine of x.""" + SYSDYN.HasModelicaFunctionCode """ input Real x; + output Real z; +algorithm + z := cosh(x);""" + +VF.TAN : SYSDYN.SysdynModelicaFunction + L0.HasDescription """TAN(x) + Returns the tangent of x.""" + SYSDYN.HasModelicaFunctionCode """ input Real x; + output Real z; +algorithm + z := tan(x);""" + +VF.TANH : SYSDYN.SysdynModelicaFunction + L0.HasDescription """TANH(x) + Returns the hyperbolic tangent of x.""" + SYSDYN.HasModelicaFunctionCode """ input Real x; + output Real z; +algorithm + z := tanh(x);""" + +VF.LN : SYSDYN.SysdynModelicaFunction + L0.HasDescription """LN(x) + Returns the natural logarithm of x. + In modelica log(x) is the natural logarighm and log10(x) is base 10 logarighm.""" + SYSDYN.HasModelicaFunctionCode """ input Real x; + output Real z; +algorithm + z := log(x);""" + \ No newline at end of file diff --git a/org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java index 5a2dfc43..5c3e87a7 100644 --- a/org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java +++ b/org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java @@ -14,6 +14,26 @@ public class SysdynResource { public final Resource AuxiliarySymbol; public final Resource BasicExperiment; public final Resource Built$in_Functions; + public final Resource Built$in_Functions_Vensim_Functions; + public final Resource Built$in_Functions_Vensim_Functions_ABS; + public final Resource Built$in_Functions_Vensim_Functions_COS; + public final Resource Built$in_Functions_Vensim_Functions_COSH; + public final Resource Built$in_Functions_Vensim_Functions_EXP; + public final Resource Built$in_Functions_Vensim_Functions_IFTHENELSE; + public final Resource Built$in_Functions_Vensim_Functions_LN; + public final Resource Built$in_Functions_Vensim_Functions_MAX; + public final Resource Built$in_Functions_Vensim_Functions_MIN; + public final Resource Built$in_Functions_Vensim_Functions_MODULO; + public final Resource Built$in_Functions_Vensim_Functions_PULSE; + public final Resource Built$in_Functions_Vensim_Functions_RAMP; + public final Resource Built$in_Functions_Vensim_Functions_SIN; + public final Resource Built$in_Functions_Vensim_Functions_SINH; + public final Resource Built$in_Functions_Vensim_Functions_SQRT; + public final Resource Built$in_Functions_Vensim_Functions_STEP; + public final Resource Built$in_Functions_Vensim_Functions_TAN; + public final Resource Built$in_Functions_Vensim_Functions_TANH; + public final Resource Built$in_Functions_Vensim_Functions_XIDZ; + public final Resource Built$in_Functions_Vensim_Functions_ZIDZ; public final Resource Built$in_Functions_interpolate; public final Resource Built$in_Functions_interpolateFull; public final Resource Built$in_Functions_xidz; @@ -156,6 +176,26 @@ public class SysdynResource { public static final String AuxiliarySymbol = "http://www.simantics.org/Sysdyn-1.0/AuxiliarySymbol"; public static final String BasicExperiment = "http://www.simantics.org/Sysdyn-1.0/BasicExperiment"; public static final String Built$in_Functions = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions"; + public static final String Built$in_Functions_Vensim_Functions = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions"; + public static final String Built$in_Functions_Vensim_Functions_ABS = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/ABS"; + public static final String Built$in_Functions_Vensim_Functions_COS = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/COS"; + public static final String Built$in_Functions_Vensim_Functions_COSH = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/COSH"; + public static final String Built$in_Functions_Vensim_Functions_EXP = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/EXP"; + public static final String Built$in_Functions_Vensim_Functions_IFTHENELSE = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/IFTHENELSE"; + public static final String Built$in_Functions_Vensim_Functions_LN = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/LN"; + public static final String Built$in_Functions_Vensim_Functions_MAX = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/MAX"; + public static final String Built$in_Functions_Vensim_Functions_MIN = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/MIN"; + public static final String Built$in_Functions_Vensim_Functions_MODULO = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/MODULO"; + public static final String Built$in_Functions_Vensim_Functions_PULSE = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/PULSE"; + public static final String Built$in_Functions_Vensim_Functions_RAMP = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/RAMP"; + public static final String Built$in_Functions_Vensim_Functions_SIN = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/SIN"; + public static final String Built$in_Functions_Vensim_Functions_SINH = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/SINH"; + public static final String Built$in_Functions_Vensim_Functions_SQRT = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/SQRT"; + public static final String Built$in_Functions_Vensim_Functions_STEP = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/STEP"; + public static final String Built$in_Functions_Vensim_Functions_TAN = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/TAN"; + public static final String Built$in_Functions_Vensim_Functions_TANH = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/TANH"; + public static final String Built$in_Functions_Vensim_Functions_XIDZ = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/XIDZ"; + public static final String Built$in_Functions_Vensim_Functions_ZIDZ = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/Vensim+Functions/ZIDZ"; public static final String Built$in_Functions_interpolate = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/interpolate"; public static final String Built$in_Functions_interpolateFull = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/interpolateFull"; public static final String Built$in_Functions_xidz = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/xidz"; @@ -308,6 +348,26 @@ public class SysdynResource { AuxiliarySymbol = getResourceOrNull(graph, URIs.AuxiliarySymbol); BasicExperiment = getResourceOrNull(graph, URIs.BasicExperiment); Built$in_Functions = getResourceOrNull(graph, URIs.Built$in_Functions); + Built$in_Functions_Vensim_Functions = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions); + Built$in_Functions_Vensim_Functions_ABS = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_ABS); + Built$in_Functions_Vensim_Functions_COS = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_COS); + Built$in_Functions_Vensim_Functions_COSH = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_COSH); + Built$in_Functions_Vensim_Functions_EXP = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_EXP); + Built$in_Functions_Vensim_Functions_IFTHENELSE = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_IFTHENELSE); + Built$in_Functions_Vensim_Functions_LN = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_LN); + Built$in_Functions_Vensim_Functions_MAX = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_MAX); + Built$in_Functions_Vensim_Functions_MIN = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_MIN); + Built$in_Functions_Vensim_Functions_MODULO = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_MODULO); + Built$in_Functions_Vensim_Functions_PULSE = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_PULSE); + Built$in_Functions_Vensim_Functions_RAMP = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_RAMP); + Built$in_Functions_Vensim_Functions_SIN = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_SIN); + Built$in_Functions_Vensim_Functions_SINH = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_SINH); + Built$in_Functions_Vensim_Functions_SQRT = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_SQRT); + Built$in_Functions_Vensim_Functions_STEP = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_STEP); + Built$in_Functions_Vensim_Functions_TAN = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_TAN); + Built$in_Functions_Vensim_Functions_TANH = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_TANH); + Built$in_Functions_Vensim_Functions_XIDZ = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_XIDZ); + Built$in_Functions_Vensim_Functions_ZIDZ = getResourceOrNull(graph, URIs.Built$in_Functions_Vensim_Functions_ZIDZ); Built$in_Functions_interpolate = getResourceOrNull(graph, URIs.Built$in_Functions_interpolate); Built$in_Functions_interpolateFull = getResourceOrNull(graph, URIs.Built$in_Functions_interpolateFull); Built$in_Functions_xidz = getResourceOrNull(graph, URIs.Built$in_Functions_xidz); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/FunctionLibraries.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/FunctionLibraries.java index 6bebe335..cf4b0ce8 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/FunctionLibraries.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/FunctionLibraries.java @@ -13,8 +13,6 @@ package org.simantics.sysdyn.ui.browser.contributions; import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; -import java.util.Map; import org.simantics.browsing.ui.common.node.AbstractNode; import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; @@ -23,7 +21,6 @@ import org.simantics.db.Resource; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; -import org.simantics.scl.runtime.function.Function; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.browser.nodes.FunctionLibraryNode; import org.simantics.sysdyn.ui.browser.nodes.FunctionNode; @@ -32,7 +29,6 @@ import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionsFolder; public class FunctionLibraries extends ViewpointContributor { - @SuppressWarnings("unchecked") @Override public Collection getContribution(ReadGraph graph, FunctionsFolder functionsFolder) throws DatabaseException { @@ -47,30 +43,19 @@ public class FunctionLibraries extends ViewpointContributor { result.add(new FunctionNode(function)); } - // Find function libraries in model and sysdyn ontology - Function index = graph.adapt(L0.Dependencies, Function.class); - Collection> results = (Collection>)index.apply(graph, functionsFolder.data, "Types:SysdynModelicaFunctionLibrary"); - HashSet modelLibraries = new HashSet(); - for(Map r : results) { - Resource library = (Resource)r.get("Resource"); - if(library == null) - continue; - Resource root = graph.getPossibleObject(library, l0.PartOf); - while(!graph.isInstanceOf(root, sr.SysdynModel) && !graph.isInstanceOf(root, l0.Ontology)) { - library = root; - root = graph.getPossibleObject(library, l0.PartOf); + // Find model function libraries + for(Resource modelLibrary : graph.syncRequest(new ObjectsWithType(functionsFolder.data, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary))) { + new FunctionLibraryNode(modelLibrary); + } + + Resource sysdyn = graph.getPossibleResource("http://www.simantics.org/Sysdyn-1.0"); + if(sysdyn != null) { + for(Resource library : graph.syncRequest(new ObjectsWithType(sysdyn, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary))) { + result.add(new FunctionLibraryNode(library)); } - - // If the root is not a shared library (it is the model or sysdyn ontology) - if(graph.isInstanceOf(root, sr.SysdynModel)) - modelLibraries.add(library); } - for(Resource modelLibrary : modelLibraries) { - result.add(new FunctionLibraryNode(modelLibrary)); - } - result.add(new SharedFunctionsFolder(functionsFolder.data)); return result; 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 fd21b27a..72c9ba73 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 @@ -26,20 +26,15 @@ import org.simantics.db.Builtins; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; -import org.simantics.db.VirtualGraph; import org.simantics.db.WriteGraph; -import org.simantics.db.common.procedure.single.SingleSetSyncListener; -import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.Queries; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.adapter.GenericRelationIndex; import org.simantics.db.layer0.adapter.RuntimeValuations; import org.simantics.db.layer0.adapter.TrendVariable; import org.simantics.db.layer0.service.ActivationManager; -import org.simantics.db.layer0.util.Simantics; import org.simantics.db.request.Read; import org.simantics.db.service.GraphChangeListenerSupport; import org.simantics.db.service.LifecycleSupport; @@ -161,7 +156,6 @@ public class SysdynProject extends AbstractProjectFeature { if(g.isInstanceOf(dep, PROJ.NamespaceRequirement)) { for(Resource req : g.getObjects(dep, PROJ.RequiresNamespace)) { String uri = g.getPossibleValue(req, Bindings.STRING); - System.err.println("dep uri=" + uri); if(uri != null) { Resource target = g.getResource(uri); if(target != null) { @@ -232,7 +226,6 @@ public class SysdynProject extends AbstractProjectFeature { - final Layer0 l0 = Layer0.getInstance(session); ActivationManager activationManager = session.getService(ActivationManager.class); if (activationManager != null) { activationManager.activate(session, projectResource); @@ -240,46 +233,6 @@ public class SysdynProject extends AbstractProjectFeature { VirtualGraphSupport support = session.getService(VirtualGraphSupport.class); -// final VirtualGraph graph = session.getService(VirtualGraph.class); - -// SysdynResource SYSDYN = SysdynResource.getInstance(session); -// session.asyncRequest(new ObjectsWithType(projectResource, l0.ConsistsOf, SYSDYN.SysdynModel), new SingleSetSyncListener() { -// -// @Override -// public void add(ReadGraph g, final Resource model) throws DatabaseException { -// -// GenericRelationIndex index = g.adapt(l0.DependenciesRelation, GenericRelationIndex.class); -// index.trackAndIndex(g.getSession(), model); -// -// // FIXME: -// Simantics.async(new Runnable() { -// -// @Override -// public void run() { -// try { -// // This creates experiment realizations -// graph.register(new HistoryRealizationVirtualGraph(session, model)); -// // This creates the BaseRealization -// graph.register(new DefaultRealizationVirtualGraph(session, model)); -// } catch (DatabaseException e) { -// e.printStackTrace(); -// } -// } -// -// }); -// } -// -// @Override -// public void exception(ReadGraph graph, Throwable t) { -// t.printStackTrace(); -// } -// -// @Override -// public boolean isDisposed() { -// return false; -// } -// -// }); support.getWorkspacePersistent("experiments"); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java index 585601dd..ee40f1e8 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java @@ -37,7 +37,7 @@ public class FunctionUtils { SysdynResource sr = SysdynResource.getInstance(graph); SimulationResource simu = SimulationResource.getInstance(graph); - Set parents = getParents(graph, model); + Set parents = getParents(graph, model, true); for(Resource parent : parents) { if(graph.isInstanceOf(parent, sr.SysdynModel)) { Resource configuration = graph.getPossibleObject(parent, simu.HasConfiguration); @@ -59,7 +59,7 @@ public class FunctionUtils { return paths; } - private static Set getParents(ReadGraph graph, SysdynModel model) throws DatabaseException{ + private static Set getParents(ReadGraph graph, SysdynModel model, boolean onlyWithContent) throws DatabaseException{ HashSet parents = new HashSet(); Layer0 l0 = Layer0.getInstance(graph); @@ -69,18 +69,53 @@ public class FunctionUtils { parents.add(modelResource); - Collection sharedOntologies = graph.syncRequest(new ObjectsWithType( - modelResource, l0.IsLinkedTo, sr.SharedFunctionOntology)); - parents.addAll(sharedOntologies); + for(Resource r : graph.getObjects(modelResource, l0.IsLinkedTo)) { + if(graph.isInstanceOf(r, sr.SharedFunctionOntology)) { + parents.add(r); + } else { + Collection libraries = graph.syncRequest(new ObjectsWithType( + r, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary)); + if(!libraries.isEmpty()) + parents.add(r); + } + } + + if(onlyWithContent) { + HashSet contentParents = new HashSet(); + for(Resource parent : parents) { + if(hasContent(graph, parent)) + contentParents.add(parent); + } + parents = contentParents; + } return parents; } + private static boolean hasContent(ReadGraph graph, Resource library) { + Layer0 l0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + try { + if(!graph.syncRequest(new ObjectsWithType(library, l0.ConsistsOf, sr.SysdynModelicaFunction)).isEmpty()) + return true; + else { + for(Resource l : graph.syncRequest(new ObjectsWithType(library, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary))) { + boolean hasContent = hasContent(graph, l); + if(hasContent) + return true; + } + } + } catch (DatabaseException e) { + e.printStackTrace(); + } + return false; + } + public static void updateFunctionFilesForModel(ReadGraph graph, SysdynModel model) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - Set parents = getParents(graph, model); + Set parents = getParents(graph, model, false); for(Resource parent : parents) { if(graph.isInstanceOf(parent, sr.SysdynModel)) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java index 66f44e26..2f68f086 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/MdlParser.java @@ -30,6 +30,7 @@ import org.simantics.sysdyn.mdlImport.mdlElements.Element; import org.simantics.sysdyn.mdlImport.mdlElements.EquivalenceSubscript; import org.simantics.sysdyn.mdlImport.mdlElements.Expression; import org.simantics.sysdyn.mdlImport.mdlElements.Flow; +import org.simantics.sysdyn.mdlImport.mdlElements.Function; import org.simantics.sysdyn.mdlImport.mdlElements.Model; import org.simantics.sysdyn.mdlImport.mdlElements.Stock; import org.simantics.sysdyn.mdlImport.mdlElements.Subscript; @@ -299,7 +300,7 @@ public class MdlParser { name =(nameAndData[0].replace("\"", "")); variable = getVariable(model, name); if(variable == null) { - variable = new Auxiliary(); + variable = new Function(); variable.setName(name); model.addElement(variable); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Connection.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Connection.java index 06bde8e5..d6c64469 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Connection.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Connection.java @@ -36,11 +36,13 @@ public abstract class Connection implements IWriteableMDLObject { DiagramResource dr = DiagramResource.getInstance(graph); StructuralResource2 sr2 = StructuralResource2.getInstance(graph); - Resource diagram = graph.getSingleObject(configuration, mr.CompositeToDiagram); - - if(diagram == null) + Resource diagram = graph.getPossibleObject(configuration, mr.CompositeToDiagram); + Resource startElement = graph.getPossibleObject(start.getResource(), mr.ComponentToElement); + Resource endElement = graph.getPossibleObject(end.getResource(), mr.ComponentToElement); + if(diagram == null || startElement == null || endElement == null) return null; + if(connectionType == null) connectionType = sr.Dependency; @@ -57,12 +59,10 @@ public abstract class Connection implements IWriteableMDLObject { // Build diagram connectors and connection - Resource startElement = graph.getSingleObject(start.getResource(), mr.ComponentToElement); Resource tailConnector = GraphUtils.create2(graph, dr.Connector, sr.HasTailTerminal, startElement); - Resource endElement = graph.getSingleObject(end.getResource(), mr.ComponentToElement); Resource headConnector = GraphUtils.create2(graph, dr.Connector, sr.HasHeadTerminal, endElement, diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Function.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Function.java new file mode 100644 index 00000000..faadc02f --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Function.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * Copyright (c) 2007, 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.mdlImport.mdlElements; + +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.mdlImport.ImportUtils; + +public class Function extends Variable { + + @Override + public void write(WriteGraph graph, Resource parent, double xOffset, + double yOffset) { + if(parent == null || graph == null) + return; + + try { + SysdynResource sr = SysdynResource.getInstance(graph); + if(!graph.isInstanceOf(parent, sr.SysdynModel)) + return; + Layer0 l0 = Layer0.getInstance(graph); + + Resource function = GraphUtils.create2(graph, + sr.SysdynModelicaFunction, + l0.HasName, ImportUtils.escapeName(this.getName())); + + if(comments != null && comments.length() > 0) + graph.claimLiteral(function, l0.HasDescription, comments); + + if(expressions != null && expressions.get(0) != null) { + StringBuilder sb = new StringBuilder(); + sb.append(" input Real a;\n"); + sb.append(" output Real result;\n"); + sb.append("algorithm\n"); + sb.append(" result := interpolate(a, " + expressions.get(0).getExpression() + ");"); + graph.claimLiteral(function, sr.HasModelicaFunctionCode, sb.toString()); + } + + graph.claim(parent, l0.ConsistsOf, function); + resource = function; + } catch (DatabaseException e) { + e.printStackTrace(); + } + + } + + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Model.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Model.java index 011cb30b..98e00c95 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Model.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/Model.java @@ -36,6 +36,7 @@ public class Model implements IWriteableMDLObject { private HashMap elementMap = new HashMap(); private ArrayList subscripts = new ArrayList(); + private ArrayList functions = new ArrayList(); private ArrayList connections = new ArrayList(); private ArrayList views = new ArrayList(); private ArrayList unlocatedElements = new ArrayList(); @@ -43,6 +44,8 @@ public class Model implements IWriteableMDLObject { public void addElement(Element element) { if(element instanceof Subscript) addSubscript((Subscript)element); + else if(element instanceof Function) + addFunction((Function)element); else unlocatedElements.add(element); if(element.getName() != null) @@ -53,6 +56,10 @@ public class Model implements IWriteableMDLObject { subscripts.add(subscript); } + public void addFunction(Function function) { + functions.add(function); + } + public void addElement(View view, Element element) { if(element instanceof Subscript) addSubscript((Subscript)element); @@ -218,6 +225,10 @@ public class Model implements IWriteableMDLObject { // Diagram creation copied from SysdynProject. // Could the same code be reused? + + Resource sysdyn = graph.getResource("http://www.simantics.org/Sysdyn-1.0"); + Resource layer0 = graph.getResource("http://www.simantics.org/Layer0-1.0"); + Resource model = GraphUtils.create2( graph, sr.SysdynModel, @@ -225,7 +236,9 @@ public class Model implements IWriteableMDLObject { l0.HasName, getName(), l0.HasLabel, getName(), sr.HasStartTime, startTime, - sr.HasStopTime, endTime + sr.HasStopTime, endTime, + l0.IsLinkedTo, sysdyn, + l0.IsLinkedTo, layer0 ); Resource diagram = OrderedSetUtils.create(graph, sr.ConfigurationDiagram); @@ -241,6 +254,7 @@ public class Model implements IWriteableMDLObject { graph.claim(conf, mr.CompositeToDiagram, diagram); graph.claim(model, simu.HasConfiguration, conf); + graph.claim(model, l0.HasBaseRealization, conf); Resource mapping = graph.newResource(); graph.claim(mapping, l0.InstanceOf, null, sr.DiagramToCompositeMapping); @@ -290,6 +304,10 @@ public class Model implements IWriteableMDLObject { for(Connection c : connections) { c.write(graph, conf); } + + for(Function f : functions) { + f.write(graph, model, 0, 0); + } } catch (DatabaseException e) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/View.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/View.java index abf26b21..88fe41e6 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/View.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/mdlImport/mdlElements/View.java @@ -35,6 +35,10 @@ public class View implements IWriteableMDLObject { } public void addElement(Element e) { + if(e instanceof Subscript || + e instanceof Function) + return; + if(e.getX()maxX)