aboutsummaryrefslogtreecommitdiffstats
path: root/src/Data/JLD/Flattening
diff options
context:
space:
mode:
authorVolpeon <github@volpeon.ink>2023-05-27 21:19:25 +0200
committerVolpeon <github@volpeon.ink>2023-05-27 21:19:25 +0200
commit4f3dc25f63c21fc74f1b2370335eb83c27d42ecd (patch)
treef1a481dddce751e2c1f98419ca0610cfdac1c277 /src/Data/JLD/Flattening
parentAdded Node Map Generation algorithm (diff)
downloadhs-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.hs35
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 @@
1module Data.JLD.Flattening.NodeMap (NodeMap, BNMParams (..), buildNodeMap) where 1module Data.JLD.Flattening.NodeMap (NodeMap, BNMParams (..), buildNodeMap, mergeNodeMaps) where
2 2
3import Data.JLD.Prelude 3import Data.JLD.Prelude
4 4
5import Data.JLD.Control.Monad.RES (REST, execREST, withErrorRES') 5import Data.JLD.Control.Monad.RES (REST, execREST, withErrorRES')
6import Data.JLD.Error (JLDError (..)) 6import Data.JLD.Error (JLDError (..))
7import Data.JLD.Model.IRI (isBlankIri) 7import Data.JLD.Model.IRI (isBlankIri)
8import Data.JLD.Model.Keyword (Keyword (..), isNotKeyword) 8import Data.JLD.Model.Keyword (Keyword (..), isKeywordLike, isNotKeyword)
9import Data.JLD.Model.NodeMap (NodeMap) 9import Data.JLD.Model.NodeMap (NodeMap, PropertyMap)
10import Data.JLD.Model.NodeMap qualified as N (hasKey2, hasKey3, insert, lookup3, memberArray, modifyArray) 10import Data.JLD.Model.NodeMap qualified as N (hasKey2, hasKey3, insert, lookup2, lookup3, memberArray, modifyArray)
11import Data.JLD.Model.NodeObject (isNodeObject) 11import Data.JLD.Model.NodeObject (isNodeObject)
12import Data.JLD.Util (valueIsScalar, valueToArray, valueToNonNullArray) 12import Data.JLD.Util (valueIsScalar, valueToArray, valueToNonNullArray)
13 13
14import Control.Monad.Except (MonadError (..)) 14import Control.Monad.Except (MonadError (..))
15import Data.Aeson (Array, Object, Value (..)) 15import Data.Aeson (Array, Key, Object, Value (..))
16import Data.Aeson.Key qualified as K (toText) 16import Data.Aeson.Key qualified as K (toText)
17import Data.Aeson.KeyMap qualified as KM (filterWithKey, insert, lookup, member, singleton) 17import Data.Aeson.KeyMap qualified as KM (filterWithKey, insert, lookup, member, singleton)
18import Data.Foldable.WithIndex (iforM_) 18import Data.Foldable.WithIndex (FoldableWithIndex (..), iforM_)
19import Data.Map.Strict qualified as M (insert, lookup) 19import Data.Map.Strict qualified as M (insert, lookup)
20import Data.Vector qualified as V (singleton, snoc, uniq) 20import 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
304mergeNodeMaps :: NodeMap -> NodeMap
305mergeNodeMaps = 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