PEP 55 Update license on source files to current license text and date
[tpot/pegasus/.git] / src / Pegasus / Repository / InheritanceTree.h
1 //%2003////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002  BMC Software, Hewlett-Packard Development
4 // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 // 
15 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
16 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
17 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //==============================================================================
25 //
26 // Author: Mike Brasher (mbrasher@bmc.com)
27 //
28 // Modified By: Carol Ann Krug Graves, Hewlett-Packard Company
29 //                (carolann_graves@hp.com)
30 //
31 //%/////////////////////////////////////////////////////////////////////////////
32
33 #ifndef Pegasus_InheritanceTree_h
34 #define Pegasus_InheritanceTree_h
35
36 #include <iostream>
37 #include <Pegasus/Common/Config.h>
38 #include <Pegasus/Common/ArrayInternal.h>
39 #include <Pegasus/Common/CIMName.h>
40 #include <Pegasus/Common/String.h>
41 #include <Pegasus/Common/InternalException.h>
42 #include <Pegasus/Repository/Linkage.h>
43 #include <Pegasus/Common/MessageLoader.h> //l10n
44
45 PEGASUS_NAMESPACE_BEGIN
46
47 struct InheritanceTreeRep;
48
49 /** The InheritanceTree class tracks inheritance relationships of CIM classes.
50
51     This class is a memory resident version of the repository's persistent 
52     inheritance information (represented using file names). The InheritanceTree 
53     provides O(1) access (via hashing) to any class in the inheritance tree.
54
55     The inheritance tree provides methods for interrogating certain kinds of
56     information about a class, including:
57
58         <ul>
59         <li>the superclass</li>
60         <li>the subclasses</li>
61         <li>the descendent classes</li>
62         </ul>
63
64     The insert() method is used to build up an InheritanceTree. The insert()
65     method is called for each class-subclass relationship. For example, consider
66     the following list of class-subclass pairs:
67
68         <pre>
69         { "D", "B" }
70         { "E", "B" }
71         { "B", "A" }
72         { "C", "A" }
73         { "F", "C" }
74         </pre>
75
76     These pairs specify the following inheritance tree:
77
78         <pre>
79               A
80             /   \
81            B     C
82          /   \     \
83         D     E     F
84         </pre>
85
86     The pairs above may be used to build a class tree as follows:
87
88         <pre>
89         InheritanceTree it;
90         it.insert("D", "B");
91         it.insert("E", "B");
92         it.insert("B", "A");
93         it.insert("C", "A");
94         it.insert("F", "C");
95         it.insert("A", "");
96         it.check();
97         </pre>
98
99     The check() method determines whether insert() was called for every class
100     used as a superclass. In the following example, check() would fail (and
101     throw and exception) since the "B" class is passed as a superclass (second
102     argument) in two insert() calls but was never passed as the class itself
103     (first argument) in any insert() call:
104
105         <pre>
106         InheritanceTree it;
107         it.insert("D", "B");
108         it.insert("E", "B");
109         it.insert("C", "A");
110         it.insert("F", "C");
111         it.insert("A", "");
112         it.check();
113         </pre>
114
115     In this case, check() throws an InvalidInheritanceTree exception.
116
117     The InheritanceTree may be printed by calling the print() method.
118
119     The insertFromPath() method is used to build up an InheritanceTree from
120     the file names in a certain directory as used by the CIMRepository. The
121     CIMRepository contains a disk file per class and the name has this form:
122
123         <pre>
124         <ClassName>.<SuperClassName>
125         </pre>
126
127     For example, a class called "ThisClass" with super class "ThatClass" 
128     has this name:
129
130         <pre>
131         ThisClass.ThisClass
132         </pre>
133
134     The file or course contains the XML encoding of the ThisClass class (which 
135     is irrelevant for the InheritanceTree). A root class (with no superclass
136     has the following form):
137
138         <pre>
139         <ClassName>.#
140         </pre>
141
142     Suppose that ThatClass is a root class; then its file name is:
143
144         <pre>
145         ThatClass.#
146         </pre>
147
148     It must be obvious by now that the insertFromPath() method just scans
149     the file names in a directory and calls insert() for each one (splitting
150     the class name from superclass name and translating '#' to an empty string).
151
152     The insertFromPath() method does NOT call check(), so it still must be 
153     called to verify the InheritanceTree.
154 */
155 class PEGASUS_REPOSITORY_LINKAGE InheritanceTree
156 {
157 public:
158
159     /** Default constructor. */
160     InheritanceTree();
161
162     /** Destructor. */
163     ~InheritanceTree();
164
165     /** Inserts a class-subclass relationship into the inheritance three.
166         Note that a class CAN be inserted before its superclass, in which case
167         a provisional entry is made for the superclass and flagged as such; 
168         when the superclass is later inserted, the provisional flag is cleared.
169         @param className - name of class being inserted.
170         @param superClassName - name of super class of class.
171     */
172     void insert(const String& className, const String& superClassName);
173
174     /** Scan directory for file names of the form <ClassName>.<SuperClass> and
175         call insert on insert for each one. Note that root classes (classes with
176         no superclass) will use "#" for a SuperClass name.
177         @param path - directory that contains files describing inheritance 
178             infoformation.
179         @exception throws CannotOpenDirectory is invalid path specifies an
180             invalid directory.
181     */
182     void insertFromPath(const String& path);
183
184     /** Checks that every superClassName passed to insert() was also passed
185         as a className argument to insert(). In other words, it checks that
186         there are no provisional entries as described in the insert() method.
187         @exception InvalidInheritanceTree
188     */
189     void check() const;
190
191     /** Get subclass names of the given class.
192         @param className - class whose subclass names will be gotten. If
193             className is empty, all classnames are returned.
194         @param deepInheritance - if true all descendent classes of class
195             are returned. If className is empty, only root classes are returned.
196         @param subClassNames - output argument to hold subclass names.
197         @return true on success. False if no such class.
198     */
199     Boolean getSubClassNames(
200         const CIMName& className,
201         Boolean deepInheritance,
202         Array<CIMName>& subClassNames) const;
203
204     /** Returns true if class1 is a subclass of class2.
205     */
206     Boolean isSubClass(const CIMName& class1, const CIMName& class2) const;
207
208     /** Get the names of all superclasses of this class (direct and indirect).
209     */
210     Boolean getSuperClassNames(
211         const CIMName& className,
212         Array<CIMName>& subClassNames) const;
213
214     /** Get the superclass of the given class.
215         @param className name of class.
216         @param superClassName name of superclass upon return.
217         @return true if class was found; false otherwise.
218     */
219     Boolean getSuperClass(
220         const CIMName& className,
221         CIMName& superClassName) const;
222
223     /** Returns true if the given class has sub-classes. */
224     Boolean hasSubClasses(
225         const CIMName& className,
226         Boolean& hasSubClasses) const;
227
228     /** Returns true if this inhertance tree contains the given class. */
229     Boolean containsClass(const CIMName& className) const;
230
231     /** Removes the given class from the class graph. 
232         @exception CIMException(CIM_ERR_CLASS_HAS_CHILDREN)
233         @exception CIMException(CIM_ERR_INVALID_CLASS)
234     */
235     void remove(const CIMName& className);
236
237     /** Prints the class */
238     void print(PEGASUS_STD(ostream)& os) const;
239
240 private:
241
242     InheritanceTree(const InheritanceTree&) { }
243
244     InheritanceTree& operator=(const InheritanceTree&) { return *this; }
245
246     InheritanceTreeRep* _rep;
247 };
248
249 /** The InvalidInheritanceTree exception is thrown when the
250     InheritanceTreeRep::check() method determines that an inheritance tree
251     was not fully specified (when any class was passed as a superClassName
252     argument to insert() but never as a className argument.
253 */
254 class InvalidInheritanceTree : public Exception
255 {
256 public:
257         //l10n start
258     //InvalidInheritanceTree(const String& className) 
259         //: Exception("Invalid inheritance tree: unknown class: " + className) { }
260         InvalidInheritanceTree(const String& className) 
261         : Exception(MessageLoaderParms("Repository.InheritanceTree.INVALID_INHERITANCE_TREE",
262                                                                    "Invalid inheritance tree: unknown class: $0", className)) { }
263         //l10n end
264 };
265
266 PEGASUS_NAMESPACE_END
267
268 #endif /* Pegasus_InheritanceTree_h */