10 martin 1.4 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
|
22 martin 1.4 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
29 mike 1.2 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #include "Closure.h"
33 #include <Pegasus/Common/Pair.h>
34
35 PEGASUS_NAMESPACE_BEGIN
36
37 // ATTN-MEB: handle ModelCorrespondence() qualifier.
38
39 static Uint32 _findClass(const Array<CIMClass>& classes, const CIMName& cn)
40 {
41 for (Uint32 i = 0; i < classes.size(); i++)
42 {
43 if (classes[i].getClassName() == cn)
44 return i;
45 }
46
47 // Not found!
48 return PEG_NOT_FOUND;
49 }
50 mike 1.2
51 static Uint32 _findClass(const Array<CIMName>& classNames, const CIMName& cn)
52 {
53 for (Uint32 i = 0; i < classNames.size(); i++)
54 {
55 if (classNames[i] == cn)
56 return i;
57 }
58
59 // Not found!
60 return PEG_NOT_FOUND;
61 }
62
63 static bool _isAssociation(const CIMClass& cc)
64 {
65 Uint32 pos = cc.findQualifier("Association");
66
67 if (pos == PEG_NOT_FOUND)
68 return false;
69
70 CIMConstQualifier cq = cc.getQualifier(pos);
71 mike 1.2
72 if (cq.getType() != CIMTYPE_BOOLEAN || cq.isArray())
73 return false;
74
75 return true;
76 }
77
78 template<class CONTAINER>
79 static int _getEmbeddedClassName(const CONTAINER& c, String& ecn)
80 {
81 ecn.clear();
|
83 mike 1.2
84 if (pos == PEG_NOT_FOUND)
85 return 0;
86
87 CIMConstQualifier cq = c.getQualifier(pos);
88
89 if (cq.getType() != CIMTYPE_STRING || cq.isArray())
90 return -1;
91
92 cq.getValue().get(ecn);
93 return 0;
94 }
95
96 static bool _isA(
97 const Array<CIMClass>& classes,
98 const CIMName& superClassName,
99 const CIMName& className)
100 {
101 // If same class, return true now:
102
103 if (superClassName == className)
104 mike 1.2 return true;
105
106 // Find the class:
107
108 Uint32 pos = _findClass(classes, className);
109
110 if (pos == PEG_NOT_FOUND)
111 return false;
112
113 const CIMClass& cc = classes[pos];
114
115 // Get superclass:
116
117 const CIMName& scn = cc.getSuperClassName();
118
119 if (scn.isNull())
120 return false;
121
122 return _isA(classes, superClassName, scn);
123 }
124
125 mike 1.2 int Closure(
126 const CIMName& className,
127 const Array<CIMClass>& classes,
128 Array<CIMName>& closure)
129 {
130 // Avoid if class already in closure:
131
132 if (_findClass(closure, className) != PEG_NOT_FOUND)
133 return 0;
134
135 // Add class to closure:
136
137 closure.append(className);
138
139 // Find the class:
140
141 Uint32 pos = _findClass(classes, className);
142
143 if (pos == PEG_NOT_FOUND)
144 return -1;
145
146 mike 1.2 const CIMClass& cc = classes[pos];
147
148 // Add superclass to closure:
149
150 const CIMName& scn = cc.getSuperClassName();
151
152 if (!scn.isNull())
153 {
154 if (Closure(scn, classes, closure) != 0)
155 return -1;
156 }
157
158 // References and EmbeddedInstances.
159
160 for (Uint32 i = 0; i < cc.getPropertyCount(); i++)
161 {
162 const CIMConstProperty& cp = cc.getProperty(i);
163
164 if (cp.getType() == CIMTYPE_REFERENCE)
165 {
166 const CIMName& rcn = cp.getReferenceClassName();
167 mike 1.2
168 if (Closure(rcn, classes, closure) != 0)
169 return -1;
170 }
171 else if (cp.getType() == CIMTYPE_STRING)
172 {
173 String ecn;
174
175 if (_getEmbeddedClassName(cp, ecn) != 0)
176 return -1;
177
178 if (ecn.size() && Closure(ecn, classes, closure) != 0)
179 return -1;
180 }
181 }
182
183 // Methods and EmbeddedInstances:
184
185 for (Uint32 i = 0; i < cc.getMethodCount(); i++)
186 {
187 const CIMConstMethod& cm = cc.getMethod(i);
188 mike 1.2
189 if (cm.getType() == CIMTYPE_STRING)
190 {
191 String ecn;
192
193 if (_getEmbeddedClassName(cm, ecn) != 0)
194 return -1;
195
196 if (ecn.size() && Closure(ecn, classes, closure) != 0)
197 return -1;
198 }
199
200 // Parameters and EmbeddedInstances:
201
202 for (Uint32 j = 0; j < cm.getParameterCount(); j++)
203 {
204 const CIMConstParameter& cp = cm.getParameter(j);
205
206 if (cp.getType() == CIMTYPE_REFERENCE)
207 {
208 const CIMName& rcn = cp.getReferenceClassName();
209 mike 1.2
210 if (Closure(rcn, classes, closure) != 0)
211 return -1;
212 }
213 else if (cp.getType() == CIMTYPE_STRING)
214 {
215 String ecn;
216
217 if (_getEmbeddedClassName(cp, ecn) != 0)
218 return -1;
219
220 if (ecn.size() && Closure(ecn, classes, closure) != 0)
221 return -1;
222 }
223 }
224 }
225
226 // Experimental only!
227 #if 0
228
229 // Include closure of all assosicate classes that refer to the source
230 mike 1.2 // class.
231
232 for (Uint32 i = 0; i < classes.size(); i++)
233 {
234 const CIMClass& tcc = classes[i];
235
236 if (!_isAssociation(tcc))
237 continue;
238
239 const CIMName& tcn = tcc.getClassName();
240
241 if (_findClass(closure, tcn) != PEG_NOT_FOUND)
242 continue;
243
244 for (Uint32 j = 0; j < tcc.getPropertyCount(); j++)
245 {
246 const CIMConstProperty& cp = tcc.getProperty(j);
247
248 if (cp.getType() == CIMTYPE_REFERENCE)
249 {
250 const CIMName& rcn = cp.getReferenceClassName();
251 mike 1.2
252 if (_isA(classes, rcn, cc.getClassName()))
253 {
254 if (Closure(tcc.getClassName(), classes, closure) != 0)
255 return -1;
256 }
257 }
258 }
259 }
260
261 #endif
262
263 return 0;
264 }
265
266 PEGASUS_NAMESPACE_END
|