1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.adapters;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.List;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.request.ObjectsWithType;
23 import org.simantics.db.common.request.PossibleIndexRoot;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.function.DbConsumer;
26 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
27 import org.simantics.db.layer0.adapter.impl.DefaultCopyHandler;
28 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
29 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec;
30 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec.SeedSpecType;
31 import org.simantics.diagram.stubs.DiagramResource;
32 import org.simantics.layer0.Layer0;
33 import org.simantics.modeling.ModelingResources;
34 import org.simantics.modeling.ModelingUtils.CompositeInfo;
35 import org.simantics.modeling.ModelingUtils.DiagramComponentInfo;
36 import org.simantics.structural.stubs.StructuralResource2;
39 * @author Tuukka Lehtonen
41 public class CompositeCopyHandler extends DefaultCopyHandler {
43 public CompositeCopyHandler(Resource composite) {
47 public CompositeCopyHandler(Collection<Resource> composites) {
52 protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {
54 Layer0 L0 = Layer0.getInstance(graph);
55 StructuralResource2 SR = StructuralResource2.getInstance(graph);
56 DiagramResource DIA = DiagramResource.getInstance(graph);
57 ModelingResources MOD = ModelingResources.getInstance(graph);
59 Set<Resource> resourceSet = (resources instanceof Set)
60 ? (Set<Resource>) resources : new HashSet<>(resources);
61 Set<Resource> exclusions = new HashSet<>();
62 Set<Resource> externals = new HashSet<>();
63 List<SeedSpec> roots = new ArrayList<>();
65 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resources.iterator().next()));
66 if(indexRoot == null) throw new DatabaseException("Composite is not part of any index root");
67 String indexRootUri = graph.getURI(indexRoot);
69 DbConsumer<Resource> identifierExcluder = r -> {
71 exclusions.addAll(graph.getObjects(r, L0.identifier));
74 for(Resource resource : resources) {
75 // Process all connection joins.
76 // This is the only way to access all of them.
77 for (Resource diagram : graph.getObjects(resource, MOD.CompositeToDiagram)) {
78 identifierExcluder.accept(diagram);
80 for (Resource element : graph.getObjects(diagram, L0.ConsistsOf)) {
81 identifierExcluder.accept(element);
83 Set<Resource> types = graph.getTypes(element);
85 // Check all diagram flag elements for necessary exclusions.
86 if (types.contains(DIA.Flag)) {
87 for (Resource join : graph.getObjects(element, DIA.FlagIsJoinedBy)) {
88 // Joins with external references are omitted
89 for (Resource comp : graph.getObjects(join, SR.JoinsComposite)) {
90 if (!resourceSet.contains(comp))
93 // This code excludes joins with flags to external
94 // diagrams that are not connected (have no
95 // configuration for the flag)
96 for (Resource flag2 : graph.getObjects(join, DIA.JoinsFlag)) {
97 Resource diagram2 = graph.getPossibleObject(flag2, L0.PartOf);
98 if (diagram2 != null) {
99 Resource comp = graph.getPossibleObject(diagram2, MOD.DiagramToComposite);
100 if (!resourceSet.contains(comp))
101 exclusions.add(join);
107 // Check all diagram monitor elements.
108 // Any components referenced that are external to the exported diagrams must be excluded from the export.
109 // This will leave the monitors without a monitored component but export and import will work anyway.
110 if (types.contains(DIA.Monitor)) {
111 for (Resource monitoredComponent : graph.getObjects(element, DIA.HasMonitorComponent)) {
112 Resource monitoredComponentComposite = graph.getPossibleObject(monitoredComponent, L0.PartOf);
113 if (monitoredComponentComposite != null && !resourceSet.contains(monitoredComponentComposite)) {
114 exclusions.add(monitoredComponent);
119 // Check all diagram reference elements for necessary exclusions.
120 if (types.contains(MOD.ReferenceElement)) {
121 for (Resource rel : graph.getObjects(element, L0.ConsistsOf)) {
122 identifierExcluder.accept(rel);
123 for (Resource invRel : graph.getObjects(rel, L0.ConsistsOf)) {
124 identifierExcluder.accept(invRel);
131 // Leave any diagram monitors out of the export that are contained
132 // by diagrams that are not part of this export, when the monitored
133 // components happen to be a part of the exported composites.
134 for (Resource ref : graph.syncRequest(new ObjectsWithType(resource, L0.ConsistsOf, SR.Component))) {
135 for (Resource monitor : graph.getObjects(ref, DIA.HasMonitorComponent_Inverse)) {
136 Resource monitorDiagram = graph.getPossibleObject(monitor, L0.PartOf);
137 if (monitorDiagram != null) {
138 Resource monitorComposite = graph.getPossibleObject(monitorDiagram, MOD.DiagramToComposite);
139 if (monitorComposite != null && !resourceSet.contains(monitorComposite))
140 exclusions.add(monitor);
145 // Include resource as root
146 CompositeInfo info = CompositeInfo.fromResource(graph, resource);
147 roots.add(new SeedSpec(resource, info.getTGName(), SeedSpecType.ROOT, typeId(graph, L0, indexRootUri, resource)));
148 identifierExcluder.accept(resource);
149 // Include components as roots
150 for (Resource child : graph.sync(new ObjectsWithType(resource, L0.ConsistsOf, SR.Component))) {
151 DiagramComponentInfo cinfo = DiagramComponentInfo.fromResource(graph, info, child);
152 identifierExcluder.accept(child);
153 roots.add(new SeedSpec(child, cinfo.getTGName(info), SeedSpecType.ROOT, typeId(graph, L0, indexRootUri, child)));
157 roots.add(new SeedSpec(indexRoot, "%model", SeedSpecType.SPECIAL_ROOT));
159 TransferableGraphConfiguration2 config = TransferableGraphConfiguration2.createWithNames2(graph, roots, exclusions, true, false);
160 for (Resource external : externals)
161 config.preStatus.put(external, ExtentStatus.EXTERNAL);
165 private static String typeId(ReadGraph graph, Layer0 L0, String indexRootUri, Resource r) throws DatabaseException {
166 Resource type = graph.getPossibleType(r, L0.Entity);
168 return Layer0.URIs.Entity;
169 String typeUri = graph.getPossibleURI(type);
170 if (typeUri == null || !typeUri.startsWith(indexRootUri))
172 return "%model" + typeUri.substring(indexRootUri.length());