{-# LANGUAGE RankNTypes, NoMonomorphismRestriction, GADTs, TemplateHaskell #-}
module Theory.Drasil.DataDefinition where
import Control.Lens
import Language.Drasil
import Language.Drasil.Development (showUID)
import Data.Drasil.TheoryConcepts (dataDefn)
newtype Scope = Scp { Scope -> UID
_spec :: UID }
data ScopeType =
Local Scope
| Global
data DDPkt = DDPkt {
DDPkt -> ScopeType
_pktST :: ScopeType,
DDPkt -> [DecRef]
_pktDR :: [DecRef],
DDPkt -> Maybe Derivation
_pktMD :: Maybe Derivation,
DDPkt -> ShortName
_pktSN :: ShortName,
DDPkt -> String
_pktS :: String,
DDPkt -> [Sentence]
_pktSS :: [Sentence]
}
makeLenses ''DDPkt
data DataDefinition where
DDE :: SimpleQDef -> DDPkt -> DataDefinition
DDME :: ModelQDef -> DDPkt -> DataDefinition
ddQD :: Lens' SimpleQDef a -> Lens' ModelQDef a -> Lens' DataDefinition a
ddQD :: Lens' SimpleQDef a -> Lens' ModelQDef a -> Lens' DataDefinition a
ddQD lqde :: Lens' SimpleQDef a
lqde lqdme :: Lens' ModelQDef a
lqdme = (DataDefinition -> a)
-> (DataDefinition -> a -> DataDefinition)
-> Lens' DataDefinition a
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens DataDefinition -> a
g DataDefinition -> a -> DataDefinition
s
where
g :: DataDefinition -> a
g (DDE qd :: SimpleQDef
qd _) = SimpleQDef
qd SimpleQDef -> Getting a SimpleQDef a -> a
forall s a. s -> Getting a s a -> a
^. Getting a SimpleQDef a
Lens' SimpleQDef a
lqde
g (DDME qd :: ModelQDef
qd _) = ModelQDef
qd ModelQDef -> Getting a ModelQDef a -> a
forall s a. s -> Getting a s a -> a
^. Getting a ModelQDef a
Lens' ModelQDef a
lqdme
s :: DataDefinition -> a -> DataDefinition
s (DDE qd :: SimpleQDef
qd pkt :: DDPkt
pkt) u :: a
u = SimpleQDef -> DDPkt -> DataDefinition
DDE (SimpleQDef
qd SimpleQDef -> (SimpleQDef -> SimpleQDef) -> SimpleQDef
forall a b. a -> (a -> b) -> b
& (a -> Identity a) -> SimpleQDef -> Identity SimpleQDef
Lens' SimpleQDef a
lqde ((a -> Identity a) -> SimpleQDef -> Identity SimpleQDef)
-> a -> SimpleQDef -> SimpleQDef
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a
u) DDPkt
pkt
s (DDME qd :: ModelQDef
qd pkt :: DDPkt
pkt) u :: a
u = ModelQDef -> DDPkt -> DataDefinition
DDME (ModelQDef
qd ModelQDef -> (ModelQDef -> ModelQDef) -> ModelQDef
forall a b. a -> (a -> b) -> b
& (a -> Identity a) -> ModelQDef -> Identity ModelQDef
Lens' ModelQDef a
lqdme ((a -> Identity a) -> ModelQDef -> Identity ModelQDef)
-> a -> ModelQDef -> ModelQDef
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a
u) DDPkt
pkt
ddPkt :: Lens' DDPkt a -> Lens' DataDefinition a
ddPkt :: Lens' DDPkt a -> Lens' DataDefinition a
ddPkt lpkt :: Lens' DDPkt a
lpkt = (DataDefinition -> a)
-> (DataDefinition -> a -> DataDefinition)
-> Lens' DataDefinition a
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens DataDefinition -> a
g DataDefinition -> a -> DataDefinition
s
where
g :: DataDefinition -> a
g (DDE _ pkt :: DDPkt
pkt) = DDPkt
pkt DDPkt -> Getting a DDPkt a -> a
forall s a. s -> Getting a s a -> a
^. Getting a DDPkt a
Lens' DDPkt a
lpkt
g (DDME _ pkt :: DDPkt
pkt) = DDPkt
pkt DDPkt -> Getting a DDPkt a -> a
forall s a. s -> Getting a s a -> a
^. Getting a DDPkt a
Lens' DDPkt a
lpkt
s :: DataDefinition -> a -> DataDefinition
s (DDE qd :: SimpleQDef
qd pkt :: DDPkt
pkt) a' :: a
a' = SimpleQDef -> DDPkt -> DataDefinition
DDE SimpleQDef
qd (DDPkt
pkt DDPkt -> (DDPkt -> DDPkt) -> DDPkt
forall a b. a -> (a -> b) -> b
& (a -> Identity a) -> DDPkt -> Identity DDPkt
Lens' DDPkt a
lpkt ((a -> Identity a) -> DDPkt -> Identity DDPkt)
-> a -> DDPkt -> DDPkt
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a
a')
s (DDME qd :: ModelQDef
qd pkt :: DDPkt
pkt) a' :: a
a' = ModelQDef -> DDPkt -> DataDefinition
DDME ModelQDef
qd (DDPkt
pkt DDPkt -> (DDPkt -> DDPkt) -> DDPkt
forall a b. a -> (a -> b) -> b
& (a -> Identity a) -> DDPkt -> Identity DDPkt
Lens' DDPkt a
lpkt ((a -> Identity a) -> DDPkt -> Identity DDPkt)
-> a -> DDPkt -> DDPkt
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a
a')
instance HasUID DataDefinition where uid :: (UID -> f UID) -> DataDefinition -> f DataDefinition
uid = Lens' SimpleQDef UID
-> Lens' ModelQDef UID -> Lens' DataDefinition UID
forall a.
Lens' SimpleQDef a -> Lens' ModelQDef a -> Lens' DataDefinition a
ddQD forall c. HasUID c => Lens' c UID
Lens' SimpleQDef UID
uid forall c. HasUID c => Lens' c UID
Lens' ModelQDef UID
uid
instance NamedIdea DataDefinition where term :: (NP -> f NP) -> DataDefinition -> f DataDefinition
term = Lens' SimpleQDef NP
-> Lens' ModelQDef NP -> Lens' DataDefinition NP
forall a.
Lens' SimpleQDef a -> Lens' ModelQDef a -> Lens' DataDefinition a
ddQD forall c. NamedIdea c => Lens' c NP
Lens' SimpleQDef NP
term forall c. NamedIdea c => Lens' c NP
Lens' ModelQDef NP
term
instance Idea DataDefinition where getA :: DataDefinition -> Maybe String
getA = (SimpleQDef -> Maybe String)
-> (ModelQDef -> Maybe String)
-> Either SimpleQDef ModelQDef
-> Maybe String
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either SimpleQDef -> Maybe String
forall c. Idea c => c -> Maybe String
getA ModelQDef -> Maybe String
forall c. Idea c => c -> Maybe String
getA (Either SimpleQDef ModelQDef -> Maybe String)
-> (DataDefinition -> Either SimpleQDef ModelQDef)
-> DataDefinition
-> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DataDefinition -> Either SimpleQDef ModelQDef
qdFromDD
instance HasSpace DataDefinition where typ :: (Space -> f Space) -> DataDefinition -> f DataDefinition
typ = Lens' SimpleQDef Space
-> Lens' ModelQDef Space -> Lens' DataDefinition Space
forall a.
Lens' SimpleQDef a -> Lens' ModelQDef a -> Lens' DataDefinition a
ddQD forall c. HasSpace c => Lens' c Space
Lens' SimpleQDef Space
typ forall c. HasSpace c => Lens' c Space
Lens' ModelQDef Space
typ
instance HasSymbol DataDefinition where symbol :: DataDefinition -> Stage -> Symbol
symbol = (SimpleQDef -> Stage -> Symbol)
-> (ModelQDef -> Stage -> Symbol)
-> Either SimpleQDef ModelQDef
-> Stage
-> Symbol
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either SimpleQDef -> Stage -> Symbol
forall c. HasSymbol c => c -> Stage -> Symbol
symbol ModelQDef -> Stage -> Symbol
forall c. HasSymbol c => c -> Stage -> Symbol
symbol (Either SimpleQDef ModelQDef -> Stage -> Symbol)
-> (DataDefinition -> Either SimpleQDef ModelQDef)
-> DataDefinition
-> Stage
-> Symbol
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DataDefinition -> Either SimpleQDef ModelQDef
qdFromDD
instance Quantity DataDefinition where
instance Express DataDefinition where
express :: DataDefinition -> ModelExpr
express d :: DataDefinition
d = ModelExpr -> ModelExpr -> ModelExpr
forall r. ModelExprC r => r -> r -> r
defines (DataDefinition -> ModelExpr
forall r c. (ExprC r, HasUID c, HasSymbol c) => c -> r
sy DataDefinition
d) ((SimpleQDef -> ModelExpr)
-> (ModelQDef -> ModelExpr)
-> Either SimpleQDef ModelQDef
-> ModelExpr
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Expr -> ModelExpr
forall c. Express c => c -> ModelExpr
express (Expr -> ModelExpr)
-> (SimpleQDef -> Expr) -> SimpleQDef -> ModelExpr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SimpleQDef -> Getting Expr SimpleQDef Expr -> Expr
forall s a. s -> Getting a s a -> a
^. Getting Expr SimpleQDef Expr
forall (c :: * -> *) e. DefiningExpr c => Lens' (c e) e
defnExpr)) (ModelQDef -> Getting ModelExpr ModelQDef ModelExpr -> ModelExpr
forall s a. s -> Getting a s a -> a
^. Getting ModelExpr ModelQDef ModelExpr
forall (c :: * -> *) e. DefiningExpr c => Lens' (c e) e
defnExpr) (DataDefinition -> Either SimpleQDef ModelQDef
qdFromDD DataDefinition
d))
instance HasDecRef DataDefinition where getDecRefs :: ([DecRef] -> f [DecRef]) -> DataDefinition -> f DataDefinition
getDecRefs = Lens' DDPkt [DecRef] -> Lens' DataDefinition [DecRef]
forall a. Lens' DDPkt a -> Lens' DataDefinition a
ddPkt Lens' DDPkt [DecRef]
pktDR
instance Eq DataDefinition where a :: DataDefinition
a == :: DataDefinition -> DataDefinition -> Bool
== b :: DataDefinition
b = (DataDefinition
a DataDefinition -> Getting UID DataDefinition UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID DataDefinition UID
forall c. HasUID c => Lens' c UID
uid) UID -> UID -> Bool
forall a. Eq a => a -> a -> Bool
== (DataDefinition
b DataDefinition -> Getting UID DataDefinition UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID DataDefinition UID
forall c. HasUID c => Lens' c UID
uid)
instance HasDerivation DataDefinition where derivations :: (Maybe Derivation -> f (Maybe Derivation))
-> DataDefinition -> f DataDefinition
derivations = Lens' DDPkt (Maybe Derivation)
-> Lens' DataDefinition (Maybe Derivation)
forall a. Lens' DDPkt a -> Lens' DataDefinition a
ddPkt Lens' DDPkt (Maybe Derivation)
pktMD
instance HasAdditionalNotes DataDefinition where getNotes :: ([Sentence] -> f [Sentence]) -> DataDefinition -> f DataDefinition
getNotes = Lens' DDPkt [Sentence] -> Lens' DataDefinition [Sentence]
forall a. Lens' DDPkt a -> Lens' DataDefinition a
ddPkt Lens' DDPkt [Sentence]
pktSS
instance MayHaveUnit DataDefinition where getUnit :: DataDefinition -> Maybe UnitDefn
getUnit = (SimpleQDef -> Maybe UnitDefn)
-> (ModelQDef -> Maybe UnitDefn)
-> Either SimpleQDef ModelQDef
-> Maybe UnitDefn
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either SimpleQDef -> Maybe UnitDefn
forall u. MayHaveUnit u => u -> Maybe UnitDefn
getUnit ModelQDef -> Maybe UnitDefn
forall u. MayHaveUnit u => u -> Maybe UnitDefn
getUnit (Either SimpleQDef ModelQDef -> Maybe UnitDefn)
-> (DataDefinition -> Either SimpleQDef ModelQDef)
-> DataDefinition
-> Maybe UnitDefn
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DataDefinition -> Either SimpleQDef ModelQDef
qdFromDD
instance HasShortName DataDefinition where shortname :: DataDefinition -> ShortName
shortname = (DataDefinition
-> Getting ShortName DataDefinition ShortName -> ShortName
forall s a. s -> Getting a s a -> a
^. Lens' DDPkt ShortName -> Lens' DataDefinition ShortName
forall a. Lens' DDPkt a -> Lens' DataDefinition a
ddPkt Lens' DDPkt ShortName
pktSN)
instance HasRefAddress DataDefinition where getRefAdd :: DataDefinition -> LblType
getRefAdd l :: DataDefinition
l = IRefProg -> String -> LblType
RP (String -> IRefProg
prepend (String -> IRefProg) -> String -> IRefProg
forall a b. (a -> b) -> a -> b
$ DataDefinition -> String
forall c. CommonIdea c => c -> String
abrv DataDefinition
l) (DataDefinition
l DataDefinition -> Getting String DataDefinition String -> String
forall s a. s -> Getting a s a -> a
^. Lens' DDPkt String -> Lens' DataDefinition String
forall a. Lens' DDPkt a -> Lens' DataDefinition a
ddPkt Lens' DDPkt String
pktS)
instance ConceptDomain DataDefinition where cdom :: DataDefinition -> [UID]
cdom _ = CI -> [UID]
forall c. ConceptDomain c => c -> [UID]
cdom CI
dataDefn
instance CommonIdea DataDefinition where abrv :: DataDefinition -> String
abrv _ = CI -> String
forall c. CommonIdea c => c -> String
abrv CI
dataDefn
instance Referable DataDefinition where
refAdd :: DataDefinition -> String
refAdd = (DataDefinition -> Getting String DataDefinition String -> String
forall s a. s -> Getting a s a -> a
^. Lens' DDPkt String -> Lens' DataDefinition String
forall a. Lens' DDPkt a -> Lens' DataDefinition a
ddPkt Lens' DDPkt String
pktS)
renderRef :: DataDefinition -> LblType
renderRef l :: DataDefinition
l = IRefProg -> String -> LblType
RP (String -> IRefProg
prepend (String -> IRefProg) -> String -> IRefProg
forall a b. (a -> b) -> a -> b
$ DataDefinition -> String
forall c. CommonIdea c => c -> String
abrv DataDefinition
l) (DataDefinition -> String
forall s. Referable s => s -> String
refAdd DataDefinition
l)
ddE :: SimpleQDef -> [DecRef] -> Maybe Derivation -> String -> [Sentence] -> DataDefinition
ddE :: SimpleQDef
-> [DecRef]
-> Maybe Derivation
-> String
-> [Sentence]
-> DataDefinition
ddE q :: SimpleQDef
q [] _ _ = String -> [Sentence] -> DataDefinition
forall a. HasCallStack => String -> a
error (String -> [Sentence] -> DataDefinition)
-> String -> [Sentence] -> DataDefinition
forall a b. (a -> b) -> a -> b
$ "Source field of " String -> String -> String
forall a. [a] -> [a] -> [a]
++ SimpleQDef -> String
forall a. HasUID a => a -> String
showUID SimpleQDef
q String -> String -> String
forall a. [a] -> [a] -> [a]
++ " is empty"
ddE q :: SimpleQDef
q refs :: [DecRef]
refs der :: Maybe Derivation
der sn :: String
sn = SimpleQDef -> DDPkt -> DataDefinition
DDE SimpleQDef
q (DDPkt -> DataDefinition)
-> ([Sentence] -> DDPkt) -> [Sentence] -> DataDefinition
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScopeType
-> [DecRef]
-> Maybe Derivation
-> ShortName
-> String
-> [Sentence]
-> DDPkt
DDPkt ScopeType
Global [DecRef]
refs Maybe Derivation
der (Sentence -> ShortName
shortname' (Sentence -> ShortName) -> Sentence -> ShortName
forall a b. (a -> b) -> a -> b
$ String -> Sentence
S String
sn) (CI -> String -> String
forall c. CommonIdea c => c -> String -> String
prependAbrv CI
dataDefn String
sn)
ddENoRefs :: SimpleQDef -> Maybe Derivation -> String -> [Sentence] -> DataDefinition
ddENoRefs :: SimpleQDef
-> Maybe Derivation -> String -> [Sentence] -> DataDefinition
ddENoRefs q :: SimpleQDef
q der :: Maybe Derivation
der sn :: String
sn = SimpleQDef -> DDPkt -> DataDefinition
DDE SimpleQDef
q (DDPkt -> DataDefinition)
-> ([Sentence] -> DDPkt) -> [Sentence] -> DataDefinition
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScopeType
-> [DecRef]
-> Maybe Derivation
-> ShortName
-> String
-> [Sentence]
-> DDPkt
DDPkt ScopeType
Global [] Maybe Derivation
der (Sentence -> ShortName
shortname' (Sentence -> ShortName) -> Sentence -> ShortName
forall a b. (a -> b) -> a -> b
$ String -> Sentence
S String
sn) (CI -> String -> String
forall c. CommonIdea c => c -> String -> String
prependAbrv CI
dataDefn String
sn)
ddME :: ModelQDef -> [DecRef] -> Maybe Derivation -> String -> [Sentence] -> DataDefinition
ddME :: ModelQDef
-> [DecRef]
-> Maybe Derivation
-> String
-> [Sentence]
-> DataDefinition
ddME q :: ModelQDef
q [] _ _ = String -> [Sentence] -> DataDefinition
forall a. HasCallStack => String -> a
error (String -> [Sentence] -> DataDefinition)
-> String -> [Sentence] -> DataDefinition
forall a b. (a -> b) -> a -> b
$ "Source field of " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ModelQDef -> String
forall a. HasUID a => a -> String
showUID ModelQDef
q String -> String -> String
forall a. [a] -> [a] -> [a]
++ " is empty"
ddME q :: ModelQDef
q refs :: [DecRef]
refs der :: Maybe Derivation
der sn :: String
sn = ModelQDef -> DDPkt -> DataDefinition
DDME ModelQDef
q (DDPkt -> DataDefinition)
-> ([Sentence] -> DDPkt) -> [Sentence] -> DataDefinition
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScopeType
-> [DecRef]
-> Maybe Derivation
-> ShortName
-> String
-> [Sentence]
-> DDPkt
DDPkt ScopeType
Global [DecRef]
refs Maybe Derivation
der (Sentence -> ShortName
shortname' (Sentence -> ShortName) -> Sentence -> ShortName
forall a b. (a -> b) -> a -> b
$ String -> Sentence
S String
sn) (CI -> String -> String
forall c. CommonIdea c => c -> String -> String
prependAbrv CI
dataDefn String
sn)
ddMENoRefs :: ModelQDef -> Maybe Derivation -> String -> [Sentence] -> DataDefinition
ddMENoRefs :: ModelQDef
-> Maybe Derivation -> String -> [Sentence] -> DataDefinition
ddMENoRefs q :: ModelQDef
q der :: Maybe Derivation
der sn :: String
sn = ModelQDef -> DDPkt -> DataDefinition
DDME ModelQDef
q (DDPkt -> DataDefinition)
-> ([Sentence] -> DDPkt) -> [Sentence] -> DataDefinition
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScopeType
-> [DecRef]
-> Maybe Derivation
-> ShortName
-> String
-> [Sentence]
-> DDPkt
DDPkt ScopeType
Global [] Maybe Derivation
der (Sentence -> ShortName
shortname' (Sentence -> ShortName) -> Sentence -> ShortName
forall a b. (a -> b) -> a -> b
$ String -> Sentence
S String
sn) (CI -> String -> String
forall c. CommonIdea c => c -> String -> String
prependAbrv CI
dataDefn String
sn)
qdFromDD :: DataDefinition -> Either SimpleQDef ModelQDef
qdFromDD :: DataDefinition -> Either SimpleQDef ModelQDef
qdFromDD (DDE qd :: SimpleQDef
qd _) = SimpleQDef -> Either SimpleQDef ModelQDef
forall a b. a -> Either a b
Left SimpleQDef
qd
qdFromDD (DDME qd :: ModelQDef
qd _) = ModelQDef -> Either SimpleQDef ModelQDef
forall a b. b -> Either a b
Right ModelQDef
qd
qdEFromDD :: DataDefinition -> Maybe SimpleQDef
qdEFromDD :: DataDefinition -> Maybe SimpleQDef
qdEFromDD (DDE qd :: SimpleQDef
qd _) = SimpleQDef -> Maybe SimpleQDef
forall a. a -> Maybe a
Just SimpleQDef
qd
qdEFromDD _ = Maybe SimpleQDef
forall a. Maybe a
Nothing