model: add mproject module
authorJean Privat <jean@pryen.org>
Tue, 4 Feb 2014 15:00:37 +0000 (10:00 -0500)
committerJean Privat <jean@pryen.org>
Wed, 5 Feb 2014 15:28:18 +0000 (10:28 -0500)
mmodule uses it but the API is not changed yet

Change-Id: Ie8efdc51014530966a7398bd25c946589d213df1
Signed-off-by: Jean Privat <jean@pryen.org>

src/model/mmodule.nit
src/model/mproject.nit [new file with mode: 0644]

index e3fda2d..a3ee105 100644 (file)
@@ -19,7 +19,7 @@ module mmodule
 
 import poset
 import location
-import model_base
+import mproject
 private import more_collections
 
 # The container class of a Nit object-oriented model.
@@ -52,8 +52,12 @@ redef class Model
        end
 end
 
+redef class MGroup
+       # The loaded modules of this group
+       var mmodules = new Array[MModule]
+end
+
 # A Nit module is usually associated with a Nit source file.
-# Modules can be nested (see `direct_owner`, `public_owner`, and `in_nesting`)
 class MModule
        # The model considered
        var model: Model
@@ -61,6 +65,9 @@ class MModule
        # The direct nesting module, return null if self is not nested (ie. is a top-level module)
        var direct_owner: nullable MModule
 
+       # The group of module in the project if any
+       var mgroup: nullable MGroup
+
        # The short name of the module
        var name: String
 
@@ -77,14 +84,14 @@ class MModule
        var in_importation: POSetElement[MModule]
 
        # The canonical name of the module
-       # Example: `"owner::name"`
+       # Example: `"project::name"`
        fun full_name: String
        do
-               var owner = self.public_owner
-               if owner == null then
+               var mgroup = self.mgroup
+               if mgroup == null or mgroup.mproject.name == self.name then
                        return self.name
                else
-                       return "{owner.full_name}::{self.name}"
+                       return "{mgroup.mproject.name}::{self.name}"
                end
        end
 
diff --git a/src/model/mproject.nit b/src/model/mproject.nit
new file mode 100644 (file)
index 0000000..9c1765f
--- /dev/null
@@ -0,0 +1,108 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Modelisation of a Nit project
+module mproject
+
+import model_base
+private import more_collections
+import poset
+
+# A Nit project, thas encompass a product
+class MProject
+       # The name of the project
+       var name: String
+
+       # The model of the project
+       var model: Model
+
+       # The root of the group tree
+       var root: nullable MGroup writable = null
+
+       # The group tree, as a POSet
+       var mgroups = new POSet[MGroup]
+
+       redef fun to_s do return name
+
+       init(name: String, model: Model)
+       do
+               self.name = name
+               self.model = model
+               model.mprojects.add(self)
+               model.mproject_by_name.add_one(name, self)
+       end
+end
+
+# A group of modules in a project
+class MGroup
+       # The name of the group
+       # empty name for a default group in a single-module project
+       var name: String
+
+       # The englobing project
+       var mproject: MProject
+
+       # The parent group if any
+       # see `in_nesting` for more
+       var parent: nullable MGroup
+
+       # fully qualified name
+       fun full_name: String
+       do
+               var p = parent
+               if p == null then return name
+               return "{p.full_name}/{name}"
+       end
+
+       # The group is the group tree on the project (`mproject.mgroups`)
+       # nested groups (children) are smallers
+       # nesting group (see `parent`) is bigger
+       var in_nesting: POSetElement[MGroup]
+
+       # The filepath (usualy a directory) of the group, if any
+       var filepath: nullable String writable
+
+       init (name: String, mproject: MProject, parent: nullable MGroup)
+       do
+               self.name = name
+               self.mproject = mproject
+               self.parent = parent
+               var tree = mproject.mgroups
+               self.in_nesting = tree.add_node(self)
+               if parent != null then
+                       tree.add_edge(self, parent)
+               end
+       end
+
+       redef fun to_s do return name
+end
+
+redef class Model
+       # projects of the model
+       var mprojects = new Array[MProject]
+
+       # Collections of project grouped by their names
+       private var mproject_by_name: MultiHashMap[String, MProject] = new MultiHashMap[String, MProject]
+
+       # Return all project named `name`
+       # If such a project is not yet loaded, null is returned (instead of an empty array)
+       fun get_mprojects_by_name(name: String): nullable Array[MProject]
+       do
+               if mproject_by_name.has_key(name) then
+                       return mproject_by_name[name]
+               else
+                       return null
+               end
+       end
+end