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` or `MSignature`, depending on the class of
137 # 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 relationships for `MSignature`:
165 # * `parameter_names`: JSON array representing the list of the parameter names.
166 # * `(:MSignature)-[:PARAMETER]->(:MParameter)`: parameters.
167 # * `(:MSignature)-[:RETURNTYPE]->(:MType)`: return type. Does not exist for
170 # In order to maintain the correct parameters order, each `MSignature` node
171 # contains an array of parameter names corresponding to the parameter order in
174 # For example, if the source code contains:
176 # fun foo(a: A, b: B, c: C)
178 # The `MSignature` node will contain a property
179 # `parameter_names = ["a", "b", "c"]` so the MSignature can be reconstructed
180 # with the parameters in the correct order.
184 # * labels: `MParameter`, `model_name` and `MEntity`.
185 # * `is_vararg`: Is the parameter a vararg?
186 # * `rank`: position of the parameter (0 for the first parameter).
187 # * `(:MParameter)-[:TYPE]->(:MType)`: static type of the parameter.
189 # MParameters are also ranked by their position in the corresponding signature.
190 # Rank 0 for the first parameter, 1 for the next one and etc.
197 # Helper class that can save and load a `Model` into a Neo4j database.
202 # Because we use only one Neo4j instance to store all the models,
203 # we need to mark their appartenance to a particular model and avoid loading all models.
205 # The name is used as a Neo label on each created nodes and used to load nodes from base.
206 var model_name
: String
208 # The toolcontext used to init the `NeoModel` tool.
209 var toolcontext
: ToolContext
211 # The Neo4j `client` used to communicate with the Neo4j instance.
212 var client
: Neo4jClient
214 # Fill `model` using base pointed by `client`.
215 fun load
(model
: Model): Model do
216 toolcontext
.info
("Locate all mentities...", 1)
217 var nodes
= client
.nodes_with_label
(model_name
)
219 toolcontext
.info
("Preload nodes...", 1)
220 pull_all_nodes
(nodes
)
221 toolcontext
.info
("Preload edges...", 1)
222 pull_all_edges
(nodes
)
224 toolcontext
.info
("Build model...", 1)
225 nodes
= client
.nodes_with_labels
([model_name
, "MProject"])
226 for node
in nodes
do to_mproject
(model
, node
)
227 nodes
= client
.nodes_with_labels
([model_name
, "MGroup"])
228 for node
in nodes
do to_mgroup
(model
, node
)
229 nodes
= client
.nodes_with_labels
([model_name
, "MModule"])
230 for node
in nodes
do to_mmodule
(model
, node
)
231 nodes
= client
.nodes_with_labels
([model_name
, "MClass"])
232 for node
in nodes
do to_mclass
(model
, node
)
233 nodes
= client
.nodes_with_labels
([model_name
, "MClassDef"])
234 for node
in nodes
do to_mclassdef
(model
, node
)
235 nodes
= client
.nodes_with_labels
([model_name
, "MProperty"])
236 for node
in nodes
do to_mproperty
(model
, node
)
237 nodes
= client
.nodes_with_labels
([model_name
, "MPropDef"])
238 for node
in nodes
do to_mpropdef
(model
, node
)
242 # Save `model` in the base pointed by `client`.
243 fun save
(model
: Model) do
244 var nodes
= collect_model_nodes
(model
)
245 toolcontext
.info
("Save {nodes.length} nodes...", 1)
247 var edges
= collect_model_edges
(model
)
248 toolcontext
.info
("Save {edges.length} edges...", 1)
252 # Save `neo_entities` in base using batch mode.
253 private fun push_all
(neo_entities
: Collection[NeoEntity]) do
254 var batch
= new NeoBatch(client
)
255 var len
= neo_entities
.length
258 for nentity
in neo_entities
do
259 batch
.save_entity
(nentity
)
260 if i
== batch_max_size
then
262 sum
+= batch_max_size
263 toolcontext
.info
(" {sum * 100 / len}% done", 1)
264 batch
= new NeoBatch(client
)
273 # Load content for all `nodes` from base.
275 # Content corresponds to properties and labels that are loaded in batch mode.
276 private fun pull_all_nodes
(nodes
: Collection[NeoNode]) do
277 var batch
= new NeoBatch(client
)
278 var len
= nodes
.length
282 batch
.load_node
(node
)
283 if i
== batch_max_size
then
285 sum
+= batch_max_size
286 toolcontext
.info
(" {sum * 100 / len}% done", 1)
287 batch
= new NeoBatch(client
)
296 # Load all edges from base linked to `nodes`.
298 # Edges are loaded in batch mode.
299 private fun pull_all_edges
(nodes
: Collection[NeoNode]) do
300 var batch
= new NeoBatch(client
)
301 var len
= nodes
.length
305 batch
.load_node_edges
(node
)
306 if i
== batch_max_size
then
308 sum
+= batch_max_size
309 toolcontext
.info
(" {sum * 100 / len}% done", 1)
310 batch
= new NeoBatch(client
)
319 # How many operation can be executed in one batch?
320 private var batch_max_size
= 1000
322 # Execute `batch` and check for errors.
324 # Abort if `batch.execute` returns errors.
325 private fun do_batch
(batch
: NeoBatch) do
326 var errors
= batch
.execute
327 if not errors
.is_empty
then
333 # Collect all nodes from the current `model`.
334 private fun collect_model_nodes
(model
: Model): Collection[NeoNode] do
335 for mproject
in model
.mprojects
do
337 for mgroup
in mproject
.mgroups
do to_node
(mgroup
)
342 # Collect all edges from the current `model`.
344 # Actually collect all out_edges from all nodes.
345 private fun collect_model_edges
(model
: Model): Collection[NeoEdge] do
346 var edges
= new HashSet[NeoEdge]
347 for node
in nodes
.values
do edges
.add_all
(node
.out_edges
)
351 # Mentities associated to nodes.
352 private var mentities
= new HashMap[NeoNode, MEntity]
354 # Nodes associated with MEntities.
355 private var nodes
= new HashMap[MEntity, NeoNode]
357 # Get the `NeoNode` associated with `mentity`.
358 # `mentities` are stored locally to avoid duplication.
359 fun to_node
(mentity
: MEntity): NeoNode do
360 if nodes
.has_key
(mentity
) then return nodes
[mentity
]
361 if mentity
isa MProject then return mproject_node
(mentity
)
362 if mentity
isa MGroup then return mgroup_node
(mentity
)
363 if mentity
isa MModule then return mmodule_node
(mentity
)
364 if mentity
isa MClass then return mclass_node
(mentity
)
365 if mentity
isa MClassDef then return mclassdef_node
(mentity
)
366 if mentity
isa MProperty then return mproperty_node
(mentity
)
367 if mentity
isa MPropDef then return mpropdef_node
(mentity
)
368 if mentity
isa MType then return mtype_node
(mentity
)
369 if mentity
isa MParameter then return mparameter_node
(mentity
)
373 # Make a new `NeoNode` based on `mentity`.
374 private fun make_node
(mentity
: MEntity): NeoNode do
375 var node
= new NeoNode
376 nodes
[mentity
] = node
377 node
.labels
.add
"MEntity"
378 node
.labels
.add model_name
379 node
["name"] = mentity
.name
380 if mentity
.mdoc
!= null then node
["mdoc"] = new JsonArray.from
(mentity
.mdoc
.content
)
384 # Build a `NeoNode` representing `mproject`.
385 private fun mproject_node
(mproject
: MProject): NeoNode do
386 var node
= make_node
(mproject
)
387 node
.labels
.add
"MProject"
388 var root
= mproject
.root
390 node
.out_edges
.add
(new NeoEdge(node
, "ROOT", to_node
(root
)))
395 # Build a new `MProject` from a `node`.
397 # REQUIRE `node.labels.has("MProject")`
398 private fun to_mproject
(model
: Model, node
: NeoNode): MProject do
399 if mentities
.has_key
(node
) then return mentities
[node
].as(MProject)
400 assert node
.labels
.has
("MProject")
401 var mproject
= new MProject(node
["name"].to_s
, model
)
402 mentities
[node
] = mproject
403 set_doc
(node
, mproject
)
404 mproject
.root
= to_mgroup
(model
, node
.out_nodes
("ROOT").first
)
408 # Build a `NeoNode` representing `mgroup`.
409 private fun mgroup_node
(mgroup
: MGroup): NeoNode do
410 var node
= make_node
(mgroup
)
411 node
.labels
.add
"MGroup"
412 node
["full_name"] = mgroup
.full_name
413 var parent
= mgroup
.parent
414 node
.out_edges
.add
(new NeoEdge(node
, "PROJECT", to_node
(mgroup
.mproject
)))
415 if parent
!= null then
416 node
.out_edges
.add
(new NeoEdge(node
, "PARENT", to_node
(parent
)))
418 for mmodule
in mgroup
.mmodules
do
419 node
.out_edges
.add
(new NeoEdge(node
, "DECLARES", to_node
(mmodule
)))
421 for subgroup
in mgroup
.in_nesting
.direct_smallers
do
422 node
.in_edges
.add
(new NeoEdge(node
, "NESTS", to_node
(subgroup
)))
427 # Build a new `MGroup` from a `node`.
429 # REQUIRE `node.labels.has("MGroup")`
430 private fun to_mgroup
(model
: Model, node
: NeoNode): MGroup do
431 if mentities
.has_key
(node
) then return mentities
[node
].as(MGroup)
432 assert node
.labels
.has
("MGroup")
433 var mproject
= to_mproject
(model
, node
.out_nodes
("PROJECT").first
)
434 var parent
: nullable MGroup = null
435 var out
= node
.out_nodes
("PARENT")
436 if not out
.is_empty
then
437 parent
= to_mgroup
(model
, out
.first
)
439 var mgroup
= new MGroup(node
["name"].to_s
, mproject
, parent
)
440 mentities
[node
] = mgroup
441 set_doc
(node
, mgroup
)
445 # Build a `NeoNode` representing `mmodule`.
446 private fun mmodule_node
(mmodule
: MModule): NeoNode do
447 var node
= make_node
(mmodule
)
448 node
.labels
.add
"MModule"
449 node
["full_name"] = mmodule
.full_name
450 node
["location"] = mmodule
.location
.to_s
451 for parent
in mmodule
.in_importation
.direct_greaters
do
452 node
.out_edges
.add
(new NeoEdge(node
, "IMPORTS", to_node
(parent
)))
454 for mclass
in mmodule
.intro_mclasses
do
455 node
.out_edges
.add
(new NeoEdge(node
, "INTRODUCES", to_node
(mclass
)))
457 for mclassdef
in mmodule
.mclassdefs
do
458 node
.out_edges
.add
(new NeoEdge(node
, "DEFINES", to_node
(mclassdef
)))
463 # Build a new `MModule` from a `node`.
465 # REQUIRE `node.labels.has("MModule")`
466 private fun to_mmodule
(model
: Model, node
: NeoNode): MModule do
467 if mentities
.has_key
(node
) then return mentities
[node
].as(MModule)
468 assert node
.labels
.has
("MModule")
469 var ins
= node
.in_nodes
("DECLARES")
470 var mgroup
: nullable MGroup = null
471 if not ins
.is_empty
then
472 mgroup
= to_mgroup
(model
, ins
.first
)
474 var name
= node
["name"].to_s
475 var location
= to_location
(node
["location"].to_s
)
476 var mmodule
= new MModule(model
, mgroup
, name
, location
)
477 mentities
[node
] = mmodule
478 set_doc
(node
, mmodule
)
479 var imported_mmodules
= new Array[MModule]
480 for smod
in node
.out_nodes
("IMPORTS") do
481 imported_mmodules
.add to_mmodule
(model
, smod
)
483 mmodule
.set_imported_mmodules
(imported_mmodules
)
487 # Build a `NeoNode` representing `mclass`.
488 private fun mclass_node
(mclass
: MClass): NeoNode do
489 var node
= make_node
(mclass
)
490 node
.labels
.add
"MClass"
491 node
["full_name"] = mclass
.full_name
492 node
["kind"] = mclass
.kind
.to_s
493 node
["visibility"] = mclass
.visibility
.to_s
494 if not mclass
.mparameters
.is_empty
then
495 var parameter_names
= new Array[String]
496 for p
in mclass
.mparameters
do parameter_names
.add
(p
.name
)
497 node
["parameter_names"] = new JsonArray.from
(parameter_names
)
499 node
.out_edges
.add
(new NeoEdge(node
, "CLASSTYPE", to_node
(mclass
.mclass_type
)))
503 # Build a new `MClass` from a `node`.
505 # REQUIRE `node.labels.has("MClass")`
506 private fun to_mclass
(model
: Model, node
: NeoNode): MClass do
507 if mentities
.has_key
(node
) then return mentities
[node
].as(MClass)
508 assert node
.labels
.has
("MClass")
509 var mmodule
= to_mmodule
(model
, node
.in_nodes
("INTRODUCES").first
)
510 var name
= node
["name"].to_s
511 var kind
= to_kind
(node
["kind"].to_s
)
512 var visibility
= to_visibility
(node
["visibility"].to_s
)
513 var parameter_names
= new Array[String]
514 if node
.has_key
("parameter_names") then
515 for e
in node
["parameter_names"].as(JsonArray) do
516 parameter_names
.add e
.to_s
519 var mclass
= new MClass(mmodule
, name
, parameter_names
, kind
, visibility
)
520 mentities
[node
] = mclass
521 set_doc
(node
, mclass
)
525 # Build a `NeoNode` representing `mclassdef`.
526 private fun mclassdef_node
(mclassdef
: MClassDef): NeoNode do
527 var node
= make_node
(mclassdef
)
528 node
.labels
.add
"MClassDef"
529 node
["location"] = mclassdef
.location
.to_s
530 node
.out_edges
.add
(new NeoEdge(node
, "BOUNDTYPE", to_node
(mclassdef
.bound_mtype
)))
531 node
.out_edges
.add
(new NeoEdge(node
, "MCLASS", to_node
(mclassdef
.mclass
)))
532 for mproperty
in mclassdef
.intro_mproperties
do
533 node
.out_edges
.add
(new NeoEdge(node
, "INTRODUCES", to_node
(mproperty
)))
535 for mpropdef
in mclassdef
.mpropdefs
do
536 node
.out_edges
.add
(new NeoEdge(node
, "DECLARES", to_node
(mpropdef
)))
538 for sup
in mclassdef
.supertypes
do
539 node
.out_edges
.add
(new NeoEdge(node
, "INHERITS", to_node
(sup
)))
544 # Build a new `MClassDef` from a `node`.
546 # REQUIRE `node.labels.has("MClassDef")`
547 private fun to_mclassdef
(model
: Model, node
: NeoNode): MClassDef do
548 if mentities
.has_key
(node
) then return mentities
[node
].as(MClassDef)
549 assert node
.labels
.has
("MClassDef")
550 var mmodule
= to_mmodule
(model
, node
.in_nodes
("DEFINES").first
)
551 var mtype
= to_mtype
(model
, node
.out_nodes
("BOUNDTYPE").first
).as(MClassType)
552 var location
= to_location
(node
["location"].to_s
)
553 var mclassdef
= new MClassDef(mmodule
, mtype
, location
)
554 mentities
[node
] = mclassdef
555 set_doc
(node
, mclassdef
)
556 var supertypes
= new Array[MClassType]
557 for sup
in node
.out_nodes
("INHERITS") do
558 supertypes
.add to_mtype
(model
, sup
).as(MClassType)
560 mclassdef
.set_supertypes
(supertypes
)
561 mclassdef
.add_in_hierarchy
565 # Build a `NeoNode` representing `mproperty`.
566 private fun mproperty_node
(mproperty
: MProperty): NeoNode do
567 var node
= make_node
(mproperty
)
568 node
.labels
.add
"MProperty"
569 node
["full_name"] = mproperty
.full_name
570 node
["visibility"] = mproperty
.visibility
.to_s
571 if mproperty
isa MMethod then
572 node
.labels
.add
"MMethod"
573 node
["is_init"] = mproperty
.is_init
574 else if mproperty
isa MAttribute then
575 node
.labels
.add
"MAttribute"
576 else if mproperty
isa MVirtualTypeProp then
577 node
.labels
.add
"MVirtualTypeProp"
579 node
.out_edges
.add
(new NeoEdge(node
, "INTRO_CLASSDEF", to_node
(mproperty
.intro_mclassdef
)))
583 # Build a new `MProperty` from a `node`.
585 # REQUIRE `node.labels.has("MProperty")`
586 private fun to_mproperty
(model
: Model, node
: NeoNode): MProperty do
587 if mentities
.has_key
(node
) then return mentities
[node
].as(MProperty)
588 assert node
.labels
.has
("MProperty")
589 var intro_mclassdef
= to_mclassdef
(model
, node
.out_nodes
("INTRO_CLASSDEF").first
)
590 var name
= node
["name"].to_s
591 var visibility
= to_visibility
(node
["visibility"].to_s
)
592 var mprop
: nullable MProperty = null
593 if node
.labels
.has
("MMethod") then
594 mprop
= new MMethod(intro_mclassdef
, name
, visibility
)
595 mprop
.is_init
= node
["is_init"].as(Bool)
596 else if node
.labels
.has
("MAttribute") then
597 mprop
= new MAttribute(intro_mclassdef
, name
, visibility
)
598 else if node
.labels
.has
("MVirtualTypeProp") then
599 mprop
= new MVirtualTypeProp(intro_mclassdef
, name
, visibility
)
601 if mprop
== null then
602 print
"not yet implemented to_mproperty for {node.labels.join(",")}"
605 mentities
[node
] = mprop
610 # Build a `NeoNode` representing `mpropdef`.
611 private fun mpropdef_node
(mpropdef
: MPropDef): NeoNode do
612 var node
= make_node
(mpropdef
)
613 node
.labels
.add
"MPropDef"
614 node
["location"] = mpropdef
.location
.to_s
615 node
.out_edges
.add
(new NeoEdge(node
, "DEFINES", to_node
(mpropdef
.mproperty
)))
616 if mpropdef
isa MMethodDef then
617 node
.labels
.add
"MMethodDef"
618 node
["is_abstract"] = mpropdef
.is_abstract
619 node
["is_intern"] = mpropdef
.is_intern
620 node
["is_extern"] = mpropdef
.is_extern
621 var msignature
= mpropdef
.msignature
622 if msignature
!= null then
623 node
.out_edges
.add
(new NeoEdge(node
, "SIGNATURE", to_node
(msignature
)))
625 else if mpropdef
isa MAttributeDef then
626 node
.labels
.add
"MAttributeDef"
627 var static_mtype
= mpropdef
.static_mtype
628 if static_mtype
!= null then
629 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(static_mtype
)))
631 else if mpropdef
isa MVirtualTypeDef then
632 node
.labels
.add
"MVirtualTypeDef"
633 var bound
= mpropdef
.bound
634 if bound
!= null then
635 node
.out_edges
.add
(new NeoEdge(node
, "BOUND", to_node
(bound
)))
641 # Build a new `MPropDef` from a `node`.
643 # REQUIRE `node.labels.has("MPropDef")`
644 private fun to_mpropdef
(model
: Model, node
: NeoNode): MPropDef do
645 if mentities
.has_key
(node
) then return mentities
[node
].as(MPropDef)
646 assert node
.labels
.has
("MPropDef")
647 var mclassdef
= to_mclassdef
(model
, node
.in_nodes
("DECLARES").first
)
648 var mproperty
= to_mproperty
(model
, node
.out_nodes
("DEFINES").first
)
649 var location
= to_location
(node
["location"].to_s
)
650 var mpropdef
: nullable MPropDef = null
651 if node
.labels
.has
("MMethodDef") then
652 mpropdef
= new MMethodDef(mclassdef
, mproperty
.as(MMethod), location
)
653 mpropdef
.is_abstract
= node
["is_abstract"].as(Bool)
654 mpropdef
.is_intern
= node
["is_intern"].as(Bool)
655 mpropdef
.is_extern
= node
["is_extern"].as(Bool)
656 mentities
[node
] = mpropdef
657 mpropdef
.msignature
= to_mtype
(model
, node
.out_nodes
("SIGNATURE").first
).as(MSignature)
658 else if node
.labels
.has
("MAttributeDef") then
659 mpropdef
= new MAttributeDef(mclassdef
, mproperty
.as(MAttribute), location
)
660 mentities
[node
] = mpropdef
661 var static_mtype
= node
.out_nodes
("TYPE")
662 if not static_mtype
.is_empty
then mpropdef
.static_mtype
= to_mtype
(model
, static_mtype
.first
)
663 else if node
.labels
.has
("MVirtualTypeDef") then
664 mpropdef
= new MVirtualTypeDef(mclassdef
, mproperty
.as(MVirtualTypeProp), location
)
665 mentities
[node
] = mpropdef
666 var bound
= node
.out_nodes
("BOUND")
667 if not bound
.is_empty
then mpropdef
.bound
= to_mtype
(model
, bound
.first
)
669 if mpropdef
== null then
670 print
"not yet implemented to_mpropdef for {node.labels.join(",")}"
673 set_doc
(node
, mpropdef
)
677 # Build a `NeoNode` representing `mtype`.
678 private fun mtype_node
(mtype
: MType): NeoNode do
679 var node
= make_node
(mtype
)
680 node
.labels
.add
"MType"
681 if mtype
isa MClassType then
682 node
.labels
.add
"MClassType"
683 node
.out_edges
.add
(new NeoEdge(node
, "CLASS", to_node
(mtype
.mclass
)))
684 for arg
in mtype
.arguments
do
685 node
.out_edges
.add
(new NeoEdge(node
, "ARGUMENT", to_node
(arg
)))
687 if mtype
isa MGenericType then
688 node
.labels
.add
"MGenericType"
690 else if mtype
isa MVirtualType then
691 node
.labels
.add
"MVirtualType"
692 node
.out_edges
.add
(new NeoEdge(node
, "PROPERTY", to_node
(mtype
.mproperty
)))
693 else if mtype
isa MParameterType then
694 node
.labels
.add
"MParameterType"
695 node
["rank"] = mtype
.rank
696 node
.out_edges
.add
(new NeoEdge(node
, "CLASS", to_node
(mtype
.mclass
)))
697 else if mtype
isa MNullableType then
698 node
.labels
.add
"MNullableType"
699 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(mtype
.mtype
)))
700 else if mtype
isa MSignature then
701 node
.labels
.add
"MSignature"
702 var names
= new JsonArray
704 for mparameter
in mtype
.mparameters
do
705 names
.add mparameter
.name
706 var pnode
= to_node
(mparameter
)
708 node
.out_edges
.add
(new NeoEdge(node
, "PARAMETER", pnode
))
710 if not names
.is_empty
then node
["parameter_names"] = names
711 var return_mtype
= mtype
.return_mtype
712 if return_mtype
!= null then
713 node
.out_edges
.add
(new NeoEdge(node
, "RETURNTYPE", to_node
(return_mtype
)))
719 # Build a new `MType` from a `node`.
721 # REQUIRE `node.labels.has("MType")`
722 private fun to_mtype
(model
: Model, node
: NeoNode): MType do
723 if mentities
.has_key
(node
) then return mentities
[node
].as(MType)
724 assert node
.labels
.has
("MType")
725 if node
.labels
.has
("MClassType") then
726 var mclass
= to_mclass
(model
, node
.out_nodes
("CLASS").first
)
727 var args
= new Array[MType]
728 for narg
in node
.out_nodes
("ARGUMENT") do
729 args
.add to_mtype
(model
, narg
)
731 var mtype
= mclass
.get_mtype
(args
)
732 mentities
[node
] = mtype
734 else if node
.labels
.has
("MParameterType") then
735 var mclass
= to_mclass
(model
, node
.out_nodes
("CLASS").first
)
736 var rank
= node
["rank"].to_s
.to_i
737 var mtype
= mclass
.mparameters
[rank
]
738 mentities
[node
] = mtype
740 else if node
.labels
.has
("MNullableType") then
741 var intype
= to_mtype
(model
, node
.out_nodes
("TYPE").first
)
742 var mtype
= intype
.as_nullable
743 mentities
[node
] = mtype
745 else if node
.labels
.has
("MVirtualType") then
746 var mproperty
= to_mproperty
(model
, node
.out_nodes
("PROPERTY").first
)
747 assert mproperty
isa MVirtualTypeProp
748 var mtype
= mproperty
.mvirtualtype
749 mentities
[node
] = mtype
751 else if node
.labels
.has
("MSignature") then
752 # Get all param nodes
753 var mparam_nodes
= new HashMap[String, MParameter]
754 for pnode
in node
.out_nodes
("PARAMETER") do
755 var mparam
= to_mparameter
(model
, pnode
)
756 mparam_nodes
[mparam
.name
] = mparam
758 # Load params in the good order
759 var mparam_names
= node
["parameter_names"]
760 var mparameters
= new Array[MParameter]
761 if mparam_names
isa JsonArray then
762 for mparam_name
in mparam_names
do
763 var mparam
= mparam_nodes
[mparam_name
.to_s
]
764 mparameters
.add mparam
767 var return_mtype
: nullable MType = null
768 var ret_nodes
= node
.out_nodes
("RETURNTYPE")
769 if not ret_nodes
.is_empty
then
770 return_mtype
= to_mtype
(model
, ret_nodes
.first
)
772 var mtype
= new MSignature(mparameters
, return_mtype
)
773 mentities
[node
] = mtype
776 print
"not yet implemented to_mtype for {node.labels.join(",")}"
780 # Build a `NeoNode` representing `mparameter`.
781 private fun mparameter_node
(mparameter
: MParameter): NeoNode do
782 var node
= make_node
(mparameter
)
783 node
.labels
.add
"MParameter"
784 node
["name"] = mparameter
.name
785 node
["is_vararg"] = mparameter
.is_vararg
786 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(mparameter
.mtype
)))
790 # Build a new `MParameter` from `node`.
792 # REQUIRE `node.labels.has("MParameter")`
793 private fun to_mparameter
(model
: Model, node
: NeoNode): MParameter do
794 if mentities
.has_key
(node
) then return mentities
[node
].as(MParameter)
795 assert node
.labels
.has
("MParameter")
796 var name
= node
["name"].to_s
797 var mtype
= to_mtype
(model
, node
.out_nodes
("TYPE").first
)
798 var is_vararg
= node
["is_vararg"].as(Bool)
799 var mparameter
= new MParameter(name
, mtype
, is_vararg
)
800 mentities
[node
] = mparameter
804 # Get a `Location` from its string representation.
805 private fun to_location
(loc
: String): Location do
807 var parts
= loc
.split_with
(":")
808 var file
= new SourceFile.from_string
(parts
[0], "")
809 var pos
= parts
[1].split_with
("--")
810 var pos1
= pos
[0].split_with
(",")
811 var pos2
= pos
[1].split_with
(",")
812 var line_s
= pos1
[0].to_i
813 var line_e
= pos2
[0].to_i
814 var column_s
= pos1
[1].to_i
816 if pos2
.length
== 2 then pos2
[1].to_i
817 return new Location(file
, line_s
, line_e
, column_s
, column_e
)
820 # Get a `MVisibility` from its string representation.
821 private fun to_visibility
(vis
: String): MVisibility do
822 if vis
== intrude_visibility
.to_s
then
823 return intrude_visibility
824 else if vis
== public_visibility
.to_s
then
825 return public_visibility
826 else if vis
== protected_visibility
.to_s
then
827 return protected_visibility
828 else if vis
== private_visibility
.to_s
then
829 return private_visibility
831 return none_visibility
835 # Get a `MKind` from its string representation.
836 private fun to_kind
(kind
: String): MClassKind do
837 if kind
== abstract_kind
.to_s
then
839 else if kind
== concrete_kind
.to_s
then
841 else if kind
== interface_kind
.to_s
then
842 return interface_kind
843 else if kind
== enum_kind
.to_s
then
845 else if kind
== extern_kind
.to_s
then
851 # Extract the `MDoc` from `node` and link it to `mentity`.
852 private fun set_doc
(node
: NeoNode, mentity
: MEntity) do
853 if node
.has_key
("mdoc") then
854 var lines
= new Array[String]
855 for e
in node
["mdoc"].as(JsonArray) do
856 lines
.add e
.to_s
#.replace("\n", "\\n")
859 mdoc
.content
.add_all
(lines
)
860 mdoc
.original_mentity
= mentity