{-# LANGUAGE GADTs, TemplateHaskell, RankNTypes #-}
-- | Define types and functions related to creating a system information database.

-- Changes to System should be reflected in the 'Creating Your Project in
-- Drasil' tutorial found on the wiki:
-- https://github.com/JacquesCarette/Drasil/wiki/Creating-Your-Project-in-Drasil
module Drasil.System.OldSystem (
  -- * System
  -- ** Types
  System(..), SystemKind(..),
  Purpose, Background, Scope, Motivation,
  -- ** Lenses
  HasSystem(..), HasSystemMeta(..),
  -- ** Functions
  whatsTheBigIdea, mkSystem,
  -- ** Hacks
  refbyLookup, traceLookup
) where

import Control.Lens (makeClassy, (^.))
import Data.Char (isSpace)
import qualified Data.Map.Strict as M
import Data.Maybe (fromMaybe)

import Drasil.Database (UID, HasUID(..), ChunkDB)
import Language.Drasil (Quantity, MayHaveUnit, Concept,
  Reference, People, IdeaDict, CI, Constrained, ConstQDef, nw, abrv)
import Theory.Drasil (TheoryModel, GenDefn, DataDefinition, InstanceModel)
import Drasil.Metadata.SupportedSoftware (runnableSoftware, website)
import Drasil.Metadata.Documentation (srs, notebook)
import Utils.Drasil (toPlainName)

import Drasil.System.Core

-- | Enumeration of /kinds/ of 'System's we can encode.
data SystemKind =
    Specification
  | RunnableSoftware
  | Notebook
  | Website

-- | Data structure for holding all of the requisite information about a system
-- to be used in artifact generation.
data System where
 SI :: (Quantity h, MayHaveUnit h, Concept h,
  Quantity i, MayHaveUnit i, Concept i,
  HasUID j, Constrained j) =>
  { System -> SystemMeta
_meta         :: SystemMeta
  , System -> SystemKind
_kind         :: SystemKind
  , System -> String
_programName  :: String
  , System -> [TheoryModel]
_theoryModels :: [TheoryModel]
  , System -> [GenDefn]
_genDefns     :: [GenDefn]
  , System -> [DataDefinition]
_dataDefns    :: [DataDefinition]
  , System -> [InstanceModel]
_instModels   :: [InstanceModel]
  , ()
_inputs       :: [h]
  , ()
_outputs      :: [i]
  , ()
_constraints  :: [j]
  , System -> [ConstQDef]
_constants    :: [ConstQDef]
  -- FIXME: Hacks to be removed once 'Reference's are rebuilt.
  , System -> Map UID Reference
_refTable     :: M.Map UID Reference
  , System -> Map UID [UID]
_refbyTable   :: M.Map UID [UID]
  , System -> Map UID [UID]
_traceTable   :: M.Map UID [UID]
  } -> System

makeClassy ''System

instance HasSystemMeta System where
  systemMeta :: Lens' System SystemMeta
systemMeta = (SystemMeta -> f SystemMeta) -> System -> f System
forall c. HasSystem c => Lens' c SystemMeta
Lens' System SystemMeta
meta

-- | Probe what kind of 'System' one is. For example, does it represent a
-- (Problem) specification, runnable software, a 'notebook', or a website?
whatsTheBigIdea :: System -> IdeaDict
whatsTheBigIdea :: System -> IdeaDict
whatsTheBigIdea = SystemKind -> IdeaDict
whatKind' (SystemKind -> IdeaDict)
-> (System -> SystemKind) -> System -> IdeaDict
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (System -> Getting SystemKind System SystemKind -> SystemKind
forall s a. s -> Getting a s a -> a
^. Getting SystemKind System SystemKind
forall c. HasSystem c => Lens' c SystemKind
Lens' System SystemKind
kind)
  where
    whatKind' :: SystemKind -> IdeaDict
    whatKind' :: SystemKind -> IdeaDict
whatKind' SystemKind
Specification = CI -> IdeaDict
forall c. Idea c => c -> IdeaDict
nw CI
srs
    whatKind' SystemKind
RunnableSoftware = IdeaDict
runnableSoftware
    whatKind' SystemKind
Notebook = CI -> IdeaDict
forall c. Idea c => c -> IdeaDict
nw CI
notebook
    whatKind' SystemKind
Website = IdeaDict
website

-- | Build a 'System'.
mkSystem :: (Quantity h, MayHaveUnit h, Concept h,
  Quantity i, MayHaveUnit i, Concept i,
  HasUID j, Constrained j) =>
  CI -> SystemKind -> People -> Purpose -> Background -> Scope -> Motivation ->
    [TheoryModel] -> [GenDefn] -> [DataDefinition] -> [InstanceModel] ->
    [h] -> [i] -> [j] -> [ConstQDef] -> ChunkDB -> [Reference] ->
    System
mkSystem :: forall h i j.
(Quantity h, MayHaveUnit h, Concept h, Quantity i, MayHaveUnit i,
 Concept i, HasUID j, Constrained j) =>
CI
-> SystemKind
-> People
-> Background
-> Background
-> Background
-> Background
-> [TheoryModel]
-> [GenDefn]
-> [DataDefinition]
-> [InstanceModel]
-> [h]
-> [i]
-> [j]
-> [ConstQDef]
-> ChunkDB
-> [Reference]
-> System
mkSystem CI
nm SystemKind
sk People
ppl Background
prps Background
bkgrd Background
scp Background
motive [TheoryModel]
tms [GenDefn]
gds [DataDefinition]
dds [InstanceModel]
ims [h]
hs [i]
is [j]
js [ConstQDef]
cqds ChunkDB
db [Reference]
refs
  = SystemMeta
-> SystemKind
-> String
-> [TheoryModel]
-> [GenDefn]
-> [DataDefinition]
-> [InstanceModel]
-> [h]
-> [i]
-> [j]
-> [ConstQDef]
-> Map UID Reference
-> Map UID [UID]
-> Map UID [UID]
-> System
forall h i j.
(Quantity h, MayHaveUnit h, Concept h, Quantity i, MayHaveUnit i,
 Concept i, HasUID j, Constrained j) =>
SystemMeta
-> SystemKind
-> String
-> [TheoryModel]
-> [GenDefn]
-> [DataDefinition]
-> [InstanceModel]
-> [h]
-> [i]
-> [j]
-> [ConstQDef]
-> Map UID Reference
-> Map UID [UID]
-> Map UID [UID]
-> System
SI (CI
-> People
-> Background
-> Background
-> Background
-> Background
-> ChunkDB
-> SystemMeta
SystemMeta CI
nm People
ppl Background
prps Background
bkgrd Background
scp Background
motive ChunkDB
db) SystemKind
sk String
progName [TheoryModel]
tms [GenDefn]
gds [DataDefinition]
dds [InstanceModel]
ims [h]
hs [i]
is [j]
js
      [ConstQDef]
cqds Map UID Reference
refsMap Map UID [UID]
forall a. Monoid a => a
mempty Map UID [UID]
forall a. Monoid a => a
mempty
  where
    refsMap :: Map UID Reference
refsMap = [(UID, Reference)] -> Map UID Reference
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(UID, Reference)] -> Map UID Reference)
-> [(UID, Reference)] -> Map UID Reference
forall a b. (a -> b) -> a -> b
$ (Reference -> (UID, Reference))
-> [Reference] -> [(UID, Reference)]
forall a b. (a -> b) -> [a] -> [b]
map (\Reference
x -> (Reference
x Reference -> Getting UID Reference UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID Reference UID
forall c. HasUID c => Getter c UID
Getter Reference UID
uid, Reference
x)) [Reference]
refs
    progName :: String
progName = String -> String
toPlainName (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
isSpace) (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ CI -> String
forall c. CommonIdea c => c -> String
abrv CI
nm

-- | Find what chunks reference a specific chunk.
refbyLookup :: UID -> System -> [UID]
refbyLookup :: UID -> System -> [UID]
refbyLookup UID
u = [UID] -> Maybe [UID] -> [UID]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [UID] -> [UID])
-> (System -> Maybe [UID]) -> System -> [UID]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UID -> Map UID [UID] -> Maybe [UID]
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup UID
u (Map UID [UID] -> Maybe [UID])
-> (System -> Map UID [UID]) -> System -> Maybe [UID]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (System
-> Getting (Map UID [UID]) System (Map UID [UID]) -> Map UID [UID]
forall s a. s -> Getting a s a -> a
^. Getting (Map UID [UID]) System (Map UID [UID])
forall c. HasSystem c => Lens' c (Map UID [UID])
Lens' System (Map UID [UID])
refbyTable)

-- | Find what chunks a specific one references.
traceLookup :: UID -> System -> [UID]
traceLookup :: UID -> System -> [UID]
traceLookup UID
u = [UID] -> Maybe [UID] -> [UID]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [UID] -> [UID])
-> (System -> Maybe [UID]) -> System -> [UID]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UID -> Map UID [UID] -> Maybe [UID]
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup UID
u (Map UID [UID] -> Maybe [UID])
-> (System -> Map UID [UID]) -> System -> Maybe [UID]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (System
-> Getting (Map UID [UID]) System (Map UID [UID]) -> Map UID [UID]
forall s a. s -> Getting a s a -> a
^. Getting (Map UID [UID]) System (Map UID [UID])
forall c. HasSystem c => Lens' c (Map UID [UID])
Lens' System (Map UID [UID])
traceTable)