Skip to content
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

Introducing scalafix #676

Draft
wants to merge 17 commits into
base: dev
Choose a base branch
from
Draft
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
17 changes: 16 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,20 @@
# https://help.github.com/articles/dealing-with-line-endings/
#
# These are explicitly windows files and should use crlf
*.bat text eol=crlf
*.bat text eol=crlf

# sh files
*.sh text eol=lf
*.bash text eol=lf

# These files are text and should be normalized
*.scala text
*.java text
*.groovy text
*.csv text
*.conf text

# binary is a macro for -text -diff
*.png binary
*.jpg binary
*.jar binary
33 changes: 33 additions & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
rules = [
OrganizeImports,
DisableSyntax,
LeakingImplicitClassVal,
RedundantSyntax
]

// default settings are stated explicitly, in case defaults might change
OrganizeImports {
blankLines = Auto
coalesceToWildcardImportThreshold = null // too risky, see documentation
expandRelative = true
groupExplicitlyImportedImplicitsSeparately = false
groupedImports = Merge
groups = [
"*"
"re:(javax?|scala)\\."
]
importSelectorsOrder = Ascii
importsOrder = Ascii
preset = DEFAULT
removeUnused = true
}

DisableSyntax {
noWhileLoops = true
// noIsInstanceOf = true FIXME can be enabled once pureconfig is in use
// noAsInstanceOf = true FIXME same
noXml = true
noFinalize = true
noValPatterns = true
}

1 change: 1 addition & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ version = 3.7.3
runner.dialect = scala213

rewrite.trailingCommas.style = multiple
lineEndings = preserve
8 changes: 1 addition & 7 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ node {

sh 'java -version'

gradle('--refresh-dependencies clean spotlessCheck pmdMain pmdTest reportScoverage checkScoverage', projectName)
gradle('--refresh-dependencies clean spotlessCheck checkScalafix pmdMain pmdTest reportScoverage checkScoverage', projectName)

sh(script: """set +x && cd $projectName""" + ''' set +x; ./gradlew javadoc''', returnStdout: true)
}
Expand Down Expand Up @@ -426,12 +426,6 @@ def publishReports(String relativeProjectDir) {
// publish scalatest reports for main project only (currently the only one with scala sources!)
publishHTML([allowMissing: false, alwaysLinkToLastBuild: true, escapeUnderscores: false, keepAll: true, reportDir: relativeProjectDir + '/build/reports/tests/scalatest', reportFiles: 'index.html', reportName: "${relativeProjectDir}_scala_tests_report", reportTitles: ''])

// publish scapegoat src report for main project only
publishHTML([allowMissing: false, alwaysLinkToLastBuild: true, escapeUnderscores: false, keepAll: true, reportDir: relativeProjectDir + '/build/reports/scapegoat/src', reportFiles: 'scapegoat.html', reportName: "${relativeProjectDir}_scapegoat_src_report", reportTitles: ''])

// publish scapegoat testsrc report for main project only
publishHTML([allowMissing: false, alwaysLinkToLastBuild: true, escapeUnderscores: false, keepAll: true, reportDir: relativeProjectDir + '/build/reports/scapegoat/testsrc', reportFiles: 'scapegoat.html', reportName: "${relativeProjectDir}_scapegoat_testsrc_report", reportTitles: ''])

// scoverage report dir
publishHTML([allowMissing: false, alwaysLinkToLastBuild: true, escapeUnderscores: false, keepAll: true, reportDir: relativeProjectDir + '/build/reports/scoverageTest', reportFiles: 'scoverage.xml', reportName: "${relativeProjectDir}_scoverage_report", reportTitles: ''])
}
Expand Down
21 changes: 4 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins {
id 'maven-publish' // publish to a maven repo (local or mvn central, has to be defined)
id 'pmd' // code check, working on source code
id 'com.diffplug.spotless' version '6.25.0'// code format
id 'io.github.cosmicsilence.scalafix' version '0.1.15'
id "com.github.ben-manes.versions" version '0.51.0'
id "de.undercouch.download" version "5.5.0" // downloads plugin
id "kr.motd.sphinx" version "2.10.1" // documentation generation
Expand All @@ -29,7 +30,6 @@ ext {
jtsVersion = '1.19.0'
confluentKafkaVersion = '7.4.0'
tscfgVersion = '1.0.0'
scapegoatVersion = '2.1.3'

testContainerVersion = '0.41.2'

Expand All @@ -46,6 +46,7 @@ java {

apply from: scriptsLocation + 'pmd.gradle'
apply from: scriptsLocation + 'spotless.gradle'
apply from: scriptsLocation + 'scalafix.gradle'
apply from: scriptsLocation + 'checkJavaVersion.gradle'
apply from: scriptsLocation + 'tscfg.gradle' // config tasks
apply from: scriptsLocation + 'documentation.gradle' // documentation tasks + configuration
Expand Down Expand Up @@ -135,10 +136,6 @@ dependencies {
}
implementation "org.locationtech.jts.io:jts-io-common:${jtsVersion}"

/* Scala compiler plugin for static code analysis */
implementation "com.sksamuel.scapegoat:scalac-scapegoat-plugin_${scalaBinaryVersion}:${scapegoatVersion}"
scalaCompilerPlugin "com.sksamuel.scapegoat:scalac-scapegoat-plugin_${scalaBinaryVersion}:${scapegoatVersion}"

/* Kafka */
implementation "org.apache.kafka:kafka-clients:${confluentKafkaVersion}-ccs"
implementation "io.confluent:kafka-streams-avro-serde:${confluentKafkaVersion}"
Expand Down Expand Up @@ -180,27 +177,17 @@ shadowJar {
archiveBaseName.set('simona')
}



// scapegoat hook configuration
// https://github.com/sksamuel/scapegoat
// using compileScala instead of tasks.withType(ScalaCompile) prevents applying scapegoat to scala test classes
// see https://docs.gradle.org/current/userguide/scala_plugin.html#sec:configure_scala_classpath for details
tasks.withType(ScalaCompile) {
scalaCompileOptions.additionalParameters = [
"-Xplugin:" + configurations.scalaCompilerPlugin.asPath,
"-P:scapegoat:dataDir:" + buildDir + "/reports/scapegoat/src/",
"-P:scapegoat:disabledInspections:VariableShadowing",
"-P:scapegoat:ignoredFiles:.*/SimonaConfig.scala" // see scapegoat-sbt page for this param
"-Ywarn-unused" // required for scalafix OrganizeImports
]
}

// separate scapegoat report for test classes
compileTestScala {
scalaCompileOptions.additionalParameters = [
"-Xplugin:" + configurations.scalaCompilerPlugin.asPath,
"-P:scapegoat:dataDir:" + buildDir + "/reports/scapegoat/testsrc/",
"-P:scapegoat:disabledInspections:VariableShadowing"
"-Ywarn-unused" // required for scalafix OrganizeImports
]
}

Expand Down
3 changes: 2 additions & 1 deletion docs/readthedocs/developersguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ In short, mergeable PRs have to meet our standards in several areas:
- Automated checks
- [Jenkins](https://simona.ie3.e-technik.tu-dortmund.de/ci/job/ie3-institute/job/simona/) run succeeds, i.e.
- The code needs to be properly formatted (`gradle spotlessApply`)
- The code needs to adhere to some standards regarding import order etc. (`gradle scalafix`)
- The code needs to compile
- All tests need to succeed
- [SonarQube](https://simona.ie3.e-technik.tu-dortmund.de/sonar/dashboard?id=edu.ie3%3Asimona) run succeeds, i.e.
Expand All @@ -45,7 +46,7 @@ In short, mergeable PRs have to meet our standards in several areas:
- All vital parts of the new code need to be covered by tests (see {ref}`developersguide:tests`)
- Code quality should be sufficient (see {ref}`developersguide:coding conventions`)

There can be exceptions to these rules, which have to be approved by the reviewer.
There can be exceptions to these rules in special cases, which have to be approved by the reviewer.

### Tests

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions docs/readthedocs/usersguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ You can find and download the source code of the latest stable SIMONA version [h

$ git clone https://github.com/ie3-institute/simona.git

For speedy execution and optimal convenience, change the Gradle build settings to build and run using Gradle and run tests using IntelliJ IDEA.
Open the settings window via ``File > Settings``. Find the Gradle settings in at ``Build, Execution, Deployment > Build Tools > Gradle`` or via the search bar at the top.
When building the project using Gradle, tools such as ``scalafix`` can work on the already assembled project via Gradle.
Running tests is most convenient via IntelliJ, because this way one can properly take advantage of the respective graphical user interface.

![](../readthedocs/images/usersguide/intellij-gradle-settings.png)

## Running a Standalone Simulation

Expand Down
4 changes: 4 additions & 0 deletions gradle/scripts/scalafix.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
scalafix {
includes.set(["**/*.scala"])
ignoreSourceSets.set(["scoverage"])
}
5 changes: 0 additions & 5 deletions gradle/scripts/sonarqube.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ sonarqube {
// reports stuff (for all languages)
property 'sonar.junit.reportPaths', [
'build/test-results/test'] // Comma-delimited list of paths to Surefire XML-format reports.
// unit tests reports dirs
// scapegoat report dir
property "sonar.scala.scapegoat.reportPaths", [
"build/reports/scapegoat/src/scapegoat-scalastyle.xml",
"build/reports/scapegoat/testsrc/scapegoat-scalastyle.xml"] // Comma-delimited list of paths to Scapegoat reports in the Scalastyle format
// scala specific stuff
property 'sonar.scala.coverage.reportPaths', 'build/reports/scoverageTest/scoverage.xml'

Expand Down
6 changes: 2 additions & 4 deletions src/main/scala/edu/ie3/simona/agent/SimonaAgent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@

package edu.ie3.simona.agent

import org.apache.pekko.actor.FSM.CurrentState
import org.apache.pekko.actor.{LoggingFSM, PoisonPill, Stash, Status}
import edu.ie3.simona.agent.state.AgentState
import edu.ie3.simona.event.notifier.Notifier
import edu.ie3.simona.logging.SimonaFSMActorLogging

import scala.language.postfixOps
import org.apache.pekko.actor.FSM.CurrentState
import org.apache.pekko.actor.{LoggingFSM, PoisonPill, Stash, Status}

/** Trait that is mixed into every agent providing methods and services common
* to all or most of all agents in the SIMONA simulation environment.
Expand Down
30 changes: 15 additions & 15 deletions src/main/scala/edu/ie3/simona/agent/grid/DBFSAlgorithm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

package edu.ie3.simona.agent.grid

import org.apache.pekko.actor.typed.scaladsl.adapter.ClassicActorRefOps
import org.apache.pekko.actor.{ActorRef, FSM, PoisonPill}
import org.apache.pekko.pattern.{ask, pipe}
import org.apache.pekko.util.{Timeout => PekkoTimeout}
import breeze.linalg.{DenseMatrix, DenseVector}
import breeze.math.Complex
import edu.ie3.datamodel.graph.SubGridGate
Expand Down Expand Up @@ -46,6 +42,10 @@ import edu.ie3.simona.ontology.messages.VoltageMessage.{
import edu.ie3.simona.util.TickUtil.TickLong
import edu.ie3.util.scala.quantities.Megavars
import edu.ie3.util.scala.quantities.SquantsUtils.RichElectricPotential
import org.apache.pekko.actor.typed.scaladsl.adapter.ClassicActorRefOps
import org.apache.pekko.actor.{ActorRef, FSM, PoisonPill}
import org.apache.pekko.pattern.{ask, pipe}
import org.apache.pekko.util.{Timeout => PekkoTimeout}
import squants.Each
import squants.energy.Megawatts

Expand Down Expand Up @@ -158,7 +158,7 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
gridAgentBaseData: GridAgentBaseData,
) =>
log.debug(
s"Received Slack Voltages request from {} for nodes {} and sweepNo: {}",
"Received Slack Voltages request from {} for nodes {} and sweepNo: {}",
sender(),
nodeUuids,
gridAgentBaseData.currentSweepNo,
Expand Down Expand Up @@ -197,7 +197,7 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
throw new DBFSAlgorithmException(
s"Requested nodeUuid $nodeUuid " +
s"not found in sweep value store data for sweepNo: $sweepNo. This indicates" +
s"either a wrong processing of a previous sweep result or inconsistencies in grid model data!"
"either a wrong processing of a previous sweep result or inconsistencies in grid model data!"
)
}
}.getOrElse {
Expand Down Expand Up @@ -229,7 +229,7 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
} match {
case (slackE, slackF) =>
log.debug(
s"Provide {} to {} for node {} and sweepNo: {}",
"Provide {} to {} for node {} and sweepNo: {}",
s"$slackE, $slackF",
sender(),
nodeUuid,
Expand All @@ -255,14 +255,14 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
) =>
if (gridAgentBaseData.currentSweepNo == requestSweepNo) {
log.debug(
s"Received request for grid power values for sweepNo {} before my first power flow calc. Stashing away.",
"Received request for grid power values for sweepNo {} before my first power flow calc. Stashing away.",
requestSweepNo,
)
stash()
stay()
} else {
log.debug(
s"Received request for grid power values for a NEW sweep (request: {}, my: {})",
"Received request for grid power values for a NEW sweep (request: {}, my: {})",
requestSweepNo,
gridAgentBaseData.currentSweepNo,
)
Expand Down Expand Up @@ -395,7 +395,7 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
gridAgentBaseData.powerFlowParams.sweepTimeout,
)

log.debug(s"Going to {}", HandlePowerFlowCalculations)
log.debug("Going to {}", HandlePowerFlowCalculations)

goto(HandlePowerFlowCalculations) using gridAgentBaseData

Expand Down Expand Up @@ -517,7 +517,7 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
// when we don't have assets we can skip another request for different asset behaviour due to changed
// voltage values and go back to SimulateGrid directly
log.debug(
s"No generation or load assets in the grid. Going back to {}.",
"No generation or load assets in the grid. Going back to {}.",
SimulateGrid,
)
unstashAll() // we can answer the stashed grid power requests now
Expand Down Expand Up @@ -591,7 +591,7 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
} else {
// no changes from assets, we want to go back to SimulateGrid and report the LF results to our parent grids if any requests
log.debug(
s"Assets have not changed their exchanged power or no voltage dependent behaviour. Going back to {}.",
"Assets have not changed their exchanged power or no voltage dependent behaviour. Going back to {}.",
SimulateGrid,
)
unstashAll() // we can answer the stashed grid power requests now
Expand Down Expand Up @@ -1065,7 +1065,7 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {

implicit val timeout: PekkoTimeout = PekkoTimeout.create(askTimeout)

log.debug(s"asking assets for power values: {}", nodeToAssetAgents)
log.debug("asking assets for power values: {}", nodeToAssetAgents)

if (nodeToAssetAgents.values.flatten.nonEmpty)
Some(
Expand Down Expand Up @@ -1141,7 +1141,7 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
): Option[Future[ReceivedPowerValues]] = {
implicit val timeout: PekkoTimeout = PekkoTimeout.create(askTimeout)
log.debug(
s"asking inferior grids for power values: {}",
"asking inferior grids for power values: {}",
inferiorGridGates,
)
Option.when(inferiorGridGates.nonEmpty) {
Expand Down Expand Up @@ -1200,7 +1200,7 @@ trait DBFSAlgorithm extends PowerFlowSupport with GridResultsSupport {
): Option[Future[ReceivedSlackVoltageValues]] = {
implicit val timeout: PekkoTimeout = PekkoTimeout.create(askTimeout)
log.debug(
s"asking superior grids for slack voltage values: {}",
"asking superior grids for slack voltage values: {}",
superiorGridGates,
)

Expand Down
9 changes: 4 additions & 5 deletions src/main/scala/edu/ie3/simona/agent/grid/GridAgent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ import edu.ie3.simona.ontology.messages.{Activation, StopMessage}
import edu.ie3.simona.scheduler.ScheduleLock.ScheduleKey
import edu.ie3.simona.util.SimonaConstants.INIT_SIM_TICK
import edu.ie3.util.TimeUtil
import org.apache.pekko.actor.{ActorRef, Props, Stash}
import org.apache.pekko.actor.typed.scaladsl.adapter.ClassicActorRefOps
import org.apache.pekko.actor.{ActorRef, Props, Stash}

import java.time.ZonedDateTime
import java.time.temporal.ChronoUnit
import java.util.UUID
import scala.language.postfixOps

object GridAgent {
def props(
Expand Down Expand Up @@ -160,13 +159,13 @@ class GridAgent(
failFast(gridAgentInitData)

log.debug(
s"Inferior Subnets: {}; Inferior Subnet Nodes: {}",
"Inferior Subnets: {}; Inferior Subnet Nodes: {}",
gridAgentInitData.inferiorGridIds,
gridAgentInitData.inferiorGridNodeUuids,
)

log.debug(
s"Superior Subnets: {}; Superior Subnet Nodes: {}",
"Superior Subnets: {}; Superior Subnet Nodes: {}",
gridAgentInitData.superiorGridIds,
gridAgentInitData.superiorGridNodeUuids,
)
Expand Down Expand Up @@ -276,7 +275,7 @@ class GridAgent(
)
throw new GridAgentInitializationException(
s"$actorName has neither superior nor inferior grids! This can either " +
s"be cause by wrong subnetGate information or invalid parametrization of the simulation!"
"be cause by wrong subnetGate information or invalid parametrization of the simulation!"
)
}
}
Loading