diff options
author | Volpeon <github@volpeon.ink> | 2023-05-27 12:10:51 +0200 |
---|---|---|
committer | Volpeon <github@volpeon.ink> | 2023-05-27 12:10:51 +0200 |
commit | 1bb6f74645e39bb45e33a7413771ea7f971628c9 (patch) | |
tree | 7b8c2866ff9198264a99f4da60d9fe82a3bf21fb | |
parent | Meta: Link to about page instead of raw readme (diff) | |
download | hs-jsonld-1bb6f74645e39bb45e33a7413771ea7f971628c9.tar.gz hs-jsonld-1bb6f74645e39bb45e33a7413771ea7f971628c9.tar.bz2 hs-jsonld-1bb6f74645e39bb45e33a7413771ea7f971628c9.zip |
Structural improvements
-rw-r--r-- | jsonld.cabal | 7 | ||||
-rw-r--r-- | src/Data/JLD.hs | 53 | ||||
-rw-r--r-- | src/Data/JLD/Control/Monad/RES.hs | 4 | ||||
-rw-r--r-- | src/Data/JLD/Expansion.hs | 56 | ||||
-rw-r--r-- | src/Data/JLD/Expansion/Context.hs (renamed from src/Data/JLD/Context.hs) | 74 | ||||
-rw-r--r-- | src/Data/JLD/Expansion/Global.hs | 38 | ||||
-rw-r--r-- | src/Data/JLD/Monad.hs | 86 | ||||
-rw-r--r-- | src/Data/JLD/NodeMap.hs | 88 | ||||
-rw-r--r-- | test/Test/Expansion.hs | 25 |
9 files changed, 255 insertions, 176 deletions
diff --git a/jsonld.cabal b/jsonld.cabal index 6f36afa..1308318 100644 --- a/jsonld.cabal +++ b/jsonld.cabal | |||
@@ -1,6 +1,6 @@ | |||
1 | cabal-version: 1.12 | 1 | cabal-version: 1.12 |
2 | 2 | ||
3 | -- This file has been generated from package.yaml by hpack version 0.35.1. | 3 | -- This file has been generated from package.yaml by hpack version 0.35.2. |
4 | -- | 4 | -- |
5 | -- see: https://github.com/sol/hpack | 5 | -- see: https://github.com/sol/hpack |
6 | 6 | ||
@@ -24,10 +24,11 @@ source-repository head | |||
24 | library | 24 | library |
25 | exposed-modules: | 25 | exposed-modules: |
26 | Data.JLD | 26 | Data.JLD |
27 | Data.JLD.Context | ||
28 | Data.JLD.Control.Monad.RES | 27 | Data.JLD.Control.Monad.RES |
29 | Data.JLD.Error | 28 | Data.JLD.Error |
30 | Data.JLD.Expansion | 29 | Data.JLD.Expansion |
30 | Data.JLD.Expansion.Context | ||
31 | Data.JLD.Expansion.Global | ||
31 | Data.JLD.Mime | 32 | Data.JLD.Mime |
32 | Data.JLD.Model.ActiveContext | 33 | Data.JLD.Model.ActiveContext |
33 | Data.JLD.Model.Direction | 34 | Data.JLD.Model.Direction |
@@ -41,7 +42,7 @@ library | |||
41 | Data.JLD.Model.TermDefinition | 42 | Data.JLD.Model.TermDefinition |
42 | Data.JLD.Model.URI | 43 | Data.JLD.Model.URI |
43 | Data.JLD.Model.ValueObject | 44 | Data.JLD.Model.ValueObject |
44 | Data.JLD.Monad | 45 | Data.JLD.NodeMap |
45 | Data.JLD.Options | 46 | Data.JLD.Options |
46 | Data.JLD.Prelude | 47 | Data.JLD.Prelude |
47 | Data.JLD.Util | 48 | Data.JLD.Util |
diff --git a/src/Data/JLD.hs b/src/Data/JLD.hs index d60e5a1..1f894bb 100644 --- a/src/Data/JLD.hs +++ b/src/Data/JLD.hs | |||
@@ -2,22 +2,22 @@ module Data.JLD ( | |||
2 | module Data.JLD.Mime, | 2 | module Data.JLD.Mime, |
3 | module Data.JLD.Error, | 3 | module Data.JLD.Error, |
4 | module Data.JLD.Options, | 4 | module Data.JLD.Options, |
5 | JLDExpandParams (..), | 5 | JLDExpansionParams (..), |
6 | expand, | 6 | expand, |
7 | ) where | 7 | ) where |
8 | 8 | ||
9 | import Data.JLD.Prelude | 9 | import Data.JLD.Prelude |
10 | 10 | ||
11 | import Data.JLD.Context (buildActiveContext) | ||
12 | import Data.JLD.Control.Monad.RES (evalREST, runREST) | 11 | import Data.JLD.Control.Monad.RES (evalREST, runREST) |
13 | import Data.JLD.Error | 12 | import Data.JLD.Error |
14 | import Data.JLD.Expansion (JLDEParams (..)) | 13 | import Data.JLD.Expansion (JLDEParams (..)) |
15 | import Data.JLD.Expansion qualified as E (expand) | 14 | import Data.JLD.Expansion qualified as E (expand) |
15 | import Data.JLD.Expansion.Context (buildActiveContext) | ||
16 | import Data.JLD.Expansion.Global (JLDExpansionEnv (..), JLDExpansionState (..)) | ||
16 | import Data.JLD.Mime | 17 | import Data.JLD.Mime |
17 | import Data.JLD.Model.ActiveContext (ActiveContext (..), newActiveContext) | 18 | import Data.JLD.Model.ActiveContext (ActiveContext (..), newActiveContext) |
18 | import Data.JLD.Model.Keyword (Keyword (..)) | 19 | import Data.JLD.Model.Keyword (Keyword (..)) |
19 | import Data.JLD.Model.URI (uriToIri) | 20 | import Data.JLD.Model.URI (uriToIri) |
20 | import Data.JLD.Monad (JLDEnv, JLDState, newEnv, newState) | ||
21 | import Data.JLD.Options | 21 | import Data.JLD.Options |
22 | import Data.JLD.Util (flattenSingletonArray, valueToArray) | 22 | import Data.JLD.Util (flattenSingletonArray, valueToArray) |
23 | 23 | ||
@@ -26,31 +26,46 @@ import Data.Aeson.KeyMap qualified as KM | |||
26 | import Data.Vector qualified as V (singleton) | 26 | import Data.Vector qualified as V (singleton) |
27 | import Text.URI (URI) | 27 | import Text.URI (URI) |
28 | 28 | ||
29 | data JLDExpandParams e m = JLDExpandParams | 29 | data JLDExpansionParams e m = JLDExpansionParams |
30 | { jldExpandParamsExpandContext :: Maybe Value | 30 | { jldExpansionParamsDocumentLoader :: DocumentLoader e m |
31 | , jldExpandParamsFrameExpansion :: Bool | 31 | , jldExpansionParamsProcessingMode :: JLDVersion |
32 | , jldExpandParamsEnv :: JLDEnv e m | 32 | , jldExpansionParamsMaxRemoteContexts :: Int |
33 | , jldExpandParamsState :: JLDState | 33 | , jldExpansionParamsExpandContext :: Maybe Value |
34 | , jldExpansionParamsFrameExpansion :: Bool | ||
35 | , jldExpansionParamsState :: JLDExpansionState | ||
34 | } | 36 | } |
35 | deriving (Show) | 37 | deriving (Show) |
36 | 38 | ||
37 | expand :: Monad m => Value -> URI -> (JLDExpandParams () m -> JLDExpandParams e m) -> m (Either (JLDError e) Value, JLDState) | 39 | expand :: Monad m => Value -> URI -> (JLDExpansionParams () m -> JLDExpansionParams e m) -> m (Either (JLDError e) Value, JLDExpansionState) |
38 | expand document baseUrl paramsFn = do | 40 | expand document baseUrl paramsFn = do |
39 | let JLDExpandParams{..} = | 41 | let JLDExpansionParams{..} = |
40 | paramsFn | 42 | paramsFn |
41 | JLDExpandParams | 43 | JLDExpansionParams |
42 | { jldExpandParamsExpandContext = Nothing | 44 | { jldExpansionParamsDocumentLoader = DocumentLoader <. const <. pure <| Left () |
43 | , jldExpandParamsFrameExpansion = False | 45 | , jldExpansionParamsProcessingMode = JLD1_1 |
44 | , jldExpandParamsEnv = newEnv id | 46 | , jldExpansionParamsMaxRemoteContexts = 20 |
45 | , jldExpandParamsState = newState id | 47 | , jldExpansionParamsExpandContext = Nothing |
48 | , jldExpansionParamsFrameExpansion = False | ||
49 | , jldExpansionParamsState = | ||
50 | JLDExpansionState | ||
51 | { jldExpansionStateContextCache = mempty | ||
52 | , jldExpansionStateDocumentCache = mempty | ||
53 | } | ||
46 | } | 54 | } |
47 | 55 | ||
56 | env = | ||
57 | JLDExpansionEnv | ||
58 | { jldExpansionEnvDocumentLoader = jldExpansionParamsDocumentLoader | ||
59 | , jldExpansionEnvProcessingMode = jldExpansionParamsProcessingMode | ||
60 | , jldExpansionEnvMaxRemoteContexts = jldExpansionParamsMaxRemoteContexts | ||
61 | } | ||
62 | |||
48 | activeContext = newActiveContext \ac -> ac{activeContextBaseUrl = Just baseUrl, activeContextBaseIri = uriToIri baseUrl} | 63 | activeContext = newActiveContext \ac -> ac{activeContextBaseUrl = Just baseUrl, activeContextBaseIri = uriToIri baseUrl} |
49 | expansionParams p = p{jldeParamsFrameExpansion = jldExpandParamsFrameExpansion} | 64 | expansionParams p = p{jldeParamsFrameExpansion = jldExpansionParamsFrameExpansion} |
50 | 65 | ||
51 | -- 6. | 66 | -- 6. |
52 | let maybeExpandContext = | 67 | let maybeExpandContext = |
53 | jldExpandParamsExpandContext <&> flattenSingletonArray .> \case | 68 | jldExpansionParamsExpandContext <&> flattenSingletonArray .> \case |
54 | Array expandedContext -> Array expandedContext | 69 | Array expandedContext -> Array expandedContext |
55 | (Object expandedContext) | Just ctx <- KM.lookup (show KeywordContext) expandedContext -> ctx | 70 | (Object expandedContext) | Just ctx <- KM.lookup (show KeywordContext) expandedContext -> ctx |
56 | expandedContext -> Array <| V.singleton expandedContext | 71 | expandedContext -> Array <| V.singleton expandedContext |
@@ -58,14 +73,14 @@ expand document baseUrl paramsFn = do | |||
58 | activeContext' <- case maybeExpandContext of | 73 | activeContext' <- case maybeExpandContext of |
59 | Just expandContext -> | 74 | Just expandContext -> |
60 | buildActiveContext activeContext expandContext (Just baseUrl) id | 75 | buildActiveContext activeContext expandContext (Just baseUrl) id |
61 | |> evalREST jldExpandParamsEnv jldExpandParamsState | 76 | |> evalREST env jldExpansionParamsState |
62 | |> fmap (fromRight activeContext) | 77 | |> fmap (fromRight activeContext) |
63 | Nothing -> pure activeContext | 78 | Nothing -> pure activeContext |
64 | 79 | ||
65 | -- 8. | 80 | -- 8. |
66 | (result, state') <- | 81 | (result, state') <- |
67 | E.expand activeContext' document baseUrl expansionParams | 82 | E.expand activeContext' document baseUrl expansionParams |
68 | |> runREST jldExpandParamsEnv jldExpandParamsState | 83 | |> runREST env jldExpansionParamsState |
69 | 84 | ||
70 | let result' = case result of | 85 | let result' = case result of |
71 | -- 8.1. | 86 | -- 8.1. |
diff --git a/src/Data/JLD/Control/Monad/RES.hs b/src/Data/JLD/Control/Monad/RES.hs index 1c96d46..b9f8f22 100644 --- a/src/Data/JLD/Control/Monad/RES.hs +++ b/src/Data/JLD/Control/Monad/RES.hs | |||
@@ -2,6 +2,7 @@ module Data.JLD.Control.Monad.RES ( | |||
2 | REST, | 2 | REST, |
3 | runREST, | 3 | runREST, |
4 | evalREST, | 4 | evalREST, |
5 | execREST, | ||
5 | withEnvRES, | 6 | withEnvRES, |
6 | withErrorRES, | 7 | withErrorRES, |
7 | withErrorRES', | 8 | withErrorRES', |
@@ -20,6 +21,9 @@ runREST env st = flip runReaderT env .> runExceptT .> flip runStateT st | |||
20 | evalREST :: Monad m => r -> s -> REST r e s m a -> m (Either e a) | 21 | evalREST :: Monad m => r -> s -> REST r e s m a -> m (Either e a) |
21 | evalREST env st = flip runReaderT env .> runExceptT .> flip evalStateT st | 22 | evalREST env st = flip runReaderT env .> runExceptT .> flip evalStateT st |
22 | 23 | ||
24 | execREST :: Monad m => r -> s -> REST r e s m a -> m s | ||
25 | execREST env st = flip runReaderT env .> runExceptT .> flip execStateT st | ||
26 | |||
23 | withEnvRES :: (r -> r') -> REST r' e s m a -> REST r e s m a | 27 | withEnvRES :: (r -> r') -> REST r' e s m a -> REST r e s m a |
24 | withEnvRES fn (ReaderT m) = ReaderT <| fn .> m | 28 | withEnvRES fn (ReaderT m) = ReaderT <| fn .> m |
25 | 29 | ||
diff --git a/src/Data/JLD/Expansion.hs b/src/Data/JLD/Expansion.hs index 18d7fc6..ff2d9c3 100644 --- a/src/Data/JLD/Expansion.hs +++ b/src/Data/JLD/Expansion.hs | |||
@@ -3,19 +3,19 @@ module Data.JLD.Expansion (JLDEParams (..), expand) where | |||
3 | import Data.JLD.Prelude | 3 | import Data.JLD.Prelude |
4 | 4 | ||
5 | import Data.JLD.Control.Monad.RES (REST, withEnvRES, withStateRES) | 5 | import Data.JLD.Control.Monad.RES (REST, withEnvRES, withStateRES) |
6 | import Data.JLD.Context (BACParams (..), EIParams (..), buildActiveContext, expandIri) | 6 | import Data.JLD.Error (JLDError (..)) |
7 | import Data.JLD.Expansion.Context (BACParams (..), EIParams (..), buildActiveContext, expandIri) | ||
8 | import Data.JLD.Expansion.Global (JLDExpansionEnv (..), JLDExpansionState, JLDExpansionT) | ||
7 | import Data.JLD.Model.ActiveContext (ActiveContext (..), lookupTerm) | 9 | import Data.JLD.Model.ActiveContext (ActiveContext (..), lookupTerm) |
8 | import Data.JLD.Model.Direction (Direction (..)) | 10 | import Data.JLD.Model.Direction (Direction (..)) |
9 | import Data.JLD.Error (JLDError (..)) | ||
10 | import Data.JLD.Model.GraphObject (isNotGraphObject, toGraphObject) | 11 | import Data.JLD.Model.GraphObject (isNotGraphObject, toGraphObject) |
11 | import Data.JLD.Model.Keyword (Keyword (..), isKeyword, isNotKeyword, parseKeyword) | 12 | import Data.JLD.Model.Keyword (Keyword (..), isKeyword, isNotKeyword, parseKeyword) |
12 | import Data.JLD.Model.Language (Language (..)) | 13 | import Data.JLD.Model.Language (Language (..)) |
13 | import Data.JLD.Model.ListObject (isListObject, isNotListObject, toListObject) | 14 | import Data.JLD.Model.ListObject (isListObject, isNotListObject, toListObject) |
14 | import Data.JLD.Monad (JLDEEnv (..), JLDEState (..), JLDET, JLDEnv (..), JLDT, modifyActiveContext) | ||
15 | import Data.JLD.Model.NodeObject (isNotNodeObject) | 15 | import Data.JLD.Model.NodeObject (isNotNodeObject) |
16 | import Data.JLD.Options (JLDVersion (..)) | ||
17 | import Data.JLD.Model.TermDefinition (TermDefinition (..)) | 16 | import Data.JLD.Model.TermDefinition (TermDefinition (..)) |
18 | import Data.JLD.Model.ValueObject (isNotValueObject', isValueObject, isValueObject') | 17 | import Data.JLD.Model.ValueObject (isNotValueObject', isValueObject, isValueObject') |
18 | import Data.JLD.Options (JLDVersion (..)) | ||
19 | import Data.JLD.Util ( | 19 | import Data.JLD.Util ( |
20 | allStrings, | 20 | allStrings, |
21 | getMapDefault, | 21 | getMapDefault, |
@@ -44,6 +44,36 @@ import Data.Vector qualified as V (catMaybes, concat, cons, filter, fromList, ma | |||
44 | import Data.Vector.Algorithms.Merge qualified as V | 44 | import Data.Vector.Algorithms.Merge qualified as V |
45 | import Text.URI (URI) | 45 | import Text.URI (URI) |
46 | 46 | ||
47 | type JLDET e m = REST (JLDEEnv e m) (JLDError e) JLDEState m | ||
48 | |||
49 | data JLDEEnv e m = JLDEEnv | ||
50 | { jldeEnvGlobal :: JLDExpansionEnv e m | ||
51 | , jldeEnvFrameExpansion :: Bool | ||
52 | , jldeEnvFromMap :: Bool | ||
53 | , jldeEnvBaseUrl :: URI | ||
54 | , jldeEnvActiveProperty :: Maybe Text | ||
55 | } | ||
56 | deriving (Show) | ||
57 | |||
58 | data JLDEState = JLDEState | ||
59 | { jldeStateGlobal :: JLDExpansionState | ||
60 | , jldeStateActiveContext :: ActiveContext | ||
61 | } | ||
62 | deriving (Show, Eq) | ||
63 | |||
64 | data JLDEParams = JLDEParams | ||
65 | { jldeParamsFrameExpansion :: Bool | ||
66 | , jldeParamsFromMap :: Bool | ||
67 | , jldeParamsBaseUrl :: URI | ||
68 | , jldeParamsActiveProperty :: Maybe Text | ||
69 | } | ||
70 | deriving (Show, Eq) | ||
71 | |||
72 | modifyActiveContext :: MonadState JLDEState m => (ActiveContext -> ActiveContext) -> m () | ||
73 | modifyActiveContext fn = modify \s -> s{jldeStateActiveContext = fn (jldeStateActiveContext s)} | ||
74 | |||
75 | -- | ||
76 | |||
47 | type EO1314T e m = REST (JLDEEnv e m) (JLDError e) EO1314State m | 77 | type EO1314T e m = REST (JLDEEnv e m) (JLDError e) EO1314State m |
48 | 78 | ||
49 | data EO1314State = EO1314State | 79 | data EO1314State = EO1314State |
@@ -141,7 +171,7 @@ eo1314ExpandValue activeProperty value = do | |||
141 | eo1314ExpandKeywordItem :: Monad m => Maybe Text -> Key -> Keyword -> Value -> EO1314T e m () | 171 | eo1314ExpandKeywordItem :: Monad m => Maybe Text -> Key -> Keyword -> Value -> EO1314T e m () |
142 | eo1314ExpandKeywordItem inputType key keyword value = do | 172 | eo1314ExpandKeywordItem inputType key keyword value = do |
143 | JLDEEnv{..} <- ask | 173 | JLDEEnv{..} <- ask |
144 | let JLDEnv{..} = jldeEnvGlobal | 174 | let JLDExpansionEnv{..} = jldeEnvGlobal |
145 | 175 | ||
146 | -- 13.4.1. | 176 | -- 13.4.1. |
147 | when (jldeEnvActiveProperty == Just (show KeywordReverse)) <| throwError InvalidReversePropertyMap | 177 | when (jldeEnvActiveProperty == Just (show KeywordReverse)) <| throwError InvalidReversePropertyMap |
@@ -222,7 +252,7 @@ eo1314ExpandKeywordItem inputType key keyword value = do | |||
222 | -- 13.4.6. | 252 | -- 13.4.6. |
223 | KeywordIncluded | 253 | KeywordIncluded |
224 | -- 13.4.6.1. | 254 | -- 13.4.6.1. |
225 | | JLD1_0 <- jldEnvProcessingMode -> pure Nothing | 255 | | JLD1_0 <- jldExpansionEnvProcessingMode -> pure Nothing |
226 | -- 13.4.6.2. | 256 | -- 13.4.6.2. |
227 | | otherwise -> do | 257 | | otherwise -> do |
228 | expandedValue <- valueToArray <$> eo1314ExpandAC Nothing value id | 258 | expandedValue <- valueToArray <$> eo1314ExpandAC Nothing value id |
@@ -242,7 +272,7 @@ eo1314ExpandKeywordItem inputType key keyword value = do | |||
242 | expandedValue <- case value of | 272 | expandedValue <- case value of |
243 | -- 13.4.7.1. | 273 | -- 13.4.7.1. |
244 | _ | inputType == Just (show KeywordJson) -> do | 274 | _ | inputType == Just (show KeywordJson) -> do |
245 | if jldEnvProcessingMode == JLD1_0 | 275 | if jldExpansionEnvProcessingMode == JLD1_0 |
246 | then throwError InvalidValueObjectValue | 276 | then throwError InvalidValueObjectValue |
247 | else pure value | 277 | else pure value |
248 | -- 13.4.7.2. | 278 | -- 13.4.7.2. |
@@ -269,7 +299,7 @@ eo1314ExpandKeywordItem inputType key keyword value = do | |||
269 | _ -> throwError InvalidLanguageTaggedString | 299 | _ -> throwError InvalidLanguageTaggedString |
270 | -- 13.4.9. | 300 | -- 13.4.9. |
271 | KeywordDirection | 301 | KeywordDirection |
272 | | JLD1_0 <- jldEnvProcessingMode -> pure Nothing | 302 | | JLD1_0 <- jldExpansionEnvProcessingMode -> pure Nothing |
273 | | otherwise -> case value of | 303 | | otherwise -> case value of |
274 | String ((`elem` ["ltr", "rtl"]) -> True) | 304 | String ((`elem` ["ltr", "rtl"]) -> True) |
275 | | jldeEnvFrameExpansion -> pure <. Just <. Array <| V.singleton value | 305 | | jldeEnvFrameExpansion -> pure <. Just <. Array <| V.singleton value |
@@ -815,14 +845,6 @@ expandValue activeProperty value = do | |||
815 | 845 | ||
816 | -- | 846 | -- |
817 | 847 | ||
818 | data JLDEParams = JLDEParams | ||
819 | { jldeParamsFrameExpansion :: Bool | ||
820 | , jldeParamsFromMap :: Bool | ||
821 | , jldeParamsBaseUrl :: URI | ||
822 | , jldeParamsActiveProperty :: Maybe Text | ||
823 | } | ||
824 | deriving (Show, Eq) | ||
825 | |||
826 | exModifyActiveContext :: Monad m => (ActiveContext -> ActiveContext) -> JLDET e m () | 848 | exModifyActiveContext :: Monad m => (ActiveContext -> ActiveContext) -> JLDET e m () |
827 | exModifyActiveContext fn = modify \st -> st{jldeStateActiveContext = fn (jldeStateActiveContext st)} | 849 | exModifyActiveContext fn = modify \st -> st{jldeStateActiveContext = fn (jldeStateActiveContext st)} |
828 | 850 | ||
@@ -911,7 +933,7 @@ expand' = \case | |||
911 | -- 4.3. | 933 | -- 4.3. |
912 | | otherwise -> Object <$> expandValue activeProperty value | 934 | | otherwise -> Object <$> expandValue activeProperty value |
913 | 935 | ||
914 | expand :: Monad m => ActiveContext -> Value -> URI -> (JLDEParams -> JLDEParams) -> JLDT e m Value | 936 | expand :: Monad m => ActiveContext -> Value -> URI -> (JLDEParams -> JLDEParams) -> JLDExpansionT e m Value |
915 | expand activeContext value baseUrl paramsFn = | 937 | expand activeContext value baseUrl paramsFn = |
916 | expand' value | 938 | expand' value |
917 | |> withEnvRES env | 939 | |> withEnvRES env |
diff --git a/src/Data/JLD/Context.hs b/src/Data/JLD/Expansion/Context.hs index a999395..ce61644 100644 --- a/src/Data/JLD/Context.hs +++ b/src/Data/JLD/Expansion/Context.hs | |||
@@ -1,19 +1,19 @@ | |||
1 | module Data.JLD.Context (BTDParams (..), EIParams (..), BACParams (..), buildTermDefinition, expandIri, buildActiveContext) where | 1 | module Data.JLD.Expansion.Context (BTDParams (..), EIParams (..), BACParams (..), buildTermDefinition, expandIri, buildActiveContext) where |
2 | 2 | ||
3 | import Data.JLD.Prelude | 3 | import Data.JLD.Prelude |
4 | 4 | ||
5 | import Data.JLD.Control.Monad.RES (REST, withEnvRES, withErrorRES, withErrorRES', withStateRES) | 5 | import Data.JLD.Control.Monad.RES (REST, withEnvRES, withErrorRES, withErrorRES', withStateRES) |
6 | import Data.JLD.Error (JLDError (..)) | ||
7 | import Data.JLD.Expansion.Global (JLDExpansionEnv (..), JLDExpansionState (..), JLDExpansionT, hoistEnv, modifyContextCache, modifyDocumentCache) | ||
6 | import Data.JLD.Model.ActiveContext (ActiveContext (..), containsProtectedTerm, lookupTerm, newActiveContext) | 8 | import Data.JLD.Model.ActiveContext (ActiveContext (..), containsProtectedTerm, lookupTerm, newActiveContext) |
7 | import Data.JLD.Model.Direction (Direction (..)) | 9 | import Data.JLD.Model.Direction (Direction (..)) |
8 | import Data.JLD.Error (JLDError (..)) | ||
9 | import Data.JLD.Model.IRI (CompactIRI (..), endsWithGenericDelim, isBlankIri, parseCompactIri) | 10 | import Data.JLD.Model.IRI (CompactIRI (..), endsWithGenericDelim, isBlankIri, parseCompactIri) |
10 | import Data.JLD.Model.Keyword (Keyword (..), allKeywords, isKeyword, isKeywordLike, isNotKeyword, parseKeyword) | 11 | import Data.JLD.Model.Keyword (Keyword (..), allKeywords, isKeyword, isKeywordLike, isNotKeyword, parseKeyword) |
11 | import Data.JLD.Model.Language (Language (..)) | 12 | import Data.JLD.Model.Language (Language (..)) |
12 | import Data.JLD.Monad (JLDEnv (..), JLDState (..), JLDT, hoistEnv, modifyContextCache, modifyDocumentCache) | ||
13 | import Data.JLD.Options (ContextCache, Document (..), DocumentCache, DocumentLoader (..), JLDVersion (..)) | ||
14 | import Data.JLD.Model.TermDefinition (TermDefinition (..), newTermDefinition) | 13 | import Data.JLD.Model.TermDefinition (TermDefinition (..), newTermDefinition) |
15 | import Data.JLD.Util (flattenSingletonArray, valueContains, valueContainsAny, valueIsTrue, valueToArray) | ||
16 | import Data.JLD.Model.URI (parseUri, uriToIri) | 14 | import Data.JLD.Model.URI (parseUri, uriToIri) |
15 | import Data.JLD.Options (ContextCache, Document (..), DocumentCache, DocumentLoader (..), JLDVersion (..)) | ||
16 | import Data.JLD.Util (flattenSingletonArray, valueContains, valueContainsAny, valueIsTrue, valueToArray) | ||
17 | 17 | ||
18 | import Control.Monad.Except (MonadError (..)) | 18 | import Control.Monad.Except (MonadError (..)) |
19 | import Data.Aeson (Object, Value (..)) | 19 | import Data.Aeson (Object, Value (..)) |
@@ -30,7 +30,7 @@ import Text.URI qualified as U (render) | |||
30 | type BACT e m = REST (BACEnv e m) (Either (JLDError e) ()) BACState m | 30 | type BACT e m = REST (BACEnv e m) (Either (JLDError e) ()) BACState m |
31 | 31 | ||
32 | data BACEnv e m = BACEnv | 32 | data BACEnv e m = BACEnv |
33 | { bacEnvGlobal :: JLDEnv e m | 33 | { bacEnvGlobal :: JLDExpansionEnv e m |
34 | , bacEnvOverrideProtected :: Bool | 34 | , bacEnvOverrideProtected :: Bool |
35 | , bacEnvValidateScopedContext :: Bool | 35 | , bacEnvValidateScopedContext :: Bool |
36 | , bacEnvPropagate :: Bool | 36 | , bacEnvPropagate :: Bool |
@@ -38,7 +38,7 @@ data BACEnv e m = BACEnv | |||
38 | deriving (Show) | 38 | deriving (Show) |
39 | 39 | ||
40 | data BACState = BACState | 40 | data BACState = BACState |
41 | { bacStateGlobal :: JLDState | 41 | { bacStateGlobal :: JLDExpansionState |
42 | , bacStateActiveContext :: ActiveContext | 42 | , bacStateActiveContext :: ActiveContext |
43 | , bacStateRemoteContexts :: Set Text | 43 | , bacStateRemoteContexts :: Set Text |
44 | } | 44 | } |
@@ -103,7 +103,7 @@ bacBuildActiveContext context uri = do | |||
103 | bacProcessItem :: Monad m => Maybe URI -> Value -> BACT e m () | 103 | bacProcessItem :: Monad m => Maybe URI -> Value -> BACT e m () |
104 | bacProcessItem baseUrl item = do | 104 | bacProcessItem baseUrl item = do |
105 | BACEnv{..} <- ask | 105 | BACEnv{..} <- ask |
106 | let JLDEnv{..} = hoistEnv (lift .> lift .> lift) bacEnvGlobal | 106 | let JLDExpansionEnv{..} = hoistEnv (lift .> lift .> lift) bacEnvGlobal |
107 | 107 | ||
108 | result <- gets bacStateActiveContext | 108 | result <- gets bacStateActiveContext |
109 | 109 | ||
@@ -133,10 +133,10 @@ bacProcessItem baseUrl item = do | |||
133 | -- 5.5. 5.5.1. 5.5.2. | 133 | -- 5.5. 5.5.1. 5.5.2. |
134 | case KM.lookup (show KeywordVersion) contextDefinition of | 134 | case KM.lookup (show KeywordVersion) contextDefinition of |
135 | Just (String "1.1") | 135 | Just (String "1.1") |
136 | | JLD1_0 <- jldEnvProcessingMode -> throwError <| Left ProcessingModeConflict | 136 | | JLD1_0 <- jldExpansionEnvProcessingMode -> throwError <| Left ProcessingModeConflict |
137 | | otherwise -> pure () | 137 | | otherwise -> pure () |
138 | Just (Number 1.1) | 138 | Just (Number 1.1) |
139 | | JLD1_0 <- jldEnvProcessingMode -> throwError <| Left ProcessingModeConflict | 139 | | JLD1_0 <- jldExpansionEnvProcessingMode -> throwError <| Left ProcessingModeConflict |
140 | | otherwise -> pure () | 140 | | otherwise -> pure () |
141 | Just value -> throwError <. Left <| InvalidKeywordValue KeywordVersion value | 141 | Just value -> throwError <. Left <| InvalidKeywordValue KeywordVersion value |
142 | -- | 142 | -- |
@@ -145,12 +145,12 @@ bacProcessItem baseUrl item = do | |||
145 | -- 5.6. | 145 | -- 5.6. |
146 | contextDefinition' <- case KM.lookup (show KeywordImport) contextDefinition of | 146 | contextDefinition' <- case KM.lookup (show KeywordImport) contextDefinition of |
147 | -- 5.6.1. | 147 | -- 5.6.1. |
148 | Just _ | JLD1_0 <- jldEnvProcessingMode -> throwError <| Left InvalidContextEntry | 148 | Just _ | JLD1_0 <- jldExpansionEnvProcessingMode -> throwError <| Left InvalidContextEntry |
149 | -- 5.6.3. | 149 | -- 5.6.3. |
150 | Just (String value) | 150 | Just (String value) |
151 | | Just importUri <- parseUri value | 151 | | Just importUri <- parseUri value |
152 | , Just contextUri <- relativeTo importUri =<< baseUrl -> | 152 | , Just contextUri <- relativeTo importUri =<< baseUrl -> |
153 | runDocumentLoader jldEnvDocumentLoader contextUri >>= \case | 153 | runDocumentLoader jldExpansionEnvDocumentLoader contextUri >>= \case |
154 | Right (Object document) -> case KM.lookup (show KeywordContext) document of | 154 | Right (Object document) -> case KM.lookup (show KeywordContext) document of |
155 | Just (Object remoteContext) | 155 | Just (Object remoteContext) |
156 | -- 5.6.7. | 156 | -- 5.6.7. |
@@ -226,7 +226,7 @@ bacProcessItem baseUrl item = do | |||
226 | -- 5.10. 5.10.2. | 226 | -- 5.10. 5.10.2. |
227 | case KM.lookup (show KeywordDirection) contextDefinition' of | 227 | case KM.lookup (show KeywordDirection) contextDefinition' of |
228 | -- 5.10.1. | 228 | -- 5.10.1. |
229 | Just _ | JLD1_0 <- jldEnvProcessingMode -> throwError <| Left InvalidContextEntry | 229 | Just _ | JLD1_0 <- jldExpansionEnvProcessingMode -> throwError <| Left InvalidContextEntry |
230 | -- 5.10.3. | 230 | -- 5.10.3. |
231 | Just Null -> bacModifyActiveContext \ac -> ac{activeContextDefaultBaseDirection = Nothing} | 231 | Just Null -> bacModifyActiveContext \ac -> ac{activeContextDefaultBaseDirection = Nothing} |
232 | -- | 232 | -- |
@@ -239,7 +239,7 @@ bacProcessItem baseUrl item = do | |||
239 | -- 5.11. | 239 | -- 5.11. |
240 | case KM.lookup (show KeywordPropagate) contextDefinition' of | 240 | case KM.lookup (show KeywordPropagate) contextDefinition' of |
241 | -- 5.11.1. | 241 | -- 5.11.1. |
242 | Just _ | JLD1_0 <- jldEnvProcessingMode -> throwError <| Left InvalidContextEntry | 242 | Just _ | JLD1_0 <- jldExpansionEnvProcessingMode -> throwError <| Left InvalidContextEntry |
243 | Just (Bool _) -> pure () | 243 | Just (Bool _) -> pure () |
244 | Just invalid -> throwError <. Left <| InvalidKeywordValue KeywordPropagate invalid | 244 | Just invalid -> throwError <. Left <| InvalidKeywordValue KeywordPropagate invalid |
245 | -- | 245 | -- |
@@ -271,7 +271,7 @@ bacFetchRemoteContext url maybeBaseUrl | |||
271 | , isPathAbsolute contextUri | 271 | , isPathAbsolute contextUri |
272 | , contextKey <- U.render contextUri = do | 272 | , contextKey <- U.render contextUri = do |
273 | BACEnv{..} <- ask | 273 | BACEnv{..} <- ask |
274 | let JLDEnv{..} = hoistEnv (lift .> lift .> lift) bacEnvGlobal | 274 | let JLDExpansionEnv{..} = hoistEnv (lift .> lift .> lift) bacEnvGlobal |
275 | 275 | ||
276 | remoteContexts <- gets bacStateRemoteContexts | 276 | remoteContexts <- gets bacStateRemoteContexts |
277 | 277 | ||
@@ -279,12 +279,12 @@ bacFetchRemoteContext url maybeBaseUrl | |||
279 | when (not bacEnvValidateScopedContext && S.member contextKey remoteContexts) <| throwError (Right ()) | 279 | when (not bacEnvValidateScopedContext && S.member contextKey remoteContexts) <| throwError (Right ()) |
280 | 280 | ||
281 | -- 5.2.3. | 281 | -- 5.2.3. |
282 | when (S.size remoteContexts > jldEnvMaxRemoteContexts) <| throwError (Left ContextOverflow) | 282 | when (S.size remoteContexts > jldExpansionEnvMaxRemoteContexts) <| throwError (Left ContextOverflow) |
283 | 283 | ||
284 | bacModifyRemoteContexts <| S.insert contextKey | 284 | bacModifyRemoteContexts <| S.insert contextKey |
285 | 285 | ||
286 | -- 5.2.4. | 286 | -- 5.2.4. |
287 | gets (bacStateGlobal .> jldStateContextCache .> M.lookup contextKey) >>= \case | 287 | gets (bacStateGlobal .> jldExpansionStateContextCache .> M.lookup contextKey) >>= \case |
288 | Just cachedContext -> do | 288 | Just cachedContext -> do |
289 | bacBuildActiveContext cachedContext contextUri | 289 | bacBuildActiveContext cachedContext contextUri |
290 | throwError <| Right () | 290 | throwError <| Right () |
@@ -293,10 +293,10 @@ bacFetchRemoteContext url maybeBaseUrl | |||
293 | 293 | ||
294 | -- 5.2.5. | 294 | -- 5.2.5. |
295 | document <- | 295 | document <- |
296 | gets (bacStateGlobal .> jldStateDocumentCache .> M.lookup contextKey) >>= \case | 296 | gets (bacStateGlobal .> jldExpansionStateDocumentCache .> M.lookup contextKey) >>= \case |
297 | Just document -> pure document | 297 | Just document -> pure document |
298 | Nothing -> | 298 | Nothing -> |
299 | runDocumentLoader jldEnvDocumentLoader contextUri >>= \case | 299 | runDocumentLoader jldExpansionEnvDocumentLoader contextUri >>= \case |
300 | Right (Object document) -> pure <| Document contextUri document | 300 | Right (Object document) -> pure <| Document contextUri document |
301 | -- 5.2.5.2. | 301 | -- 5.2.5.2. |
302 | Right _ -> throwError <| Left InvalidRemoteContext | 302 | Right _ -> throwError <| Left InvalidRemoteContext |
@@ -341,7 +341,7 @@ buildActiveContext' localContext baseUrl = do | |||
341 | .> withEnvRES (\env -> env{bacEnvPropagate = propagate}) | 341 | .> withEnvRES (\env -> env{bacEnvPropagate = propagate}) |
342 | .> withErrorRES' (either (Left .> throwError) pure) | 342 | .> withErrorRES' (either (Left .> throwError) pure) |
343 | 343 | ||
344 | buildActiveContext :: Monad m => ActiveContext -> Value -> Maybe URI -> (BACParams -> BACParams) -> JLDT e m ActiveContext | 344 | buildActiveContext :: Monad m => ActiveContext -> Value -> Maybe URI -> (BACParams -> BACParams) -> JLDExpansionT e m ActiveContext |
345 | buildActiveContext activeContext localContext baseUrl paramsFn = do | 345 | buildActiveContext activeContext localContext baseUrl paramsFn = do |
346 | BACState{..} <- | 346 | BACState{..} <- |
347 | (buildActiveContext' localContext baseUrl >> get) | 347 | (buildActiveContext' localContext baseUrl >> get) |
@@ -379,7 +379,7 @@ buildActiveContext activeContext localContext baseUrl paramsFn = do | |||
379 | type EIT e m = REST (EIEnv e m) (JLDError e) EIState m | 379 | type EIT e m = REST (EIEnv e m) (JLDError e) EIState m |
380 | 380 | ||
381 | data EIEnv e m = EIEnv | 381 | data EIEnv e m = EIEnv |
382 | { eiEnvGlobal :: JLDEnv e m | 382 | { eiEnvGlobal :: JLDExpansionEnv e m |
383 | , eiEnvDocumentRelative :: Bool | 383 | , eiEnvDocumentRelative :: Bool |
384 | , eiEnvVocab :: Bool | 384 | , eiEnvVocab :: Bool |
385 | , eiEnvLocalContext :: Maybe Object | 385 | , eiEnvLocalContext :: Maybe Object |
@@ -387,7 +387,7 @@ data EIEnv e m = EIEnv | |||
387 | deriving (Show) | 387 | deriving (Show) |
388 | 388 | ||
389 | data EIState = EIState | 389 | data EIState = EIState |
390 | { eiStateGlobal :: JLDState | 390 | { eiStateGlobal :: JLDExpansionState |
391 | , eiStateDefined :: Map Text Bool | 391 | , eiStateDefined :: Map Text Bool |
392 | , eiStateActiveContext :: ActiveContext | 392 | , eiStateActiveContext :: ActiveContext |
393 | } | 393 | } |
@@ -500,7 +500,7 @@ expandIri' value | |||
500 | -- | 500 | -- |
501 | _ -> eiExpandResult value | 501 | _ -> eiExpandResult value |
502 | 502 | ||
503 | expandIri :: Monad m => ActiveContext -> Text -> (EIParams -> EIParams) -> JLDT e m (Maybe Text, ActiveContext, Map Text Bool) | 503 | expandIri :: Monad m => ActiveContext -> Text -> (EIParams -> EIParams) -> JLDExpansionT e m (Maybe Text, ActiveContext, Map Text Bool) |
504 | expandIri activeContext value paramsFn = do | 504 | expandIri activeContext value paramsFn = do |
505 | (value', EIState{..}) <- | 505 | (value', EIState{..}) <- |
506 | (expandIri' value >>= \v -> gets (v,)) | 506 | (expandIri' value >>= \v -> gets (v,)) |
@@ -537,7 +537,7 @@ expandIri activeContext value paramsFn = do | |||
537 | type BTDT e m = REST (BTDEnv e m) (Either (JLDError e) ()) BTDState m | 537 | type BTDT e m = REST (BTDEnv e m) (Either (JLDError e) ()) BTDState m |
538 | 538 | ||
539 | data BTDEnv e m = BTDEnv | 539 | data BTDEnv e m = BTDEnv |
540 | { btdEnvGlobal :: JLDEnv e m | 540 | { btdEnvGlobal :: JLDExpansionEnv e m |
541 | , btdEnvLocalContext :: Object | 541 | , btdEnvLocalContext :: Object |
542 | , btdEnvBaseUrl :: Maybe URI | 542 | , btdEnvBaseUrl :: Maybe URI |
543 | , btdEnvProtectedFlag :: Bool | 543 | , btdEnvProtectedFlag :: Bool |
@@ -547,7 +547,7 @@ data BTDEnv e m = BTDEnv | |||
547 | deriving (Show) | 547 | deriving (Show) |
548 | 548 | ||
549 | data BTDState = BTDState | 549 | data BTDState = BTDState |
550 | { btdStateGlobal :: JLDState | 550 | { btdStateGlobal :: JLDExpansionState |
551 | , btdStateDefined :: Map Text Bool | 551 | , btdStateDefined :: Map Text Bool |
552 | , btdStateTermDefinition :: TermDefinition | 552 | , btdStateTermDefinition :: TermDefinition |
553 | , btdStateActiveContext :: ActiveContext | 553 | , btdStateActiveContext :: ActiveContext |
@@ -573,10 +573,10 @@ btdModifyTermDefinition fn = modify \s -> s{btdStateTermDefinition = fn (btdStat | |||
573 | btdModifyDefined :: Monad m => (Map Text Bool -> Map Text Bool) -> BTDT e m () | 573 | btdModifyDefined :: Monad m => (Map Text Bool -> Map Text Bool) -> BTDT e m () |
574 | btdModifyDefined fn = modify \s -> s{btdStateDefined = fn (btdStateDefined s)} | 574 | btdModifyDefined fn = modify \s -> s{btdStateDefined = fn (btdStateDefined s)} |
575 | 575 | ||
576 | btdValidateContainer :: JLDEnv e m -> Value -> Bool | 576 | btdValidateContainer :: JLDExpansionEnv e m -> Value -> Bool |
577 | btdValidateContainer _ Null = False | 577 | btdValidateContainer _ Null = False |
578 | btdValidateContainer JLDEnv{..} value | 578 | btdValidateContainer JLDExpansionEnv{..} value |
579 | | JLD1_0 <- jldEnvProcessingMode = case value of | 579 | | JLD1_0 <- jldExpansionEnvProcessingMode = case value of |
580 | String value' -> isNotKeyword value' [KeywordGraph, KeywordId, KeywordType] | 580 | String value' -> isNotKeyword value' [KeywordGraph, KeywordId, KeywordType] |
581 | _ -> False | 581 | _ -> False |
582 | | otherwise = case flattenSingletonArray value of | 582 | | otherwise = case flattenSingletonArray value of |
@@ -647,7 +647,7 @@ buildTermDefinition' :: Monad m => Text -> BTDT e m () | |||
647 | buildTermDefinition' "" = throwError <| Left InvalidTermDefinition -- 2. | 647 | buildTermDefinition' "" = throwError <| Left InvalidTermDefinition -- 2. |
648 | buildTermDefinition' term = do | 648 | buildTermDefinition' term = do |
649 | BTDEnv{..} <- ask | 649 | BTDEnv{..} <- ask |
650 | let JLDEnv{..} = btdEnvGlobal | 650 | let JLDExpansionEnv{..} = btdEnvGlobal |
651 | 651 | ||
652 | -- 1. | 652 | -- 1. |
653 | gets (btdStateDefined .> M.lookup term) >>= \case | 653 | gets (btdStateDefined .> M.lookup term) >>= \case |
@@ -664,7 +664,7 @@ buildTermDefinition' term = do | |||
664 | -- 4. | 664 | -- 4. |
665 | case term of | 665 | case term of |
666 | ((`isKeyword` [KeywordType]) -> True) | 666 | ((`isKeyword` [KeywordType]) -> True) |
667 | | JLD1_0 <- jldEnvProcessingMode -> throwError <| Left KeywordRedefinition | 667 | | JLD1_0 <- jldExpansionEnvProcessingMode -> throwError <| Left KeywordRedefinition |
668 | | Object map' <- value -> | 668 | | Object map' <- value -> |
669 | if | 669 | if |
670 | | KM.size map' == 1 | 670 | | KM.size map' == 1 |
@@ -699,7 +699,7 @@ buildTermDefinition' term = do | |||
699 | 699 | ||
700 | -- 11. | 700 | -- 11. |
701 | case KM.lookup (show KeywordProtected) valueObject of | 701 | case KM.lookup (show KeywordProtected) valueObject of |
702 | Just _ | JLD1_0 <- jldEnvProcessingMode -> throwError <| Left InvalidTermDefinition | 702 | Just _ | JLD1_0 <- jldExpansionEnvProcessingMode -> throwError <| Left InvalidTermDefinition |
703 | Just (Bool protected) -> btdModifyTermDefinition \d -> d{termDefinitionProtectedFlag = protected} | 703 | Just (Bool protected) -> btdModifyTermDefinition \d -> d{termDefinitionProtectedFlag = protected} |
704 | Just invalid -> throwError <. Left <| InvalidKeywordValue KeywordProtected invalid | 704 | Just invalid -> throwError <. Left <| InvalidKeywordValue KeywordProtected invalid |
705 | Nothing -> pure () | 705 | Nothing -> pure () |
@@ -713,7 +713,7 @@ buildTermDefinition' term = do | |||
713 | Just expandedType | 713 | Just expandedType |
714 | -- 12.3. | 714 | -- 12.3. |
715 | | isKeyword expandedType [KeywordJson, KeywordNone] | 715 | | isKeyword expandedType [KeywordJson, KeywordNone] |
716 | , JLD1_0 <- jldEnvProcessingMode -> | 716 | , JLD1_0 <- jldExpansionEnvProcessingMode -> |
717 | throwError <| Left InvalidTypeMapping | 717 | throwError <| Left InvalidTypeMapping |
718 | -- 12.4. | 718 | -- 12.4. |
719 | | isNotKeyword expandedType [KeywordId, KeywordJson, KeywordNone, KeywordVocab] | 719 | | isNotKeyword expandedType [KeywordId, KeywordJson, KeywordNone, KeywordVocab] |
@@ -866,7 +866,7 @@ buildTermDefinition' term = do | |||
866 | containerMapping <- gets (btdStateTermDefinition .> termDefinitionContainerMapping) | 866 | containerMapping <- gets (btdStateTermDefinition .> termDefinitionContainerMapping) |
867 | case KM.lookup (show KeywordIndex) valueObject of | 867 | case KM.lookup (show KeywordIndex) valueObject of |
868 | -- 20.1. | 868 | -- 20.1. |
869 | Just _ | jldEnvProcessingMode == JLD1_0 || S.notMember (show KeywordIndex) containerMapping -> throwError <| Left InvalidTermDefinition | 869 | Just _ | jldExpansionEnvProcessingMode == JLD1_0 || S.notMember (show KeywordIndex) containerMapping -> throwError <| Left InvalidTermDefinition |
870 | -- 20.2. | 870 | -- 20.2. |
871 | Just (String index) -> | 871 | Just (String index) -> |
872 | btdExpandIri index >>= \case | 872 | btdExpandIri index >>= \case |
@@ -879,7 +879,7 @@ buildTermDefinition' term = do | |||
879 | -- 21. | 879 | -- 21. |
880 | case KM.lookup (show KeywordContext) valueObject of | 880 | case KM.lookup (show KeywordContext) valueObject of |
881 | -- 21.1. | 881 | -- 21.1. |
882 | Just _ | JLD1_0 <- jldEnvProcessingMode -> throwError <| Left InvalidTermDefinition | 882 | Just _ | JLD1_0 <- jldExpansionEnvProcessingMode -> throwError <| Left InvalidTermDefinition |
883 | -- 21.2. | 883 | -- 21.2. |
884 | Just context -> do | 884 | Just context -> do |
885 | -- 21.3. | 885 | -- 21.3. |
@@ -925,7 +925,7 @@ buildTermDefinition' term = do | |||
925 | -- 24. | 925 | -- 24. |
926 | case KM.lookup (show KeywordNest) valueObject of | 926 | case KM.lookup (show KeywordNest) valueObject of |
927 | -- 24.1. | 927 | -- 24.1. |
928 | Just _ | JLD1_0 <- jldEnvProcessingMode -> throwError <| Left InvalidTermDefinition | 928 | Just _ | JLD1_0 <- jldExpansionEnvProcessingMode -> throwError <| Left InvalidTermDefinition |
929 | Just (String nest) | 929 | Just (String nest) |
930 | | parseKeyword nest /= Just KeywordNest -> throwError <. Left <| InvalidKeywordValue KeywordNest (String nest) | 930 | | parseKeyword nest /= Just KeywordNest -> throwError <. Left <| InvalidKeywordValue KeywordNest (String nest) |
931 | | otherwise -> btdModifyTermDefinition \d -> d{termDefinitionNestValue = Just nest} | 931 | | otherwise -> btdModifyTermDefinition \d -> d{termDefinitionNestValue = Just nest} |
@@ -937,7 +937,7 @@ buildTermDefinition' term = do | |||
937 | case KM.lookup (show KeywordPrefix) valueObject of | 937 | case KM.lookup (show KeywordPrefix) valueObject of |
938 | -- 25.1. | 938 | -- 25.1. |
939 | Just _ | 939 | Just _ |
940 | | jldEnvProcessingMode == JLD1_0 || T.elem ':' term || T.elem '/' term -> | 940 | | jldExpansionEnvProcessingMode == JLD1_0 || T.elem ':' term || T.elem '/' term -> |
941 | throwError <| Left InvalidTermDefinition | 941 | throwError <| Left InvalidTermDefinition |
942 | Just (Bool prefix) | 942 | Just (Bool prefix) |
943 | | prefix, Just _ <- parseKeyword =<< maybeIriMapping -> throwError <| Left InvalidTermDefinition | 943 | | prefix, Just _ <- parseKeyword =<< maybeIriMapping -> throwError <| Left InvalidTermDefinition |
@@ -981,7 +981,7 @@ buildTermDefinition' term = do | |||
981 | 981 | ||
982 | btdModifyDefined <| M.insert term True | 982 | btdModifyDefined <| M.insert term True |
983 | 983 | ||
984 | buildTermDefinition :: Monad m => ActiveContext -> Object -> Text -> (BTDParams -> BTDParams) -> JLDT e m (ActiveContext, Map Text Bool) | 984 | buildTermDefinition :: Monad m => ActiveContext -> Object -> Text -> (BTDParams -> BTDParams) -> JLDExpansionT e m (ActiveContext, Map Text Bool) |
985 | buildTermDefinition activeContext localContext term paramsFn = do | 985 | buildTermDefinition activeContext localContext term paramsFn = do |
986 | BTDState{..} <- | 986 | BTDState{..} <- |
987 | (buildTermDefinition' term >> get) | 987 | (buildTermDefinition' term >> get) |
diff --git a/src/Data/JLD/Expansion/Global.hs b/src/Data/JLD/Expansion/Global.hs new file mode 100644 index 0000000..b92b4af --- /dev/null +++ b/src/Data/JLD/Expansion/Global.hs | |||
@@ -0,0 +1,38 @@ | |||
1 | module Data.JLD.Expansion.Global ( | ||
2 | JLDExpansionT, | ||
3 | JLDExpansionEnv (..), | ||
4 | JLDExpansionState (..), | ||
5 | hoistEnv, | ||
6 | modifyContextCache, | ||
7 | modifyDocumentCache, | ||
8 | ) where | ||
9 | |||
10 | import Data.JLD.Prelude | ||
11 | |||
12 | import Data.JLD.Control.Monad.RES (REST) | ||
13 | import Data.JLD.Error (JLDError) | ||
14 | import Data.JLD.Options (ContextCache, DocumentCache, DocumentLoader (..), JLDVersion (..), hoistDocumentLoader) | ||
15 | |||
16 | type JLDExpansionT e m = REST (JLDExpansionEnv e m) (JLDError e) JLDExpansionState m | ||
17 | |||
18 | data JLDExpansionEnv e m = JLDExpansionEnv | ||
19 | { jldExpansionEnvDocumentLoader :: DocumentLoader e m | ||
20 | , jldExpansionEnvProcessingMode :: JLDVersion | ||
21 | , jldExpansionEnvMaxRemoteContexts :: Int | ||
22 | } | ||
23 | deriving (Show) | ||
24 | |||
25 | data JLDExpansionState = JLDExpansionState | ||
26 | { jldExpansionStateContextCache :: ContextCache | ||
27 | , jldExpansionStateDocumentCache :: DocumentCache | ||
28 | } | ||
29 | deriving (Show, Eq) | ||
30 | |||
31 | hoistEnv :: (forall a. m a -> n a) -> JLDExpansionEnv e m -> JLDExpansionEnv e n | ||
32 | hoistEnv map' options = options{jldExpansionEnvDocumentLoader = options |> jldExpansionEnvDocumentLoader .> hoistDocumentLoader map'} | ||
33 | |||
34 | modifyContextCache :: MonadState JLDExpansionState m => (ContextCache -> ContextCache) -> m () | ||
35 | modifyContextCache fn = modify \s -> s{jldExpansionStateContextCache = fn (jldExpansionStateContextCache s)} | ||
36 | |||
37 | modifyDocumentCache :: MonadState JLDExpansionState m => (DocumentCache -> DocumentCache) -> m () | ||
38 | modifyDocumentCache fn = modify \s -> s{jldExpansionStateDocumentCache = fn (jldExpansionStateDocumentCache s)} | ||
diff --git a/src/Data/JLD/Monad.hs b/src/Data/JLD/Monad.hs deleted file mode 100644 index 3ae929d..0000000 --- a/src/Data/JLD/Monad.hs +++ /dev/null | |||
@@ -1,86 +0,0 @@ | |||
1 | module Data.JLD.Monad ( | ||
2 | JLDT, | ||
3 | JLDEnv (..), | ||
4 | JLDState (..), | ||
5 | newEnv, | ||
6 | newState, | ||
7 | hoistEnv, | ||
8 | modifyContextCache, | ||
9 | modifyDocumentCache, | ||
10 | JLDET, | ||
11 | JLDEEnv (..), | ||
12 | JLDEState (..), | ||
13 | modifyActiveContext, | ||
14 | ) where | ||
15 | |||
16 | import Data.JLD.Prelude | ||
17 | |||
18 | import Data.JLD.Control.Monad.RES (REST) | ||
19 | import Data.JLD.Error (JLDError) | ||
20 | import Data.JLD.Model.ActiveContext (ActiveContext) | ||
21 | import Data.JLD.Options (ContextCache, DocumentCache, DocumentLoader (..), JLDVersion (..), hoistDocumentLoader) | ||
22 | |||
23 | import Text.URI (URI) | ||
24 | |||
25 | type JLDT e m = REST (JLDEnv e m) (JLDError e) JLDState m | ||
26 | |||
27 | data JLDEnv e m = JLDEnv | ||
28 | { jldEnvDocumentLoader :: DocumentLoader e m | ||
29 | , jldEnvProcessingMode :: JLDVersion | ||
30 | , jldEnvMaxRemoteContexts :: Int | ||
31 | } | ||
32 | deriving (Show) | ||
33 | |||
34 | data JLDState = JLDState | ||
35 | { jldStateContextCache :: ContextCache | ||
36 | , jldStateDocumentCache :: DocumentCache | ||
37 | } | ||
38 | deriving (Show, Eq) | ||
39 | |||
40 | newEnv :: Applicative m => (JLDEnv () m -> JLDEnv e m) -> JLDEnv e m | ||
41 | newEnv fn = | ||
42 | fn | ||
43 | JLDEnv | ||
44 | { jldEnvDocumentLoader = DocumentLoader (const <. pure <| Left ()) | ||
45 | , jldEnvProcessingMode = JLD1_1 | ||
46 | , jldEnvMaxRemoteContexts = 20 | ||
47 | } | ||
48 | |||
49 | newState :: (JLDState -> JLDState) -> JLDState | ||
50 | newState fn = | ||
51 | fn | ||
52 | JLDState | ||
53 | { jldStateContextCache = mempty | ||
54 | , jldStateDocumentCache = mempty | ||
55 | } | ||
56 | |||
57 | hoistEnv :: (forall a. m a -> n a) -> JLDEnv e m -> JLDEnv e n | ||
58 | hoistEnv map' options = options{jldEnvDocumentLoader = options |> jldEnvDocumentLoader .> hoistDocumentLoader map'} | ||
59 | |||
60 | modifyContextCache :: MonadState JLDState m => (ContextCache -> ContextCache) -> m () | ||
61 | modifyContextCache fn = modify \s -> s{jldStateContextCache = fn (jldStateContextCache s)} | ||
62 | |||
63 | modifyDocumentCache :: MonadState JLDState m => (DocumentCache -> DocumentCache) -> m () | ||
64 | modifyDocumentCache fn = modify \s -> s{jldStateDocumentCache = fn (jldStateDocumentCache s)} | ||
65 | |||
66 | -- | ||
67 | |||
68 | type JLDET e m = REST (JLDEEnv e m) (JLDError e) JLDEState m | ||
69 | |||
70 | data JLDEEnv e m = JLDEEnv | ||
71 | { jldeEnvGlobal :: JLDEnv e m | ||
72 | , jldeEnvFrameExpansion :: Bool | ||
73 | , jldeEnvFromMap :: Bool | ||
74 | , jldeEnvBaseUrl :: URI | ||
75 | , jldeEnvActiveProperty :: Maybe Text | ||
76 | } | ||
77 | deriving (Show) | ||
78 | |||
79 | data JLDEState = JLDEState | ||
80 | { jldeStateGlobal :: JLDState | ||
81 | , jldeStateActiveContext :: ActiveContext | ||
82 | } | ||
83 | deriving (Show, Eq) | ||
84 | |||
85 | modifyActiveContext :: MonadState JLDEState m => (ActiveContext -> ActiveContext) -> m () | ||
86 | modifyActiveContext fn = modify \s -> s{jldeStateActiveContext = fn (jldeStateActiveContext s)} | ||
diff --git a/src/Data/JLD/NodeMap.hs b/src/Data/JLD/NodeMap.hs new file mode 100644 index 0000000..0c40c9a --- /dev/null +++ b/src/Data/JLD/NodeMap.hs | |||
@@ -0,0 +1,88 @@ | |||
1 | module Data.JLD.NodeMap (NodeMap, BNMParams (..)) where | ||
2 | |||
3 | import Data.JLD.Prelude | ||
4 | |||
5 | import Data.JLD.Control.Monad.RES (REST, execREST, runREST, withEnvRES, withErrorRES, withErrorRES', withStateRES) | ||
6 | import Data.JLD.Error (JLDError (..)) | ||
7 | import Data.JLD.Model.ActiveContext (ActiveContext (..), containsProtectedTerm, lookupTerm, newActiveContext) | ||
8 | import Data.JLD.Model.Direction (Direction (..)) | ||
9 | import Data.JLD.Model.IRI (CompactIRI (..), endsWithGenericDelim, isBlankIri, parseCompactIri) | ||
10 | import Data.JLD.Model.Keyword (Keyword (..), allKeywords, isKeyword, isKeywordLike, isNotKeyword, parseKeyword) | ||
11 | import Data.JLD.Model.Language (Language (..)) | ||
12 | import Data.JLD.Model.TermDefinition (TermDefinition (..), newTermDefinition) | ||
13 | import Data.JLD.Model.URI (parseUri, uriToIri) | ||
14 | import Data.JLD.Options (ContextCache, Document (..), DocumentCache, DocumentLoader (..), JLDVersion (..)) | ||
15 | import Data.JLD.Util (flattenSingletonArray, valueContains, valueContainsAny, valueIsTrue, valueToArray) | ||
16 | |||
17 | import Control.Monad.Except (MonadError (..)) | ||
18 | import Data.Aeson (Object, Value (..)) | ||
19 | import Data.Aeson.Key qualified as K (fromText, toText) | ||
20 | import Data.Aeson.KeyMap qualified as KM (delete, keys, lookup, member, size) | ||
21 | import Data.Map.Strict qualified as M (delete, insert, lookup) | ||
22 | import Data.RDF (parseIRI, parseRelIRI, resolveIRI, serializeIRI, validateIRI) | ||
23 | import Data.Set qualified as S (insert, member, notMember, size) | ||
24 | import Data.Text qualified as T (drop, dropEnd, elem, findIndex, isPrefixOf, null, take, toLower) | ||
25 | import Data.Vector qualified as V (length) | ||
26 | import Text.URI (URI, isPathAbsolute, relativeTo) | ||
27 | import Text.URI qualified as U (render) | ||
28 | |||
29 | type NodeMap = Map (Text, Text, Text) Value | ||
30 | |||
31 | type BNMT e m = REST BNMEnv (JLDError e) BNMState m | ||
32 | |||
33 | data BNMEnv = BNMEnv | ||
34 | { bnmEnvDocument :: Value | ||
35 | , bnmEnvActiveGraph :: Text | ||
36 | , bnmEnvActiveSubject :: Maybe Text | ||
37 | , bnmEnvActiveProperty :: Maybe Text | ||
38 | } | ||
39 | deriving (Show) | ||
40 | |||
41 | newtype BNMState = BNMState | ||
42 | { bnmStateNodeMap :: NodeMap | ||
43 | } | ||
44 | deriving (Show, Eq) | ||
45 | |||
46 | data BNMParams = BNMParams | ||
47 | { bnmParamsNodeMap :: NodeMap | ||
48 | , bnmParamsActiveGraph :: Text | ||
49 | , bnmParamsActiveSubject :: Maybe Text | ||
50 | , bnmParamsActiveProperty :: Maybe Text | ||
51 | , bnmParamsList :: Map Text Value | ||
52 | } | ||
53 | deriving (Show, Eq) | ||
54 | |||
55 | bnmModifyNodeMap :: Monad m => (NodeMap -> NodeMap) -> BNMT e m () | ||
56 | bnmModifyNodeMap fn = modify \s -> s{bnmStateNodeMap = fn (bnmStateNodeMap s)} | ||
57 | |||
58 | buildNodeMap' :: Monad m => BNMT e m () | ||
59 | buildNodeMap' = do | ||
60 | pure () | ||
61 | |||
62 | buildNodeMap :: Monad m => Value -> (BNMParams -> BNMParams) -> m NodeMap | ||
63 | buildNodeMap document paramsFn = do | ||
64 | BNMState{..} <- buildNodeMap' |> execREST env st | ||
65 | pure bnmStateNodeMap | ||
66 | where | ||
67 | BNMParams{..} = | ||
68 | paramsFn | ||
69 | BNMParams | ||
70 | { bnmParamsNodeMap = mempty | ||
71 | , bnmParamsActiveGraph = show KeywordDefault | ||
72 | , bnmParamsActiveSubject = Nothing | ||
73 | , bnmParamsActiveProperty = Nothing | ||
74 | , bnmParamsList = mempty | ||
75 | } | ||
76 | |||
77 | env = | ||
78 | BNMEnv | ||
79 | { bnmEnvDocument = document | ||
80 | , bnmEnvActiveGraph = bnmParamsActiveGraph | ||
81 | , bnmEnvActiveSubject = bnmParamsActiveSubject | ||
82 | , bnmEnvActiveProperty = bnmParamsActiveProperty | ||
83 | } | ||
84 | |||
85 | st = | ||
86 | BNMState | ||
87 | { bnmStateNodeMap = bnmParamsNodeMap | ||
88 | } | ||
diff --git a/test/Test/Expansion.hs b/test/Test/Expansion.hs index 33397f4..89024c6 100644 --- a/test/Test/Expansion.hs +++ b/test/Test/Expansion.hs | |||
@@ -2,9 +2,8 @@ module Test.Expansion (W3CExpansionTestList, expansionTests) where | |||
2 | 2 | ||
3 | import Data.JLD.Prelude | 3 | import Data.JLD.Prelude |
4 | 4 | ||
5 | import Data.JLD (DocumentLoader (..), JLDExpandParams (..), JLDVersion (..), expand, mimeType, toJldErrorCode) | 5 | import Data.JLD (DocumentLoader (..), JLDExpansionParams (..), JLDVersion (..), expand, mimeType, toJldErrorCode) |
6 | import Data.JLD.Model.URI (parseUri) | 6 | import Data.JLD.Model.URI (parseUri) |
7 | import Data.JLD.Monad (JLDEnv (..), newEnv) | ||
8 | 7 | ||
9 | import Test.Tasty | 8 | import Test.Tasty |
10 | import Test.Tasty.ExpectedFailure (ignoreTestBecause) | 9 | import Test.Tasty.ExpectedFailure (ignoreTestBecause) |
@@ -31,7 +30,7 @@ instance FromJSON W3CExpansionTestOption where | |||
31 | <*> (v .:? "processingMode") | 30 | <*> (v .:? "processingMode") |
32 | <*> (v .:? "base") | 31 | <*> (v .:? "base") |
33 | <*> (v .:? "expandContext") | 32 | <*> (v .:? "expandContext") |
34 | parseJSON invalid = prependFailure "parsing Coord failed, " (typeMismatch "Object" invalid) | 33 | parseJSON invalid = prependFailure "parsing W3CExpansionTestOption failed, " (typeMismatch "Object" invalid) |
35 | 34 | ||
36 | data W3CExpansionTest = W3CExpansionTest | 35 | data W3CExpansionTest = W3CExpansionTest |
37 | { w3cExpansionTestName :: Text | 36 | { w3cExpansionTestName :: Text |
@@ -50,7 +49,7 @@ instance FromJSON W3CExpansionTest where | |||
50 | <*> (v .:? "expect") | 49 | <*> (v .:? "expect") |
51 | <*> (v .:? "expectErrorCode") | 50 | <*> (v .:? "expectErrorCode") |
52 | <*> (v .:? "option") | 51 | <*> (v .:? "option") |
53 | parseJSON invalid = prependFailure "parsing Coord failed, " (typeMismatch "Object" invalid) | 52 | parseJSON invalid = prependFailure "parsing W3CExpansionTest failed, " (typeMismatch "Object" invalid) |
54 | 53 | ||
55 | newtype W3CExpansionTestList = W3CExpansionTestList | 54 | newtype W3CExpansionTestList = W3CExpansionTestList |
56 | { w3cExpansionSequence :: [W3CExpansionTest] | 55 | { w3cExpansionSequence :: [W3CExpansionTest] |
@@ -59,7 +58,7 @@ newtype W3CExpansionTestList = W3CExpansionTestList | |||
59 | 58 | ||
60 | instance FromJSON W3CExpansionTestList where | 59 | instance FromJSON W3CExpansionTestList where |
61 | parseJSON (Object v) = W3CExpansionTestList <$> (v .: "sequence") | 60 | parseJSON (Object v) = W3CExpansionTestList <$> (v .: "sequence") |
62 | parseJSON invalid = prependFailure "parsing Coord failed, " (typeMismatch "Object" invalid) | 61 | parseJSON invalid = prependFailure "parsing W3CExpansionTestList failed, " (typeMismatch "Object" invalid) |
63 | 62 | ||
64 | documentLoader :: MonadIO m => DocumentLoader Text m | 63 | documentLoader :: MonadIO m => DocumentLoader Text m |
65 | documentLoader = DocumentLoader \uri -> | 64 | documentLoader = DocumentLoader \uri -> |
@@ -75,7 +74,7 @@ fetchTest url = do | |||
75 | res <- req GET reqUrl NoReqBody jsonResponse (reqOptions <> header "Accept" mimeType) | 74 | res <- req GET reqUrl NoReqBody jsonResponse (reqOptions <> header "Accept" mimeType) |
76 | pure <| responseBody res | 75 | pure <| responseBody res |
77 | 76 | ||
78 | parseOptions :: URI -> URI -> Maybe W3CExpansionTestOption -> IO (URI, JLDExpandParams () IO -> JLDExpandParams Text IO) | 77 | parseOptions :: URI -> URI -> Maybe W3CExpansionTestOption -> IO (URI, JLDExpansionParams () IO -> JLDExpansionParams Text IO) |
79 | parseOptions baseUrl inputUrl maybeOptions = do | 78 | parseOptions baseUrl inputUrl maybeOptions = do |
80 | expandContext <- case maybeOptions >>= w3cExpansionTestOptionExpandContext of | 79 | expandContext <- case maybeOptions >>= w3cExpansionTestOptionExpandContext of |
81 | Just rawUrl -> do | 80 | Just rawUrl -> do |
@@ -85,20 +84,18 @@ parseOptions baseUrl inputUrl maybeOptions = do | |||
85 | 84 | ||
86 | let params p = | 85 | let params p = |
87 | p | 86 | p |
88 | { jldExpandParamsEnv = env' | 87 | { jldExpansionParamsDocumentLoader = documentLoader |
89 | , jldExpandParamsExpandContext = expandContext <|> jldExpandParamsExpandContext p | 88 | , jldExpansionParamsProcessingMode = case maybeOptions >>= w3cExpansionTestOptionProcessingMode of |
89 | Just "json-ld-1.0" -> JLD1_0 | ||
90 | Just "json-ld-1.1" -> JLD1_1 | ||
91 | _ -> jldExpansionParamsProcessingMode p | ||
92 | , jldExpansionParamsExpandContext = expandContext <|> jldExpansionParamsExpandContext p | ||
90 | } | 93 | } |
91 | 94 | ||
92 | pure (expandBaseUrl, params) | 95 | pure (expandBaseUrl, params) |
93 | where | 96 | where |
94 | expandBaseUrl = fromMaybe inputUrl (parseUri =<< w3cExpansionTestOptionBase =<< maybeOptions) | 97 | expandBaseUrl = fromMaybe inputUrl (parseUri =<< w3cExpansionTestOptionBase =<< maybeOptions) |
95 | 98 | ||
96 | env = newEnv \e -> e{jldEnvDocumentLoader = documentLoader} | ||
97 | env' = case maybeOptions >>= w3cExpansionTestOptionProcessingMode of | ||
98 | Just "json-ld-1.0" -> env{jldEnvProcessingMode = JLD1_0} | ||
99 | Just "json-ld-1.1" -> env{jldEnvProcessingMode = JLD1_1} | ||
100 | _ -> env | ||
101 | |||
102 | expansionTests :: W3CExpansionTestList -> TestTree | 99 | expansionTests :: W3CExpansionTestList -> TestTree |
103 | expansionTests testList = testGroup "Expansion" <| uncurry expansionTest <$> (take 999 <. drop 0 <| zip (w3cExpansionSequence testList) [1 ..]) | 100 | expansionTests testList = testGroup "Expansion" <| uncurry expansionTest <$> (take 999 <. drop 0 <| zip (w3cExpansionSequence testList) [1 ..]) |
104 | 101 | ||