1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE DerivingStrategies #-}
3 module Gargantext.Core.Text.Corpus.Query (
14 -- * Useful for testing
20 import Gargantext.API.Admin.Orchestrator.Types
21 import Gargantext.Core.Types
23 import Text.ParserCombinators.Parsec
24 import qualified Data.Aeson as Aeson
25 import Data.BoolExpr as BoolExpr
26 import Data.BoolExpr.Parser as BoolExpr
27 import Data.BoolExpr.Printer as BoolExpr
28 import qualified Data.Swagger as Swagger
29 import qualified Data.Text as T
30 import qualified Servant.API as Servant
31 import qualified Text.Parsec as P
33 -- | A raw query, as typed by the user from the frontend.
34 newtype RawQuery = RawQuery { getRawQuery :: T.Text }
35 deriving newtype ( Show, Eq, IsString
36 , Servant.FromHttpApiData, Servant.ToHttpApiData
37 , Aeson.FromJSON, Aeson.ToJSON
38 , Swagger.ToParamSchema, Swagger.ToSchema)
40 -- | A limit to the number of results we want to retrieve.
41 newtype Limit = Limit { getLimit :: Int }
42 deriving newtype ( Show, Eq, Num
43 , Servant.FromHttpApiData, Servant.ToHttpApiData
44 , Aeson.FromJSON, Aeson.ToJSON
45 , Swagger.ToParamSchema, Swagger.ToSchema)
47 -- | An opaque wrapper around a 'Query' type which can be parsed from a boolean
48 -- expression like (a AND b) OR c, and which can be interpreted in many ways
49 -- according to the particular service we are targeting.
50 newtype Query = Query { getQuery :: (BoolExpr.CNF Term) }
53 interpretQuery :: Query -> (BoolExpr.BoolExpr Term -> ast) -> ast
54 interpretQuery (Query q) transform = transform . simplify . BoolExpr.fromCNF $ q
56 simplify :: BoolExpr.BoolExpr a -> BoolExpr.BoolExpr a
57 simplify expr = case expr of
58 BAnd sub BTrue -> simplify sub
59 BAnd BTrue sub -> simplify sub
60 BAnd BFalse _ -> BFalse
61 BAnd _ BFalse -> BFalse
62 BAnd sub1 sub2 -> BAnd (simplify sub1) (simplify sub2)
65 BOr sub BFalse -> simplify sub
66 BOr BFalse sub -> simplify sub
67 BOr sub1 sub2 -> BOr (simplify sub1) (simplify sub2)
70 BNot (BNot sub) -> simplify sub
71 BNot sub -> BNot (simplify sub)
74 BConst signed -> BConst signed
76 unsafeMkQuery :: BoolExpr.BoolExpr Term -> Query
77 unsafeMkQuery = Query . BoolExpr.boolTreeToCNF
79 termToken :: CharParser st Term
80 termToken = Term <$> (try (T.pack <$> BoolExpr.identifier) <|> (between dubQuote dubQuote multipleTerms))
82 dubQuote = BoolExpr.symbol "\""
83 multipleTerms = T.intercalate " " . map T.pack <$> sepBy BoolExpr.identifier BoolExpr.whiteSpace
85 -- | Parses an input 'Text' into a 'Query', reporting an error if it fails.
86 parseQuery :: RawQuery -> Either String Query
87 parseQuery (RawQuery txt) = bimap show (Query . BoolExpr.boolTreeToCNF) $
88 P.runParser (BoolExpr.parseBoolExpr termToken) () "Corpus.Query" (T.unpack txt)
90 renderQuery :: Query -> RawQuery
91 renderQuery (Query cnf) = RawQuery . T.pack $ BoolExpr.boolExprPrinter (showsPrec 0) (BoolExpr.fromCNF cnf) ""