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 `Model` from/to Neo4j base.
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 base:
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 # * `arity`: number of generic formal parameters. 0 if the class is not generic.
71 # * `kind`: kind of the class (`interface`, `abstract class`, etc.)
72 # * `visibility`: visibility of the class.
73 # * `(:MClass)-[:CLASSTYPE]->(:MClassType)`: SEE: `MClass.mclass_type`
75 # Arguments in the `CLASSTYPE` are named following the `parameter_names`
76 # attribute of the `MClassDef` that introduces the class. A class definition
77 # introduces a class if and only if it has this class as `MCLASS` and
78 # has `is_intro` set to `true`.
82 # * labels: `MClassDef`, `model_name` and `MEntity`.
83 # * `is_intro`: Does this definition introduce the class?
84 # * `location`: origin of the definition. SEE: `Location.to_s`
85 # * `parameter_names`: JSON array listing the name of each formal generic
86 # parameter (in order of declaration).
87 # * `(:MClassDef)-[:BOUNDTYPE]->(:MClassType)`: bounded type associated to the
89 # * `(:MClassDef)-[:MCLASS]->(:MClass)`: associated `MClass`.
90 # * `(:MClassDef)-[:INTRODUCES]->(:MProperty)`: all properties introduced by
92 # * `(:MClassDef)-[:DECLARES]->(:MPropDef)`: all property definitions in the
93 # classdef (introductions and redefinitions).
94 # * `(:MClassDef)-[:INHERITS]->(:MClassType)`: all declared super-types
98 # * labels: `MProperty`, `model_name` and `MEntity`. Must also have `MMethod`,
99 # `MAttribute` or `MVirtualTypeProp`, depending on the class of the represented
101 # * `full_name`: fully qualified name.
102 # * `visibility`: visibility of the property.
103 # * `is_init`: Indicates if the property is a constructor. Exists only if the
104 # node is a `MMethod`.
105 # * `(:MProperty)-[:INTRO_CLASSDEF]->(:MClassDef)`: classdef that introduces
110 # * labels: `MPropDef`, `model_name` and `MEntity`. Must also have `MMethodDef`,
111 # `MAttributeDef` or `MVirtualTypeDef`, depending on the class of the
112 # represented entity.
113 # * `is_intro`: Does this definition introduce the property?
114 # * `location`: origin of the definition. SEE: `Location.to_s`.
115 # * `(:MPropDef)-[:DEFINES]->(:MProperty)`: associated property.
117 # Additional attributes and relationship for `MMethodDef`:
119 # * `is_abstract`: Is the method definition abstract?
120 # * `is_intern`: Is the method definition intern?
121 # * `is_extern`: Is the method definition extern?
122 # * `(:MMethodDef)-[:SIGNATURE]->(:MSignature)`: signature attached to the
123 # property definition.
125 # Additional relationship for `MVirtualTypeDef`:
127 # * `(:MVirtualTypeDef)-[:BOUND]->(:MType)`: type to which the virtual type
128 # is bound in this definition. Exists only if this definition bound the virtual
129 # type to an effective type.
133 # * labels: `MType`, `model_name` and `MEntity`. Must also have `MClassType`,
134 # `MNullableType`, `MVirtualType` or `MSignature`, depending on the class of
135 # the represented entity.
137 # Additional label and relationships for `MClassType`:
139 # * If it is a `MGenericType`, also has the `MGenericType` label.
140 # * `(:MClassType)-[:CLASS]->(:MClass)`: SEE: `MClassType.mclass`
141 # * `(:MClassType)-[:ARGUMENT]->(:MType)`: type arguments.
143 # Arguments are named following the `parameter_names` attribute of the
144 # `MClassDef` that introduces the class referred by `CLASS`.
146 # Additional relationship for `MVirtualType`:
148 # * `(:MVirtualType)-[:PROPERTY]->(:MProperty)`: associated property that
149 # determines the type (usually a `MVirtualTypeProp`).
151 # Additional attribute and relationship for `MParameterType`:
153 # * `rank`: position of the parameter (0 for the first parameter).
154 # * `(:MParameterType)-[:CLASS]->(:MClass)`: generic class where the parameter
157 # Additional relationship for `MNullableType`:
159 # * `(:MNullableType)-[:TYPE]->(:MType)`: base type of the nullable type.
161 # Additional attribute and relationships for `MSignature`:
163 # * `parameter_names`: JSON array representing the list of the parameter names.
164 # * `(:MSignature)-[:PARAMETER]->(:MParameter)`: parameters.
165 # * `(:MSignature)-[:RETURNTYPE]->(:MType)`: return type. Does not exist for
168 # In order to maintain the correct parameters order, each `MSignature` node
169 # contains an array of parameter names corresponding to the parameter order in
172 # For example, if the source code contains:
174 # fun foo(a: A, b: B, c: C)
176 # The `MSignature` node will contain a property
177 # `parameter_names = ["a", "b", "c"]` so the MSignature can be reconstructed
178 # with the parameters in the correct order.
182 # * labels: `MParameter`, `model_name` and `MEntity`.
183 # * `is_vararg`: Is the parameter a vararg?
184 # * `rank`: position of the parameter (0 for the first parameter).
185 # * `(:MParameter)-[:TYPE]->(:MType)`: static type of the parameter.
187 # MParameters are also ranked by their position in the corresponding signature.
188 # Rank 0 for the first parameter, 1 for the next one and etc.
195 # Helper class that can save and load a `Model` into a Neo4j database.
200 # Because we use only one Neo4j instance to store all the models,
201 # we need to mark their appartenance to a particular model and avoid loading all models.
203 # The name is used as a Neo label on each created nodes and used to load nodes from base.
204 var model_name
: String
206 # The toolcontext used to init the `NeoModel` tool.
207 var toolcontext
: ToolContext
209 # The Neo4j `client` used to communicate with the Neo4j instance.
210 var client
: Neo4jClient
212 # Fill `model` using base pointed by `client`.
213 fun load
(model
: Model): Model do
214 toolcontext
.info
("Locate all mentities...", 1)
215 var nodes
= client
.nodes_with_label
(model_name
)
217 toolcontext
.info
("Preload nodes...", 1)
218 pull_all_nodes
(nodes
)
219 toolcontext
.info
("Preload edges...", 1)
220 pull_all_edges
(nodes
)
222 toolcontext
.info
("Build model...", 1)
223 nodes
= client
.nodes_with_labels
([model_name
, "MProject"])
224 for node
in nodes
do to_mproject
(model
, node
)
225 nodes
= client
.nodes_with_labels
([model_name
, "MGroup"])
226 for node
in nodes
do to_mgroup
(model
, node
)
227 nodes
= client
.nodes_with_labels
([model_name
, "MModule"])
228 for node
in nodes
do to_mmodule
(model
, node
)
229 nodes
= client
.nodes_with_labels
([model_name
, "MClass"])
230 for node
in nodes
do to_mclass
(model
, node
)
231 nodes
= client
.nodes_with_labels
([model_name
, "MClassDef"])
232 for node
in nodes
do to_mclassdef
(model
, node
)
233 nodes
= client
.nodes_with_labels
([model_name
, "MProperty"])
234 for node
in nodes
do to_mproperty
(model
, node
)
235 nodes
= client
.nodes_with_labels
([model_name
, "MPropDef"])
236 for node
in nodes
do to_mpropdef
(model
, node
)
240 # Save `model` in the base pointed by `client`.
241 fun save
(model
: Model) do
242 var nodes
= collect_model_nodes
(model
)
243 toolcontext
.info
("Save {nodes.length} nodes...", 1)
245 var edges
= collect_model_edges
(model
)
246 toolcontext
.info
("Save {edges.length} edges...", 1)
250 # Save `neo_entities` in base using batch mode.
251 private fun push_all
(neo_entities
: Collection[NeoEntity]) do
252 var batch
= new NeoBatch(client
)
253 var len
= neo_entities
.length
256 for nentity
in neo_entities
do
257 batch
.save_entity
(nentity
)
258 if i
== batch_max_size
then
260 sum
+= batch_max_size
261 toolcontext
.info
(" {sum * 100 / len}% done", 1)
262 batch
= new NeoBatch(client
)
271 # Load content for all `nodes` from base.
273 # Content corresponds to properties and labels that are loaded in batch mode.
274 private fun pull_all_nodes
(nodes
: Collection[NeoNode]) do
275 var batch
= new NeoBatch(client
)
276 var len
= nodes
.length
280 batch
.load_node
(node
)
281 if i
== batch_max_size
then
283 sum
+= batch_max_size
284 toolcontext
.info
(" {sum * 100 / len}% done", 1)
285 batch
= new NeoBatch(client
)
294 # Load all edges from base linked to `nodes`.
296 # Edges are loaded in batch mode.
297 private fun pull_all_edges
(nodes
: Collection[NeoNode]) do
298 var batch
= new NeoBatch(client
)
299 var len
= nodes
.length
303 batch
.load_node_edges
(node
)
304 if i
== batch_max_size
then
306 sum
+= batch_max_size
307 toolcontext
.info
(" {sum * 100 / len}% done", 1)
308 batch
= new NeoBatch(client
)
317 # How many operation can be executed in one batch?
318 private var batch_max_size
= 1000
320 # Execute `batch` and check for errors.
322 # Abort if `batch.execute` returns errors.
323 private fun do_batch
(batch
: NeoBatch) do
324 var errors
= batch
.execute
325 if not errors
.is_empty
then
331 # Collect all nodes from the current `model`.
332 private fun collect_model_nodes
(model
: Model): Collection[NeoNode] do
333 for mproject
in model
.mprojects
do
335 for mgroup
in mproject
.mgroups
do to_node
(mgroup
)
340 # Collect all edges from the current `model`.
342 # Actually collect all out_edges from all nodes.
343 private fun collect_model_edges
(model
: Model): Collection[NeoEdge] do
344 var edges
= new HashSet[NeoEdge]
345 for node
in nodes
.values
do edges
.add_all
(node
.out_edges
)
349 # Mentities associated to nodes.
350 private var mentities
= new HashMap[NeoNode, MEntity]
352 # Nodes associated with MEntities.
353 private var nodes
= new HashMap[MEntity, NeoNode]
355 # Get the `NeoNode` associated with `mentity`.
356 # `mentities` are stored locally to avoid duplication.
357 fun to_node
(mentity
: MEntity): NeoNode do
358 if nodes
.has_key
(mentity
) then return nodes
[mentity
]
359 if mentity
isa MProject then return mproject_node
(mentity
)
360 if mentity
isa MGroup then return mgroup_node
(mentity
)
361 if mentity
isa MModule then return mmodule_node
(mentity
)
362 if mentity
isa MClass then return mclass_node
(mentity
)
363 if mentity
isa MClassDef then return mclassdef_node
(mentity
)
364 if mentity
isa MProperty then return mproperty_node
(mentity
)
365 if mentity
isa MPropDef then return mpropdef_node
(mentity
)
366 if mentity
isa MType then return mtype_node
(mentity
)
367 if mentity
isa MParameter then return mparameter_node
(mentity
)
371 # Make a new `NeoNode` based on `mentity`.
372 private fun make_node
(mentity
: MEntity): NeoNode do
373 var node
= new NeoNode
374 nodes
[mentity
] = node
375 node
.labels
.add
"MEntity"
376 node
.labels
.add model_name
377 node
["name"] = mentity
.name
378 if mentity
.mdoc
!= null then node
["mdoc"] = new JsonArray.from
(mentity
.mdoc
.content
)
382 # Build a `NeoNode` representing `mproject`.
383 private fun mproject_node
(mproject
: MProject): NeoNode do
384 var node
= make_node
(mproject
)
385 node
.labels
.add
"MProject"
386 var root
= mproject
.root
388 node
.out_edges
.add
(new NeoEdge(node
, "ROOT", to_node
(root
)))
393 # Build a new `MProject` from a `node`.
395 # REQUIRE `node.labels.has("MProject")`
396 private fun to_mproject
(model
: Model, node
: NeoNode): MProject do
397 if mentities
.has_key
(node
) then return mentities
[node
].as(MProject)
398 assert node
.labels
.has
("MProject")
399 var mproject
= new MProject(node
["name"].to_s
, model
)
400 mentities
[node
] = mproject
401 set_doc
(node
, mproject
)
402 mproject
.root
= to_mgroup
(model
, node
.out_nodes
("ROOT").first
)
406 # Build a `NeoNode` representing `mgroup`.
407 private fun mgroup_node
(mgroup
: MGroup): NeoNode do
408 var node
= make_node
(mgroup
)
409 node
.labels
.add
"MGroup"
410 node
["full_name"] = mgroup
.full_name
411 var parent
= mgroup
.parent
412 node
.out_edges
.add
(new NeoEdge(node
, "PROJECT", to_node
(mgroup
.mproject
)))
413 if parent
!= null then
414 node
.out_edges
.add
(new NeoEdge(node
, "PARENT", to_node
(parent
)))
416 for mmodule
in mgroup
.mmodules
do
417 node
.out_edges
.add
(new NeoEdge(node
, "DECLARES", to_node
(mmodule
)))
419 for subgroup
in mgroup
.in_nesting
.direct_smallers
do
420 node
.in_edges
.add
(new NeoEdge(node
, "NESTS", to_node
(subgroup
)))
425 # Build a new `MGroup` from a `node`.
427 # REQUIRE `node.labels.has("MGroup")`
428 private fun to_mgroup
(model
: Model, node
: NeoNode): MGroup do
429 if mentities
.has_key
(node
) then return mentities
[node
].as(MGroup)
430 assert node
.labels
.has
("MGroup")
431 var mproject
= to_mproject
(model
, node
.out_nodes
("PROJECT").first
)
432 var parent
: nullable MGroup = null
433 var out
= node
.out_nodes
("PARENT")
434 if not out
.is_empty
then
435 parent
= to_mgroup
(model
, out
.first
)
437 var mgroup
= new MGroup(node
["name"].to_s
, mproject
, parent
)
438 mentities
[node
] = mgroup
439 set_doc
(node
, mgroup
)
443 # Build a `NeoNode` representing `mmodule`.
444 private fun mmodule_node
(mmodule
: MModule): NeoNode do
445 var node
= make_node
(mmodule
)
446 node
.labels
.add
"MModule"
447 node
["full_name"] = mmodule
.full_name
448 node
["location"] = mmodule
.location
.to_s
449 for parent
in mmodule
.in_importation
.direct_greaters
do
450 node
.out_edges
.add
(new NeoEdge(node
, "IMPORTS", to_node
(parent
)))
452 for mclass
in mmodule
.intro_mclasses
do
453 node
.out_edges
.add
(new NeoEdge(node
, "INTRODUCES", to_node
(mclass
)))
455 for mclassdef
in mmodule
.mclassdefs
do
456 node
.out_edges
.add
(new NeoEdge(node
, "DEFINES", to_node
(mclassdef
)))
461 # Build a new `MModule` from a `node`.
463 # REQUIRE `node.labels.has("MModule")`
464 private fun to_mmodule
(model
: Model, node
: NeoNode): MModule do
465 if mentities
.has_key
(node
) then return mentities
[node
].as(MModule)
466 assert node
.labels
.has
("MModule")
467 var ins
= node
.in_nodes
("DECLARES")
468 var mgroup
: nullable MGroup = null
469 if not ins
.is_empty
then
470 mgroup
= to_mgroup
(model
, ins
.first
)
472 var name
= node
["name"].to_s
473 var location
= to_location
(node
["location"].to_s
)
474 var mmodule
= new MModule(model
, mgroup
, name
, location
)
475 mentities
[node
] = mmodule
476 set_doc
(node
, mmodule
)
477 var imported_mmodules
= new Array[MModule]
478 for smod
in node
.out_nodes
("IMPORTS") do
479 imported_mmodules
.add to_mmodule
(model
, smod
)
481 mmodule
.set_imported_mmodules
(imported_mmodules
)
485 # Build a `NeoNode` representing `mclass`.
486 private fun mclass_node
(mclass
: MClass): NeoNode do
487 var node
= make_node
(mclass
)
488 node
.labels
.add
"MClass"
489 node
["full_name"] = mclass
.full_name
490 node
["kind"] = mclass
.kind
.to_s
491 node
["visibility"] = mclass
.visibility
.to_s
492 if not mclass
.mparameters
.is_empty
then
493 var parameter_names
= new Array[String]
494 for p
in mclass
.mparameters
do parameter_names
.add
(p
.name
)
495 node
["parameter_names"] = new JsonArray.from
(parameter_names
)
497 node
.out_edges
.add
(new NeoEdge(node
, "CLASSTYPE", to_node
(mclass
.mclass_type
)))
501 # Build a new `MClass` from a `node`.
503 # REQUIRE `node.labels.has("MClass")`
504 private fun to_mclass
(model
: Model, node
: NeoNode): MClass do
505 if mentities
.has_key
(node
) then return mentities
[node
].as(MClass)
506 assert node
.labels
.has
("MClass")
507 var mmodule
= to_mmodule
(model
, node
.in_nodes
("INTRODUCES").first
)
508 var name
= node
["name"].to_s
509 var kind
= to_kind
(node
["kind"].to_s
)
510 var visibility
= to_visibility
(node
["visibility"].to_s
)
511 var parameter_names
= new Array[String]
512 if node
.has_key
("parameter_names") then
513 for e
in node
["parameter_names"].as(JsonArray) do
514 parameter_names
.add e
.to_s
517 var mclass
= new MClass(mmodule
, name
, parameter_names
, kind
, visibility
)
518 mentities
[node
] = mclass
519 set_doc
(node
, mclass
)
523 # Build a `NeoNode` representing `mclassdef`.
524 private fun mclassdef_node
(mclassdef
: MClassDef): NeoNode do
525 var node
= make_node
(mclassdef
)
526 node
.labels
.add
"MClassDef"
527 node
["is_intro"] = mclassdef
.is_intro
528 node
["location"] = mclassdef
.location
.to_s
529 node
.out_edges
.add
(new NeoEdge(node
, "BOUNDTYPE", to_node
(mclassdef
.bound_mtype
)))
530 node
.out_edges
.add
(new NeoEdge(node
, "MCLASS", to_node
(mclassdef
.mclass
)))
531 for mproperty
in mclassdef
.intro_mproperties
do
532 node
.out_edges
.add
(new NeoEdge(node
, "INTRODUCES", to_node
(mproperty
)))
534 for mpropdef
in mclassdef
.mpropdefs
do
535 node
.out_edges
.add
(new NeoEdge(node
, "DECLARES", to_node
(mpropdef
)))
537 for sup
in mclassdef
.supertypes
do
538 node
.out_edges
.add
(new NeoEdge(node
, "INHERITS", to_node
(sup
)))
543 # Build a new `MClassDef` from a `node`.
545 # REQUIRE `node.labels.has("MClassDef")`
546 private fun to_mclassdef
(model
: Model, node
: NeoNode): MClassDef do
547 if mentities
.has_key
(node
) then return mentities
[node
].as(MClassDef)
548 assert node
.labels
.has
("MClassDef")
549 var mmodule
= to_mmodule
(model
, node
.in_nodes
("DEFINES").first
)
550 var mtype
= to_mtype
(model
, node
.out_nodes
("BOUNDTYPE").first
).as(MClassType)
551 var location
= to_location
(node
["location"].to_s
)
552 var mclassdef
= new MClassDef(mmodule
, mtype
, location
)
553 mentities
[node
] = mclassdef
554 set_doc
(node
, mclassdef
)
555 var supertypes
= new Array[MClassType]
556 for sup
in node
.out_nodes
("INHERITS") do
557 supertypes
.add to_mtype
(model
, sup
).as(MClassType)
559 mclassdef
.set_supertypes
(supertypes
)
560 mclassdef
.add_in_hierarchy
564 # Build a `NeoNode` representing `mproperty`.
565 private fun mproperty_node
(mproperty
: MProperty): NeoNode do
566 var node
= make_node
(mproperty
)
567 node
.labels
.add
"MProperty"
568 node
["full_name"] = mproperty
.full_name
569 node
["visibility"] = mproperty
.visibility
.to_s
570 if mproperty
isa MMethod then
571 node
.labels
.add
"MMethod"
572 node
["is_init"] = mproperty
.is_init
573 else if mproperty
isa MAttribute then
574 node
.labels
.add
"MAttribute"
575 else if mproperty
isa MVirtualTypeProp then
576 node
.labels
.add
"MVirtualTypeProp"
578 node
.out_edges
.add
(new NeoEdge(node
, "INTRO_CLASSDEF", to_node
(mproperty
.intro_mclassdef
)))
582 # Build a new `MProperty` from a `node`.
584 # REQUIRE `node.labels.has("MProperty")`
585 private fun to_mproperty
(model
: Model, node
: NeoNode): MProperty do
586 if mentities
.has_key
(node
) then return mentities
[node
].as(MProperty)
587 assert node
.labels
.has
("MProperty")
588 var intro_mclassdef
= to_mclassdef
(model
, node
.out_nodes
("INTRO_CLASSDEF").first
)
589 var name
= node
["name"].to_s
590 var visibility
= to_visibility
(node
["visibility"].to_s
)
591 var mprop
: nullable MProperty = null
592 if node
.labels
.has
("MMethod") then
593 mprop
= new MMethod(intro_mclassdef
, name
, visibility
)
594 mprop
.is_init
= node
["is_init"].as(Bool)
595 else if node
.labels
.has
("MAttribute") then
596 mprop
= new MAttribute(intro_mclassdef
, name
, visibility
)
597 else if node
.labels
.has
("MVirtualTypeProp") then
598 mprop
= new MVirtualTypeProp(intro_mclassdef
, name
, visibility
)
600 if mprop
== null then
601 print
"not yet implemented to_mproperty for {node.labels.join(",")}"
604 mentities
[node
] = mprop
606 for npropdef
in node
.in_nodes
("DEFINES") do
607 var mpropdef
= to_mpropdef
(model
, npropdef
)
608 if npropdef
["is_intro"].as(Bool) then
609 mprop
.mpropdefs
.unshift mpropdef
611 mprop
.mpropdefs
.add mpropdef
617 # Build a `NeoNode` representing `mpropdef`.
618 private fun mpropdef_node
(mpropdef
: MPropDef): NeoNode do
619 var node
= make_node
(mpropdef
)
620 node
.labels
.add
"MPropDef"
621 node
["is_intro"] = mpropdef
.is_intro
622 node
["location"] = mpropdef
.location
.to_s
623 node
.out_edges
.add
(new NeoEdge(node
, "DEFINES", to_node
(mpropdef
.mproperty
)))
624 if mpropdef
isa MMethodDef then
625 node
.labels
.add
"MMethodDef"
626 node
["is_abstract"] = mpropdef
.is_abstract
627 node
["is_intern"] = mpropdef
.is_intern
628 node
["is_extern"] = mpropdef
.is_extern
629 var msignature
= mpropdef
.msignature
630 if msignature
!= null then
631 node
.out_edges
.add
(new NeoEdge(node
, "SIGNATURE", to_node
(msignature
)))
633 else if mpropdef
isa MAttributeDef then
634 node
.labels
.add
"MAttributeDef"
635 else if mpropdef
isa MVirtualTypeDef then
636 node
.labels
.add
"MVirtualTypeDef"
637 var bound
= mpropdef
.bound
638 if bound
!= null then
639 node
.out_edges
.add
(new NeoEdge(node
, "BOUND", to_node
(bound
)))
645 # Build a new `MPropDef` from a `node`.
647 # REQUIRE `node.labels.has("MPropDef")`
648 private fun to_mpropdef
(model
: Model, node
: NeoNode): MPropDef do
649 if mentities
.has_key
(node
) then return mentities
[node
].as(MPropDef)
650 assert node
.labels
.has
("MPropDef")
651 var mclassdef
= to_mclassdef
(model
, node
.in_nodes
("DECLARES").first
)
652 var mproperty
= to_mproperty
(model
, node
.out_nodes
("DEFINES").first
)
653 var location
= to_location
(node
["location"].to_s
)
654 var mpropdef
: nullable MPropDef = null
655 if node
.labels
.has
("MMethodDef") then
656 mpropdef
= new MMethodDef(mclassdef
, mproperty
.as(MMethod), location
)
657 mpropdef
.is_abstract
= node
["is_abstract"].as(Bool)
658 mpropdef
.is_intern
= node
["is_intern"].as(Bool)
659 mpropdef
.is_extern
= node
["is_extern"].as(Bool)
660 mentities
[node
] = mpropdef
661 mpropdef
.msignature
= to_mtype
(model
, node
.out_nodes
("SIGNATURE").first
).as(MSignature)
662 else if node
.labels
.has
("MAttributeDef") then
663 mpropdef
= new MAttributeDef(mclassdef
, mproperty
.as(MAttribute), location
)
664 mentities
[node
] = mpropdef
665 else if node
.labels
.has
("MVirtualTypeDef") then
666 mpropdef
= new MVirtualTypeDef(mclassdef
, mproperty
.as(MVirtualTypeProp), location
)
667 mentities
[node
] = mpropdef
668 var bound
= node
.out_nodes
("BOUND")
669 if not bound
.is_empty
then mpropdef
.bound
= to_mtype
(model
, bound
.first
)
671 if mpropdef
== null then
672 print
"not yet implemented to_mpropdef for {node.labels.join(",")}"
675 set_doc
(node
, mpropdef
)
679 # Build a `NeoNode` representing `mtype`.
680 private fun mtype_node
(mtype
: MType): NeoNode do
681 var node
= make_node
(mtype
)
682 node
.labels
.add
"MType"
683 if mtype
isa MClassType then
684 node
.labels
.add
"MClassType"
685 node
.out_edges
.add
(new NeoEdge(node
, "CLASS", to_node
(mtype
.mclass
)))
686 for arg
in mtype
.arguments
do
687 node
.out_edges
.add
(new NeoEdge(node
, "ARGUMENT", to_node
(arg
)))
689 if mtype
isa MGenericType then
690 node
.labels
.add
"MGenericType"
692 else if mtype
isa MVirtualType then
693 node
.labels
.add
"MVirtualType"
694 node
.out_edges
.add
(new NeoEdge(node
, "PROPERTY", to_node
(mtype
.mproperty
)))
695 else if mtype
isa MParameterType then
696 node
.labels
.add
"MParameterType"
697 node
["rank"] = mtype
.rank
698 node
.out_edges
.add
(new NeoEdge(node
, "CLASS", to_node
(mtype
.mclass
)))
699 else if mtype
isa MNullableType then
700 node
.labels
.add
"MNullableType"
701 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(mtype
.mtype
)))
702 else if mtype
isa MSignature then
703 node
.labels
.add
"MSignature"
704 var names
= new JsonArray
706 for mparameter
in mtype
.mparameters
do
707 names
.add mparameter
.name
708 var pnode
= to_node
(mparameter
)
710 node
.out_edges
.add
(new NeoEdge(node
, "PARAMETER", pnode
))
712 if not names
.is_empty
then node
["parameter_names"] = names
713 var return_mtype
= mtype
.return_mtype
714 if return_mtype
!= null then
715 node
.out_edges
.add
(new NeoEdge(node
, "RETURNTYPE", to_node
(return_mtype
)))
721 # Build a new `MType` from a `node`.
723 # REQUIRE `node.labels.has("MType")`
724 private fun to_mtype
(model
: Model, node
: NeoNode): MType do
725 if mentities
.has_key
(node
) then return mentities
[node
].as(MType)
726 assert node
.labels
.has
("MType")
727 if node
.labels
.has
("MClassType") then
728 var mclass
= to_mclass
(model
, node
.out_nodes
("CLASS").first
)
729 var args
= new Array[MType]
730 for narg
in node
.out_nodes
("ARGUMENT") do
731 args
.add to_mtype
(model
, narg
)
733 var mtype
= mclass
.get_mtype
(args
)
734 mentities
[node
] = mtype
736 else if node
.labels
.has
("MParameterType") then
737 var mclass
= to_mclass
(model
, node
.out_nodes
("CLASS").first
)
738 var rank
= node
["rank"].to_s
.to_i
739 var mtype
= mclass
.mparameters
[rank
]
740 mentities
[node
] = mtype
742 else if node
.labels
.has
("MNullableType") then
743 var intype
= to_mtype
(model
, node
.out_nodes
("TYPE").first
)
744 var mtype
= intype
.as_nullable
745 mentities
[node
] = mtype
747 else if node
.labels
.has
("MVirtualType") then
748 var mproperty
= to_mproperty
(model
, node
.out_nodes
("PROPERTY").first
)
749 assert mproperty
isa MVirtualTypeProp
750 var mtype
= mproperty
.mvirtualtype
751 mentities
[node
] = mtype
753 else if node
.labels
.has
("MSignature") then
754 # Get all param nodes
755 var mparam_nodes
= new HashMap[String, MParameter]
756 for pnode
in node
.out_nodes
("PARAMETER") do
757 var mparam
= to_mparameter
(model
, pnode
)
758 mparam_nodes
[mparam
.name
] = mparam
760 # Load params in the good order
761 var mparam_names
= node
["parameter_names"]
762 var mparameters
= new Array[MParameter]
763 if mparam_names
isa JsonArray then
764 for mparam_name
in mparam_names
do
765 var mparam
= mparam_nodes
[mparam_name
.to_s
]
766 mparameters
.add mparam
769 var return_mtype
: nullable MType = null
770 var ret_nodes
= node
.out_nodes
("RETURNTYPE")
771 if not ret_nodes
.is_empty
then
772 return_mtype
= to_mtype
(model
, ret_nodes
.first
)
774 var mtype
= new MSignature(mparameters
, return_mtype
)
775 mentities
[node
] = mtype
778 print
"not yet implemented to_mtype for {node.labels.join(",")}"
782 # Build a `NeoNode` representing `mparameter`.
783 private fun mparameter_node
(mparameter
: MParameter): NeoNode do
784 var node
= make_node
(mparameter
)
785 node
.labels
.add
"MParameter"
786 node
["name"] = mparameter
.name
787 node
["is_vararg"] = mparameter
.is_vararg
788 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(mparameter
.mtype
)))
792 # Build a new `MParameter` from `node`.
794 # REQUIRE `node.labels.has("MParameter")`
795 private fun to_mparameter
(model
: Model, node
: NeoNode): MParameter do
796 if mentities
.has_key
(node
) then return mentities
[node
].as(MParameter)
797 assert node
.labels
.has
("MParameter")
798 var name
= node
["name"].to_s
799 var mtype
= to_mtype
(model
, node
.out_nodes
("TYPE").first
)
800 var is_vararg
= node
["is_vararg"].as(Bool)
801 var mparameter
= new MParameter(name
, mtype
, is_vararg
)
802 mentities
[node
] = mparameter
806 # Get a `Location` from its string representation.
807 private fun to_location
(loc
: String): Location do
809 var parts
= loc
.split_with
(":")
810 var file
= new SourceFile.from_string
(parts
[0], "")
811 var pos
= parts
[1].split_with
("--")
812 var pos1
= pos
[0].split_with
(",")
813 var pos2
= pos
[1].split_with
(",")
814 var line_s
= pos1
[0].to_i
815 var line_e
= pos2
[0].to_i
816 var column_s
= pos1
[1].to_i
818 if pos2
.length
== 2 then pos2
[1].to_i
819 return new Location(file
, line_s
, line_e
, column_s
, column_e
)
822 # Get a `MVisibility` from its string representation.
823 private fun to_visibility
(vis
: String): MVisibility do
824 if vis
== intrude_visibility
.to_s
then
825 return intrude_visibility
826 else if vis
== public_visibility
.to_s
then
827 return public_visibility
828 else if vis
== protected_visibility
.to_s
then
829 return protected_visibility
830 else if vis
== private_visibility
.to_s
then
831 return private_visibility
833 return none_visibility
837 # Get a `MKind` from its string representation.
838 private fun to_kind
(kind
: String): MClassKind do
839 if kind
== abstract_kind
.to_s
then
841 else if kind
== concrete_kind
.to_s
then
843 else if kind
== interface_kind
.to_s
then
844 return interface_kind
845 else if kind
== enum_kind
.to_s
then
847 else if kind
== extern_kind
.to_s
then
853 # Extract the `MDoc` from `node` and link it to `mentity`.
854 private fun set_doc
(node
: NeoNode, mentity
: MEntity) do
855 if node
.has_key
("mdoc") then
856 var lines
= new Array[String]
857 for e
in node
["mdoc"].as(JsonArray) do
858 lines
.add e
.to_s
#.replace("\n", "\\n")
861 mdoc
.content
.add_all
(lines
)
862 mdoc
.original_mentity
= mentity