package org.simantics.district.network.ui.contributions;
import java.util.Collection;
+import java.util.HashMap;
import java.util.List;
-import java.util.concurrent.ForkJoinPool;
+import java.util.Map;
import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
import javax.inject.Named;
import org.simantics.databoard.Bindings;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
+import org.simantics.db.Statement;
import org.simantics.db.WriteGraph;
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;
return false;
}
}
+
+ private static Resource doCopy(WriteGraph graph, Resource diagram, Resource target, Resource source) throws DatabaseException {
+ DiagramResource DIA = DiagramResource.getInstance(graph);
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ double[] location = graph.getRelatedValue(target, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
+ double elevation = graph.getRelatedValue(target, DN.Vertex_HasElevation, Bindings.DOUBLE);
+ Resource vertex = DistrictNetworkUtil.createVertex(graph, diagram, location, elevation);
+ copySourceToTarget(graph, source, vertex);
+ return vertex;
+ }
+
+ private static void copySourceToTarget(WriteGraph graph, Resource source, Resource target) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ graph.deny(target, DN.HasMapping);
+
+ Collection<Statement> statements = graph.getStatements(source, L0.HasProperty);
+ Collection<Statement> assertedStatements = graph.getAssertedStatements(source, L0.HasProperty);
+ statements.removeAll(assertedStatements);
+ for (Statement stm : statements) {
+ if (!graph.hasStatement(target, stm.getPredicate())) {
+ graph.claim(target, stm.getPredicate(), stm.getObject());
+ }
+ }
+ }
+ private static void copyVertexInverses(WriteGraph graph, Resource sourceElement, Resource targetElement) throws DatabaseException {
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ Collection<Resource> sourceStartVertex_inverse = graph.getObjects(sourceElement, DN.HasStartVertex_Inverse);
+ Collection<Resource> sourceEndVertex_inverse = graph.getObjects(sourceElement, DN.HasEndVertex_Inverse);
+ Collection<Resource> targetStartVertex_inverse = graph.getObjects(targetElement, DN.HasStartVertex_Inverse);
+ Collection<Resource> targetEndVertex_inverse = graph.getObjects(targetElement, DN.HasEndVertex_Inverse);
+ graph.deny(sourceElement, DN.HasStartVertex_Inverse);
+ graph.deny(sourceElement, DN.HasEndVertex_Inverse);
+ graph.deny(targetElement, DN.HasStartVertex_Inverse);
+ graph.deny(targetElement, DN.HasEndVertex_Inverse);
+ for (Resource startVertexInverse : sourceStartVertex_inverse) {
+ graph.claim(targetElement, DN.HasStartVertex_Inverse, startVertexInverse);
+ }
+ for (Resource targetVertexInverse : targetStartVertex_inverse) {
+ graph.claim(sourceElement, DN.HasStartVertex_Inverse, targetVertexInverse);
+ }
+ for (Resource endVertexInverse : sourceEndVertex_inverse) {
+ graph.claim(targetElement, DN.HasEndVertex_Inverse, endVertexInverse);
+ }
+ for (Resource targetVertexInverse : targetEndVertex_inverse) {
+ graph.claim(sourceElement, DN.HasEndVertex_Inverse, targetVertexInverse);
+ }
+ }
+
+ private static void deleteExistingTarget(WriteGraph graph, Resource target) throws DatabaseException {
+ RemoverUtil.remove(graph, target);
+ }
+
@Execute
public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
final List<Resource> elements = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_MAIN, Resource.class);
Resource sourceElement = copiedElements.get(0);
try {
+ Map<Resource, Object> sourceCopyAttributes = new HashMap<>();
+ Map<Resource, Object> targetCopyAttributes = new HashMap<>();
Simantics.getSession().syncRequest(new WriteRequest() {
@Override
DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
Resource sourceMappedElement = graph.getPossibleObject(sourceElement, DN.MappedComponent);
+ sourceCopyAttributes.putAll(copyAttributes(graph, sourceElement, sourceMappedElement));
Resource targetMappedElement = graph.getPossibleObject(targetElement, DN.MappedComponent);
- if (sourceMappedElement != null)
+ targetCopyAttributes.putAll(copyAttributes(graph, targetElement, targetMappedElement));
+ if (sourceMappedElement != null && cut)
RemoverUtil.remove(graph, sourceMappedElement);
if (targetMappedElement != null)
RemoverUtil.remove(graph, targetMappedElement);
- ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
- Simantics.getSession().asyncRequest(new WriteRequest() {
-
- @Override
- public void perform(WriteGraph graph) throws DatabaseException {
- //copyMapping(graph, DN, sourceElement, targetElement);
- copyLocation(graph, DN, sourceElement, targetElement, cut);
- copyVertexInverses(graph, DN, sourceElement, targetElement, cut);
- copyElevation(graph, DN, sourceElement, targetElement, cut);
- }
- });
- }, 200, TimeUnit.MILLISECONDS);
- }
-
- private void copyMapping(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement) throws DatabaseException {
- Resource sourceMapping = graph.getSingleObject(sourceElement, DN.Mapping);
- Resource targetMapping = graph.getSingleObject(targetElement, DN.Mapping);
- if (cut) {
- graph.deny(sourceElement, DN.Mapping);
- graph.claim(sourceElement, DN.Mapping, targetMapping);
- }
- graph.deny(targetElement, DN.Mapping);
- graph.claim(targetElement, DN.Mapping, sourceMapping);
}
+ });
+ ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
+ try {
+ Simantics.getSession().syncRequest(new WriteRequest() {
- private void copyElevation(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
- double sourceElevation = graph.getRelatedValue(sourceElement, DN.Vertex_HasElevation, Bindings.DOUBLE);
- double targetElevation = graph.getRelatedValue(targetElement, DN.Vertex_HasElevation, Bindings.DOUBLE);
- if (cut) {
- graph.deny(sourceElement, DN.Vertex_HasElevation);
- graph.claimLiteral(sourceElement, DN.Vertex_HasElevation, targetElevation, Bindings.DOUBLE);
- }
- graph.deny(targetElement, DN.Vertex_HasElevation);
- graph.claimLiteral(targetElement, DN.Vertex_HasElevation, sourceElevation, Bindings.DOUBLE);
- }
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+
+ Resource diagram = graph.getSingleObject(sourceElement, L0.PartOf);
+ 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) {
+ e.printStackTrace();
+ }
+ }, 500, TimeUnit.MILLISECONDS);
+
+// ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
+// Simantics.getSession().asyncRequest(new WriteRequest() {
+//
+// @Override
+// public void perform(WriteGraph graph) throws DatabaseException {
+// DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+// //copyMapping(graph, DN, sourceElement, targetElement);
+// copyLocation(graph, DN, sourceElement, targetElement, cut);
+// copyVertexInverses(graph, DN, sourceElement, targetElement, cut);
+// copyElevation(graph, DN, sourceElement, targetElement, cut);
+// }
+//
+// private void copyMapping(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement) throws DatabaseException {
+// Resource sourceMapping = graph.getSingleObject(sourceElement, DN.Mapping);
+// Resource targetMapping = graph.getSingleObject(targetElement, DN.Mapping);
+// if (cut) {
+// graph.deny(sourceElement, DN.Mapping);
+// graph.claim(sourceElement, DN.Mapping, targetMapping);
+// }
+// graph.deny(targetElement, DN.Mapping);
+// graph.claim(targetElement, DN.Mapping, sourceMapping);
+// }
+//
+// private void copyElevation(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
+// double sourceElevation = graph.getRelatedValue(sourceElement, DN.Vertex_HasElevation, Bindings.DOUBLE);
+// double targetElevation = graph.getRelatedValue(targetElement, DN.Vertex_HasElevation, Bindings.DOUBLE);
+// if (cut) {
+// graph.deny(sourceElement, DN.Vertex_HasElevation);
+// graph.claimLiteral(sourceElement, DN.Vertex_HasElevation, targetElevation, Bindings.DOUBLE);
+// }
+// graph.deny(targetElement, DN.Vertex_HasElevation);
+// graph.claimLiteral(targetElement, DN.Vertex_HasElevation, sourceElevation, Bindings.DOUBLE);
+// }
+//
+// private void copyLocation(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
+// DiagramResource DIA = DiagramResource.getInstance(graph);
+// double[] sourceLocation = graph.getRelatedValue(sourceElement, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
+// double[] targetLocation = graph.getRelatedValue(targetElement, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
+// if (cut) {
+// graph.deny(sourceElement, DIA.HasLocation);
+// graph.claimLiteral(sourceElement, DIA.HasLocation, targetLocation, Bindings.DOUBLE_ARRAY);
+// }
+// graph.deny(targetElement, DIA.HasLocation);
+// graph.claimLiteral(targetElement, DIA.HasLocation, sourceLocation, Bindings.DOUBLE_ARRAY);
+// }
+//
+
+//
+//
+// });
+// }, 500, TimeUnit.MILLISECONDS);
- private void copyLocation(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
- DiagramResource DIA = DiagramResource.getInstance(graph);
- double[] sourceLocation = graph.getRelatedValue(sourceElement, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
- double[] targetLocation = graph.getRelatedValue(targetElement, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
- if (cut) {
- graph.deny(sourceElement, DIA.HasLocation);
- graph.claimLiteral(sourceElement, DIA.HasLocation, targetLocation, Bindings.DOUBLE_ARRAY);
- }
- graph.deny(targetElement, DIA.HasLocation);
- graph.claimLiteral(targetElement, DIA.HasLocation, sourceLocation, Bindings.DOUBLE_ARRAY);
- }
-
- private void copyVertexInverses(WriteGraph graph, DistrictNetworkResource DN, Resource sourceElement, Resource targetElement, boolean cut) throws DatabaseException {
- Collection<Resource> sourceStartVertex_inverse = graph.getObjects(sourceElement, DN.HasStartVertex_Inverse);
- Collection<Resource> sourceEndVertex_inverse = graph.getObjects(sourceElement, DN.HasEndVertex_Inverse);
- Collection<Resource> targetStartVertex_inverse = graph.getObjects(targetElement, DN.HasStartVertex_Inverse);
- Collection<Resource> targetEndVertex_inverse = graph.getObjects(targetElement, DN.HasEndVertex_Inverse);
- graph.deny(sourceElement, DN.HasStartVertex_Inverse);
- graph.deny(sourceElement, DN.HasEndVertex_Inverse);
- graph.deny(targetElement, DN.HasStartVertex_Inverse);
- graph.deny(targetElement, DN.HasEndVertex_Inverse);
- for (Resource startVertexInverse : sourceStartVertex_inverse) {
- graph.claim(targetElement, DN.HasStartVertex_Inverse, startVertexInverse);
- }
- for (Resource targetVertexInverse : targetStartVertex_inverse) {
- graph.claim(sourceElement, DN.HasStartVertex_Inverse, targetVertexInverse);
- }
- for (Resource endVertexInverse : sourceEndVertex_inverse) {
- graph.claim(targetElement, DN.HasEndVertex_Inverse, endVertexInverse);
- }
- for (Resource targetVertexInverse : targetEndVertex_inverse) {
- graph.claim(sourceElement, DN.HasEndVertex_Inverse, targetVertexInverse);
- }
- }
- });
} catch (DatabaseException e) {
e.printStackTrace();
}
}
+
+ private static Map<Resource, Object> copyAttributes(WriteGraph graph, Resource sourceElement, 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;
+ }
+
+ private static Resource attributeMappingToVertexAttribute(ReadGraph graph, DistrictNetworkResource DN, Resource attribute) throws DatabaseException {
+ Resource attr = null;
+ if (attribute.equals(DN.Mapping_VertexMapping_ElevationAttribute))
+ attr = null; // ignore elevation as well
+ 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_PumpInReturnLineAttribute))
+ attr = DN.Vertex_HasPumpInReturnLine;
+ else if (attribute.equals(DN.Mapping_VertexMapping_HeadPumpMaximumAttribute))
+ attr = DN.Vertex_HasHeadPumpMaximum;
+ else if (attribute.equals(DN.Mapping_VertexMapping_HeadPumpNominalAttribute))
+ attr = DN.Vertex_HasHeadPumpNominal;
+ else if (attribute.equals(DN.Mapping_VertexMapping_FrequencyConverterControlledAttribute))
+ attr = DN.Vertex_HasFrequencyConverterControlled;
+ else if (attribute.equals(DN.Mapping_VertexMapping_InternalValveMeasurementAttribute))
+ attr = DN.Vertex_HasInternalValveMeasurement;
+ else if (attribute.equals(DN.Mapping_VertexMapping_PumpMassFlowNominalAttribute))
+ attr = DN.Vertex_HasPumpMassFlowNominal;
+ else if (attribute.equals(DN.Mapping_VertexMapping_PumpMeMaxAttribute))
+ attr = DN.Vertex_HasPumpMeMax;
+ else if (attribute.equals(DN.Mapping_VertexMapping_PumpMeMinAttribute))
+ attr = DN.Vertex_HasPumpMeMin;
+ else if (attribute.equals(DN.Mapping_VertexMapping_PumpSpeedMaxAttribute))
+ attr = DN.Vertex_HasPumpSpeedMax;
+ else if (attribute.equals(DN.Mapping_VertexMapping_PumpSpeedMinAttribute))
+ attr = DN.Vertex_HasPumpSpeedMin;
+ else if (attribute.equals(DN.Mapping_VertexMapping_ValveReturnLineAttribute))
+ attr = DN.Vertex_HasValveReturnLine;
+ else if (attribute.equals(DN.Mapping_VertexMapping_ValveMeMaxAttribute))
+ attr = DN.Vertex_HasValveMeMax;
+ else if (attribute.equals(DN.Mapping_VertexMapping_ValveMeMinAttribute))
+ attr = DN.Vertex_HasValveMeMin;
+ else if (attribute.equals(DN.Mapping_VertexMapping_ValveMinPositionAttribute))
+ attr = DN.Vertex_HasValveMinPosition;
+ else if (attribute.equals(DN.Mapping_VertexMapping_ValveOutletModeAttribute))
+ attr = DN.Vertex_HasValveOutletMode;
+ else if (attribute.equals(DN.Mapping_VertexMapping_ValvePressLossNominalAttribute))
+ attr = DN.Vertex_HasValvePressLossNominal;
+ else if (attribute.equals(DN.Mapping_VertexMapping_OpeningTimeAttribute))
+ attr = DN.Vertex_HasOpeningTime;
+ 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;
+ }
}