-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
267 lines (243 loc) · 11.4 KB
/
Main.hs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
{- |
Description: Module of the @main@ function to run demos and print meta-information.
License: GNU LGPLv3
Maintainer: [email protected]
Module of the @main@ function to run demos and print meta-information.
-}
module Main where
import Control.Monad.State ( State )
import UUID ( UUID )
import Util (removeQuotes, genIndent )
import Tree.Tree as Tree ( prettyPrint )
import Tree.AST
import Tree.Grammar
import Tree.ASTPrettyPrinter
import Tree.Edits ( edit_identity )
import Propositions.Logic
import Data.Void
import Propositions.Propositions
import Propositions.NullPropositions as NullPropositions
import Feature.FeatureTrace as FeatureTrace
import Feature.Recording.FeatureTraceRecording
import Feature.FeatureColour (FeatureFormulaColourPalette)
import Examples.Example as Example
import Tree.TikzExport ( astToTikzWithTraceDefault )
import Examples.StackPopAlice as StackPopAlice (example)
import Examples.StackPopBob as StackPopBob (example)
import Examples.EditPatterns as EditPatterns
import Data.List (intercalate)
-- imports for Terminal printing ---------
import Data.Text.Prettyprint.Doc
( Doc, (<+>), annotate, hardline, Pretty(pretty) )
import System.Terminal
import Propositions.Truthtable (generatetruthtablesfor)
-- | Style defining how to print 'AST's.
data CodePrintStyle
= ShowAST -- ^ Prints the 'AST' by showing each 'Node' in the hierarchy in an XML-like format.
| ShowCode -- ^ Prints the 'AST' as the actual source code that it represents.
| ShowTikz -- ^ Print the 'AST' as tikz code to use for our paper.
deriving (Show)
-- | Format defining whether to 'FeatureTrace's or presence conditions ('pc').
data TraceDisplay
= Trace -- ^ Show the feature mapping of each node (i.e., the formula a node is directly annotated with).
| PC -- ^ Show the presence condition of each node (i.e., the conjunction of its feature mapping with all feature mappings inherited from ancestors).
deriving (Show, Eq)
-- | Style defining how to print 'FeatureTrace's.
data TraceStyle
= Text -- ^ Show feature mapping formulas as plain text.
| Colour -- ^ Encode features as colours to visualize feature mappings by colouring source code.
| None -- ^ Do not show feature traces at all.
deriving (Show, Eq)
{- |
Format in which code and (recorded) feature mappings should be printed to the terminal.
-}
data OutputFormat = OutputFormat {
codeStyle :: CodePrintStyle,
traceDisplay :: TraceDisplay,
traceStyle :: TraceStyle,
withTraceLines :: Bool -- ^ Whether there should be vertical lines next to the shown code on the left that indicate presence condtions.
}
-- Some presets for output formats:
{- |
The perspective of the developer, who is editing code while traces are recorded in the background
This is the format used in the figures in the paper.
-}
userFormat :: OutputFormat
userFormat = OutputFormat {
codeStyle = ShowCode,
traceDisplay = PC,
traceStyle = Colour,
withTraceLines = False
}
{- |
A variation of 'userFormat' where traces and presence conditions can be investigated seperately at the same time.
Code is coloured in the colour of its trace while presence conditions are indicated by coloured lines on the left.
-}
userFormatDetailed :: OutputFormat
userFormatDetailed = OutputFormat {
codeStyle = ShowCode,
traceDisplay = Trace,
traceStyle = Colour,
withTraceLines = True
}
{- |
Shows the 'AST' of the source code with 'FeatureTrace's as formulas.
-}
astFormat :: OutputFormat
astFormat = OutputFormat {
codeStyle = ShowAST,
traceDisplay = Trace,
traceStyle = Text,
withTraceLines = False
}
{- |
Tikz export of 'AST' with 'FeatureTrace's.
Used for figures in the paper.
-}
tikzFormat :: OutputFormat
tikzFormat = OutputFormat {
codeStyle = ShowTikz,
traceDisplay = Trace,
traceStyle = None,
withTraceLines = False
}
{- |
Entry point of the demo.
The main method will run and print the output of all examples (Alice, Bob, and the edit patterns).
You can change the format of the printed source code and feature mappings by changing the @format@ parameter inside 'main'.
Additionally, you might want to look at the truthtable of the ternary logic by Sobocinski we use (by uncommenting the line @showTruthtables@).
-}
main :: IO ()
main =
{-
Select your OutputFormat here.
Above, there is a list of presets you can choose from.
-}
let format = userFormat in
do
showExamples format
-- showTruthtables
{- |
Runs the motivating example from the paper and examples for all edit patterns with the given 'OutputFormat'.
First, runs Alice's example where she records feature traces upon editing the pop method of a class Stack in Java (Figure 1 in the paper).
Second, shows how Bob could propagate Alice's edits and recorded feature traces to his variant as envisioned in future research.
Third, shows an instance of each edit pattern from our evaluation.
-}
showExamples :: OutputFormat -> IO ()
showExamples format = withTerminal $ runTerminalT $
let run = runStepwise format in
do
putDoc hardline
headline "Running Feature Trace Recording Demo"
headline ">>> [Motivating Example] <<<"
run StackPopAlice.example
run StackPopBob.example
headline ">>> [Edit Patterns] <<<"
run EditPatterns.addIfdef
run EditPatterns.addIfdefWithPC
-- We omitted AddIfdef* as it is just a repitition of the previous pattern with arbitrary contexts and code fragments.
-- AddIfDefElse has to be reproduced using two variants.
-- Hence, we need two different examples here, one for the if-branch and one for the else-branch.
run EditPatterns.addIfdefElse_IfBranch
run EditPatterns.addIfdefElse_ElseBranch
run EditPatterns.addIfdefElse_IfBranchWithPC
run EditPatterns.addIfdefElse_ElseBranchWithPC
run EditPatterns.addIfdefWrapElse
run EditPatterns.addIfdefWrapThen
-- Adding non-variational code (code that belongs to all clones)
run EditPatterns.addNormalCode_nonvariational
-- Adding code without any associated trace into a tree-optional scope that is already traced.
run EditPatterns.addNormalCode_outerpc
-- Removing code that does not have a presence condition
run EditPatterns.remNormalCode_null
-- Removing code that has a feature trace and thereby a presence condition
run EditPatterns.remNormalCode_notnull
-- Removing code that has a feature trace
run EditPatterns.remIfdef
-- | Turns the given text into a headline in the terminal.
-- We indicate headlines with a red background.
headline :: (MonadColorPrinter m) => String -> m()
headline text = putDoc $ hardline <+> (annotate (background red) $ pretty text) <+> hardline <+> hardline
-- | Runs the given 'Example' in the given 'OutputFormat' step by step (i.e., showing all intermediate results).
runStepwise :: (MonadColorPrinter m, Grammar g, ASTPrettyPrinter g) => OutputFormat -> State UUID (Example m g String) -> m ()
runStepwise format ex =
let example = finalizeExample ex
result = printTraces format example (Example.runExampleWithDefaultFTR example) in
(putDoc $ result <+> hardline) >> flush
-- | Prints the given 'AST' with the given 'FeatureTrace's in the given 'OutputFormat'.
-- If the 'OutputFormat' mandates to visualize feature mappings as colours (see 'TraceStyle'),
-- the given 'FeatureFormulaColourPalette' will be used to assign colours to feature formulas.
printASTWithTrace :: (MonadColorPrinter m, Grammar g, ASTPrettyPrinter g, Show a, Eq a) =>
OutputFormat -> FeatureFormulaColourPalette m -> AST g a -> FeatureTrace g a -> Doc (Attribute m)
printASTWithTrace OutputFormat
{ codeStyle = codestyle
, traceStyle = tracestyle
, traceDisplay = tracedisplay
, withTraceLines = withtracelines
}
featureColourPalette tree trace =
let
nodePrint n = case tracestyle of
None -> pretty.removeQuotes.show $ value n
Colour -> paint (trace n) $ removeQuotes.show $ value n
Text -> pretty $ concat ["<", NullPropositions.prettyPrint $ trace n, ">", removeQuotes.show $ value n]
stringPrint n s = case tracestyle of
Colour -> paint (trace n) s
_ -> pretty s
indentGenerator n i = if tracestyle == Colour && tracedisplay == Trace && withtracelines && optionaltype n == Optional
then mappend (paint (trace n) "|") (pretty $ genIndent (i-1))
else pretty $ genIndent i
paint formula = (annotate (foreground $ featureColourPalette formula)).pretty
in
case codestyle of
ShowAST -> (case tracestyle of
None -> pretty.show
Colour -> Tree.prettyPrint 0 pretty (\n -> paint (trace n) $ show n)
Text -> pretty.(FeatureTrace.prettyPrint trace)) tree
ShowTikz -> pretty $ astToTikzWithTraceDefault (trace, tree)
ShowCode -> showCodeAs mempty indentGenerator stringPrint nodePrint tree
-- | Prints the given list of versions that were produced from the given example.
printTraces :: (MonadColorPrinter m, Grammar g, ASTPrettyPrinter g, Show a, Eq a) =>
OutputFormat -> Example m g a -> [Version g a] -> Doc (Attribute m)
printTraces format example tracesAndTrees =
let
featureColourPalette = colours example
tracedisplay = traceDisplay format
toPC = \trace tree -> if tracedisplay == PC then pc tree trace else trace
in
mappend (mappend (pretty "\n") $ annotate (background red) $ pretty $ intercalate "\n " [
"Running "++name example
-- "codeStyle = "++show codestyle,
-- "traceDisplay = "++show tracedisplay,
-- "traceStyle = "++show tracestyle
])
$ flip foldr
mempty
(\((edit, fc), (trace, tree)) s ->
mconcat [
hardline,
hardline,
pretty $ concat ["==== Run ", show edit, " under context = "],
annotate (foreground $ featureColourPalette fc) $ pretty $ NullPropositions.prettyPrint fc,
pretty $ " giving us ====",
hardline,
printASTWithTrace format featureColourPalette tree (toPC trace tree),
s])
$ zip
-- We have to do this as the first entry in tracesAndTrees will be the initial state of the program
(alsoShowInitialStateInHistory (history example))
tracesAndTrees
-- | Helper function to show the initial state of the given history in 'printTraces'.
-- Prepends an identity edit and dummy 'FeatureContext'.
-- The context could be anything and thus is set to 'Nothing' (/null/).
alsoShowInitialStateInHistory :: History g a -> History g a
alsoShowInitialStateInHistory h = (edit_identity, Nothing):h
-- | Prints truthtables for common operators in 'PropositionalFormula's and 'NullableFormula's (not, and, or, implies, equiv)
showTruthtables :: IO()
showTruthtables = withTerminal $ runTerminalT $
do
headline "Propositional Logic"
putDoc.pretty $ generatetruthtablesfor (lvalues :: [PropositionalFormula Void])
putDoc $ hardline <+> hardline
headline "Ternary Logic With Null"
putDoc.pretty $ generatetruthtablesfor (lvalues :: [NullableFormula Void])