4481b2eef8f1cb5394587d874c24573b05b6713d
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 # Graphs and basic entities.
23 var all_nodes
: SimpleCollection[NeoNode] = new Array[NeoNode]
24 var all_edges
: SimpleCollection[NeoEdge] = new Array[NeoEdge]
26 # Add a relationship between two nodes.
28 # Parameters are the same than for the constructor of `NeoEdge`.
29 fun add_edge
(from
: NeoNode, rel_type
: String, to
: NeoNode) do
30 all_edges
.add
(new NeoEdge(from
, rel_type
, to
))
34 # The project’s graph.
38 # The node reperesenting the project.
40 # Once the project’s graph is initialized, this node must not be edited.
41 var project
= new NeoNode
43 # Entities by `model_id`.
44 var by_id
: Map[String, Entity] = new HashMap[String, Entity]
46 # Initialize a new project graph using the specified project name.
48 # The specified name will label all nodes of the project’s graph.
50 project
.labels
.add
(name
)
51 project
.labels
.add
("MEntity")
52 project
.labels
.add
("MProject")
53 project
["name"] = name
54 all_nodes
.add
(project
)
56 var root
= new RootNamespace(self)
61 # Request to all nodes in the graph to add their related edges.
64 add_edge
(project
, "ROOT", by_id
[""])
75 # In practice, this is the base class of every node in a `ProjectGraph`.
79 # Graph that will embed the entity.
80 var graph
: ProjectGraph
82 # ID of the entity in the model.
84 # Is empty for entities without an ID.
85 var model_id
: String = "" is writable
87 # Associated documentation.
88 var doc
= new JsonArray is writable
91 self.labels
.add
(graph
.project
["name"].to_s
)
92 self.labels
.add
("MEntity")
95 # The short (unqualified) name.
97 # May be also set by `full_name=`.
98 fun name
=(name
: String) do
102 # The short (unqualified) name.
104 var name
= self["name"]
105 assert name
isa String
109 # Include the documentation of `self` in the graph.
110 protected fun set_mdoc
do
114 # The namespace separator of Nit/C++.
115 fun ns_separator
: String do return "::"
117 # The name separator used when calling `full_name=`.
118 fun name_separator
: String do return ns_separator
120 # The full (qualified) name.
122 # Also set `name` using `name_separator`.
123 fun full_name
=(full_name
: String) do
124 var m
: nullable Match = full_name
.search_last
(name_separator
)
126 self["full_name"] = full_name
130 name
= full_name
.substring_from
(m
.after
)
134 # The full (qualified) name.
135 fun full_name
: String do
136 var full_name
= self["full_name"]
137 assert full_name
isa String
141 # Set the full name using the current name and the specified parent name.
142 fun parent_name
=(parent_name
: String) do
143 self["full_name"] = parent_name
+ name_separator
+ self["name"].as(not null).to_s
146 # Set the location of the entity in the source code.
147 fun location
=(location
: nullable Location) do
148 self["location"] = location
151 # Put the entity in the graph.
153 # Called by the loader when it has finished to read the entity.
155 if doc
.length
> 0 then
158 graph
.all_nodes
.add
(self)
159 if model_id
!= "" then graph
.by_id
[model_id
] = self
162 # Put the related edges in the graph.
164 # This method is called on each node by `ProjectGraph.put_edges`.
166 # Note: Even at this step, the entity may modify its own attributes and
167 # inner entities’ ones because some values are only known once the entity
168 # know its relationships with the rest of the graph.
172 # An entity whose the location is mandatory.
173 abstract class CodeBlock
177 self["location"] = new Location
180 redef fun location
=(location
: nullable Location) do
181 if location
== null then
191 # Usually corresponds to a `<compounddef>` element in of the XML output of
193 abstract class Compound
196 # Set the declared visibility (the proctection) of the compound.
197 fun visibility
=(visibility
: String) do
198 self["visibility"] = visibility
201 # Set the specific kind of the compound.
202 fun kind
=(kind
: String) do
206 # Declare an inner namespace.
208 # Note: Althought Doxygen indicates that the name is optional,
209 # declarations with an empty name are not supported yet.
213 # * `id`: `model_id` of the inner namespace. May be empty.
214 # * `full_name`: qualified name of the inner namespace.
215 fun declare_namespace
(id
: String, full_name
: String) do end
217 # Declare an inner class.
219 # Note: Althought Doxygen indicates that both arguments are optional,
220 # declarations with either an empty name or an empty ID are not
225 # * `id`: `model_id` of the inner class.
226 # * `full_name`: qualified name of the inner class.
227 fun declare_class
(id
: String, full_name
: String) do end
229 # Declare a base compound (usually, a base class).
233 # * `id`: `model_id` of the base compound. May be empty.
234 # * `full_name`: qualified name of the base compound. May be empty.
235 # * `prot`: visibility (proctection) of the relationship.
236 # * `virt`: level of virtuality of the relationship.
237 fun declare_super
(id
: String, full_name
: String, prot
: String,
241 # An unrecognized compound.
243 # Used to simplify the handling of ignored entities.
244 class UnknownCompound
247 redef fun put_in_graph
do end
248 redef fun put_edges
do end
253 # Corresponds to a group in Nit.
257 # Inner namespaces (IDs).
259 # Left empty for the root namespace.
260 var inner_namespaces
: SimpleCollection[String] = new Array[String]
264 self.labels
.add
("MGroup")
267 redef fun declare_namespace
(id
: String, name
: String) do
268 inner_namespaces
.add
(id
)
271 redef fun put_edges
do
273 graph
.add_edge
(self, "PROJECT", graph
.project
)
274 if self["name"] == self["full_name"] and self["full_name"] != "" then
275 # The root namespace does not know its children.
276 var root
= graph
.by_id
[""]
277 graph
.add_edge
(self, "PARENT", root
)
278 graph
.add_edge
(root
, "NESTS", self)
280 for ns
in inner_namespaces
do
281 var node
= graph
.by_id
[ns
]
282 graph
.add_edge
(node
, "PARENT", self)
283 graph
.add_edge
(self, "NESTS", node
)
288 # The root namespace of a `ProjectGraph`.
290 # This the only entity in the graph whose `model_id` is really `""`.
291 # Added automatically at the initialization of a `ProjectGraph`.
297 self["full_name"] = ""
298 self["name"] = graph
.project
["name"]
301 redef fun declare_namespace
(id
: String, name
: String) do end