/**
 * Tools related to the ontology repository
 */

package uk.ac.man.cs.lethe.internal.application.benchmarks


import java.io.File


// Print statistics about the current benchmark
object BenchmarkStatistics { 
  def main(args: Array[String]): Unit = { 
    new Benchmark(new File(args(0))).printStatistics()
  }
} 

// trait OntologyRepository { 
//   def ontologies: Iterable[String]
//   def ontology(index: Int): String
// }

// class SimpleRepository(folder: File) extends OntologyRepository { 

//   val files = folder.list

//   override def ontologies = files

//   override def ontology(index: Int) = files(index)
// }

class Benchmark private (folder: File, csv: CSV) { //extends OntologyRepository{ 

  def this(folder: File) = { 
    // indices have to be stored since they are needed for ontology retrieval
    // (the initial indices in the csv-file form the ids of the ontologies)
      this(folder, CSVParser.parse(new File(folder.getAbsolutePath()+"/all.csv")).addIndexColumn("index"))
  } 

  
  def ontologies: Iterable[String] = 
    csv.column("index").map(index => ontology(index.toInt))

  // restricts the benchmark to ontologies satisfying one of the values of the given property  
  def filterOntologies(property: String, values: Iterable[String]): Benchmark = 
    new Benchmark(folder, csv.filter(property, values.toSet))

  def filterOutOntologies(property: String, values: Iterable[String]): Benchmark = 
    new Benchmark(folder, csv.filterOut(property, values.toSet))
 

  def ontology(index: Int): String = {  
    val path = new File(folder.getAbsolutePath()+"/ontologies/"+index.toString)
    if(!path.exists())
      return null
    
    path.toString+"/"+path.list().filter(_.endsWith(".mont"))(0)
  }

  def printStatistics() = { 
    println("DL-families in the benchmark: ")

    val families = csv.column("expressivity").toSet.toSeq.sorted

    families.foreach{ family =>
      println(family+": \t"+csv.filter("expressivity", family).size)
    }
  }
}
