model/model_contract: Move contract model representation
[nit.git] / src / model / model_examples.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Examples for Model entities
18 #
19 # This module introduce flags in all mentities so they can be tagged as example.
20 # Flagged classes will be treated differently by the documentation tools.
21 #
22 # See `MEntity::is_example` and `MEntity::examples`.
23 module model_examples
24
25 import model
26
27 # An example from a MEntity flagged with `is example`.
28 class MExample
29
30 redef init do
31 super
32 mentity.mexample = self
33 end
34
35 # MEntity containing this example
36 var mentity: MEntity
37
38 # MEntities this example is for
39 #
40 # For each entity we provide a weight so examples can be ranked.
41 var example_for = new ArrayMap[MEntity, Int]
42
43 redef fun to_s do return mentity.full_name
44 end
45
46 redef class MEntity
47
48 # Is `self` existing for an example purpose?
49 #
50 # All mentities annotated with `is example` or located inside a mentity that
51 # is an example are considered as examples.
52 fun is_example: Bool do return mexample != null
53
54 # Return this entity as a MExample
55 var mexample: nullable MExample = null
56
57 # Examples found for `self`
58 var examples = new Array[MExample]
59 end
60
61 redef class MPackage
62 redef fun examples do
63 var res = super
64 for mgroup in mgroups do
65 for example in mgroup.examples do
66 if not res.has(example) then res.add example
67 end
68 end
69 return res
70 end
71 end
72
73 redef class MGroup
74 redef var is_example is lazy do
75 var parent = self.parent
76 if parent != null and parent.is_example then return true
77 return name == "examples"
78 end
79
80 redef fun examples do
81 var res = super
82 for mmodule in mmodules do
83 for example in mmodule.examples do
84 if not res.has(example) then res.add example
85 end
86 end
87 return res
88 end
89 end
90
91 redef class MClass
92 redef var is_example is lazy do return intro.is_example
93
94 redef fun examples do
95 var res = super
96 for mclassdef in mclassdefs do
97 for example in mclassdef.examples do
98 if not res.has(example) then res.add example
99 end
100 end
101 return res
102 end
103 end
104
105 redef class MProperty
106 redef var is_example is lazy do return intro.is_example
107
108 redef fun examples do
109 var res = super
110 for mpropdef in mpropdefs do
111 for example in mpropdef.examples do
112 if not res.has(example) then res.add example
113 end
114 end
115 return res
116 end
117 end