DSL & C#
" E V E R Y T H I N G I S B E T T E R W I T H B L U E T O O T H " D S L
T H E B I G B A N G T H E O R Y
WHAT IS DSL?
• DSL – Domain Specific Language
SIMPLE EXAMPLE
WORKFLOW DSL
WORKFLOW DSL
DSL ALGORITHM VISUALIZATION
DSL FOR GUI TESTING
DSL MESS
DSL MESS FIXING
GRAPHVIZ
GRAPHVIZ
DSL PROS & CONS
PROS
• Close focus on the domain area
– Own abstractions
– Specialized Syntax (till to jargon
words)
– Tweaked errors handling
– Specific tools
CONS
• Lack of support for common things,
such as math expressions
• We need to study it
• No one wants to study bad DSLs
DSL MICRO
INTERNAL DSL
A L S O K N O W N A S E M B E D D E D D S L
EXAMPLES
C# BUILDER PATTERN
C# BUILDER PATTERN
G R A P H B U I L D E R
NB! Return types
E D G E B U I L D E R
NB!
Style Return type!
D O N E !
Graph created successfully!
REVIEW GRAPH CREATION SYNTAX
INTERNAL DSL
->
EXTERNAL DSL
M A G I C T R A N S F O R M AT I O N
NUGET THEM ALL
DYNAMIC GRAPH VERTICES QUERY
DYNAMIC EXPRESSION API
EXTERNAL DSL
REGULAR EXPRESSION EDITOR
REQUIREMENTS
• Write complex regular expressions
• Reuse regular expression parts in other regular expressions
• Test regular expressions and theirs parts
PARTS DECLARATION
COMPOSING REGULAR EXPRESSION
ADDING BLACKJACK
ADDING … ONE MORE BLACKJACK
ADDING … AND MORE
THE END. TO BE CONTINUE
Author: Dmitry Dorogoy
Emaii: dorogoj (dog) live.ru
https://dimonsmart.wordpress.com/
https://twitter.com/dimonsmart
https://www.facebook.com/DimonSmart

C# DSL (eng)

Editor's Notes

  • #3 DSL – Domain Specific Language. Another words – language specifically designed for specific scope. Unlike most general-purpose programming languages that are familiar to all, specialized languages have a very narrow focus.
  • #4 The simplest examples of DSL are: SQL - a database-specific language, Air tickets search-search language, etc.
  • #5 Often, the desire to create a specific tool for a specific area results in the construction of a graphic editor to indicate connections and relationships. In many cases, this approach is very good, but not at all.
  • #6 Sometimes the desire to add a little graphics to something very similar to the general purpose language begins to compete with common sense. Notice how much more complex the definition of a simple condition has become. If you decided to implement DSL in your project. It must be profitable. In other words do not do graphical DSL only because other do it. In case shown – the graphical representation is a complete disaster. It not only make a mess but is consume more space then direct English condition (if then else). Simple off topic. Do you know why Microsoft tiles menu with life tiles is not always good idea? IMHO the main disadvantage is impossibility to remember application icon. For example for photos application. Why I remember this? The graphical representation for DSL not always a bad idea. But remember the goal of this presentation. One of possible goals is simple presentation for complicated things. Have you ever seen a wall near a database developer? What is on that wall? A database diagram! And database developer could always remember where Clients database located. Developer used his visual memory and imagination to visualize a huge database structure. And in this case graphical representation – is a deal. But! One simple thought. What happened if you rearrange tables on this DB diagram and put it back to the wall?
  • #7 But in some cases, visualization can be useful. For example, to show high-level diagrams, or algorithms block-scheme.
  • #8 Another very good example of DSL – is graphical and text combined DSL. It looks like a “visual sugar” but I think it’s more then just a sugar. This algorithm representation change the way of thinking about algorithm. Try to imagine this example without images. Is it the same DSL? I suppose – no.
  • #9 Often DSL is not just a language, but a language tied to a specific development environment. For example, it is very convenient to use the programming language to debug electrical circuits, with hints about the pin assignment. But does the diagram in the middle make the language clearer than the text? And next discussion point – what do you think about complexity? Is it easier to support both text and graphical representation then only one of them? And what about equivalence? Is it always possible to represent graphically any program text and vice versa?
  • #10 Speaking about different DSL representations it is impossible not to mention the need to transform between representations. And some development environments provide this function. Usually this is only a one way road. May be many of you remember html file and browser window. This is a good example of one way transformation between text and graphical represntations.
  • #11 A very good example of DSL is the language of graph description.
  • #12 Don't think it's a very primitive language. At due diligence, it will be very powerful tool.
  • #13 Cons and pros. I hope all points is clear enough. I want you to focus on “support for common things”
  • #14 This is a screenshot of the very popular graphic editor - CorelDraw. Note how easy it is to divide page size by 2. This is the same as fold paper sheet. Or add 10 mm to a page border. It’s sometime needed for magazine prepress operations. You also could easily transform between different units of measurement and do simple mathematical operations. And Yes! It's a small, but very powerful and useful DSL.
  • #16 An internal DSL is a DSL based on a general purpose programming language. In fact, it is just a very well-designed library, which hides language specific details and show domain specific object and functions to the user. it's not a 100% accurate description but for now it’s ok. Let's take an example of how you can implement a small piece of internal DSL. As an example, let's take the graph description language.
  • #17 Let's describe the classes in our subject area. Vertex. It’ll be described only with Label just for simplicity.
  • #18 Graph edges. We assume that our graph is oriented. So we describe edges as From and To vertex, and edge style. For simplicity we treat Style just as string.
  • #19 And now let’s apply the popular builder pattern. In our case, it fits just perfect. For those who are not familiar with the pattern - pay attention to class return types. I used two approaches here for demonstrate both. In the first case, the class returns itself. And in the second case, the class returns another builder-class.
  • #20 The second builder contains the edge creation features. And the latest function sets the edge style and returns the previous builder. This function could be spited into two. One sets the style and the other completes the edge creation and returns the previous builder.
  • #21 As a result, we created a graph using syntax specifically focused on graph creation.
  • #22 Let’s review graph creation syntax. You could hardly find C# here. It looks like special graph creation language, Of course we see a typical C# function chain syntax. But show this to your school geometry teacher. I expect everything will be understandable.
  • #23 It's all good, but it was an internal DSL. Of course, this syntax is nice to use. And now you could create any graphs with simple builder syntax. Next goal is search through the graph. In other word let’s try to give uses a function to search through the already created graph without digging into C# programming environment. And now you can do a simple but very spectacular trick! NB! We just switch a target goal just for demo. Similar trick could be used for graph creation.
  • #24 First thing we need is a System.Linq.Dynamic nuget package
  • #25 And now look how we could use an internal and external DSL side by side. In the first example we filter vertices by Key which contains the letter ‘e’. Our Graph class contains vertices dictionary, so we use Key. It’s easy to get rid of this just by exposing a Vertices collection with a specially designed public property. And look at the second example. We simulate user input as the userInput variable. And this variable contains exactly the same expression we used in the first code part. And final action – we compare these two approaches results and get exactly the same vertices. At that moment I’m very close to my presentation goal. If you are dreaming about giving your users the ability to write some queries by themselves. Just try to give them MVP (minimal valuable product). I showed you how easily it could be done. My solution is not for a public and production, but its very fast and simple for in-house implementation and for research. You should not dive into external dsl implementation without a detailed research!
  • #26 In case you start thinking about sql injection or something similar, the mentioned Linq.Dynamic library gives you a solution with passing parameters into the query as parameters and not as part of string. If you are interested – just take a look.
  • #27 And now you are ready to hear the truth about a big, “real” DSL, It’s really complicated. It’s true. And at the same time it’s very interesting. Just for example take a look at the screenshot. You can see a C# based library with a beautiful name Irony. And at the left window you can see a simple C# program. At the right window you can find a parsing tree for this program, It seems big, but if you carefully look through line by line you will find it easy to understand. At the first sight it’s a bit verbose. But it only depends on the grammar complexity and variability. Your own grammar may look more simple. My presentation’s goal is not to dive deep into real grammars and parsers. I just want to inspire you to look at DSL’s and try.
  • #28 Now it’s time to show you a real and sophisticated DSL sample. This sample is a visual regular expression editor with Black Jack and own regular expression syntax!
  • #29 Let me explain the initial requirements. First of all we need to test a regular expression against a list of samples. Both positive and negative samples. We might want to extend these samples by some expressions just for exploration without marking them as positive or negative. Second, we need building blocks for composing complex regular expressions. And we would like to test these building blocks separately.
  • #30 Let me describe an regular expression part syntax. It is a declaration with name and regular expression text. Note that I use a freeform expression format that allows me to arrange regular expression by lines and provide comments for separate lines if needed. On the slide I declare two building blocks. The first is for Number named (try to guess ) Number, And the second one is for event name having two forms. Short form ”DevDay” or long form “DeveloperDay”. At this moment I have only building blocks. My next step is to compose a whole regular expression.
  • #31 Just for a simple demonstration I suppose that we want to match number and event name or name and number. Pay your attention at the screenshot. It looks quite simple and readable. At this moment my regular expression syntax is more powerful than many others. But where is a Black Jack?
  • #32 Now it’s time to add a syntax for testing the regular expressions. A good example of syntax extension is embedding tests into the syntax. The Accept keyword is for texts that must be matched. Reject - for texts that should not be matched. And Sample – for a text we just want to track for curiosity. It looks like a real black jack. And now it is time for …. Another black jack 
  • #33 We want more than just to see our tests status. We also want to see the exact matched text. Cool? If not, we can even see a predicted execution time and the complexity of the resulted regular expression. By the way, there can be some pathological regular expressions that perform very slowly and with a significant memory consumption. And the difference between normal and pathological regular expressions may be just with one symbol! So it’s important to not only know the matching status but also the matching speed. Speed? You should note how I measure the matching speed only for short samples. It’s useful for edge cases. But what about a normal flow?
  • #34 It’s my DSL and I can improve it! Testing speed of the short samples is a good idea but we can do even better. I’ve added a testing against some typical patterns, English text, Numbers (spreadsheet simulation), mixed text and numbers and something close to random binary file.
  • #35 Feel free to contact me. If I’m not answering just try twitter 