1 package org.simantics.modeling.mapping;
3 import java.util.HashMap;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.WriteOnlyGraph;
11 import org.simantics.db.common.request.FreshEscapedName;
12 import org.simantics.db.common.utils.OrderedSetUtils;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.request.PossibleModel;
15 import org.simantics.diagram.stubs.DiagramResource;
16 import org.simantics.diagram.stubs.G2DResource;
17 import org.simantics.graph.db.IImportAdvisor;
18 import org.simantics.graph.representation.Root;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.modeling.ModelingResources;
23 * ImportAdvisor for handling Component and Element copies.
25 * The advisor can be configured to copy just component or element.
27 * When copying a component, handling the element many vary:
28 * 1. If copied data does not contain element, but the target composite is attached to a diagram, the advisor creates the element.
29 * 2. If copied data contains element, but target composite is not attached to a diagram, the advisor deletes the element.
32 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
35 public class ComponentAndElementPasteImportAdvisor implements IImportAdvisor {
37 private static boolean DEBUG = false;
39 protected Resource element;
40 protected Resource component;
42 protected Root elementRoot;
43 protected Root componentRoot;
45 protected Resource composite;
46 protected Resource diagram;
47 protected Resource model;
49 protected Map<String, String> nameMappings = new HashMap<String, String>();
51 protected boolean includeComponent = true;
52 protected boolean includeElement = true;
54 public ComponentAndElementPasteImportAdvisor(ReadGraph graph, Resource composite) throws DatabaseException{
55 this.composite = composite;
56 this.diagram = graph.getPossibleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram);
57 this.includeElement = diagram != null;
58 this.model = graph.sync(new PossibleModel(composite));
61 public ComponentAndElementPasteImportAdvisor(ReadGraph graph, Resource resource, boolean includeComponent, boolean includeElement) throws DatabaseException{
62 if (!includeComponent && !includeElement)
63 throw new IllegalArgumentException();
65 this.includeComponent = includeComponent;
66 this.includeElement = includeElement;
67 if (includeComponent) {
68 this.composite = resource;
69 this.diagram = graph.getPossibleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram);
70 if (this.diagram == null)
71 this.includeElement = false;
73 this.diagram = resource;
74 this.composite = graph.getPossibleObject(diagram, ModelingResources.getInstance(graph).DiagramToComposite);
80 public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
82 if("%model".equals(root.name)) return model;
84 DiagramResource DIA = DiagramResource.getInstance(graph);
85 String type = root.type;
87 Resource typeRes = graph.getResource(type);
88 if (graph.isInheritedFrom(typeRes, DIA.Element)) {
90 String newName = root.name;
92 newName = newName(graph, diagram, root.name);
93 nameMappings.put(root.name, newName);
96 String newName = newName(graph, composite, root.name);
97 nameMappings.put(root.name, newName);
104 public String newName(ReadGraph graph, Resource library, String name) throws DatabaseException {
105 return graph.syncRequest(new FreshEscapedName(library, Layer0.getInstance(graph).ConsistsOf, name));
108 public Resource getComponent() {
112 public Resource getElement() {
117 public Resource createRoot(WriteOnlyGraph graph, Root root)
118 throws DatabaseException {
119 Layer0 l0 = graph.getService(Layer0.class);
120 if (root == elementRoot) {
121 element = graph.newResource();
122 String name = root.name;
123 String newName = nameMappings.get(name);
124 graph.addLiteral(element, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);
125 if (DEBUG) System.out.println("CaECopyAdvisor created element " + element + " " + newName);
127 } else if (root == componentRoot) {
128 component = graph.newResource();
129 String name = root.name;
130 String newName = nameMappings.get(name);
131 graph.addLiteral(component, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);
132 if (DEBUG) System.out.println("CaECopyAdvisor created component " + component + " " + newName);
135 throw new DatabaseException("Unknown root " + root);
139 * Attaches pasted component / element to composite / diagram
141 * @throws DatabaseException
143 public void attach(WriteGraph graph) throws DatabaseException{
144 Layer0 l0 = Layer0.getInstance(graph);
145 if (includeComponent) {
146 if (component != null)
147 graph.claim(composite, l0.ConsistsOf, l0.PartOf, component);
149 if (component != null)
150 graph.deny(component);
152 if (!includeElement) {
153 // paste target does not need the element.
154 if (element != null) {
158 // paste target needs the element.
159 boolean created = false;
160 if (element == null) {
161 // element was not in the copied data, we must create a new one.
163 ModelingResources MR = ModelingResources.getInstance(graph);
164 DiagramResource DIA = DiagramResource.getInstance(graph);
165 G2DResource G2D = G2DResource.getInstance(graph);
167 Resource componentType = graph.getSingleType(component);
168 Resource elementType = graph.getSingleObject(componentType, MR.ComponentTypeToSymbol);
170 element = graph.newResource();
172 String newName = newName(graph, diagram, "element");
173 graph.claimLiteral(element, l0.HasName, newName);
175 graph.claim(element, l0.InstanceOf, elementType);
176 graph.claim(component, MR.ComponentToElement, element);
178 graph.claimLiteral(element, DIA.HasTransform, G2D.Transform, new double[]{1,0,0,1,0,0});
180 if (DEBUG) System.out.println("CaECopyAdvisor created deferred element " + element + " " + newName);
184 graph.claim(diagram, l0.ConsistsOf, l0.PartOf, element);
185 OrderedSetUtils.add(graph, diagram, element);
187 createElement(graph, element);
194 * Callback called, if copied data did not contain element, and the advisor had to create the element by itself.
196 * Note: default implementation does nothing.
200 * @throws DatabaseException
202 public void createElement(WriteGraph graph, Resource element) throws DatabaseException{