Spiral.scala
author Chengsong
Wed, 13 Mar 2019 13:14:38 +0000
changeset 0 902326e1615a
child 1 99f4459d9bb6
permissions -rwxr-xr-x
run scalac lex_blex_Frankensteined.scala BRexp.scala Element.scala Partial.scala Spiral.scala then run scala Spiral to see the results
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
Chengsong
parents:
diff changeset
     1
import Element.elem
Chengsong
parents:
diff changeset
     2
import RexpRelated._
Chengsong
parents:
diff changeset
     3
import RexpRelated.Rexp._
Chengsong
parents:
diff changeset
     4
import Partial._
Chengsong
parents:
diff changeset
     5
import BRexp._
Chengsong
parents:
diff changeset
     6
import scala.collection.mutable.ListBuffer
Chengsong
parents:
diff changeset
     7
object Spiral{
Chengsong
parents:
diff changeset
     8
Chengsong
parents:
diff changeset
     9
  val space = elem(" ")
Chengsong
parents:
diff changeset
    10
  val corner = elem("+")
Chengsong
parents:
diff changeset
    11
Chengsong
parents:
diff changeset
    12
  def spiral(nEdges: Int, direction: Int): Element = {
Chengsong
parents:
diff changeset
    13
    if(nEdges == 1)
Chengsong
parents:
diff changeset
    14
      elem("+")
Chengsong
parents:
diff changeset
    15
    else {
Chengsong
parents:
diff changeset
    16
      val sp = spiral(nEdges - 1, (direction + 3) % 4)
Chengsong
parents:
diff changeset
    17
      def verticalBar = elem('|', 1, sp.height)
Chengsong
parents:
diff changeset
    18
      def horizontalBar = elem('-', sp.width, 1)
Chengsong
parents:
diff changeset
    19
      if(direction == 0)
Chengsong
parents:
diff changeset
    20
        (corner beside horizontalBar) above sp//(sp beside space)
Chengsong
parents:
diff changeset
    21
      else if (direction == 1)
Chengsong
parents:
diff changeset
    22
        sp beside (corner above verticalBar)
Chengsong
parents:
diff changeset
    23
      else if (direction == 2)
Chengsong
parents:
diff changeset
    24
        (space beside sp) above (horizontalBar beside corner)
Chengsong
parents:
diff changeset
    25
      else
Chengsong
parents:
diff changeset
    26
        (verticalBar above corner) beside (space above sp)
Chengsong
parents:
diff changeset
    27
    }
Chengsong
parents:
diff changeset
    28
  }
Chengsong
parents:
diff changeset
    29
  val alphabet = ("""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:"=()\;-+*!<>\/%{} """+"\n\t").toSet//Set('a','b','c')
Chengsong
parents:
diff changeset
    30
  def bregx_tree(r: BRexp): Element = regx_tree(berase(r))
Chengsong
parents:
diff changeset
    31
  def regx_tree(r: Rexp): Element = aregx_tree(internalise(r))
Chengsong
parents:
diff changeset
    32
  def aregx_tree(r: ARexp): Element = {
Chengsong
parents:
diff changeset
    33
    r match {
Chengsong
parents:
diff changeset
    34
      case ACHAR(bs, d) => {
Chengsong
parents:
diff changeset
    35
        //val Some(d) = alphabet.find(f)
Chengsong
parents:
diff changeset
    36
        d match {
Chengsong
parents:
diff changeset
    37
          case '\n' => elem("\\n")
Chengsong
parents:
diff changeset
    38
          case '\t' => elem("\\t")
Chengsong
parents:
diff changeset
    39
          case ' ' => elem("space")
Chengsong
parents:
diff changeset
    40
          case d => elem(d.toString)
Chengsong
parents:
diff changeset
    41
        }   
Chengsong
parents:
diff changeset
    42
      }
Chengsong
parents:
diff changeset
    43
      case AONE(bs) => {
Chengsong
parents:
diff changeset
    44
        elem("ONE")
Chengsong
parents:
diff changeset
    45
      }
Chengsong
parents:
diff changeset
    46
      case AZERO => {
Chengsong
parents:
diff changeset
    47
        elem("ZERO")
Chengsong
parents:
diff changeset
    48
      }
Chengsong
parents:
diff changeset
    49
      case ASEQ(bs, r1, r2) => {
Chengsong
parents:
diff changeset
    50
        binary_print("SEQ", r1, r2)
Chengsong
parents:
diff changeset
    51
      }
Chengsong
parents:
diff changeset
    52
      case AALTS(bs, rs) => {
Chengsong
parents:
diff changeset
    53
        //elem("Awaiting completion")
Chengsong
parents:
diff changeset
    54
        list_print("ALT", rs)
Chengsong
parents:
diff changeset
    55
      }
Chengsong
parents:
diff changeset
    56
      case ASTAR(bs, r) => {
Chengsong
parents:
diff changeset
    57
        list_print("STA", List(r))
Chengsong
parents:
diff changeset
    58
      }
Chengsong
parents:
diff changeset
    59
    }
Chengsong
parents:
diff changeset
    60
  }
Chengsong
parents:
diff changeset
    61
  val port = elem(" └-")
Chengsong
parents:
diff changeset
    62
  def list_print(name: String, rs: List[ARexp]): Element = {
Chengsong
parents:
diff changeset
    63
    rs match {
Chengsong
parents:
diff changeset
    64
      case r::Nil => {
Chengsong
parents:
diff changeset
    65
        val pref = aregx_tree(r)
Chengsong
parents:
diff changeset
    66
        val head = elem(name)
Chengsong
parents:
diff changeset
    67
        (head left_align (port up_align pref) ) 
Chengsong
parents:
diff changeset
    68
      }
Chengsong
parents:
diff changeset
    69
      case r2::r1::Nil => {
Chengsong
parents:
diff changeset
    70
        binary_print(name, r2, r1)
Chengsong
parents:
diff changeset
    71
      }
Chengsong
parents:
diff changeset
    72
      case r::rs1 => {
Chengsong
parents:
diff changeset
    73
        val pref = aregx_tree(r)
Chengsong
parents:
diff changeset
    74
        val head = elem(name)
Chengsong
parents:
diff changeset
    75
        if (pref.height > 1){
Chengsong
parents:
diff changeset
    76
          val link = elem('|', 1, pref.height - 1)
Chengsong
parents:
diff changeset
    77
          (head left_align ((port above link) beside pref)) left_align tail_print(rs1)    
Chengsong
parents:
diff changeset
    78
        }
Chengsong
parents:
diff changeset
    79
        else{
Chengsong
parents:
diff changeset
    80
          (head left_align (port beside pref) ) left_align tail_print(rs1)
Chengsong
parents:
diff changeset
    81
        }
Chengsong
parents:
diff changeset
    82
      }
Chengsong
parents:
diff changeset
    83
    }
Chengsong
parents:
diff changeset
    84
  }
Chengsong
parents:
diff changeset
    85
  def tail_print(rs: List[ARexp]): Element = {
Chengsong
parents:
diff changeset
    86
    rs match {
Chengsong
parents:
diff changeset
    87
      case r2::r1::Nil => {
Chengsong
parents:
diff changeset
    88
        val pref = aregx_tree(r2)
Chengsong
parents:
diff changeset
    89
        val suff = aregx_tree(r1)
Chengsong
parents:
diff changeset
    90
        if (pref.height > 1){
Chengsong
parents:
diff changeset
    91
          val link = elem('|', 1, pref.height - 1)
Chengsong
parents:
diff changeset
    92
          ((port above link) beside pref) left_align (port up_align suff)
Chengsong
parents:
diff changeset
    93
        }
Chengsong
parents:
diff changeset
    94
        else{
Chengsong
parents:
diff changeset
    95
          (port beside pref) left_align (port up_align suff)
Chengsong
parents:
diff changeset
    96
        } 
Chengsong
parents:
diff changeset
    97
      }
Chengsong
parents:
diff changeset
    98
      case r2::rs1 => {
Chengsong
parents:
diff changeset
    99
        val pref = aregx_tree(r2)
Chengsong
parents:
diff changeset
   100
        
Chengsong
parents:
diff changeset
   101
        if (pref.height > 1){
Chengsong
parents:
diff changeset
   102
          val link = elem('|', 1, pref.height - 1)
Chengsong
parents:
diff changeset
   103
          ((port above link) beside pref) left_align tail_print(rs1)//(port up_align tail_print(rs1) )
Chengsong
parents:
diff changeset
   104
        }
Chengsong
parents:
diff changeset
   105
        else{
Chengsong
parents:
diff changeset
   106
          (port beside pref) left_align tail_print(rs1)//(port up_align tail_print(rs1))
Chengsong
parents:
diff changeset
   107
        } 
Chengsong
parents:
diff changeset
   108
        //pref left_align tail_print(rs1)
Chengsong
parents:
diff changeset
   109
      }
Chengsong
parents:
diff changeset
   110
    }
Chengsong
parents:
diff changeset
   111
  }
Chengsong
parents:
diff changeset
   112
Chengsong
parents:
diff changeset
   113
  def binary_print(name: String, r1: ARexp, r2: ARexp): Element = {
Chengsong
parents:
diff changeset
   114
    val pref = aregx_tree(r1)
Chengsong
parents:
diff changeset
   115
    val suff = aregx_tree(r2)
Chengsong
parents:
diff changeset
   116
    val head = elem(name)
Chengsong
parents:
diff changeset
   117
    if (pref.height > 1){
Chengsong
parents:
diff changeset
   118
      val link = elem('|', 1, pref.height - 1)
Chengsong
parents:
diff changeset
   119
      (head left_align ((port above link) beside pref) ) left_align (port up_align suff)
Chengsong
parents:
diff changeset
   120
    }
Chengsong
parents:
diff changeset
   121
    else{
Chengsong
parents:
diff changeset
   122
      (head left_align (port beside pref) ) left_align (port up_align suff)
Chengsong
parents:
diff changeset
   123
    }
Chengsong
parents:
diff changeset
   124
  }
Chengsong
parents:
diff changeset
   125
  val arr_of_size = ListBuffer.empty[Int]
Chengsong
parents:
diff changeset
   126
  def spill(r: Rexp, or: Rexp): Set[Rexp] = {
Chengsong
parents:
diff changeset
   127
    if(r == or) 
Chengsong
parents:
diff changeset
   128
      Set(r)
Chengsong
parents:
diff changeset
   129
    else{
Chengsong
parents:
diff changeset
   130
      r match {
Chengsong
parents:
diff changeset
   131
          case ALTS(rs) => rs.flatMap(r1 => spill(r1, or)).toSet
Chengsong
parents:
diff changeset
   132
          case SEQ(ALTS(rs), r3) => rs.flatMap(r1 => spill(r1, or).map(a => if(a == ONE) r3 else SEQ(a, r3)) ).toSet
Chengsong
parents:
diff changeset
   133
          case ZERO => Set()
Chengsong
parents:
diff changeset
   134
          case r => Set(r)
Chengsong
parents:
diff changeset
   135
      }
Chengsong
parents:
diff changeset
   136
    }
Chengsong
parents:
diff changeset
   137
  }
Chengsong
parents:
diff changeset
   138
  def pC(r: Rexp): Set[Rexp] = {//PD's companion
Chengsong
parents:
diff changeset
   139
    r match {
Chengsong
parents:
diff changeset
   140
      case SEQ(r1, r2) => pC(r2)
Chengsong
parents:
diff changeset
   141
      case ALTS(rs) => rs.flatMap(a => pC(a) ).toSet
Chengsong
parents:
diff changeset
   142
      case CHAR(c) => Set(r)
Chengsong
parents:
diff changeset
   143
      case r => Set()
Chengsong
parents:
diff changeset
   144
    }
Chengsong
parents:
diff changeset
   145
  }
Chengsong
parents:
diff changeset
   146
Chengsong
parents:
diff changeset
   147
  def aspill(ar: ARexp, or: Rexp): Set[Rexp] = spill(erase(ar), or)
Chengsong
parents:
diff changeset
   148
  def illustration(r: Rexp, s: String){
Chengsong
parents:
diff changeset
   149
    var i_like_imperative_style = internalise(r)
Chengsong
parents:
diff changeset
   150
    val all_chars = s.toList
Chengsong
parents:
diff changeset
   151
    for (i <- 0 to s.length - 1){
Chengsong
parents:
diff changeset
   152
      val der_res =  bder(all_chars(i), i_like_imperative_style)
Chengsong
parents:
diff changeset
   153
      val simp_res = bsimp(der_res)
Chengsong
parents:
diff changeset
   154
      println("The original regex, the regex after derivative w.r.t " + all_chars(i) + " and the simplification of the derivative.")
Chengsong
parents:
diff changeset
   155
      println(aregx_tree(i_like_imperative_style) up_align aregx_tree(der_res) up_align aregx_tree(simp_res))
Chengsong
parents:
diff changeset
   156
      //println(asize(i_like_imperative_style), asize(der_res), asize(simp_res))
Chengsong
parents:
diff changeset
   157
      arr_of_size += asize(i_like_imperative_style)
Chengsong
parents:
diff changeset
   158
      //println(asize(simp_res), asize(simp_res) / arr_of_size(0) )
Chengsong
parents:
diff changeset
   159
      i_like_imperative_style = simp_res
Chengsong
parents:
diff changeset
   160
    }
Chengsong
parents:
diff changeset
   161
    arr_of_size += asize(i_like_imperative_style)
Chengsong
parents:
diff changeset
   162
  }
Chengsong
parents:
diff changeset
   163
  val ran = scala.util.Random
Chengsong
parents:
diff changeset
   164
  var alphabet_size = 3
Chengsong
parents:
diff changeset
   165
  def balanced_seq_star_gen(depth: Int, star: Boolean): Rexp = {
Chengsong
parents:
diff changeset
   166
    if(depth == 1){
Chengsong
parents:
diff changeset
   167
      ((ran.nextInt(4) + 97).toChar).toString
Chengsong
parents:
diff changeset
   168
    }
Chengsong
parents:
diff changeset
   169
    else if(star){
Chengsong
parents:
diff changeset
   170
      STAR(balanced_seq_star_gen(depth - 1, false))
Chengsong
parents:
diff changeset
   171
    }
Chengsong
parents:
diff changeset
   172
    else{
Chengsong
parents:
diff changeset
   173
      SEQ(balanced_seq_star_gen(depth - 1, true), balanced_seq_star_gen(depth - 1, true))
Chengsong
parents:
diff changeset
   174
    }
Chengsong
parents:
diff changeset
   175
  }
Chengsong
parents:
diff changeset
   176
  def max(i: Int, j: Int): Int = {
Chengsong
parents:
diff changeset
   177
    if(i > j)
Chengsong
parents:
diff changeset
   178
      i
Chengsong
parents:
diff changeset
   179
    else 
Chengsong
parents:
diff changeset
   180
      j
Chengsong
parents:
diff changeset
   181
  }
Chengsong
parents:
diff changeset
   182
  def random_struct_gen(depth:Int): Rexp = {
Chengsong
parents:
diff changeset
   183
    val dice = ran.nextInt(3)
Chengsong
parents:
diff changeset
   184
    val dice2 = ran.nextInt(3)
Chengsong
parents:
diff changeset
   185
    (dice, depth) match {
Chengsong
parents:
diff changeset
   186
      case (_, 0) => ((ran.nextInt(3) + 97).toChar).toString
Chengsong
parents:
diff changeset
   187
      case (0, i) => STAR(random_struct_gen(max(0, i - 1 - dice2)))
Chengsong
parents:
diff changeset
   188
      case (1, i) => SEQ(random_struct_gen(max(0, i - 1 - dice2)), random_struct_gen(max(0, i - 1 - dice2)))
Chengsong
parents:
diff changeset
   189
      case (2, i) => ALTS( List(random_struct_gen(max(0, i - 1 - dice2)), random_struct_gen(max(0, i - 1 - dice2))) )
Chengsong
parents:
diff changeset
   190
    }
Chengsong
parents:
diff changeset
   191
  }
Chengsong
parents:
diff changeset
   192
  def balanced_struct_gen(depth: Int): Rexp = {
Chengsong
parents:
diff changeset
   193
    val dice = ran.nextInt(3)
Chengsong
parents:
diff changeset
   194
    (dice, depth) match {
Chengsong
parents:
diff changeset
   195
      case (_, 0) => ((ran.nextInt(3) + 97).toChar).toString
Chengsong
parents:
diff changeset
   196
      case (0, i) => STAR(random_struct_gen(depth - 1))
Chengsong
parents:
diff changeset
   197
      case (1, i) => SEQ(random_struct_gen(depth - 1), random_struct_gen(depth - 1))
Chengsong
parents:
diff changeset
   198
      case (2, i) => ALTS( List(random_struct_gen(depth - 1), random_struct_gen(depth - 1) ) )
Chengsong
parents:
diff changeset
   199
    }
Chengsong
parents:
diff changeset
   200
  }
Chengsong
parents:
diff changeset
   201
  def rd_string_gen(alp_size: Int, len: Int): String = {
Chengsong
parents:
diff changeset
   202
    if( len > 0)
Chengsong
parents:
diff changeset
   203
      ((ran.nextInt(alp_size) + 97).toChar).toString + rd_string_gen(alp_size, len - 1)
Chengsong
parents:
diff changeset
   204
    else
Chengsong
parents:
diff changeset
   205
      ((ran.nextInt(alp_size) + 97).toChar).toString
Chengsong
parents:
diff changeset
   206
  }
Chengsong
parents:
diff changeset
   207
  def plot(b: List[Int]){
Chengsong
parents:
diff changeset
   208
    println(b(0),b.max)
Chengsong
parents:
diff changeset
   209
Chengsong
parents:
diff changeset
   210
  }
Chengsong
parents:
diff changeset
   211
  def dep_exp(depth: List[Int]){
Chengsong
parents:
diff changeset
   212
    for(i <- depth){
Chengsong
parents:
diff changeset
   213
      arr_of_size.clear()
Chengsong
parents:
diff changeset
   214
      val s = rd_string_gen(alphabet_size, (i-8)*(i-8)+10)
Chengsong
parents:
diff changeset
   215
      val r = random_struct_gen(i)
Chengsong
parents:
diff changeset
   216
      println("depth: "+i)
Chengsong
parents:
diff changeset
   217
      illustration(r, s) //"abcabadaaadcabdbabcdaadbabbcbbdabdabbcbdbabdbcdb") 
Chengsong
parents:
diff changeset
   218
      //println("depth: " + i + " general stats:"+ arr_of_size(0), arr_of_size.max, arr_of_size.max/arr_of_size(0))
Chengsong
parents:
diff changeset
   219
      //println("x y label alignment")
Chengsong
parents:
diff changeset
   220
      /*for(i <- 0 to s.length - 1){
Chengsong
parents:
diff changeset
   221
        if(s(i) == '\n')
Chengsong
parents:
diff changeset
   222
          println(i+" "+arr_of_size(i)+" "+"nl"+" -140")
Chengsong
parents:
diff changeset
   223
        else if(s(i) ==  ' ')
Chengsong
parents:
diff changeset
   224
          println(i+" "+arr_of_size(i)+" "+"sp"+" -140")
Chengsong
parents:
diff changeset
   225
        else
Chengsong
parents:
diff changeset
   226
          println(i+" "+arr_of_size(i)+" "+s(i)+" -140")
Chengsong
parents:
diff changeset
   227
      }*/
Chengsong
parents:
diff changeset
   228
      //println(s.length + " " + arr_of_size(s.length) + " ]" + " -140")
Chengsong
parents:
diff changeset
   229
    }
Chengsong
parents:
diff changeset
   230
  }
Chengsong
parents:
diff changeset
   231
  def case_study(ss: List[String], r: Rexp){
Chengsong
parents:
diff changeset
   232
    for(s <- ss){
Chengsong
parents:
diff changeset
   233
      arr_of_size.clear()
Chengsong
parents:
diff changeset
   234
      illustration(r, s)
Chengsong
parents:
diff changeset
   235
      println("x y label alignment")
Chengsong
parents:
diff changeset
   236
      for(i <- 0 to s.length - 1){
Chengsong
parents:
diff changeset
   237
        if(s(i) == '\n')
Chengsong
parents:
diff changeset
   238
          println(i+" "+arr_of_size(i)+" "+"nl"+" -140")
Chengsong
parents:
diff changeset
   239
        else if(s(i) ==  ' ')
Chengsong
parents:
diff changeset
   240
          println(i+" "+arr_of_size(i)+" "+"sp"+" -140")
Chengsong
parents:
diff changeset
   241
        else
Chengsong
parents:
diff changeset
   242
          println(i+" "+arr_of_size(i)+" "+s(i)+" -140")
Chengsong
parents:
diff changeset
   243
      }
Chengsong
parents:
diff changeset
   244
    }
Chengsong
parents:
diff changeset
   245
  }
Chengsong
parents:
diff changeset
   246
  def star_gen(dp: Int): Rexp = {
Chengsong
parents:
diff changeset
   247
    if(dp > 0)
Chengsong
parents:
diff changeset
   248
      STAR(star_gen(dp - 1))
Chengsong
parents:
diff changeset
   249
    else
Chengsong
parents:
diff changeset
   250
      "a"
Chengsong
parents:
diff changeset
   251
  }
Chengsong
parents:
diff changeset
   252
  def strs_gen(len: Int, num: Int): List[String] = {
Chengsong
parents:
diff changeset
   253
    if(num > 0){
Chengsong
parents:
diff changeset
   254
      rd_string_gen(3, len)::strs_gen(len, num - 1)
Chengsong
parents:
diff changeset
   255
    }
Chengsong
parents:
diff changeset
   256
    else{
Chengsong
parents:
diff changeset
   257
      Nil
Chengsong
parents:
diff changeset
   258
    }
Chengsong
parents:
diff changeset
   259
  }
Chengsong
parents:
diff changeset
   260
  def regx_print(r: Rexp): String = {
Chengsong
parents:
diff changeset
   261
    r match {
Chengsong
parents:
diff changeset
   262
      case ZERO =>
Chengsong
parents:
diff changeset
   263
        "ZERO"
Chengsong
parents:
diff changeset
   264
      case CHAR(c) => {
Chengsong
parents:
diff changeset
   265
         //val Some(c) = alphabet.find(f)
Chengsong
parents:
diff changeset
   266
         "\"" + c.toString + "\""
Chengsong
parents:
diff changeset
   267
      }
Chengsong
parents:
diff changeset
   268
      case ONE => {
Chengsong
parents:
diff changeset
   269
        "ONE"
Chengsong
parents:
diff changeset
   270
      }
Chengsong
parents:
diff changeset
   271
      case ALTS(rs) => {
Chengsong
parents:
diff changeset
   272
        "ALTS(List("+(rs.map(regx_print)).foldLeft("")((a, b) => if(a == "") b else a + "," + b)+"))"
Chengsong
parents:
diff changeset
   273
      }
Chengsong
parents:
diff changeset
   274
      case SEQ(r1, r2) => {
Chengsong
parents:
diff changeset
   275
        "SEQ(" + regx_print(r1) + "," + regx_print(r2) + ")"
Chengsong
parents:
diff changeset
   276
      }
Chengsong
parents:
diff changeset
   277
      case STAR(r) => {
Chengsong
parents:
diff changeset
   278
        "STAR(" + regx_print(r) + ")"
Chengsong
parents:
diff changeset
   279
      }
Chengsong
parents:
diff changeset
   280
    }
Chengsong
parents:
diff changeset
   281
  }
Chengsong
parents:
diff changeset
   282
  val mkst = "abcdefghijklmnopqrstuvwxyz"
Chengsong
parents:
diff changeset
   283
  def weak_sub_check(r: Rexp, s: String, i: Int, f: (List[Rexp], Set[Rexp]) => Boolean){
Chengsong
parents:
diff changeset
   284
    //we first compute pders over the set of all strings on the alphabet
Chengsong
parents:
diff changeset
   285
    val pd = pderas(Set(r), i + 4)
Chengsong
parents:
diff changeset
   286
    //then "b-internalise" the regular expression into a brexp(this is essentially 
Chengsong
parents:
diff changeset
   287
    //attaching a bit Z to every alts to signify that they come from the original regular expression)
Chengsong
parents:
diff changeset
   288
    var old = brternalise(r)
Chengsong
parents:
diff changeset
   289
    //this is for comparison between normal simp and the weakened version of simp
Chengsong
parents:
diff changeset
   290
    //normal simp will be performed on syncold
Chengsong
parents:
diff changeset
   291
    //weakend simp will be performed on old
Chengsong
parents:
diff changeset
   292
    var syncold = brternalise(r)
Chengsong
parents:
diff changeset
   293
    val all_chars = s.toList
Chengsong
parents:
diff changeset
   294
    for (i <- 0 to s.length - 1){
Chengsong
parents:
diff changeset
   295
      val syncder_res = brder(all_chars(i), syncold)
Chengsong
parents:
diff changeset
   296
      val syncsimp_res = strong_br_simp(syncder_res)
Chengsong
parents:
diff changeset
   297
      //see brder for detailed explanation
Chengsong
parents:
diff changeset
   298
      //just changes bit Z to S when deriving an ALTS, 
Chengsong
parents:
diff changeset
   299
      //signifying that the structure has been "touched" and
Chengsong
parents:
diff changeset
   300
      //therefore able to be spilled in the bspill function
Chengsong
parents:
diff changeset
   301
      val der_res =  brder(all_chars(i), old)
Chengsong
parents:
diff changeset
   302
      val simp_res = br_simp(der_res)
Chengsong
parents:
diff changeset
   303
      val anatomy = bspill(simp_res)
Chengsong
parents:
diff changeset
   304
      //track if the number of regular expressions exceeds those in the PD set(remember PD means the pders over A*)
Chengsong
parents:
diff changeset
   305
      if(f(anatomy, pd)  == false){
Chengsong
parents:
diff changeset
   306
        /*println("regular expression")
Chengsong
parents:
diff changeset
   307
        println(regx_tree(r))
Chengsong
parents:
diff changeset
   308
        println("string at " + i)
Chengsong
parents:
diff changeset
   309
        println(s)
Chengsong
parents:
diff changeset
   310
        println("partial derivatives")
Chengsong
parents:
diff changeset
   311
        (pd.foreach(a => println(regx_tree(a))))
Chengsong
parents:
diff changeset
   312
        println("simp result")
Chengsong
parents:
diff changeset
   313
        println(bregx_tree(simp_res))
Chengsong
parents:
diff changeset
   314
        println("bspill result")
Chengsong
parents:
diff changeset
   315
        (anatomy.foreach(a => println(regx_tree(a))))*/
Chengsong
parents:
diff changeset
   316
        println(size(berase(syncsimp_res)))
Chengsong
parents:
diff changeset
   317
        println(size(berase(simp_res)))
Chengsong
parents:
diff changeset
   318
        println(anatomy.map(size).sum)
Chengsong
parents:
diff changeset
   319
        println(pd.map(size).sum)
Chengsong
parents:
diff changeset
   320
      }  
Chengsong
parents:
diff changeset
   321
      old = simp_res
Chengsong
parents:
diff changeset
   322
      syncold = syncsimp_res
Chengsong
parents:
diff changeset
   323
    }
Chengsong
parents:
diff changeset
   324
  }
Chengsong
parents:
diff changeset
   325
  def inclusion_truth(anatomy: List[Rexp], pd: Set[Rexp]): Boolean = {
Chengsong
parents:
diff changeset
   326
    val aset = anatomy.toSet
Chengsong
parents:
diff changeset
   327
    if(aset subsetOf pd){
Chengsong
parents:
diff changeset
   328
      true
Chengsong
parents:
diff changeset
   329
    }
Chengsong
parents:
diff changeset
   330
    else{
Chengsong
parents:
diff changeset
   331
      println("inclusion not true")
Chengsong
parents:
diff changeset
   332
      false
Chengsong
parents:
diff changeset
   333
    }
Chengsong
parents:
diff changeset
   334
  }
Chengsong
parents:
diff changeset
   335
  def size_comp(anatomy: List[Rexp], pd: Set[Rexp]):Boolean = {println("size of PD and bspilled simp regx: ", pd.size, anatomy.size); true}
Chengsong
parents:
diff changeset
   336
  def size_expansion_rate(anatomy: List[Rexp], pd: Set[Rexp]): Boolean = if(anatomy.size > (pd.size)*2 ) {println("size of PD and bspilled simp regx: ", pd.size, anatomy.size); inclusion_truth(anatomy, pd); false }else {true}
Chengsong
parents:
diff changeset
   337
  
Chengsong
parents:
diff changeset
   338
  def check_all(){
Chengsong
parents:
diff changeset
   339
    for(i <- 1 to 1)
Chengsong
parents:
diff changeset
   340
    {
Chengsong
parents:
diff changeset
   341
        val s = "bbb"//rd_string_gen(alphabet_size, 5)//"ac"//rd_string_gen(alphabet_size, 5)
Chengsong
parents:
diff changeset
   342
        val r = STAR(STAR(ALTS(List(SEQ(CHAR('b'),CHAR('b')), ALTS(List(CHAR('a'), CHAR('b')))))))//balanced_struct_gen(4)//SEQ(ALTS(List(STAR("a"),ALTS(List("a","c")))),SEQ(ALTS(List("c","a")),ALTS(List("c","b")))) //random_struct_gen(7)
Chengsong
parents:
diff changeset
   343
        //subset_check(r, s)
Chengsong
parents:
diff changeset
   344
        weak_sub_check(r, s, 5, size_expansion_rate)
Chengsong
parents:
diff changeset
   345
    }
Chengsong
parents:
diff changeset
   346
  }
Chengsong
parents:
diff changeset
   347
  def main(args: Array[String]) {
Chengsong
parents:
diff changeset
   348
    check_all()   
Chengsong
parents:
diff changeset
   349
  } 
Chengsong
parents:
diff changeset
   350
}
Chengsong
parents:
diff changeset
   351