X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fadapters%2FCompositeCopyHandler.java;h=8c91852cdd83b143189290654b0fa814f4b7fa79;hb=4e402f51b0e34d40c3f9d33a4bec7feffeb8d339;hp=144448d9cff358a9e26a78126a278a04cf9b80a1;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/CompositeCopyHandler.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/CompositeCopyHandler.java index 144448d9c..8c91852cd 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/CompositeCopyHandler.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/CompositeCopyHandler.java @@ -22,10 +22,12 @@ import org.simantics.db.Resource; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.request.PossibleIndexRoot; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.function.DbConsumer; import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus; import org.simantics.db.layer0.adapter.impl.DefaultCopyHandler; import org.simantics.db.layer0.util.TransferableGraphConfiguration2; -import org.simantics.db.layer0.util.TransferableGraphConfiguration2.RootSpec; +import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec; +import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec.SeedSpecType; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; @@ -58,41 +60,69 @@ public class CompositeCopyHandler extends DefaultCopyHandler { ? (Set) resources : new HashSet<>(resources); Set exclusions = new HashSet<>(); Set externals = new HashSet<>(); - List roots = new ArrayList<>(); + List roots = new ArrayList<>(); + + Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resources.iterator().next())); + if(indexRoot == null) throw new DatabaseException("Composite is not part of any index root"); + String indexRootUri = graph.getURI(indexRoot); + + DbConsumer identifierExcluder = r -> { + if (r != null) + exclusions.addAll(graph.getObjects(r, L0.identifier)); + }; for(Resource resource : resources) { // Process all connection joins. // This is the only way to access all of them. for (Resource diagram : graph.getObjects(resource, MOD.CompositeToDiagram)) { - for (Resource flag : graph.syncRequest(new ObjectsWithType(diagram, L0.ConsistsOf, DIA.Flag))) { - for (Resource join : graph.getObjects(flag, DIA.FlagIsJoinedBy)) { - // Joins with external references are omitted - for (Resource comp : graph.getObjects(join, SR.JoinsComposite)) { - if (!resourceSet.contains(comp)) - exclusions.add(join); - } - // This code excludes joins with flags to external - // diagrams that are not connected (have no - // configuration for the flag) - for (Resource flag2 : graph.getObjects(join, DIA.JoinsFlag)) { - Resource diagram2 = graph.getPossibleObject(flag2, L0.PartOf); - if (diagram2 != null) { - Resource comp = graph.getPossibleObject(diagram2, MOD.DiagramToComposite); + identifierExcluder.accept(diagram); + + for (Resource element : graph.getObjects(diagram, L0.ConsistsOf)) { + identifierExcluder.accept(element); + + Set types = graph.getTypes(element); + + // Check all diagram flag elements for necessary exclusions. + if (types.contains(DIA.Flag)) { + for (Resource join : graph.getObjects(element, DIA.FlagIsJoinedBy)) { + // Joins with external references are omitted + for (Resource comp : graph.getObjects(join, SR.JoinsComposite)) { if (!resourceSet.contains(comp)) - exclusions.add(join); + exclusions.add(join); + } + // This code excludes joins with flags to external + // diagrams that are not connected (have no + // configuration for the flag) + for (Resource flag2 : graph.getObjects(join, DIA.JoinsFlag)) { + Resource diagram2 = graph.getPossibleObject(flag2, L0.PartOf); + if (diagram2 != null) { + Resource comp = graph.getPossibleObject(diagram2, MOD.DiagramToComposite); + if (!resourceSet.contains(comp)) + exclusions.add(join); + } } } } - } - // Check all diagram monitor elements. - // Any components referenced that are external to the exported diagrams must be excluded from the export. - // This will leave the monitors without a monitored component but export and import will work anyway. - for (Resource ref : graph.syncRequest(new ObjectsWithType(diagram, L0.ConsistsOf, DIA.Monitor))) { - for (Resource monitoredComponent : graph.getObjects(ref, DIA.HasMonitorComponent)) { - Resource monitoredComponentComposite = graph.getPossibleObject(monitoredComponent, L0.PartOf); - if (monitoredComponentComposite != null && !resourceSet.contains(monitoredComponentComposite)) { - exclusions.add(monitoredComponent); + // Check all diagram monitor elements. + // Any components referenced that are external to the exported diagrams must be excluded from the export. + // This will leave the monitors without a monitored component but export and import will work anyway. + if (types.contains(DIA.Monitor)) { + for (Resource monitoredComponent : graph.getObjects(element, DIA.HasMonitorComponent)) { + Resource monitoredComponentComposite = graph.getPossibleObject(monitoredComponent, L0.PartOf); + if (monitoredComponentComposite != null && !resourceSet.contains(monitoredComponentComposite)) { + exclusions.add(monitoredComponent); + } + } + } + + // Check all diagram reference elements for necessary exclusions. + if (types.contains(MOD.ReferenceElement)) { + for (Resource rel : graph.getObjects(element, L0.ConsistsOf)) { + identifierExcluder.accept(rel); + for (Resource invRel : graph.getObjects(rel, L0.ConsistsOf)) { + identifierExcluder.accept(invRel); + } } } } @@ -113,28 +143,18 @@ public class CompositeCopyHandler extends DefaultCopyHandler { } // Include resource as root -// GUID rootId = graph.getRelatedValue(resource, L0.identifier, GUID.BINDING); -// String rootName = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING); -// String escapedRootName = URIStringUtils.escape(rootName); -// String escapedPath = ModelingUtils.getDiagramCompositePath(graph, resource); CompositeInfo info = CompositeInfo.fromResource(graph, resource); - roots.add(new RootSpec(resource, info.getTGName(), true)); - Resource id = graph.getPossibleObject(resource, L0.identifier); - if(id != null) exclusions.add(id); + roots.add(new SeedSpec(resource, info.getTGName(), SeedSpecType.ROOT, typeId(graph, L0, indexRootUri, resource))); + identifierExcluder.accept(resource); // Include components as roots - for(Resource child : graph.sync(new ObjectsWithType(resource, L0.ConsistsOf, SR.Component))) { - DiagramComponentInfo cinfo = DiagramComponentInfo.fromResource(graph, info, child); -// GUID childId = graph.getRelatedValue(resource, L0.identifier, GUID.BINDING); -// String childName = graph.getRelatedValue(child, L0.HasName, Bindings.STRING); - id = graph.getPossibleObject(child, L0.identifier); - if(id != null) exclusions.add(id); - roots.add(new RootSpec(child, cinfo.getTGName(info), true)); + for (Resource child : graph.sync(new ObjectsWithType(resource, L0.ConsistsOf, SR.Component))) { + DiagramComponentInfo cinfo = DiagramComponentInfo.fromResource(graph, info, child); + identifierExcluder.accept(child); + roots.add(new SeedSpec(child, cinfo.getTGName(info), SeedSpecType.ROOT, typeId(graph, L0, indexRootUri, child))); } } - Resource model = graph.syncRequest(new PossibleIndexRoot(resources.iterator().next())); - if(model == null) throw new DatabaseException("Composite is not part of any index root"); - roots.add(new RootSpec(model, "%model", false)); + roots.add(new SeedSpec(indexRoot, "%model", SeedSpecType.SPECIAL_ROOT)); TransferableGraphConfiguration2 config = TransferableGraphConfiguration2.createWithNames2(graph, roots, exclusions, true, false); for (Resource external : externals) @@ -142,4 +162,14 @@ public class CompositeCopyHandler extends DefaultCopyHandler { return config; } + private static String typeId(ReadGraph graph, Layer0 L0, String indexRootUri, Resource r) throws DatabaseException { + Resource type = graph.getPossibleType(r, L0.Entity); + if (type == null) + return Layer0.URIs.Entity; + String typeUri = graph.getPossibleURI(type); + if (typeUri == null || !typeUri.startsWith(indexRootUri)) + return typeUri; + return "%model" + typeUri.substring(indexRootUri.length()); + } + }