Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Class diagram extraction Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica [email protected] 16/03/2007 Class diagram extraction 1 Exercise Given a Java program build the Class Diagram (associations, extends, attributes) of it. Class A int x, y Class B Class C extend B Aa 16/03/2007 C a extends uses A x, y Class diagram extraction B 2 Dotty Dotty is a customizable graph editor. The dotty language is intuitive. http://hoagland.org/Dot.html digraph g { A [shape = “record”, label = “{A | x, y}”]; C [shape = “record”, label = “{C | a}”]; C -> A [label=“uses”]; C -> B [label=“extends”]; } 16/03/2007 Class diagram extraction 3 Exercise: step by step 1. 2. 3. 4. 5. 6. 7. 8. Give a look at http://hoagland.org/Dot.html Download dotty at http://www.graphviz.org/Download_windows.php Install Dotty Download the java grammar http://sra.itc.it/people/ceccato/couses/lsa Download the dotty grammar http://sra.itc.it/people/ceccato/couses/lsa Test it with small examples Implement in TXL the “getClassNames.txl” (print class names) Implement in TXL the “TransformClassNamesInDotty.txl” (implement dot.Grm) 16/03/2007 Class diagram extraction 4 The “Dot” language Dot is a language that can be used to draw graphs and directed graphs. The syntax is very intuitive Download the language manual here: http://www.graphviz.org/ digraph a -> a -> b -> b -> } 16/03/2007 g b c c d { ; ; ; ; a d b c Class diagram extraction 5 The “Dot” language digraph G { main -> parse -> execute; main -> init; main -> cleanup; execute -> make_string; execute -> printf init -> make_string; main -> printf; execute -> compare; } 16/03/2007 Class diagram extraction 6 The “Dot” language digraph G { a -> b; b -> c; b -> d [style="dashed"]; a [shape="box", color="blue",style="filled"]; c [label="hello world"]; d [shape="record", label="{1|2|3}"]; e [shape="record", label="a|b|c\nd|{e|f}"]; } 16/03/2007 Class diagram extraction 7 Reverse Engineering TXL can be used to extract design information from the code. Such information is then translated in Dot and then visualized B class A { int x, y; } class B { } A class C extends B { A a; } 16/03/2007 int x int y Class diagram extraction C Aa 8 Just one source file Join all the source files in one huge file Not always possible, because for some languages source file names have semantics (e.g. Java). When possible it causes loss of the information about the actual source location, loss of modularity. class A{ int x, y; } class B { } class C extends B{ A a; } test.java 16/03/2007 Class diagram extraction 9 Reverse Engineering Extract the UML class diagram using TXL and represent it in Dot: B class A { int x, y; } class B { } A int x int y class C extends B { A a; } 16/03/2007 Class diagram extraction C Aa 10 class A{ int x, y; } class B { } class C extends B{ A a; } digraph CLASS { node [shape = "record"]; A [label = "A"] ; B [label = "B"] ; C [label = "C"] ; C -> B ; C -> A ; } 16/03/2007 Class diagram extraction 11 class A{ int x, y; } class B { } class C extends B{ A a; } digraph CLASS { node [shape = "record"]; A [label = "A"] ; B [label = "B"] ; C [label = "C"] ; C -> B [arrowhead = "vee"] ; C -> A [arrowhead = "onormal"]; } 16/03/2007 Class diagram extraction 12 class A{ int x, y; } class B { } class C extends B{ A a; } digraph CLASS { rankdir = "BT"; node [shape = "record"]; A [label = "{A|int x;\nint y;}"] ; B [label = "{B}"] ; C [label = "{C|A a;}"] ; C -> B [arrowhead = "vee"]; C -> A [arrowhead = "onormal"]; } 16/03/2007 Class diagram extraction 13 Exercise 1. 2. 3. 4. Extract the class manes from one small example. Extract the class manes from the big case study. Extract the UML class diagram from the small example. Extract the UML class diagram from the case study: 16/03/2007 Generalization (“extends” relations) Associations (“contains” relations) Attributes (fields) Dependencies (“uses” relations) Operations (method) Class diagram extraction 14 UML a -> b [arrowhead = "onormal"]; a -> b [arrowhead = "vee"] ; Generalization: relation between a general class and a more specific one (subclass or child) Association: structural relationship (a class is used as an attribute) Dependency: using relationship (a class is used in an operation signature) a -> b [arrowhead = "vee", style="dashed"]; 16/03/2007 Class diagram extraction 15 Java.Grm Dotty.Grm define program [repeat java_source] end define define program [repeat graph] end define define java_source] [package_declaration] end define define graph [opt 'strict] 'graph [id] '{ [repeat stmt] '} | [opt 'strict] 'digraph [id] '{ [repeat stmt] '} end define define package_declaration [opt package_header] [repeat import_declaration] [repeat type_declaration] end define define stmt [attrStmt] [opt ';] | [nodeStmt] [opt ';] define package_header | [edgeStmt] [opt ';] 'package [package_name] '; | [subgraph] [opt ';] end define | [id] '= [id] [opt ';] end define 16/03/2007 Class diagram extraction 16 Java.Grm Dotty.Grm include “Java.Grm” define java_program define dotty_program include “Dotty.Grm” [repeat java_source] [repeat graph] end define end define define program [java_program] define java_source] define graph | [dotty_program] [package_declaration]end define [opt 'strict] 'graph [id] end define '{ [repeat stmt] '} | [opt 'strict] 'digraph [id] define package_declaration '{ [repeat stmt] '} rule main [opt package_header] end define replace [program] [repeat import_declaration] [repeat type_declaration] code [java_program] define stmt … end define [attrStmt] [opt ';] construct graph [dotty_program] | [nodeStmt] [opt ';] … define package_header | [edgeStmt] [opt ';] by 'package [package_name] '; | [subgraph] [opt ';] graph end define | [id] '= [id] [opt ';] end rule end define 16/03/2007 Class diagram extraction 17