From: jsimomaa Date: Wed, 18 Dec 2019 09:11:52 +0000 (+0200) Subject: Add support for importing regions to district models X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=1a9e7423c31c56955bcf7d051d1c8e756d2d4cb7;p=simantics%2Fdistrict.git Add support for importing regions to district models gitlab #73 Change-Id: Icea73ce0f8aac0866dc0735a5756315e86e94439 (cherry picked from commit c98b9f92547d6e7d4cdf59e2f8f10b355300b7e9) --- diff --git a/org.simantics.district.imports.ui/META-INF/MANIFEST.MF b/org.simantics.district.imports.ui/META-INF/MANIFEST.MF index dc0819b8..49a37894 100644 --- a/org.simantics.district.imports.ui/META-INF/MANIFEST.MF +++ b/org.simantics.district.imports.ui/META-INF/MANIFEST.MF @@ -17,5 +17,6 @@ Require-Bundle: org.eclipse.ui.ide, org.simantics.modeling.ontology, org.simantics.district.network, org.simantics.district.network.ontology, - org.simantics.district.maps + org.simantics.district.maps, + org.slf4j.api Bundle-ActivationPolicy: lazy diff --git a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java index a4d29fe1..6162314e 100644 --- a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java +++ b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java @@ -46,6 +46,7 @@ public class CSVImportWizard extends Wizard implements IImportWizard { } else { DistrictImportUtils.importEdges(model); } + } catch (Exception e) { throw new InvocationTargetException(e); } diff --git a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardFirstPage.java b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardFirstPage.java index a0cc023c..eb0faef0 100644 --- a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardFirstPage.java +++ b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardFirstPage.java @@ -28,9 +28,13 @@ import org.simantics.modeling.ModelingResources; import org.simantics.utils.ui.widgets.FileOrDirectorySelectionWidget; import org.simantics.utils.ui.widgets.FileSelectionListener; import org.simantics.utils.ui.widgets.FileSelectionWidget; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class CSVImportWizardFirstPage extends WizardPage implements FileSelectionListener { + private static final Logger LOGGER = LoggerFactory.getLogger(CSVImportWizardFirstPage.class); + private final CSVImportModel model; private Map diagrams = new HashMap<>(); @@ -107,7 +111,7 @@ public class CSVImportWizardFirstPage extends WizardPage implements FileSelectio diagramCombo.getDisplay().asyncExec(() -> validateComplete()); } } catch (DatabaseException e) { - e.printStackTrace(); + LOGGER.error("Could not update available parents", e); } } diff --git a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardPage.java b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardPage.java index 268ed310..910ae428 100644 --- a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardPage.java +++ b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardPage.java @@ -68,6 +68,7 @@ public class CSVImportWizardPage extends WizardPage { private DynamicComboFieldEditor dtSelector; private DynamicComboFieldEditor heatPowerSelector; private DynamicComboFieldEditor peakPowerSelector; + private DynamicComboFieldEditor regionSelector; private DynamicComboFieldEditor nominalHeadMSelector; private DynamicComboFieldEditor nominalHeadBSelector; private DynamicComboFieldEditor nominalFlowSelector; @@ -343,7 +344,20 @@ public class CSVImportWizardPage extends WizardPage { validatePageComplete(); } }); - + regionSelector = new DynamicComboFieldEditor("regionValue", "Region", parent); + regionSelector.addComboListener(new SelectionListener() { + + @Override + public void widgetSelected(SelectionEvent e) { + widgetDefaultSelected(e); + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + model.setRegionIndex(Integer.parseInt(regionSelector.getValue())); + validatePageComplete(); + } + }); } private void createVertexIndexMappingField(Group parent) { @@ -518,6 +532,7 @@ public class CSVImportWizardPage extends WizardPage { validatePageComplete(); } }); + nominalHeadMSelector = new DynamicComboFieldEditor("nominalHeadMValue", "nominalHeadM", parent); nominalHeadMSelector.addComboListener(new SelectionListener() { diff --git a/org.simantics.district.imports/scl/Simantics/District/Import.scl b/org.simantics.district.imports/scl/Simantics/District/Import.scl index a3d36f4c..fc697d6f 100644 --- a/org.simantics.district.imports/scl/Simantics/District/Import.scl +++ b/org.simantics.district.imports/scl/Simantics/District/Import.scl @@ -38,6 +38,7 @@ importJava "org.simantics.district.imports.CSVImportModel" where // Common setSourceCRS :: CSVImportModel -> String -> () setIdIndex :: CSVImportModel -> Integer -> () + setRegionIndex :: CSVImportModel -> Integer -> () setAddressIndex :: CSVImportModel -> Integer -> () setComponentMappingIndex :: CSVImportModel -> Integer -> () setComponentMappings :: CSVImportModel -> String -> Resource -> () diff --git a/org.simantics.district.imports/src/org/simantics/district/imports/CSVImportModel.java b/org.simantics.district.imports/src/org/simantics/district/imports/CSVImportModel.java index 9f85225f..1acae8de 100644 --- a/org.simantics.district.imports/src/org/simantics/district/imports/CSVImportModel.java +++ b/org.simantics.district.imports/src/org/simantics/district/imports/CSVImportModel.java @@ -74,6 +74,7 @@ public class CSVImportModel { private int lengthIndex = -1; private int detailedGeometryIndex = -1; private int peakPowerIndex = -1; + private int regionIndex = -1; // Third page @@ -518,4 +519,12 @@ public class CSVImportModel { public int getPeakPowerIndex() { return peakPowerIndex; } + + public int getRegionIndex() { + return regionIndex; + } + + public void setRegionIndex(int regionIndex) { + this.regionIndex = regionIndex; + } } diff --git a/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java b/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java index 7e2a0e1e..083b58d5 100644 --- a/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java +++ b/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java @@ -256,6 +256,7 @@ public class DistrictImportUtils { int flowAreaIndex = model.getFlowAreaIndex(); int nominalPressureLossIndex = model.getNominalPressureLossIndex(); int addressIndex = model.getAddressIndex(); + int regionIndex = model.getRegionIndex(); int mappingColumn = model.getComponentMappingIndex(); int idColumn = model.getIdIndex(); @@ -345,6 +346,7 @@ public class DistrictImportUtils { writeValue(graph, row, flowAreaIndex, vertex, DN.Vertex_HasFlowArea); writeValue(graph, row, nominalPressureLossIndex, vertex, DN.Vertex_HasNominalPressureLoss); writeStringValue(graph, row, addressIndex, vertex, DN.Vertex_HasAddress); + writeStringValue(graph, row, regionIndex, vertex, DN.HasRegion); } catch (DatabaseException | MismatchedDimensionException | TransformException e) { throw new RuntimeException(e); } @@ -366,6 +368,7 @@ public class DistrictImportUtils { Path csvFile = model.getSource(); char delim = model.getDelimiter(); + Set writtenIds = new HashSet<>(); int startXCoordColumnIndex = model.getStartXCoordIndex(); int startYCoordColumnIndex = model.getStartYCoordIndex(); @@ -382,6 +385,7 @@ public class DistrictImportUtils { int kSupplyIndex = model.getkSupplyIndex(); int lengthIndex = model.getLengthIndex(); int detailedGeometryIndex = model.getDetailedGeometryIndex(); + int regionIndex = model.getRegionIndex(); int mappingColumn = model.getComponentMappingIndex(); int idColumn = model.getIdIndex(); @@ -435,60 +439,66 @@ public class DistrictImportUtils { DistrictImportUtils.consumeCSV(csvFile, delim, true, row -> { try { - String mappingValue = row.get(mappingColumn); - - String startXCoords = row.get(startXCoordColumnIndex); - String startYCoords = row.get(startYCoordColumnIndex); - String startZCoords = row.get(startZValueColumnIndex); - String endXCoords = row.get(endXCoordColumnIndex); - String endYCoords = row.get(endYCoordColumnIndex); - String endZCoords = row.get(endZValueColumnIndex); - - double startXCoord = Double.parseDouble(startXCoords); // make negative - double startYCoord = Double.parseDouble(startYCoords); - double startZCoord = Double.parseDouble(startZCoords); - double endXCoord = Double.parseDouble(endXCoords); // make negative - double endYCoord = Double.parseDouble(endYCoords); - double endZCoord = Double.parseDouble(endZCoords); - - double[] startCoords; - double[] endCoords; - if (actualDoTransform) { - DirectPosition2D startTargetPos = new DirectPosition2D(); - DirectPosition2D startSourcePos = new DirectPosition2D(startXCoord, startYCoord); - DirectPosition startRes = actualTransform.transform(startSourcePos, startTargetPos); - startCoords = startRes.getCoordinate(); + String idValue = row.get(idColumn); + if (!writtenIds.contains(idValue)) { + writtenIds.add(idValue); + String mappingValue = row.get(mappingColumn); - DirectPosition2D endTargetPos = new DirectPosition2D(); - DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord); - DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos); - endCoords = endRes.getCoordinate(); - } else { - startCoords = new double[] { startXCoord , startYCoord }; - endCoords = new double[] { endXCoord , endYCoord }; - } - - // Switch to (longitude, latitude) - flipAxes(startCoords); - flipAxes(endCoords); - - Optional oedge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, startZCoord, endCoords, endZCoord, new double[0], padding, true); - if (oedge.isPresent()) { - Resource edge = oedge.get(); - writeStringValue(graph, row, idColumn, edge, DN.HasId); + String startXCoords = row.get(startXCoordColumnIndex); + String startYCoords = row.get(startYCoordColumnIndex); + String startZCoords = row.get(startZValueColumnIndex); + String endXCoords = row.get(endXCoordColumnIndex); + String endYCoords = row.get(endYCoordColumnIndex); + String endZCoords = row.get(endZValueColumnIndex); + + double startXCoord = Double.parseDouble(startXCoords); // make negative + double startYCoord = Double.parseDouble(startYCoords); + double startZCoord = Double.parseDouble(startZCoords); + + double endXCoord = Double.parseDouble(endXCoords); // make negative + double endYCoord = Double.parseDouble(endYCoords); + double endZCoord = Double.parseDouble(endZCoords); + + double[] startCoords; + double[] endCoords; + if (actualDoTransform) { + DirectPosition2D startTargetPos = new DirectPosition2D(); + DirectPosition2D startSourcePos = new DirectPosition2D(startXCoord, startYCoord); + DirectPosition startRes = actualTransform.transform(startSourcePos, startTargetPos); + startCoords = startRes.getCoordinate(); + + DirectPosition2D endTargetPos = new DirectPosition2D(); + DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord); + DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos); + endCoords = endRes.getCoordinate(); + } else { + startCoords = new double[] { startXCoord , startYCoord }; + endCoords = new double[] { endXCoord , endYCoord }; + } + + // Switch to (longitude, latitude) + flipAxes(startCoords); + flipAxes(endCoords); - writeValue(graph, row, diameterColumnIndex, edge, DN.Edge_HasDiameter); - writeValue(graph, row, outerDiameterColumnIndex, edge, DN.Edge_HasOuterDiameter); - writeValue(graph, row, nominalMassFlowIndex, edge, DN.Edge_HasNominalMassFlow); - writeValue(graph, row, tGroundIndex, edge, DN.Edge_HasTGround); - writeValue(graph, row, kReturnIndex, edge, DN.Edge_HasKReturn); - writeValue(graph, row, kSupplyIndex, edge, DN.Edge_HasKSupply); - writeValue(graph, row, edgeFlowAreaIndex, edge, DN.Edge_HasFlowArea); - writeValue(graph, row, lengthIndex, edge, DN.Edge_HasLength); - writeDoubleArrayFromString(graph, row, detailedGeometryIndex, edge, DN.Edge_HasGeometry, actualTransform); + Optional oedge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, startZCoord, endCoords, endZCoord, new double[0], padding, true); + if (oedge.isPresent()) { + Resource edge = oedge.get(); + + writeStringValue(graph, row, idColumn, edge, DN.HasId); + + writeValue(graph, row, diameterColumnIndex, edge, DN.Edge_HasDiameter); + writeValue(graph, row, outerDiameterColumnIndex, edge, DN.Edge_HasOuterDiameter); + writeValue(graph, row, nominalMassFlowIndex, edge, DN.Edge_HasNominalMassFlow); + writeValue(graph, row, tGroundIndex, edge, DN.Edge_HasTGround); + writeValue(graph, row, kReturnIndex, edge, DN.Edge_HasKReturn); + writeValue(graph, row, kSupplyIndex, edge, DN.Edge_HasKSupply); + writeValue(graph, row, edgeFlowAreaIndex, edge, DN.Edge_HasFlowArea); + writeValue(graph, row, lengthIndex, edge, DN.Edge_HasLength); + writeStringValue(graph, row, regionIndex, edge, DN.HasRegion); + writeDoubleArrayFromString(graph, row, detailedGeometryIndex, edge, DN.Edge_HasGeometry, actualTransform); + } } - return true; } catch (DatabaseException | MismatchedDimensionException | TransformException e) { throw new RuntimeException(e); diff --git a/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph index e4aaeba9..87547328 100644 --- a/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph +++ b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph @@ -201,6 +201,10 @@ DN.HasId --> L0.String L0.String -- DN.Mapping.Id --> L0.String -- DN.Mapping.Region --> L0.String -- DN.Mapping.ComponentType --> STR.ComponentType -- DN.Mapping.Terminals --> DN.Mapping.TerminalPair