Skip to content

Upgraded to scala3. #576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
version = 3.8.1
runner.dialect = scala213
runner.dialect = scala3

rewrite.scala3.convertToNewSyntax = false
rewrite.scala3.removeOptionalBraces = no

rewrite.trailingCommas.style = multiple

rewrite.rules = []
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Improve clustering efficiency by using faster hashcode method [#515](https://github.com/ie3-institute/OSMoGrid/issues/515)
- Update concept in developers' documentation [#517](https://github.com/ie3-institute/OSMoGrid/issues/517)
- Enhance documentation regarding overpass examples [#494](https://github.com/ie3-institute/OSMoGrid/issues/494)
- Upgraded to `scala3` [#575](https://github.com/ie3-institute/OSMoGrid/issues/575)

### Fixed
- Fixed bug in `LvGridGeneratorSupport` [#388](https://github.com/ie3-institute/OSMoGrid/issues/388)
Expand Down
12 changes: 5 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ plugins {
ext {
javaVersion = JavaVersion.VERSION_17

scalaVersion = '2.13'
scalaBinaryVersion = '2.13.16'
scalaVersion = '3'
scalaBinaryVersion = '3.7.0'
pekkoVersion = '1.1.3'

tscfgVersion = '0.9.995'
Expand Down Expand Up @@ -63,7 +63,7 @@ dependencies {
scalaCompilerPlugin "com.sksamuel.scapegoat:scalac-scapegoat-plugin_${scalaBinaryVersion}:${scapegoatVersion}"

// scala core
implementation "org.scala-lang:scala-library:${scalaBinaryVersion}"
implementation "org.scala-lang:scala3-library_3:${scalaBinaryVersion}"
implementation group: 'org.scala-lang.modules', name: "scala-parallel-collections_${scalaVersion}", version: '1.2.0'

// pekko
Expand All @@ -84,15 +84,14 @@ dependencies {


// ie³ power system utils
implementation('com.github.ie3-institute:PowerSystemUtils:2.2.1') {
implementation('com.github.ie3-institute:PowerSystemUtils:3.1.0') {
exclude group: 'org.slf4j'
exclude group: 'org.apache.logging.log4j'
exclude group: 'com.github.ie3-institute'
exclude group: 'com.github.johanneshiry', module: 'OSMonaut'
}

// ie³ power system data model
implementation('com.github.ie3-institute:PowerSystemDataModel:6.0.0') {
implementation('com.github.ie3-institute:PowerSystemDataModel:7.0.0') {
exclude group: 'org.slf4j'
exclude group: 'org.apache.logging.log4j'
exclude group: 'com.github.ie3-institute'
Expand Down Expand Up @@ -126,7 +125,6 @@ tasks.withType(ScalaCompile) {
"-P:scapegoat:dataDir:" + layout.buildDirectory.dir("reports/scapegoat/src/").get(),
"-P:scapegoat:disabledInspections:VariableShadowing",
"-P:scapegoat:overrideLevels:LonelySealedTrait=warning",
"-P:scapegoat:ignoredFiles:.*/SimonaConfig.scala" // see scapegoat-sbt page for this param
]
}

Expand Down
1 change: 0 additions & 1 deletion gradle/scripts/scoverage.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

scoverage {
scoverageVersion = "2.3.0"
scoverageScalaVersion = scalaBinaryVersion
coverageOutputHTML = false
coverageOutputXML = true
coverageOutputCobertura = false
Expand Down
75 changes: 33 additions & 42 deletions src/main/scala/edu/ie3/osmogrid/graph/OsmGraph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import java.util
import java.util.function.Supplier
import javax.measure.Quantity
import javax.measure.quantity.Length
import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters.*
import scala.util.boundary
import scala.util.boundary.{Label, break}

@SerialVersionUID(-2797654003980753341L)
class OsmGraph(
Expand Down Expand Up @@ -141,57 +143,46 @@ class OsmGraph(
def containsEdgeIntersection(): Boolean = {
val edges = edgeSet().asScala

val connections: util.List[(DistanceWeightedEdge, DistanceWeightedEdge)] =
new util.ArrayList[(DistanceWeightedEdge, DistanceWeightedEdge)]

// algorithm to check if two edges intersects each other
edges.foreach(edgeA => {
val sourceA = getEdgeSource(edgeA)
val targetA = getEdgeTarget(edgeA)

edges.foreach(edgeB => {
// an edge cannot intersect itself, therefore these combination are not tested
if (edgeA != edgeB) {
// two combinations possible
val t1 = (edgeA, edgeB)
val t2 = (edgeB, edgeA)

// check if the possible combinations are already tested
if (!connections.contains(t1) && !connections.contains(t2)) {
connections.add(t1)

val sourceB = getEdgeSource(edgeB)
val targetB = getEdgeTarget(edgeB)

val lineA =
getLineSegmentBetweenNodes(sourceA, targetA)
val lineB =
getLineSegmentBetweenNodes(sourceB, targetB)

// checks if the two line intersects each other
val intersection = hasIntersection(lineA, lineB)

if (intersection) {
return true
}
}
boundary {
for {
(edgeA, i) <- edges.zipWithIndex
edgeB <- edges.drop(i + 1)
} {
val sourceA = getEdgeSource(edgeA)
val targetA = getEdgeTarget(edgeA)

val sourceB = getEdgeSource(edgeB)
val targetB = getEdgeTarget(edgeB)

val lineA =
getLineSegmentBetweenNodes(sourceA, targetA)
val lineB =
getLineSegmentBetweenNodes(sourceB, targetB)

// checks if the two line intersects each other
val intersection = hasIntersection(lineA, lineB)

if (intersection) {
break(true)
}
})
})

false
}
false
}
}

/** Returns true if at least one vertex of this graph is connected to more
* than two edges.
*/
def tooManyVertexConnections(): Boolean = {
vertexSet().asScala.foreach { v =>
if (edgesOf(v).size() > 2) {
return true
boundary {
vertexSet().asScala.foreach { v =>
if (edgesOf(v).size() > 2) {
break(true)
}
}
false
}
false
}

/** Uses the given [[Polygon]] to create a subgraph that only contains
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/edu/ie3/osmogrid/io/input/OsmSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object OsmSource {

final case class PbfFileSource(
filePath: String,
ctx: ActorContext[_],
ctx: ActorContext[?],
) extends OsmSource {

def read(
Expand Down
19 changes: 12 additions & 7 deletions src/main/scala/edu/ie3/osmogrid/lv/LvGraphGeneratorSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
package edu.ie3.osmogrid.lv

import com.typesafe.scalalogging.LazyLogging
import edu.ie3.osmogrid.exception.OsmDataException
import edu.ie3.osmogrid.exception.{InputDataException, OsmDataException}
import edu.ie3.osmogrid.graph.OsmGraph
import edu.ie3.osmogrid.model.OsmoGridModel
import edu.ie3.osmogrid.model.OsmoGridModel.{EnhancedOsmEntity, LvOsmoGridModel}
import edu.ie3.util.geo.GeoUtils
import edu.ie3.util.geo.GeoUtils.{buildCoordinate, orthogonalProjection}
import edu.ie3.util.geo.RichGeometries.{RichCoordinate, RichPolygon}
import edu.ie3.util.geo.RichGeometries.{
calcAreaOnEarth,
haversineDistance,
isBetween,
}
import edu.ie3.util.osm.model.OsmEntity.Way.ClosedWay
import edu.ie3.util.osm.model.OsmEntity.{Node, Way}
import edu.ie3.util.quantities.QuantityUtils.asKiloWatt
import edu.ie3.util.quantities.interfaces.Irradiance
import org.jgrapht.alg.connectivity.ConnectivityInspector
import org.locationtech.jts.geom.Coordinate
Expand All @@ -27,12 +32,10 @@ import utils.OsmoGridUtils.{
safeBuildPolygon,
}

import edu.ie3.util.quantities.QuantityUtils.RichQuantityDouble

import java.util.UUID
import javax.measure.quantity.{Length, Power}
import scala.collection.parallel.ParSeq
import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters.*
import scala.util.{Success, Try}

object LvGraphGeneratorSupport extends LazyLogging {
Expand Down Expand Up @@ -281,8 +284,10 @@ object LvGraphGeneratorSupport extends LazyLogging {
coordinateA,
coordinateB,
1e-3,
) && ((orthogonalPt haversineDistance coordinateA) isGreaterThan minDistance)
&& ((orthogonalPt haversineDistance coordinateB) isGreaterThan minDistance)
) && orthogonalPt
.haversineDistance(coordinateA)
.isGreaterThan(minDistance)
&& orthogonalPt.haversineDistance(coordinateB).isGreaterThan(minDistance)
) {
val closestNode = Node(
id = UUID.randomUUID().getMostSignificantBits,
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/edu/ie3/osmogrid/lv/LvGridGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import edu.ie3.osmogrid.exception.IllegalStateException
import edu.ie3.osmogrid.guardian.run.RunGuardian
import edu.ie3.osmogrid.lv.LvGraphGeneratorSupport.buildConnectedGridGraphs
import edu.ie3.osmogrid.lv.LvGridGeneratorSupport.buildGrid
import edu.ie3.util.quantities.QuantityUtils.RichQuantityDouble
import edu.ie3.util.quantities.QuantityUtils.{asKiloVolt, asWattPerSquareMetre}
import org.apache.pekko.actor.typed.scaladsl.Behaviors
import tech.units.indriya.quantity.Quantities
import tech.units.indriya.unit.Units
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ object LvGridGeneratorSupport extends LazyLogging {
* @param mvVoltage
* the rated medium voltage of the grid to build
* @param considerHouseConnectionPoints
* whether or not to build distinct lines to houses
* whether to build distinct lines to houses
* @param loadSimultaneousFactor
* simultaneous factor for loads
* @param lineType
Expand All @@ -93,7 +93,7 @@ object LvGridGeneratorSupport extends LazyLogging {
val nodesWithBuildings: ParMap[Node, BuildingGraphConnection] =
buildingGraphConnections.map(bgc => (bgc.graphConnectionNode, bgc)).toMap

val nodeCreator = buildNode(lvVoltage) _
val nodeCreator = buildNode(lvVoltage)

val gridElements = osmGraph
.vertexSet()
Expand All @@ -120,7 +120,7 @@ object LvGridGeneratorSupport extends LazyLogging {
val loadCreator = buildLoad(
"Load of building: " + buildingGraphConnection.building.entity.id.toString,
buildingGraphConnection.buildingPower,
) _
)
if (considerHouseConnectionPoints) {
val osmBuildingConnectionNode =
buildingGraphConnection.buildingConnectionNode.getOrElse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package edu.ie3.osmogrid.lv.region_coordinator

import edu.ie3.osmogrid.io.input.BoundaryAdminLevel
import edu.ie3.osmogrid.lv.{GenerateLvGrid, LvGridGenerator}
import edu.ie3.osmogrid.model.OsmoGridModel
import org.apache.pekko.actor.typed.scaladsl.Behaviors

import java.util.UUID
Expand Down Expand Up @@ -45,7 +46,7 @@ object LvRegionCoordinator {
)
if (partitionedAreas.isEmpty)
Iterable.single(osmoGridModel)
else partitionedAreas.values
else partitionedAreas.values.seq
}

val levels = BoundaryAdminLevel
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/edu/ie3/osmogrid/model/OsmoGridModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ object OsmoGridModel {
models: ParSeq[LvOsmoGridModel],
filterNodes: Boolean = true,
): Option[OsmoGridModel] = {
models.headOption.flatMap { lvHeadModel: LvOsmoGridModel =>
models.headOption.flatMap { (lvHeadModel: LvOsmoGridModel) =>
if (models.forall(_.filter == lvHeadModel.filter)) {
Some(
LvOsmoGridModel(
Expand Down
20 changes: 13 additions & 7 deletions src/main/scala/edu/ie3/osmogrid/mv/VoronoiPolygonSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,24 @@ object VoronoiPolygonSupport {
builder.setSites(nodes.asJava)

/* finds all subdivisions and returns them as polygons */
builder.getSubdivision
val geometries = builder.getSubdivision
.getVoronoiCellPolygons(
GeoUtils.DEFAULT_GEOMETRY_FACTORY
)
/* necessary to get proper polygons */
.asScala
.toList match {
case polygons: List[Polygon] => polygons
case wrongType =>
throw new GeoException(
s"Expected a list of polygons as a result, but got $wrongType instead."
)
.toList

val wrongTypes = geometries.filter(geom => !geom.isInstanceOf[Polygon])

if (wrongTypes.nonEmpty) {
throw new GeoException(
s"Expected only a list of polygons as a result, but also got $wrongTypes."
)
}

geometries.map { case geom: Polygon =>
geom
}
}
}
5 changes: 3 additions & 2 deletions src/main/scala/utils/Clustering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,11 @@ object Clustering {
transformer2WTypeInput,
)

val nodes = gridElements.nodes.values.map(NodeWrapper).toIndexedSeq
val nodes = gridElements.nodes.values.map(NodeWrapper.apply).toIndexedSeq

val connections = Connections(gridElements, lines.toSeq)
val osmSubstations = gridElements.substations.values.map(NodeWrapper).toSet
val osmSubstations =
gridElements.substations.values.map(NodeWrapper.apply).toSet

val additionalSubstationCount = substationCount - osmSubstations.size

Expand Down
6 changes: 4 additions & 2 deletions src/main/scala/utils/Connections.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ object Connections extends LazyLogging {

// adding all nodes to the graph
val nodes: Set[NodeWrapper] =
(elements.nodes.values.map(NodeWrapper) ++ elements.substations.values
.map(NodeWrapper)).toSet
(elements.nodes.values.map(
NodeWrapper.apply
) ++ elements.substations.values
.map(NodeWrapper.apply)).toSet
nodes.foreach { n => graph.addVertex(n) }

lines.foreach { line =>
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/utils/GridConversion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ object GridConversion {

// calculate the distance to each osm node
val sortedList = osmNodes
.map { node: Node =>
.map { (node: Node) =>
(
node,
GeoUtils.calcHaversine(coordinate, node.coordinate.getCoordinate),
Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/utils/OsmoGridUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import edu.ie3.osmogrid.graph.OsmGraph
import edu.ie3.osmogrid.guardian.run.RunGuardian
import edu.ie3.osmogrid.io.input.AssetInformation
import edu.ie3.util.geo.GeoUtils
import edu.ie3.util.geo.RichGeometries.RichPolygon
import edu.ie3.util.geo.RichGeometries.containsCoordinate
import edu.ie3.util.osm.OsmUtils.GeometryUtils.buildPolygon
import edu.ie3.util.osm.model.OsmEntity.Way.ClosedWay
import edu.ie3.util.osm.model.OsmEntity.{Node, Way}
import edu.ie3.util.quantities.PowerSystemUnits
import edu.ie3.util.quantities.QuantityUtils.{RichQuantity, RichQuantityDouble}
import edu.ie3.util.quantities.QuantityUtils.{asKiloVolt, asPu, round}
import edu.ie3.util.quantities.interfaces.Irradiance
import org.locationtech.jts.algorithm.Centroid
import org.locationtech.jts.geom.{Coordinate, Polygon}
Expand All @@ -32,7 +32,7 @@ import tech.units.indriya.unit.Units
import java.util.UUID
import javax.measure.quantity.{Area, Power}
import scala.collection.parallel.ParSeq
import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters.*
import scala.util.{Failure, Success}

object OsmoGridUtils {
Expand Down Expand Up @@ -174,7 +174,7 @@ object OsmoGridUtils {
Option(new Centroid(hull).getCentroid) match {
case Some(coordinate) =>
val sortedList = mvNodes
.map { node: NodeInput =>
.map { (node: NodeInput) =>
(
node,
GeoUtils.calcHaversine(
Expand Down
Loading