Guards, branch, simple factorial example.

This commit is contained in:
Robbie Gleichman 2016-01-13 15:08:08 -08:00
parent 41df0fd9eb
commit 4de1e369b1
3 changed files with 47 additions and 6 deletions

View File

@ -21,16 +21,20 @@ import Data.Maybe (fromMaybe)
-- TYPES --
-- | A datatype that represents an icon.
-- The BranchIcon is used as a branching point for a line.
-- The TextBoxIcon's data is the text that appears in the text box.
-- The LambdaRegionIcon's data is the number of lambda ports, and the name of it's
-- subdrawing.
data Icon = Apply0Icon | ResultIcon | TextBoxIcon String | LambdaRegionIcon Int Name
data Icon = Apply0Icon | ResultIcon | BranchIcon | TextBoxIcon String | GuardIcon Int
| LambdaRegionIcon Int Name
-- FUNCTIONS --
iconToDiagram Apply0Icon _ = apply0Dia
iconToDiagram ResultIcon _ = resultIcon
iconToDiagram BranchIcon _ = branchIcon
iconToDiagram (TextBoxIcon s) _ = textBox s
iconToDiagram (GuardIcon n) _ = guardIcon n
iconToDiagram (LambdaRegionIcon n diagramName) nameToSubdiagramMap =
lambdaRegion n dia
where
@ -129,6 +133,10 @@ lambdaRegion n dia =
-- RESULT ICON --
resultIcon = unitSquare # lw none # fc lime
-- BRANCH ICON --
branchIcon :: Diagram B
branchIcon = circle 0.3 # fc white # lc white
-- GUARD ICON --
guardTriangle :: Int -> Diagram B
guardTriangle x = triangleAndPort # alignL
@ -147,7 +155,7 @@ guardLBracket x = ell # alignT # alignL <> makePort x
-- Ports 1,3,5...: The left ports for the booleans
-- Ports 2,4...: The right ports for the values
guardIcon :: Int -> Diagram B
guardIcon n = (vcat $ take n trianglesAndBrackets # alignT) <> makePort 0
guardIcon n = centerXY $ vcat (take n trianglesAndBrackets # alignT) <> makePort 0
where
--guardTriangles = vsep 0.4 (take n (map guardTriangle [0,1..]))
trianglesWithPorts = map guardTriangle [2,4..]

View File

@ -114,6 +114,38 @@ super3Edges =
d1Name = toName "d1"
super3Drawing = Drawing super3Icons super2Edges [(d1Name, super2Drawing)]
factIcons = toNames
[
("g0", GuardIcon 2),
("one", TextBoxIcon "1"),
("eq0", TextBoxIcon "== 0"),
("-1", TextBoxIcon "-1"),
("eq0Ap", Apply0Icon),
("-1Ap", Apply0Icon),
("*", TextBoxIcon "*"),
("recurAp", Apply0Icon),
("*Ap1", Apply0Icon),
("*Ap2", Apply0Icon),
("arg", BranchIcon)
]
factEdges = [
iconToPort "eq0" "eq0Ap" 0,
portToPort "eq0Ap" 2 "g0" 1,
iconToPort "-1" "-1Ap" 0,
iconToPort "*" "*Ap1" 0,
iconToPort "one" "g0" 2,
portToPort "*Ap2" 2 "g0" 4,
portToPort "*Ap1" 2 "*Ap2" 0,
portToPort "recurAp" 2 "*Ap1" 1,
iconToPort "arg" "eq0Ap" 1,
iconToPort "arg" "-1Ap" 1,
iconToPort "arg" "*Ap2" 1
]
factDrawing = Drawing factIcons factEdges []
-- This is left commented out for a future test of the manual connect functions.
-- connectNodes g =
-- g # connectIconToPort "res" "A" (PortName 2) # connectIconToPort "foo" "B" (PortName 0)
@ -124,10 +156,10 @@ super3Drawing = Drawing super3Icons super2Edges [(d1Name, super2Drawing)]
main1 :: IO ()
main1 = do
placedNodes <- renderDrawing super2Drawing
placedNodes <- renderDrawing factDrawing
mainWith (placedNodes # bgFrame 0.1 black)
main2 = mainWith (guardIcon 3 # bgFrame 0.1 black)
main :: IO ()
main = main2
main = main1

View File

@ -64,9 +64,10 @@ placeNodes layoutResult nameDiagramMap = mconcat placedNodes
where
(positionMap, _) = getGraph layoutResult
placedNodes = map placeNode nameDiagramMap
placeNode (name, diagram) = place diagram (scaleFactor *^ (positionMap ! name))
-- todo: Not sure if the diagrams should already be centered at this point.
placeNode (name, diagram) = place (diagram # centerXY) (scaleFactor *^ (positionMap ! name))
-- constant
scaleFactor = 0.017
scaleFactor = 0.025
doGraphLayout graph nameDiagramMap connectNodes = do
layoutResult <- layoutGraph' layoutParams Neato graph