src/Tools/Find_Facts/src/find_facts.scala
author wenzelm
Sat, 11 Jan 2025 23:33:55 +0100
changeset 81771 5589ab62869e
parent 81770 f54881ce5cf3
child 81772 c405ad565d70
permissions -rw-r--r--
clarified options;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
81764
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/Find_Facts.scala
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
     2
    Author:     Fabian Huch, TU Muenchen
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
     3
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
     4
Full-text search engine for Isabelle (including web server), using Solr as backend.
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
     5
*/
81770
f54881ce5cf3 proper component src/Tools/Find_Facts;
wenzelm
parents: 81764
diff changeset
     6
f54881ce5cf3 proper component src/Tools/Find_Facts;
wenzelm
parents: 81764
diff changeset
     7
package isabelle.find_facts
81764
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
     8
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
     9
81770
f54881ce5cf3 proper component src/Tools/Find_Facts;
wenzelm
parents: 81764
diff changeset
    10
import isabelle._
f54881ce5cf3 proper component src/Tools/Find_Facts;
wenzelm
parents: 81764
diff changeset
    11
81764
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    12
import scala.annotation.tailrec
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    13
import scala.collection.immutable.TreeMap
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    14
import scala.collection.mutable
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    15
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    16
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    17
object Find_Facts {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    18
  /** blocks **/
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    19
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    20
  object Kind {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    21
    val CONST = "constant"
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    22
    val TYPE = "type"
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    23
    val THM = "fact"
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    24
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    25
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    26
  case class Block(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    27
    id: String,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    28
    version: Long,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    29
    chapter: String,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    30
    session: String,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    31
    theory: String,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    32
    file: String,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    33
    url_path: Path,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    34
    command: String,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    35
    start_line: Int,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    36
    src_before: String,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    37
    src: String,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    38
    src_after: String,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    39
    xml: XML.Body,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    40
    html: String,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    41
    entity_kname: Option[String],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    42
    consts: List[String],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    43
    typs: List[String],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    44
    thms: List[String]
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    45
  ) {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    46
    def path = Path.explode(file)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    47
    def file_type: String = path.get_ext
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    48
    def file_name: String = path.file_name
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    49
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    50
    def kinds: List[String] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    51
      (if (typs.nonEmpty) List(Kind.TYPE) else Nil) :::
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    52
      (if (consts.nonEmpty) List(Kind.CONST) else Nil) :::
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    53
      (if (thms.nonEmpty) List(Kind.THM) else Nil)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    54
    def names: List[String] = (typs ::: consts ::: thms).distinct
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    55
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    56
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    57
  case class Blocks(num_found: Long, blocks: List[Block], cursor: String)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    58
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    59
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    60
  /** queries */
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    61
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    62
  enum Atom {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    63
    case Exact(s: String) extends Atom
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    64
    case Value(s: String) extends Atom
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    65
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    66
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    67
  enum Field {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    68
    case chapter, session, file_type, theory, command, source, names, consts, typs, thms, kinds
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    69
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    70
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    71
  sealed trait Filter
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    72
  case class Field_Filter(field: Field, either: List[Atom]) extends Filter
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    73
  case class Any_Filter(either: List[Atom]) extends Filter {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    74
    def fields: List[Field] = List(Field.session, Field.theory, Field.source, Field.names)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    75
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    76
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    77
  case class Select(field: Field, values: List[String])
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    78
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    79
  object Query {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    80
    def apply(filters: Filter*): Query = new Query(filters.toList)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    81
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    82
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    83
  case class Query(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    84
    filters: List[Filter] = Nil,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    85
    exclude: List[Filter] = Nil,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    86
    selects: List[Select] = Nil)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    87
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    88
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    89
  /* stats and facets */
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    90
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    91
  case class Stats(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    92
    results: Long,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    93
    sessions: Long,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    94
    theories: Long,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    95
    commands: Long,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    96
    consts: Long,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    97
    typs: Long,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    98
    thms: Long)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
    99
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   100
  case class Facets(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   101
    chapter: Map[String, Long],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   102
    session: Map[String, Long],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   103
    theory: Map[String, Long],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   104
    file_type: Map[String, Long],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   105
    command: Map[String, Long],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   106
    kinds: Map[String, Long],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   107
    consts: Map[String, Long],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   108
    typs: Map[String, Long],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   109
    thms: Map[String, Long])
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   110
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   111
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   112
  /** Solr data model **/
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   113
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   114
  object private_data extends Solr.Data("find_facts") {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   115
    /* types */
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   116
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   117
    val symbol_codes =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   118
      for {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   119
        entry <- Symbol.symbols.entries
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   120
        code <- entry.decode.toList
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   121
        input <- entry.symbol :: entry.abbrevs
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   122
      } yield input -> code
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   123
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   124
    val replacements =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   125
      for ((symbol, codes) <- symbol_codes.groupMap(_._1)(_._2).toList if codes.length == 1)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   126
      yield symbol -> Library.the_single(codes)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   127
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   128
    val Special_Char = """(.*[(){}\[\].,:"].*)""".r
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   129
    val Arrow = """(.*=>.*)""".r
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   130
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   131
    val synonyms =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   132
      for {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   133
        (symbol, code) <- symbol_codes
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   134
        if !Special_Char.matches(symbol) && !Arrow.matches(symbol)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   135
      } yield symbol + " => " + code
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   136
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   137
    override lazy val more_config = Map(Path.basic("synonyms.txt") -> synonyms.mkString("\n"))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   138
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   139
    object Types {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   140
      private val strip_html = Solr.Class("charFilter", "HTMLStripCharFilterFactory")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   141
      private val replace_symbol_chars =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   142
        replacements.collect {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   143
          case (Special_Char(pattern), code) =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   144
            Solr.Class(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   145
              "charFilter", "PatternReplaceCharFilterFactory",
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   146
              List("pattern" -> ("\\Q" + pattern + "\\E"), "replacement" -> code))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   147
        }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   148
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   149
      private val symbol_pattern =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   150
         """\s*(::|[(){}\[\].,:"]|[^\p{ASCII}]|((?![^\p{ASCII}])[^(){}\[\].,:"\s])+)\s*""".r
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   151
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   152
      private val tokenize =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   153
        Solr.Class("tokenizer", "WhitespaceTokenizerFactory", List("rule" -> "java"))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   154
      private val tokenize_symbols =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   155
        Solr.Class("tokenizer", "PatternTokenizerFactory",
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   156
          List("pattern" -> symbol_pattern.toString, "group" -> "1"))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   157
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   158
      private val to_lower = Solr.Class("filter", "LowerCaseFilterFactory")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   159
      private val add_ascii =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   160
        Solr.Class("filter", "ASCIIFoldingFilterFactory", List("preserveOriginal" -> "true"))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   161
      private val delimit_words =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   162
        Solr.Class("filter", "WordDelimiterGraphFilterFactory", List(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   163
          "splitOnCaseChange" -> "0", "stemEnglishPossessive" -> "0", "preserveOriginal" -> "1"))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   164
      private val flatten = Solr.Class("filter", "FlattenGraphFilterFactory")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   165
      private val replace_symbols =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   166
        Solr.Class("filter", "SynonymGraphFilterFactory", List("synonyms" -> "synonyms.txt"))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   167
      private val replace_special_symbols =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   168
        replacements.collect {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   169
          case (Arrow(arrow), code) =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   170
            Solr.Class("filter", "PatternReplaceFilterFactory",
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   171
              List("pattern" -> ("\\Q" + arrow + "\\E"), "replacement" -> code))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   172
        }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   173
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   174
      val source =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   175
        Solr.Type("name", "TextField", Nil, List(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   176
          XML.Elem(Markup("analyzer", List("type" -> "index")),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   177
            List(strip_html, tokenize_symbols, to_lower, add_ascii, delimit_words, flatten)),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   178
          XML.Elem(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   179
            Markup("analyzer", List("type" -> "query")),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   180
              replace_symbol_chars ::: tokenize_symbols :: replace_symbols ::
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   181
                replace_special_symbols ::: to_lower :: Nil)))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   182
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   183
      val name =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   184
        Solr.Type("source", "TextField", Nil, List(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   185
          XML.Elem(Markup("analyzer", List("type" -> "index")),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   186
            List(tokenize, to_lower, delimit_words, flatten)),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   187
          XML.Elem(Markup("analyzer", List("type" -> "query")), List(tokenize, to_lower))))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   188
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   189
      val text = Solr.Type("text", "TextField")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   190
    }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   191
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   192
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   193
    /* fields */
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   194
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   195
    object Fields {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   196
      val id = Solr.Field("id", Solr.Type.string).make_unique_key
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   197
      val version = Solr.Field("version", Solr.Type.long, Solr.Column_Wise(true))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   198
      val chapter = Solr.Field("chapter", Solr.Type.string, Solr.Column_Wise(true))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   199
      val session = Solr.Field("session", Types.name)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   200
      val session_facet = Solr.Field("session_facet", Solr.Type.string, Solr.Stored(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   201
      val theory = Solr.Field("theory", Types.name)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   202
      val theory_facet = Solr.Field("theory_facet", Solr.Type.string, Solr.Stored(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   203
      val file = Solr.Field("file", Solr.Type.string, Solr.Indexed(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   204
      val file_type =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   205
        Solr.Field("file_type", Solr.Type.string, Solr.Column_Wise(true) ::: Solr.Stored(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   206
      val url_path = Solr.Field("url_path", Solr.Type.string, Solr.Indexed(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   207
      val command = Solr.Field("command", Solr.Type.string, Solr.Column_Wise(true))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   208
      val start_line = Solr.Field("start_line", Solr.Type.int, Solr.Column_Wise(true))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   209
      val src_before = Solr.Field("src_before", Solr.Type.string, Solr.Indexed(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   210
      val src_after = Solr.Field("src_after", Solr.Type.string, Solr.Indexed(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   211
      val src = Solr.Field("src", Types.source)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   212
      val xml = Solr.Field("xml", Solr.Type.bytes, Solr.Indexed(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   213
      val html = Solr.Field("html", Solr.Type.bytes, Solr.Indexed(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   214
      val entity_kname = Solr.Field("entity_kname", Solr.Type.string, Solr.Indexed(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   215
      val consts = Solr.Field("consts", Types.name, Solr.Multi_Valued(true))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   216
      val consts_facet =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   217
        Solr.Field("consts_facet", Solr.Type.string, Solr.Multi_Valued(true) ::: Solr.Stored(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   218
      val typs = Solr.Field("typs", Types.name, Solr.Multi_Valued(true))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   219
      val typs_facet =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   220
        Solr.Field("typs_facet", Solr.Type.string, Solr.Multi_Valued(true) ::: Solr.Stored(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   221
      val thms = Solr.Field("thms", Types.name, Solr.Multi_Valued(true))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   222
      val thms_facet =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   223
        Solr.Field("thms_facet", Solr.Type.string, Solr.Multi_Valued(true) ::: Solr.Stored(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   224
      val names = Solr.Field("names", Types.name, Solr.Multi_Valued(true) ::: Solr.Stored(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   225
      val kinds =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   226
        Solr.Field("kinds", Solr.Type.string,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   227
          Solr.Multi_Valued(true) ::: Solr.Column_Wise(true) ::: Solr.Stored(false))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   228
    }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   229
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   230
    lazy val fields: Solr.Fields = Solr.Fields(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   231
      Fields.id, Fields.version, Fields.chapter, Fields.session, Fields.session_facet,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   232
      Fields.theory, Fields.theory_facet, Fields.file, Fields.file_type, Fields.url_path,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   233
      Fields.command, Fields.start_line, Fields.src_before, Fields.src_after, Fields.src,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   234
      Fields.xml, Fields.html, Fields.entity_kname, Fields.consts, Fields.consts_facet, Fields.typs,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   235
      Fields.typs_facet, Fields.thms, Fields.thms_facet, Fields.names, Fields.kinds)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   236
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   237
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   238
    /* operations */
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   239
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   240
    def read_domain(db: Solr.Database, q: Solr.Source): Set[String] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   241
      db.execute_query(Fields.id, List(Fields.id), None, 100000,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   242
        { results =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   243
          results.map(_.string(Fields.id)).toSet
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   244
        }, q = q)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   245
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   246
    def read_block(res: Solr.Result): Block = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   247
      val id = res.string(Fields.id)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   248
      val version = res.long(Fields.version)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   249
      val chapter = res.string(Fields.chapter)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   250
      val session = res.string(Fields.session)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   251
      val theory = res.string(Fields.theory)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   252
      val file = res.string(Fields.file)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   253
      val url_path = Path.explode(res.string(Fields.url_path))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   254
      val command = res.string(Fields.command)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   255
      val start_line = res.int(Fields.start_line)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   256
      val src_before = res.string(Fields.src_before)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   257
      val src = res.string(Fields.src)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   258
      val src_after = res.string(Fields.src_after)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   259
      val xml = YXML.parse_body(res.bytes(Fields.xml))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   260
      val html = res.bytes(Fields.html).text
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   261
      val entity_kname = res.get_string(Fields.entity_kname)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   262
      val consts = res.list_string(Fields.consts)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   263
      val typs = res.list_string(Fields.typs)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   264
      val thms = res.list_string(Fields.thms)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   265
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   266
      Block(id = id, version = version, chapter = chapter, session = session, theory = theory,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   267
        file = file, url_path = url_path, command = command, start_line = start_line, src_before =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   268
        src_before, src = src, src_after = src_after, xml = xml, html = html, entity_kname =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   269
        entity_kname, consts = consts, typs = typs, thms = thms)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   270
    }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   271
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   272
    def read_blocks(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   273
      db: Solr.Database,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   274
      q: Solr.Source,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   275
      fq: List[Solr.Source],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   276
      cursor: Option[String] = None,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   277
      max_results: Int = 10
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   278
    ): Blocks =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   279
      db.execute_query(Fields.id, stored_fields, cursor, max_results,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   280
        { results =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   281
          val next_cursor = results.next_cursor
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   282
          val blocks = results.map(read_block).toList
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   283
          Blocks(results.num_found, blocks, next_cursor)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   284
        }, q = q, fq = fq, more_chunks = 0)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   285
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   286
    def stream_blocks(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   287
      db: Solr.Database,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   288
      q: Solr.Source,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   289
      stream: Iterator[Block] => Unit,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   290
      cursor: Option[String] = None,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   291
    ): Unit =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   292
      db.execute_query(Fields.id, stored_fields, cursor, 10000,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   293
        { results =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   294
          stream(results.map(read_block))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   295
        }, q = q)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   296
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   297
    def update_theory(db: Solr.Database, theory_name: String, blocks: List[Block]): Unit =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   298
      db.transaction {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   299
        val delete =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   300
          read_domain(db, Solr.filter(Fields.theory, Solr.phrase(theory_name))) -- blocks.map(_.id)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   301
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   302
        if (delete.nonEmpty) db.execute_batch_delete(delete.toList)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   303
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   304
        db.execute_batch_insert(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   305
          for (block <- blocks) yield { (doc: Solr.Document) =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   306
            doc.string(Fields.id) = block.id
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   307
            doc.long(Fields.version) = block.version
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   308
            doc.string(Fields.chapter) = block.chapter
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   309
            doc.string(Fields.session) = block.session
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   310
            doc.string(Fields.session_facet) = block.session
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   311
            doc.string(Fields.theory) = block.theory
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   312
            doc.string(Fields.theory_facet) = block.theory
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   313
            doc.string(Fields.file) = block.file
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   314
            doc.string(Fields.file_type) = block.file_type
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   315
            doc.string(Fields.url_path) = block.url_path.implode
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   316
            doc.string(Fields.command) = block.command
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   317
            doc.int(Fields.start_line) = block.start_line
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   318
            doc.string(Fields.src_before) = block.src_before
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   319
            doc.string(Fields.src) = block.src
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   320
            doc.string(Fields.src_after) = block.src_after
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   321
            doc.bytes(Fields.xml) = YXML.bytes_of_body(block.xml)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   322
            doc.bytes(Fields.html) = Bytes(block.html)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   323
            doc.string(Fields.entity_kname) = block.entity_kname
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   324
            doc.string(Fields.consts) = block.consts
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   325
            doc.string(Fields.consts_facet) = block.consts
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   326
            doc.string(Fields.typs) = block.typs
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   327
            doc.string(Fields.typs_facet) = block.typs
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   328
            doc.string(Fields.thms) = block.thms
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   329
            doc.string(Fields.thms_facet) = block.thms
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   330
            doc.string(Fields.names) = block.names
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   331
            doc.string(Fields.kinds) = block.kinds
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   332
          })
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   333
      }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   334
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   335
    def read_theory(db: Solr.Database, theory_name: String): List[Block] = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   336
      val blocks = new mutable.ListBuffer[Block]
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   337
      stream_blocks(db, Solr.filter(Fields.theory_facet, Solr.phrase(theory_name)), {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   338
        res => blocks ++= res
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   339
      })
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   340
      blocks.toList
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   341
    }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   342
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   343
    def delete_session(db: Solr.Database, session_name: String): Unit =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   344
      db.transaction {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   345
        val delete = read_domain(db, Solr.filter(Fields.session, Solr.phrase(session_name)))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   346
        if (delete.nonEmpty) db.execute_batch_delete(delete.toList)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   347
      }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   348
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   349
    def query_stats(db: Solr.Database, q: Solr.Source, fq: List[Solr.Source]): Stats =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   350
      db.execute_stats_query(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   351
        List(Fields.session_facet, Fields.theory_facet, Fields.command, Fields.consts_facet,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   352
          Fields.typs_facet, Fields.thms_facet, Fields.start_line),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   353
        { res =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   354
          val results = res.count
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   355
          val sessions = res.count(Fields.session_facet)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   356
          val theories = res.count(Fields.theory_facet)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   357
          val commands = res.count(Fields.theory_facet)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   358
          val consts = res.count(Fields.consts_facet)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   359
          val typs = res.count(Fields.typs_facet)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   360
          val thms = res.count(Fields.thms_facet)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   361
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   362
          Stats(results, sessions, theories, commands, consts, typs, thms)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   363
        }, q = q, fq = fq)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   364
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   365
    def query_facets(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   366
      db: Solr.Database,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   367
      q: Solr.Source,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   368
      fq: List[Solr.Source],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   369
      max_terms: Int = 100
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   370
    ): Facets =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   371
      db.execute_facet_query(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   372
        List(Fields.chapter, Fields.session_facet, Fields.theory_facet, Fields.file_type,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   373
          Fields.command, Fields.kinds, Fields.consts_facet, Fields.typs_facet, Fields.thms_facet),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   374
        { res =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   375
          val chapter = res.string(Fields.chapter)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   376
          val sessions = res.string(Fields.session_facet)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   377
          val theories = res.string(Fields.theory_facet)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   378
          val file_types = res.string(Fields.file_type)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   379
          val commands = res.string(Fields.command)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   380
          val kinds = res.string(Fields.kinds)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   381
          val consts = res.string(Fields.consts_facet)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   382
          val typs = res.string(Fields.typs_facet)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   383
          val thms = res.string(Fields.thms_facet)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   384
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   385
          Facets(chapter, sessions, theories, file_types, commands, kinds, consts, typs, thms)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   386
        }, q = q, fq = fq, max_terms = max_terms)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   387
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   388
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   389
    /* queries */
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   390
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   391
    def solr_field(field: Field, select: Boolean = false): Solr.Field =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   392
      field match {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   393
        case Field.chapter => Fields.chapter
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   394
        case Field.session if select => Fields.session_facet
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   395
        case Field.session => Fields.session
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   396
        case Field.theory if select => Fields.theory_facet
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   397
        case Field.theory => Fields.theory
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   398
        case Field.file_type => Fields.file_type
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   399
        case Field.command => Fields.command
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   400
        case Field.source => Fields.src
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   401
        case Field.names => Fields.names
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   402
        case Field.consts if select => Fields.consts_facet
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   403
        case Field.consts => Fields.consts
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   404
        case Field.typs if select => Fields.typs_facet
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   405
        case Field.typs => Fields.typs
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   406
        case Field.thms if select => Fields.thms_facet
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   407
        case Field.thms => Fields.thms
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   408
        case Field.kinds => Fields.kinds
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   409
      }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   410
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   411
    def solr_query(query: Query): (Solr.Source, List[Solr.Source]) = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   412
      def solr_atom(atom: Atom): List[Solr.Source] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   413
        atom match {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   414
          case Atom.Value(s) if s.isEmpty => Nil
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   415
          case Atom.Value(s) if !s.exists(Solr.wildcard_char(_)) => List(Solr.term(s))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   416
          case Atom.Value(s) =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   417
            val terms = s.split("\\s+").toList.filterNot(_.isBlank)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   418
            if (terms.isEmpty) Nil else terms.map(Solr.wildcard)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   419
          case Atom.Exact(s) => List(Solr.phrase(s))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   420
        }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   421
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   422
      def solr_atoms(field: Field, atoms: List[Atom]): List[Solr.Source] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   423
        for {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   424
          atom <- atoms
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   425
          source <- solr_atom(atom)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   426
        } yield Solr.filter(solr_field(field), source)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   427
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   428
      def solr_filter(filter: Filter): List[Solr.Source] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   429
        filter match {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   430
          case Field_Filter(field, atoms) => solr_atoms(field, atoms)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   431
          case any@Any_Filter(atoms) => any.fields.flatMap(solr_atoms(_, atoms))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   432
        }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   433
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   434
      def solr_select(select: Select): Solr.Source = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   435
        val field = solr_field(select.field, select = true)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   436
        Solr.tag(field.name, Solr.filter(field, Solr.OR(select.values.map(Solr.phrase))))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   437
      }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   438
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   439
      val filter = query.filters.map(filter => Solr.OR(solr_filter(filter)))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   440
      val exclude = query.exclude.flatMap(solr_filter).map(Solr.exclude)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   441
      val selects = query.selects.map(solr_select)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   442
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   443
      (Solr.AND(filter ::: exclude), selects)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   444
    }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   445
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   446
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   447
  def query_block(db: Solr.Database, id: String): Option[Block] = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   448
    val q = Solr.filter(Find_Facts.private_data.Fields.id, Solr.phrase(id))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   449
    Find_Facts.private_data.read_blocks(db, q, Nil).blocks.headOption
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   450
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   451
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   452
  def query_blocks(db: Solr.Database, query: Query, cursor: Option[String] = None): Blocks = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   453
    val (q, fq) = Find_Facts.private_data.solr_query(query)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   454
    Find_Facts.private_data.read_blocks(db, q, fq, cursor = cursor)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   455
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   456
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   457
  def query_stats(db: Solr.Database, query: Query): Stats = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   458
    val (q, fq) = Find_Facts.private_data.solr_query(query)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   459
    Find_Facts.private_data.query_stats(db, q, fq)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   460
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   461
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   462
  def query_facet(db: Solr.Database, query: Query): Facets = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   463
    val (q, fq) = Find_Facts.private_data.solr_query(query)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   464
    Find_Facts.private_data.query_facets(db, q, fq)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   465
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   466
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   467
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   468
  /** indexing **/
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   469
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   470
  def make_thy_blocks(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   471
    options: Options,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   472
    session: Session,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   473
    browser_info_context: Browser_Info.Context,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   474
    document_info: Document_Info,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   475
    theory_context: Export.Theory_Context,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   476
    snapshot: Document.Snapshot,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   477
    chapter: String
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   478
  ): List[Block] = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   479
    val theory = theory_context.theory
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   480
    val entities = Export_Theory.read_theory(theory_context).entity_iterator.toList
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   481
    val session_name = theory_context.session_context.session_name
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   482
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   483
    val theory_info =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   484
      document_info.theory_by_name(session_name, theory).getOrElse(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   485
        error("No info for theory " + theory))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   486
    val thy_dir = browser_info_context.theory_dir(theory_info)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   487
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   488
    def make_node_blocks(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   489
      snapshot: Document.Snapshot,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   490
      command_ranges: List[(String, Text.Range)]
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   491
    ): List[Block] = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   492
      val version = snapshot.version.id
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   493
      val file = Path.explode(snapshot.node_name.node).squash.implode
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   494
      val url_path = thy_dir + browser_info_context.smart_html(theory_info, snapshot.node_name.node)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   495
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   496
      val elements =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   497
        Browser_Info.Elements(html = Browser_Info.default_elements.html - Markup.ENTITY)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   498
      val node_context = Browser_Info.Node_Context.empty
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   499
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   500
      val content = XML.content(snapshot.xml_markup(elements = Markup.Elements.empty))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   501
      def get_content(range: Text.Range): String =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   502
        Symbol.decode(Line.normalize(range.substring(content)))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   503
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   504
      val document = Line.Document(content.replace('\r', '\u001a'))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   505
      val num_lines = document.lines.length
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   506
      def content_range(start: Line.Position, stop: Line.Position): Text.Range =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   507
        Text.Range(document.offset(start).get, document.offset(stop).get)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   508
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   509
      val index = Symbol.Index(content)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   510
      val node_entities =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   511
        TreeMap.from(entities
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   512
          .filter(entity => entity.file == snapshot.node_name.node)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   513
          .groupBy(entity => index.decode(entity.range).start))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   514
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   515
      val rendering = new Rendering(snapshot, options, session)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   516
      val comment_ranges = rendering.comments(Text.Range.full).map(markup => ("", markup.range))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   517
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   518
      for ((command, range) <- command_ranges ::: comment_ranges) yield {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   519
        val line_range = document.range(range)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   520
        val start_line = line_range.start.line1
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   521
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   522
        val id = file + "|" + range.start + ".." + range.stop
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   523
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   524
        val src_before = get_content(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   525
          content_range(Line.Position((line_range.start.line - 5).max(0)), line_range.start))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   526
        val src = get_content(range)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   527
        val src_after = get_content(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   528
          content_range(line_range.stop, Line.Position((line_range.stop.line + 5).min(num_lines))))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   529
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   530
        val xml = snapshot.xml_markup(range, elements = elements.html)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   531
        val html =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   532
          HTML.output(node_context.make_html(elements, xml), hidden = true, structural = false)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   533
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   534
        val entities = node_entities.range(range.start, range.stop).values.toList.flatten.distinct
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   535
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   536
        def get_entities(kind: String): List[String] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   537
          for {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   538
            entity <- entities
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   539
            if entity.export_kind == kind
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   540
            if range.contains(index.decode(entity.range))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   541
          } yield entity.name
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   542
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   543
        val entity_kname = entities.sortBy(_.name.length).headOption.map(_.kname)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   544
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   545
        val typs = get_entities(Export_Theory.Kind.TYPE)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   546
        val consts = get_entities(Export_Theory.Kind.CONST)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   547
        val thms = get_entities(Export_Theory.Kind.THM)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   548
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   549
        Block(id = id, version = version, chapter = chapter, session = session_name, theory =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   550
          theory, file = file, url_path = url_path, command = command, start_line = start_line,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   551
          src_before = src_before, src = src, src_after = src_after, xml = xml, html = html,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   552
          entity_kname = entity_kname, consts = consts, typs = typs, thms = thms)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   553
      }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   554
    }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   555
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   556
    def expand_block(block: Thy_Blocks.Block): List[Thy_Blocks.Block] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   557
      block match {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   558
        case Thy_Blocks.Thy(inner) => inner.flatMap(expand_block)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   559
        case e@Thy_Blocks.Decl(inner) =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   560
          val inner1 = inner.flatMap(expand_block)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   561
          if (inner.length > 1) e :: inner1 else List(e)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   562
        case _ => List(block)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   563
      }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   564
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   565
    val thy_command_ranges =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   566
      for (block <- Thy_Blocks.read_blocks(snapshot).flatMap(expand_block))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   567
      yield (block.command, block.range)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   568
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   569
    make_node_blocks(snapshot, thy_command_ranges) :::
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   570
      (for {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   571
        blob_name <- snapshot.node_files.tail
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   572
        snapshot1 = snapshot.switch(blob_name)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   573
        if snapshot1.node.source_wellformed
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   574
        range = Text.Range.length(snapshot1.node.source)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   575
        block <- make_node_blocks(snapshot1, List(("", range)))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   576
      } yield block)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   577
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   578
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   579
  def find_facts_index(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   580
    options: Options,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   581
    sessions: List[String],
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   582
    dirs: List[Path] = Nil,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   583
    clean: Boolean = false,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   584
    progress: Progress = new Progress
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   585
  ): Unit = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   586
    val store = Store(options)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   587
    val database = options.string("find_facts_database_name")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   588
    val session = Session(options, Resources.bootstrap)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   589
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   590
    val selection = Sessions.Selection(sessions = sessions)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   591
    val session_structure = Sessions.load_structure(options, dirs = dirs).selection(selection)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   592
    val deps = Sessions.Deps.load(session_structure)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   593
    val browser_info_context = Browser_Info.context(session_structure)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   594
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   595
    if (sessions.isEmpty) progress.echo("Nothing to index")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   596
    else {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   597
      val stats =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   598
        using(Solr.init_database(database, Find_Facts.private_data, clean = clean)) { db =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   599
          using(Export.open_database_context(store)) { database_context =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   600
            val document_info = Document_Info.read(database_context, deps, sessions)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   601
            
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   602
            def index_session(session_name: String): Unit = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   603
              using(database_context.open_session0(session_name)) { session_context =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   604
                val info = session_structure(session_name)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   605
                progress.echo("Session " + info.chapter + "/" + session_name + " ...")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   606
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   607
                Find_Facts.private_data.delete_session(db, session_name)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   608
                deps(session_name).proper_session_theories.foreach { name =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   609
                  val theory_context = session_context.theory(name.theory)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   610
                  Build.read_theory(theory_context) match {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   611
                    case None => progress.echo_warning("No snapshot for theory " + name.theory)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   612
                    case Some(snapshot) =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   613
                      progress.echo("Theory " + name.theory + " ...")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   614
                      val blocks =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   615
                        make_thy_blocks(options, session, browser_info_context, document_info,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   616
                          theory_context, snapshot, info.chapter)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   617
                      Find_Facts.private_data.update_theory(db, theory_context.theory, blocks)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   618
                  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   619
                }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   620
              }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   621
            }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   622
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   623
            Par_List.map(index_session, sessions)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   624
          }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   625
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   626
          val query = Query(Field_Filter(Field.session, sessions.map(Atom.Exact(_))))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   627
          Find_Facts.query_stats(db, query)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   628
        }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   629
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   630
      progress.echo("Indexed " + stats.results + " blocks with " +
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   631
        stats.consts + " consts, " + stats.typs + " typs, " + stats.thms + " thms")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   632
    }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   633
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   634
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   635
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   636
  /* Isabelle tool wrapper */
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   637
81770
f54881ce5cf3 proper component src/Tools/Find_Facts;
wenzelm
parents: 81764
diff changeset
   638
  val isabelle_tool1 = Isabelle_Tool("find_facts_index", "index sessions for find_facts",
81764
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   639
    Scala_Project.here,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   640
    { args =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   641
      var clean = false
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   642
      val dirs = new mutable.ListBuffer[Path]
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   643
      var options = Options.init()
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   644
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   645
      val getopts = Getopts("""
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   646
  Usage: isabelle find_facts_index [OPTIONS] [SESSIONS ...]
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   647
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   648
    Options are:
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   649
      -c           clean previous index
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   650
      -d DIR       include session directory
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   651
      -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   652
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   653
    Index sessions for find_facts.
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   654
  """,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   655
        "c" -> (_ => clean = true),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   656
        "d:" -> (arg => dirs += Path.explode(arg)),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   657
        "o:" -> (arg => options = options + arg))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   658
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   659
      val sessions = getopts(args)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   660
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   661
      val progress = new Console_Progress()
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   662
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   663
      find_facts_index(options, sessions, dirs = dirs.toList, clean = clean, progress = progress)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   664
    })
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   665
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   666
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   667
  /** index components **/
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   668
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   669
  def find_facts_index_component(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   670
    options: Options,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   671
    target_dir: Path = Path.current,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   672
    progress: Progress = new Progress
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   673
  ): Unit = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   674
    val database = options.string("find_facts_database_name")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   675
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   676
    val component = "find_facts_index-" + database
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   677
    val component_dir =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   678
      Components.Directory(target_dir + Path.basic(component)).create(progress = progress)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   679
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   680
    Isabelle_System.copy_dir(Solr.database_dir(database), component_dir.path)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   681
    component_dir.write_settings("SOLR_COMPONENTS=\"$SOLR_COMPONENTS:$COMPONENT/" + database + "\"")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   682
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   683
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   684
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   685
  /* Isabelle tool wrapper */
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   686
81770
f54881ce5cf3 proper component src/Tools/Find_Facts;
wenzelm
parents: 81764
diff changeset
   687
  val isabelle_tool2 = Isabelle_Tool("find_facts_index_component",
81764
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   688
    "build component from find_facts index", Scala_Project.here,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   689
    { args =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   690
      var options = Options.init()
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   691
      var target_dir = Path.current
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   692
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   693
      val getopts = Getopts("""
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   694
  Usage: isabelle find_facts_index_component
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   695
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   696
    Options are:
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   697
      -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   698
      -D DIR       target directory (default ".")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   699
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   700
    Build component from finalized find_facts index with given name.
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   701
  """,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   702
        "o:" -> (arg => options = options + arg),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   703
        "D:" -> (arg => target_dir = Path.explode(arg)))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   704
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   705
      val more_args = getopts(args)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   706
      if (more_args.nonEmpty) getopts.usage()
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   707
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   708
      find_facts_index_component(options, target_dir = target_dir)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   709
    })
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   710
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   711
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   712
  /** querying **/
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   713
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   714
  /* requests and parsing */
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   715
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   716
  case class Query_Blocks(query: Query, cursor: String)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   717
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   718
  object Parse {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   719
    def atom(json: JSON.T): Option[Atom] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   720
      JSON.string(json, "value").map(Atom.Value(_)) orElse
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   721
        JSON.string(json, "exact").map(Atom.Exact(_))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   722
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   723
    def field(name: String): Option[Field] = Field.values.find(_.toString == name)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   724
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   725
    def filter(json: JSON.T): Option[Filter] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   726
      for {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   727
        atoms <- JSON.list(json, "either", atom)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   728
        filter <-
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   729
          JSON.string(json, "field") match {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   730
            case None => Some(Any_Filter(atoms))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   731
            case Some(name) => for (field <- field(name)) yield Field_Filter(field, atoms)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   732
          }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   733
      } yield filter
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   734
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   735
    def select(json: JSON.T): Option[Select] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   736
      for {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   737
        name <- JSON.string(json, "field")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   738
        field <- field(name)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   739
        values <- JSON.strings(json, "values")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   740
      } yield Select(field, values)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   741
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   742
    def query(json: JSON.T): Option[Query] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   743
      for {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   744
        filters <- JSON.list(json, "filters", filter)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   745
        exclude <- JSON.list(json, "exclude", filter)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   746
        selects <- JSON.list(json, "selects", select)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   747
      } yield Query(filters, exclude, selects)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   748
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   749
    def query_blocks(json: JSON.T): Option[Query_Blocks] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   750
      for {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   751
        query <- JSON.value(json, "query", query)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   752
        cursor <- JSON.string(json, "cursor")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   753
      } yield Query_Blocks(query, cursor)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   754
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   755
    def query_block(json: JSON.T): Option[String] = for (id <- JSON.string(json, "id")) yield id
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   756
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   757
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   758
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   759
  /* responses and encoding */
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   760
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   761
  case class Result(blocks: Blocks, facets: Facets)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   762
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   763
  class Encode(options: Options) {
81771
5589ab62869e clarified options;
wenzelm
parents: 81770
diff changeset
   764
    val library_base_url = Url(options.string("find_facts_url_library"))
5589ab62869e clarified options;
wenzelm
parents: 81770
diff changeset
   765
    val afp_base_url = Url(options.string("find_facts_url_afp"))
81764
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   766
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   767
    def url(block: Block): Url = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   768
      val base_url = if (block.chapter == AFP.chapter) afp_base_url else library_base_url
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   769
      base_url.resolve(block.url_path.implode)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   770
    }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   771
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   772
    def block(block: Block): JSON.T =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   773
      JSON.Object(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   774
        "id" -> block.id,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   775
        "chapter" -> block.chapter,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   776
        "session" -> block.session,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   777
        "theory" -> block.theory,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   778
        "file" -> block.file,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   779
        "file_name" -> block.file_name,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   780
        "url" -> url(block).toString,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   781
        "command" -> block.command,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   782
        "start_line" -> block.start_line,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   783
        "src_before" -> block.src_before,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   784
        "src_after" -> block.src_after,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   785
        "html" -> block.html,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   786
        "entity_kname" -> block.entity_kname.orNull)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   787
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   788
    def blocks(blocks: Blocks): JSON.T =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   789
      JSON.Object(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   790
        "num_found" -> blocks.num_found,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   791
        "blocks" -> blocks.blocks.map(block),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   792
        "cursor" -> blocks.cursor)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   793
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   794
    def facets(facet: Facets): JSON.T =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   795
      JSON.Object(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   796
        "chapter" -> facet.chapter,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   797
        "session" -> facet.session,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   798
        "file_type" -> facet.file_type,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   799
        "theory" -> facet.theory,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   800
        "command" -> facet.command,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   801
        "kinds" -> facet.kinds,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   802
        "consts" -> facet.consts,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   803
        "typs" -> facet.typs,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   804
        "thms" -> facet.thms)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   805
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   806
    def result(result: Result): JSON.T =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   807
      JSON.Object(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   808
        "blocks" -> blocks(result.blocks),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   809
        "facets" -> facets(result.facets))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   810
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   811
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   812
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   813
  /* find facts */
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   814
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   815
  abstract class REST_Service(path: Path, progress: Progress, method: String = "POST")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   816
    extends HTTP.Service(path.implode, method = method) {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   817
    def handle(body: JSON.T): Option[JSON.T]
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   818
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   819
    def apply(request: HTTP.Request): Option[HTTP.Response] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   820
      try {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   821
        for {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   822
          json <-
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   823
            Exn.capture(JSON.parse(request.input.text)) match {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   824
              case Exn.Res(json) => Some(json)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   825
              case _ =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   826
                progress.echo("Could not parse: " + quote(request.input.text), verbose = true)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   827
                None
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   828
            }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   829
          res <-
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   830
            handle(json) match {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   831
              case Some(res) => Some(res)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   832
              case None =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   833
                progress.echo("Invalid request: " + JSON.Format(json), verbose = true)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   834
                None
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   835
            }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   836
        } yield HTTP.Response(Bytes(JSON.Format(res)), content_type = "application/json")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   837
      }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   838
      catch { case exn: Throwable =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   839
        val uuid = UUID.random()
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   840
        progress.echo_error_message("Server error <" + uuid + ">: " + exn)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   841
        Some(HTTP.Response.text("internal server error: " + uuid))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   842
      }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   843
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   844
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   845
  def find_facts(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   846
    options: Options,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   847
    port: Int,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   848
    devel: Boolean = false,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   849
    progress: Progress = new Progress
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   850
  ): Unit = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   851
    val database = options.string("find_facts_database_name")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   852
    val encode = new Encode(options)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   853
    val logo = Bytes.read(Path.explode("$FIND_FACTS_HOME/web/favicon.ico"))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   854
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   855
    val isabelle_style = HTML.fonts_css("/fonts/" + _) + "\n\n" + File.read(HTML.isabelle_css)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   856
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   857
    val project = Elm.Project("Find_Facts", Path.explode("$FIND_FACTS_HOME/web"), head = List(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   858
      HTML.style("html,body {width: 100%, height: 100%}"),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   859
      Web_App.More_HTML.icon("data:image/x-icon;base64," + logo.encode_base64.text),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   860
      HTML.style_file("isabelle.css"),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   861
      HTML.style_file("https://fonts.googleapis.com/css?family=Roboto:300,400,500|Material+Icons"),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   862
      HTML.style_file(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   863
        "https://unpkg.com/material-components-web-elm@9.1.0/dist/material-components-web-elm.min.css"),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   864
      HTML.script_file(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   865
        "https://unpkg.com/material-components-web-elm@9.1.0/dist/material-components-web-elm.min.js")))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   866
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   867
    val frontend = project.build_html(progress)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   868
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   869
    using(Solr.open_database(database)) { db =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   870
      val stats = Find_Facts.query_stats(db, Query(Nil))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   871
      progress.echo("Started find facts with " + stats.results + " blocks, " +
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   872
        stats.consts + " consts, " + stats.typs + " typs, " + stats.thms + " thms")
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   873
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   874
      val server =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   875
        HTTP.server(port, name = "", services = List(
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   876
          HTTP.Fonts_Service,
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   877
          new HTTP.Service("isabelle.css") {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   878
            def apply(request: HTTP.Request): Option[HTTP.Response] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   879
              Some(HTTP.Response(Bytes(isabelle_style), "text/css"))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   880
          },
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   881
          new HTTP.Service("app") {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   882
            def apply(request: HTTP.Request): Option[HTTP.Response] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   883
              Some(HTTP.Response.html(if (devel) project.build_html(progress) else frontend))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   884
          },
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   885
          new REST_Service(Path.explode("api/block"), progress) {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   886
            def handle(body: JSON.T): Option[JSON.T] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   887
              for {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   888
                request <- Parse.query_block(body)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   889
                block <- query_block(db, request)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   890
              } yield encode.block(block)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   891
          },
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   892
          new REST_Service(Path.explode("api/blocks"), progress) {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   893
            def handle(body: JSON.T): Option[JSON.T] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   894
              for (request <- Parse.query_blocks(body))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   895
              yield encode.blocks(query_blocks(db, request.query, Some(request.cursor)))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   896
          },
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   897
          new REST_Service(Path.explode("api/query"), progress) {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   898
            def handle(body: JSON.T): Option[JSON.T] =
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   899
              for (query <- Parse.query(body)) yield {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   900
                val facet = query_facet(db, query)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   901
                val blocks = query_blocks(db, query)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   902
                encode.result(Result(blocks, facet))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   903
              }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   904
          }))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   905
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   906
      server.start()
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   907
      progress.echo("Server started on port " + server.http_server.getAddress.getPort)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   908
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   909
      @tailrec
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   910
      def loop(): Unit = {
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   911
        Thread.sleep(Long.MaxValue)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   912
        loop()
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   913
      }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   914
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   915
      Isabelle_Thread.interrupt_handler(_ => server.stop()) { loop() }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   916
    }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   917
  }
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   918
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   919
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   920
  /* Isabelle tool wrapper */
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   921
81770
f54881ce5cf3 proper component src/Tools/Find_Facts;
wenzelm
parents: 81764
diff changeset
   922
  val isabelle_tool3 = Isabelle_Tool("find_facts", "run find_facts server", Scala_Project.here,
81764
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   923
  { args =>
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   924
    var devel = false
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   925
    var options = Options.init()
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   926
    var port = 8080
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   927
    var verbose = false
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   928
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   929
    val getopts = Getopts("""
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   930
Usage: isabelle find_facts [OPTIONS]
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   931
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   932
  Options are:
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   933
    -d           devel mode
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   934
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   935
    -p PORT      explicit web server port
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   936
    -v           verbose server
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   937
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   938
  Run the find_facts web service.
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   939
""",
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   940
        "d" -> (_ => devel = true),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   941
        "o:" -> (arg => options = options + arg),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   942
        "p:" -> (arg => port = Value.Int.parse(arg)),
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   943
        "v" -> (_ => verbose = true))
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   944
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   945
    val more_args = getopts(args)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   946
    if (more_args.nonEmpty) getopts.usage()
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   947
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   948
    val progress = new Console_Progress(verbose = verbose)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   949
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   950
    find_facts(options, port, devel = devel, progress = progress)
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   951
  })
fcba3250fb2a original sources of find-facts 271b5af0c4c8;
wenzelm
parents:
diff changeset
   952
}