diff options
author | Volpeon <github@volpeon.ink> | 2023-05-28 08:13:08 +0200 |
---|---|---|
committer | Volpeon <github@volpeon.ink> | 2023-05-28 08:13:08 +0200 |
commit | 8c49a30faa431b8b56a4b4926e7dae56b1311fea (patch) | |
tree | 6a103b49cdfe6df38fadad1f9d59521dd92ebf74 /src/Data | |
parent | Added Node Map Merging algorithm (diff) | |
download | hs-jsonld-8c49a30faa431b8b56a4b4926e7dae56b1311fea.tar.gz hs-jsonld-8c49a30faa431b8b56a4b4926e7dae56b1311fea.tar.bz2 hs-jsonld-8c49a30faa431b8b56a4b4926e7dae56b1311fea.zip |
Completed untested Flattening implementation
Diffstat (limited to 'src/Data')
-rw-r--r-- | src/Data/JLD.hs | 9 | ||||
-rw-r--r-- | src/Data/JLD/Flattening.hs | 44 | ||||
-rw-r--r-- | src/Data/JLD/Flattening/Global.hs | 7 | ||||
-rw-r--r-- | src/Data/JLD/Flattening/NodeMap.hs | 18 | ||||
-rw-r--r-- | src/Data/JLD/Model/NodeMap.hs | 16 |
5 files changed, 87 insertions, 7 deletions
diff --git a/src/Data/JLD.hs b/src/Data/JLD.hs index c5c28eb..d7688d0 100644 --- a/src/Data/JLD.hs +++ b/src/Data/JLD.hs | |||
@@ -5,6 +5,7 @@ module Data.JLD ( | |||
5 | JLDExpansionParams (..), | 5 | JLDExpansionParams (..), |
6 | JLDExpansionState (..), | 6 | JLDExpansionState (..), |
7 | expand, | 7 | expand, |
8 | flatten, | ||
8 | ) where | 9 | ) where |
9 | 10 | ||
10 | import Data.JLD.Prelude | 11 | import Data.JLD.Prelude |
@@ -15,6 +16,7 @@ import Data.JLD.Expansion (JLDEParams (..)) | |||
15 | import Data.JLD.Expansion qualified as E (expand) | 16 | import Data.JLD.Expansion qualified as E (expand) |
16 | import Data.JLD.Expansion.Context (buildActiveContext) | 17 | import Data.JLD.Expansion.Context (buildActiveContext) |
17 | import Data.JLD.Expansion.Global (JLDExpansionEnv (..), JLDExpansionState (..)) | 18 | import Data.JLD.Expansion.Global (JLDExpansionEnv (..), JLDExpansionState (..)) |
19 | import Data.JLD.Flattening qualified as F (flatten) | ||
18 | import Data.JLD.Mime | 20 | import Data.JLD.Mime |
19 | import Data.JLD.Model.ActiveContext (ActiveContext (..), newActiveContext) | 21 | import Data.JLD.Model.ActiveContext (ActiveContext (..), newActiveContext) |
20 | import Data.JLD.Model.Keyword (Keyword (..)) | 22 | import Data.JLD.Model.Keyword (Keyword (..)) |
@@ -97,3 +99,10 @@ expand document baseUrl paramsFn = do | |||
97 | Left err -> Left err | 99 | Left err -> Left err |
98 | 100 | ||
99 | pure (result', state') | 101 | pure (result', state') |
102 | |||
103 | flatten :: Monad m => Value -> URI -> (JLDExpansionParams () m -> JLDExpansionParams e m) -> m (Either (JLDError e) Value, JLDExpansionState) | ||
104 | flatten document baseUrl paramsFn = do | ||
105 | (result, state') <- expand document baseUrl paramsFn | ||
106 | case result of | ||
107 | Left err -> pure (Left err, state') | ||
108 | Right expanded -> fmap (,state') <. runExceptT <| F.flatten expanded | ||
diff --git a/src/Data/JLD/Flattening.hs b/src/Data/JLD/Flattening.hs new file mode 100644 index 0000000..2bfd8dd --- /dev/null +++ b/src/Data/JLD/Flattening.hs | |||
@@ -0,0 +1,44 @@ | |||
1 | module Data.JLD.Flattening (flatten) where | ||
2 | |||
3 | import Data.JLD.Prelude | ||
4 | |||
5 | import Data.JLD.Flattening.NodeMap (buildNodeMap) | ||
6 | |||
7 | import Data.Aeson (Array, Value (..)) | ||
8 | import Data.Foldable.WithIndex (FoldableWithIndex (..)) | ||
9 | import Data.JLD.Flattening.Global (JLDFlatteningT) | ||
10 | import Data.JLD.Model.Keyword (Keyword (..)) | ||
11 | import Data.JLD.Model.NodeMap (PropertyMap, SubjectMap, propsToKeyMap) | ||
12 | import Data.Map qualified as M (insert, lookup, member, singleton, size) | ||
13 | import Data.Vector qualified as V | ||
14 | |||
15 | collectGraphsStep :: Text -> SubjectMap -> SubjectMap -> SubjectMap | ||
16 | collectGraphsStep graphName dg graph | ||
17 | | graphName == show KeywordDefault = dg | ||
18 | | otherwise = M.insert (Just graphName) entry' dg | ||
19 | where | ||
20 | -- 4.1. 4.2. | ||
21 | entry = case M.lookup (Just graphName) dg of | ||
22 | Just e -> e | ||
23 | Nothing -> M.singleton (Just <| show KeywordId) (String graphName) | ||
24 | |||
25 | graphArray = Array <| foldl' collectNodesStep mempty graph | ||
26 | |||
27 | entry' = M.insert (Just <| show KeywordGraph) graphArray entry | ||
28 | |||
29 | collectNodesStep :: Array -> PropertyMap -> Array | ||
30 | collectNodesStep ar node | ||
31 | | M.size node == 1 && M.member (Just <| show KeywordId) node = ar | ||
32 | | otherwise = V.snoc ar (Object <| propsToKeyMap node) | ||
33 | |||
34 | flatten :: Monad m => Value -> JLDFlatteningT e m Value | ||
35 | flatten element = do | ||
36 | -- 1. 2. | ||
37 | nodeMap <- fst <$> buildNodeMap element id | ||
38 | |||
39 | -- 3. 4. | ||
40 | let defaultGraph = fromMaybe mempty <| M.lookup (show KeywordDefault) nodeMap | ||
41 | defaultGraph' = ifoldl' collectGraphsStep defaultGraph nodeMap | ||
42 | |||
43 | -- 5. 6. 7. | ||
44 | pure <. Array <| foldl' collectNodesStep mempty defaultGraph' | ||
diff --git a/src/Data/JLD/Flattening/Global.hs b/src/Data/JLD/Flattening/Global.hs new file mode 100644 index 0000000..591d3ad --- /dev/null +++ b/src/Data/JLD/Flattening/Global.hs | |||
@@ -0,0 +1,7 @@ | |||
1 | module Data.JLD.Flattening.Global (JLDFlatteningT) where | ||
2 | |||
3 | import Data.JLD.Prelude | ||
4 | |||
5 | import Data.JLD.Error (JLDError) | ||
6 | |||
7 | type JLDFlatteningT e m = ExceptT (JLDError e) m | ||
diff --git a/src/Data/JLD/Flattening/NodeMap.hs b/src/Data/JLD/Flattening/NodeMap.hs index 919aec7..65db9ab 100644 --- a/src/Data/JLD/Flattening/NodeMap.hs +++ b/src/Data/JLD/Flattening/NodeMap.hs | |||
@@ -2,8 +2,9 @@ module Data.JLD.Flattening.NodeMap (NodeMap, BNMParams (..), buildNodeMap, merge | |||
2 | 2 | ||
3 | import Data.JLD.Prelude | 3 | import Data.JLD.Prelude |
4 | 4 | ||
5 | import Data.JLD.Control.Monad.RES (REST, execREST, withErrorRES') | 5 | import Data.JLD.Control.Monad.RES (REST, runREST, withErrorRES') |
6 | import Data.JLD.Error (JLDError (..)) | 6 | import Data.JLD.Error (JLDError (..)) |
7 | import Data.JLD.Flattening.Global (JLDFlatteningT) | ||
7 | import Data.JLD.Model.IRI (isBlankIri) | 8 | import Data.JLD.Model.IRI (isBlankIri) |
8 | import Data.JLD.Model.Keyword (Keyword (..), isKeywordLike, isNotKeyword) | 9 | import Data.JLD.Model.Keyword (Keyword (..), isKeywordLike, isNotKeyword) |
9 | import Data.JLD.Model.NodeMap (NodeMap, PropertyMap) | 10 | import Data.JLD.Model.NodeMap (NodeMap, PropertyMap) |
@@ -12,7 +13,7 @@ import Data.JLD.Model.NodeObject (isNodeObject) | |||
12 | import Data.JLD.Util (valueIsScalar, valueToArray, valueToNonNullArray) | 13 | import Data.JLD.Util (valueIsScalar, valueToArray, valueToNonNullArray) |
13 | 14 | ||
14 | import Control.Monad.Except (MonadError (..)) | 15 | import Control.Monad.Except (MonadError (..)) |
15 | import Data.Aeson (Array, Key, Object, Value (..)) | 16 | import Data.Aeson (Array, Object, Value (..)) |
16 | import Data.Aeson.Key qualified as K (toText) | 17 | import Data.Aeson.Key qualified as K (toText) |
17 | import Data.Aeson.KeyMap qualified as KM (filterWithKey, insert, lookup, member, singleton) | 18 | import Data.Aeson.KeyMap qualified as KM (filterWithKey, insert, lookup, member, singleton) |
18 | import Data.Foldable.WithIndex (FoldableWithIndex (..), iforM_) | 19 | import Data.Foldable.WithIndex (FoldableWithIndex (..), iforM_) |
@@ -85,7 +86,10 @@ bnmBuildNodeMap value paramsFn = do | |||
85 | , bnmParamsActiveSubject = bnmEnvActiveSubject | 86 | , bnmParamsActiveSubject = bnmEnvActiveSubject |
86 | , bnmParamsActiveProperty = bnmEnvActiveProperty | 87 | , bnmParamsActiveProperty = bnmEnvActiveProperty |
87 | } | 88 | } |
88 | (nodeMap', list) <- buildNodeMap value params | 89 | (nodeMap', list) <- |
90 | buildNodeMap value params |> runExceptT >=> \case | ||
91 | Left err -> throwError <| Left err | ||
92 | Right a -> pure a | ||
89 | bnmModifyNodeMap <| const nodeMap' | 93 | bnmModifyNodeMap <| const nodeMap' |
90 | pure list | 94 | pure list |
91 | 95 | ||
@@ -269,10 +273,12 @@ buildNodeMap' element = case element of | |||
269 | -- | 273 | -- |
270 | _ -> pure () | 274 | _ -> pure () |
271 | 275 | ||
272 | buildNodeMap :: Monad m => Value -> (BNMParams -> BNMParams) -> m (NodeMap, Maybe Array) | 276 | buildNodeMap :: Monad m => Value -> (BNMParams -> BNMParams) -> JLDFlatteningT e m (NodeMap, Maybe Array) |
273 | buildNodeMap document paramsFn = do | 277 | buildNodeMap document paramsFn = do |
274 | BNMState{..} <- buildNodeMap' document |> execREST env st | 278 | (result, BNMState{..}) <- buildNodeMap' document |> runREST env st |
275 | pure (bnmStateNodeMap, bnmStateList) | 279 | case result of |
280 | Left (Left err) -> throwError err | ||
281 | _ -> pure (bnmStateNodeMap, bnmStateList) | ||
276 | where | 282 | where |
277 | BNMParams{..} = | 283 | BNMParams{..} = |
278 | paramsFn | 284 | paramsFn |
diff --git a/src/Data/JLD/Model/NodeMap.hs b/src/Data/JLD/Model/NodeMap.hs index d0fb2f9..f76c662 100644 --- a/src/Data/JLD/Model/NodeMap.hs +++ b/src/Data/JLD/Model/NodeMap.hs | |||
@@ -10,13 +10,18 @@ module Data.JLD.Model.NodeMap ( | |||
10 | hasKey2, | 10 | hasKey2, |
11 | hasKey3, | 11 | hasKey3, |
12 | memberArray, | 12 | memberArray, |
13 | propsToKeyMap, | ||
13 | ) where | 14 | ) where |
14 | 15 | ||
15 | import Data.JLD.Prelude hiding (modify) | 16 | import Data.JLD.Prelude hiding (modify) |
16 | 17 | ||
17 | import Data.Aeson (Array, Value (..)) | 18 | import Data.Aeson (Array, Value (..)) |
19 | import Data.Aeson.Key qualified as K | ||
20 | import Data.Aeson.KeyMap (KeyMap) | ||
21 | import Data.Aeson.KeyMap qualified as KM | ||
22 | import Data.Foldable.WithIndex (FoldableWithIndex (..)) | ||
18 | import Data.JLD.Util (valueToArray) | 23 | import Data.JLD.Util (valueToArray) |
19 | import Data.Map.Strict qualified as M (alter, insert, lookup, member) | 24 | import Data.Map.Strict qualified as M (alter, insert, lookup, member, toList) |
20 | 25 | ||
21 | type PropertyKey = Maybe Text | 26 | type PropertyKey = Maybe Text |
22 | type PropertyMap = Map PropertyKey Value | 27 | type PropertyMap = Map PropertyKey Value |
@@ -55,3 +60,12 @@ memberArray :: GraphKey -> SubjectKey -> PropertyKey -> Value -> NodeMap -> Bool | |||
55 | memberArray graphName subject property value nodeMap = case lookup3 graphName subject property nodeMap of | 60 | memberArray graphName subject property value nodeMap = case lookup3 graphName subject property nodeMap of |
56 | Just (Array a) -> value `elem` a | 61 | Just (Array a) -> value `elem` a |
57 | _ -> False | 62 | _ -> False |
63 | |||
64 | propsToKeyMap :: PropertyMap -> KeyMap Value | ||
65 | propsToKeyMap = | ||
66 | ifoldl' | ||
67 | ( \maybeKey km value -> case maybeKey of | ||
68 | Just key -> KM.insert (K.fromText key) value km | ||
69 | Nothing -> km | ||
70 | ) | ||
71 | mempty | ||