model: introduce MConcern
[nit.git] / src / model / mproject.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Modelisation of a Nit project
16 module mproject
17
18 import model_base
19 private import more_collections
20 import poset
21
22 # A Nit project, thas encompass a product
23 class MProject
24 super MConcern
25
26 # The name of the project
27 redef var name: String
28
29 # The model of the project
30 var model: Model
31
32 # The root of the group tree
33 var root: nullable MGroup writable = null
34
35 # The group tree, as a POSet
36 var mgroups = new POSet[MGroup]
37
38 redef fun to_s do return name
39
40 init(name: String, model: Model)
41 do
42 self.name = name
43 self.model = model
44 model.mprojects.add(self)
45 model.mproject_by_name.add_one(name, self)
46 end
47
48 # MProject are always roots of the concerns hierarchy
49 redef fun parent_concern do return null
50 end
51
52 # A group of modules in a project
53 class MGroup
54 super MConcern
55
56 # The name of the group
57 # empty name for a default group in a single-module project
58 redef var name: String
59
60 # The englobing project
61 var mproject: MProject
62
63 # The parent group if any
64 # see `in_nesting` for more
65 var parent: nullable MGroup
66
67 # fully qualified name
68 fun full_name: String
69 do
70 var p = parent
71 if p == null then return name
72 return "{p.full_name}/{name}"
73 end
74
75 # The group is the group tree on the project (`mproject.mgroups`)
76 # nested groups (children) are smallers
77 # nesting group (see `parent`) is bigger
78 var in_nesting: POSetElement[MGroup]
79
80 # Is `self` the root of its project?
81 fun is_root: Bool do return mproject.root == self
82
83 # The filepath (usualy a directory) of the group, if any
84 var filepath: nullable String writable
85
86 init (name: String, mproject: MProject, parent: nullable MGroup)
87 do
88 self.name = name
89 self.mproject = mproject
90 self.parent = parent
91 var tree = mproject.mgroups
92 self.in_nesting = tree.add_node(self)
93 if parent != null then
94 tree.add_edge(self, parent)
95 end
96 end
97
98 redef fun parent_concern do
99 if not is_root then return parent
100 return mproject
101 end
102
103 redef fun to_s do return name
104 end
105
106 redef class Model
107 # projects of the model
108 var mprojects = new Array[MProject]
109
110 # Collections of project grouped by their names
111 private var mproject_by_name: MultiHashMap[String, MProject] = new MultiHashMap[String, MProject]
112
113 # Return all project named `name`
114 # If such a project is not yet loaded, null is returned (instead of an empty array)
115 fun get_mprojects_by_name(name: String): nullable Array[MProject]
116 do
117 if mproject_by_name.has_key(name) then
118 return mproject_by_name[name]
119 else
120 return null
121 end
122 end
123 end