1 package org.simantics.district.network.ui.contributions;
3 import java.util.Collection;
5 import java.util.concurrent.TimeUnit;
7 import javax.inject.Named;
9 import org.eclipse.e4.core.di.annotations.CanExecute;
10 import org.eclipse.e4.core.di.annotations.Execute;
11 import org.eclipse.e4.ui.services.IServiceConstants;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.simantics.Simantics;
14 import org.simantics.databoard.Bindings;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.Statement;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.SelectionHints;
22 import org.simantics.db.layer0.util.RemoverUtil;
23 import org.simantics.db.request.Read;
24 import org.simantics.diagram.stubs.DiagramResource;
25 import org.simantics.district.network.DistrictNetworkUtil;
26 import org.simantics.district.network.ontology.DistrictNetworkResource;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.utils.threads.ThreadUtils;
29 import org.simantics.utils.ui.ISelectionUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class PasteDistrictVertexHandler {
35 private static final Logger LOGGER = LoggerFactory.getLogger(PasteDistrictVertexHandler.class);
38 public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection) {
39 List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
40 if (elements.size() < 1)
43 return Simantics.getSession().syncRequest(new Read<Boolean>() {
46 public Boolean perform(ReadGraph graph) throws DatabaseException {
47 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
48 for (Resource selection : elements) {
49 if (!graph.isInstanceOf(selection, DN.Element)) {
56 } catch (DatabaseException e) {
57 LOGGER.error("Could not evaluate if mapping can be changed for selection {}", elements, e);
62 private static Resource doCopy(WriteGraph graph, Resource diagram, Resource target, Resource source) throws DatabaseException {
63 DiagramResource DIA = DiagramResource.getInstance(graph);
64 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
65 double[] location = graph.getRelatedValue(target, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
66 double elevation = graph.getRelatedValue(target, DN.Vertex_HasElevation, Bindings.DOUBLE);
67 Resource vertex = DistrictNetworkUtil.createVertex(graph, diagram, location, elevation);
68 copySourceToTarget(graph, source, vertex);
72 private static void copySourceToTarget(WriteGraph graph, Resource source, Resource target) throws DatabaseException {
73 Layer0 L0 = Layer0.getInstance(graph);
74 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
75 graph.deny(target, DN.HasMapping);
77 Collection<Statement> statements = graph.getStatements(source, L0.HasProperty);
78 Collection<Statement> assertedStatements = graph.getAssertedStatements(source, L0.HasProperty);
79 statements.removeAll(assertedStatements);
80 for (Statement stm : statements) {
81 if (!graph.hasStatement(target, stm.getPredicate())) {
82 graph.claim(target, stm.getPredicate(), stm.getObject());
87 private static void copyVertexInverses(WriteGraph graph, Resource sourceElement, Resource targetElement) throws DatabaseException {
88 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
89 Collection<Resource> sourceStartVertex_inverse = graph.getObjects(sourceElement, DN.HasStartVertex_Inverse);
90 Collection<Resource> sourceEndVertex_inverse = graph.getObjects(sourceElement, DN.HasEndVertex_Inverse);
91 Collection<Resource> targetStartVertex_inverse = graph.getObjects(targetElement, DN.HasStartVertex_Inverse);
92 Collection<Resource> targetEndVertex_inverse = graph.getObjects(targetElement, DN.HasEndVertex_Inverse);
93 graph.deny(sourceElement, DN.HasStartVertex_Inverse);
94 graph.deny(sourceElement, DN.HasEndVertex_Inverse);
95 graph.deny(targetElement, DN.HasStartVertex_Inverse);
96 graph.deny(targetElement, DN.HasEndVertex_Inverse);
97 for (Resource startVertexInverse : sourceStartVertex_inverse) {
98 graph.claim(targetElement, DN.HasStartVertex_Inverse, startVertexInverse);
100 for (Resource targetVertexInverse : targetStartVertex_inverse) {
101 graph.claim(sourceElement, DN.HasStartVertex_Inverse, targetVertexInverse);
103 for (Resource endVertexInverse : sourceEndVertex_inverse) {
104 graph.claim(targetElement, DN.HasEndVertex_Inverse, endVertexInverse);
106 for (Resource targetVertexInverse : targetEndVertex_inverse) {
107 graph.claim(sourceElement, DN.HasEndVertex_Inverse, targetVertexInverse);
111 private static void deleteExistingTarget(WriteGraph graph, Resource target) throws DatabaseException {
112 RemoverUtil.remove(graph, target);
116 public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
117 final List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
118 final List<Resource> copiedElements = CopyDistrictVertexHandler.elements; // TODO: this could be implemented more nicely with clipboard ?
119 boolean cut = CopyDistrictVertexHandler.cut;
121 Resource targetElement = elements.get(0);
122 Resource sourceElement = copiedElements.get(0);
125 Simantics.getSession().syncRequest(new WriteRequest() {
128 public void perform(WriteGraph graph) throws DatabaseException {
129 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
131 Resource sourceMappedElement = graph.getPossibleObject(sourceElement, DN.MappedComponent);
132 Resource targetMappedElement = graph.getPossibleObject(targetElement, DN.MappedComponent);
133 if (sourceMappedElement != null && cut)
134 RemoverUtil.remove(graph, sourceMappedElement);
135 if (targetMappedElement != null)
136 RemoverUtil.remove(graph, targetMappedElement);
139 ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
141 Simantics.getSession().syncRequest(new WriteRequest() {
144 public void perform(WriteGraph graph) throws DatabaseException {
145 Layer0 L0 = Layer0.getInstance(graph);
147 Resource diagram = graph.getSingleObject(sourceElement, L0.PartOf);
148 Resource newTarget = doCopy(graph, diagram, targetElement, sourceElement);
150 Resource newSource = doCopy(graph, diagram, sourceElement, targetElement);
151 copyVertexInverses(graph, sourceElement, newSource);
152 deleteExistingTarget(graph, sourceElement);
154 copyVertexInverses(graph, targetElement, newTarget);
155 deleteExistingTarget(graph, targetElement);
158 } catch (DatabaseException e) {
159 // TODO Auto-generated catch block
162 }, 500, TimeUnit.MILLISECONDS);
164 // ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
165 // Simantics.getSession().asyncRequest(new WriteRequest() {
168 // public void perform(WriteGraph graph) throws DatabaseException {
169 // DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
170 // //copyMapping(graph, DN, sourceElement, targetElement);
171 // copyLocation(graph, DN, sourceElement, targetElement, cut);
172 // copyVertexInverses(graph, DN, sourceElement, targetElement, cut);
173 // copyElevation(graph, DN, sourceElement, targetElement, cut);
176 // private void copyMapping(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement) throws DatabaseException {
177 // Resource sourceMapping = graph.getSingleObject(sourceElement, DN.Mapping);
178 // Resource targetMapping = graph.getSingleObject(targetElement, DN.Mapping);
180 // graph.deny(sourceElement, DN.Mapping);
181 // graph.claim(sourceElement, DN.Mapping, targetMapping);
183 // graph.deny(targetElement, DN.Mapping);
184 // graph.claim(targetElement, DN.Mapping, sourceMapping);
187 // private void copyElevation(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
188 // double sourceElevation = graph.getRelatedValue(sourceElement, DN.Vertex_HasElevation, Bindings.DOUBLE);
189 // double targetElevation = graph.getRelatedValue(targetElement, DN.Vertex_HasElevation, Bindings.DOUBLE);
191 // graph.deny(sourceElement, DN.Vertex_HasElevation);
192 // graph.claimLiteral(sourceElement, DN.Vertex_HasElevation, targetElevation, Bindings.DOUBLE);
194 // graph.deny(targetElement, DN.Vertex_HasElevation);
195 // graph.claimLiteral(targetElement, DN.Vertex_HasElevation, sourceElevation, Bindings.DOUBLE);
198 // private void copyLocation(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
199 // DiagramResource DIA = DiagramResource.getInstance(graph);
200 // double[] sourceLocation = graph.getRelatedValue(sourceElement, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
201 // double[] targetLocation = graph.getRelatedValue(targetElement, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
203 // graph.deny(sourceElement, DIA.HasLocation);
204 // graph.claimLiteral(sourceElement, DIA.HasLocation, targetLocation, Bindings.DOUBLE_ARRAY);
206 // graph.deny(targetElement, DIA.HasLocation);
207 // graph.claimLiteral(targetElement, DIA.HasLocation, sourceLocation, Bindings.DOUBLE_ARRAY);
214 // }, 500, TimeUnit.MILLISECONDS);
216 } catch (DatabaseException e) {