Examples.scala 3.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
package de.tum.niwo

import blocks._
import java.io.File

import scala.io.Source
import com.typesafe.scalalogging.LazyLogging
import de.tum.niwo.parser.{TransitionSystemParser, WorkflowParser}


object Examples extends LazyLogging {

  private val BASE = new File("examples/").toPath
  private val WFFOLDERS = List("tests/", "omitting/", "nonomitting/")
  private val TSFOLDERS = List("tstests/")
  private val ENDING = ".spec"
  
  def listExamples(folder:File):Map[String,File] = {

    val sublists = (for (f <- folder.listFiles if f.isDirectory) yield {
       listExamples(f)
     }).flatten
    
     val here = (for (f <- folder.listFiles() if f.isFile && f.getName.endsWith(ENDING)) yield {
       val name = BASE.relativize(f.toPath).toString.stripSuffix(ENDING)
       name -> f
     }).toList
     
     sublists.toMap ++ here
  }
  
  def parseExampleWF(s:String):Option[NISpec] = {
    if (!wfexamples.contains(s)) {
      logger.error(s"$s not contained in list of examples")
      None
    } else {
      val f = wfexamples(s)
      val source = Source.fromFile(f).mkString
      val spec = WorkflowParser.parseSpec(source)
      if (!spec.successful) {
      	logger.error(s"Parsing of $f unsuccessful: $spec")
      	None
      } else {
        if (!spec.get.isSane()) {
          logger.error(s"Sanity check of $f failed. Skipping file.")
        	None
        } else {
        	Some(spec.get)
        }
      }
    }
  }
  
  def parsedWFExamples():Map[String, NISpec] = {
    val map = for ((name, f) <- wfexamples) yield {
      val s = Source.fromFile(f).mkString
      val spec = WorkflowParser.parseSpec(s)

      logger.info(s"Parsing file $f")

      if (!spec.successful) {
        logger.error(s"Parsing of $f unsuccessful: $spec")
        name -> None
      } else {
        if (!spec.get.isSane()) {
          logger.error(s"Sanity check of $f failed. Skipping file.")
          name -> None
        } else {
          name -> Some(spec.get)
        }
      }
    }
    
    // filter out all empty specs and those that are not sane
    for ((k, v) <- map if v.isDefined) yield {
      (k, v.get)
    }
  }

  def parseExampleTS(s:String):Option[InvariantSpec] = {
    if (!tsexamples.contains(s)) {
      logger.error(s"$s not contained in list of examples")
      None
    } else {
      val f = tsexamples(s)
      val source = Source.fromFile(f).mkString
      val spec = TransitionSystemParser.parseSpec(source)
      if (!spec.successful) {
        logger.error(s"Parsing of $f unsuccessful: $spec")
        None
      } else {
        if (!spec.get.isSane) {
          logger.error(s"Sanity check of $f failed. Skipping file.")
          None
        } else {
          Some(spec.get)
        }
      }
    }
  }

  def parsedTSExamples():Map[String, InvariantSpec] = {
    val map = for ((name, f) <- tsexamples) yield {
      val s = Source.fromFile(f).mkString
      logger.info(s"Parsing file $f")

      val spec = TransitionSystemParser.parseSpec(s)

      if (!spec.successful) {
        logger.error(s"Parsing of $f unsuccessful: $spec")
        name -> None
      } else {
        if (!spec.get.isSane) {
          logger.error(s"Sanity check of $f failed. Skipping file.")
          name -> None
        } else {
          name -> Some(spec.get)
        }
      }
    }

    // filter out all empty specs and those that are not sane
    for ((k, v) <- map if v.isDefined) yield {
      (k, v.get)
    }
  }
  
  val wfexamples: Map[String, File] = WFFOLDERS.flatMap(wffolder => listExamples(new File(BASE.toFile, wffolder))).toMap
  val tsexamples: Map[String, File] = TSFOLDERS.flatMap(tsfolder => listExamples(new File(BASE.toFile, tsfolder))).toMap
}