refactor: deduplicate resource loading code

This commit is contained in:
2026-01-29 11:26:35 +03:00
parent 704ff16cc4
commit 59737178c8
3 changed files with 23 additions and 43 deletions

View File

@@ -75,19 +75,21 @@ private class RearTrap10kev: ITrap {
}
}
fun loadResourceAsString(resourcePath: String): String {
return {}::class.java.classLoader
.getResourceAsStream(resourcePath)
?.bufferedReader()
?.use { it.readText() }
?: throw IllegalArgumentException("Resource not found: $resourcePath")
}
fun getRearTrapByName(name: String): ITrap {
return when(name) {
"RearTrap10kev" -> RearTrap10kev()
"25-11-26-2+" -> {
val resourcePath = "reartrap-6-days.json"
// Load JSON from resources at compile-time
val jsonInput = {}::class.java.classLoader
.getResourceAsStream(resourcePath)
?.bufferedReader()
?.use { it.readText() }
?: throw IllegalArgumentException("Resource not found: $resourcePath")
FineGridInterpolation(jsonInput, 2000.0, 2.0)
FineGridInterpolation(loadResourceAsString("reartrap-6-days.json"), 2000.0, 2.0)
}
else -> throw IllegalArgumentException()
}
}

View File

@@ -69,17 +69,6 @@ enum class TrapModel(val description: String) : ITrap {
val jsonInput = loadResourceAsString("trap-fine-5-day-2V.json")
FineGridInterpolation(jsonInput, wallL = 2000.0, deltaStep = 2.0)
}
// =====================================================================
// Утилита для загрузки ресурсов
// =====================================================================
private fun loadResourceAsString(resourcePath: String): String {
return {}::class.java.classLoader
.getResourceAsStream(resourcePath)
?.bufferedReader()
?.use { it.readText() }
?: throw IllegalArgumentException("Ресурс не найден: $resourcePath")
}
}
}

View File

@@ -3,14 +3,15 @@ package ru.inr.mass.scripts
import org.apache.commons.math3.analysis.interpolation.LinearInterpolator
import space.kscience.kmath.real.step
import space.kscience.kmath.structures.toDoubleArray
import kotlin.collections.toDoubleArray
/**
* Единая модель задней стенки (rear wall transmission)
*/
enum class WallModel(val description: String) {
GEANT_2025_03_10("Geant4 симуляция, угол 170.4°, март 2025"),
GEANT_2025_10_09("Geant4 симуляция, угол ~174.3°, октябрь 2025"),
GEANT_2024_07_12("Geant4 симуляция, старая версия, июль 2024"),
GEANT_2025_03_10("Geant4 симуляция, угол 170.4°, март 2025"), GEANT_2025_10_09("Geant4 симуляция, угол ~174.3°, октябрь 2025"), GEANT_2024_07_12(
"Geant4 симуляция, старая версия, июль 2024"
),
TSV_2025_12_01("Данные из файла rearwall-25-12-01.tsv, декабрь 2025");
/**
@@ -563,14 +564,10 @@ enum class WallModel(val description: String) {
// =====================================================================
// 4. Данные из внешнего TSV-файла (декабрь 2025)
// =====================================================================
private val Y_2025_12_01 = {}::class.java.classLoader
.getResourceAsStream("rearwall-25-12-01.tsv")
?.bufferedReader()
?.readLines()
?.filter { it.isNotBlank() }
?.map { it.trim().toDouble() }
?.toDoubleArray()
?: throw IllegalStateException("Не найден файл rearwall-25-12-01.tsv в ресурсах")
private val Y_2025_12_01 = loadResourceAsString("rearwall-25-12-01.tsv")
.lines()
.filter { it.isNotBlank() }
.map { it.trim().toDouble() }.toDoubleArray()
private val X_TSV = (2000.0..19000.0 step 50.0).toDoubleArray()
@@ -611,8 +608,7 @@ const val WALL_L = 2_000.0
const val WALL_R = 19_000.0
private val WALL_INTERPOLATOR = LinearInterpolator().interpolate(
(10_000.0..WALL_R step WALL_STEP).toDoubleArray(),
WALL_FROM_GEANT.toDoubleArray()
(10_000.0..WALL_R step WALL_STEP).toDoubleArray(), WALL_FROM_GEANT.toDoubleArray()
)
fun wallStep(u: Double): Double {
@@ -624,25 +620,18 @@ private val WALL_25_10_09 = arrayOf<Double>(
)
private val WALL_25_10_09_INTERPOLATOR = LinearInterpolator().interpolate(
(10_000.0..WALL_R step WALL_STEP).toDoubleArray(),
WALL_25_10_09.toDoubleArray()
(10_000.0..WALL_R step WALL_STEP).toDoubleArray(), WALL_25_10_09.toDoubleArray()
)
fun wall251009(u: Double): Double {
return WALL_25_10_09_INTERPOLATOR.value(u)
}
private val WALL_25_12_01 = {}::class.java.classLoader
.getResourceAsStream("rearwall-25-12-01.tsv")
?.bufferedReader()!!
.readLines()
.filter { it.isNotBlank() }
.map { it.trim().toDouble() }
.toDoubleArray()
private val WALL_25_12_01 = loadResourceAsString("rearwall-25-12-01.tsv")
.lines().filter { it.isNotBlank() }.map { it.trim().toDouble() }.toDoubleArray()
private val WALL_25_12_01_INTERPOLATOR = LinearInterpolator().interpolate(
(2000.0..19000.0 step 50.0).toDoubleArray(),
WALL_25_12_01
(2000.0..19000.0 step 50.0).toDoubleArray(), WALL_25_12_01
)
fun getWallByName(name: String): (u: Double) -> Double {