-- | Utilities to get grab certain chunks (from 'Expr', 'Sentence', etc) by
-- 'UID' and dereference the chunk it refers to.
module Drasil.SRS.GetChunks (
  resolveAllVars, vars
) where

import qualified Data.Set as Set

import Language.Drasil
import Language.Drasil.Development (sdep)
import Language.Drasil.ModelExpr.Development (meDep)
import Drasil.Database (ChunkDB, findOrErr)

-- | Extract and resolve all referenced 'DefinedQuantityDict's in a 'ModelExpr'.
vars :: ModelExpr -> ChunkDB -> [DefinedQuantityDict]
vars :: ModelExpr -> ChunkDB -> [DefinedQuantityDict]
vars ModelExpr
e ChunkDB
m = (UID -> DefinedQuantityDict) -> [UID] -> [DefinedQuantityDict]
forall a b. (a -> b) -> [a] -> [b]
map (UID -> ChunkDB -> DefinedQuantityDict
forall a. Typeable a => UID -> ChunkDB -> a
`findOrErr` ChunkDB
m) ([UID] -> [DefinedQuantityDict]) -> [UID] -> [DefinedQuantityDict]
forall a b. (a -> b) -> a -> b
$ ModelExpr -> [UID]
meDep ModelExpr
e

-- | Extract and resolve all referenced 'DefinedQuantityDict's in a 'Sentence'.
vars' :: Sentence -> ChunkDB -> [DefinedQuantityDict]
vars' :: Sentence -> ChunkDB -> [DefinedQuantityDict]
vars' Sentence
a ChunkDB
m = (UID -> DefinedQuantityDict) -> [UID] -> [DefinedQuantityDict]
forall a b. (a -> b) -> [a] -> [b]
map (UID -> ChunkDB -> DefinedQuantityDict
forall a. Typeable a => UID -> ChunkDB -> a
`findOrErr` ChunkDB
m) ([UID] -> [DefinedQuantityDict]) -> [UID] -> [DefinedQuantityDict]
forall a b. (a -> b) -> a -> b
$ Set UID -> [UID]
forall a. Set a -> [a]
Set.toList (Sentence -> Set UID
sdep Sentence
a)

-- | Extract and resolve all references to 'DefinedQuantityDict's in a list of
-- 'Sentence's and a list of 'ModelExpr's.
resolveAllVars :: [Sentence] -> [ModelExpr] -> ChunkDB -> [DefinedQuantityDict]
resolveAllVars :: [Sentence] -> [ModelExpr] -> ChunkDB -> [DefinedQuantityDict]
resolveAllVars [Sentence]
s [ModelExpr]
e ChunkDB
c = (Sentence -> [DefinedQuantityDict])
-> [Sentence] -> [DefinedQuantityDict]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Sentence -> ChunkDB -> [DefinedQuantityDict]
`vars'` ChunkDB
c) [Sentence]
s [DefinedQuantityDict]
-> [DefinedQuantityDict] -> [DefinedQuantityDict]
forall a. [a] -> [a] -> [a]
++ (ModelExpr -> [DefinedQuantityDict])
-> [ModelExpr] -> [DefinedQuantityDict]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (ModelExpr -> ChunkDB -> [DefinedQuantityDict]
`vars` ChunkDB
c) [ModelExpr]
e