]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Support tg discovery in export/import 81/381/2
authorAntti Villberg <antti.villberg@semantum.fi>
Mon, 27 Mar 2017 08:17:25 +0000 (11:17 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 28 Mar 2017 14:27:58 +0000 (17:27 +0300)
refs #7103

Change-Id: I471f9f0d5fa2b1bb1b825d7fde69eabc57422799

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/MigrationUtils.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ExternalDownloadBean.java [new file with mode: 0644]
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelTransferableGraphSource.java
bundles/org.simantics.layer0/graph.tg
bundles/org.simantics.layer0/graph/Layer0.pgraph
bundles/org.simantics.layer0/src/org/simantics/layer0/Layer0.java
bundles/org.simantics.modeling/src/org/simantics/modeling/ModelingUtils.java

index 052bc6e6fb393beb39f2f0e1e4b93ef7852f9eb8..7af76baf330b23fb476da913f165d1f929f59a9a 100644 (file)
  *******************************************************************************/
 package org.simantics.db.layer0.migration;
 
+import java.io.DataInputStream;
 import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.simantics.databoard.Bindings;
+import org.simantics.databoard.adapter.AdaptException;
 import org.simantics.databoard.binding.mutable.Variant;
 import org.simantics.databoard.container.DataContainer;
+import org.simantics.databoard.container.DataContainers;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.WriteOnlyGraph;
 import org.simantics.db.common.CommentMetadata;
+import org.simantics.db.common.primitiverequest.PossibleResource;
 import org.simantics.db.common.request.BinaryRead;
 import org.simantics.db.common.request.FreshEscapedName;
 import org.simantics.db.common.request.UnaryRead;
@@ -42,6 +50,7 @@ import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
 import org.simantics.db.layer0.adapter.impl.SharedOntologyImportAdvisor;
 import org.simantics.db.layer0.adapter.impl.TrashBinRemover;
 import org.simantics.db.layer0.internal.SimanticsInternal;
+import org.simantics.db.layer0.util.ExternalDownloadBean;
 import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.db.layer0.util.TGTransferableGraphSource;
 import org.simantics.db.service.XSupport;
@@ -307,32 +316,39 @@ public class MigrationUtils {
        
        if(monitor == null) monitor = new NullProgressMonitor();
        
+       Variant edbVariant = tg.extensions.get(ExternalDownloadBean.EXTENSION_KEY);
+       if(edbVariant != null) {
+               try {
+                               ExternalDownloadBean edb = (ExternalDownloadBean)edbVariant.getValue(ExternalDownloadBean.BINDING);
+                               for(Map.Entry<String, String> entry : edb.downloads.entrySet()) {
+                                       String uri = entry.getKey();
+                                       Resource existing = session.syncRequest(new PossibleResource(uri));
+                                       if(existing == null) {
+                                               String download = entry.getValue();
+                                                URL url = new URL(download);
+                                                DataContainer container = DataContainers.readFile(new DataInputStream(url.openStream()));
+                                                TransferableGraph1 dependencyTg = (TransferableGraph1) container.content.getValue(TransferableGraph1.BINDING);
+                                                importSharedOntology(monitor, session, dependencyTg, true);
+                                       }
+                               }
+                       } catch (AdaptException e) {
+                               throw new DatabaseException(e);
+                       } catch (MalformedURLException e) {
+                               throw new DatabaseException(e);
+                       } catch (IOException e) {
+                               throw new DatabaseException(e);
+                       }
+               
+       }
+       
         Collection<Identity> roots = TransferableGraphUtils.getRoots(tg);
         if(roots.size() == 1) {
-//             Identity id = roots.iterator().next();
-//             final Root root = (Root)id.definition;
-//             Resource rootResource = session.syncRequest(new WriteResultRequest<Resource>() {
-//                             @Override
-//                             public Resource perform(WriteGraph graph) throws DatabaseException {
-//                                     Resource type = graph.getResource(root.type);
-//                                     Resource existing = graph.getPossibleResource(root.name);
-//                                     if(existing != null) throw new DatabaseException("Shared library " + root.name + " exists already.");
-//                                     return Layer0Utils.applySCL("Simantics/SharedOntologies", "createSharedOntology", graph, root.name, type);
-//                             }
-//             });
             try {
                
                 TGTransferableGraphSource tgSource = new TGTransferableGraphSource(tg);
                 SharedOntologyImportAdvisor advisor = new SharedOntologyImportAdvisor(published);
-//                TransferableGraphs.importGraph1(session, tgSource, advisor);
-                
-//                if (advisor.getRoots().size() == 1) {
-//                     return advisor.getRoots().iterator().next();
-//                }
-                               //TransferableGraphs.importGraph1(session, tg, new SharedOntologyImportAdvisor(), null);
 
                 MigrationState state = newState();
-                //state.setProperty(MigrationStateKeys.BASE_URI, AprosBuiltins.URIs.Migration);
                 state.setProperty(MigrationStateKeys.UPDATE_DEPENDENCIES, false);
                 state.setProperty(MigrationStateKeys.CURRENT_TGS, tgSource);
                 state.setProperty(MigrationStateKeys.SESSION, session);
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ExternalDownloadBean.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ExternalDownloadBean.java
new file mode 100644 (file)
index 0000000..6dd2b84
--- /dev/null
@@ -0,0 +1,20 @@
+package org.simantics.db.layer0.util;
+
+import java.util.TreeMap;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.binding.Binding;
+
+public class ExternalDownloadBean {
+
+       public static final String EXTENSION_KEY = ExternalDownloadBean.class.getSimpleName();
+       
+       public static final Binding BINDING = Bindings.getBindingUnchecked(ExternalDownloadBean.class);
+       
+       public TreeMap<String,String> downloads = new TreeMap<>();
+       
+       public ExternalDownloadBean(TreeMap<String,String> downloads) {
+               this.downloads = downloads;
+       }
+
+}
index 8602da82789d97de8830f1927dcd4b5d53c7295b..cbd3130eea6d737f2c431b685e47f6c306576399 100644 (file)
@@ -52,6 +52,7 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
 
        TIntArrayList externalParents = new TIntArrayList();
        ArrayList<String> externalNames = new ArrayList<String>();
+       TreeMap<String,String> downloads = new TreeMap<String,String>();
 
        public ModelTransferableGraphSource(final ReadGraph graph, TransferableGraphConfiguration2 configuration, final DomainProcessorState state, File ... fs) throws DatabaseException {
 
@@ -118,6 +119,8 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                
                this.resourceCount = state.id;
                
+               state.extensions.put(ExternalDownloadBean.EXTENSION_KEY, new Variant(ExternalDownloadBean.BINDING, new ExternalDownloadBean(downloads)));
+               
        }
 
        int indent = 0;
@@ -200,6 +203,11 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                        state.ids.put(r, state.id);
                        // Ensure that this resource is included into the set of externals to maintain the total number of externals 
                        state.externals.add(r);
+                       String download = graph.getPossibleRelatedValue(res, L0.Ontology_download, Bindings.STRING);
+                       if(download != null) {
+                               String uri = graph.getURI(res);
+                               downloads.put(uri, download);
+                       }
                        return state.id++;
                }
        }
index 931d67ed3fa59d49198af25f0c2b807b5612a2fe..37fc6470b1af1b90d96da5365c07f1787a10ea5d 100644 (file)
Binary files a/bundles/org.simantics.layer0/graph.tg and b/bundles/org.simantics.layer0/graph.tg differ
index 41f13c301b227fa2400d36bd65b9aa100a64017e..93d348f05f779dd022dff8f45a321d3fb7ff54cf 100644 (file)
@@ -107,7 +107,8 @@ L0.Ontology <T L0.Library <T L0.IndexRoot
     >-- L0.Ontology.global <R L0.HasProperty : L0.FunctionalRelation
         L0.HasLabel "Global?"
         --> L0.Boolean
-        
+    >-- L0.Ontology.download --> L0.String <R L0.HasProperty : L0.FunctionalRelation
+            
 L0.SharedOntology <T L0.Ontology
     >-- L0.SharedOntology.treatAsSystemOntology --> L0.Boolean <R L0.HasProperty : L0.FunctionalRelation
     @L0.assert L0.SharedOntology.treatAsSystemOntology false
index 752039a160f001cae7f95665acfd6f8169d8dd55..16b7159f0249eaf3d491af8c8932b67e39307e43 100644 (file)
@@ -192,6 +192,8 @@ public class Layer0 {
     public final Resource NamespaceMigrationStep_Prefix_to;
     public final Resource NamespaceMigrationStep_Prefix_to_Inverse;
     public final Resource Ontology;
+    public final Resource Ontology_download;
+    public final Resource Ontology_download_Inverse;
     public final Resource Ontology_global;
     public final Resource Ontology_global_Inverse;
     public final Resource OrderedSet;
@@ -482,6 +484,8 @@ public class Layer0 {
         public static final String NamespaceMigrationStep_Prefix_to = "http://www.simantics.org/Layer0-1.1/NamespaceMigrationStep/Prefix/to";
         public static final String NamespaceMigrationStep_Prefix_to_Inverse = "http://www.simantics.org/Layer0-1.1/NamespaceMigrationStep/Prefix/to/Inverse";
         public static final String Ontology = "http://www.simantics.org/Layer0-1.1/Ontology";
+        public static final String Ontology_download = "http://www.simantics.org/Layer0-1.1/Ontology/download";
+        public static final String Ontology_download_Inverse = "http://www.simantics.org/Layer0-1.1/Ontology/download/Inverse";
         public static final String Ontology_global = "http://www.simantics.org/Layer0-1.1/Ontology/global";
         public static final String Ontology_global_Inverse = "http://www.simantics.org/Layer0-1.1/Ontology/global/Inverse";
         public static final String OrderedSet = "http://www.simantics.org/Layer0-1.1/OrderedSet";
@@ -782,6 +786,8 @@ public class Layer0 {
         NamespaceMigrationStep_Prefix_to = getResourceOrNull(graph, URIs.NamespaceMigrationStep_Prefix_to);
         NamespaceMigrationStep_Prefix_to_Inverse = getResourceOrNull(graph, URIs.NamespaceMigrationStep_Prefix_to_Inverse);
         Ontology = getResourceOrNull(graph, URIs.Ontology);
+        Ontology_download = getResourceOrNull(graph, URIs.Ontology_download);
+        Ontology_download_Inverse = getResourceOrNull(graph, URIs.Ontology_download_Inverse);
         Ontology_global = getResourceOrNull(graph, URIs.Ontology_global);
         Ontology_global_Inverse = getResourceOrNull(graph, URIs.Ontology_global_Inverse);
         OrderedSet = getResourceOrNull(graph, URIs.OrderedSet);
index 78125828c58cc6628c572744d9394b56785698c2..3b4806c7ce927a78b46bdc60f8931b3bb6db535d 100644 (file)
@@ -101,6 +101,7 @@ import org.simantics.db.layer0.request.IsLinkedTo;
 import org.simantics.db.layer0.request.PossibleModel;
 import org.simantics.db.layer0.util.ClipboardUtils;
 import org.simantics.db.layer0.util.DraftStatusBean;
+import org.simantics.db.layer0.util.ExternalDownloadBean;
 import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
 import org.simantics.db.layer0.util.PasteEventHandler;
@@ -1497,6 +1498,11 @@ public class ModelingUtils {
                
             TransferableGraph1 tg = ClipboardUtils.accept(processor, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
             monitor.worked(95);
+            
+            Variant edb = tg.extensions.get(ExternalDownloadBean.EXTENSION_KEY);
+            if(edb != null) {
+               metadata.put(ExternalDownloadBean.EXTENSION_KEY, edb);
+            }
 
             monitor.setTaskName("Writing transferable graph...");
             DataContainers.writeFile(location, new DataContainer(