diff options
| author | Volpeon <github@volpeon.ink> | 2023-06-24 08:58:22 +0200 |
|---|---|---|
| committer | Volpeon <github@volpeon.ink> | 2023-06-24 08:58:22 +0200 |
| commit | b19440a4a30828f12f8eafaa7292152ecf733334 (patch) | |
| tree | 00210fae1f860d76bb5319d10167d744c5d4037d /src/Data/JLD/Compaction/InverseContext.hs | |
| parent | Small code optimization (diff) | |
| download | hs-jsonld-b19440a4a30828f12f8eafaa7292152ecf733334.tar.gz hs-jsonld-b19440a4a30828f12f8eafaa7292152ecf733334.tar.bz2 hs-jsonld-b19440a4a30828f12f8eafaa7292152ecf733334.zip | |
WIP: Compaction
Diffstat (limited to 'src/Data/JLD/Compaction/InverseContext.hs')
| -rw-r--r-- | src/Data/JLD/Compaction/InverseContext.hs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/Data/JLD/Compaction/InverseContext.hs b/src/Data/JLD/Compaction/InverseContext.hs new file mode 100644 index 0000000..b351e34 --- /dev/null +++ b/src/Data/JLD/Compaction/InverseContext.hs | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | module Data.JLD.Compaction.InverseContext (buildInverseContext) where | ||
| 2 | |||
| 3 | import Data.JLD.Prelude | ||
| 4 | |||
| 5 | import Data.JLD.Model.ActiveContext (ActiveContext (..)) | ||
| 6 | import Data.JLD.Model.Direction (Direction (..)) | ||
| 7 | import Data.JLD.Model.InverseContext (InverseContext) | ||
| 8 | import Data.JLD.Model.Keyword (Keyword (..)) | ||
| 9 | import Data.JLD.Model.Language (Language (Language)) | ||
| 10 | import Data.JLD.Model.TermDefinition (TermDefinition (..)) | ||
| 11 | |||
| 12 | import Data.Map qualified as M | ||
| 13 | |||
| 14 | processTerm :: Text -> InverseContext -> Text -> TermDefinition -> InverseContext | ||
| 15 | processTerm defaultLangDir out termName TermDefinition{..} | ||
| 16 | | Just variableName <- termDefinitionIriMapping = | ||
| 17 | out | ||
| 18 | |> M.insert (variableName, container, show KeywordAny, show KeywordNone) termName | ||
| 19 | .> if | ||
| 20 | | termDefinitionReversePropertyFlag -> | ||
| 21 | M.insert (variableName, container, show KeywordType, show KeywordReverse) termName | ||
| 22 | | termDefinitionTypeMapping == Just (show KeywordNone) -> | ||
| 23 | M.insert (variableName, container, show KeywordLanguage, show KeywordAny) termName | ||
| 24 | .> M.insert (variableName, container, show KeywordType, show KeywordAny) termName | ||
| 25 | | Just typeMapping <- termDefinitionTypeMapping -> | ||
| 26 | M.insert (variableName, container, show KeywordType, typeMapping) termName | ||
| 27 | | Just langDir <- maybeLangDir -> | ||
| 28 | M.insert (variableName, container, show KeywordLanguage, langDir) termName | ||
| 29 | | otherwise -> | ||
| 30 | M.insert (variableName, container, show KeywordLanguage, defaultLangDir) termName | ||
| 31 | .> M.insert (variableName, container, show KeywordLanguage, show KeywordNone) termName | ||
| 32 | .> M.insert (variableName, container, show KeywordType, show KeywordNone) termName | ||
| 33 | | otherwise = out | ||
| 34 | where | ||
| 35 | container = if null termDefinitionContainerMapping then show KeywordNone else fold termDefinitionContainerMapping | ||
| 36 | maybeLangDir = case (termDefinitionLanguageMapping, termDefinitionDirectionMapping) of | ||
| 37 | (Just (Language language), Just LTR) -> Just <| language <> "_ltr" | ||
| 38 | (Just (Language language), Just RTL) -> Just <| language <> "_rtl" | ||
| 39 | (Just (Language language), _) -> Just <| language | ||
| 40 | (Just _, Just LTR) -> Just "_ltr" | ||
| 41 | (Just _, Just RTL) -> Just "_rtl" | ||
| 42 | (Just _, _) -> Just <| show KeywordNull | ||
| 43 | (Nothing, Just LTR) -> Just "_ltr" | ||
| 44 | (Nothing, Just RTL) -> Just "_rtl" | ||
| 45 | (Nothing, Just NoDirection) -> Just <| show KeywordNone | ||
| 46 | (Nothing, Nothing) -> Nothing | ||
| 47 | |||
| 48 | buildInverseContext :: ActiveContext -> InverseContext | ||
| 49 | buildInverseContext ActiveContext{..} = M.foldlWithKey (processTerm defaultLangDir) mempty activeContextTerms | ||
| 50 | where | ||
| 51 | defaultLangDir = case (activeContextDefaultBaseDirection, activeContextDefaultLanguage) of | ||
| 52 | (Just bd, Just (Language dl)) -> dl <> "_" <> show bd | ||
| 53 | (Just bd, _) -> "_" <> show bd | ||
| 54 | (_, _) -> show KeywordNone | ||
