-- | Utilities to get grab certain chunks (from 'Expr', 'Sentence', etc) by 'UID' and
-- dereference the chunk it refers to.
module SysInfo.Drasil.GetChunk (ccss, ccss', combine, getIdeaDict, vars) where

import Language.Drasil
import Language.Drasil.Development
import Language.Drasil.ModelExpr.Development (meDep)

import Database.Drasil (ChunkDB, defResolve, symbResolve, termResolve)

import Data.List (nub)

-- | Gets a list of quantities ('QuantityDict') from an equation in order to print.
vars :: ModelExpr -> ChunkDB -> [QuantityDict]
vars :: ModelExpr -> ChunkDB -> [QuantityDict]
vars ModelExpr
e ChunkDB
m = forall a b. (a -> b) -> [a] -> [b]
map (ChunkDB -> UID -> QuantityDict
symbResolve ChunkDB
m) forall a b. (a -> b) -> a -> b
$ ModelExpr -> [UID]
meDep ModelExpr
e

-- | Gets a list of quantities ('QuantityDict') from a 'Sentence' in order to print.
vars' :: Sentence -> ChunkDB -> [QuantityDict]
vars' :: Sentence -> ChunkDB -> [QuantityDict]
vars' Sentence
a ChunkDB
m = forall a b. (a -> b) -> [a] -> [b]
map (ChunkDB -> UID -> QuantityDict
symbResolve ChunkDB
m) forall a b. (a -> b) -> a -> b
$ Sentence -> [UID]
sdep Sentence
a

-- | Combines the functions of 'vars' and 'concpt' to create a list of 'DefinedQuantityDict's from a 'Sentence'.
combine :: Sentence -> ChunkDB -> [DefinedQuantityDict]
combine :: Sentence -> ChunkDB -> [DefinedQuantityDict]
combine Sentence
a ChunkDB
m = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall c.
(Quantity c, MayHaveUnit c) =>
c -> ConceptChunk -> DefinedQuantityDict
dqdQd (Sentence -> ChunkDB -> [QuantityDict]
vars' Sentence
a ChunkDB
m) (Sentence -> ChunkDB -> [ConceptChunk]
concpt Sentence
a ChunkDB
m)

-- | Combines the functions of 'vars' and 'concpt' to create a list of 'DefinedQuantityDict's from an equation.
combine' :: ModelExpr -> ChunkDB -> [DefinedQuantityDict]
combine' :: ModelExpr -> ChunkDB -> [DefinedQuantityDict]
combine' ModelExpr
a ChunkDB
m = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall c.
(Quantity c, MayHaveUnit c) =>
c -> ConceptChunk -> DefinedQuantityDict
dqdQd (ModelExpr -> ChunkDB -> [QuantityDict]
vars ModelExpr
a ChunkDB
m) (ModelExpr -> ChunkDB -> [ConceptChunk]
concpt' ModelExpr
a ChunkDB
m)

-- | Gets a list of defined quantities ('DefinedQuantityDict's) from 'Sentence's and expressions that are contained in the database ('ChunkDB').
ccss :: [Sentence] -> [ModelExpr] -> ChunkDB -> [DefinedQuantityDict]
ccss :: [Sentence] -> [ModelExpr] -> ChunkDB -> [DefinedQuantityDict]
ccss [Sentence]
s [ModelExpr]
e ChunkDB
c = forall a. Eq a => [a] -> [a]
nub forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Sentence -> ChunkDB -> [DefinedQuantityDict]
`combine` ChunkDB
c) [Sentence]
s forall a. [a] -> [a] -> [a]
++ forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (ModelExpr -> ChunkDB -> [DefinedQuantityDict]
`combine'` ChunkDB
c) [ModelExpr]
e

-- | Gets a list of quantities ('QuantityDict's) from 'Sentence's and expressions that are contained in the database ('ChunkDB').
ccss' :: [Sentence] -> [ModelExpr] -> ChunkDB -> [QuantityDict]
ccss' :: [Sentence] -> [ModelExpr] -> ChunkDB -> [QuantityDict]
ccss' [Sentence]
s [ModelExpr]
e ChunkDB
c = forall a. Eq a => [a] -> [a]
nub forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Sentence -> ChunkDB -> [QuantityDict]
`vars'` ChunkDB
c) [Sentence]
s forall a. [a] -> [a] -> [a]
++ forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (ModelExpr -> ChunkDB -> [QuantityDict]
`vars` ChunkDB
c) [ModelExpr]
e

-- | Gets a list of concepts ('ConceptChunk') from a 'Sentence' in order to print.
concpt :: Sentence -> ChunkDB -> [ConceptChunk]
concpt :: Sentence -> ChunkDB -> [ConceptChunk]
concpt Sentence
a ChunkDB
m = forall a b. (a -> b) -> [a] -> [b]
map (ChunkDB -> UID -> ConceptChunk
defResolve ChunkDB
m) forall a b. (a -> b) -> a -> b
$ Sentence -> [UID]
sdep Sentence
a

-- | Gets a list of concepts ('ConceptChunk') from an expression in order to print.
concpt' :: ModelExpr -> ChunkDB -> [ConceptChunk]
concpt' :: ModelExpr -> ChunkDB -> [ConceptChunk]
concpt' ModelExpr
a ChunkDB
m = forall a b. (a -> b) -> [a] -> [b]
map (ChunkDB -> UID -> ConceptChunk
defResolve ChunkDB
m) forall a b. (a -> b) -> a -> b
$ ModelExpr -> [UID]
meDep ModelExpr
a

-- | Gets a list of ideas ('IdeaDict') from a 'Sentence' in order to print.
getIdeaDict :: Sentence -> ChunkDB -> [IdeaDict]
getIdeaDict :: Sentence -> ChunkDB -> [IdeaDict]
getIdeaDict Sentence
a ChunkDB
m = forall a b. (a -> b) -> [a] -> [b]
map (ChunkDB -> UID -> IdeaDict
termResolve ChunkDB
m) forall a b. (a -> b) -> a -> b
$ Sentence -> [UID]
shortdep Sentence
a