Spiral.scala
author Chengsong
Sat, 16 Mar 2019 14:14:42 +0000
changeset 5 622ddbb1223a
parent 4 7a349fe58bf4
child 6 26b40a985622
permissions -rwxr-xr-x
correctness test with enumeration
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))
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    32
  def annotated_tree(r: ARexp): Element = {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    33
    r match {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    34
      case ACHAR(bs, d) => {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    35
        //val Some(d) = alphabet.find(f)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    36
        d match {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    37
          case '\n' => elem("\\n")
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    38
          case '\t' => elem("\\t")
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    39
          case ' ' => elem("space")
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    40
          case d => elem(d.toString)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    41
        }   
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    42
      }
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    43
      case AONE(bs) => {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    44
        elem("ONE")
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    45
      }
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    46
      case AZERO => {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    47
        elem("ZERO")
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    48
      }
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    49
      case ASEQ(bs, r1, r2) => {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    50
        binary_print("SEQ", r1, r2)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    51
      }
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    52
      case AALTS(bs, rs) => {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    53
        //elem("Awaiting completion")
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    54
        list_print("ALT", rs)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    55
      }
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    56
      case ASTAR(bs, r) => {
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    57
        list_print("STA", List(r))
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    58
      }
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    59
    } 
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
    60
  }
0
Chengsong
parents:
diff changeset
    61
  def aregx_tree(r: ARexp): Element = {
Chengsong
parents:
diff changeset
    62
    r match {
Chengsong
parents:
diff changeset
    63
      case ACHAR(bs, d) => {
Chengsong
parents:
diff changeset
    64
        //val Some(d) = alphabet.find(f)
Chengsong
parents:
diff changeset
    65
        d match {
Chengsong
parents:
diff changeset
    66
          case '\n' => elem("\\n")
Chengsong
parents:
diff changeset
    67
          case '\t' => elem("\\t")
Chengsong
parents:
diff changeset
    68
          case ' ' => elem("space")
Chengsong
parents:
diff changeset
    69
          case d => elem(d.toString)
Chengsong
parents:
diff changeset
    70
        }   
Chengsong
parents:
diff changeset
    71
      }
Chengsong
parents:
diff changeset
    72
      case AONE(bs) => {
Chengsong
parents:
diff changeset
    73
        elem("ONE")
Chengsong
parents:
diff changeset
    74
      }
Chengsong
parents:
diff changeset
    75
      case AZERO => {
Chengsong
parents:
diff changeset
    76
        elem("ZERO")
Chengsong
parents:
diff changeset
    77
      }
Chengsong
parents:
diff changeset
    78
      case ASEQ(bs, r1, r2) => {
Chengsong
parents:
diff changeset
    79
        binary_print("SEQ", r1, r2)
Chengsong
parents:
diff changeset
    80
      }
Chengsong
parents:
diff changeset
    81
      case AALTS(bs, rs) => {
Chengsong
parents:
diff changeset
    82
        //elem("Awaiting completion")
Chengsong
parents:
diff changeset
    83
        list_print("ALT", rs)
Chengsong
parents:
diff changeset
    84
      }
Chengsong
parents:
diff changeset
    85
      case ASTAR(bs, r) => {
Chengsong
parents:
diff changeset
    86
        list_print("STA", List(r))
Chengsong
parents:
diff changeset
    87
      }
Chengsong
parents:
diff changeset
    88
    }
Chengsong
parents:
diff changeset
    89
  }
Chengsong
parents:
diff changeset
    90
  val port = elem(" └-")
Chengsong
parents:
diff changeset
    91
  def list_print(name: String, rs: List[ARexp]): Element = {
Chengsong
parents:
diff changeset
    92
    rs match {
Chengsong
parents:
diff changeset
    93
      case r::Nil => {
Chengsong
parents:
diff changeset
    94
        val pref = aregx_tree(r)
Chengsong
parents:
diff changeset
    95
        val head = elem(name)
Chengsong
parents:
diff changeset
    96
        (head left_align (port up_align pref) ) 
Chengsong
parents:
diff changeset
    97
      }
Chengsong
parents:
diff changeset
    98
      case r2::r1::Nil => {
Chengsong
parents:
diff changeset
    99
        binary_print(name, r2, r1)
Chengsong
parents:
diff changeset
   100
      }
Chengsong
parents:
diff changeset
   101
      case r::rs1 => {
Chengsong
parents:
diff changeset
   102
        val pref = aregx_tree(r)
Chengsong
parents:
diff changeset
   103
        val head = elem(name)
Chengsong
parents:
diff changeset
   104
        if (pref.height > 1){
Chengsong
parents:
diff changeset
   105
          val link = elem('|', 1, pref.height - 1)
Chengsong
parents:
diff changeset
   106
          (head left_align ((port above link) beside pref)) left_align tail_print(rs1)    
Chengsong
parents:
diff changeset
   107
        }
Chengsong
parents:
diff changeset
   108
        else{
Chengsong
parents:
diff changeset
   109
          (head left_align (port beside pref) ) left_align tail_print(rs1)
Chengsong
parents:
diff changeset
   110
        }
Chengsong
parents:
diff changeset
   111
      }
Chengsong
parents:
diff changeset
   112
    }
Chengsong
parents:
diff changeset
   113
  }
Chengsong
parents:
diff changeset
   114
  def tail_print(rs: List[ARexp]): Element = {
Chengsong
parents:
diff changeset
   115
    rs match {
Chengsong
parents:
diff changeset
   116
      case r2::r1::Nil => {
Chengsong
parents:
diff changeset
   117
        val pref = aregx_tree(r2)
Chengsong
parents:
diff changeset
   118
        val suff = aregx_tree(r1)
Chengsong
parents:
diff changeset
   119
        if (pref.height > 1){
Chengsong
parents:
diff changeset
   120
          val link = elem('|', 1, pref.height - 1)
Chengsong
parents:
diff changeset
   121
          ((port above link) beside pref) left_align (port up_align suff)
Chengsong
parents:
diff changeset
   122
        }
Chengsong
parents:
diff changeset
   123
        else{
Chengsong
parents:
diff changeset
   124
          (port beside pref) left_align (port up_align suff)
Chengsong
parents:
diff changeset
   125
        } 
Chengsong
parents:
diff changeset
   126
      }
Chengsong
parents:
diff changeset
   127
      case r2::rs1 => {
Chengsong
parents:
diff changeset
   128
        val pref = aregx_tree(r2)
Chengsong
parents:
diff changeset
   129
        
Chengsong
parents:
diff changeset
   130
        if (pref.height > 1){
Chengsong
parents:
diff changeset
   131
          val link = elem('|', 1, pref.height - 1)
Chengsong
parents:
diff changeset
   132
          ((port above link) beside pref) left_align tail_print(rs1)//(port up_align tail_print(rs1) )
Chengsong
parents:
diff changeset
   133
        }
Chengsong
parents:
diff changeset
   134
        else{
Chengsong
parents:
diff changeset
   135
          (port beside pref) left_align tail_print(rs1)//(port up_align tail_print(rs1))
Chengsong
parents:
diff changeset
   136
        } 
Chengsong
parents:
diff changeset
   137
        //pref left_align tail_print(rs1)
Chengsong
parents:
diff changeset
   138
      }
Chengsong
parents:
diff changeset
   139
    }
Chengsong
parents:
diff changeset
   140
  }
Chengsong
parents:
diff changeset
   141
Chengsong
parents:
diff changeset
   142
  def binary_print(name: String, r1: ARexp, r2: ARexp): Element = {
Chengsong
parents:
diff changeset
   143
    val pref = aregx_tree(r1)
Chengsong
parents:
diff changeset
   144
    val suff = aregx_tree(r2)
Chengsong
parents:
diff changeset
   145
    val head = elem(name)
Chengsong
parents:
diff changeset
   146
    if (pref.height > 1){
Chengsong
parents:
diff changeset
   147
      val link = elem('|', 1, pref.height - 1)
Chengsong
parents:
diff changeset
   148
      (head left_align ((port above link) beside pref) ) left_align (port up_align suff)
Chengsong
parents:
diff changeset
   149
    }
Chengsong
parents:
diff changeset
   150
    else{
Chengsong
parents:
diff changeset
   151
      (head left_align (port beside pref) ) left_align (port up_align suff)
Chengsong
parents:
diff changeset
   152
    }
Chengsong
parents:
diff changeset
   153
  }
Chengsong
parents:
diff changeset
   154
  val arr_of_size = ListBuffer.empty[Int]
2
cf169411b771 more comments
Chengsong
parents: 1
diff changeset
   155
0
Chengsong
parents:
diff changeset
   156
  def pC(r: Rexp): Set[Rexp] = {//PD's companion
Chengsong
parents:
diff changeset
   157
    r match {
Chengsong
parents:
diff changeset
   158
      case SEQ(r1, r2) => pC(r2)
Chengsong
parents:
diff changeset
   159
      case ALTS(rs) => rs.flatMap(a => pC(a) ).toSet
Chengsong
parents:
diff changeset
   160
      case CHAR(c) => Set(r)
Chengsong
parents:
diff changeset
   161
      case r => Set()
Chengsong
parents:
diff changeset
   162
    }
Chengsong
parents:
diff changeset
   163
  }
Chengsong
parents:
diff changeset
   164
  def illustration(r: Rexp, s: String){
Chengsong
parents:
diff changeset
   165
    var i_like_imperative_style = internalise(r)
Chengsong
parents:
diff changeset
   166
    val all_chars = s.toList
Chengsong
parents:
diff changeset
   167
    for (i <- 0 to s.length - 1){
Chengsong
parents:
diff changeset
   168
      val der_res =  bder(all_chars(i), i_like_imperative_style)
Chengsong
parents:
diff changeset
   169
      val simp_res = bsimp(der_res)
Chengsong
parents:
diff changeset
   170
      println("The original regex, the regex after derivative w.r.t " + all_chars(i) + " and the simplification of the derivative.")
Chengsong
parents:
diff changeset
   171
      println(aregx_tree(i_like_imperative_style) up_align aregx_tree(der_res) up_align aregx_tree(simp_res))
Chengsong
parents:
diff changeset
   172
      //println(asize(i_like_imperative_style), asize(der_res), asize(simp_res))
Chengsong
parents:
diff changeset
   173
      arr_of_size += asize(i_like_imperative_style)
Chengsong
parents:
diff changeset
   174
      //println(asize(simp_res), asize(simp_res) / arr_of_size(0) )
Chengsong
parents:
diff changeset
   175
      i_like_imperative_style = simp_res
Chengsong
parents:
diff changeset
   176
    }
Chengsong
parents:
diff changeset
   177
    arr_of_size += asize(i_like_imperative_style)
Chengsong
parents:
diff changeset
   178
  }
Chengsong
parents:
diff changeset
   179
  val ran = scala.util.Random
Chengsong
parents:
diff changeset
   180
  var alphabet_size = 3
Chengsong
parents:
diff changeset
   181
  def balanced_seq_star_gen(depth: Int, star: Boolean): Rexp = {
Chengsong
parents:
diff changeset
   182
    if(depth == 1){
Chengsong
parents:
diff changeset
   183
      ((ran.nextInt(4) + 97).toChar).toString
Chengsong
parents:
diff changeset
   184
    }
Chengsong
parents:
diff changeset
   185
    else if(star){
Chengsong
parents:
diff changeset
   186
      STAR(balanced_seq_star_gen(depth - 1, false))
Chengsong
parents:
diff changeset
   187
    }
Chengsong
parents:
diff changeset
   188
    else{
Chengsong
parents:
diff changeset
   189
      SEQ(balanced_seq_star_gen(depth - 1, true), balanced_seq_star_gen(depth - 1, true))
Chengsong
parents:
diff changeset
   190
    }
Chengsong
parents:
diff changeset
   191
  }
Chengsong
parents:
diff changeset
   192
  def max(i: Int, j: Int): Int = {
Chengsong
parents:
diff changeset
   193
    if(i > j)
Chengsong
parents:
diff changeset
   194
      i
Chengsong
parents:
diff changeset
   195
    else 
Chengsong
parents:
diff changeset
   196
      j
Chengsong
parents:
diff changeset
   197
  }
Chengsong
parents:
diff changeset
   198
  def random_struct_gen(depth:Int): Rexp = {
Chengsong
parents:
diff changeset
   199
    val dice = ran.nextInt(3)
Chengsong
parents:
diff changeset
   200
    val dice2 = ran.nextInt(3)
Chengsong
parents:
diff changeset
   201
    (dice, depth) match {
Chengsong
parents:
diff changeset
   202
      case (_, 0) => ((ran.nextInt(3) + 97).toChar).toString
Chengsong
parents:
diff changeset
   203
      case (0, i) => STAR(random_struct_gen(max(0, i - 1 - dice2)))
Chengsong
parents:
diff changeset
   204
      case (1, i) => SEQ(random_struct_gen(max(0, i - 1 - dice2)), random_struct_gen(max(0, i - 1 - dice2)))
Chengsong
parents:
diff changeset
   205
      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
   206
    }
Chengsong
parents:
diff changeset
   207
  }
Chengsong
parents:
diff changeset
   208
  def balanced_struct_gen(depth: Int): Rexp = {
Chengsong
parents:
diff changeset
   209
    val dice = ran.nextInt(3)
Chengsong
parents:
diff changeset
   210
    (dice, depth) match {
Chengsong
parents:
diff changeset
   211
      case (_, 0) => ((ran.nextInt(3) + 97).toChar).toString
Chengsong
parents:
diff changeset
   212
      case (0, i) => STAR(random_struct_gen(depth - 1))
Chengsong
parents:
diff changeset
   213
      case (1, i) => SEQ(random_struct_gen(depth - 1), random_struct_gen(depth - 1))
Chengsong
parents:
diff changeset
   214
      case (2, i) => ALTS( List(random_struct_gen(depth - 1), random_struct_gen(depth - 1) ) )
Chengsong
parents:
diff changeset
   215
    }
Chengsong
parents:
diff changeset
   216
  }
Chengsong
parents:
diff changeset
   217
  def rd_string_gen(alp_size: Int, len: Int): String = {
Chengsong
parents:
diff changeset
   218
    if( len > 0)
Chengsong
parents:
diff changeset
   219
      ((ran.nextInt(alp_size) + 97).toChar).toString + rd_string_gen(alp_size, len - 1)
Chengsong
parents:
diff changeset
   220
    else
Chengsong
parents:
diff changeset
   221
      ((ran.nextInt(alp_size) + 97).toChar).toString
Chengsong
parents:
diff changeset
   222
  }
Chengsong
parents:
diff changeset
   223
  def plot(b: List[Int]){
Chengsong
parents:
diff changeset
   224
    println(b(0),b.max)
Chengsong
parents:
diff changeset
   225
Chengsong
parents:
diff changeset
   226
  }
Chengsong
parents:
diff changeset
   227
  def dep_exp(depth: List[Int]){
Chengsong
parents:
diff changeset
   228
    for(i <- depth){
Chengsong
parents:
diff changeset
   229
      arr_of_size.clear()
Chengsong
parents:
diff changeset
   230
      val s = rd_string_gen(alphabet_size, (i-8)*(i-8)+10)
Chengsong
parents:
diff changeset
   231
      val r = random_struct_gen(i)
Chengsong
parents:
diff changeset
   232
      println("depth: "+i)
Chengsong
parents:
diff changeset
   233
      illustration(r, s) //"abcabadaaadcabdbabcdaadbabbcbbdabdabbcbdbabdbcdb") 
Chengsong
parents:
diff changeset
   234
      //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
   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
      //println(s.length + " " + arr_of_size(s.length) + " ]" + " -140")
Chengsong
parents:
diff changeset
   245
    }
Chengsong
parents:
diff changeset
   246
  }
Chengsong
parents:
diff changeset
   247
  def case_study(ss: List[String], r: Rexp){
Chengsong
parents:
diff changeset
   248
    for(s <- ss){
Chengsong
parents:
diff changeset
   249
      arr_of_size.clear()
Chengsong
parents:
diff changeset
   250
      illustration(r, s)
Chengsong
parents:
diff changeset
   251
      println("x y label alignment")
Chengsong
parents:
diff changeset
   252
      for(i <- 0 to s.length - 1){
Chengsong
parents:
diff changeset
   253
        if(s(i) == '\n')
Chengsong
parents:
diff changeset
   254
          println(i+" "+arr_of_size(i)+" "+"nl"+" -140")
Chengsong
parents:
diff changeset
   255
        else if(s(i) ==  ' ')
Chengsong
parents:
diff changeset
   256
          println(i+" "+arr_of_size(i)+" "+"sp"+" -140")
Chengsong
parents:
diff changeset
   257
        else
Chengsong
parents:
diff changeset
   258
          println(i+" "+arr_of_size(i)+" "+s(i)+" -140")
Chengsong
parents:
diff changeset
   259
      }
Chengsong
parents:
diff changeset
   260
    }
Chengsong
parents:
diff changeset
   261
  }
Chengsong
parents:
diff changeset
   262
  def star_gen(dp: Int): Rexp = {
Chengsong
parents:
diff changeset
   263
    if(dp > 0)
Chengsong
parents:
diff changeset
   264
      STAR(star_gen(dp - 1))
Chengsong
parents:
diff changeset
   265
    else
Chengsong
parents:
diff changeset
   266
      "a"
Chengsong
parents:
diff changeset
   267
  }
Chengsong
parents:
diff changeset
   268
  def strs_gen(len: Int, num: Int): List[String] = {
Chengsong
parents:
diff changeset
   269
    if(num > 0){
Chengsong
parents:
diff changeset
   270
      rd_string_gen(3, len)::strs_gen(len, num - 1)
Chengsong
parents:
diff changeset
   271
    }
Chengsong
parents:
diff changeset
   272
    else{
Chengsong
parents:
diff changeset
   273
      Nil
Chengsong
parents:
diff changeset
   274
    }
Chengsong
parents:
diff changeset
   275
  }
Chengsong
parents:
diff changeset
   276
  def regx_print(r: Rexp): String = {
Chengsong
parents:
diff changeset
   277
    r match {
Chengsong
parents:
diff changeset
   278
      case ZERO =>
Chengsong
parents:
diff changeset
   279
        "ZERO"
Chengsong
parents:
diff changeset
   280
      case CHAR(c) => {
Chengsong
parents:
diff changeset
   281
         //val Some(c) = alphabet.find(f)
Chengsong
parents:
diff changeset
   282
         "\"" + c.toString + "\""
Chengsong
parents:
diff changeset
   283
      }
Chengsong
parents:
diff changeset
   284
      case ONE => {
Chengsong
parents:
diff changeset
   285
        "ONE"
Chengsong
parents:
diff changeset
   286
      }
Chengsong
parents:
diff changeset
   287
      case ALTS(rs) => {
Chengsong
parents:
diff changeset
   288
        "ALTS(List("+(rs.map(regx_print)).foldLeft("")((a, b) => if(a == "") b else a + "," + b)+"))"
Chengsong
parents:
diff changeset
   289
      }
Chengsong
parents:
diff changeset
   290
      case SEQ(r1, r2) => {
Chengsong
parents:
diff changeset
   291
        "SEQ(" + regx_print(r1) + "," + regx_print(r2) + ")"
Chengsong
parents:
diff changeset
   292
      }
Chengsong
parents:
diff changeset
   293
      case STAR(r) => {
Chengsong
parents:
diff changeset
   294
        "STAR(" + regx_print(r) + ")"
Chengsong
parents:
diff changeset
   295
      }
Chengsong
parents:
diff changeset
   296
    }
Chengsong
parents:
diff changeset
   297
  }
Chengsong
parents:
diff changeset
   298
  val mkst = "abcdefghijklmnopqrstuvwxyz"
Chengsong
parents:
diff changeset
   299
  def weak_sub_check(r: Rexp, s: String, i: Int, f: (List[Rexp], Set[Rexp]) => Boolean){
Chengsong
parents:
diff changeset
   300
    //we first compute pders over the set of all strings on the alphabet
Chengsong
parents:
diff changeset
   301
    val pd = pderas(Set(r), i + 4)
Chengsong
parents:
diff changeset
   302
    //then "b-internalise" the regular expression into a brexp(this is essentially 
Chengsong
parents:
diff changeset
   303
    //attaching a bit Z to every alts to signify that they come from the original regular expression)
Chengsong
parents:
diff changeset
   304
    var old = brternalise(r)
Chengsong
parents:
diff changeset
   305
    //this is for comparison between normal simp and the weakened version of simp
Chengsong
parents:
diff changeset
   306
    //normal simp will be performed on syncold
Chengsong
parents:
diff changeset
   307
    //weakend simp will be performed on old
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   308
    var syncold = internalise(r)
0
Chengsong
parents:
diff changeset
   309
    val all_chars = s.toList
Chengsong
parents:
diff changeset
   310
    for (i <- 0 to s.length - 1){
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   311
      val syncder_res = bder(all_chars(i), syncold)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   312
      val syncsimp_res = super_bsimp(syncder_res)
0
Chengsong
parents:
diff changeset
   313
      //see brder for detailed explanation
Chengsong
parents:
diff changeset
   314
      //just changes bit Z to S when deriving an ALTS, 
Chengsong
parents:
diff changeset
   315
      //signifying that the structure has been "touched" and
Chengsong
parents:
diff changeset
   316
      //therefore able to be spilled in the bspill function
Chengsong
parents:
diff changeset
   317
      val der_res =  brder(all_chars(i), old)
Chengsong
parents:
diff changeset
   318
      val simp_res = br_simp(der_res)
Chengsong
parents:
diff changeset
   319
      val anatomy = bspill(simp_res)
Chengsong
parents:
diff changeset
   320
      //track if the number of regular expressions exceeds those in the PD set(remember PD means the pders over A*)
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   321
      if(f(List(berase(simp_res)), pd)  == false ){
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   322
        println(size(erase(syncsimp_res)))
0
Chengsong
parents:
diff changeset
   323
        println(size(berase(simp_res)))
3
f15dccc42c7b removing PRED
Chengsong
parents: 2
diff changeset
   324
        println(bregx_tree(simp_res))
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   325
        println(s)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   326
        println(i)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   327
        println(r)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   328
        println()
0
Chengsong
parents:
diff changeset
   329
        println(anatomy.map(size).sum)
Chengsong
parents:
diff changeset
   330
        println(pd.map(size).sum)
Chengsong
parents:
diff changeset
   331
      }  
Chengsong
parents:
diff changeset
   332
      old = simp_res
Chengsong
parents:
diff changeset
   333
      syncold = syncsimp_res
Chengsong
parents:
diff changeset
   334
    }
Chengsong
parents:
diff changeset
   335
  }
Chengsong
parents:
diff changeset
   336
  def inclusion_truth(anatomy: List[Rexp], pd: Set[Rexp]): Boolean = {
Chengsong
parents:
diff changeset
   337
    val aset = anatomy.toSet
Chengsong
parents:
diff changeset
   338
    if(aset subsetOf pd){
Chengsong
parents:
diff changeset
   339
      true
Chengsong
parents:
diff changeset
   340
    }
Chengsong
parents:
diff changeset
   341
    else{
Chengsong
parents:
diff changeset
   342
      println("inclusion not true")
Chengsong
parents:
diff changeset
   343
      false
Chengsong
parents:
diff changeset
   344
    }
Chengsong
parents:
diff changeset
   345
  }
Chengsong
parents:
diff changeset
   346
  def size_comp(anatomy: List[Rexp], pd: Set[Rexp]):Boolean = {println("size of PD and bspilled simp regx: ", pd.size, anatomy.size); true}
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   347
  def size_expansion_rate(r: List[Rexp], pd: Set[Rexp]): Boolean = if(size(r(0)) > (pd.map(size).sum) * 3 ) { false }else {true}
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   348
  def ders_simp(r: ARexp, s: List[Char]): ARexp = {
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   349
    s match {
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   350
      case Nil => r 
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   351
      case c::cs => ders_simp(bsimp(bder(c, r)), cs)
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   352
    }
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   353
  }
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   354
  val big_panda = STAR(STAR(STAR(ALTS(List(ALTS(List(CHAR('c'), CHAR('b'))), SEQ(CHAR('c'),CHAR('c')))))))
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   355
  val str_panda = "ccccb"
0
Chengsong
parents:
diff changeset
   356
  def check_all(){
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   357
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   358
        weak_sub_check(big_panda, str_panda, 6, size_expansion_rate)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   359
0
Chengsong
parents:
diff changeset
   360
  }
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   361
  //simplified regex size 291, so called pd_simp size 70 (did not do simplification to terms in the PD set)
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   362
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   363
  def correctness_proof_convenient_path(){
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   364
    for(i <- 1 to 1)
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   365
    {
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   366
        val s = "abaa"//rd_string_gen(alphabet_size, 3)
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   367
        val r = ASTAR(List(),AALTS(List(),List(ASTAR(List(Z),ACHAR(List(),'a')), ASEQ(List(S),ACHAR(List(),'a'),ACHAR(List(),'b')))))//internalise(balanced_struct_gen(3))//SEQ(ALTS(List(STAR("a"),ALTS(List("a","c")))),SEQ(ALTS(List("c","a")),ALTS(List("c","b")))) //random_struct_gen(7)
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   368
        for(j <- 0 to s.length - 1){
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   369
          val ss = s.slice(0, j+ 1)
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   370
          val nangao = ders_simp(r, ss.toList)
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   371
          val easy = bsimp(bders(ss.toList, r))
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   372
          if(true){
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   373
            println(j)
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   374
            if(j == 3) println("not equal")
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   375
            println("string")
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   376
            println(ss)
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   377
            println("original regex")
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   378
            println(r)
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   379
            println("regex after ders simp")
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   380
            println(nangao)
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   381
            println("regex after ders")
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   382
            println(bders(ss.toList, r))
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   383
            println("regex after ders and then a single simp")
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   384
            println(easy)
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   385
          }
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   386
        }
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   387
    }
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   388
  }
0
Chengsong
parents:
diff changeset
   389
  def main(args: Array[String]) {
4
7a349fe58bf4 :test whether bsimp(bders(r, s)) == ders_simp(r,s)
Chengsong
parents: 3
diff changeset
   390
    //check_all()   
5
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   391
    enum(3, "abc").map(tests_blexer_simp(strs(3, "abc"))).toSet
622ddbb1223a correctness test with enumeration
Chengsong
parents: 4
diff changeset
   392
    //correctness_proof_convenient_path()
0
Chengsong
parents:
diff changeset
   393
  } 
Chengsong
parents:
diff changeset
   394
}
Chengsong
parents:
diff changeset
   395