{-# LANGUAGE PostfixOperators #-}
-- | Defines functions used to create the Traceability Matrices and Graphs section.
module Drasil.Sections.TraceabilityMandGs (
  -- * Main Functions
  generateTraceTable, traceMatAssumpAssump, traceMatAssumpOther,
  traceMatRefinement, traceMatOtherReq, traceMatStandard,
  -- * Helpers
  tvAssumps, tvDataDefns, tvGenDefns, tvTheoryModels,
  tvInsModels, tvGoals, tvReqs, tvChanges
  ) where

import Drasil.DocumentLanguage.Core (TraceConfig(TraceConfig))
import Drasil.DocumentLanguage.TraceabilityMatrix (generateTraceTableView,
  traceMReferrers, traceView, traceViewCC, TraceViewCat)

import Data.Drasil.Concepts.Documentation (assumption, assumpDom, chgProbDom,
  goalStmt, goalStmtDom, requirement, reqDom, item, section_, likelyChg,
  unlikelyChg)
import qualified Data.Drasil.TheoryConcepts as Doc (genDefn, dataDefn, inModel, thModel)
import Database.Drasil
import SysInfo.Drasil
import Language.Drasil
import Language.Drasil.Chunk.Concept.NamedCombinators as NC
import Language.Drasil.Sentence.Combinators as S

-- | Makes a Traceability Table/Matrix that contains Items of Different Sections.
generateTraceTable :: SystemInformation -> LabelledContent
generateTraceTable :: SystemInformation -> LabelledContent
generateTraceTable = UID
-> Sentence
-> [TraceViewCat]
-> [TraceViewCat]
-> SystemInformation
-> LabelledContent
generateTraceTableView (String -> UID
mkUid String
"Tracey")
  (forall n. NamedIdea n => n -> Sentence
titleize' IdeaDict
item Sentence -> Sentence -> Sentence
+:+ String -> Sentence
S String
"of Different" Sentence -> Sentence -> Sentence
+:+ forall n. NamedIdea n => n -> Sentence
titleize' IdeaDict
section_) [TraceViewCat
tvEverything] [TraceViewCat
tvEverything]

-- | Traceability viewing everything. Takes a 'UID' and a 'ChunkDB'. Returns a list of 'UID's.
tvEverything :: TraceViewCat
tvEverything :: TraceViewCat
tvEverything = forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall a b. a -> b -> a
const forall a. a -> a
id)

-- | Traceability viewing assumptions. Takes a 'UID' and a 'ChunkDB'. Returns a list of 'UID's.
tvAssumps :: TraceViewCat
tvAssumps :: TraceViewCat
tvAssumps = forall c. Concept c => c -> TraceViewCat
traceViewCC ConceptChunk
assumpDom

-- | Traceability viewing data definitions. Takes a 'UID' and a 'ChunkDB'. Returns a list of 'UID's.
tvDataDefns :: TraceViewCat
tvDataDefns :: TraceViewCat
tvDataDefns = forall a.
HasUID a =>
Getting (UMap a) ChunkDB (UMap a) -> TraceViewCat
traceView Lens' ChunkDB (UMap DataDefinition)
dataDefnTable

-- | Traceability viewing general definitions. Takes a 'UID' and a 'ChunkDB'. Returns a list of 'UID's.
tvGenDefns :: TraceViewCat
tvGenDefns :: TraceViewCat
tvGenDefns = forall a.
HasUID a =>
Getting (UMap a) ChunkDB (UMap a) -> TraceViewCat
traceView Lens' ChunkDB (UMap GenDefn)
gendefTable

-- | Traceability viewing theory models. Takes a 'UID' and a 'ChunkDB'. Returns a list of 'UID's.
tvTheoryModels :: TraceViewCat
tvTheoryModels :: TraceViewCat
tvTheoryModels = forall a.
HasUID a =>
Getting (UMap a) ChunkDB (UMap a) -> TraceViewCat
traceView Lens' ChunkDB (UMap TheoryModel)
theoryModelTable

-- | Traceability viewing instance models. Takes a 'UID' and a 'ChunkDB'. Returns a list of 'UID's.
tvInsModels :: TraceViewCat
tvInsModels :: TraceViewCat
tvInsModels = forall a.
HasUID a =>
Getting (UMap a) ChunkDB (UMap a) -> TraceViewCat
traceView Lens' ChunkDB (UMap InstanceModel)
insmodelTable

-- | Traceability viewing goals. Takes a 'UID' and a 'ChunkDB'. Returns a list of 'UID's.
tvGoals :: TraceViewCat
tvGoals :: TraceViewCat
tvGoals = forall c. Concept c => c -> TraceViewCat
traceViewCC ConceptChunk
goalStmtDom

-- | Traceability viewing requirements. Takes a 'UID' and a 'ChunkDB'. Returns a list of 'UID's.
tvReqs :: TraceViewCat
tvReqs :: TraceViewCat
tvReqs = forall c. Concept c => c -> TraceViewCat
traceViewCC ConceptChunk
reqDom

-- | Traceability viewing changes. Takes a 'UID' and a 'ChunkDB'. Returns a list of 'UID's.
tvChanges :: TraceViewCat
tvChanges :: TraceViewCat
tvChanges = forall c. Concept c => c -> TraceViewCat
traceViewCC ConceptChunk
chgProbDom

-- | Assumptions on the assumptions of a traceability matrix.
traceMatAssumpAssump :: TraceConfig
traceMatAssumpAssump :: TraceConfig
traceMatAssumpAssump = UID
-> [Sentence]
-> Sentence
-> [TraceViewCat]
-> [TraceViewCat]
-> TraceConfig
TraceConfig (String -> UID
mkUid String
"TraceMatAvsA") [forall n. NamedIdea n => n -> Sentence
plural CI
assumption 
  Sentence -> Sentence -> Sentence
+:+ String -> Sentence
S String
"on each other"] (forall n. NamedIdea n => n -> Sentence
titleize' CI
assumption Sentence -> Sentence -> Sentence
+:+
  String -> Sentence
S String
"and Other" Sentence -> Sentence -> Sentence
+:+ forall n. NamedIdea n => n -> Sentence
titleize' CI
assumption ) [TraceViewCat
tvAssumps] [TraceViewCat
tvAssumps]

-- | Other assumptions of the traceability matrix
traceMatAssumpOther :: TraceConfig
traceMatAssumpOther :: TraceConfig
traceMatAssumpOther = UID
-> [Sentence]
-> Sentence
-> [TraceViewCat]
-> [TraceViewCat]
-> TraceConfig
TraceConfig (String -> UID
mkUid String
"TraceMatAvsAll") [forall n. NamedIdea n => n -> Sentence
plural CI
Doc.dataDefn,
  forall n. NamedIdea n => n -> Sentence
plural CI
Doc.thModel, forall n. NamedIdea n => n -> Sentence
plural CI
Doc.genDefn, forall n. NamedIdea n => n -> Sentence
plural CI
Doc.inModel, forall n. NamedIdea n => n -> Sentence
plural CI
requirement,
  forall n. NamedIdea n => n -> Sentence
plural CI
likelyChg, forall n. NounPhrase n => n -> Sentence
pluralNP (CI
unlikelyChg forall c d. (NamedIdea c, NamedIdea d) => c -> d -> NP
`NC.onThePP` CI
assumption)]
  (forall n. NamedIdea n => n -> Sentence
titleize' CI
assumption Sentence -> Sentence -> Sentence
+:+ String -> Sentence
S String
"and Other" Sentence -> Sentence -> Sentence
+:+ forall n. NamedIdea n => n -> Sentence
titleize' IdeaDict
item) [TraceViewCat
tvAssumps]
  [TraceViewCat
tvDataDefns, TraceViewCat
tvTheoryModels, TraceViewCat
tvGenDefns, TraceViewCat
tvInsModels, TraceViewCat
tvReqs, TraceViewCat
tvChanges]

-- | Refinement of the traceability matrix.
traceMatRefinement :: TraceConfig
traceMatRefinement :: TraceConfig
traceMatRefinement = UID
-> [Sentence]
-> Sentence
-> [TraceViewCat]
-> [TraceViewCat]
-> TraceConfig
TraceConfig (String -> UID
mkUid String
"TraceMatRefvsRef") [forall n. NamedIdea n => n -> Sentence
plural CI
Doc.dataDefn,
  forall n. NamedIdea n => n -> Sentence
plural CI
Doc.thModel, forall n. NamedIdea n => n -> Sentence
plural CI
Doc.genDefn, forall n. NamedIdea n => n -> Sentence
plural CI
Doc.inModel Sentence -> Sentence -> Sentence
+:+
  String -> Sentence
S String
"on each other"] (forall n. NamedIdea n => n -> Sentence
titleize' IdeaDict
item Sentence -> Sentence -> Sentence
+:+ String -> Sentence
S String
"and Other" Sentence -> Sentence -> Sentence
+:+ forall n. NamedIdea n => n -> Sentence
titleize' IdeaDict
section_)
  [TraceViewCat
tvDataDefns, TraceViewCat
tvTheoryModels, TraceViewCat
tvGenDefns, TraceViewCat
tvInsModels]
  [TraceViewCat
tvDataDefns, TraceViewCat
tvTheoryModels, TraceViewCat
tvGenDefns, TraceViewCat
tvInsModels]

-- | Records other requirements. Converts the 'SystemInformation' into a 'TraceConfig'.
traceMatOtherReq :: SystemInformation -> TraceConfig
traceMatOtherReq :: SystemInformation -> TraceConfig
traceMatOtherReq SystemInformation
si = UID
-> [Sentence]
-> Sentence
-> [TraceViewCat]
-> [TraceViewCat]
-> TraceConfig
TraceConfig (String -> UID
mkUid String
"TraceMatAllvsR") [forall n. NamedIdea n => n -> Sentence
plural CI
requirement
  Sentence -> Sentence -> Sentence
`S.and_` forall n. NounPhrase n => n -> Sentence
pluralNP (CI
goalStmt forall c d. (NamedIdea c, NamedIdea d) => c -> d -> NP
`NC.onThePP` CI
Doc.dataDefn), forall n. NamedIdea n => n -> Sentence
plural CI
Doc.thModel, 
  forall n. NamedIdea n => n -> Sentence
plural CI
Doc.genDefn, forall n. NamedIdea n => n -> Sentence
plural CI
Doc.inModel] ((CI -> Sentence) -> Sentence
x forall n. NamedIdea n => n -> Sentence
titleize' Sentence -> Sentence -> Sentence
+:+ String -> Sentence
S String
"and Other" Sentence -> Sentence -> Sentence
+:+ 
  forall n. NamedIdea n => n -> Sentence
titleize' IdeaDict
item) [TraceViewCat
tvDataDefns, TraceViewCat
tvTheoryModels, TraceViewCat
tvGenDefns, TraceViewCat
tvInsModels, TraceViewCat
tvReqs] 
  [TraceViewCat
tvGoals, TraceViewCat
tvReqs] where
    x :: (CI -> Sentence) -> Sentence
x CI -> Sentence
g = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Sentence
a (TraceViewCat
f,CI
t) -> Sentence
a Sentence -> Sentence -> Sentence
`sC'` case ([UID] -> [UID]) -> ChunkDB -> [UID]
traceMReferrers (forall a b c. (a -> b -> c) -> b -> a -> c
flip TraceViewCat
f forall a b. (a -> b) -> a -> b
$ SystemInformation -> ChunkDB
_sysinfodb SystemInformation
si) forall a b. (a -> b) -> a -> b
$
      SystemInformation -> ChunkDB
_sysinfodb SystemInformation
si of
      [] -> forall a. Monoid a => a
mempty
      [UID]
_ -> CI -> Sentence
g CI
t) forall a. Monoid a => a
mempty [(TraceViewCat
tvReqs, CI
requirement), (TraceViewCat
tvGoals, CI
goalStmt)]
    sC' :: Sentence -> Sentence -> Sentence
sC' Sentence
EmptyS Sentence
b = Sentence
b
    sC' Sentence
a Sentence
EmptyS = Sentence
a
    sC' Sentence
a Sentence
b = Sentence -> Sentence -> Sentence
sC Sentence
a Sentence
b

-- | Helpers to check if given argument has more than one peice of information


-- | Contains traceability matrix assumptions, other assumptions, refinement, and other requirements.
traceMatStandard :: SystemInformation -> [TraceConfig]
traceMatStandard :: SystemInformation -> [TraceConfig]
traceMatStandard SystemInformation
s = forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (a -> b) -> a -> b
$ SystemInformation
s) [forall a b. a -> b -> a
const TraceConfig
traceMatAssumpAssump, forall a b. a -> b -> a
const TraceConfig
traceMatAssumpOther, forall a b. a -> b -> a
const TraceConfig
traceMatRefinement,
  SystemInformation -> TraceConfig
traceMatOtherReq]