Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion code/drasil-database/lib/Drasil/Database/UIDRef.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Drasil.Database.UIDRef (
-- * 'UID' References
UIDRef, hide, unhide, unhideOrErr,
UIDRef, hide, unhide, unhideOrErr, uidRef, unRef,
UnitypedUIDRef, hideUni, unhideUni, unhideUniOrErr
) where

Expand All @@ -26,6 +26,14 @@ instance HasChunkRefs (UIDRef t) where
hide :: IsChunk t => t -> UIDRef t
hide = UIDRef . (^. uid)

-- | Create a 'UIDRef' from a raw 'UID'.
uidRef :: UID -> UIDRef t
uidRef = UIDRef

-- | Extract the 'UID' from a 'UIDRef'.
unRef :: UIDRef t -> UID
unRef (UIDRef u) = u

-- | Find a chunk by a 'UIDRef'.
unhide :: IsChunk t => UIDRef t -> ChunkDB -> Maybe t
unhide (UIDRef u) = find u
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Data.List (nub)
import Data.Maybe (mapMaybe)

-- rest of Drasil
import Drasil.Database (ChunkDB, UID, HasUID(..), find)
import Drasil.Database (ChunkDB, UID, HasUID(..), find, uidRef)
import Drasil.System (System(_systemdb), systemdb, refbyLookup)
import Language.Drasil
import Theory.Drasil (DataDefinition, GenDefn, InstanceModel, Theory(..),
Expand Down Expand Up @@ -136,7 +136,7 @@ helpToRefField trg db
-- | Helper that makes a list of 'Reference's into a 'Sentence'. Then wraps into 'Contents'.
helperSources :: [DecRef] -> [Contents]
helperSources [] = [mkParagraph $ S "--"]
helperSources rs = [mkParagraph $ foldlList Comma List $ map (\r -> Ref (r ^. uid) EmptyS $ refInfo r) rs]
helperSources rs = [mkParagraph $ foldlList Comma List $ map (\r -> Ref (uidRef (r ^. uid)) EmptyS $ refInfo r) rs]

-- | Creates the fields for a definition from a 'QDefinition' (used by 'ddefn').
mkDDField :: DataDefinition -> System -> Field -> ModRow -> ModRow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Data.Char (toLower)
import Data.Maybe (fromMaybe)

import Language.Drasil
import Drasil.Database (UID, find, isRegistered, (+++.), mkUid, ChunkDB)
import Drasil.Database (UID, find, isRegistered, (+++.), mkUid, uidRef, ChunkDB)
import Drasil.Database.SearchTools (termResolve', shortForm)
import Drasil.System (System, systemdb)
import Control.Lens ((^.))
Expand Down Expand Up @@ -198,4 +198,4 @@ resourcePath = "../../../../traceygraphs/"

-- | Helper to create a list of traceability graph references.
folderList' :: [ItemType]
folderList' = map (Flat . (\x -> Ref (x +++. "Link") EmptyS None)) traceGUIDs
folderList' = map (Flat . (\x -> Ref (uidRef (x +++. "Link")) EmptyS None)) traceGUIDs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Language.Drasil.Development.Sentence (

import Control.Lens ((^.))

import Drasil.Database (HasUID(..))
import Drasil.Database (HasUID(..), uidRef)

import Language.Drasil.Classes (NamedIdea(term), Idea)
import Language.Drasil.Sentence ((+:+), sParen, sentenceTerm,
Expand All @@ -38,7 +38,7 @@ toSent (P p) = S.P p
-- term, where lookupS is the main helper for looking up the short form of a
-- 'Ch' Sentence.
short :: Idea c => c -> S.Sentence
short c = sentenceShort (c ^. uid)
short c = sentenceShort (uidRef (c ^. uid))

-- | Helper for common pattern of introducing the title-case version of a
-- noun phrase (from an Idea)
Expand All @@ -64,11 +64,11 @@ titleize' n = toSent $ NP.titleizeNP' (n ^. term)

-- | Helper for getting the phrase from a 'NamedIdea' using it's UID.
phrase :: NamedIdea n => n -> S.Sentence
phrase n = sentenceTerm (n ^. uid)
phrase n = sentenceTerm (uidRef (n ^. uid))

-- | Helper for getting the plural of a phrase from a 'NamedIdea'.
plural :: NamedIdea n => n -> S.Sentence
plural n = sentencePlural (n ^. uid)
plural n = sentencePlural (uidRef (n ^. uid))
--plural n = NP.plural (n ^. term)

-- | Helper for getting the possesive cases from the term of a 'NamedIdea'.
Expand Down
6 changes: 3 additions & 3 deletions code/drasil-lang/lib/Language/Drasil/Reference.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Language.Drasil.Reference (

import Control.Lens ((^.), makeLenses, Lens')

import Drasil.Database (UID, HasUID(..), HasChunkRefs(..))
import Drasil.Database (UID, HasUID(..), HasChunkRefs(..), uidRef)

import Language.Drasil.Label.Type (LblType, HasRefAddress(..))
import Language.Drasil.ShortName (HasShortName(..), ShortName)
Expand Down Expand Up @@ -64,9 +64,9 @@ namedRef r s = namedComplexRef r s None

-- | Takes a 'Reference' with additional display info. Uses the internal shortname for its display name.
complexRef :: (HasUID r, HasRefAddress r, HasShortName r) => r -> RefInfo -> Sentence
complexRef r = Ref (ref r ^. uid) EmptyS
complexRef r = Ref (uidRef (ref r ^. uid)) EmptyS

-- | Takes a 'Reference' with a name to be displayed and any additional information and wraps it into a 'Sentence'.
-- Does not overwrite the shortname contained in the reference, but will only display as the given 'Sentence' along with the given 'RefInfo'.
namedComplexRef :: (HasUID r, HasRefAddress r, HasShortName r) => r -> Sentence -> RefInfo -> Sentence
namedComplexRef r = Ref (ref r ^. uid)
namedComplexRef r = Ref (uidRef (ref r ^. uid))
113 changes: 105 additions & 8 deletions code/drasil-lang/lib/Language/Drasil/Sentence.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@ module Language.Drasil.Sentence (
-- * Functions
(+:+), (+:+.), (+:), (!.), capSent, headSent, ch, eS, eS', sC, sDash, sParen,
sentencePlural, sentenceShort,
sentenceSymb, sentenceTerm
sentenceSymb, sentenceTerm,
sdep, shortdep, lnames, lnames'
) where

import Control.Lens ((^.))
import Data.Char (toUpper)

import Drasil.Database (HasUID(..), UID)
import Drasil.Database (HasChunkRefs(..), HasUID(..), UID, UIDRef, uidRef, unRef)

import Language.Drasil.ExprClasses (Express(express))
import Language.Drasil.ModelExpr.Lang (ModelExpr)
import Language.Drasil.ModelExpr.Extract (meNames)
import Language.Drasil.NounPhrase.Core (NP)
import Language.Drasil.UnitLang (USymb)
import Language.Drasil.Symbol (HasSymbol, Symbol)

import Data.Containers.ListUtils (nubOrd)
import qualified Data.Set as Set

-- | Used in 'Ch' constructor to determine the state of a term
-- (can record whether something is in plural form, a singular term, or in short form).
data SentenceStyle = PluralTerm
Expand Down Expand Up @@ -54,9 +59,9 @@ infixr 5 :+:
data Sentence where
-- | Ch looks up the term for a given 'UID' and displays the term with a given 'SentenceStyle' and 'CapitalizationRule'.
-- This allows Sentences to hold plural forms of 'NamedIdea's.
Ch :: SentenceStyle -> TermCapitalization -> UID -> Sentence
Ch :: SentenceStyle -> TermCapitalization -> UIDRef typ -> Sentence
-- | A branch of Ch dedicated to SymbolStyle only.
SyCh :: UID -> Sentence
SyCh :: UIDRef typ -> Sentence
-- | Converts a unit symbol into a usable Sentence form.
Sy :: USymb -> Sentence
-- | Directly embeds a 'NP'
Expand All @@ -68,7 +73,7 @@ data Sentence where
-- | Lifts an expression into a Sentence.
E :: ModelExpr -> Sentence
-- | Takes a 'UID' to a reference, a display name ('Sentence'), and any additional reference display information ('RefInfo'). Resolves the reference later (similar to Ch).
Ref :: UID -> Sentence -> RefInfo -> Sentence
Ref :: UIDRef typ -> Sentence -> RefInfo -> Sentence
-- | Adds quotation marks around a Sentence.
Quote :: Sentence -> Sentence
-- | Used for a % symbol.
Expand All @@ -87,7 +92,7 @@ eS' = E . express
-- The HasSymbol is redundant, but on purpose
-- | Gets a symbol and places it in a 'Sentence'.
ch :: (HasUID c, HasSymbol c) => c -> Sentence
ch x = SyCh (x ^. uid)
ch x = SyCh (uidRef (x ^. uid))

-- | Sentences can be concatenated.
instance Semigroup Sentence where
Expand All @@ -97,8 +102,8 @@ instance Semigroup Sentence where
instance Monoid Sentence where
mempty = EmptyS

-- | Smart constructors for turning a 'UID' into a 'Sentence'.
sentencePlural, sentenceShort, sentenceSymb, sentenceTerm :: UID -> Sentence
-- | Smart constructors for turning a 'UID' reference into a 'Sentence'.
sentencePlural, sentenceShort, sentenceSymb, sentenceTerm :: UIDRef typ -> Sentence
-- | Gets plural term of 'UID'.
sentencePlural = Ch PluralTerm NoCap
-- | Gets short form of 'UID'.
Expand Down Expand Up @@ -149,3 +154,95 @@ capSent x = x
-- | Helper which creates a Header with size s of the 'Sentence'.
headSent :: Int -> Sentence -> Sentence
headSent s x = S (concat (replicate s "#")) :+: S " " :+: x

-- | Helpers for extracting references -----------------------------------------

-- | Generic traverse of all positions that could lead to /symbolic/ 'UID's from 'Sentence's.
getUIDs :: Sentence -> [UID]
getUIDs (Ch ShortStyle _ _) = []
getUIDs (Ch TermStyle _ _) = []
getUIDs (Ch PluralTerm _ _) = []
getUIDs (SyCh a) = [unRef a]
getUIDs Sy {} = []
getUIDs NP {} = []
getUIDs S {} = []
getUIDs P {} = []
getUIDs Ref {} = []
getUIDs Percent = []
getUIDs ((:+:) a b) = getUIDs a ++ getUIDs b
getUIDs (Quote a) = getUIDs a
getUIDs (E a) = meNames a
getUIDs EmptyS = []

-- | Generic traverse of all positions that could lead to /symbolic/ and /abbreviated/ 'UID's from 'Sentence's
-- but doesn't go into expressions.
getUIDshort :: Sentence -> [UID]
getUIDshort (Ch ShortStyle _ a) = [unRef a]
getUIDshort (Ch TermStyle _ _) = []
getUIDshort (Ch PluralTerm _ _) = []
getUIDshort SyCh {} = []
getUIDshort Sy {} = []
getUIDshort NP {} = []
getUIDshort S {} = []
getUIDshort Percent = []
getUIDshort P {} = []
getUIDshort Ref {} = []
getUIDshort ((:+:) a b) = getUIDshort a ++ getUIDshort b
getUIDshort (Quote a) = getUIDshort a
getUIDshort E {} = []
getUIDshort EmptyS = []

-----------------------------------------------------------------------------
-- And now implement the exported traversals all in terms of the above
-- | This is to collect /symbolic/ 'UID's that are printed out as a 'Symbol'.
sdep :: Sentence -> [UID]
sdep = nubOrd . getUIDs
{-# INLINE sdep #-}

-- This is to collect symbolic 'UID's that are printed out as an /abbreviation/.
shortdep :: Sentence -> [UID]
shortdep = nubOrd . getUIDshort
{-# INLINE shortdep #-}

-- | Generic traverse of all positions that could lead to /reference/ 'UID's from 'Sentence's.
lnames :: Sentence -> [UID]
lnames Ch {} = []
lnames SyCh {} = []
lnames Sy {} = []
lnames NP {} = []
lnames S {} = []
lnames Percent = []
lnames P {} = []
lnames (Ref a _ _) = [unRef a]
lnames ((:+:) a b) = lnames a ++ lnames b
lnames Quote {} = []
lnames E {} = []
lnames EmptyS = []
{-# INLINE lnames #-}

-- | Get /reference/ 'UID's from 'Sentence's.
lnames' :: [Sentence] -> [UID]
lnames' = concatMap lnames
{-# INLINE lnames' #-}

-- | Generic traverse of all positions that could lead to any 'UID's from 'Sentence's.
getAllUIDs :: Sentence -> [UID]
getAllUIDs (Ch ShortStyle _ a) = [unRef a]
getAllUIDs (Ch TermStyle _ _) = []
getAllUIDs (Ch PluralTerm _ _) = []
getAllUIDs (SyCh a) = [unRef a]
getAllUIDs Sy {} = []
getAllUIDs NP {} = []
getAllUIDs S {} = []
getAllUIDs P {} = []
getAllUIDs (Ref a _ _) = [unRef a]
getAllUIDs Percent = []
getAllUIDs ((:+:) a b) = getAllUIDs a ++ getAllUIDs b
getAllUIDs (Quote a) = getAllUIDs a
getAllUIDs (E a) = meNames a
getAllUIDs EmptyS = []
{-# INLINE getAllUIDs #-}

instance HasChunkRefs Sentence where
chunkRefs = Set.fromList . getAllUIDs
{-# INLINABLE chunkRefs #-}
75 changes: 4 additions & 71 deletions code/drasil-lang/lib/Language/Drasil/Sentence/Extract.hs
Original file line number Diff line number Diff line change
@@ -1,74 +1,7 @@
-- | Extract various kinds of UIDs from a Sentence. Used in conjunction with the
-- chunk database in order to render terms, symbols, and references properly.
module Language.Drasil.Sentence.Extract(sdep, shortdep, lnames, lnames') where
-- The actual traversals now live in 'Language.Drasil.Sentence', but this
-- module keeps the old import path working.
module Language.Drasil.Sentence.Extract (sdep, shortdep, lnames, lnames') where

import Data.Containers.ListUtils (nubOrd)

import Drasil.Database (UID)

import Language.Drasil.Sentence(Sentence(..), SentenceStyle(..))
import Language.Drasil.ModelExpr.Extract (meNames)

-- | Generic traverse of all positions that could lead to /symbolic/ 'UID's from 'Sentence's.
getUIDs :: Sentence -> [UID]
getUIDs (Ch ShortStyle _ _) = []
getUIDs (Ch TermStyle _ _) = []
getUIDs (Ch PluralTerm _ _) = []
getUIDs (SyCh a) = [a]
getUIDs Sy {} = []
getUIDs NP {} = []
getUIDs S {} = []
getUIDs P {} = []
getUIDs Ref {} = []
getUIDs Percent = []
getUIDs ((:+:) a b) = getUIDs a ++ getUIDs b
getUIDs (Quote a) = getUIDs a
getUIDs (E a) = meNames a
getUIDs EmptyS = []

-- | Generic traverse of all positions that could lead to /symbolic/ and /abbreviated/ 'UID's from 'Sentence's
-- but doesn't go into expressions.
getUIDshort :: Sentence -> [UID]
getUIDshort (Ch ShortStyle _ a) = [a]
getUIDshort (Ch TermStyle _ _) = []
getUIDshort (Ch PluralTerm _ _) = []
getUIDshort SyCh {} = []
getUIDshort Sy {} = []
getUIDshort NP {} = []
getUIDshort S {} = []
getUIDshort Percent = []
getUIDshort P {} = []
getUIDshort Ref {} = []
getUIDshort ((:+:) a b) = getUIDshort a ++ getUIDshort b
getUIDshort (Quote a) = getUIDshort a
getUIDshort E {} = []
getUIDshort EmptyS = []

-----------------------------------------------------------------------------
-- And now implement the exported traversals all in terms of the above
-- | This is to collect /symbolic/ 'UID's that are printed out as a 'Symbol'.
sdep :: Sentence -> [UID]
sdep = nubOrd . getUIDs

-- This is to collect symbolic 'UID's that are printed out as an /abbreviation/.
shortdep :: Sentence -> [UID]
shortdep = nubOrd . getUIDshort

-- | Generic traverse of all positions that could lead to /reference/ 'UID's from 'Sentence's.
lnames :: Sentence -> [UID]
lnames Ch {} = []
lnames SyCh {} = []
lnames Sy {} = []
lnames NP {} = []
lnames S {} = []
lnames Percent = []
lnames P {} = []
lnames (Ref a _ _) = [a]
lnames ((:+:) a b) = lnames a ++ lnames b
lnames Quote {} = []
lnames E {} = []
lnames EmptyS = []

-- | Get /reference/ 'UID's from 'Sentence's.
lnames' :: [Sentence] -> [UID]
lnames' = concatMap lnames
import Language.Drasil.Sentence (sdep, shortdep, lnames, lnames')
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Language.Drasil.Printing.Import.Sentence where
import Control.Lens ((^.))
import Data.Maybe (fromMaybe)

import Drasil.Database (unRef)
import Language.Drasil hiding (neg, sec, symbol, isIn)
import Language.Drasil.Development (toSent)
import Drasil.Database.SearchTools (termResolve', TermAbbr(..))
Expand All @@ -29,16 +30,16 @@ spec _ (Sy s) = P.E $ pUnit s
spec sm (NP np) = spec sm (toSent $ phraseNP np)
spec _ Percent = P.E $ P.MO P.Perc
spec _ (P s) = P.E $ symbol s
spec sm (SyCh s) = P.E $ symbol $ lookupC' sm s
spec sm (SyCh s) = P.E $ symbol $ lookupC' sm (unRef s)

-- First term is the tooltip, second term is the rendered short form
spec sm (Ch ShortStyle caps s) = P.Tooltip (spec sm $ lookupT
sm s caps) (spec sm $ lookupS sm s caps)
sm (unRef s) caps) (spec sm $ lookupS sm (unRef s) caps)

spec sm (Ch TermStyle caps s) = spec sm $ lookupT sm s caps
spec sm (Ch PluralTerm caps s) = spec sm $ lookupP sm s caps
spec sm (Ch TermStyle caps s) = spec sm $ lookupT sm (unRef s) caps
spec sm (Ch PluralTerm caps s) = spec sm $ lookupP sm (unRef s) caps
spec sm (Ref u EmptyS notes) =
let reff = refFind u sm in
let reff = refFind (unRef u) sm in
case reff of
(Reference _ (RP rp ra) sn) ->
P.Ref P.Internal ra (spec sm $ renderShortName sm rp sn)
Expand All @@ -47,7 +48,7 @@ spec sm (Ref u EmptyS notes) =
(Reference _ (URI ra) sn) ->
P.Ref P.External ra (spec sm $ renderURI sm sn)
spec sm (Ref u dName notes) =
let reff = refFind u sm in
let reff = refFind (unRef u) sm in
case reff of
(Reference _ (RP _ ra) _) ->
P.Ref P.Internal ra (spec sm dName)
Expand Down