]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Function libraries started
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 15 Apr 2011 08:35:53 +0000 (08:35 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 15 Apr 2011 08:35:53 +0000 (08:35 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20506 ac1ea38d-2e2b-0410-8846-a27921b304fc

29 files changed:
org.simantics.sysdyn.ontology/graph.tg
org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph
org.simantics.sysdyn.ontology/graph/SysdynFunctions.pgraph
org.simantics.sysdyn.ontology/graph/WorkModel.pgraph
org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java
org.simantics.sysdyn.ui/icons/folder_link.png [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/actions/ShowInstantiatedModuleAction.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/FunctionLibraries.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/FunctionLibraryLabeler.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/LibraryFunctions.java [moved from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Functions.java with 51% similarity]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SysdynProject.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/FunctionLibraryNode.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/FunctionNode.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/FunctionsFolder.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SharedFunctionLibraryNode.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportFunctionLibrary.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportExternalFunctionFilesHandler.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportFunctionLibrary.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewFunctionHandler.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewFunctionLibraryHandler.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewSharedFunctionLibraryHandler.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/FunctionLibraryTab.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/externalFiles/ExternalFileNode.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/OldTransferableGraph1.java [new file with mode: 0644]
org.simantics.sysdyn/META-INF/MANIFEST.MF
org.simantics.sysdyn/adapters.xml
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/FunctionUtils.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java
org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModelManager.java

index 70f588911261395275b73f94a76cb7f2a65d53fe..8f0c57d871e5ab8e8b6a338b2f2fa075a137ea7b 100644 (file)
Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ
index 0161567ec77bc9c45a20d9511d59199963cf9209..f150ba4393d42fdcf92d8c5bdc8b019d0af898c1 100644 (file)
@@ -19,6 +19,9 @@ SYSDYN.ImportedOntologies : PROJ.NamespaceRequirement
     PROJ.RequiresNamespace
         "http://www.simantics.org/Sysdyn-1.0" : L0.URI
         "http://www.simantics.org/Layer0-1.0" : L0.URI
+        
+SYSDYN.SharedFunctionOntology <T L0.Ontology
+SYSDYN.SharedModuleOntolofgy <T L0.Ontology        
 
 //#####################################################################
 // Model
@@ -152,6 +155,12 @@ SYSDYN.ModuleSymbol <T DIA.FontProvider <T DIA.ColorProvider
 SYSDYN.IsOutput <R L0.IsRelatedTo : L0.Tag
     @L0.symmetric
 
+
+SYSDYN.SysdynModuleLibrary <T L0.Library  
+    @L0.singleProperty L0.HasName  
+    @L0.optionalProperty L0.HasDescription
+    
+    
 //#####################################################################
 // Connections and Relations
 //#####################################################################
@@ -316,7 +325,10 @@ SYSDYN.ExternalFunctionFile <T L0.Entity
 SYSDYN.HasExternalFile <R L0.HasProperty
     L0.HasRange L0.ByteArray
 
-    
+SYSDYN.SysdynModelicaFunctionLibrary <T L0.Library  
+    @L0.singleProperty L0.HasName  
+    @L0.optionalProperty L0.HasDescription
+        
 //#####################################################################
 // Experiments
 //#####################################################################
index 18a279fa9911057d937a2663dcc93c997cfdea4f..c21108818854c561a024d4061ab072647652c8f5 100644 (file)
@@ -1,7 +1,7 @@
 L0 = <http://www.simantics.org/Layer0-1.0>\r
 SYSDYN = <http://www.simantics.org/Sysdyn-1.0>\r
 \r
-FL = <http://www.simantics.org/Sysdyn-1.0/BasicFunctions> : L0.Library\r
+FL = <http://www.simantics.org/Sysdyn-1.0/Built-in Functions> : SYSDYN.SysdynModelicaFunctionLibrary\r
 \r
 \r
 FL.xidz : SYSDYN.SysdynModelicaFunction\r
index 4670d952a119b83d598c22154d94fe819f606352..7d715409bf542bf7446c93c67375ed8d3d014afe 100644 (file)
@@ -12,21 +12,13 @@ PROJ = <http://www.simantics.org/Project-1.0>
 //######################################################################\r
 \r
 /*\r
-WM = <http://www.simantics.org/Sysdyn-1.0/WorkModel> : PROJ.Project\r
-    PROJ.HasFeature _ : PROJ.FeatureSpec\r
-        PROJ.HasGroupId "org.simantics.sysdyn.feature.group"\r
-    L0.PartOf <http://Projects>\r
-*/\r
-\r
-WORKMODEL = <http://www.simantics.org/WorkModel> : L0.Ontology\r
+WM = <http://Projects/Development Project> : PROJ.Project\r
     @L0.new\r
+*/\r
     \r
-WORKMODEL.WM : PROJ.Project\r
-    PROJ.HasFeature _ : PROJ.FeatureSpec\r
-        PROJ.HasGroupId "org.simantics.sysdyn.feature.group"\r
-    L0.PartOf <http://Projects>\r
+WM = <http://Projects/Work Model Project> : PROJ.Project\r
+    @L0.new\r
 \r
-WM = WORKMODEL.WM\r
 \r
 TAGS = WM.Tags : L0.Library\r
    \r
@@ -96,6 +88,9 @@ WM.WorkModel : SYSDYN.SysdynModel
     SIMU.HasConfiguration WC\r
     SYSDYN.HasStartTime 0.0 \r
     SYSDYN.HasStopTime 24.0 \r
+    L0.IsLinkedTo SYSDYN\r
+    L0.IsLinkedTo L0\r
+    \r
 WM.WorkModel.Experiment : SYSDYN.Experiment\r
     L0.HasLabel "Experiment" \r
 \r
index ad496fe540bcf5ab5b3bdacd8f4b7801b16c813c..5a2dfc432e5049b7e46f4fda8bae98c7a43feffa 100644 (file)
@@ -13,11 +13,11 @@ public class SysdynResource {
     public final Resource Auxiliary;\r
     public final Resource AuxiliarySymbol;\r
     public final Resource BasicExperiment;\r
-    public final Resource BasicFunctions;\r
-    public final Resource BasicFunctions_interpolate;\r
-    public final Resource BasicFunctions_interpolateFull;\r
-    public final Resource BasicFunctions_xidz;\r
-    public final Resource BasicFunctions_zidz;\r
+    public final Resource Built$in_Functions;\r
+    public final Resource Built$in_Functions_interpolate;\r
+    public final Resource Built$in_Functions_interpolateFull;\r
+    public final Resource Built$in_Functions_xidz;\r
+    public final Resource Built$in_Functions_zidz;\r
     public final Resource Cloud;\r
     public final Resource CloudSymbol;\r
     public final Resource Configuration;\r
@@ -124,6 +124,8 @@ public class SysdynResource {
     public final Resource ReplacingEnumeration;\r
     public final Resource ReplacingEnumeration_Inverse;\r
     public final Resource Result;\r
+    public final Resource SharedFunctionOntology;\r
+    public final Resource SharedModuleOntolofgy;\r
     public final Resource ShowEnumerationIndexInCharts;\r
     public final Resource ShowEnumerationIndexInCharts_Inverse;\r
     public final Resource SimulateOnChangeExperiment;\r
@@ -137,6 +139,8 @@ public class SysdynResource {
     public final Resource SysdynDiagramModelingRules;\r
     public final Resource SysdynModel;\r
     public final Resource SysdynModelicaFunction;\r
+    public final Resource SysdynModelicaFunctionLibrary;\r
+    public final Resource SysdynModuleLibrary;\r
     public final Resource SysdynTerminal;\r
     public final Resource Valve;\r
     public final Resource ValveSymbol;\r
@@ -151,11 +155,11 @@ public class SysdynResource {
         public static final String Auxiliary = "http://www.simantics.org/Sysdyn-1.0/Auxiliary";\r
         public static final String AuxiliarySymbol = "http://www.simantics.org/Sysdyn-1.0/AuxiliarySymbol";\r
         public static final String BasicExperiment = "http://www.simantics.org/Sysdyn-1.0/BasicExperiment";\r
-        public static final String BasicFunctions = "http://www.simantics.org/Sysdyn-1.0/BasicFunctions";\r
-        public static final String BasicFunctions_interpolate = "http://www.simantics.org/Sysdyn-1.0/BasicFunctions/interpolate";\r
-        public static final String BasicFunctions_interpolateFull = "http://www.simantics.org/Sysdyn-1.0/BasicFunctions/interpolateFull";\r
-        public static final String BasicFunctions_xidz = "http://www.simantics.org/Sysdyn-1.0/BasicFunctions/xidz";\r
-        public static final String BasicFunctions_zidz = "http://www.simantics.org/Sysdyn-1.0/BasicFunctions/zidz";\r
+        public static final String Built$in_Functions = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions";\r
+        public static final String Built$in_Functions_interpolate = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/interpolate";\r
+        public static final String Built$in_Functions_interpolateFull = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/interpolateFull";\r
+        public static final String Built$in_Functions_xidz = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/xidz";\r
+        public static final String Built$in_Functions_zidz = "http://www.simantics.org/Sysdyn-1.0/Built-in+Functions/zidz";\r
         public static final String Cloud = "http://www.simantics.org/Sysdyn-1.0/Cloud";\r
         public static final String CloudSymbol = "http://www.simantics.org/Sysdyn-1.0/CloudSymbol";\r
         public static final String Configuration = "http://www.simantics.org/Sysdyn-1.0/Configuration";\r
@@ -262,6 +266,8 @@ public class SysdynResource {
         public static final String ReplacingEnumeration = "http://www.simantics.org/Sysdyn-1.0/ReplacingEnumeration";\r
         public static final String ReplacingEnumeration_Inverse = "http://www.simantics.org/Sysdyn-1.0/ReplacingEnumeration/Inverse";\r
         public static final String Result = "http://www.simantics.org/Sysdyn-1.0/Result";\r
+        public static final String SharedFunctionOntology = "http://www.simantics.org/Sysdyn-1.0/SharedFunctionOntology";\r
+        public static final String SharedModuleOntolofgy = "http://www.simantics.org/Sysdyn-1.0/SharedModuleOntolofgy";\r
         public static final String ShowEnumerationIndexInCharts = "http://www.simantics.org/Sysdyn-1.0/ShowEnumerationIndexInCharts";\r
         public static final String ShowEnumerationIndexInCharts_Inverse = "http://www.simantics.org/Sysdyn-1.0/ShowEnumerationIndexInCharts/Inverse";\r
         public static final String SimulateOnChangeExperiment = "http://www.simantics.org/Sysdyn-1.0/SimulateOnChangeExperiment";\r
@@ -275,6 +281,8 @@ public class SysdynResource {
         public static final String SysdynDiagramModelingRules = "http://www.simantics.org/Sysdyn-1.0/SysdynDiagramModelingRules";\r
         public static final String SysdynModel = "http://www.simantics.org/Sysdyn-1.0/SysdynModel";\r
         public static final String SysdynModelicaFunction = "http://www.simantics.org/Sysdyn-1.0/SysdynModelicaFunction";\r
+        public static final String SysdynModelicaFunctionLibrary = "http://www.simantics.org/Sysdyn-1.0/SysdynModelicaFunctionLibrary";\r
+        public static final String SysdynModuleLibrary = "http://www.simantics.org/Sysdyn-1.0/SysdynModuleLibrary";\r
         public static final String SysdynTerminal = "http://www.simantics.org/Sysdyn-1.0/SysdynTerminal";\r
         public static final String Valve = "http://www.simantics.org/Sysdyn-1.0/Valve";\r
         public static final String ValveSymbol = "http://www.simantics.org/Sysdyn-1.0/ValveSymbol";\r
@@ -299,11 +307,11 @@ public class SysdynResource {
         Auxiliary = getResourceOrNull(graph, URIs.Auxiliary);\r
         AuxiliarySymbol = getResourceOrNull(graph, URIs.AuxiliarySymbol);\r
         BasicExperiment = getResourceOrNull(graph, URIs.BasicExperiment);\r
-        BasicFunctions = getResourceOrNull(graph, URIs.BasicFunctions);\r
-        BasicFunctions_interpolate = getResourceOrNull(graph, URIs.BasicFunctions_interpolate);\r
-        BasicFunctions_interpolateFull = getResourceOrNull(graph, URIs.BasicFunctions_interpolateFull);\r
-        BasicFunctions_xidz = getResourceOrNull(graph, URIs.BasicFunctions_xidz);\r
-        BasicFunctions_zidz = getResourceOrNull(graph, URIs.BasicFunctions_zidz);\r
+        Built$in_Functions = getResourceOrNull(graph, URIs.Built$in_Functions);\r
+        Built$in_Functions_interpolate = getResourceOrNull(graph, URIs.Built$in_Functions_interpolate);\r
+        Built$in_Functions_interpolateFull = getResourceOrNull(graph, URIs.Built$in_Functions_interpolateFull);\r
+        Built$in_Functions_xidz = getResourceOrNull(graph, URIs.Built$in_Functions_xidz);\r
+        Built$in_Functions_zidz = getResourceOrNull(graph, URIs.Built$in_Functions_zidz);\r
         Cloud = getResourceOrNull(graph, URIs.Cloud);\r
         CloudSymbol = getResourceOrNull(graph, URIs.CloudSymbol);\r
         Configuration = getResourceOrNull(graph, URIs.Configuration);\r
@@ -410,6 +418,8 @@ public class SysdynResource {
         ReplacingEnumeration = getResourceOrNull(graph, URIs.ReplacingEnumeration);\r
         ReplacingEnumeration_Inverse = getResourceOrNull(graph, URIs.ReplacingEnumeration_Inverse);\r
         Result = getResourceOrNull(graph, URIs.Result);\r
+        SharedFunctionOntology = getResourceOrNull(graph, URIs.SharedFunctionOntology);\r
+        SharedModuleOntolofgy = getResourceOrNull(graph, URIs.SharedModuleOntolofgy);\r
         ShowEnumerationIndexInCharts = getResourceOrNull(graph, URIs.ShowEnumerationIndexInCharts);\r
         ShowEnumerationIndexInCharts_Inverse = getResourceOrNull(graph, URIs.ShowEnumerationIndexInCharts_Inverse);\r
         SimulateOnChangeExperiment = getResourceOrNull(graph, URIs.SimulateOnChangeExperiment);\r
@@ -423,6 +433,8 @@ public class SysdynResource {
         SysdynDiagramModelingRules = getResourceOrNull(graph, URIs.SysdynDiagramModelingRules);\r
         SysdynModel = getResourceOrNull(graph, URIs.SysdynModel);\r
         SysdynModelicaFunction = getResourceOrNull(graph, URIs.SysdynModelicaFunction);\r
+        SysdynModelicaFunctionLibrary = getResourceOrNull(graph, URIs.SysdynModelicaFunctionLibrary);\r
+        SysdynModuleLibrary = getResourceOrNull(graph, URIs.SysdynModuleLibrary);\r
         SysdynTerminal = getResourceOrNull(graph, URIs.SysdynTerminal);\r
         Valve = getResourceOrNull(graph, URIs.Valve);\r
         ValveSymbol = getResourceOrNull(graph, URIs.ValveSymbol);\r
diff --git a/org.simantics.sysdyn.ui/icons/folder_link.png b/org.simantics.sysdyn.ui/icons/folder_link.png
new file mode 100644 (file)
index 0000000..b9b75f6
Binary files /dev/null and b/org.simantics.sysdyn.ui/icons/folder_link.png differ
index d28452ba9b40bbf106c2272604739cf81c281d0d..e66d38d741d1920a4924a4c9e3759b37e2c8a409 100644 (file)
@@ -18,10 +18,13 @@ import org.eclipse.ui.PartInitException;
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.modeling.ModelingResources;\r
 import org.simantics.structural.stubs.StructuralResource2;\r
 import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.adapter.ChildVariable;\r
 import org.simantics.sysdyn.ui.browser.BrowserSelection;\r
 import org.simantics.sysdyn.ui.browser.nodes.ModuleNode;\r
 import org.simantics.ui.DoubleClickEvent;\r
@@ -46,23 +49,39 @@ public class ShowInstantiatedModuleAction implements IDoubleClickAction {
                        if(!bss.isEmpty()) {\r
                                Layer0 l0 = Layer0.getInstance(g);\r
                                for(BrowserSelection bs : bss) {\r
-                                       Resource instanceOf = g.getSingleObject((Resource)bs.getAdapter(Resource.class), l0.InstanceOf);\r
+                                       Resource configuration = (Resource)bs.getAdapter(Resource.class);\r
+                                       Resource instanceOf = g.getSingleObject(configuration, l0.InstanceOf);\r
+                                       \r
                                        SysdynResource sr = SysdynResource.getInstance(g);\r
+                                       StructuralResource2 sr2 = StructuralResource2.getInstance(g);\r
                                        if(g.isInheritedFrom(instanceOf, sr.Module)) {\r
-                                               StructuralResource2 sr2 = StructuralResource2.getInstance(g);\r
-                                               Resource conf = g.getSingleObject(instanceOf, sr2.IsDefinedBy);\r
-                                               addShowModuleAction(e, getResourceEditorInput(g, bs.getModel(), conf, bs.getURI()));\r
-                                       }\r
+                                               configuration = g.getSingleObject(instanceOf, sr2.IsDefinedBy);\r
+                                               ChildVariable variable = (ChildVariable) bs.getAdapter(Variable.class);\r
+                                               String rvi = variable.getRvi(g);\r
+                                               Resource model = Variables.getModel(g, variable);\r
+                                               addShowModuleAction(e, getResourceEditorInput(g, model, configuration, rvi));\r
+                                       } \r
                                }\r
                        } else {\r
                                ModuleNode moduleNode = ISelectionUtils.filterSingleSelection(e.getResource(), ModuleNode.class);\r
                                if(moduleNode != null) {\r
                                        Layer0 l0 = Layer0.getInstance(g);\r
                                        StructuralResource2 sr2 = StructuralResource2.getInstance(g);\r
-                               String rvi = moduleNode.getURI(g);\r
-                               Resource instanceOf = g.getSingleObject(moduleNode.getResource() != null ? moduleNode.getResource() : moduleNode.data, l0.InstanceOf);\r
+                                       Resource model;\r
+                                       String rvi;\r
+                                       if(moduleNode.getVariable() != null) {\r
+                                               model = Variables.getModel(g, moduleNode.getVariable());\r
+                                               ChildVariable var = (ChildVariable)moduleNode.getVariable();\r
+                                               rvi = var.getRvi(g);\r
+                                       } else {\r
+                                               rvi = (String)g.getRelatedValue(moduleNode.data, Layer0.getInstance(g).HasName);\r
+                                               model = g.getSingleObject(moduleNode.data, l0.PartOf);\r
+                                       }\r
+                                       \r
+                               Resource instanceOf = g.getSingleObject(moduleNode.data, l0.InstanceOf);\r
                                        Resource conf = g.getSingleObject(instanceOf, sr2.IsDefinedBy);\r
-                                       addShowModuleAction(e, getResourceEditorInput(g, moduleNode.getModel(), conf, rvi));\r
+                                   \r
+                                       addShowModuleAction(e, getResourceEditorInput(g, model, conf, rvi));\r
                        }\r
                        }\r
         } \r
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
new file mode 100644 (file)
index 0000000..a0a250c
--- /dev/null
@@ -0,0 +1,104 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.browser.contributions;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.HashSet;\r
+import java.util.Map;\r
+\r
+import org.simantics.browsing.ui.common.node.AbstractNode;\r
+import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.scl.runtime.function.Function;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.browser.nodes.FunctionLibraryNode;\r
+import org.simantics.sysdyn.ui.browser.nodes.FunctionNode;\r
+import org.simantics.sysdyn.ui.browser.nodes.FunctionsFolder;\r
+import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionLibraryNode;\r
+\r
+public class FunctionLibraries  extends ViewpointContributor<FunctionsFolder> {\r
+\r
+    @SuppressWarnings("unchecked")\r
+       @Override\r
+    public Collection<?> getContribution(ReadGraph graph, FunctionsFolder functionsFolder)\r
+    throws DatabaseException {\r
+\r
+       Layer0 l0 = Layer0.getInstance(graph);\r
+       SysdynResource sr = SysdynResource.getInstance(graph);\r
+       \r
+        ArrayList<AbstractNode<Resource>> result = new ArrayList<AbstractNode<Resource>>();\r
+        \r
+        // Find model functions\r
+        for(Resource function : graph.syncRequest(new ObjectsWithType(functionsFolder.data, l0.ConsistsOf, sr.SysdynModelicaFunction))) {\r
+               result.add(new FunctionNode(function));\r
+        }\r
+        \r
+        // Find shared libraries\r
+        Function index = graph.adapt(L0.Dependencies, Function.class);\r
+        Collection<Map<String, Object>> results = (Collection<Map<String, Object>>)index.apply(graph, functionsFolder.data, "Types:SharedFunctionOntology");\r
+        HashSet<Resource> sharedLibraries = new HashSet<Resource>();\r
+        for(Map<String, Object> r : results) {\r
+               Resource sharedLibrary = (Resource)r.get("Resource");\r
+               if(sharedLibrary == null)\r
+                       continue;\r
+               sharedLibraries.add(sharedLibrary);\r
+        }\r
+        \r
+        // Find function libraries in model and sysdyn ontology\r
+        index = graph.adapt(L0.Dependencies, Function.class);\r
+        results = (Collection<Map<String, Object>>)index.apply(graph, functionsFolder.data, "Types:SysdynModelicaFunctionLibrary");\r
+        HashSet<Resource> modelLibraries = new HashSet<Resource>();\r
+        for(Map<String, Object> r : results) {\r
+               Resource library = (Resource)r.get("Resource");\r
+               if(library == null)\r
+                       continue;\r
+               Resource root = graph.getPossibleObject(library, l0.PartOf);\r
+               while(!graph.isInstanceOf(root, sr.SysdynModel) && !graph.isInstanceOf(root, l0.Ontology)) {\r
+                       library = root;\r
+                       root = graph.getPossibleObject(library, l0.PartOf);\r
+               }\r
+               \r
+               // If the root is not a shared library (it is the model or sysdyn ontology)\r
+               if(!sharedLibraries.contains(root))\r
+                       modelLibraries.add(library);\r
+        }\r
+        \r
+        \r
+        for(Resource modelLibrary : modelLibraries) {\r
+               Resource partOf = graph.getPossibleObject(modelLibrary, l0.PartOf);\r
+               if(graph.isInstanceOf(partOf, sr.SysdynModel))\r
+                       // From the model\r
+                       result.add(new FunctionLibraryNode<Resource>(modelLibrary));\r
+               else\r
+                       // From sysdyn ontology, this is read only\r
+                       result.add(new FunctionLibraryNode<Resource>(modelLibrary));\r
+        }\r
+        \r
+        for(Resource sharedLibrary : sharedLibraries)\r
+               result.add(new SharedFunctionLibraryNode(sharedLibrary));\r
+        \r
+        return result;\r
+\r
+    }\r
+\r
+    @Override\r
+    public String getViewpointId() {\r
+        return "Standard";\r
+    }\r
+    \r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/FunctionLibraryLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/FunctionLibraryLabeler.java
new file mode 100644 (file)
index 0000000..1ac513f
--- /dev/null
@@ -0,0 +1,20 @@
+package org.simantics.sysdyn.ui.browser.contributions;\r
+\r
+import org.simantics.browsing.ui.graph.contributor.labeler.LabelerContributor;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.sysdyn.ui.browser.nodes.FunctionLibraryNode;\r
+\r
+public class FunctionLibraryLabeler extends LabelerContributor<FunctionLibraryNode<Resource>>{\r
+\r
+       @Override\r
+       public String getLabel(ReadGraph graph, FunctionLibraryNode<Resource> input)\r
+                       throws DatabaseException {\r
+               String name = NameUtils.getSafeName(graph, input.data);\r
+               return name;\r
+       }\r
+\r
+\r
+}\r
similarity index 51%
rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Functions.java
rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/LibraryFunctions.java
index 344f6af3b3756dcdcfde76f37df6fe4c191fb3b9..c37ea4657f8e81f0e69a738141ebe92503f76d29 100644 (file)
@@ -13,32 +13,32 @@ package org.simantics.sysdyn.ui.browser.contributions;
 \r
 import java.util.ArrayList;\r
 import java.util.Collection;\r
-import java.util.Map;\r
 \r
 import org.simantics.browsing.ui.common.node.AbstractNode;\r
 import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
 import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.scl.runtime.function.Function;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.ui.browser.nodes.FunctionLibraryNode;\r
 import org.simantics.sysdyn.ui.browser.nodes.FunctionNode;\r
-import org.simantics.sysdyn.ui.browser.nodes.FunctionsFolder;\r
 \r
-public class Functions  extends ViewpointContributor<FunctionsFolder> {\r
-\r
-    @Override\r
-    public Collection<?> getContribution(ReadGraph graph, FunctionsFolder functionsFolder)\r
-    throws DatabaseException {\r
-\r
-        ArrayList<AbstractNode<Resource>> result = new ArrayList<AbstractNode<Resource>>();\r
-        Function index = graph.adapt(L0.Dependencies, Function.class);\r
-        Collection<Map<String, Object>> results = (Collection<Map<String, Object>>)index.apply(graph, functionsFolder.data, "Types:SysdynModelicaFunction");\r
-        for(Map<String, Object> r : results) {\r
-               Resource function = (Resource)r.get("Resource");\r
-               if(function != null);\r
-                       result.add(new FunctionNode(function));\r
-        }\r
-        return result;\r
+public class LibraryFunctions extends ViewpointContributor<FunctionLibraryNode<Resource>> {\r
+\r
+       @Override\r
+       public Collection<?> getContribution(ReadGraph graph,\r
+                       FunctionLibraryNode<Resource> library) throws DatabaseException {\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               ArrayList<AbstractNode<Resource>> result = new ArrayList<AbstractNode<Resource>>();\r
+               for(Resource function : graph.syncRequest(new ObjectsWithType(library.data, l0.ConsistsOf, SysdynResource.getInstance(graph).SysdynModelicaFunction))) {\r
+                       result.add(new FunctionNode(function));\r
+               }\r
+               for(Resource functionLibrary : graph.syncRequest(new ObjectsWithType(library.data, l0.ConsistsOf, SysdynResource.getInstance(graph).SysdynModelicaFunctionLibrary))) {\r
+                       result.add(new FunctionLibraryNode<Resource>(functionLibrary));\r
+               }\r
+               return result;\r
 \r
     }\r
 \r
@@ -46,6 +46,4 @@ public class Functions  extends ViewpointContributor<FunctionsFolder> {
     public String getViewpointId() {\r
         return "Standard";\r
     }\r
-    \r
-\r
 }\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SysdynProject.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SysdynProject.java
new file mode 100644 (file)
index 0000000..91c46b6
--- /dev/null
@@ -0,0 +1,39 @@
+package org.simantics.sysdyn.ui.browser.contributions;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.ObjectsWithType;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.structural.ui.modelBrowser.nodes.AbstractNode;\r
+import org.simantics.sysdyn.SysdynResource;\r
+\r
+public class SysdynProject  extends ViewpointContributor<Resource> {\r
+\r
+    @Override\r
+    public Collection<?> getContribution(ReadGraph graph, Resource project)\r
+    throws DatabaseException {\r
+\r
+        ArrayList<AbstractNode> result = new ArrayList<AbstractNode>();\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        for(Resource r : graph.syncRequest(new ObjectsWithType(project, L0.ConsistsOf, SysdynResource.getInstance(graph).SysdynModel))) {\r
+            try {\r
+                result.add(graph.adapt(r, AbstractNode.class));\r
+            } catch(DatabaseException e) {\r
+                e.printStackTrace();\r
+            }\r
+        }\r
+        return result;\r
+\r
+    }\r
+\r
+    @Override\r
+    public String getViewpointId() {\r
+        return "Standard";\r
+    }\r
+\r
+}
\ No newline at end of file
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/FunctionLibraryNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/FunctionLibraryNode.java
new file mode 100644 (file)
index 0000000..eddb126
--- /dev/null
@@ -0,0 +1,97 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.browser.nodes;\r
+\r
+import org.simantics.browsing.ui.common.node.AbstractNode;\r
+import org.simantics.browsing.ui.common.node.DeleteException;\r
+import org.simantics.browsing.ui.common.node.IDeletableNode;\r
+import org.simantics.browsing.ui.common.node.IDropTargetNode;\r
+import org.simantics.browsing.ui.common.node.IModifiableNode;\r
+import org.simantics.browsing.ui.content.Labeler.Modifier;\r
+import org.simantics.browsing.ui.graph.impl.LabelModifier;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.FunctionUtils;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+\r
+public class FunctionLibraryNode<T> extends AbstractNode<Resource> implements IDeletableNode, IModifiableNode, IDropTargetNode {\r
+\r
+       \r
+    public FunctionLibraryNode(Resource resource) {\r
+        super(resource);\r
+    }\r
+\r
+       @Override\r
+       public Modifier getModifier(String columnId) {\r
+               try {\r
+                       Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
+                       LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
+                               @Override\r
+                               public String isValid(String label) {\r
+                                       if (label.isEmpty())\r
+                                               return "Empty label not allowed";\r
+                                       if (label.contains(" "))\r
+                                               return "Spaces are not allowed";\r
+                                       return null;\r
+                               }\r
+                       };\r
+                       return modifier;\r
+               } catch (DatabaseException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               return null;\r
+       }\r
+\r
+       @Override\r
+       public void delete() throws DeleteException {\r
+               SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+\r
+                       @Override\r
+                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                               Layer0 l0 = Layer0.getInstance(graph);\r
+                               graph.deny(data, l0.PartOf);\r
+                       }\r
+               });\r
+       }\r
+\r
+       @Override\r
+       public void drop(Object data) {\r
+               final Resource[] resources = ResourceAdaptionUtils.toResources(data);\r
+               final Resource library = this.data;\r
+               if(resources.length > 0) {\r
+                       SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+                               \r
+                               @Override\r
+                               public void perform(WriteGraph graph) throws DatabaseException {\r
+                                       SysdynResource sr = SysdynResource.getInstance(graph);\r
+                                       Layer0 l0 = Layer0.getInstance(graph);\r
+                                       for(Resource tobeMoved : resources) {\r
+                                               if(graph.isInstanceOf(tobeMoved, sr.SysdynModelicaFunction) ||\r
+                                                               graph.isInstanceOf(tobeMoved, sr.SysdynModelicaFunctionLibrary)) {\r
+                                                       Resource oldLib = graph.getSingleObject(tobeMoved, l0.PartOf);\r
+                                                       graph.deny(tobeMoved, l0.PartOf);\r
+                                                       graph.claim(tobeMoved, l0.PartOf, library);\r
+                                                       FunctionUtils.updateFunctionFileForLibrary(graph, oldLib);\r
+                                                       FunctionUtils.updateFunctionFileForLibrary(graph, library);\r
+                                               }\r
+                                       }\r
+                               }\r
+                       });\r
+               }\r
+       }\r
+       \r
+}\r
index 31589a4c91c0acbf04425e808ca7bf6128213548..2f2baa239147259da7c759ebb92dc1f29e5fda02 100644 (file)
  *******************************************************************************/\r
 package org.simantics.sysdyn.ui.browser.nodes;\r
 \r
-import org.simantics.browsing.ui.common.node.IDeletable;\r
+import org.simantics.browsing.ui.common.node.DeleteException;\r
+import org.simantics.browsing.ui.common.node.IDeletableNode;\r
+import org.simantics.browsing.ui.common.node.IDropTargetNode;\r
+import org.simantics.browsing.ui.common.node.IModifiableNode;\r
+import org.simantics.browsing.ui.content.Labeler.Modifier;\r
+import org.simantics.browsing.ui.graph.impl.LabelModifier;\r
 import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.FunctionUtils;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
 \r
-public class FunctionNode extends VariableNode<Resource> implements IDeletable {\r
+public class FunctionNode extends VariableNode<Resource> implements IDeletableNode, IModifiableNode, IDropTargetNode {\r
        \r
     public FunctionNode(final Resource resource) {\r
        super(resource);\r
     }\r
+    \r
+       @Override\r
+       public Modifier getModifier(String columnId) {\r
+               try {\r
+                       Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
+                       LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
+                               @Override\r
+                               public String isValid(String label) {\r
+                                       if (label.isEmpty())\r
+                                               return "Empty label not allowed";\r
+                                       if (label.contains(" "))\r
+                                               return "Spaces are not allowed";\r
+                                       return null;\r
+                               }\r
+                       };\r
+                       return modifier;\r
+               } catch (DatabaseException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               return null;\r
+       }\r
 \r
+       @Override\r
+       public void delete() throws DeleteException {\r
+               SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+\r
+                       @Override\r
+                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                               Layer0 l0 = Layer0.getInstance(graph);\r
+                               Resource partOf = graph.getSingleObject(data, l0.PartOf);\r
+                               if(partOf != null) {\r
+                                       graph.deny(data, l0.PartOf, partOf);\r
+                               }\r
+                       }\r
+               });\r
+       }\r
+       \r
+       @Override\r
+       public void drop(Object data) {\r
+               final Resource[] resources = ResourceAdaptionUtils.toResources(data);\r
+               final Resource thisFunction = this.data;\r
+               \r
+               if(resources.length > 0) {\r
+                       SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+                               \r
+                               @Override\r
+                               public void perform(WriteGraph graph) throws DatabaseException {\r
+                                       SysdynResource sr = SysdynResource.getInstance(graph);\r
+                                       Layer0 l0 = Layer0.getInstance(graph);\r
+                                       Resource library = null;\r
+                                       for(Resource tobeMoved : resources) {\r
+                                               if(graph.isInstanceOf(tobeMoved, sr.SysdynModelicaFunction) ||\r
+                                                               graph.isInstanceOf(tobeMoved, sr.SysdynModelicaFunctionLibrary)) {\r
+                                                       if(library == null) {\r
+                                                               library = graph.getSingleObject(thisFunction, l0.PartOf);\r
+                                                       }\r
+                                                       Resource oldLib = graph.getSingleObject(tobeMoved, l0.PartOf);\r
+                                                       graph.deny(tobeMoved, l0.PartOf);\r
+                                                       graph.claim(tobeMoved, l0.PartOf, library);\r
+                                                       FunctionUtils.updateFunctionFileForLibrary(graph, oldLib);\r
+                                                       FunctionUtils.updateFunctionFileForLibrary(graph, library);\r
+                                               }\r
+                                       }\r
+                               }\r
+                       });\r
+               }\r
+       }\r
 }\r
index c8c5735204d3bee0e89770acdc30fbdf837234ff..bbba4a0b82f7275b3d08f144a491eb643122bcf3 100644 (file)
 package org.simantics.sysdyn.ui.browser.nodes;\r
 \r
 import org.simantics.browsing.ui.common.node.AbstractNode;\r
+import org.simantics.browsing.ui.common.node.IDropTargetNode;\r
 import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.FunctionUtils;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
 \r
-public class FunctionsFolder extends AbstractNode<Resource> {\r
+public class FunctionsFolder extends AbstractNode<Resource> implements IDropTargetNode{\r
 \r
     public FunctionsFolder(Resource resource) {\r
         super(resource);\r
@@ -27,4 +36,35 @@ public class FunctionsFolder extends AbstractNode<Resource> {
             return null;\r
         return super.getAdapter(adapter);\r
     }\r
+    \r
+       @Override\r
+       public void drop(Object data) {\r
+               final Resource[] resources = ResourceAdaptionUtils.toResources(data);\r
+               final Resource library = this.data;\r
+               if(resources.length > 0) {\r
+                       SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+                               \r
+                               @Override\r
+                               public void perform(WriteGraph graph) throws DatabaseException {\r
+                                       SysdynResource sr = SysdynResource.getInstance(graph);\r
+                                       Layer0 l0 = Layer0.getInstance(graph);\r
+                                       for(Resource tobeMoved : resources) {\r
+                                               if(graph.isInstanceOf(tobeMoved, sr.SysdynModelicaFunction) ||\r
+                                                               graph.isInstanceOf(tobeMoved, sr.SysdynModelicaFunctionLibrary)) {\r
+                                                       Resource oldLib = graph.getSingleObject(tobeMoved, l0.PartOf);\r
+                                                       graph.deny(tobeMoved, l0.PartOf);\r
+                                                       graph.claim(tobeMoved, l0.PartOf, library);\r
+                                                       FunctionUtils.updateFunctionFileForLibrary(graph, oldLib);\r
+                                                       FunctionUtils.updateFunctionFileForLibrary(graph, library);\r
+                                               }\r
+                                       }\r
+                               }\r
+                       });\r
+               }\r
+       }\r
+       \r
+\r
+
+       \r
+       \r
 }\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SharedFunctionLibraryNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SharedFunctionLibraryNode.java
new file mode 100644 (file)
index 0000000..7d76469
--- /dev/null
@@ -0,0 +1,75 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.browser.nodes;\r
+\r
+import org.simantics.browsing.ui.common.node.DeleteException;\r
+import org.simantics.browsing.ui.common.node.IDropTargetNode;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.FunctionUtils;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+\r
+public class SharedFunctionLibraryNode extends FunctionLibraryNode<Resource> implements IDropTargetNode {\r
+\r
+    public SharedFunctionLibraryNode(Resource resource) {\r
+        super(resource);\r
+    }\r
+\r
+       @Override\r
+       public void delete() throws DeleteException {\r
+               SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+\r
+                       @Override\r
+                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                               Layer0 l0 = Layer0.getInstance(graph);\r
+                               \r
+                               graph.deny(data, l0.PartOf);\r
+                               graph.deny(data, l0.IsLinkedTo_Inverse);\r
+\r
+                               // TODO: remove file\r
+                       }\r
+               });\r
+       }\r
+       \r
+       @Override\r
+       public void drop(Object data) {\r
+               final Resource[] resources = ResourceAdaptionUtils.toResources(data);\r
+               final Resource library = this.data;\r
+               if(resources.length > 0) {\r
+                       SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+                               \r
+                               @Override\r
+                               public void perform(WriteGraph graph) throws DatabaseException {\r
+                                       SysdynResource sr = SysdynResource.getInstance(graph);\r
+                                       Layer0 l0 = Layer0.getInstance(graph);\r
+                                       for(Resource tobeMoved : resources) {\r
+                                               if(graph.isInstanceOf(tobeMoved, sr.SysdynModelicaFunction) ||\r
+                                                               graph.isInstanceOf(tobeMoved, sr.SysdynModelicaFunctionLibrary)) {\r
+                                                       Resource oldLib = graph.getSingleObject(tobeMoved, l0.PartOf);\r
+                                                       graph.deny(tobeMoved, l0.PartOf);\r
+                                                       graph.claim(tobeMoved, l0.PartOf, library);\r
+                                                       FunctionUtils.updateFunctionFileForLibrary(graph, oldLib);\r
+                                                       FunctionUtils.updateFunctionFileForLibrary(graph, library);\r
+                                               }\r
+                                       }\r
+                                       \r
+                               }\r
+                       });\r
+               }\r
+       }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportFunctionLibrary.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ExportFunctionLibrary.java
new file mode 100644 (file)
index 0000000..4ad047d
--- /dev/null
@@ -0,0 +1,108 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.handlers;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Files;\r
+import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.TransferableGraphRequest2;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+import org.simantics.utils.datastructures.Pair;\r
+\r
+public class ExportFunctionLibrary  extends AbstractHandler {\r
+\r
+       @Override\r
+       public Object execute(ExecutionEvent event) throws ExecutionException {\r
+\r
+        ISelection sel = HandlerUtil.getCurrentSelection(event);\r
+        final Resource functionLibrary = ResourceAdaptionUtils.toSingleResource(sel);\r
+        if(functionLibrary == null) return null;\r
+               \r
+               String name = null;\r
+               try {\r
+                       name = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
+\r
+                               @Override\r
+                               public String perform(ReadGraph graph) throws DatabaseException {\r
+                                       if (!graph.hasStatement(functionLibrary, Layer0.getInstance(graph).PartOf))\r
+                                               return null;\r
+                                       Layer0 l0 = Layer0.getInstance(graph);\r
+                                       String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));\r
+                                       return name;\r
+                                       \r
+                               }\r
+                               \r
+                       });\r
+               } catch (DatabaseException e1) {\r
+                       e1.printStackTrace();\r
+               }\r
+               if(name == null) return null;\r
+               \r
+               Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+               FileDialog fd = new FileDialog(shell, SWT.SAVE);\r
+               fd.setText("Export..");\r
+               fd.setFileName(name);\r
+               fd.setFilterPath(Platform.getLocation().toOSString());\r
+               String[] filterExt = {"*.tg"};\r
+               fd.setFilterExtensions(filterExt);\r
+               final String selected = fd.open();\r
+               if(selected == null) return null;\r
+               \r
+               \r
+               SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
+                       \r
+                       @Override\r
+                       public void run(ReadGraph graph) throws DatabaseException {\r
+                               Layer0 l0 = Layer0.getInstance(graph);\r
+                               String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));\r
+                               ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();\r
+                               roots.add(Pair.make(functionLibrary, name));\r
+                               TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, functionLibrary));\r
+\r
+                               try {\r
+                                       Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);\r
+                               } catch (RuntimeBindingConstructionException e) {\r
+                                       e.printStackTrace();\r
+                               } catch (IOException e) {\r
+                                       e.printStackTrace();\r
+                               }\r
+\r
+                               \r
+                       }\r
+               });\r
+\r
+               return null;\r
+       }\r
+\r
+}\r
index c8f30da299d89289ce8cc0581d3b1cc8a3edd2fd..2115825793ce2aacffd0092793bed779b80e8b33 100644 (file)
@@ -22,9 +22,7 @@ import org.eclipse.core.runtime.Platform;
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.widgets.FileDialog;\r
 import org.eclipse.swt.widgets.Shell;\r
-import org.eclipse.ui.IWorkbenchPart;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
-import org.simantics.browsing.ui.platform.PropertyPageView;\r
 import org.simantics.databoard.Bindings;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
@@ -33,7 +31,7 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.layer0.Layer0;\r
 import org.simantics.layer0.utils.direct.GraphUtils;\r
 import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.ui.properties.SysdynPropertyPage;\r
+import org.simantics.sysdyn.manager.FunctionUtils;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.utils.datastructures.Pair;\r
 \r
@@ -42,13 +40,6 @@ public class ImportExternalFunctionFilesHandler extends AbstractHandler {
        public static final String[] C_EXTENSIONS = {"*.c","*.h","*.a","*.o"};\r
        @Override\r
        public Object execute(ExecutionEvent event) throws ExecutionException {\r
-               IWorkbenchPart part = HandlerUtil.getActivePart(event);\r
-               PropertyPageView ppv = (PropertyPageView)part;\r
-               SysdynPropertyPage page = (SysdynPropertyPage)ppv.getCurrentPage();\r
-               Object o = page.getAdapter(Resource.class);\r
-        if (part == null)\r
-            return null;\r
-               \r
                Shell shell = HandlerUtil.getActiveShellChecked(event);\r
                Pair<String, String[]> selected = importFiles(shell, "Import...", C_EXTENSIONS);\r
                if(selected.second == null || selected.second.length < 1) return null;\r
@@ -101,12 +92,14 @@ public class ImportExternalFunctionFilesHandler extends AbstractHandler {
                                FileInputStream fis = new FileInputStream(file);\r
                                fis.read(fileBArray);\r
                                graph.claimLiteral(externalFile, sr.HasExternalFile, fileBArray, Bindings.BYTE_ARRAY);\r
+                               \r
+                               FunctionUtils.createExternalFunctionFile(graph, externalFile);\r
                        } catch (IOException e) {\r
-                               // TODO Auto-generated catch block\r
                                e.printStackTrace();\r
                        }\r
 \r
                }\r
+               \r
        }\r
 \r
 }\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportFunctionLibrary.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/ImportFunctionLibrary.java
new file mode 100644 (file)
index 0000000..422f3dc
--- /dev/null
@@ -0,0 +1,178 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.handlers;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Files;\r
+import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;\r
+import org.simantics.db.Builtins;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.WriteOnlyGraph;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ResourceNotFoundException;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;\r
+import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.graph.representation.Root;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.FunctionUtils;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+\r
+public class ImportFunctionLibrary  extends AbstractHandler {\r
+\r
+       @Override\r
+       public Object execute(ExecutionEvent event) throws ExecutionException {\r
+               Resource project = SimanticsUI.getProject().get();\r
+               if(project == null) return null;\r
+\r
+        ISelection sel = HandlerUtil.getCurrentSelection(event);\r
+        final Resource functionLibrary = ResourceAdaptionUtils.toSingleResource(sel);\r
+        if(functionLibrary == null) return null;\r
+        \r
+               Shell shell = HandlerUtil.getActiveShellChecked(event);\r
+               FileDialog fd = new FileDialog(shell, SWT.OPEN);\r
+               fd.setText("Import Function Library");\r
+               fd.setFilterPath(Platform.getLocation().toOSString());\r
+               String[] filterExt = {"*.tg"};\r
+               fd.setFilterExtensions(filterExt);\r
+               String selected = fd.open();\r
+               if(selected == null) return null;\r
+\r
+               TransferableGraph1 tg = null;\r
+               try {\r
+                       tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));\r
+               } catch (RuntimeBindingConstructionException e) {\r
+                       e.printStackTrace();\r
+               } catch (IOException e) {\r
+                       e.printStackTrace();\r
+               }\r
+               if(tg == null) return null;\r
+\r
+       \r
+               try {\r
+                       Boolean hasSharedOntologies;\r
+                       hasSharedOntologies = SimanticsUI.getSession().syncRequest(new Read<Boolean>() {\r
+\r
+                               @Override\r
+                               public Boolean perform(ReadGraph graph) throws DatabaseException {\r
+                                       try {\r
+                                               graph.getResource("http://SharedOntologies");\r
+                                       } catch (ResourceNotFoundException e) {\r
+                                               return false;\r
+                                       }               \r
+                                       return true;\r
+                               }\r
+                       });\r
+\r
+                       if(!hasSharedOntologies) {\r
+                               SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+\r
+                                       @Override\r
+                                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                                               Layer0 l0 = Layer0.getInstance(graph);\r
+                                               GraphUtils.create2(graph, l0.Library, \r
+                                                               l0.HasName, "SharedOntologies",\r
+                                                               l0.PartOf, graph.getResource("http:/"));\r
+                                       }\r
+                               });\r
+\r
+                       }\r
+               } catch (DatabaseException e) {\r
+                       e.printStackTrace();\r
+                       return null;\r
+               }\r
+\r
+               \r
+               SysdynFunctionLibraryImportAdvisor ia = new SysdynFunctionLibraryImportAdvisor(functionLibrary);\r
+               try {\r
+                       DefaultPasteHandler.defaultExecute(tg, functionLibrary, ia);\r
+               } catch (Exception e) {\r
+                       e.printStackTrace();\r
+               }\r
+               \r
+               SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
+                       \r
+                       @Override\r
+                       public void run(ReadGraph graph) throws DatabaseException {\r
+                               FunctionUtils.updateFunctionFileForLibrary(graph, functionLibrary);\r
+                       }\r
+               });\r
+               return null;\r
+       }\r
+       \r
+       private class SysdynFunctionLibraryImportAdvisor extends DefaultPasteImportAdvisor {\r
+\r
+               \r
+               Resource model = null;\r
+               \r
+               public SysdynFunctionLibraryImportAdvisor(Resource library) {\r
+                       super(library);\r
+               }\r
+               \r
+               @Override\r
+               public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {\r
+                       if(root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) {\r
+                               library = null;\r
+                               try {\r
+                                       SysdynResource sr = SysdynResource.getInstance(graph);\r
+                                       Layer0 l0 = Layer0.getInstance(graph);\r
+                                       Resource r = library;\r
+                                       while(!graph.isInstanceOf(r, sr.SysdynModel) && graph.isInstanceOf(r, l0.Ontology))\r
+                                               r = graph.getSingleObject(r, l0.PartOf);\r
+                                       if(graph.isInstanceOf(r, sr.SysdynModel))\r
+                                               model = r;\r
+                                       \r
+                                       library = graph.getResource("http://SharedOntologies");\r
+                               } catch (ResourceNotFoundException e) {\r
+                                       e.printStackTrace();\r
+                               }\r
+                       }\r
+               }\r
+               \r
+               @Override\r
+               public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {\r
+                       Builtins b = graph.getBuiltins();\r
+                       this.root = graph.newResource();\r
+                       graph.claim(library, b.ConsistsOf, b.PartOf, this.root);\r
+                       String name = root.name;\r
+                       String newName = nameMappings.get(name);\r
+                       graph.addLiteral(this.root, b.HasName, b.NameOf, b.String, newName, Bindings.STRING);\r
+                       if(model != null && root.type.equals(SysdynResource.URIs.SharedFunctionOntology)) {\r
+                               graph.claim(model, b.IsLinkedTo, b.IsLinkedTo_Inverse, this.root);\r
+                       }\r
+                       return this.root;\r
+\r
+               }\r
+                       \r
+       }\r
+\r
+}\r
index 926cde5d40a9374d321932ff2b282fc4737228b1..410801563fa21a5829657e0afaa7c1373da6bbdd 100644 (file)
@@ -16,6 +16,7 @@ import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;\r
 import org.eclipse.jface.viewers.ISelection;\r
 import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.browsing.ui.common.node.AbstractNode;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.common.request.WriteRequest;\r
@@ -25,7 +26,6 @@ import org.simantics.layer0.Layer0;
 import org.simantics.layer0.utils.direct.GraphUtils;\r
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.manager.FunctionUtils;\r
-import org.simantics.sysdyn.ui.browser.nodes.FunctionsFolder;\r
 import org.simantics.ui.SimanticsUI;\r
 import org.simantics.ui.utils.AdaptionUtils;\r
 \r
@@ -36,27 +36,39 @@ public class NewFunctionHandler  extends AbstractHandler {
 \r
         ISelection sel = HandlerUtil.getCurrentSelection(event);\r
         \r
-        FunctionsFolder node = AdaptionUtils.adaptToSingle(sel, FunctionsFolder.class);\r
+        @SuppressWarnings("unchecked")\r
+               AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);\r
         if (node == null)\r
             return null;\r
 \r
-        final Resource model = node.data;\r
+        final Resource data = node.data;\r
 \r
         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
 \r
             @Override\r
             public void perform(WriteGraph g) throws DatabaseException {\r
                Layer0 l0 = Layer0.getInstance(g);\r
-                String name = NameUtils.findFreshName(g, "Function", model, l0.ConsistsOf, "%s%d");\r
-\r
                 SysdynResource sr = SysdynResource.getInstance(g);\r
+\r
+                Resource library = null;\r
+                if(g.isInstanceOf(data, sr.SysdynModel) || g.isInstanceOf(data, l0.Library))\r
+                       library = data;\r
+                else if (g.isInstanceOf(data, sr.SysdynModelicaFunction))\r
+                       library = g.getPossibleObject(data, l0.PartOf);\r
+                \r
+                if(library == null)\r
+                       return;\r
+                       \r
+                \r
+                String name = NameUtils.findFreshName(g, "Function", library, l0.ConsistsOf, "%s%d");\r
+\r
                 GraphUtils.create2(g, sr.SysdynModelicaFunction,\r
                         l0.HasName, name,\r
                         l0.HasDescription, "",\r
                         sr.HasModelicaFunctionCode, "",\r
-                        l0.PartOf, model);\r
+                        l0.PartOf, library);\r
                 \r
-                FunctionUtils.updateFunctionFileForLibrary(g, model);\r
+                FunctionUtils.updateFunctionFileForLibrary(g, library);\r
             }\r
         });\r
         \r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewFunctionLibraryHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewFunctionLibraryHandler.java
new file mode 100644 (file)
index 0000000..3465aa1
--- /dev/null
@@ -0,0 +1,103 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.handlers;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.browsing.ui.common.node.AbstractNode;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ResourceNotFoundException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.layer0.utils.direct.GraphUtils;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.manager.FunctionUtils;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+\r
+public class NewFunctionLibraryHandler extends AbstractHandler {\r
+\r
+       @Override\r
+       public Object execute(ExecutionEvent event) throws ExecutionException {\r
+\r
+        ISelection sel = HandlerUtil.getCurrentSelection(event);\r
+        \r
+        @SuppressWarnings("unchecked")\r
+               AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);\r
+        if (node == null)\r
+            return null;\r
+\r
+        createLibrary(node.data, false);\r
+        return null;\r
+       }\r
+       \r
+       /**\r
+        * Create function library. Shared libraries are created to root, local libraries to the model. \r
+        * Shared libraries can be used in other models in the project.\r
+        * \r
+        * @param model The initial location of the command\r
+        * @param shared Shared libraries are created to root, local libraries to the model.\r
+        */\r
+       protected void createLibrary(final Resource model, final boolean shared) {\r
+        SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+\r
+            @Override\r
+            public void perform(WriteGraph g) throws DatabaseException {\r
+               Layer0 l0 = Layer0.getInstance(g);\r
+                SysdynResource sr = SysdynResource.getInstance(g);\r
+\r
+                if(!(g.isInstanceOf(model, sr.SysdynModel) ||\r
+                               g.isInstanceOf(model, sr.SysdynModelicaFunctionLibrary) ||\r
+                                               g.isInstanceOf(model, sr.SharedFunctionOntology)))\r
+                       return;\r
+\r
+                Resource root = model;\r
+\r
+                String name = "FunctionLibrary";\r
+                Resource libraryType = sr.SysdynModelicaFunctionLibrary;\r
+                \r
+                if(shared) {\r
+                       \r
+                       try {\r
+                               root = g.getResource("http://SharedOntologies");\r
+                       } catch (ResourceNotFoundException e) {\r
+                               root = g.getResource("http:/");\r
+                               root = GraphUtils.create2(g, l0.Library, \r
+                                               l0.HasName, "SharedOntologies",\r
+                                               l0.PartOf, root);\r
+                       }\r
+                       \r
+                       name = "Shared" + name;\r
+                    libraryType = sr.SharedFunctionOntology;\r
+                }\r
+\r
+                name = NameUtils.findFreshName(g, name, root, l0.ConsistsOf, "%s%d");\r
+                \r
+                Resource functionLibrary = GraphUtils.create2(g, libraryType,\r
+                        l0.HasName, name,\r
+                        l0.HasDescription, "",\r
+                        l0.PartOf, root);\r
+                \r
+                if(shared)\r
+                       g.claim(model, l0.IsLinkedTo, functionLibrary);\r
+\r
+                FunctionUtils.updateFunctionFileForLibrary(g, functionLibrary);\r
+            }\r
+        });\r
+       }\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewSharedFunctionLibraryHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/handlers/NewSharedFunctionLibraryHandler.java
new file mode 100644 (file)
index 0000000..83ab344
--- /dev/null
@@ -0,0 +1,37 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.handlers;\r
+\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.browsing.ui.common.node.AbstractNode;\r
+import org.simantics.db.Resource;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+\r
+public class NewSharedFunctionLibraryHandler extends NewFunctionLibraryHandler {\r
+\r
+       @Override\r
+       public Object execute(ExecutionEvent event) throws ExecutionException {\r
+\r
+        ISelection sel = HandlerUtil.getCurrentSelection(event);\r
+        \r
+        @SuppressWarnings("unchecked")\r
+               AbstractNode<Resource> node = AdaptionUtils.adaptToSingle(sel, AbstractNode.class);\r
+        if (node == null)\r
+            return null;\r
+\r
+        createLibrary(node.data, true);\r
+        return null;\r
+       }\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/FunctionLibraryTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/FunctionLibraryTab.java
new file mode 100644 (file)
index 0000000..abbdacf
--- /dev/null
@@ -0,0 +1,49 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.properties;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.simantics.browsing.ui.swt.PropertyTabContributorImpl;\r
+import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;\r
+import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;\r
+import org.simantics.browsing.ui.swt.widgets.TrackedText;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class FunctionLibraryTab extends PropertyTabContributorImpl {\r
+\r
+       @Override\r
+       public void createControls(Composite body, IWorkbenchSite site,\r
+                       ISessionContext context, WidgetSupport support) {\r
+               Composite composite = new Composite(body, SWT.NONE);\r
+               GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
+               GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(composite);\r
+\r
+\r
+               TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
+               nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName));\r
+               nameText.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasName));\r
+               GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
+\r
+\r
+               TrackedText information = new TrackedText(composite, support, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);\r
+               information.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasDescription));\r
+               information.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasDescription));\r
+               GridDataFactory.fillDefaults().grab(true, true).applyTo(information.getWidget());\r
+       }\r
+\r
+}\r
index 493a574b5104da7f809f4bc9f84a2b348ddac209..409ee860085677e2282e7932a77f4426d743c3d1 100644 (file)
@@ -21,7 +21,9 @@ import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.common.request.WriteRequest;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Write;\r
 import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.manager.FunctionUtils;\r
 import org.simantics.ui.SimanticsUI;\r
 \r
 public class ExternalFileNode extends AbstractNode<Resource> implements IModifiableNode, IDeletableNode {\r
@@ -33,7 +35,7 @@ public class ExternalFileNode extends AbstractNode<Resource> implements IModifia
        @Override\r
        public Modifier getModifier(String columnId) {\r
                try {\r
-                       Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
+                       final Resource hasName = Layer0.getInstance(SimanticsUI.getSession()).HasName;\r
                        LabelModifier modifier = new LabelModifier(SimanticsUI.getSession(), data, hasName) {\r
                                @Override\r
                                public String isValid(String label) {\r
@@ -43,6 +45,18 @@ public class ExternalFileNode extends AbstractNode<Resource> implements IModifia
                                                return "Spaces are not allowed";\r
                                        return null;\r
                                }\r
+\r
+                               @Override\r
+                               protected Write getWriteRequest(final String label) {\r
+                                       return new WriteRequest() {\r
+                                               @Override\r
+                                               public void perform(WriteGraph g) throws DatabaseException {\r
+                                                       FunctionUtils.removeExternalFunctionFile(g, data);\r
+                                                       g.claimLiteral(data, hasName, label);\r
+                                                       FunctionUtils.createExternalFunctionFile(g, data);\r
+                                               }\r
+                                       };\r
+                               }\r
                        };\r
                        return modifier;\r
                } catch (DatabaseException e) {\r
@@ -62,6 +76,7 @@ public class ExternalFileNode extends AbstractNode<Resource> implements IModifia
                                if(function != null) {\r
                                        graph.deny(data, l0.PartOf, function);\r
                                }\r
+                               FunctionUtils.removeExternalFunctionFile(graph, data);\r
                        }\r
                });\r
        }\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/OldTransferableGraph1.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/OldTransferableGraph1.java
new file mode 100644 (file)
index 0000000..558f5a3
--- /dev/null
@@ -0,0 +1,76 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.utils;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.serialization.Serializer;\r
+import org.simantics.graph.representation.External;\r
+import org.simantics.graph.representation.Identity;\r
+import org.simantics.graph.representation.Internal;\r
+import org.simantics.graph.representation.Root;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.graph.representation.Value;\r
+\r
+\r
+/**\r
+ * Transferable graph datatype. \r
+ * See <a href="https://www.simantics.org/wiki/index.php/Graph_exchange_format">specification</a>. \r
+ * @author Hannu Niemistö\r
+ */\r
+public class OldTransferableGraph1 {\r
+       public static Binding BINDING = Bindings.getBindingUnchecked(TransferableGraph1.class);\r
+       public static Serializer SERIALIZER = Bindings.getSerializerUnchecked(BINDING);\r
+       \r
+       public int resourceCount;\r
+       public Identity[] identities;   \r
+       public int[] statements;\r
+       public Value[] values;  \r
+       \r
+       public OldTransferableGraph1() {\r
+       }\r
+       \r
+       public OldTransferableGraph1(int resourceCount, Identity[] identities,\r
+                       int[] statements, Value[] values) {\r
+               this.resourceCount = resourceCount;\r
+               this.identities = identities;\r
+               this.statements = statements;\r
+               this.values = values;\r
+       }\r
+\r
+       public void print() {\r
+               System.out.println("Identities");\r
+               for(Identity id : identities) {\r
+                       System.out.print("    " + id.resource + " = ");\r
+                       if(id.definition instanceof Root) {\r
+                               Root def = (Root)id.definition;\r
+                               System.out.println("ROOT(" + def.name + ")");\r
+                       }\r
+                       else if(id.definition instanceof External) {\r
+                               External def = (External)id.definition;\r
+                               System.out.println("EXTERNAL(" + def.parent + ", " + def.name + ")");\r
+                       }\r
+                       else if(id.definition instanceof Internal) {\r
+                               Internal def = (Internal)id.definition;\r
+                               System.out.println("INTERNAL(" + def.parent + ", " + def.name + ")");\r
+                       }\r
+               }\r
+               System.out.println("Statements:");\r
+               for(int i=0;i<statements.length;i+=4)\r
+                       System.out.println("    " + \r
+                                       statements[i] + " " +\r
+                                       statements[i+1] + " " +\r
+                                       statements[i+2] + " " +\r
+                                       statements[i+3]\r
+                                       );\r
+       }\r
+}\r
index fd145491d18c8c16c1e6ec1ab31df83ebe232dec..ce7c53a5eeb22780664a2499722071c4ed6c8592 100644 (file)
@@ -32,7 +32,8 @@ Export-Package: org.simantics.sysdyn,
  org.simantics.sysdyn.representation,
  org.simantics.sysdyn.representation.visitors,
  org.simantics.sysdyn.simulation,
- org.simantics.sysdyn.tableParser
+ org.simantics.sysdyn.tableParser,
+ org.simantics.sysdyn.adapter
 Bundle-Activator: org.simantics.sysdyn.Activator
 Bundle-ActivationPolicy: lazy
 Import-Package: org.eclipse.ui
index 2d68b7c8cc01b9a563f665c0ca2c39aaa117d274..8a9ea9e9daab5d0cb4c1fac56c5bf4b3a9c94958 100644 (file)
                <adapter uri="http://www.simantics.org/Sysdyn-1.0/Variable"\r
                        contextClass="org.simantics.db.layer0.variable.Variable"\r
                        adapterClass="org.simantics.sysdyn.adapter.ChildVariableAdapter"/>\r
+               <adapter uri="http://www.simantics.org/Sysdyn-1.0/Module"\r
+                       contextClass="org.simantics.db.layer0.variable.Variable"\r
+                       adapterClass="org.simantics.sysdyn.adapter.ChildVariableAdapter"/>\r
+               \r
        </target>\r
     \r
 </adapters>
\ No newline at end of file
index aa06e2c7478d61cf3f4c565b6b5aac5fd76cb835..def1fcbbb1831c27bf7f75e3b8ce45d0ef574d6c 100644 (file)
@@ -2,6 +2,8 @@ package org.simantics.sysdyn.manager;
 \r
 import java.io.File;\r
 import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
 import java.io.PrintStream;\r
 import java.util.ArrayList;\r
 import java.util.Collection;\r
@@ -10,12 +12,14 @@ import java.util.List;
 import java.util.Map;\r
 import java.util.Set;\r
 \r
+import org.simantics.databoard.Bindings;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.common.request.ObjectsWithType;\r
 import org.simantics.db.common.request.ReadRequest;\r
 import org.simantics.db.common.utils.NameUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Simantics;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.scl.runtime.function.Function;\r
 import org.simantics.simulation.ontology.SimulationResource;\r
@@ -28,25 +32,21 @@ public class FunctionUtils {
                final ArrayList<String> paths = new ArrayList<String>();\r
                \r
                try {\r
-                       model.session.syncRequest(new ReadRequest() {\r
+                       Simantics.getSession().syncRequest(new ReadRequest() {\r
                                \r
                                @Override\r
                                public void run(ReadGraph graph) throws DatabaseException {\r
                                        SysdynResource sr = SysdynResource.getInstance(graph);\r
-                                       Layer0 l0 = Layer0.getInstance(graph);\r
                                        SimulationResource simu = SimulationResource.getInstance(graph);\r
                                        \r
                                        Set<Resource> parents = getParents(graph, model);\r
                                        for(Resource parent : parents) {\r
-                                               Resource partof = graph.getPossibleObject(parent, l0.PartOf);\r
                                                if(graph.isInstanceOf(parent, sr.SysdynModel)) {\r
                                                        Resource configuration = graph.getPossibleObject(parent, simu.HasConfiguration);\r
-                                                       if(configuration.equals(model.configurationResource)) {\r
+                                                       if(configuration.equals(model.getConfigurationResource())) {\r
                                                                String parentName = NameUtils.getSafeName(graph, parent);\r
                                                                paths.add(parentName + "_functions.mo");\r
                                                        }\r
-                                               } else if (graph.isInstanceOf(partof, sr.SysdynModel)) {\r
-                                                       // TODO\r
                                                } else {\r
                                                        String libraryname = NameUtils.getSafeName(graph, parent);\r
                                                        paths.add("..\\\\..\\\\libraries\\\\functions\\\\" + libraryname + ".mo");\r
@@ -63,21 +63,80 @@ public class FunctionUtils {
        \r
        private static Set<Resource> getParents(ReadGraph graph, SysdynModel model) throws DatabaseException{\r
                HashSet<Resource> parents = new HashSet<Resource>();\r
+               HashSet<Resource> checked = new HashSet<Resource>();\r
+               ArrayList<Resource> toCheck = new ArrayList<Resource>();\r
 \r
                Layer0 l0 = Layer0.getInstance(graph);\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
                SimulationResource simu = SimulationResource.getInstance(graph);\r
-               Resource modelResource = graph.getPossibleObject(model.configurationResource, simu.IsConfigurationOf);\r
+               Resource modelResource = graph.getPossibleObject(model.getConfigurationResource(), simu.IsConfigurationOf);\r
+               \r
+               \r
+               // add model to parents and all the model libraries to checked\r
+               if(!graph.syncRequest(new ObjectsWithType(modelResource, l0.ConsistsOf, sr.SysdynModelicaFunction)).isEmpty() ||\r
+                               !graph.syncRequest(new ObjectsWithType(modelResource, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary)).isEmpty()) {\r
+               parents.add(modelResource);\r
+               toCheck.clear();\r
+               toCheck.add(modelResource);\r
+               while(!toCheck.isEmpty()){\r
+                       Resource checkedLibrary = toCheck.get(0);\r
+                       checked.add(checkedLibrary);\r
+                       toCheck.addAll(graph.syncRequest(new ObjectsWithType(checkedLibrary, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary)));\r
+                       toCheck.remove(checkedLibrary);\r
+               }\r
+               }\r
+               \r
+       // add all linked function ontologies to parents and their children ontologies to checked \r
+        Function index = graph.adapt(l0.Dependencies, Function.class);\r
+        Collection<Map<String, Object>> results = (Collection<Map<String, Object>>)index.apply(graph, modelResource, "Types:SharedFunctionOntology");\r
+        for(Map<String, Object> r : results) {\r
+               Resource sharedLibrary = (Resource)r.get("Resource");\r
+               if(sharedLibrary == null)\r
+                       continue;\r
+               if(graph.syncRequest(new ObjectsWithType(sharedLibrary, l0.ConsistsOf, sr.SysdynModelicaFunction)).isEmpty() &&\r
+                               graph.syncRequest(new ObjectsWithType(sharedLibrary, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary)).isEmpty())\r
+                       continue;\r
+                       \r
+               parents.add(sharedLibrary);\r
+               toCheck.clear();\r
+               toCheck.add(sharedLibrary);\r
+               while(!toCheck.isEmpty()){\r
+                       Resource checkedLibrary = toCheck.get(0);\r
+                       checked.add(checkedLibrary);\r
+                       toCheck.addAll(graph.syncRequest(new ObjectsWithType(checkedLibrary, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary)));\r
+                       toCheck.remove(checkedLibrary);\r
+               }\r
+        }\r
+               \r
+        // it is assumed that only built-in function libraries remain\r
                if(modelResource != null) {\r
-                       Function index = graph.adapt(l0.Dependencies, Function.class);\r
-                       Collection<Map<String, Object>> results = (Collection<Map<String, Object>>)index.apply(graph, modelResource, "Types:SysdynModelicaFunction");\r
+                       index = graph.adapt(l0.Dependencies, Function.class);\r
+                       results = (Collection<Map<String, Object>>)index.apply(graph, modelResource, "Types:SysdynModelicaFunctionLibrary");\r
                        for(Map<String, Object> r : results) {\r
-                               Resource function = (Resource)r.get("Resource");\r
-                               if(function != null) {\r
-                                       parents.add(graph.getSingleObject(function, l0.PartOf));\r
+                               Resource library = (Resource)r.get("Resource");\r
+                               if(library != null && !checked.contains(library)) {\r
+                                       \r
+                               while(!graph.isInstanceOf(library, sr.SysdynModel) && !graph.isInstanceOf(library, l0.Ontology)) {\r
+                                       library = graph.getPossibleObject(library, l0.PartOf);\r
+                               }\r
+                               \r
+                               if(graph.syncRequest(new ObjectsWithType(library, l0.ConsistsOf, sr.SysdynModelicaFunction)).isEmpty() &&\r
+                                               graph.syncRequest(new ObjectsWithType(library, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary)).isEmpty())\r
+                                       continue;\r
+                               \r
+                               parents.add(library);\r
+                               \r
+                               toCheck.clear();\r
+                               toCheck.add(library);\r
+                               while(!toCheck.isEmpty()){\r
+                                       Resource checkedLibrary = toCheck.get(0);\r
+                                       checked.add(checkedLibrary);\r
+                                       toCheck.addAll(graph.syncRequest(new ObjectsWithType(checkedLibrary, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary)));\r
+                                       toCheck.remove(checkedLibrary);\r
+                               }\r
                                }\r
                        }\r
                }\r
-               \r
                return parents;\r
                \r
        }\r
@@ -91,7 +150,7 @@ public class FunctionUtils {
                        if(graph.isInstanceOf(parent, sr.SysdynModel)) {\r
                                String parentName = NameUtils.getSafeName(graph, parent);\r
                                File scriptFile = new File(model.getSimulationDir(), parentName + "_functions.mo");\r
-                               updateFunctionFile(graph, parent, scriptFile);\r
+                               updateFunctionFile(graph, parent, scriptFile, false);\r
                        } else {\r
                                updateFunctionFileForLibrary(graph, parent);\r
                        }\r
@@ -99,8 +158,76 @@ public class FunctionUtils {
                return;\r
        }\r
        \r
+       private static File getDir(ReadGraph graph, Resource resource) throws DatabaseException {\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               \r
+               Resource library = resource;\r
+               while(!graph.isInstanceOf(library, sr.SysdynModel) && !graph.isInstanceOf(library, l0.Ontology)) {\r
+                       library = graph.getSingleObject(resource, l0.PartOf);\r
+               }\r
+               \r
+               File dir = null;\r
+               \r
+               if(graph.isInstanceOf(library, sr.SysdynModel)) {\r
+                       SimulationResource simu = SimulationResource.getInstance(graph);\r
+                       Resource configuration = graph.getSingleObject(library, simu.HasConfiguration);\r
+                       SysdynModelManager smm = SysdynModelManager.getInstance(graph.getSession());\r
+                       SysdynModel model = smm.getModel(graph, configuration);\r
+                       dir = model.getSimulationDir();\r
+               } else {\r
+                       File librariesDir  = Activator.getBundleContext().getDataFile("libraries");\r
+                       if (!librariesDir.exists()) {\r
+                               librariesDir.mkdir();\r
+                       }\r
+                       dir = new File(librariesDir, "functions");\r
+                       if (!dir.exists()) {\r
+                               dir.mkdir();\r
+                       }\r
+               }\r
+               return dir;\r
+       }\r
+\r
+       public static void createExternalFunctionFile(ReadGraph graph, Resource externalFunction) throws DatabaseException {\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+\r
+               Resource function = graph.getSingleObject(externalFunction, l0.PartOf);\r
+               File dir = getDir(graph, function);\r
+\r
+               try {\r
+                       String name = NameUtils.getSafeName(graph, externalFunction);\r
+                       if(name.endsWith(".o") || name.endsWith(".a")) {\r
+                               FileOutputStream fos = new FileOutputStream(dir + "\\" + name);\r
+                               byte[] fileBArray = graph.getPossibleRelatedValue(externalFunction, sr.HasExternalFile, Bindings.BYTE_ARRAY);\r
+                               fos.write(fileBArray);\r
+                               fos.close();\r
+                       }\r
+               } catch (IOException e) {\r
+                       e.printStackTrace();\r
+               }\r
+       }\r
+       \r
+       public static void removeExternalFunctionFile(ReadGraph graph, Resource externalFunction) throws DatabaseException {\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+\r
+               Resource function = graph.getSingleObject(externalFunction, l0.PartOf);\r
+               File dir = getDir(graph, function);\r
+               String name = NameUtils.getSafeName(graph, externalFunction);\r
+               File file = new File(dir, name);\r
+               file.delete();\r
+       }\r
+\r
+       \r
+       \r
        public static void updateFunctionFileForLibrary(ReadGraph graph, Resource library) throws DatabaseException {\r
                SysdynResource sr = SysdynResource.getInstance(graph);\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               \r
+               while(!graph.isInstanceOf(library, sr.SysdynModel) && !graph.isInstanceOf(library, l0.Ontology)) {\r
+                       library = graph.getSingleObject(library, l0.PartOf);\r
+               }\r
+               \r
                if(graph.isInstanceOf(library, sr.SysdynModel)) {\r
                        SimulationResource simu = SimulationResource.getInstance(graph);\r
                        Resource configuration = graph.getSingleObject(library, simu.HasConfiguration);\r
@@ -108,6 +235,7 @@ public class FunctionUtils {
                        SysdynModel model = smm.getModel(graph, configuration);\r
                        updateFunctionFilesForModel(graph, model);\r
                } else {\r
+               \r
                        File librariesDir  = Activator.getBundleContext().getDataFile("libraries");\r
                        if (!librariesDir.exists()) {\r
                                librariesDir.mkdir();\r
@@ -118,16 +246,20 @@ public class FunctionUtils {
                        }\r
                        String parentName = NameUtils.getSafeName(graph, library);\r
                        File scriptFile = new File(dir, parentName + ".mo");\r
-                       updateFunctionFile(graph, library, scriptFile);\r
+                       \r
+                       Resource sysdyn = graph.getResource("http://www.simantics.org/Sysdyn-1.0");\r
+\r
+                       if(library.equals(sysdyn)) {\r
+                               updateFunctionFile(graph, library, scriptFile, true);\r
+                       } else {\r
+                               updateFunctionFile(graph, library, scriptFile, false);\r
+                       }\r
                }\r
                 \r
 \r
        }\r
        \r
-       private static void updateFunctionFile(ReadGraph graph, Resource library, File scriptFile) throws DatabaseException {\r
-               Layer0 l0 = Layer0.getInstance(graph);\r
-               SysdynResource sr = SysdynResource.getInstance(graph);\r
-               \r
+       private static void updateFunctionFile(ReadGraph graph, Resource library, File scriptFile, boolean builtIn) throws DatabaseException {\r
                PrintStream s;\r
                try {\r
                        s = new PrintStream(scriptFile);\r
@@ -135,14 +267,44 @@ public class FunctionUtils {
                        e.printStackTrace();\r
                        return;\r
                }\r
-\r
+               \r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               String name = NameUtils.getSafeName(graph, library);\r
+               if(!builtIn && !graph.isInstanceOf(library, sr.SysdynModel))\r
+                       s.println("package " + name);\r
+               writeLibrary(graph, library, s, builtIn);\r
+               if(!builtIn && !graph.isInstanceOf(library, sr.SysdynModel))\r
+                       s.println("end " + name + ";\n");\r
+               \r
+               s.close();\r
+       }\r
+       \r
+       private static void writeLibrary(ReadGraph graph, Resource library, PrintStream stream, boolean builtIn) throws DatabaseException {\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
+               writeLibraryFunctions(graph, library, stream);\r
+               for(Resource sublibrary : graph.syncRequest(new ObjectsWithType(library, l0.ConsistsOf, sr.SysdynModelicaFunctionLibrary))) {\r
+                       String name = NameUtils.getSafeName(graph, sublibrary);\r
+                       if(!builtIn)\r
+                               stream.println("encapsulated package " + name);\r
+                       writeLibrary(graph, sublibrary, stream, builtIn);\r
+                       if(!builtIn)\r
+                               stream.println("end " + name + ";\n");\r
+               }\r
+       }\r
+       \r
+       private static void writeLibraryFunctions(ReadGraph graph, Resource library, PrintStream stream) throws DatabaseException {\r
+               Layer0 l0 = Layer0.getInstance(graph);\r
+               SysdynResource sr = SysdynResource.getInstance(graph);\r
                for(Resource function : graph.syncRequest(new ObjectsWithType(library, l0.ConsistsOf, sr.SysdynModelicaFunction))) {\r
                        String name = NameUtils.getSafeName(graph, function);\r
                        String functionCode = graph.getRelatedValue(function, sr.HasModelicaFunctionCode);\r
-                       s.println("function " + name);\r
-               s.println(functionCode);\r
-               s.println("end " + name + ";\n");\r
+                       stream.println("function " + name);\r
+                       stream.println(functionCode);\r
+                       stream.println("end " + name + ";\n");\r
+                       for(Resource externalFunction : graph.syncRequest(new ObjectsWithType(function, l0.ConsistsOf, sr.ExternalFunctionFile))) {\r
+                               createExternalFunctionFile(graph, externalFunction);\r
+                       }\r
                }\r
-               s.close();\r
        }\r
 }\r
index 1b5df7cee7062cf43fd9b15923590781562f50e9..094d29a590bd625260649ef5433930d89e2c4ce8 100644 (file)
@@ -30,6 +30,8 @@ import org.simantics.db.Resource;
 import org.simantics.db.Session;\r
 import org.simantics.db.common.request.ReadRequest;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
+import org.simantics.db.exception.ServiceException;\r
 import org.simantics.layer0.Layer0;\r
 import org.simantics.modelica.IModelicaMonitor;\r
 import org.simantics.modelica.ModelicaException;\r
@@ -65,32 +67,34 @@ import org.simantics.sysdyn.representation.expressions.ParameterExpression;
  */\r
 public class SysdynModel implements IMappingListener, IModel {\r
 \r
-       Session session;\r
+       private Session session;\r
 \r
-       IMapping mapping;\r
+       private IMapping mapping;\r
 \r
-       Resource configurationResource;\r
-       Configuration configuration;\r
+       private Resource configurationResource;\r
+       private Resource modelResource;\r
+       \r
+       private Configuration configuration;\r
 \r
-       Set<Configuration> modules = new HashSet<Configuration>();\r
+       private Set<Configuration> modules = new HashSet<Configuration>();\r
 \r
-       Process process;\r
-       boolean canceled;\r
-       SimulationResult result;\r
-       SysdynResult sysdynResult;\r
+       private Process process;\r
+       private boolean canceled;\r
+       private SimulationResult result;\r
+       private SysdynResult sysdynResult;\r
 \r
-       CopyOnWriteArrayList<Runnable> modificationListeners =\r
+       private CopyOnWriteArrayList<Runnable> modificationListeners =\r
                new CopyOnWriteArrayList<Runnable>();\r
-       CopyOnWriteArrayList<Runnable> resultListeners =\r
+       private CopyOnWriteArrayList<Runnable> resultListeners =\r
                new CopyOnWriteArrayList<Runnable>();\r
 \r
        @SuppressWarnings("rawtypes")\r
-       Map<Class, Object> services = new HashMap<Class, Object>();\r
+       private Map<Class, Object> services = new HashMap<Class, Object>();\r
 \r
-       String previousModelStructure;\r
-       HashMap<String, String> previousImportantInits = new HashMap<String, String>();\r
+       private String previousModelStructure;\r
+       private HashMap<String, String> previousImportantInits = new HashMap<String, String>();\r
 \r
-       File simulationDir;\r
+       private File simulationDir;\r
 \r
        void readModules(ReadGraph graph, Resource configResource, Set<Resource> result) throws DatabaseException {\r
 \r
@@ -140,38 +144,14 @@ public class SysdynModel implements IMappingListener, IModel {
                sysdynResult.setResult(new SimulationResult());\r
 \r
                previousModelStructure = "";\r
-\r
-               File modelsDir = Activator.getBundleContext().getDataFile("models");\r
-               String configName = configuration.getName();\r
-               List<String> files = Arrays.asList(modelsDir.list());\r
-               if (files.contains(configName)) {\r
-                       int i = 2;\r
-                       while (files.contains(configName + "_" + i)){\r
-                               i++;\r
-                       }\r
-                       configName += "_" + i;\r
-               }\r
-\r
-               simulationDir  = Activator.getBundleContext().getDataFile("models/" + configName);\r
-               if (!simulationDir.exists()) {\r
-                       simulationDir.mkdir();\r
-               }\r
-               \r
-               try {\r
-                       FunctionUtils.updateFunctionFilesForModel(g, this);\r
-               } catch (DatabaseException e) {\r
-                       System.err.println("Error creating functions");\r
-                       e.printStackTrace();\r
-               }\r
-               \r
        }\r
 \r
 \r
        /*\r
         * some dummy(?) stuff for experiments\r
         */\r
-       public SysdynModel(Resource configurationResource) {\r
-               this.configurationResource = configurationResource;\r
+       public SysdynModel(Resource modelResource) {\r
+               this.modelResource = modelResource;\r
        }\r
 \r
        public synchronized void simulate(IModelicaMonitor monitor, final IProgressMonitor progressMonitor, final Experiment experiment) throws IOException {\r
@@ -209,7 +189,7 @@ public class SysdynModel implements IMappingListener, IModel {
                }\r
                \r
                final SimulationLocation simulationLocation = ModelicaManager.createInputFiles(\r
-                               simulationDir,\r
+                               getSimulationDir(),\r
                                configuration.getName(),\r
                                writer.toString(),\r
                                inits,\r
@@ -400,6 +380,10 @@ public class SysdynModel implements IMappingListener, IModel {
        public Configuration getConfiguration() {\r
                return configuration;\r
        }\r
+       \r
+       public Resource getConfigurationResource() {\r
+               return configurationResource;\r
+       }\r
 \r
        public IMapping getMapping() {\r
                return mapping;\r
@@ -423,7 +407,21 @@ public class SysdynModel implements IMappingListener, IModel {
 \r
        @Override\r
        public IExperiment loadExperiment(ReadGraph g, Resource experiment, IExperimentActivationListener listener) {\r
-               SysdynExperiment exp = new SysdynExperiment(experiment, configurationResource);\r
+               \r
+               // Not good. IModel pushes only modelResource and it is assumed that\r
+               // SysdynModel is based on configuration\r
+               if(configurationResource == null && modelResource != null) {\r
+                       SimulationResource simu = SimulationResource.getInstance(g);\r
+                       try {\r
+                               configurationResource = g.getPossibleObject(modelResource, simu.HasConfiguration);\r
+                       } catch (ManyObjectsForFunctionalRelationException e) {\r
+                               e.printStackTrace();\r
+                       } catch (ServiceException e) {\r
+                               e.printStackTrace();\r
+                       } \r
+               }\r
+\r
+               SysdynExperiment exp = new SysdynExperiment(experiment, modelResource);\r
                try {\r
                        exp.init(g);\r
                        ExperimentRuns.createRun(g.getSession(), experiment, exp, listener, null);\r
@@ -491,6 +489,23 @@ public class SysdynModel implements IMappingListener, IModel {
        }\r
        \r
        public File getSimulationDir() {\r
+               if(simulationDir == null) {\r
+                       File modelsDir = Activator.getBundleContext().getDataFile("models");\r
+                       String configName = configuration.getName();\r
+                       List<String> files = Arrays.asList(modelsDir.list());\r
+                       if (files.contains(configName)) {\r
+                               int i = 2;\r
+                               while (files.contains(configName + "_" + i)){\r
+                                       i++;\r
+                               }\r
+                               configName += "_" + i;\r
+                       }\r
+\r
+                       simulationDir  = Activator.getBundleContext().getDataFile("models/" + configName);\r
+                       if (!simulationDir.exists()) {\r
+                               simulationDir.mkdir();\r
+                       }\r
+               }\r
                return simulationDir;\r
        }\r
 \r
index 6c2a07e46e8d2451a871af2ea616ff3f0d0dd6e5..efa5578c308a4ff8684811a32157dbfb478b0e93 100644 (file)
@@ -18,6 +18,7 @@ import org.simantics.db.Resource;
 import org.simantics.db.Session;\r
 import org.simantics.db.common.request.ReadRequest;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
 \r
 /**\r
  * Manages system dynamic models.\r
@@ -45,8 +46,9 @@ public class SysdynModelManager {
                                \r
                                @Override\r
                                public void run(ReadGraph graph) throws DatabaseException {\r
-                                   models.put(resource, new SysdynModel(graph, resource));\r
-                                   \r
+                                       SysdynModel model = new SysdynModel(graph, resource);\r
+                                   models.put(resource, model);\r
+                                   FunctionUtils.updateFunctionFilesForModel(graph, model);\r
                                }\r
                            });\r
                    } catch (DatabaseException e) {\r
@@ -69,6 +71,14 @@ public class SysdynModelManager {
                if(model == null) {\r
                    model = new SysdynModel(g, resource);\r
                    models.put(resource, model);\r
+                try {\r
+                       SimulationResource simu = SimulationResource.getInstance(g);\r
+                       Resource modelResource = g.getPossibleObject(model.getConfigurationResource(), simu.IsConfigurationOf);\r
+                       if(modelResource != null)\r
+                               FunctionUtils.updateFunctionFilesForModel(g, model);\r
+                               } catch (DatabaseException e) {\r
+                                       e.printStackTrace();\r
+                               }\r
                }\r
                return model;\r
        }\r