GraphMenu.hs revision 54ea981a0503c396c2923a1c06421c6235baf27f
{-# LANGUAGE CPP #-}
{- |
Module : $Header$
Description : Menu creation functions for the Graphdisplay
Copyright : (c) Thiemo Wiedemeyer, Uni Bremen 2002-2008
License : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
Maintainer : raider@informatik.uni-bremen.de
Stability : provisional
Portability : non-portable (imports Logic)
Menu creation
-}
module GUI.GraphMenu (createGraph) where
import qualified GUI.GraphAbstraction as GA
import GUI.GraphTypes
import GUI.GraphLogic
import GUI.ShowLogicGraph (showLogicGraph)
import GUI.Utils
import GUI.UDGUtils
import qualified GUI.HTkUtils as HTk
#ifdef GTKGLADE
import GUI.GtkLinkTypeChoice
import GUI.GtkConsistencyChecker
#endif
import Control.Concurrent.MVar
import Data.IORef
import Data.List (isPrefixOf)
import qualified Data.Map as Map
import System.Directory (getCurrentDirectory)
import Static.DevGraph
import Static.PrintDevGraph ()
import Proofs.ComputeTheory (computeTheory)
import qualified Proofs.VSE as VSE
import Common.Result
import Common.DocUtils
import Common.Consistency
import Driver.Options (HetcatsOpts, rmSuffix, prfSuffix)
import Driver.ReadFn (libNameToFile)
import Interfaces.DataTypes
import Interfaces.Command
import Interfaces.CmdAction
-- | Adds to the DGNodeType list style options for each type
nodeTypes :: HetcatsOpts
-> [( DGNodeType -- Nodetype
, Shape GA.NodeValue -- Shape
, String -- Color
)]
nodeTypes opts = map
( (\ (n, s) -> if isLocallyEmpty n then -- Add color
case nonRefType n of
NonRefType { isProvenCons = False }
-> (n, s, getColor opts Yellow False True)
_ -> (n, s, getColor opts Green True False)
else case nonRefType n of
RefType -> (n, s, getColor opts Coral False False)
t -> (n, s, getColor opts Coral False $ isProvenCons t)
)
. (\ n -> case nonRefType n of -- Add shape
RefType -> (n, Box)
NonRefType { isInternalSpec = True } -> (n, Circle)
NonRefType { isInternalSpec = False } -> (n, Ellipse)
)
) listDGNodeTypes
-- | A Map of all nodetypes and their properties.
mapNodeTypes :: HetcatsOpts -> Map.Map DGNodeType (Shape GA.NodeValue, String)
mapNodeTypes = Map.fromList . map (\ (n, s, c) -> (n, (s, c))) . nodeTypes
-- | Adds to the DGEdgeType list style options for each type
edgeTypes :: HetcatsOpts
-> [( DGEdgeType -- Edgetype
, EdgePattern GA.EdgeValue -- Lineformat
, String -- Color
, Bool -- has conservativity
)]
edgeTypes opts = map
( (\ (e, l, c) -> case edgeTypeModInc e of -- Add menu options
ThmType { thmEdgeType = GlobalOrLocalThm _ _ } -> (e, l, c, True)
GlobalDef -> (e, l, c, True)
HetDef -> (e, l, c, True)
LocalDef -> (e, l, c, True)
_ -> (e, l, c, False)
)
. (\ (e, l) -> case edgeTypeModInc e of -- Add colors
HidingDef -> (e, l, getColor opts Blue True $ not $ isInc e)
FreeOrCofreeDef -> (e, l, getColor opts Blue False $ not $ isInc e)
ThmType { thmEdgeType = thmType
, isProvenEdge = False } -> case thmType of
GlobalOrLocalThm { isLocalThmType = Local, isHomThm = False }
-> (e, l, getColor opts Coral True $ not $ isInc e)
HidingThm -> (e, l, getColor opts Yellow False $ not $ isInc e)
_ -> (e, l, getColor opts Coral False $ not $ isInc e)
ThmType { thmEdgeType = thmType
, isConservativ = False } -> case thmType of
GlobalOrLocalThm { isLocalThmType = Local, isHomThm = False }
-> (e, l, getColor opts Yellow True $ not $ isInc e)
_ -> (e, l, getColor opts Yellow False $ not $ isInc e)
ThmType { thmEdgeType = thmType
, isPending = True } -> case thmType of
GlobalOrLocalThm { isLocalThmType = Local, isHomThm = False }
-> (e, l, getColor opts Yellow True $ not $ isInc e)
_ -> (e, l, getColor opts Yellow False $ not $ isInc e)
ThmType { thmEdgeType = thmType } -> case thmType of
GlobalOrLocalThm { isLocalThmType = Local, isHomThm = False }
-> (e, l, getColor opts Green True $ not $ isInc e)
HidingThm -> (e, l, getColor opts Green True $ not $ isInc e)
_ -> (e, l, getColor opts Green False $ not $ isInc e)
_ -> (e, l, getColor opts Black False $ not $ isInc e)
)
. (\ e -> case edgeTypeModInc e of -- Add lineformat
ThmType { thmEdgeType = GlobalOrLocalThm { isLocalThmType = Local
, isHomThm = True } }
-> (e, Dashed)
ThmType { thmEdgeType = GlobalOrLocalThm { isHomThm = False } }
-> (e, Double)
LocalDef -> (e, Dashed)
HetDef -> (e, Double)
_ -> (e, Solid)
)
) listDGEdgeTypes
-- | A Map of all edgetypes and their properties.
mapEdgeTypes
:: HetcatsOpts -> Map.Map DGEdgeType (EdgePattern GA.EdgeValue, String)
mapEdgeTypes =
Map.fromList . map (\ (e, l, c, _) -> (e, (l, c))) . edgeTypes
-- | Creates the graph. Runs makegraph
createGraph :: GInfo -> String -> ConvFunc -> LibFunc -> IO ()
createGraph gInfo@(GInfo { graphInfo = gi
, hetcatsOpts = hetOpts
, options = opts
, libName = ln }) title convGraph showLib = do
ost <- readIORef $ intState gInfo
case i_state ost of
Nothing -> return ()
Just _ -> do
let file = rmSuffix (libNameToFile hetOpts ln) ++ prfSuffix
deselectEdgeTypes <- newIORef []
globMenu <- createGlobalMenu gInfo showLib deselectEdgeTypes
GA.makeGraph gi
title
(createOpen gInfo file convGraph showLib)
(createSave gInfo file)
(createSaveAs gInfo file)
(createClose gInfo)
(Just (createExit gInfo))
globMenu
(createNodeTypes gInfo convGraph showLib)
(createEdgeTypes gInfo)
(getColor hetOpts Purple False False)
$ runAndLock gInfo $ do
flags <- readIORef opts
writeIORef opts $ flags { flagHideNodes = False}
updateGraph gInfo []
-- | Returns the open-function
createOpen :: GInfo -> FilePath -> ConvFunc -> LibFunc -> Maybe (IO ())
createOpen gInfo file convGraph showLib = Just (
do
maybeFilePath <- fileOpenDialog file [ ("Proof", ["*.prf"])
, ("All Files", ["*"])] Nothing
case maybeFilePath of
Just fPath -> do
openProofStatus gInfo fPath convGraph showLib
return ()
Nothing -> fail "Could not open file."
)
-- | Returns the save-function
createSave :: GInfo -> FilePath -> Maybe (IO ())
createSave gInfo = Just . saveProofStatus gInfo
-- | Returns the saveas-function
createSaveAs :: GInfo -> FilePath -> Maybe (IO ())
createSaveAs gInfo file = Just (
do
maybeFilePath <- fileSaveDialog file [ ("Proof", ["*.prf"])
, ("All Files", ["*"])] Nothing
case maybeFilePath of
Just fPath -> saveProofStatus gInfo fPath
Nothing -> fail "Could not save file."
)
-- | Returns the close-function
createClose :: GInfo -> IO Bool
createClose gInfo@(GInfo { windowCount = wc
, openGraphs = oGrRef
, exitMVar = exit
, libName = ln }) = do
ost <- readIORef $ intState gInfo
case i_state ost of
Nothing -> return False
Just ist -> do
let le = i_libEnv ist
case Map.lookup ln le of
Just dgraph -> case openlock dgraph of
Just lock -> do
notopen <- isEmptyMVar lock
if notopen then
error "development graph seems to be closed already"
else takeMVar lock
Nothing -> error $ "MVar of " ++ show ln ++ " not initialized"
Nothing -> error $ "development graph with libname " ++ show ln
++" does not exist"
count <- takeMVar wc
if count <= 1
then putMVar exit ()
else do
putMVar wc $ count - 1
oGraphs <- readIORef oGrRef
writeIORef oGrRef $ Map.delete ln oGraphs
return True
-- | Returns the exit-function
createExit :: GInfo -> IO ()
createExit (GInfo {exitMVar = exit}) = putMVar exit ()
-- | Creates the global menu
createGlobalMenu :: GInfo -> LibFunc -> IORef [String]
-> IO [GlobalMenu]
createGlobalMenu gInfo@(GInfo { hetcatsOpts = opts
, graphInfo = gi
, libName = ln })
showLib deselectEdgeTypes =
do
ost <- readIORef $ intState gInfo
case i_state ost of
Nothing -> return []
Just _ -> do
let ral = runAndLock gInfo
performProofMenuAction cmd =
ral . proofMenu gInfo cmd
mkGlobProofButton cmd =
Button (menuTextGlobCmd cmd) . performProofMenuAction (GlobCmd cmd)
return
[GlobalMenu (Menu Nothing
[ Button "Undo" $ ral $ undo gInfo True
, Button "Redo" $ ral $ undo gInfo False
, Menu (Just "Hide/Show names/nodes/edges")
[ Button "Hide/Show internal node names"
$ ral $ toggleHideNames gInfo
, Button "Hide/Show unnamed nodes without open proofs"
$ ral $ toggleHideNodes gInfo
, Button "Hide/Show newly added proven edges"
$ ral $ toggleHideEdges gInfo
]
, Button "Focus node" $ ral $ focusNode gInfo
#ifdef GTKGLADE
, Button "Select Linktypes" $ showLinkTypeChoice deselectEdgeTypes
(\ eTypes -> ral $ do
GA.hideSetOfEdgeTypes gi eTypes
updateGraph gInfo []
)
, Button "Consistency checker" (showConsistencyChecker gInfo)
#endif
, Menu (Just "Proofs") $ map (\ (cmd, act) ->
-- History ? or just some partial history in ch ?
mkGlobProofButton cmd $ return . return . act ln) globLibAct
++ map (\ (cmd, act) -> mkGlobProofButton cmd $ return . act ln)
globLibResultAct
++ [ Menu (Just "Flattening") $ map ( \ (cmd, act) ->
mkGlobProofButton cmd $ return . act) globResultAct ]
, Button "Dump Development Graph" $ do
ost2 <- readIORef $ intState gInfo
case i_state ost2 of
Nothing -> putStrLn "no lib"
Just ist2 -> print . pretty . lookupDGraph (i_ln ist2)
$ i_libEnv ist2
, Button "Show Logic Graph" $ ral $ showLogicGraph daVinciSort
, Button "Show Library Graph" $ ral $ showLibGraph gInfo showLib
, Button "Save Graph for uDrawGraph" $ ral
$ saveUDGraph gInfo (mapNodeTypes opts) $ mapEdgeTypes opts
, Button "Save proof-script" $ ral
$ askSaveProofScript (graphInfo gInfo) $ intState gInfo
])
]
-- | A list of all Node Types
createNodeTypes :: GInfo -> ConvFunc -> LibFunc
-> [(DGNodeType,DaVinciNodeTypeParms GA.NodeValue)]
createNodeTypes gInfo@(GInfo {hetcatsOpts = opts}) cGraph showLib = map
(\ (n, s, c) ->
( n
, case nonRefType n of
RefType -> createLocalMenuNodeTypeDgRef s c gInfo cGraph showLib
NonRefType { isInternalSpec = True }
-> createLocalMenuNodeTypeInternal s c gInfo
NonRefType { isInternalSpec = False }
-> createLocalMenuNodeTypeSpec s c gInfo
)
) $ nodeTypes opts
-- | the edge types (share strings to avoid typos)
createEdgeTypes :: GInfo -> [(DGEdgeType,DaVinciArcTypeParms GA.EdgeValue)]
createEdgeTypes gInfo@(GInfo {hetcatsOpts = opts}) =
map (\(title, look, color, hasCons) ->
(title, look
$$$ Color color
$$$ (if hasCons then createLocalEdgeMenuConsEdge gInfo
else createLocalEdgeMenu gInfo)
$$$ (if hasCons then createLocalMenuValueTitleShowConservativity
$$$ emptyArcTypeParms :: DaVinciArcTypeParms GA.EdgeValue
else
emptyArcTypeParms :: DaVinciArcTypeParms GA.EdgeValue))
) $ edgeTypes opts
-- * methods to create the local menus of the different nodetypes
createLocalMenuNode :: GInfo -> DaVinciNodeTypeParms GA.NodeValue
createLocalMenuNode gInfo = LocalMenu (Menu (Just "node menu") $ map ($ gInfo)
[ createLocalMenuButtonShowNodeInfo
, createLocalMenuButtonShowTheory
, createLocalMenuButtonTranslateTheory
, createLocalMenuTaxonomy
, createLocalMenuButtonShowProofStatusOfNode
, createLocalMenuButtonProveAtNode
, createLocalMenuButtonProveStructured
, createLocalMenuButtonCheckCons
, createLocalMenuButtonCCCAtNode ]) $$$ emptyNodeTypeParms
-- | local menu for the nodetypes spec and locallyEmpty_spec
createLocalMenuNodeTypeSpec :: Shape GA.NodeValue -> String -> GInfo
-> DaVinciNodeTypeParms GA.NodeValue
createLocalMenuNodeTypeSpec shape color gInfo =
shape $$$ Color color
$$$ ValueTitle (\ (s,_) -> return s)
$$$ createLocalMenuNode gInfo
-- | local menu for the nodetypes internal and locallyEmpty_internal
createLocalMenuNodeTypeInternal :: Shape GA.NodeValue -> String -> GInfo
-> DaVinciNodeTypeParms GA.NodeValue
createLocalMenuNodeTypeInternal shape color
gInfo@(GInfo { internalNames = updaterIORef }) =
shape $$$ Color color
$$$ ValueTitleSource (\ (s,_) -> do
b <- newSimpleBroadcaster ""
updater <- readIORef updaterIORef
let upd = (s,applySimpleUpdate b)
writeIORef updaterIORef $ upd:updater
return $ toSimpleSource b)
$$$ createLocalMenuNode gInfo
-- | local menu for the nodetypes dg_ref and locallyEmpty_dg_ref
createLocalMenuNodeTypeDgRef :: Shape GA.NodeValue -> String -> GInfo
-> ConvFunc -> LibFunc
-> DaVinciNodeTypeParms GA.NodeValue
createLocalMenuNodeTypeDgRef shape color gInfo convGraph showLib =
shape $$$ Color color
$$$ ValueTitle (\ (s,_) -> return s)
$$$ LocalMenu (Menu (Just "node menu")
[createLocalMenuButtonShowNodeInfo gInfo,
createLocalMenuButtonShowTheory gInfo,
createLocalMenuButtonShowProofStatusOfNode gInfo,
createLocalMenuButtonProveAtNode gInfo,
Button "Show referenced library"
(\ (_, descr) -> do
showReferencedLibrary descr gInfo convGraph showLib
return ()
)])
$$$ emptyNodeTypeParms
type ButtonMenu a = MenuPrim (Maybe String) (a -> IO ())
-- | menu button for local menus
createMenuButton :: String -> (Int -> DGraph -> IO ())
-> GInfo -> ButtonMenu GA.NodeValue
createMenuButton title menuFun gInfo@(GInfo { libName = ln }) = Button title
$ \ (_, descr) -> do
ost <- readIORef $ intState gInfo
case i_state ost of
Nothing -> return ()
Just ist -> do
let le = i_libEnv ist
dGraph = lookupDGraph ln le
menuFun descr dGraph
return ()
createLocalMenuButtonShowTheory :: GInfo -> ButtonMenu GA.NodeValue
createLocalMenuButtonShowTheory gInfo =
createMenuButton "Show theory" (getTheoryOfNode gInfo) gInfo
createLocalMenuButtonTranslateTheory :: GInfo -> ButtonMenu GA.NodeValue
createLocalMenuButtonTranslateTheory gInfo =
createMenuButton "Translate theory" (translateTheoryOfNode gInfo) gInfo
-- | create a sub menu for taxonomy visualisation
createLocalMenuTaxonomy :: GInfo -> ButtonMenu GA.NodeValue
createLocalMenuTaxonomy gInfo = let
passTh displayFun descr _ = do
ost <- readIORef $ intState gInfo
case i_state ost of
Nothing -> return ()
Just ist -> do
let Result ds res = computeTheory (i_libEnv ist) (libName gInfo) descr
showDiagMess (hetcatsOpts gInfo) ds
maybe (return ()) (displayFun $ show descr) res
in Menu (Just "Taxonomy graphs")
[ createMenuButton "Subsort graph" (passTh displaySubsortGraph) gInfo
, createMenuButton "Concept graph" (passTh displayConceptGraph) gInfo ]
createLocalMenuButtonShowProofStatusOfNode :: GInfo -> ButtonMenu GA.NodeValue
createLocalMenuButtonShowProofStatusOfNode gInfo =
createMenuButton "Show proof status" (showProofStatusOfNode gInfo) gInfo
createLocalMenuButtonProveAtNode :: GInfo -> ButtonMenu GA.NodeValue
createLocalMenuButtonProveAtNode gInfo =
createMenuButton "Prove"
(proveAtNode False gInfo) gInfo
createLocalMenuButtonProveStructured :: GInfo -> ButtonMenu GA.NodeValue
createLocalMenuButtonProveStructured gInfo =
createMenuButton "Prove VSE Structured"
(\ descr _ -> proveVSEStructured gInfo descr) gInfo
createLocalMenuButtonCheckCons :: GInfo -> ButtonMenu GA.NodeValue
createLocalMenuButtonCheckCons gInfo =
createMenuButton "Check conservativity"
(\ descr -> checkconservativityOfNode descr gInfo) gInfo
-- | call VSE structured
proveVSEStructured :: GInfo -> Int -> IO ()
proveVSEStructured gi n =
proofMenu gi (SelectCmd Prover $ "VSE structured: " ++ show n)
$ VSE.prove (libName gi, n)
createLocalMenuButtonCCCAtNode :: GInfo -> ButtonMenu GA.NodeValue
createLocalMenuButtonCCCAtNode gInfo =
createMenuButton "Check consistency" (proveAtNode True gInfo) gInfo
createLocalMenuButtonShowNodeInfo :: GInfo -> ButtonMenu GA.NodeValue
createLocalMenuButtonShowNodeInfo =
createMenuButton "Show node info" showNodeInfo
-- * methods to create the local menus for the edges
createLocalEdgeMenu :: GInfo -> LocalMenu GA.EdgeValue
createLocalEdgeMenu gInfo = LocalMenu $ Menu (Just "short edge menu")
[createLocalMenuButtonShowEdgeInfo gInfo]
createLocalEdgeMenuConsEdge :: GInfo -> LocalMenu GA.EdgeValue
createLocalEdgeMenuConsEdge gInfo =
LocalMenu (Menu (Just "egde menu")
[ createLocalMenuButtonShowEdgeInfo gInfo
, createLocalMenuButtonCheckconservativityOfEdge gInfo])
createLocalMenuButtonShowEdgeInfo :: GInfo -> ButtonMenu GA.EdgeValue
createLocalMenuButtonShowEdgeInfo _ = Button "Show info"
(\ (_, (EdgeId descr), maybeLEdge) -> showEdgeInfo descr maybeLEdge)
createLocalMenuButtonCheckconservativityOfEdge :: GInfo
-> ButtonMenu GA.EdgeValue
createLocalMenuButtonCheckconservativityOfEdge gInfo =
Button "Check conservativity"
(\ (_, (EdgeId descr), maybeLEdge) ->
checkconservativityOfEdge descr gInfo maybeLEdge)
createLocalMenuValueTitleShowConservativity :: ValueTitle GA.EdgeValue
createLocalMenuValueTitleShowConservativity = ValueTitle
(\ (_, _, maybeLEdge) -> case maybeLEdge of
Just (_,_,edgelab) -> case dgl_type edgelab of
ScopedLink _ _ (ConsStatus c cp status) -> return (showCons c cp status)
_ -> return ""
Nothing -> return "")
where
showCons :: Conservativity -> Conservativity -> ThmLinkStatus -> String
showCons c cp status = case (c, cp, status) of
(None, None, _) -> ""
(None, _, LeftOpen) -> ""
(_, _, LeftOpen) -> show c ++ "?"
_ -> show cp
-- Suggests a proof-script filename.
getProofScriptFileName :: String -> IO FilePath
getProofScriptFileName f
| "/" `isPrefixOf` f = return $ f ++ ".hpf"
| otherwise = do
dir <- getCurrentDirectory
return $ dir ++ '/':f ++ ".hpf"
-- | Displays a Save-As dialog and writes the proof-script.
askSaveProofScript :: GA.GraphInfo -> IORef IntState -> IO ()
askSaveProofScript gi ch =
do
h <- readIORef ch
case undoList $ i_hist h of
[] -> infoDialog "Information" "The history is empty. No file written."
_ -> do
ff <- getProofScriptFileName $ rmSuffix $ filename h
maybeFilePath<- fileSaveDialog ff [("Proof Script",["*.hpf"])
, ("All Files", ["*"])] Nothing
case maybeFilePath of
Just fPath -> do
GA.showTemporaryMessage gi "Saving proof script ..."
saveCommandHistory ch fPath
GA.showTemporaryMessage gi $ "Proof script saved to " ++
fPath ++ "!"
Nothing -> GA.showTemporaryMessage gi "Aborted!"
-- Saves the history of commands in a file.
saveCommandHistory :: IORef IntState -> String -> IO ()
saveCommandHistory c f = do
h <- readIORef c
let history = ["# automatically generated hets proof-script", "",
"use " ++ filename h, ""]
++ reverse (map (showCmd . command) $ undoList $ i_hist h)
writeFile f $ unlines history