1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Save and load a `Model` to/from a Neo4j graph.
17 # Nit models are composed by MEntities.
18 # This module creates NeoNode for each MEntity found in a `Model` and save them
19 # into Neo4j database.
23 # NeoNodes can also be translated back to MEntities to rebuild a Nit `Model`.
25 # Structure of the nit `Model` in the graph:
27 # Note : Any null or empty attribute will not be saved in the database.
29 # For any `MEntity` (in addition to specific data):
31 # * labels: model name (`model_name`) and `MEntity`.
32 # * `name`: short (unqualified) name.
33 # * `mdoc`: JSON array representing the associated Markdown documentation
36 # Note : All nodes described here are MEntities.
40 # * labels: `MProject`, `model_name` and `MEntity`.
41 # * `(:MProject)-[:ROOT]->(:MGroup)`: root of the group tree.
45 # * labels: `MGroup`, `model_name` and `MEntity`.
46 # * `full_name`: fully qualified name.
47 # * `(:MGroup)-[:PROJECT]->(:MProject)`: associated project.
48 # * `(:MGroup)-[:PARENT]->(:MGroup)`: parent group. Does not exist for the root
50 # * `(:MGroup)-[:DECLARES]->(:MModule)`: modules that are direct children of
52 # * `(:MGroup)-[:NESTS]->(:MGroup)`: nested groups that are direct children of
57 # * labels: `MModule`, `model_name` and `MEntity`.
58 # * `full_name`: fully qualified name.
59 # * `location`: origin of the definition. SEE: `Location.to_s`
60 # * `(:MModule)-[:IMPORTS]->(:MModule)`: modules that are imported directly.
61 # * `(:MModule)-[:INTRODUCES]->(:MClass)`: all by classes introduced by this
63 # * `(:MModule)-[:DEFINES]->(:MClassDef)`: all class definitons contained in
68 # * labels: `MClass`, `model_name` and `MEntity`.
69 # * `full_name`: fully qualified name.
70 # * `kind`: kind of the class (`interface`, `abstract class`, etc.)
71 # * `visibility`: visibility of the class.
72 # * `parameter_names`: JSON array listing the name of each formal generic
73 # parameter (in order of declaration).
74 # * `(:MClass)-[:CLASSTYPE]->(:MClassType)`: SEE: `MClass.mclass_type`
76 # Arguments in the `CLASSTYPE` are named following the `parameter_names`
77 # attribute of the `MClassDef` that introduces the class. A class definition
78 # introduces a class if and only if it has this class as `MCLASS` and
79 # has `is_intro` set to `true`.
83 # * labels: `MClassDef`, `model_name` and `MEntity`.
84 # * `location`: origin of the definition. SEE: `Location.to_s`
85 # * `(:MClassDef)-[:BOUNDTYPE]->(:MClassType)`: bounded type associated to the
87 # * `(:MClassDef)-[:MCLASS]->(:MClass)`: associated `MClass`.
88 # * `(:MClassDef)-[:INTRODUCES]->(:MProperty)`: all properties introduced by
90 # * `(:MClassDef)-[:DECLARES]->(:MPropDef)`: all property definitions in the
91 # classdef (introductions and redefinitions).
92 # * `(:MClassDef)-[:INHERITS]->(:MClassType)`: all declared super-types
96 # * labels: `MProperty`, `model_name` and `MEntity`. Must also have `MMethod`,
97 # `MAttribute` or `MVirtualTypeProp`, depending on the class of the represented
99 # * `full_name`: fully qualified name.
100 # * `visibility`: visibility of the property.
101 # * `is_init`: Indicates if the property is a constructor. Exists only if the
102 # node is a `MMethod`.
103 # * `(:MProperty)-[:INTRO_CLASSDEF]->(:MClassDef)`: classdef that introduces
108 # * labels: `MPropDef`, `model_name` and `MEntity`. Must also have `MMethodDef`,
109 # `MAttributeDef` or `MVirtualTypeDef`, depending on the class of the
110 # represented entity.
111 # * `location`: origin of the definition. SEE: `Location.to_s`.
112 # * `(:MPropDef)-[:DEFINES]->(:MProperty)`: associated property.
114 # Additional attributes and relationship for `MMethodDef`:
116 # * `is_abstract`: Is the method definition abstract?
117 # * `is_intern`: Is the method definition intern?
118 # * `is_extern`: Is the method definition extern?
119 # * `(:MMethodDef)-[:SIGNATURE]->(:MSignature)`: signature attached to the
120 # property definition.
122 # Additional relationship for `MAttributeDef`:
124 # * `(:MAttributeDef)-[:TYPE]->(:MType)`: static type of the attribute,
127 # Additional relationship for `MVirtualTypeDef`:
129 # * `(:MVirtualTypeDef)-[:BOUND]->(:MType)`: type to which the virtual type
130 # is bound in this definition. Exists only if this definition bound the virtual
131 # type to an effective type.
135 # * labels: `MType`, `model_name` and `MEntity`. Must also have `MClassType`,
136 # `MNullableType`, `MVirtualType`, `MRawType` or `MSignature`, depending on the
137 # class of the represented entity.
139 # Additional label and relationships for `MClassType`:
141 # * If it is a `MGenericType`, also has the `MGenericType` label.
142 # * `(:MClassType)-[:CLASS]->(:MClass)`: SEE: `MClassType.mclass`
143 # * `(:MClassType)-[:ARGUMENT]->(:MType)`: type arguments.
145 # Arguments are named following the `parameter_names` attribute of the
146 # `MClass` referred by `CLASS`.
148 # Additional relationship for `MVirtualType`:
150 # * `(:MVirtualType)-[:PROPERTY]->(:MProperty)`: associated property that
151 # determines the type (usually a `MVirtualTypeProp`).
153 # Additional attribute and relationship for `MParameterType`:
155 # * `rank`: position of the parameter (0 for the first parameter).
156 # * `(:MParameterType)-[:CLASS]->(:MClass)`: generic class where the parameter
159 # Additional relationship for `MNullableType`:
161 # * `(:MNullableType)-[:TYPE]->(:MType)`: base type of the nullable type.
163 # Additional attribute and relationship for `MRawType`:
165 # * `text`: JSON array of the parts’ text.
166 # * `(:MRawType)-[:LINK]->(:MTypePart)`: the parts that link to another entity.
168 # Additional attribute and relationships for `MSignature`:
170 # * `parameter_names`: JSON array representing the list of the parameter names.
171 # * `(:MSignature)-[:PARAMETER]->(:MParameter)`: parameters.
172 # * `(:MSignature)-[:RETURNTYPE]->(:MType)`: return type. Does not exist for
175 # In order to maintain the correct parameters order, each `MSignature` node
176 # contains an array of parameter names corresponding to the parameter order in
179 # For example, if the source code contains:
182 # fun foo(a: A, b: B, c: C)
185 # The `MSignature` node will contain a property
186 # `parameter_names = ["a", "b", "c"]` so the MSignature can be reconstructed
187 # with the parameters in the correct order.
191 # * labels: `MParameter`, `model_name` and `MEntity`.
192 # * `is_vararg`: Is the parameter a vararg?
193 # * `rank`: position of the parameter (0 for the first parameter).
194 # * `(:MParameter)-[:TYPE]->(:MType)`: static type of the parameter.
196 # MParameters are also ranked by their position in the corresponding signature.
197 # Rank 0 for the first parameter, 1 for the next one and etc.
201 # * labels: `MTypePart`, `model_name` and `MEntity`.
202 # * `rank`: position in the `MRawType`.
203 # * `(:MTypePart)-[:TARGET]->(:MEntity)`: the target of the link.
206 import doc
::model_ext
210 # Helper class that can save and load a `Model` into a Neo4j database.
215 # Because we use only one Neo4j instance to store all the models,
216 # we need to mark their appartenance to a particular model and avoid loading all models.
218 # The name is used as a Neo label on each created nodes and used to load nodes from base.
219 var model_name
: String
221 # The toolcontext used to init the `NeoModel` tool.
222 var toolcontext
: ToolContext
224 # The Neo4j `client` used to communicate with the Neo4j instance.
225 var client
: Neo4jClient
227 # Fill `model` using base pointed by `client`.
228 fun load
(model
: Model): Model do
229 var nodes
: Array[NeoNode]
231 toolcontext
.info
("Loading project node...", 1)
232 nodes
= client
.nodes_with_labels
([model_name
, "MProject"])
233 for node
in nodes
do to_mproject
(model
, node
)
234 toolcontext
.info
("Loading groups...", 1)
235 nodes
= client
.nodes_with_labels
([model_name
, "MGroup"])
236 for node
in nodes
do to_mgroup
(model
, node
)
237 toolcontext
.info
("Loading modules...", 1)
238 nodes
= client
.nodes_with_labels
([model_name
, "MModule"])
239 for node
in nodes
do to_mmodule
(model
, node
)
240 toolcontext
.info
("Loading classes...", 1)
241 nodes
= client
.nodes_with_labels
([model_name
, "MClass"])
242 for node
in nodes
do to_mclass
(model
, node
)
243 toolcontext
.info
("Loading class definitions...", 1)
244 nodes
= client
.nodes_with_labels
([model_name
, "MClassDef"])
245 for node
in nodes
do to_mclassdef
(model
, node
)
246 toolcontext
.info
("Loading properties...", 1)
247 nodes
= client
.nodes_with_labels
([model_name
, "MProperty"])
248 for node
in nodes
do to_mproperty
(model
, node
)
249 toolcontext
.info
("Loading property definitions...", 1)
250 nodes
= client
.nodes_with_labels
([model_name
, "MPropDef"])
251 for node
in nodes
do to_mpropdef
(model
, node
)
255 # Save `model` in the base pointed by `client`.
256 fun save
(model
: Model) do
257 var nodes
= collect_model_nodes
(model
)
258 toolcontext
.info
("Save {nodes.length} nodes...", 1)
260 var edges
= collect_model_edges
(model
)
261 toolcontext
.info
("Save {edges.length} edges...", 1)
265 # Save `neo_entities` in base using batch mode.
266 private fun push_all
(neo_entities
: Collection[NeoEntity]) do
267 var batch
= new NeoBatch(client
)
268 var len
= neo_entities
.length
271 for nentity
in neo_entities
do
272 batch
.save_entity
(nentity
)
273 if i
== batch_max_size
then
275 sum
+= batch_max_size
276 toolcontext
.info
(" {sum * 100 / len}% done", 1)
277 batch
= new NeoBatch(client
)
286 # How many operation can be executed in one batch?
287 private var batch_max_size
= 1000
289 # Execute `batch` and check for errors.
291 # Abort if `batch.execute` returns errors.
292 private fun do_batch
(batch
: NeoBatch) do
293 var errors
= batch
.execute
294 if not errors
.is_empty
then
300 # Collect all nodes from the current `model`.
301 private fun collect_model_nodes
(model
: Model): Collection[NeoNode] do
302 for mproject
in model
.mprojects
do
304 for mgroup
in mproject
.mgroups
do to_node
(mgroup
)
309 # Collect all edges from the current `model`.
311 # Actually collect all out_edges from all nodes.
312 private fun collect_model_edges
(model
: Model): Collection[NeoEdge] do
313 var edges
= new HashSet[NeoEdge]
314 for node
in nodes
.values
do edges
.add_all
(node
.out_edges
)
318 # Mentities associated to nodes.
320 # The key is the node’s id.
321 private var mentities
= new HashMap[Int, MEntity]
323 # Nodes associated with MEntities.
324 private var nodes
= new HashMap[MEntity, NeoNode]
326 # Get the `NeoNode` associated with `mentity`.
327 # `mentities` are stored locally to avoid duplication.
328 fun to_node
(mentity
: MEntity): NeoNode do
329 if nodes
.has_key
(mentity
) then return nodes
[mentity
]
330 if mentity
isa MProject then return mproject_node
(mentity
)
331 if mentity
isa MGroup then return mgroup_node
(mentity
)
332 if mentity
isa MModule then return mmodule_node
(mentity
)
333 if mentity
isa MClass then return mclass_node
(mentity
)
334 if mentity
isa MClassDef then return mclassdef_node
(mentity
)
335 if mentity
isa MProperty then return mproperty_node
(mentity
)
336 if mentity
isa MPropDef then return mpropdef_node
(mentity
)
337 if mentity
isa MType then return mtype_node
(mentity
)
338 if mentity
isa MParameter then return mparameter_node
(mentity
)
342 # Get the `MEntity` associated with `node`.
343 fun to_mentity
(model
: Model, node
: NeoNode): MEntity do
344 if node
.labels
.has
("MProject") then return to_mproject
(model
, node
)
345 if node
.labels
.has
("MGroup") then return to_mgroup
(model
, node
)
346 if node
.labels
.has
("MModule") then return to_mmodule
(model
, node
)
347 if node
.labels
.has
("MClass") then return to_mclass
(model
, node
)
348 if node
.labels
.has
("MClassDef") then return to_mclassdef
(model
, node
)
349 if node
.labels
.has
("MProperty") then return to_mproperty
(model
, node
)
350 if node
.labels
.has
("MPropDef") then return to_mpropdef
(model
, node
)
351 if node
.labels
.has
("MType") then return to_mtype
(model
, node
)
352 if node
.labels
.has
("MParameter") then return to_mparameter
(model
, node
)
356 # Make a new `NeoNode` based on `mentity`.
357 private fun make_node
(mentity
: MEntity): NeoNode do
358 var node
= new NeoNode
359 nodes
[mentity
] = node
360 node
.labels
.add
"MEntity"
361 node
.labels
.add model_name
362 node
["name"] = mentity
.name
363 if mentity
.mdoc
!= null then node
["mdoc"] = new JsonArray.from
(mentity
.mdoc
.content
)
367 # Build a `NeoNode` representing `mproject`.
368 private fun mproject_node
(mproject
: MProject): NeoNode do
369 var node
= make_node
(mproject
)
370 node
.labels
.add
"MProject"
371 var root
= mproject
.root
373 node
.out_edges
.add
(new NeoEdge(node
, "ROOT", to_node
(root
)))
378 # Build a new `MProject` from a `node`.
380 # REQUIRE `node.labels.has("MProject")`
381 private fun to_mproject
(model
: Model, node
: NeoNode): MProject do
382 var m
= mentities
.get_or_null
(node
.id
.as(Int))
383 if m
isa MProject then return m
385 assert node
.labels
.has
("MProject")
386 var mproject
= new MProject(node
["name"].to_s
, model
)
387 mentities
[node
.id
.as(Int)] = mproject
388 set_doc
(node
, mproject
)
389 mproject
.root
= to_mgroup
(model
, node
.out_nodes
("ROOT").first
)
393 # Build a `NeoNode` representing `mgroup`.
394 private fun mgroup_node
(mgroup
: MGroup): NeoNode do
395 var node
= make_node
(mgroup
)
396 node
.labels
.add
"MGroup"
397 node
["full_name"] = mgroup
.full_name
398 var parent
= mgroup
.parent
399 node
.out_edges
.add
(new NeoEdge(node
, "PROJECT", to_node
(mgroup
.mproject
)))
400 if parent
!= null then
401 node
.out_edges
.add
(new NeoEdge(node
, "PARENT", to_node
(parent
)))
403 for mmodule
in mgroup
.mmodules
do
404 node
.out_edges
.add
(new NeoEdge(node
, "DECLARES", to_node
(mmodule
)))
406 for subgroup
in mgroup
.in_nesting
.direct_smallers
do
407 node
.in_edges
.add
(new NeoEdge(node
, "NESTS", to_node
(subgroup
)))
412 # Build a new `MGroup` from a `node`.
414 # REQUIRE `node.labels.has("MGroup")`
415 private fun to_mgroup
(model
: Model, node
: NeoNode): MGroup do
416 var m
= mentities
.get_or_null
(node
.id
.as(Int))
417 if m
isa MGroup then return m
419 assert node
.labels
.has
("MGroup")
420 var mproject
= to_mproject
(model
, node
.out_nodes
("PROJECT").first
)
421 var parent
: nullable MGroup = null
422 var out
= node
.out_nodes
("PARENT")
423 if not out
.is_empty
then
424 parent
= to_mgroup
(model
, out
.first
)
426 var mgroup
= new MGroup(node
["name"].to_s
, mproject
, parent
)
427 mentities
[node
.id
.as(Int)] = mgroup
428 set_doc
(node
, mgroup
)
432 # Build a `NeoNode` representing `mmodule`.
433 private fun mmodule_node
(mmodule
: MModule): NeoNode do
434 var node
= make_node
(mmodule
)
435 node
.labels
.add
"MModule"
436 node
["full_name"] = mmodule
.full_name
437 node
["location"] = mmodule
.location
.to_s
438 for parent
in mmodule
.in_importation
.direct_greaters
do
439 node
.out_edges
.add
(new NeoEdge(node
, "IMPORTS", to_node
(parent
)))
441 for mclass
in mmodule
.intro_mclasses
do
442 node
.out_edges
.add
(new NeoEdge(node
, "INTRODUCES", to_node
(mclass
)))
444 for mclassdef
in mmodule
.mclassdefs
do
445 node
.out_edges
.add
(new NeoEdge(node
, "DEFINES", to_node
(mclassdef
)))
450 # Build a new `MModule` from a `node`.
452 # REQUIRE `node.labels.has("MModule")`
453 private fun to_mmodule
(model
: Model, node
: NeoNode): MModule do
454 var m
= mentities
.get_or_null
(node
.id
.as(Int))
455 if m
isa MModule then return m
457 assert node
.labels
.has
("MModule")
458 var ins
= node
.in_nodes
("DECLARES")
459 var mgroup
: nullable MGroup = null
460 if not ins
.is_empty
then
461 mgroup
= to_mgroup
(model
, ins
.first
)
463 var name
= node
["name"].to_s
464 var location
= to_location
(node
["location"].to_s
)
465 var mmodule
= new MModule(model
, mgroup
, name
, location
)
466 mentities
[node
.id
.as(Int)] = mmodule
467 set_doc
(node
, mmodule
)
468 var imported_mmodules
= new Array[MModule]
469 for smod
in node
.out_nodes
("IMPORTS") do
470 imported_mmodules
.add to_mmodule
(model
, smod
)
472 mmodule
.set_imported_mmodules
(imported_mmodules
)
476 # Build a `NeoNode` representing `mclass`.
477 private fun mclass_node
(mclass
: MClass): NeoNode do
478 var node
= make_node
(mclass
)
479 node
.labels
.add
"MClass"
480 node
["full_name"] = mclass
.full_name
481 node
["kind"] = mclass
.kind
.to_s
482 node
["visibility"] = mclass
.visibility
.to_s
483 if not mclass
.mparameters
.is_empty
then
484 var parameter_names
= new Array[String]
485 for p
in mclass
.mparameters
do parameter_names
.add
(p
.name
)
486 node
["parameter_names"] = new JsonArray.from
(parameter_names
)
488 node
.out_edges
.add
(new NeoEdge(node
, "CLASSTYPE", to_node
(mclass
.mclass_type
)))
492 # Build a new `MClass` from a `node`.
494 # REQUIRE `node.labels.has("MClass")`
495 private fun to_mclass
(model
: Model, node
: NeoNode): MClass do
496 var m
= mentities
.get_or_null
(node
.id
.as(Int))
497 if m
isa MClass then return m
499 assert node
.labels
.has
("MClass")
500 var mmodule
= to_mmodule
(model
, node
.in_nodes
("INTRODUCES").first
)
501 var name
= node
["name"].to_s
502 var kind
= to_kind
(node
["kind"].to_s
)
503 var visibility
= to_visibility
(node
["visibility"].to_s
)
504 var parameter_names
= new Array[String]
505 if node
.has_key
("parameter_names") then
506 for e
in node
["parameter_names"].as(JsonArray) do
507 parameter_names
.add e
.to_s
510 var mclass
= new MClass(mmodule
, name
, parameter_names
, kind
, visibility
)
511 mentities
[node
.id
.as(Int)] = mclass
512 set_doc
(node
, mclass
)
516 # Build a `NeoNode` representing `mclassdef`.
517 private fun mclassdef_node
(mclassdef
: MClassDef): NeoNode do
518 var node
= make_node
(mclassdef
)
519 node
.labels
.add
"MClassDef"
520 node
["location"] = mclassdef
.location
.to_s
521 node
.out_edges
.add
(new NeoEdge(node
, "BOUNDTYPE", to_node
(mclassdef
.bound_mtype
)))
522 node
.out_edges
.add
(new NeoEdge(node
, "MCLASS", to_node
(mclassdef
.mclass
)))
523 for mproperty
in mclassdef
.intro_mproperties
do
524 node
.out_edges
.add
(new NeoEdge(node
, "INTRODUCES", to_node
(mproperty
)))
526 for mpropdef
in mclassdef
.mpropdefs
do
527 node
.out_edges
.add
(new NeoEdge(node
, "DECLARES", to_node
(mpropdef
)))
529 for sup
in mclassdef
.supertypes
do
530 node
.out_edges
.add
(new NeoEdge(node
, "INHERITS", to_node
(sup
)))
535 # Build a new `MClassDef` from a `node`.
537 # REQUIRE `node.labels.has("MClassDef")`
538 private fun to_mclassdef
(model
: Model, node
: NeoNode): MClassDef do
539 var m
= mentities
.get_or_null
(node
.id
.as(Int))
540 if m
isa MClassDef then return m
542 assert node
.labels
.has
("MClassDef")
543 var mmodule
= to_mmodule
(model
, node
.in_nodes
("DEFINES").first
)
544 var mtype
= to_mtype
(model
, node
.out_nodes
("BOUNDTYPE").first
).as(MClassType)
545 var location
= to_location
(node
["location"].to_s
)
546 var mclassdef
= new MClassDef(mmodule
, mtype
, location
)
547 mentities
[node
.id
.as(Int)] = mclassdef
548 set_doc
(node
, mclassdef
)
549 var supertypes
= new Array[MClassType]
550 for sup
in node
.out_nodes
("INHERITS") do
551 supertypes
.add to_mtype
(model
, sup
).as(MClassType)
553 mclassdef
.set_supertypes
(supertypes
)
554 mclassdef
.add_in_hierarchy
558 # Build a `NeoNode` representing `mproperty`.
559 private fun mproperty_node
(mproperty
: MProperty): NeoNode do
560 var node
= make_node
(mproperty
)
561 node
.labels
.add
"MProperty"
562 node
["full_name"] = mproperty
.full_name
563 node
["visibility"] = mproperty
.visibility
.to_s
564 if mproperty
isa MMethod then
565 node
.labels
.add
"MMethod"
566 node
["is_init"] = mproperty
.is_init
567 else if mproperty
isa MAttribute then
568 node
.labels
.add
"MAttribute"
569 else if mproperty
isa MVirtualTypeProp then
570 node
.labels
.add
"MVirtualTypeProp"
572 node
.out_edges
.add
(new NeoEdge(node
, "INTRO_CLASSDEF", to_node
(mproperty
.intro_mclassdef
)))
576 # Build a new `MProperty` from a `node`.
578 # REQUIRE `node.labels.has("MProperty")`
579 private fun to_mproperty
(model
: Model, node
: NeoNode): MProperty do
580 var m
= mentities
.get_or_null
(node
.id
.as(Int))
581 if m
isa MProperty then return m
583 assert node
.labels
.has
("MProperty")
584 var intro_mclassdef
= to_mclassdef
(model
, node
.out_nodes
("INTRO_CLASSDEF").first
)
585 var name
= node
["name"].to_s
586 var visibility
= to_visibility
(node
["visibility"].to_s
)
587 var mprop
: nullable MProperty = null
588 if node
.labels
.has
("MMethod") then
589 mprop
= new MMethod(intro_mclassdef
, name
, visibility
)
590 mprop
.is_init
= node
["is_init"].as(Bool)
591 else if node
.labels
.has
("MAttribute") then
592 mprop
= new MAttribute(intro_mclassdef
, name
, visibility
)
593 else if node
.labels
.has
("MVirtualTypeProp") then
594 mprop
= new MVirtualTypeProp(intro_mclassdef
, name
, visibility
)
596 if mprop
== null then
597 print
"not yet implemented to_mproperty for {node.labels.join(",")}"
600 mentities
[node
.id
.as(Int)] = mprop
605 # Build a `NeoNode` representing `mpropdef`.
606 private fun mpropdef_node
(mpropdef
: MPropDef): NeoNode do
607 var node
= make_node
(mpropdef
)
608 node
.labels
.add
"MPropDef"
609 node
["location"] = mpropdef
.location
.to_s
610 node
.out_edges
.add
(new NeoEdge(node
, "DEFINES", to_node
(mpropdef
.mproperty
)))
611 if mpropdef
isa MMethodDef then
612 node
.labels
.add
"MMethodDef"
613 node
["is_abstract"] = mpropdef
.is_abstract
614 node
["is_intern"] = mpropdef
.is_intern
615 node
["is_extern"] = mpropdef
.is_extern
616 var msignature
= mpropdef
.msignature
617 if msignature
!= null then
618 node
.out_edges
.add
(new NeoEdge(node
, "SIGNATURE", to_node
(msignature
)))
620 else if mpropdef
isa MAttributeDef then
621 node
.labels
.add
"MAttributeDef"
622 var static_mtype
= mpropdef
.static_mtype
623 if static_mtype
!= null then
624 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(static_mtype
)))
626 else if mpropdef
isa MVirtualTypeDef then
627 node
.labels
.add
"MVirtualTypeDef"
628 var bound
= mpropdef
.bound
629 if bound
!= null then
630 node
.out_edges
.add
(new NeoEdge(node
, "BOUND", to_node
(bound
)))
636 # Build a new `MPropDef` from a `node`.
638 # REQUIRE `node.labels.has("MPropDef")`
639 private fun to_mpropdef
(model
: Model, node
: NeoNode): MPropDef do
640 var m
= mentities
.get_or_null
(node
.id
.as(Int))
641 if m
isa MPropDef then return m
643 assert node
.labels
.has
("MPropDef")
644 var mclassdef
= to_mclassdef
(model
, node
.in_nodes
("DECLARES").first
)
645 var mproperty
= to_mproperty
(model
, node
.out_nodes
("DEFINES").first
)
646 var location
= to_location
(node
["location"].to_s
)
647 var mpropdef
: nullable MPropDef = null
648 if node
.labels
.has
("MMethodDef") then
649 mpropdef
= new MMethodDef(mclassdef
, mproperty
.as(MMethod), location
)
650 mpropdef
.is_abstract
= node
["is_abstract"].as(Bool)
651 mpropdef
.is_intern
= node
["is_intern"].as(Bool)
652 mpropdef
.is_extern
= node
["is_extern"].as(Bool)
653 mentities
[node
.id
.as(Int)] = mpropdef
654 mpropdef
.msignature
= to_mtype
(model
, node
.out_nodes
("SIGNATURE").first
).as(MSignature)
655 else if node
.labels
.has
("MAttributeDef") then
656 mpropdef
= new MAttributeDef(mclassdef
, mproperty
.as(MAttribute), location
)
657 mentities
[node
.id
.as(Int)] = mpropdef
658 var static_mtype
= node
.out_nodes
("TYPE")
659 if not static_mtype
.is_empty
then mpropdef
.static_mtype
= to_mtype
(model
, static_mtype
.first
)
660 else if node
.labels
.has
("MVirtualTypeDef") then
661 mpropdef
= new MVirtualTypeDef(mclassdef
, mproperty
.as(MVirtualTypeProp), location
)
662 mentities
[node
.id
.as(Int)] = mpropdef
663 var bound
= node
.out_nodes
("BOUND")
664 if not bound
.is_empty
then mpropdef
.bound
= to_mtype
(model
, bound
.first
)
666 if mpropdef
== null then
667 print
"not yet implemented to_mpropdef for {node.labels.join(",")}"
670 set_doc
(node
, mpropdef
)
674 # Build a `NeoNode` representing `mtype`.
675 private fun mtype_node
(mtype
: MType): NeoNode do
676 var node
= make_node
(mtype
)
677 node
.labels
.add
"MType"
678 if mtype
isa MClassType then
679 node
.labels
.add
"MClassType"
680 node
.out_edges
.add
(new NeoEdge(node
, "CLASS", to_node
(mtype
.mclass
)))
681 for arg
in mtype
.arguments
do
682 node
.out_edges
.add
(new NeoEdge(node
, "ARGUMENT", to_node
(arg
)))
684 if mtype
isa MGenericType then
685 node
.labels
.add
"MGenericType"
687 else if mtype
isa MVirtualType then
688 node
.labels
.add
"MVirtualType"
689 node
.out_edges
.add
(new NeoEdge(node
, "PROPERTY", to_node
(mtype
.mproperty
)))
690 else if mtype
isa MParameterType then
691 node
.labels
.add
"MParameterType"
692 node
["rank"] = mtype
.rank
693 node
.out_edges
.add
(new NeoEdge(node
, "CLASS", to_node
(mtype
.mclass
)))
694 else if mtype
isa MNullableType then
695 node
.labels
.add
"MNullableType"
696 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(mtype
.mtype
)))
697 else if mtype
isa MSignature then
698 node
.labels
.add
"MSignature"
699 var names
= new JsonArray
701 for mparameter
in mtype
.mparameters
do
702 names
.add mparameter
.name
703 var pnode
= mparameter_node
(mparameter
)
705 node
.out_edges
.add
(new NeoEdge(node
, "PARAMETER", pnode
))
708 if not names
.is_empty
then node
["parameter_names"] = names
709 var return_mtype
= mtype
.return_mtype
710 if return_mtype
!= null then
711 node
.out_edges
.add
(new NeoEdge(node
, "RETURNTYPE", to_node
(return_mtype
)))
713 else if mtype
isa MRawType then
714 node
.labels
.add
"MRawType"
715 var text
= new JsonArray
717 for part
in mtype
.parts
do
719 if part
.target
!= null then
720 var pnode
= mtypepart_node
(part
)
722 node
.out_edges
.add
(new NeoEdge(node
, "LINK", pnode
))
726 if not text
.is_empty
then node
["text"] = text
731 # Build a `NeoNode` representing `mtypepart`.
732 private fun mtypepart_node
(mtypepart
: MTypePart): NeoNode do
733 var node
= make_node
(mtypepart
)
734 node
.labels
.add
"MTypePart"
735 if mtypepart
.target
!= null then
736 var target_node
= to_node
(mtypepart
.target
.as(not null))
737 node
.out_edges
.add
(new NeoEdge(node
, "TARGET", target_node
))
742 # Build a new `MType` from a `node`.
744 # REQUIRE `node.labels.has("MType")`
745 private fun to_mtype
(model
: Model, node
: NeoNode): MType do
746 var m
= mentities
.get_or_null
(node
.id
.as(Int))
747 if m
isa MType then return m
749 assert node
.labels
.has
("MType")
750 if node
.labels
.has
("MClassType") then
751 var mclass
= to_mclass
(model
, node
.out_nodes
("CLASS").first
)
752 var args
= new Array[MType]
753 for narg
in node
.out_nodes
("ARGUMENT") do
754 args
.add to_mtype
(model
, narg
)
756 var mtype
= mclass
.get_mtype
(args
)
757 mentities
[node
.id
.as(Int)] = mtype
759 else if node
.labels
.has
("MParameterType") then
760 var mclass
= to_mclass
(model
, node
.out_nodes
("CLASS").first
)
761 var rank
= node
["rank"].to_s
.to_i
762 var mtype
= mclass
.mparameters
[rank
]
763 mentities
[node
.id
.as(Int)] = mtype
765 else if node
.labels
.has
("MNullableType") then
766 var intype
= to_mtype
(model
, node
.out_nodes
("TYPE").first
)
767 var mtype
= intype
.as_nullable
768 mentities
[node
.id
.as(Int)] = mtype
770 else if node
.labels
.has
("MVirtualType") then
771 var mproperty
= to_mproperty
(model
, node
.out_nodes
("PROPERTY").first
)
772 assert mproperty
isa MVirtualTypeProp
773 var mtype
= mproperty
.mvirtualtype
774 mentities
[node
.id
.as(Int)] = mtype
776 else if node
.labels
.has
("MSignature") then
777 # Get all param nodes
778 var mparam_nodes
= new HashMap[String, MParameter]
779 for pnode
in node
.out_nodes
("PARAMETER") do
780 var mparam
= to_mparameter
(model
, pnode
)
781 mparam_nodes
[mparam
.name
] = mparam
783 # Load params in the good order
784 var mparam_names
= node
["parameter_names"]
785 var mparameters
= new Array[MParameter]
786 if mparam_names
isa JsonArray then
787 for mparam_name
in mparam_names
do
788 var mparam
= mparam_nodes
[mparam_name
.to_s
]
789 mparameters
.add mparam
792 var return_mtype
: nullable MType = null
793 var ret_nodes
= node
.out_nodes
("RETURNTYPE")
794 if not ret_nodes
.is_empty
then
795 return_mtype
= to_mtype
(model
, ret_nodes
.first
)
797 var mtype
= new MSignature(mparameters
, return_mtype
)
798 mentities
[node
.id
.as(Int)] = mtype
800 else if node
.labels
.has
("MRawType") then
801 var mtype
= new MRawType(model
)
802 var parts
= node
["text"]
803 if parts
isa JsonArray then
805 mtype
.parts
.add
(new MTypePart(model
, p
.to_s
, null))
807 for pnode
in node
.out_nodes
("LINK") do
808 assert pnode
.labels
.has
("MTypePart")
809 if not pnode
.out_nodes
("TARGET").is_empty
then
810 var rank
= pnode
["rank"]
811 var target
= to_mentity
(model
, pnode
.out_nodes
("TARGET").first
)
813 mtype
.parts
[rank
] = mtype
.parts
[rank
].link_to
(target
)
817 mentities
[node
.id
.as(Int)] = mtype
820 print
"not yet implemented to_mtype for {node.labels.join(",")}"
824 # Build a `NeoNode` representing `mparameter`.
825 private fun mparameter_node
(mparameter
: MParameter): NeoNode do
826 var node
= make_node
(mparameter
)
827 node
.labels
.add
"MParameter"
828 node
["name"] = mparameter
.name
829 node
["is_vararg"] = mparameter
.is_vararg
830 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(mparameter
.mtype
)))
834 # Build a new `MParameter` from `node`.
836 # REQUIRE `node.labels.has("MParameter")`
837 private fun to_mparameter
(model
: Model, node
: NeoNode): MParameter do
838 var m
= mentities
.get_or_null
(node
.id
.as(Int))
839 if m
isa MParameter then return m
841 assert node
.labels
.has
("MParameter")
842 var name
= node
["name"].to_s
843 var mtype
= to_mtype
(model
, node
.out_nodes
("TYPE").first
)
844 var is_vararg
= node
["is_vararg"].as(Bool)
845 var mparameter
= new MParameter(name
, mtype
, is_vararg
)
846 mentities
[node
.id
.as(Int)] = mparameter
850 # Get a `Location` from its string representation.
851 private fun to_location
(loc
: String): Location do
853 var parts
= loc
.split_with
(":")
854 var file
= new SourceFile.from_string
(parts
[0], "")
855 var pos
= parts
[1].split_with
("--")
856 var pos1
= pos
[0].split_with
(",")
857 var pos2
= pos
[1].split_with
(",")
858 var line_s
= pos1
[0].to_i
859 var line_e
= pos2
[0].to_i
860 var column_s
= pos1
[1].to_i
862 if pos2
.length
== 2 then pos2
[1].to_i
863 return new Location(file
, line_s
, line_e
, column_s
, column_e
)
866 # Get a `MVisibility` from its string representation.
867 private fun to_visibility
(vis
: String): MVisibility do
868 if vis
== intrude_visibility
.to_s
then
869 return intrude_visibility
870 else if vis
== public_visibility
.to_s
then
871 return public_visibility
872 else if vis
== protected_visibility
.to_s
then
873 return protected_visibility
874 else if vis
== private_visibility
.to_s
then
875 return private_visibility
876 else if vis
== package_visibility
.to_s
then
877 return package_visibility
879 return none_visibility
883 # Get a `MKind` from its string representation.
884 private fun to_kind
(kind
: String): MClassKind do
885 if kind
== abstract_kind
.to_s
then
887 else if kind
== concrete_kind
.to_s
then
889 else if kind
== interface_kind
.to_s
then
890 return interface_kind
891 else if kind
== enum_kind
.to_s
then
893 else if kind
== extern_kind
.to_s
then
896 return raw_kind
(kind
)
900 # Extract the `MDoc` from `node` and link it to `mentity`.
901 private fun set_doc
(node
: NeoNode, mentity
: MEntity) do
902 if node
.has_key
("mdoc") then
903 var lines
= new Array[String]
904 for e
in node
["mdoc"].as(JsonArray) do
905 lines
.add e
.to_s
#.replace("\n", "\\n")
908 mdoc
.content
.add_all
(lines
)
909 mdoc
.original_mentity
= mentity