11package org.svis.generator.famix
2-
32import java.util.List
43import org.eclipse.emf.mwe.core.WorkflowContext
54import org.eclipse.emf.mwe.core.issues.Issues
@@ -42,18 +41,12 @@ import org.svis.xtext.famix.MethodType
4241import org.svis.generator.rd.RDSettings
4342import org.neo4j.graphdb.GraphDatabaseService
4443import org.svis.lib.database.Database
45- import org.svis.lib.database.DBConnector
4644import org.neo4j.graphdb.Node
47- import org.neo4j.graphdb.Direction
48- import org.neo4j.graphdb.traversal.Uniqueness
4945import org.svis.xtext.famix.FAMIXElement
50- import org.neo4j.graphdb.Relationship
5146import org.eclipse.emf.common.util.EList
52- import org.svis.generator.famix.FAMIXSettings.FamixParser
5347
5448class Famix2Famix extends WorkflowComponentWithConfig {
5549 var GraphDatabaseService graph
56- var DBConnector dbConnector
5750 val Set<FAMIXNamespace > rootPackages = newLinkedHashSet
5851 val Set<FAMIXNamespace > subPackages = newLinkedHashSet
5952 val List<FAMIXStructure > allStructures = newArrayList
@@ -68,176 +61,26 @@ class Famix2Famix extends WorkflowComponentWithConfig {
6861
6962 override protected invokeInternal (WorkflowContext ctx , ProgressMonitor monitor , Issues issues ) {
7063 log. info(" Famix2Famix has started." )
71- // FAMIX to DB
72- // DB to FAMIX
73- if (FAMIXSettings . FAMIX_PARSER == = FamixParser :: JQA_BYTECODE ) {
74- val famixRoot = famixFactory. createRoot
75- val famixDocument = famixFactory. createDocument
76- famixRoot. document = famixDocument
77- graph = Database :: getInstance(FAMIXSettings :: DATABASE_NAME )
78- dbConnector = new DBConnector (graph)
79-
80- // Create Namespaces
81- val namespaces = newHashMap
82- val Map<Long , FAMIXStructure > structures = newHashMap
83- val methods = newHashMap
84- val attributes = newHashMap
85- val famixEvaluator = new FamixEvaluator ()
86- var tx = graph. beginTx
87- try {
88- // get roots packages
89- val result = graph. execute(" MATCH (n:Package) WHERE NOT (n)<-[:CONTAINS]-(:Package)RETURN n" )
90- result. forEach [ row |
91- val rootnode = row. get(" n" ) as Node
92- // traverse though complete graph
93- graph. traversalDescription. relationships(Rels . CONTAINS , Direction . OUTGOING ). relationships(
94- Rels . DECLARES , Direction . OUTGOING ) // filters relevant relationships
95- . uniqueness(Uniqueness . NONE ). evaluator(famixEvaluator) // filters relevant nodes
96- . traverse(rootnode). forEach [ path |
97- // this loop is executed for every path
98- // for every end node an own path exists
99- // therefore we are just interested in the end node, the other nodes have already been handled
100- val node = path. endNode
101- var Node parent = null
102- if (path. length != 0 ) {
103- parent = path. nodes. get(path. length - 1 )
104- }
105- if (node. hasLabel(DBLabel . PACKAGE )) {
106- var FAMIXNamespace parentNamespace = null
107- if (parent !== null ) {
108- parentNamespace = namespaces. get(parent. id)
109- }
110- val namespace = createNamespace(node, parentNamespace)
111- namespaces. put(node. id, namespace)
112- famixDocument. elements + = namespace
113- } else if (node. hasLabel(DBLabel . CLASS ) || node. hasLabel(DBLabel . INTERFACE )) {
114- var FAMIXElement container
115- if (parent. hasLabel(DBLabel . PACKAGE )) {
116- container = namespaces. get(parent. id)
117- }
118- if (isStructure(parent)) {
119- container = structures. get(parent. id)
120- }
121- val class = createClass(node, container)
122- structures. put(node. id, class)
123- famixDocument. elements + = class
124- } else if (node. hasLabel(DBLabel . ENUM )) {
125- var FAMIXElement container
126- if (parent. hasLabel(DBLabel . PACKAGE )) {
127- container = namespaces. get(parent. id)
128- }
129- if (isStructure(parent)) {
130- container = structures. get(parent. id)
131- }
132- val enum = createEnum(node, container)
133- structures. put(node. id, enum)
134- famixDocument. elements + = enum
135- } else if (node. hasLabel(DBLabel . ANNOTATION )) {
136- var FAMIXElement container
137- if (parent. hasLabel(DBLabel . PACKAGE )) {
138- container = namespaces. get(parent. id)
139- }
140- if (isStructure(parent)) {
141- container = structures. get(parent. id)
142- }
143- val annotation = createAnnotation(node, container)
144- structures. put(node. id, annotation)
145- famixDocument. elements + = annotation
146- } else if (node. hasLabel(DBLabel . METHOD )) {
147- var FAMIXStructure container
148- if (isStructure(parent)) {
149- container = structures. get(parent. id)
150- }
151- val fileAnchor = famixFactory. createFAMIXFileAnchor
152- fileAnchor. filename = parent. getProperty(" sourceFileName" ) as String
153- val method = createMethod(node, container, fileAnchor)
154- methods. put(node. id, method)
155- famixDocument. elements + = method
156- } else if (node. hasLabel(DBLabel . FIELD )) {
157- var FAMIXStructure container
158- if (isStructure(parent)) {
159- container = structures. get(parent. id)
160- }
161- val fileAnchor = famixFactory. createFAMIXFileAnchor
162- fileAnchor. filename = parent. getProperty(" sourceFileName" ) as String
163- val attribute = createAttribute(node, container, fileAnchor)
164- attributes. put(node. id, attribute)
165- famixDocument. elements + = attribute
166- }
167- ]
168- ]
169- graph. execute(" MATCH p=()-[r:INVOKES]->() RETURN r" ). forEach [ row |
170- val rel = row. get(" r" ) as Relationship
171- if (methods. containsKey(rel. startNode. id) && methods. containsKey(rel. endNode. id)) {
172- val invocation = famixFactory. createFAMIXInvocation
173- val senderRef = famixFactory. createIntegerReference
174- val receiverRef = famixFactory. createIntegerReference
175- senderRef. ref = methods. get(rel. startNode. id)
176- receiverRef. ref = methods. get(rel. endNode. id)
177- invocation. sender = senderRef
178- invocation. candidates = receiverRef
179- famixDocument. elements + = invocation
180- }
181- ]
182- graph. execute(" MATCH p=()-[r:READS|WRITES]->() RETURN r" ). forEach [ row |
183- val rel = row. get(" r" ) as Relationship
184- if (methods. containsKey(rel. startNode. id) && attributes. containsKey(rel. endNode. id)) {
185- val access = famixFactory. createFAMIXAccess
186- val attributeRef = famixFactory. createIntegerReference
187- val accessorRef = famixFactory. createIntegerReference
188- attributeRef. ref = attributes. get(rel. endNode. id)
189- accessorRef. ref = methods. get(rel. startNode. id)
190- access. variable = attributeRef
191- access. accessor = accessorRef
192- famixDocument. elements + = access
193- }
194-
195- ]
196- graph. execute(" MATCH p=()-[r:EXTENDS]->() RETURN r" ). forEach [ row |
197- val rel = row. get(" r" ) as Relationship
198- if (structures. containsKey(rel. startNode. id) && structures. containsKey(rel. endNode. id)) {
199- val inheritance = famixFactory. createFAMIXInheritance
200- val subclassRef = famixFactory. createIntegerReference
201- val superclassRef = famixFactory. createIntegerReference
202- subclassRef. ref = structures. get(rel. startNode. id)
203- superclassRef. ref = structures. get(rel. endNode. id)
204- inheritance. subclass = subclassRef
205- inheritance. superclass = superclassRef
206- famixDocument. elements + = inheritance
207- }
208- ]
209- tx. success
210- } finally {
211- tx. close
212- }
213-
64+ val resourceList = ctx. get(" famix" ) as List<?>
65+ if (resourceList. size == 1 ) {
66+ val famixRoot = run((resourceList as List<Root > ). head)
21467 ctx. set(" famix" , famixRoot)
21568 val resource = new ResourceImpl ()
21669 resource. contents + = famixRoot
21770 ctx. set(" metadata" , resource)
21871 } else {
219- val resourceList = ctx. get(" famix" ) as List<?>
220- if (resourceList. size == 1 ) {
221- val famixRoot = run((resourceList as List<Root > ). head)
222- ctx. set(" famix" , famixRoot)
223- val resource = new ResourceImpl ()
224- resource. contents + = famixRoot
225- ctx. set(" metadata" , resource)
226- } else {
227- val resources = newLinkedList
228- for (famixRoot : resourceList as List<LazyLinkingResource > ) {
229- famixRoot. load(null )
230- while (famixRoot. loading) {
231- log. info(" famixRoot " + famixRoot. URI + " is loading." )
232- }
233- if (famixRoot. isLoaded) {
234- famixRoot. contents + = run(famixRoot. contents. head as Root )
235- }
236- resources + = famixRoot
72+ val resources = newLinkedList
73+ for (famixRoot : resourceList as List<LazyLinkingResource > ) {
74+ famixRoot. load(null )
75+ while (famixRoot. loading) {
76+ log. info(" famixRoot " + famixRoot. URI + " is loading." )
77+ }
78+ if (famixRoot. isLoaded) {
79+ famixRoot. contents + = run(famixRoot. contents. head as Root )
23780 }
238- ctx . set( " famix " , resources)
81+ resources + = famixRoot
23982 }
240-
83+ ctx . set( " famix " , resources)
24184 }
24285 log. info(" Famix2Famix has finished." )
24386 }
@@ -721,159 +564,6 @@ class Famix2Famix extends WorkflowComponentWithConfig {
721564 structures. removeAll(duplicateAnnotationTypes)
722565 }
723566
724- def createNamespace (Node node , FAMIXNamespace parent ) {
725- val namespace = famixFactory. createFAMIXNamespace
726- val id = node. id. toString
727- namespace. name = id
728- namespace. id = id
729- namespace. value = node. getProperty(" name" ) as String
730- namespace. fqn = node. getProperty(" fqn" ) as String
731- if (parent !== null ) {
732- var ref = famixFactory. createIntegerReference
733- ref. ref = parent
734- namespace. parentScope = ref
735- }
736- return namespace
737- }
738-
739- def createClass (Node node , FAMIXElement parent ) {
740- val class = famixFactory. createFAMIXClass
741- class. name = node. id. toString
742- class. value = node. getProperty(" name" ) as String
743- class. fqn = node. getProperty(" fqn" ) as String
744- class. id = node. getProperty(" md5" ) as String
745- addModifiers(node, class. modifiers)
746- var ref = famixFactory. createIntegerReference
747- ref. ref = parent
748- class. container = ref
749- val anchorRef = famixFactory. createIntegerReference
750- val classRef = famixFactory. createIntegerReference
751- val fileAnchor = famixFactory. createFAMIXFileAnchor
752- fileAnchor. filename = node. getProperty(" sourceFileName" ) as String
753- anchorRef. ref = class
754- classRef. ref = fileAnchor
755- class. type = anchorRef
756- fileAnchor. element = classRef
757- return class
758- }
759-
760- def createEnum (Node node , FAMIXElement parent ) {
761- val enum = famixFactory. createFAMIXEnum
762- enum. name = node. id. toString
763- enum. id = node. id. toString
764- enum. value = node. getProperty(" name" ) as String
765- enum. fqn = node. getProperty(" fqn" ) as String
766- var ref = famixFactory. createIntegerReference
767- ref. ref = parent
768- enum. container = ref
769- val anchorRef = famixFactory. createIntegerReference
770- val enumRef = famixFactory. createIntegerReference
771- val fileAnchor = famixFactory. createFAMIXFileAnchor
772- fileAnchor. filename = node. getProperty(" sourceFileName" ) as String
773- anchorRef. ref = enum
774- enumRef. ref = fileAnchor
775- enum. sourceAnchor = anchorRef
776- fileAnchor. element = enumRef
777- return enum
778- }
779-
780- def createAnnotation (Node node , FAMIXElement parent ) {
781- val annotation = famixFactory. createFAMIXAnnotationType
782- annotation. name = node. id. toString
783- annotation. id = node. id. toString
784- annotation. value = node. getProperty(" name" ) as String
785- annotation. fqn = node. getProperty(" fqn" ) as String
786- var ref = famixFactory. createIntegerReference
787- ref. ref = parent
788- annotation. container = ref
789- val anchorRef = famixFactory. createIntegerReference
790- val annoRef = famixFactory. createIntegerReference
791- val fileAnchor = famixFactory. createFAMIXFileAnchor
792- fileAnchor. filename = node. getProperty(" sourceFileName" ) as String
793- anchorRef. ref = annotation
794- annoRef. ref = fileAnchor
795- annotation. sourceAnchor = anchorRef
796- fileAnchor. element = annoRef
797- return annotation
798- }
799-
800- def createMethod (Node node , FAMIXStructure parent , FAMIXFileAnchor fileAnchor ) {
801- val method = famixFactory. createFAMIXMethod
802- method. name = node. id. toString
803- method. id = node. id. toString
804- if (node. hasProperty(" name" )) {
805- method. value = node. getProperty(" name" ) as String
806- }
807- if (node. hasProperty(" effectiveLineCount" )) {
808- val numberOfStatements = node. getProperty(" effectiveLineCount" ) as Long
809- method. numberOfStatements = numberOfStatements. intValue
810- val cyclomaticComplexity = node. getProperty(" cyclomaticComplexity" ) as Long
811- method. cyclomaticComplexity = cyclomaticComplexity. intValue
812- val firstLineNumber = node. getProperty(" firstLineNumber" ) as Long
813- fileAnchor. startline = firstLineNumber. intValue
814- val lastLineNumber = node. getProperty(" lastLineNumber" ) as Long
815- fileAnchor. endline = lastLineNumber. intValue
816- }
817- var signature = node. getProperty(" signature" ) as String
818- val index = signature. indexOf(" " ) + 1
819- signature = signature. substring(index)
820- method. signature = signature
821- method. fqn = parent. fqn + " ." + signature
822- addModifiers(node, method. modifiers)
823- var ref = famixFactory. createIntegerReference
824- ref. ref = parent
825- method. parentType = ref
826- var anchorRef = famixFactory. createIntegerReference
827- anchorRef. ref = method
828- var methodRef = famixFactory. createIntegerReference
829- methodRef. ref = fileAnchor
830- fileAnchor. element = methodRef
831- method. sourceAnchor = anchorRef
832- return method
833- }
834-
835- def createAttribute (Node node , FAMIXElement parent , FAMIXFileAnchor fileAnchor ) {
836- val attribute = famixFactory. createFAMIXAttribute
837- attribute. name = node. id. toString
838- attribute. id = node. id. toString
839- if (node. hasProperty(" name" )) {
840- attribute. value = node. getProperty(" name" ) as String
841- }
842- addModifiers(node, attribute. modifiers)
843- var ref = famixFactory. createIntegerReference
844- ref. ref = parent
845- attribute. parentType = ref
846- val anchorRef = famixFactory. createIntegerReference
847- val attributeRef = famixFactory. createIntegerReference
848- anchorRef. ref = attribute
849- attributeRef. ref = fileAnchor
850- fileAnchor. element = attributeRef
851- attribute. sourceAnchor = anchorRef
852- return attribute
853- }
854-
855- def isStructure (Node node ) {
856- if (node. hasLabel(DBLabel . TYPE )) {
857- return true
858- }
859- }
860-
861- def addModifiers (Node node , EList<String > modifiers ) {
862- if (node. hasProperty(" visibility" )) {
863- modifiers + = node. getProperty(" visibility" ) as String
864- }
865- if (node. hasProperty(" final" )) {
866- if (node. getProperty(" final" ) == = true ) {
867- modifiers + = " final"
868- }
869- }
870- if (node. hasProperty(" abstract" )) {
871- if (node. getProperty(" abstract" ) == = true ) {
872- modifiers + = " abstract"
873- }
874- }
875- }
876-
877567 def Node toDB (FAMIXNamespace namespace , Node parent ) {
878568 val node = graph. createNode(Labels . Package )
879569 node. setProperty(" name" , namespace. value)
0 commit comments