1 package org.simantics.modeling.mapping;
3 import java.util.ArrayList;
4 import java.util.Collection;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.NamedResource;
10 import org.simantics.db.common.utils.OrderedSetUtils;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.adapter.impl.DefaultCopyHandler;
13 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
14 import org.simantics.diagram.stubs.DiagramResource;
15 import org.simantics.layer0.Layer0;
16 import org.simantics.modeling.ModelingResources;
17 import org.simantics.structural.stubs.StructuralResource2;
21 * Copy handler for Component and it's Element.
23 * FIXME: Requires patched ModelTransferableGraphSourceRequest
25 * in classifyPredicates(), excluded resources must be filtered. Otherwise the code fails with :Diagram.hasInverse having no URI.
27 * Resource predicate = stm.getPredicate();
28 * Resource object = stm.getObject();
29 * if (exclusions.contains(object) || exclusions.contains(predicate))
33 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
36 public class ComponentAndElementCopyHandler extends DefaultCopyHandler{
38 protected boolean includeComponent = true;
39 protected boolean includeElement = true;
41 public ComponentAndElementCopyHandler(Resource resource) {
47 * @param resource Component, if component is included. Otherwise the Element.
48 * @param includeComponent
49 * @param includeElement
51 public ComponentAndElementCopyHandler(Resource resource, boolean includeComponent, boolean includeElement) {
53 if (!includeComponent && !includeElement)
54 throw new IllegalArgumentException();
55 this.includeComponent = includeComponent;
56 this.includeElement = includeElement;
63 protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {
64 ModelingResources MR = ModelingResources.getInstance(graph);
66 ArrayList<Resource> exclusions = new ArrayList<Resource>();
68 ArrayList<NamedResource> roots = new ArrayList<NamedResource>();
70 Resource component = null;
71 Resource element = null;
73 if (includeComponent) {
74 component = getResource();
75 element = graph.getPossibleObject(component, MR.ComponentToElement);
77 element = getResource();
78 component = graph.getPossibleObject(element, MR.ElementToComponent);
80 // Include component as root
81 if (component != null) {
82 processComponent(graph, component, includeComponent, roots, exclusions);
85 if (element != null) {
86 processElement(graph, element, includeElement, roots, exclusions);
89 return TransferableGraphConfiguration2.createWithNames(graph, roots, exclusions, true, false);
92 protected void processComponent(ReadGraph graph, Resource component, boolean includeComponent, ArrayList<NamedResource> roots, ArrayList<Resource> exclusions) throws DatabaseException {
93 Layer0 L0 = Layer0.getInstance(graph);
94 StructuralResource2 SR = StructuralResource2.getInstance(graph);
95 Resource resource = getResource();
96 if (includeComponent) {
97 String rootName = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
98 roots.add(new NamedResource(rootName, resource));
100 exclusions.add(component);
103 // filter connections
104 for (Resource connection : graph.getObjects(resource, SR.IsConnectedTo))
105 exclusions.add(connection);
108 protected void processElement(ReadGraph graph, Resource element, boolean includeElement, ArrayList<NamedResource> roots, ArrayList<Resource> exclusions) throws DatabaseException {
109 Layer0 L0 = Layer0.getInstance(graph);
110 DiagramResource DIA = DiagramResource.getInstance(graph);
111 StructuralResource2 SR = StructuralResource2.getInstance(graph);
113 if (includeElement) {
114 // Add element as a root.
115 String name = graph.getRelatedValue(element, L0.HasName, Bindings.STRING);
116 roots.add(new NamedResource(name, element));
118 exclusions.add(element);
121 // exclude diagram from copied data.
122 Collection<Resource> diagram_ = OrderedSetUtils.getOwnerLists(graph, element, DIA.Diagram);
123 for (Resource diagram : diagram_) {
125 Resource inv = graph.getInverse(diagram);
126 exclusions.add(diagram);
129 // Exclude next and previous elements in the diagram
130 Resource next = OrderedSetUtils.next(graph, diagram, element);
131 Resource previous = OrderedSetUtils.prev(graph, diagram, element);
132 if (next != null && !next.equals(element))
133 exclusions.add(next);
134 if (previous != null && !previous.equals(element) && !previous.equals(next))
135 exclusions.add(previous);
137 // filter connections
138 for (Resource connection : graph.getObjects(element, SR.IsConnectedTo))
139 exclusions.add(connection);