diff options
author | Volpeon <github@volpeon.ink> | 2023-05-27 21:19:25 +0200 |
---|---|---|
committer | Volpeon <github@volpeon.ink> | 2023-05-27 21:19:25 +0200 |
commit | 4f3dc25f63c21fc74f1b2370335eb83c27d42ecd (patch) | |
tree | f1a481dddce751e2c1f98419ca0610cfdac1c277 /src/Data/JLD/Flattening | |
parent | Added Node Map Generation algorithm (diff) | |
download | hs-jsonld-4f3dc25f63c21fc74f1b2370335eb83c27d42ecd.tar.gz hs-jsonld-4f3dc25f63c21fc74f1b2370335eb83c27d42ecd.tar.bz2 hs-jsonld-4f3dc25f63c21fc74f1b2370335eb83c27d42ecd.zip |
Added Node Map Merging algorithm
Diffstat (limited to 'src/Data/JLD/Flattening')
-rw-r--r-- | src/Data/JLD/Flattening/NodeMap.hs | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/src/Data/JLD/Flattening/NodeMap.hs b/src/Data/JLD/Flattening/NodeMap.hs index 3747402..919aec7 100644 --- a/src/Data/JLD/Flattening/NodeMap.hs +++ b/src/Data/JLD/Flattening/NodeMap.hs | |||
@@ -1,21 +1,21 @@ | |||
1 | module Data.JLD.Flattening.NodeMap (NodeMap, BNMParams (..), buildNodeMap) where | 1 | module Data.JLD.Flattening.NodeMap (NodeMap, BNMParams (..), buildNodeMap, mergeNodeMaps) where |
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, execREST, withErrorRES') |
6 | import Data.JLD.Error (JLDError (..)) | 6 | import Data.JLD.Error (JLDError (..)) |
7 | import Data.JLD.Model.IRI (isBlankIri) | 7 | import Data.JLD.Model.IRI (isBlankIri) |
8 | import Data.JLD.Model.Keyword (Keyword (..), isNotKeyword) | 8 | import Data.JLD.Model.Keyword (Keyword (..), isKeywordLike, isNotKeyword) |
9 | import Data.JLD.Model.NodeMap (NodeMap) | 9 | import Data.JLD.Model.NodeMap (NodeMap, PropertyMap) |
10 | import Data.JLD.Model.NodeMap qualified as N (hasKey2, hasKey3, insert, lookup3, memberArray, modifyArray) | 10 | import Data.JLD.Model.NodeMap qualified as N (hasKey2, hasKey3, insert, lookup2, lookup3, memberArray, modifyArray) |
11 | import Data.JLD.Model.NodeObject (isNodeObject) | 11 | import Data.JLD.Model.NodeObject (isNodeObject) |
12 | import Data.JLD.Util (valueIsScalar, valueToArray, valueToNonNullArray) | 12 | import Data.JLD.Util (valueIsScalar, valueToArray, valueToNonNullArray) |
13 | 13 | ||
14 | import Control.Monad.Except (MonadError (..)) | 14 | import Control.Monad.Except (MonadError (..)) |
15 | import Data.Aeson (Array, Object, Value (..)) | 15 | import Data.Aeson (Array, Key, Object, Value (..)) |
16 | import Data.Aeson.Key qualified as K (toText) | 16 | import Data.Aeson.Key qualified as K (toText) |
17 | import Data.Aeson.KeyMap qualified as KM (filterWithKey, insert, lookup, member, singleton) | 17 | import Data.Aeson.KeyMap qualified as KM (filterWithKey, insert, lookup, member, singleton) |
18 | import Data.Foldable.WithIndex (iforM_) | 18 | import Data.Foldable.WithIndex (FoldableWithIndex (..), iforM_) |
19 | import Data.Map.Strict qualified as M (insert, lookup) | 19 | import Data.Map.Strict qualified as M (insert, lookup) |
20 | import Data.Vector qualified as V (singleton, snoc, uniq) | 20 | import Data.Vector qualified as V (singleton, snoc, uniq) |
21 | 21 | ||
@@ -300,3 +300,26 @@ buildNodeMap document paramsFn = do | |||
300 | , bnmStateIdentifierCounter = 1 | 300 | , bnmStateIdentifierCounter = 1 |
301 | , bnmStateIdentifierMap = mempty | 301 | , bnmStateIdentifierMap = mempty |
302 | } | 302 | } |
303 | |||
304 | mergeNodeMaps :: NodeMap -> NodeMap | ||
305 | mergeNodeMaps = foldl' (ifoldl' go) mempty | ||
306 | where | ||
307 | go :: Maybe Text -> NodeMap -> PropertyMap -> NodeMap | ||
308 | go subjectKey result = ifoldl' (go' subjectKey) result' | ||
309 | where | ||
310 | result' = case N.lookup2 (show KeywordMerged) subjectKey result of | ||
311 | Just _ -> result | ||
312 | Nothing -> N.insert (show KeywordMerged) subjectKey (Just <| show KeywordId) (maybe Null String subjectKey) result | ||
313 | |||
314 | go' :: Maybe Text -> Maybe Text -> NodeMap -> Value -> NodeMap | ||
315 | go' subjectKey propertyKey result property | ||
316 | | propertyKey /= Just (show KeywordType) && maybe False isKeywordLike propertyKey = | ||
317 | N.insert (show KeywordMerged) subjectKey propertyKey property result | ||
318 | | otherwise = | ||
319 | N.insert (show KeywordMerged) subjectKey propertyKey array result | ||
320 | where | ||
321 | array = | ||
322 | Array | ||
323 | <. (<> valueToArray property) | ||
324 | <. maybe mempty valueToArray | ||
325 | <| N.lookup3 (show KeywordMerged) subjectKey propertyKey result | ||