ExampleWorkflows.scala 2.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package de.tum.workflows

import foltl.FOLTL._
import blocks._
import Implicits._
import java.io.File
import java.io.FilenameFilter
import scala.io.Source
import com.typesafe.scalalogging.LazyLogging

object ExampleWorkflows extends LazyLogging {
  
13 14
  val FOLDER = new File("examples")
  val ENDING = ".spec"
15
  
Christian Müller's avatar
Christian Müller committed
16 17 18 19 20 21 22 23 24 25 26 27
  def listExamples(prefix:String, folder:File):Map[String,File] = {
    val sublists = (for (f <- folder.listFiles if f.isDirectory()) yield {
       listExamples(s"$prefix${f.getName}/", f)
     }) flatten 
    
     val here = (for (f <- folder.listFiles() if f.isFile() && f.getName.endsWith(ENDING)) yield {
       prefix + f.getName.replace(ENDING, "") -> f
     }) toList
     
     (sublists toMap) ++ here
  }
  
Christian Müller's avatar
Christian Müller committed
28
  def parseExample(s:String):Option[NISpec] = {
Christian Müller's avatar
Christian Müller committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    if (!examples.contains(s)) {
      logger.error(s"$s not contained in list of examples")
      None
    } else {
      val f = examples(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.checkSanity()) {
          logger.error(s"Sanity check of $f failed. Skipping file.")
        	None
        } else {
        	Some(spec.get)
        }
      }
    }
  }
49
  
Christian Müller's avatar
Christian Müller committed
50
  def parsedExamples(prefix:String, folder:File):Map[String, NISpec] = {
Christian Müller's avatar
Christian Müller committed
51
    val map = for ((name, f) <- examples) yield {
52 53
      val s = Source.fromFile(f).mkString
      val spec = WorkflowParser.parseSpec(s)
Christian Müller's avatar
Christian Müller committed
54

Christian Müller's avatar
Christian Müller committed
55
      logger.info(s"Parsing file $f")
Christian Müller's avatar
Christian Müller committed
56

57
      if (!spec.successful) {
Christian Müller's avatar
Christian Müller committed
58 59
        logger.error(s"Parsing of $f unsuccessful: $spec")
        name -> None
60
      } else {
Christian Müller's avatar
Christian Müller committed
61 62
        if (!spec.get.checkSanity()) {
          logger.error(s"Sanity check of $f failed. Skipping file.")
Christian Müller's avatar
Christian Müller committed
63
          name -> None
Christian Müller's avatar
Christian Müller committed
64
        } else {
Christian Müller's avatar
Christian Müller committed
65
          name -> Some(spec.get)
Christian Müller's avatar
Christian Müller committed
66
        }
67
      }
Christian Müller's avatar
Christian Müller committed
68
    }
69
    
Christian Müller's avatar
Christian Müller committed
70
    // filter out all empty specs and those that are not sane
Christian Müller's avatar
Christian Müller committed
71
    for ((k, v) <- map if (v.isDefined)) yield {
72
      (k, v.get)
Christian Müller's avatar
Christian Müller committed
73
    }
74 75
  }
  
Christian Müller's avatar
Christian Müller committed
76
  val examples = listExamples("", FOLDER)
77
}