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