]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelTransferableGraphSource.java
Clean up and support internal seed resources in tg export
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / ModelTransferableGraphSource.java
index 1d3cf61f54c6c6ad296b06a959f62dac66601537..95923c79946206805ef467211e5523c313c766ee 100644 (file)
@@ -27,8 +27,9 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.RuntimeDatabaseException;
 import org.simantics.db.exception.ValidationException;
 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
-import org.simantics.db.layer0.util.ConsistsOfProcess.InternalEntry;
-import org.simantics.db.layer0.util.TransferableGraphConfiguration2.RootSpec;
+import org.simantics.db.layer0.util.ConsistsOfProcess.ConsistsOfProcessEntry;
+import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec;
+import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec.SeedSpecType;
 import org.simantics.db.service.SerialisationSupport;
 import org.simantics.graph.db.TransferableGraphSource;
 import org.simantics.graph.representation.External;
@@ -68,34 +69,34 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                SerialisationSupport ss = graph.getService(SerialisationSupport.class);
 
                // At this point ids contains all internal resources. Now add roots and externals.
-               
+
                // Root Library
                state.ids.put(ss.getTransientId(graph.getRootLibrary()), state.id++);
-               
+
                // External roots - internal roots were already processed as internal resources by domain processor
-               for(RootSpec rs : configuration.roots) {
-                       if(!rs.internal) {
-                               int resourceId = ss.getTransientId(rs.resource);
+               for(SeedSpec spec : configuration.seeds) {
+                       if(SeedSpecType.SPECIAL_ROOT.equals(spec.specType)) {
+                               int resourceId = ss.getTransientId(spec.resource);
                                state.ids.put(resourceId, state.id++);
                                // The fixed roots have been seen as externals by domain processor. Now remove them from external set.
                                state.externals.remove(resourceId);
                        }
                }
-               
+
                this.externalBase = state.id;
 
                final Collection<String> errors = new HashSet<>();
 
                // All resource considered as not internal by domain processor. Can also contain roots.
                int[] externals = state.externals.toArray();
-               
+
                // Build up the state.externals, externalNames and externalParents
                for(int i=0;i<externals.length;i++) {
                        getId(graph, externals[i], errors);
                }
-               
+
                state.inverses.forEachEntry(new TIntIntProcedure() {
-                       
+
                        @Override
                        public boolean execute(int predicate, int inverse) {
                                try {
@@ -106,9 +107,9 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                                }
                                return true;
                        }
-                       
+
                });
-               
+
                if(!errors.isEmpty()) {
                        ArrayList<String> sorted = new ArrayList<>(errors);
                        Collections.sort(sorted);
@@ -120,15 +121,15 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                        }
                        throw new DatabaseException(message.toString());
                }
-               
+
                this.resourceCount = state.id;
-               
+
                state.extensions.put(ExternalDownloadBean.EXTENSION_KEY, new Variant(ExternalDownloadBean.BINDING, new ExternalDownloadBean(downloads)));
-               
+
        }
 
        int indent = 0;
-       
+
        public boolean validateExternal(Resource ext) {
                if(configuration.validate) {
                        ExtentStatus status = configuration.preStatus.get(ext);
@@ -139,14 +140,14 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                }
                return true;
        }
-       
+
        private Resource getResource(ReadGraph graph, int r) throws DatabaseException {
                SerialisationSupport ss = graph.getService(SerialisationSupport.class);
                return ss.getResource(r);
        }
-       
+
        final public int getExistingId(ReadGraph graph, int r) throws DatabaseException {
-               
+
                int ret = state.ids.get(r);
                if(ret != -1) {
                        return ret;
@@ -156,7 +157,7 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                }
 
        }
-       
+
        /*
         * 
         * @return -2 if r is not really external and the statement should be excluded
@@ -166,10 +167,10 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
 
 //             // First external is root library
 //             if(r == rootId) return internalCount;
-               
+
                SerialisationSupport ss = graph.getService(SerialisationSupport.class);
                Layer0 L0 = Layer0.getInstance(graph);
-               
+
                if(state.ids.containsKey(r)) {
                    int ret = state.ids.get(r);
                    if(ret == -1) {
@@ -185,7 +186,7 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                errors.add("Illegal reference to " + graph.getPossibleURI(getResource(graph, r)));
                return -2;
             }
-                       Collection<Resource> parents = graph.getObjects(res, L0.PartOf);                        
+                       Collection<Resource> parents = graph.getObjects(res, L0.PartOf);
                        if(parents.size() != 1) {
                                throw new ValidationException("Reference to external resource " 
                                                + NameUtils.getSafeName(graph, getResource(graph, r), true) + " without unique uri (" + parents.size() + " parents).");
@@ -215,27 +216,36 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                        return state.id++;
                }
        }
-       
+
        @Override
        public DataContainer getHeader() throws Exception {
            return null;
        }
-       
+
        @Override
        public int getResourceCount() {
                return resourceCount;
        }
-       
+
+       private int countRootSeeds() {
+               int result = 0;
+               for(SeedSpec spec : configuration.seeds) {
+                       if(SeedSpecType.INTERNAL.equals(spec.specType)) continue;
+                       result++;
+               }
+               return result;
+       }
+
        @Override
        public int getIdentityCount() {
-               return configuration.roots.size() + state.externals.size() + state.internalEntries.size() + 1;
+               return countRootSeeds() + state.externals.size() + state.internalEntries.size() + 1;
        }
-       
+
        @Override
        public int getStatementCount() {
                return state.statementCount;
        }
-       
+
        @Override
        public int getValueCount() {
                return state.valueCount;
@@ -243,18 +253,18 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
 
        @Override
        public void forStatements(ReadGraph graph, TransferableGraphSourceProcedure<int[]> procedure) throws Exception {
-               
+
                int[] value = new int[4];
                long length = state.otherStatementsInput.length();
                state.otherStatementsInput.position(0);
-               
+
                while(state.otherStatementsInput.position() < length && !state.monitor.isCanceled()) {
-                       
+
                        int s = state.otherStatementsInput.readInt();
                        int subjectId = state.ids.get(s);
-                       
+
                        boolean exclude = subjectId == -1;
-                       
+
                        int size = state.otherStatementsInput.readInt();
                        for(int i=0;i<size;i++) {
                                int p = state.otherStatementsInput.readInt();
@@ -265,7 +275,7 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                                        } else if(state.excludedShared.contains(o)) {
                                                System.err.println("excluding shared " + s + " " + p + " " + o);
                                        } else {
-                                               
+
                                                int objectId = getExistingId(graph, o);
                                                // The statement can be denied still
                                                if(objectId != -2) {
@@ -280,11 +290,11 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                                                        value[3] = objectId;
 
                                                        procedure.execute(value);
-                                                       
+
                                                } else {
                                                        System.err.println("Denied (" + NameUtils.getSafeName(graph, getResource(graph, s)) + ", " + NameUtils.getSafeName(graph, getResource(graph, p)) + "," + NameUtils.getSafeName(graph, getResource(graph, o)) + ")");
-                                               }                                               
-                                               
+                                               }
+
                                        }
                                } else {
                                        System.err.println("excluding shared " + s);
@@ -385,7 +395,7 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
 
        @Override
        public void forIdentities(ReadGraph graph, TransferableGraphSourceProcedure<Identity> procedure) throws Exception {
-               
+
                SerialisationSupport support = graph.getService(SerialisationSupport.class);
                Layer0 L0 = Layer0.getInstance(graph);
 
@@ -395,7 +405,8 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                TIntObjectMap<Identity> internalMap = new TIntObjectHashMap<>(100, 0.5f, Integer.MIN_VALUE);
 
                // Declare internal and external roots
-               for(RootSpec r : configuration.roots) {
+               for(SeedSpec r : configuration.seeds) {
+                       if(SeedSpecType.INTERNAL.equals(r.specType)) continue;
                        String typeId = r.type;
                        if (typeId == null) {
                                Resource type = graph.getPossibleType(r.resource, L0.Entity);
@@ -413,32 +424,44 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                        String name = externalNames.get(i);
                        procedure.execute(new Identity(externalBase + i, new External(parent,name)));
        }
-               
+
                if(state.internalEntries != null) {
-                       for(InternalEntry ie : state.internalEntries) {
-                           if(ie.parent != null) {
-                               if(ie.name != null) {
-                                   procedure.execute(resolveInternal(graph, support, ie, internalMap));
-                           } else {
-                               // In this case there is a child that has no HasName => this should be treated as a blank
-                               }
+                       for(ConsistsOfProcessEntry ie : state.internalEntries) {
+                               if(ie.parent != null) {
+                                       if(ie.name != null) {
+                                               procedure.execute(resolveInternal(graph, support, ie, internalMap));
+                                       } else {
+                                               // In this case there is a child that has no HasName => this should be treated as a blank
+                                       }
                                } else {
-                                       throw new DatabaseException("Invalid internal entry " + ie);
+                                       procedure.execute(resolveInternal(graph, support, ie, internalMap));
                                }
                        }
                }
-       
+
        }
-       
-       private Identity resolveInternal(ReadGraph graph, SerialisationSupport ss, InternalEntry entry, TIntObjectMap<Identity> internalMap) throws DatabaseException {
+
+       private Identity resolveInternal(ReadGraph graph, SerialisationSupport ss, ConsistsOfProcessEntry entry, TIntObjectMap<Identity> internalMap) throws DatabaseException {
                int id = state.ids.get(ss.getTransientId(entry.resource));
                Identity existing = internalMap.get(id);
                if(existing != null) return existing;
-               Identity parent = resolveInternal(graph, ss, entry.parent, internalMap);
-               Identity result = new Identity(id,
-                               new Internal(parent.resource, entry.name));
-               internalMap.put(id, result);
-               return result;
+
+               if(entry.parent == null) {
+                       Layer0 L0 = Layer0.getInstance(graph);
+                       Resource possibleParent = graph.getPossibleObject(entry.resource, L0.PartOf);
+                       if(possibleParent == null) throw new DatabaseException("Invalid root or internal parent path: " + entry.resource);
+                       int externalId = state.ids.get(ss.getTransientId(possibleParent));
+                       Identity result = new Identity(id,
+                                       new Internal(externalId, entry.name));
+                       internalMap.put(id, result);
+                       return result;
+               } else {
+                       Identity parent = resolveInternal(graph, ss, entry.parent, internalMap);
+                       Identity result = new Identity(id,
+                                       new Internal(parent.resource, entry.name));
+                       internalMap.put(id, result);
+                       return result;
+               }
        }
 
        @Override
@@ -467,7 +490,7 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
        public void reset() throws Exception {
            throw new UnsupportedOperationException();
        }
-       
+
        public long[] getResourceArray(ReadGraph graph) throws DatabaseException {
                final SerialisationSupport ss = graph.getService(SerialisationSupport.class);
                final long[] result = new long[state.ids.size()];
@@ -475,38 +498,38 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
 
                        @Override
                        public boolean execute(int a, int b) {
-                               
+
                                try {
                                        Resource r = ss.getResource(a);
                                        result[b] = r.getResourceId();
                                } catch (DatabaseException e) {
                                        e.printStackTrace();
                                }
-                               
+
                                return true;
-                               
+
                        }
                });
                return result;
        }
-       
+
        public DomainProcessorState getState() {
                return state;
        }
-       
+
        public void forResourceStatements(ReadGraph graph, TransferableGraphSourceProcedure<int[]> procedure) throws Exception {
-               
+
                int[] value = new int[4];
                long length = state.otherStatementsInput.length();
                state.otherStatementsInput.position(0);
-               
+
                while(state.otherStatementsInput.position() < length) {
-                       
+
                        int s = state.otherStatementsInput.readInt();
                        int subjectId = state.ids.get(s);
-                       
+
                        boolean exclude = subjectId == -1;
-                       
+
                        int size = state.otherStatementsInput.readInt();
                        for(int i=0;i<size;i++) {
                                int p = state.otherStatementsInput.readInt();
@@ -515,7 +538,7 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                                        if(state.excludedShared.contains(o)) {
                                                System.err.println("excluding shared " + s + " " + p + " " + o);
                                        } else {
-                                               
+
                                                int objectId = getExistingId(graph, o);
                                                // The statement can be denied still
                                                if(objectId != -2) {
@@ -530,11 +553,11 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                                                        value[3] = o;
 
                                                        procedure.execute(value);
-                                                       
+
                                                } else {
                                                        System.err.println("Denied (" + NameUtils.getSafeName(graph, getResource(graph, s)) + ", " + NameUtils.getSafeName(graph, getResource(graph, p)) + "," + NameUtils.getSafeName(graph, getResource(graph, o)) + ")");
                                                }
-                                               
+
                                        }
                                } else {
                                        System.err.println("excluding shared " + s);