@@ -5,7 +5,8 @@ import com.sc4nam.module._
55import io .github .memo33 .metarules .meta .{RotFlip , Rule , EquivRule , IdTile }
66import RotFlip ._
77import syntax .IdTile
8- import SanityChecker .{isRulFile , fileEndsWithNewline , Driveside , Rhd , Lhd , RhdAndLhd , parseRule , linePatternIncludingNewlines }
8+ import SanityChecker .{fileEndsWithNewline , linePatternIncludingNewlines }
9+ import Rul2Model .{iterateRulFiles , parseRuleWithRestrictedDriveside , Rhd , Lhd , RhdAndLhd , drivesideOfFile , applyRule }
910
1011/** Run with `SBT_OPTS="-Xmx2G" sbt "runMain com.sc4nam.scripts.RedundantAdjacenciesChecker"`.
1112 * Note that this increases the heap size for more memory.
@@ -28,27 +29,6 @@ object RedundantAdjacenciesChecker {
2829 checkRedundantAdjacencies()
2930 }
3031
31- def drivesideOfFile (path : Path ): Driveside = {
32- val name = path.getFileName().toString()
33- if (name.contains(" rhd." )) Rhd
34- else if (name.contains(" lhd." )) Lhd
35- else RhdAndLhd
36- }
37-
38- val lhdPrefix = " ;###LHD###"
39- val rhdPrefix = " ;###RHD###"
40-
41- def parseRuleWithRestrictedDriveside (line : String , driveside : Driveside ): Option [(Rule [IdTile ], Driveside )] = {
42- val line1 = line.trim()
43- if (line1.startsWith(lhdPrefix)) {
44- if (driveside == Rhd ) None else parseRule(line1.substring(lhdPrefix.length)).map(_ -> Lhd )
45- } else if (line1.startsWith(rhdPrefix)) {
46- if (driveside == Lhd ) None else parseRule(line1.substring(rhdPrefix.length)).map(_ -> Rhd )
47- } else {
48- parseRule(line1).map(_ -> driveside)
49- }
50- }
51-
5232 def checkRedundantAdjacencies (): Unit = {
5333 val rulesRhd = collection.mutable.Map .empty[EquivRule , Rule [IdTile ]]
5434 val rulesLhd = collection.mutable.Map .empty[EquivRule , Rule [IdTile ]]
@@ -57,8 +37,7 @@ object RedundantAdjacenciesChecker {
5737 val lookupRuleLhd : PartialFunction [EquivRule , Rule [IdTile ]] = rulesShared.orElse(rulesLhd) // the two maps should be disjoint
5838
5939 LOGGER .info(" caching all RUL2 code for RHD and LHD" )
60- Files .walk(Paths .get(" Controller/RUL2" )).forEach { path =>
61- if (isRulFile(path)) {
40+ iterateRulFiles(Paths .get(" Controller/RUL2" )).foreach { path =>
6241 val drivesideFile = drivesideOfFile(path)
6342 scala.util.Using .resource(new java.util.Scanner (path.toFile(), " UTF-8" )) { scanner =>
6443 while (scanner.hasNextLine()) {
@@ -70,12 +49,10 @@ object RedundantAdjacenciesChecker {
7049 }
7150 }
7251 }
73- }
7452 }
7553
7654 LOGGER .info(" searching for redundant adjacencies in RUL2 code" )
77- Files .walk(Paths .get(" Controller/RUL2" )).forEach { path =>
78- if (isRulFile(path)) {
55+ iterateRulFiles(Paths .get(" Controller/RUL2" )).foreach { path =>
7956 val drivesideFile = drivesideOfFile(path)
8057 val tmpPath = path.resolveSibling(path.getFileName().toString() + " .tmp" )
8158 val endsWithNewline = fileEndsWithNewline(path) // attempt to preserve missing newlines at end of files to avoid noise
@@ -107,7 +84,6 @@ object RedundantAdjacenciesChecker {
10784 }
10885 }
10986 Files .move(tmpPath, path, java.nio.file.StandardCopyOption .REPLACE_EXISTING )
110- }
11187 }
11288 }
11389
@@ -139,17 +115,9 @@ object RedundantAdjacenciesChecker {
139115 (a * rot, b * rot, southBound)
140116 }}
141117
142- def evaluateRule (rule : Rule [IdTile ], t0 : IdTile , t1 : IdTile ): Option [(IdTile , IdTile )] = {
143- if (t0 == rule(0 ) && t1 == rule(1 )) Some ((rule(2 ), rule(3 )))
144- else if (t0 == rule(0 ) * R2F1 && t1 == rule(1 ) * R2F1 ) Some ((rule(2 ) * R2F1 , rule(3 ) * R2F1 ))
145- else if (t0 == rule(1 ) * R0F1 && t1 == rule(0 ) * R0F1 ) Some ((rule(3 ) * R0F1 , rule(2 ) * R0F1 ))
146- else if (t0 == rule(1 ) * R2F0 && t1 == rule(0 ) * R2F0 ) Some ((rule(3 ) * R2F0 , rule(2 ) * R2F0 ))
147- else None
148- }
149-
150118 def evaluateRulesOnce (lookupRule : PartialFunction [EquivRule , Rule [IdTile ]], t0 : IdTile , t1 : IdTile ): Option [(IdTile , IdTile )] = {
151119 val key = new EquivRule (Rule (t0, t1, t0, t1))
152- lookupRule.unapply(key).flatMap(rule => evaluateRule (rule, t0, t1))
120+ lookupRule.unapply(key).flatMap(rule => applyRule (rule, t0, t1))
153121 }
154122
155123 /** Checks if adjacency overrides a -> b and b -> c exist (in that order and
0 commit comments