-
Notifications
You must be signed in to change notification settings - Fork 15
/
graph_algs.txt
107 lines (91 loc) · 5.68 KB
/
graph_algs.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
-- Pseudocode for collapsing a syntax graph.
-- New stateful algorithm
-- Note: Node refers to the FGL node, which is just an integer. The node label has to be looked up separately in the graph.
-- The node label has all of the information about the node such as what SyntaxNode it represents.
collapseRoots treeRoots = foldl' (collapseTree treeRoots)
collapseNodes :: SyntaxGrrah -> SyntaxGraph
collapseNodes originalGraph = finalGraph where
-- findTreeRoots returns a list of nodes that will embed other nodes, but are not embedded themselves.
-- These nodes are thus each a root of a collapsed node tree.
treeRoots = findTreeRoots originalGraph
-- Now collapse each tree of nodes
finalGraph = collapseRoots treeRoots originalGraph treeRoots
-- |findTreeRoots returns a list of nodes that might embed other nodes, but are not embedded themselves.
-- These nodes are thus each a root of a collapsed node tree.
-- A node is a treeRoot if all of these conditions are true:
-- 1. The SyntaxNode can embed other nodes (i.e. nodeCanEmbed is true)
-- 2. The node has at least one parent that can not embed (i.e. the node has a parent where nodeCanEmbed is false.)
-- Note: A treeRoot may not actually have any embeddable children, since collapseTree will do nothing in that case.
findTreeRoots :: SyntaxGraph -> [Node]
-- filterNodes is a library function that returns a list of the nodes in the graph
-- where the filter function is true.
-- filterNodes :: (Node -> Bool) -> Graph -> [Node]
findTreeRoots graph = filterNodes (isTreeRoot graph) graph
isTreeRoot :: SyntaxGraph -> Node -> Bool
isTreeRoot graph node = graphNodeCanEmbed graph node && hasAParentThatCannotEmbed where
hasAParentThatCannotEmbed = not $ null parentsThatCannotEmbed
parentsThatCannotEmbed = filter (graphNodeCanEmbed graph) (findParents graph node)
findParents :: Graph -> Node -> [Node]
findParents = _ -- TODO
graphNodeCanEmbed :: Graph -> Node -> Bool
graphNodeCanEmbed graph node = syntaxNodeCanEmbed $ lookupSyntaxNode graph node
lookupSyntaxNode :: SyntaxGraph -> Node -> SyntaxNode
collapseTree :: [Node] -> SyntaxGraph -> Node -> SyntaxGraph
collapseTree treeRoots oldGraph rootNode = case childrenToEmbed of
[] -> oldGraph
_ -> finalGraph
where
-- TODO Write pseudocode for subfunctions
childrenToEmbed = findChildrenToEmbed treeRoots rootNode oldGraph
-- Recursively collapse the children nodes
graphWithCollapsedChildren = collapseRoots treeRoots oldGraph childrenToEmbed
-- Transfer the edges of the children to rootNode
childEdgesToTransfer = findChildEdgesToTransfer childrenToEmbed graphWithCollapsedChildren
graphWithChildEdgesDeleted = deleteChildEdges childEdgesToTransfer graphWithCollapsedChildren
graphWithEdgesTransferred = addChildEdges rootNode childEdgesToTransfer graphWithChildEdgesDeleted
-- Modify the rootNode label (i.e. SyntaxNode) to incorporate the children it is embedding
graphWithChildrenCollapsed = embedChildSyntaxNodes rootNode childrenToEmbed graphWithEdgesTransferred
-- Delete the children that have been embedded
finalGraph = deleteChildren childrenToEmbed graphWithChildrenCollapsed
-- | findChildrenToEmbed returns a list of the node's children that can be embedded
-- A child can be embedded iff all of these conditions are true:
-- 1. The node is not a treeRoot (otherwise a cycle of embedding could occur)
-- 2. The SyntaxNode is embeddable (i.e. nodeIsEmbeddable is True)
-- 3. The node has exactly one parent that can embed (i.e. nodeCanEmbed is True for one parent)
findChildrenToEmbed :: Node -> SyntaxGraph -> [Node]
findChildrenToEmbed treeRoots node graph = if graphNodeCanEmbed node graph
then childrenToEmbed
else []
where
childrenToEmbed = _ -- TODO
-- | graphNodeCanEmbed returns true if the label (SyntaxNode) associated with the
-- node can be embedded in other SyntaxNodes (i.e. nodeCanEmbed is True)
graphNodeCanEmbed :: Node -> SyntaxGraph -> Bool
graphNodeCanEmbed node graph = _
-- OLD ALGORITHM
-- To make the problem simpler, collapseNodes just cares about the
collapseNodes :: SyntaxGraph -> SyntaxGraph
collapseNodes inGraph = graphFold foldFunc initialOutputGraph inGraph where
initialOutputGraph = emptyGraph
foldFunc :: SyntaxGraph -> Context -> SyntaxGraph
foldFunc oldGraph context = let node = nodeInContext context in
-- The node can not be embedded, and can not embed other nodes, so it is by itself.
-- We thus just add it to the accumulator graph.
-- willEmbed is true iff the current node will embed other nodes in the graph
| not (willBeEmbedded context inGraph) && not (willEmbed context inGraph) = context & oldGraph
-- willBeEmbedded checks to see if the parent of the current node will embed the current node
-- In this case the current node will be embedded, and does not embed other node.
-- We do not add the current node since it will be embedded in its parent.
-- This case is not necessary. If the current node were to be added, it would simply be
-- removed again from the accumulation graph.
| willBeEmbedded context inGraph && not (willEmbed context) = oldGraph
-- This node will embed other nodes.
-- First we find our children that will be embedded. If the child is in the
-- oldGraph, then we also need to remove the child from the oldGraph.
-- If the child is not yet in the oldGraph, then we just embed it.
| willEmbed context inGraph =
newGraph where
(oldGraphChildren, oldGraphWithChildrenRemoved) = findAndRemoveChildren oldGraph context
remainingChildrem = getRemainingChildren oldGraphChildren inGraph context
embeddedNode = makeEmbeddedNode context oldGraphChildren remainingChildren
newGarph = embeddedNode & oldGraphWithChildrenRemoved