|
| 1 | +package org.combinators.models |
| 2 | + |
| 3 | +/** |
| 4 | + * sbt "dp/runMain org.combinators.modelTests.Glossary [bottomUp | topDown | topDownMemo] |
| 5 | + * |
| 6 | + * Creates output files in target/bottomUp or target\topDown or target\topDownMemo |
| 7 | + */ |
| 8 | + |
| 9 | +import cats.effect.{ExitCode, IO, IOApp} |
| 10 | +import org.apache.commons.io.FileUtils |
| 11 | +import org.combinators.archive.cogen.bottomUp.twoSequences.longestCommonSubsequence.LCSMainJava |
| 12 | +import org.combinators.archive.unenhancedModels.boilerplate.unenhancedUncrossedLines.UnenhancedUncrossedLinesMainJava |
| 13 | +import org.combinators.archive.unenhancedModels.models.twoSequences.{LongestCommonSubsequenceModel, UncrossedLinesModel} |
| 14 | +import org.combinators.dp.enhanced.EnhancedMainInterface |
| 15 | +import org.combinators.dp.{BottomUp, GenerationOption, TopDown} |
| 16 | +import org.combinators.cogen.{FileWithPath, FileWithPathPersistable} |
| 17 | +import FileWithPathPersistable._ |
| 18 | +import org.combinators.models.boilerplate.grid._ |
| 19 | +import org.combinators.models.boilerplate.integer._ |
| 20 | +import org.combinators.models.boilerplate.oneSequence._ |
| 21 | +import org.combinators.models.boilerplate.strings._ |
| 22 | +import org.combinators.models.boilerplate.twoSequences._ |
| 23 | + |
| 24 | +import java.nio.file.{Path, Paths} |
| 25 | +import scala.collection.Seq |
| 26 | + |
| 27 | +class GlossaryScala { |
| 28 | + |
| 29 | + val persistable = FileWithPathPersistable[FileWithPath] |
| 30 | + |
| 31 | + def directToDiskTransaction(targetDirectory: Path, files: Seq[FileWithPath]): IO[Unit] = { |
| 32 | + IO { |
| 33 | + print("Computing Files...") |
| 34 | + println("[OK]") |
| 35 | + if (targetDirectory.toFile.exists()) { |
| 36 | + print(s"Cleaning Target Directory ($targetDirectory)...") |
| 37 | + FileUtils.deleteDirectory(targetDirectory.toFile) |
| 38 | + println("[OK]") |
| 39 | + } |
| 40 | + print("Persisting Files...") |
| 41 | + files.foreach(file => persistable.persistOverwriting(targetDirectory, file)) |
| 42 | + println("[OK]") |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + def runDirectToDisc(targetDirectory: Path,files: Seq[FileWithPath]): IO[ExitCode] = { |
| 47 | + for { |
| 48 | + _ <- directToDiskTransaction(targetDirectory, files) |
| 49 | + } yield ExitCode.Success |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +object GlossaryScalaToDiskMain extends IOApp { |
| 54 | + |
| 55 | + // choose one of these to pass in |
| 56 | + val topDown = TopDown() |
| 57 | + val topDownWithMemo = TopDown(memo = true) |
| 58 | + val bottomUp = BottomUp() |
| 59 | + |
| 60 | + // declare the working versions for each problem in the enhanced list |
| 61 | + val known_enhanced_solutions:Seq[(EnhancedMainInterface, EnhancedModel, Seq[GenerationOption])] = Seq( |
| 62 | + (new BellNumberMainJava(), BellNumberDirectToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 63 | + (new CountSquaresMainJava(), CountSquaresToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 64 | + (new DiceThrowMainJava(), DiceThrowDirectToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 65 | + (new FibonacciEnhancedMainJava(), FibonacciEnhancedMainToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 66 | + (new InterleaveStringsMainJava(), InterleaveStringsToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 67 | + (new LongestCommonSubsequenceMainJava, LongestCommonSubsequenceToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 68 | + (new MinCostClimbingStairMain(), MinCostClimbingStairToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 69 | + (new PerfectSquareMainJava(), PerfectSquareMainDirectToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 70 | + (new ThreeStringsLCSMainJava(), ThreeStringsLCSToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 71 | + (new UncrossedLinesMainJava(), UncrossedLinesDirectDiskToMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 72 | +// (new NeedlemanWunschSequenceAlignmentMainJava(), NeedlemanWunschSequenceAlignmentToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 73 | +// (new DistinctSubsequencesMainJava(), DistinctSubsequencesToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 74 | +// (new WildcardPatternMatchingMainJava(), WildcardPatternMatchingToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 75 | +// (new ShortestCommonSupersequenceMainJava(), ShortestCommonSupersequenceToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 76 | + (new TribonacciMainJava(), TribonacciToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 77 | + (new UniquePathsMainJava(), UniquePathsToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 78 | + (new MinPathSumMainJava(), MinPathSumToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 79 | + |
| 80 | + // generates but has flawed logic because of the transformation of (r,c) into (i,j) |
| 81 | + // CHALLENGING (new MatrixChainMultiplicationMainJava(), MatrixChainMultiplicationMainDirectToDiskMain.model, Seq(topDown, topDownWithMemo, bottomUp)), |
| 82 | + ) |
| 83 | + |
| 84 | + // below are the individual DP problems generated and added to `all_files`. |
| 85 | + def top_down_memo_files(): Seq[FileWithPath] = { |
| 86 | + |
| 87 | + // UncrossedLinesMainJava and LCSMainJava still don't work |
| 88 | + |
| 89 | + val mcm_td = (new MatrixChainMultiplicationMainTopDownJava(), MatrixChainMultiplicationMainTopDownDirectToDiskMain.model, Seq(topDown, topDownWithMemo)) |
| 90 | + val just_td = Seq(mcm_td) |
| 91 | + |
| 92 | + val others = (just_td ++ known_enhanced_solutions).filter(triple |
| 93 | + => triple._3.contains(TopDown(memo = true))). |
| 94 | + flatMap(triple => triple._1.filesToGenerate(triple._2, TopDown(memo = true))) |
| 95 | + |
| 96 | + others |
| 97 | + } |
| 98 | + |
| 99 | + // below are the individual DP problems generated and added to `all_files`. |
| 100 | + def top_down(): Seq[FileWithPath] = { |
| 101 | +// val ul = new UncrossedLinesMainJava().filesToGenerate(new UncrossedLinesModel().instantiate(), TopDown()) |
| 102 | + //val lcs = new LCSMainJava().filesToGenerate(new LongestCommonSubsequenceModel().instantiate(), TopDown()) [HEINEMAN: not working] |
| 103 | + // val kp = new KnapsackMainJava().filesToGenerate(new KnapsackModel().instantiate(), TopDown()) [HEINEMAN: not working] |
| 104 | + //val nwsa = new NWSAMainJava().filesToGenerate(new NeedlemanWunschSequenceAlignmentModel().instantiate(), TopDown()) [HEINEMAN: not working] |
| 105 | + |
| 106 | + val mcm_td = (new MatrixChainMultiplicationMainTopDownJava(), MatrixChainMultiplicationMainTopDownDirectToDiskMain.model, Seq(topDown, topDownWithMemo)) |
| 107 | + val just_td = Seq(mcm_td) |
| 108 | + |
| 109 | + val others = (just_td ++ known_enhanced_solutions).filter(triple |
| 110 | + => triple._3.contains(TopDown())). |
| 111 | + flatMap(triple => triple._1.filesToGenerate(triple._2, TopDown())) |
| 112 | + |
| 113 | + others |
| 114 | + } |
| 115 | + |
| 116 | + def bottom_up_files() : Seq[FileWithPath] = { |
| 117 | + val ul = new UnenhancedUncrossedLinesMainJava().filesToGenerate(new UncrossedLinesModel().instantiate(), BottomUp()) |
| 118 | + val lcs = new LCSMainJava().filesToGenerate(new LongestCommonSubsequenceModel().instantiate(), BottomUp()) |
| 119 | + |
| 120 | + val mcm_bot = (new MatrixChainMultiplicationMainBottomUpJava(), MatrixChainMultiplicationMainBottomUpDirectToDiskMain.model, Seq(bottomUp)) |
| 121 | + val just_bot = Seq(mcm_bot) |
| 122 | + |
| 123 | + val others = (just_bot ++ known_enhanced_solutions).filter(triple |
| 124 | + => triple._3.contains(BottomUp())). |
| 125 | + flatMap(triple => triple._1.filesToGenerate(triple._2, BottomUp())) |
| 126 | + |
| 127 | + others |
| 128 | + } |
| 129 | + |
| 130 | + def run(args: List[String]): IO[ExitCode] = { |
| 131 | + val choice = if (args.isEmpty) { |
| 132 | + bottomUp // <------ CHANGE this manually when you run, to generate topDown or topDownWithMemo -- BOTTOMUP NOT YET WORKING |
| 133 | + } else { |
| 134 | + args(0).toLowerCase match { |
| 135 | + case "topdown" => topDown |
| 136 | + case "bottomup" => bottomUp |
| 137 | + case "topdownmemo" => topDownWithMemo |
| 138 | + case "topdownwithmemo" => topDownWithMemo |
| 139 | + case _ => |
| 140 | + print (s"Unknown option: ${args(0)}. Must be either 'topDown', 'bottomUp' or 'topDownMemo'.") |
| 141 | + ??? |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + val targetDirectory:Path = Paths.get("target", choice.name) |
| 146 | + |
| 147 | + for { |
| 148 | + _ <- IO { |
| 149 | + println("Initializing Generator...") |
| 150 | + println(s"Output will appear in: ${targetDirectory}") |
| 151 | + } |
| 152 | + main <- IO { new Glossary() } |
| 153 | + |
| 154 | + result <- if (choice == topDown) { |
| 155 | + main.runDirectToDisc(targetDirectory, top_down()) |
| 156 | + } else if (choice == topDownWithMemo) { |
| 157 | + main.runDirectToDisc(targetDirectory, top_down_memo_files()) |
| 158 | + } else { |
| 159 | + main.runDirectToDisc(targetDirectory, bottom_up_files()) |
| 160 | + } |
| 161 | + |
| 162 | + _ <- IO { println("Make sure you run the scripts to 'fix' the generated code.") } |
| 163 | + } yield result |
| 164 | + } |
| 165 | +} |
0 commit comments