@@ -2,6 +2,7 @@ package com.sc4nam.module
22
33import java .io .File
44import io .github .memo33 .metarules .meta .RotFlip
5+ import syntax .RuleTransducer .TileOrientationCache
56
67/** Manages the tile orientation cache. The cache is necessary to maintain
78 * information about non-standard orientations:
@@ -23,19 +24,24 @@ object RegenerateTileOrientationCache {
2324
2425 /** Repeatedly compiles the metarule code until the cache does not get changed anymore.
2526 */
26- def compileMetarulesUntilStable (tileOrientationCache : collection.mutable.Map [Int , Set [RotFlip ]]): Unit = {
27+ def compileMetarulesUntilStable (cache : collection.mutable.Map [Int , Set [RotFlip ]]): Unit = {
28+ val tileOrientationCache = TileOrientationCache (cache = cache, accum = collection.mutable.Map .empty[Int , Set [RotFlip ]])
2729 var j = 0
2830 var stabilized = false
29- var previous : collection.immutable.Map [Int , Set [RotFlip ]] = tileOrientationCache.toMap
30- while (j < 3 && ! stabilized) {
31- LOGGER .info(s " > metarules compilation: iteration $j" )
31+ val maxIter = 5
32+ while (j < 5 && ! stabilized) {
3233 j += 1
34+ LOGGER .info(s " > metarules compilation: iteration $j" )
3335 CompileAllMetarules .compileMetarulesOnce(tileOrientationCache)
34- val next = tileOrientationCache.toMap
35- if (next == previous) {
36+ if (tileOrientationCache.accum.nonEmpty) {
37+ tileOrientationCache.cache ++= tileOrientationCache.accum
38+ tileOrientationCache.accum.clear()
39+ if (j == maxIter) {
40+ LOGGER .warning(s " Tile orientation cache could not be regenerated in $maxIter iterations. Increase the number of iterations or check if there is a bug. " )
41+ }
42+ } else {
3643 stabilized = true
3744 }
38- previous = next
3945 }
4046 }
4147
@@ -50,7 +56,7 @@ object RegenerateTileOrientationCache {
5056 }
5157 }
5258
53- def loadCache (): collection.mutable. Map [ Int , Set [ RotFlip ]] = {
59+ def loadCache (): TileOrientationCache = {
5460 scala.util.Using .resource(new java.util.Scanner (cacheFile, " UTF-8" )) { scanner =>
5561 val cache = collection.mutable.Map .empty[Int , Set [RotFlip ]]
5662 while (scanner.hasNextLine()) {
@@ -62,21 +68,18 @@ object RegenerateTileOrientationCache {
6268 cache(id) = orientations
6369 }
6470 }
65- cache
71+ TileOrientationCache ( cache = cache, accum = collection.mutable. Map .empty[ Int , Set [ RotFlip ]])
6672 }
6773 }
6874
6975 /** Loads the cache and gives a warning at the end if it was changed.
7076 */
71- def withCache [U ](body : collection.mutable.Map [Int , Set [RotFlip ]] => U ): U = {
72- var previous : collection.immutable.Map [Int , Set [RotFlip ]] = null
73- val cache = loadCache()
77+ def withCache [U ](body : TileOrientationCache => U ): U = {
78+ val tileOrientationCache = loadCache()
7479 try {
75- previous = cache.toMap
76- body(cache)
80+ body(tileOrientationCache)
7781 } finally {
78- assert(previous != null )
79- if (previous != cache.toMap) {
82+ if (tileOrientationCache.accum.nonEmpty) {
8083 LOGGER .warning(s " The file ${cacheFile} is outdated. Rebuild it with `sbt regenerateTileOrientationCache` and commit the changes. " )
8184 }
8285 }
0 commit comments