81764
|
1 |
{- Author: Fabian Huch, TU Muenchen
|
|
2 |
|
|
3 |
Queries against the find facts server.
|
|
4 |
-}
|
|
5 |
module Query exposing (Atom(..), Filter(..), Select, Query, Query_Blocks, empty, empty_query,
|
|
6 |
encode_query, encode_query_blocks, encode_query_block, Block, Blocks, Facets, Result,
|
|
7 |
decode_block, decode_blocks, decode_result)
|
|
8 |
|
|
9 |
|
|
10 |
import Dict exposing (Dict)
|
|
11 |
import Json.Decode as Decode
|
|
12 |
import Json.Decode.Extra as Decode
|
|
13 |
import Json.Encode as Encode
|
|
14 |
|
|
15 |
|
|
16 |
{- queries -}
|
|
17 |
|
|
18 |
type Atom = Value String | Exact String
|
|
19 |
type Filter = Any_Filter (List Atom) | Field_Filter String (List Atom)
|
|
20 |
type alias Select = {field: String, values: List String}
|
|
21 |
type alias Query = {filters: List Filter, exclude: List Filter, selects: List Select}
|
|
22 |
type alias Query_Blocks = {query: Query, cursor: String}
|
|
23 |
|
|
24 |
empty: Query
|
|
25 |
empty = Query [] [] []
|
|
26 |
|
|
27 |
empty_atom: Atom -> Bool
|
|
28 |
empty_atom atom =
|
|
29 |
case atom of
|
|
30 |
Value s -> String.words s |> List.isEmpty
|
|
31 |
Exact s -> String.words s |> List.isEmpty
|
|
32 |
|
|
33 |
empty_filter: Filter -> Bool
|
|
34 |
empty_filter filter =
|
|
35 |
case filter of
|
|
36 |
Any_Filter atoms -> List.all empty_atom atoms
|
|
37 |
Field_Filter _ atoms -> List.all empty_atom atoms
|
|
38 |
|
|
39 |
empty_select: Select -> Bool
|
|
40 |
empty_select select = List.isEmpty select.values
|
|
41 |
|
|
42 |
empty_query: Query -> Bool
|
|
43 |
empty_query query =
|
|
44 |
List.all empty_filter (query.filters ++ query.exclude) && List.all empty_select query.selects
|
|
45 |
|
|
46 |
|
|
47 |
{- json encoding -}
|
|
48 |
|
|
49 |
encode_atom: Atom -> Encode.Value
|
|
50 |
encode_atom atom =
|
|
51 |
case atom of
|
|
52 |
Exact phrase -> Encode.object [("exact", Encode.string phrase)]
|
|
53 |
Value wildcard -> Encode.object [("value", Encode.string wildcard)]
|
|
54 |
|
|
55 |
encode_filter: Filter -> Encode.Value
|
|
56 |
encode_filter filter =
|
|
57 |
case filter of
|
|
58 |
Any_Filter atoms -> Encode.object [("either", Encode.list encode_atom atoms)]
|
|
59 |
Field_Filter field atoms ->
|
|
60 |
Encode.object [("either", Encode.list encode_atom atoms), ("field", Encode.string field)]
|
|
61 |
|
|
62 |
encode_select: Select -> Encode.Value
|
|
63 |
encode_select select =
|
|
64 |
Encode.object [
|
|
65 |
("field", Encode.string select.field),
|
|
66 |
("values", Encode.list Encode.string select.values)]
|
|
67 |
|
|
68 |
encode_query: Query -> Encode.Value
|
|
69 |
encode_query query =
|
|
70 |
Encode.object [
|
|
71 |
("filters", Encode.list encode_filter query.filters),
|
|
72 |
("exclude", Encode.list encode_filter query.exclude),
|
|
73 |
("selects", Encode.list encode_select query.selects)]
|
|
74 |
|
|
75 |
encode_query_blocks: Query_Blocks -> Encode.Value
|
|
76 |
encode_query_blocks query_blocks =
|
|
77 |
Encode.object
|
|
78 |
[("query", encode_query query_blocks.query), ("cursor", Encode.string query_blocks.cursor)]
|
|
79 |
|
|
80 |
encode_query_block: String -> Encode.Value
|
|
81 |
encode_query_block id = Encode.object [("id", Encode.string id)]
|
|
82 |
|
|
83 |
|
|
84 |
{- results -}
|
|
85 |
|
|
86 |
type alias Block = {
|
|
87 |
id: String,
|
|
88 |
chapter: String,
|
|
89 |
session: String,
|
|
90 |
theory: String,
|
|
91 |
file: String,
|
|
92 |
file_name: String,
|
|
93 |
url: String,
|
|
94 |
command: String,
|
|
95 |
start_line: Int,
|
|
96 |
src_before: String,
|
|
97 |
src_after: String,
|
|
98 |
html: String,
|
|
99 |
entity_kname: Maybe String}
|
|
100 |
|
|
101 |
type alias Blocks = {num_found: Int, blocks: List Block, cursor: String}
|
|
102 |
type alias Facets = Dict String (Dict String Int)
|
|
103 |
type alias Result = {blocks: Blocks, facets: Facets}
|
|
104 |
|
|
105 |
|
|
106 |
{- json decoding -}
|
|
107 |
|
|
108 |
decode_block: Decode.Decoder Block
|
|
109 |
decode_block =
|
|
110 |
Decode.succeed Block
|
|
111 |
|> Decode.andMap (Decode.field "id" Decode.string)
|
|
112 |
|> Decode.andMap (Decode.field "chapter" Decode.string)
|
|
113 |
|> Decode.andMap (Decode.field "session" Decode.string)
|
|
114 |
|> Decode.andMap (Decode.field "theory" Decode.string)
|
|
115 |
|> Decode.andMap (Decode.field "file" Decode.string)
|
|
116 |
|> Decode.andMap (Decode.field "file_name" Decode.string)
|
|
117 |
|> Decode.andMap (Decode.field "url" Decode.string)
|
|
118 |
|> Decode.andMap (Decode.field "command" Decode.string)
|
|
119 |
|> Decode.andMap (Decode.field "start_line" Decode.int)
|
|
120 |
|> Decode.andMap (Decode.field "src_before" Decode.string)
|
|
121 |
|> Decode.andMap (Decode.field "src_after" Decode.string)
|
|
122 |
|> Decode.andMap (Decode.field "html" Decode.string)
|
|
123 |
|> Decode.andMap (Decode.field "entity_kname" (Decode.maybe Decode.string))
|
|
124 |
|
|
125 |
decode_blocks: Decode.Decoder Blocks
|
|
126 |
decode_blocks =
|
|
127 |
Decode.map3 Blocks
|
|
128 |
(Decode.field "num_found" Decode.int)
|
|
129 |
(Decode.field "blocks" (Decode.list decode_block))
|
|
130 |
(Decode.field "cursor" Decode.string)
|
|
131 |
|
|
132 |
decode_facets: Decode.Decoder Facets
|
|
133 |
decode_facets = Decode.dict (Decode.dict Decode.int)
|
|
134 |
|
|
135 |
decode_result: Decode.Decoder Result
|
|
136 |
decode_result =
|
|
137 |
Decode.map2 Result (Decode.field "blocks" decode_blocks) (Decode.field "facets" decode_facets)
|