]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Support copying of attributes from DN.MappedComponent
authorjsimomaa <jani.simomaa@gmail.com>
Fri, 12 Apr 2019 09:53:20 +0000 (12:53 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Sat, 31 Aug 2019 20:08:36 +0000 (23:08 +0300)
gitlab #43

Change-Id: I736cbe0454247aaaff51a553cf7a3f7778331f7f

org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/PasteDistrictVertexHandler.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java

index 633395d5fd22ddacd54cfc226a7bf99bafbb8a51..0ad64469460ae4682ed7ad0e81de9c379377e4ff 100644 (file)
@@ -1,8 +1,11 @@
 package org.simantics.district.network.ui.contributions;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 import javax.inject.Named;
 
@@ -20,11 +23,14 @@ import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.SelectionHints;
 import org.simantics.db.layer0.util.RemoverUtil;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.Variables;
 import org.simantics.db.request.Read;
 import org.simantics.diagram.stubs.DiagramResource;
 import org.simantics.district.network.DistrictNetworkUtil;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
 import org.simantics.layer0.Layer0;
+import org.simantics.modeling.ModelingResources;
 import org.simantics.utils.threads.ThreadUtils;
 import org.simantics.utils.ui.ISelectionUtils;
 import org.slf4j.Logger;
@@ -122,6 +128,8 @@ public class PasteDistrictVertexHandler {
         Resource sourceElement = copiedElements.get(0);
         
         try {
+            Map<Resource, Object> sourceCopyAttributes = new HashMap<>();
+            Map<Resource, Object> targetCopyAttributes = new HashMap<>();
             Simantics.getSession().syncRequest(new WriteRequest() {
                 
                 @Override
@@ -129,12 +137,53 @@ public class PasteDistrictVertexHandler {
                     DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
                     
                     Resource sourceMappedElement = graph.getPossibleObject(sourceElement, DN.MappedComponent);
+                    sourceCopyAttributes.putAll(copyAttributes(graph, sourceMappedElement));
                     Resource targetMappedElement = graph.getPossibleObject(targetElement, DN.MappedComponent);
+                    targetCopyAttributes.putAll(copyAttributes(graph, targetMappedElement));
                     if (sourceMappedElement != null && cut)
                         RemoverUtil.remove(graph, sourceMappedElement);
                     if (targetMappedElement != null)
                         RemoverUtil.remove(graph, targetMappedElement);
                 }
+                
+                private Map<Resource, Object> copyAttributes(WriteGraph graph, Resource mappedElement) throws DatabaseException {
+                    Layer0 L0 = Layer0.getInstance(graph);
+                    DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+
+                    Resource mapping = graph.getSingleObject(sourceElement, DN.HasMapping);
+                    Collection<Statement> statements = graph.getStatements(mapping, L0.HasProperty);
+                    Collection<Statement> mappingAttributeStatements = statements.stream().filter(stm -> {
+                        try {
+                               Resource predicate = stm.getPredicate();
+                               Resource hasDomain = graph.getPossibleObject(predicate, L0.HasDomain);
+                               if (hasDomain != null && hasDomain.equals(DN.Mapping_VertexMapping)) {
+                                       // filter out x and y and z
+                                       Resource vertexAttribute = attributeMappingToVertexAttribute(graph, DN, predicate);
+                                   return vertexAttribute != null;
+                               }
+                        } catch (Exception e) {
+                            e.printStackTrace();
+                        }
+                        return false;
+                    }).collect(Collectors.toList());
+                    Resource component = graph.getPossibleObject(mappedElement, ModelingResources.getInstance(graph).ElementToComponent);
+                    Variable variable = Variables.getVariable(graph, component);
+                    Map<Resource, Object> predValues = new HashMap<>();
+                    for (Statement stm : mappingAttributeStatements) {
+                        String propertyName = graph.getPossibleValue(stm.getObject());
+                        if (propertyName != null) {
+                            Object propertyValue = variable.getPossiblePropertyValue(graph, propertyName);
+                            if (propertyValue != null) {
+                                predValues.put(attributeMappingToVertexAttribute(graph, DN, stm.getPredicate()), propertyValue);
+                            } else {
+                                System.err.println("no property value for " + variable.getURI(graph) + " and property " + propertyName);
+                            }
+                        } else {
+                            System.err.println("stm.getObject() is not string " + stm.getObject());
+                        }
+                    }
+                    return predValues;
+                }
             });
             ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
                 try {
@@ -148,15 +197,20 @@ public class PasteDistrictVertexHandler {
                                                        Resource newTarget = doCopy(graph, diagram, targetElement, sourceElement);
                                                        if (cut) {
                                                                Resource newSource = doCopy(graph, diagram, sourceElement, targetElement);
+                                                               for (Map.Entry<Resource, Object> attrValue : targetCopyAttributes.entrySet()) {
+                                                                       graph.claimLiteral(newSource, attrValue.getKey(), attrValue.getValue());
+                                                               }
                                                                copyVertexInverses(graph, sourceElement, newSource);
                                                                deleteExistingTarget(graph, sourceElement);
                                                        }
+                                                       for (Map.Entry<Resource, Object> attrValue : sourceCopyAttributes.entrySet()) {
+                                                               graph.claimLiteral(newTarget, attrValue.getKey(), attrValue.getValue());
+                                                       }
                                                        copyVertexInverses(graph, targetElement, newTarget);
                                                        deleteExistingTarget(graph, targetElement);
                                                }
                                        });
                                } catch (DatabaseException e) {
-                                       // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
             }, 500, TimeUnit.MILLISECONDS);
@@ -218,4 +272,59 @@ public class PasteDistrictVertexHandler {
         }
         
     }
+    
+    private static Resource attributeMappingToVertexAttribute(ReadGraph graph, DistrictNetworkResource DN, Resource attribute) throws DatabaseException {
+       Resource attr = null;
+        if (attribute.equals(DN.Mapping_VertexMapping_ElevationAttribute))
+            attr = DN.Vertex_HasElevation;
+        else if (attribute.equals(DN.Mapping_VertexMapping_AddressAttribute))
+               attr = DN.Vertex_HasAddress;
+        else if (attribute.equals(DN.Mapping_VertexMapping_DeltaPressureAttribute))
+               attr = DN.Vertex_HasDeltaPressure;
+        else if (attribute.equals(DN.Mapping_VertexMapping_DeltaTemperatureAttribute))
+               attr = DN.Vertex_HasDeltaTemperature;
+        else if (attribute.equals(DN.Mapping_VertexMapping_dpAttribute))
+               attr = DN.Vertex_HasDeltaPressure;
+        else if (attribute.equals(DN.Mapping_VertexMapping_dtAttribute))
+               attr = DN.Vertex_HasDeltaTemperature;
+        else if (attribute.equals(DN.Mapping_VertexMapping_FlowAreaAttribute))
+               attr = DN.Vertex_HasFlowArea;
+        else if (attribute.equals(DN.Mapping_VertexMapping_HeatLoadDsAttribute))
+               attr = DN.Vertex_HasHeatLoadDs;
+        else if (attribute.equals(DN.Mapping_VertexMapping_HeatPowerAttribute))
+               attr = DN.Vertex_HasHeatPower;
+        else if (attribute.equals(DN.Mapping_VertexMapping_MassFlowAttribute))
+               attr = DN.Vertex_HasMassFlow;
+        else if (attribute.equals(DN.Mapping_VertexMapping_MaximumHeadMAttribute))
+               attr = DN.Vertex_HasMaximumHeadM;
+        else if (attribute.equals(DN.Mapping_VertexMapping_NominalFlowAttribute))
+               attr = DN.Vertex_HasNominalFlow;
+        else if (attribute.equals(DN.Mapping_VertexMapping_NominalHeadBAttribute))
+               attr = DN.Vertex_HasNominalHeadB_Inverse;
+        else if (attribute.equals(DN.Mapping_VertexMapping_NominalHeadMAttribute))
+               attr = DN.Vertex_HasNominalHeadM;
+        else if (attribute.equals(DN.Mapping_VertexMapping_NominalMassFlowAttribute))
+               attr = DN.Vertex_HasNominalFlow;
+        else if (attribute.equals(DN.Mapping_VertexMapping_NominalPressureLossAttribute))
+               attr = DN.Vertex_HasNominalPressureLoss;
+        else if (attribute.equals(DN.Mapping_VertexMapping_ReturnPressureAttribute))
+               attr = DN.Vertex_HasReturnPressure;
+        else if (attribute.equals(DN.Mapping_VertexMapping_ReturnTemperatureAttribute))
+               attr = DN.Vertex_HasReturnTemperature;
+        else if (attribute.equals(DN.Mapping_VertexMapping_SupplyPressureAttribute))
+               attr = DN.Vertex_HasSupplyPressure;
+        else if (attribute.equals(DN.Mapping_VertexMapping_SupplyTemperatureAttribute))
+               attr = DN.Vertex_HasSupplyTemperature;
+        else if (attribute.equals(DN.Mapping_VertexMapping_ValvePositionAttribute))
+               attr = DN.Vertex_HasValvePosition;
+        else if (attribute.equals(DN.Mapping_VertexMapping_VelocityAttribute))
+               attr = DN.Vertex_HasVelocity;
+        else if (attribute.equals(DN.Mapping_VertexMapping_VolFlowAttribute))
+               attr = DN.Vertex_HasVolFlow;
+        else if (attribute.equals(DN.Mapping_VertexMapping_XAttribute))
+               attr = null; // ignore this!
+        else if (attribute.equals(DN.Mapping_VertexMapping_YAttribute))
+               attr = null; // ignore this!
+        return attr;
+    }
 }
index 00728e894e5f64c398c57ec3e86b953bff5bd81c..b94639e2311080c9881044f0228a4c06e14bbc6d 100644 (file)
@@ -55,8 +55,6 @@ public class NetworkDrawingNode extends G2DNode {
 
     private Resource diagramResource;
 
-    private boolean committed;
-
     private NetworkDrawingParticipant participant;
 
     private IDiagram diagram;
@@ -67,6 +65,7 @@ public class NetworkDrawingNode extends G2DNode {
             4.0f, new float[]{4.0f}, 0.0f);
 
     private static final Color BLUE_ALPHA = new Color(0, 0, 255, 100);
+    private static final Color RED_ALPHA = new Color(255, 0, 0, 100);
 
     private boolean scaleStroke = true;
     
@@ -123,8 +122,13 @@ public class NetworkDrawingNode extends G2DNode {
             }
             
             g2d.setColor(BLUE_ALPHA);
-
             g2d.draw(path);
+            
+            g2d.setColor(RED_ALPHA);
+            BasicStroke stroke = GeometryUtils.scaleStroke(DASHED_STROKE, (float) (1.0 / GeometryUtils.getScale(g2d.getTransform())));
+            g2d.setStroke(stroke);
+            Point2D currentPoint = path.getCurrentPoint();
+            g2d.draw(new Rectangle2D.Double(currentPoint.getX() - 0.0001 / 2, currentPoint.getY() - 0.0001 / 2, 0.0001, 0.0001));
         }
         
         g2d.setStroke(oldStroke);
@@ -232,10 +236,6 @@ public class NetworkDrawingNode extends G2DNode {
         // check ToolMode
         IToolMode mode = getToolMode();
         if (mode == Hints.CONNECTTOOL || e.hasAnyModifier(MouseEvent.ALT_MASK | MouseEvent.ALT_GRAPH_MASK)) {
-            if (committed) {
-                committed = false;
-                return false;
-            }
             if (e.button == MouseEvent.RIGHT_BUTTON && !nodes.isEmpty()) {
                 nodes.remove(nodes.size() - 1);
             } else if (e.button == MouseEvent.LEFT_BUTTON) {
@@ -254,7 +254,6 @@ public class NetworkDrawingNode extends G2DNode {
                     }
                     currentRouteNode = null;
                     nodes.clear();
-                    committed = true;
                 } else if (currentRouteNode != null) {
                     currentRouteNode.routeNodes.add(new Point2D.Double(localPos.getX(), localPos.getY()));
                 }