-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.elm
166 lines (120 loc) · 3.14 KB
/
App.elm
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
module App exposing (..)
import Html exposing (..)
import Html.Events exposing (..)
import CSS
import AcyclicDigraph exposing (Node, Edge, Cycle, AcyclicDigraph)
import ArcDiagram
import Set exposing (Set)
import Dict exposing (Dict)
import Model exposing (sample1, ProgramRepresentation)
main : Program Never Model Msg
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ program : ProgramRepresentation }
init : ( Model, Cmd Msg )
init =
( Model sample1, Cmd.none )
-- UPDATE
type Msg
= ChangeInput String
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
ChangeInput content ->
( model, Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
div [ CSS.body ]
[ h1 [ CSS.header ] [ text "elm visualize" ]
, div [ CSS.content ]
[ div []
[ h2 [ CSS.column ] [ text "Output" ]
, h2 [ CSS.column ] [ text "Input" ]
]
, div []
[ div [ CSS.column ] [ printGraph exampleGraph ]
, div [ CSS.column ] [ textarea [ onInput ChangeInput ] [] ]
]
]
]
type alias Graph =
( Set Edge, Dict Node String )
exampleGraph =
( exampleEdges, exampleLabels )
printGraph : Graph -> Html Msg
printGraph ( edges, labels ) =
let
toLabel =
(flip Dict.get) labels >> Maybe.withDefault ""
graphView =
case AcyclicDigraph.fromEdges edges of
Err cycles ->
viewCycles toLabel cycles
Ok graph ->
ArcDiagram.view
ArcDiagram.defaultLayout
(ArcDiagram.basicPaint toLabel)
graph
in
Html.div [ CSS.graphStyle ] [ graphView ]
viewCycles : (Node -> String) -> List Cycle -> Html a
viewCycles toLabel cycles =
Html.div
[]
[ Html.text "Graph has the following cycles:"
, Html.ol
[]
(cycles |> List.map (viewCycle toLabel))
]
viewCycle : (Node -> String) -> Cycle -> Html a
viewCycle toLabel cycle =
Html.li
[]
[ Html.text (cycle |> List.map toLabel |> String.join " -> ") ]
-- example data
exampleEdges : Set Edge
exampleEdges =
Set.fromList
[ ( 2, 1 )
, ( 3, 1 )
, ( 3, 2 )
, ( 4, 2 )
, ( 5, 3 )
, ( 5, 4 )
--, (5, 9) -- make cycle
, ( 6, 4 )
, ( 7, 4 )
, ( 8, 1 )
, ( 8, 3 )
, ( 8, 4 )
, ( 8, 6 )
, ( 9, 1 )
, ( 9, 3 )
, ( 9, 5 )
, ( 9, 6 )
]
exampleLabels : Dict Node String
exampleLabels =
Dict.fromList
[ ( 1, "Alfa" )
, ( 2, "Bravo" )
, ( 3, "Charlie" )
, ( 4, "Delta" )
, ( 5, "Echo" )
, ( 6, "Foxtrot" )
, ( 7, "Golf" )
, ( 8, "Hotel" )
, ( 9, "India" )
]