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