1 // Copyright (C) 2009 - 2012  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17 
18 #ifndef HLAOMTXmlVisitor_hxx
19 #define HLAOMTXmlVisitor_hxx
20 
21 #include <map>
22 #include <string>
23 
24 #include <simgear/structure/exception.hxx>
25 #include <simgear/structure/SGSharedPtr.hxx>
26 #include <simgear/xml/easyxml.hxx>
27 #include "HLADataType.hxx"
28 #include "HLATypes.hxx"
29 
30 namespace simgear {
31 
32 class HLAFederate;
33 
34 class HLAOMTXmlVisitor : public XMLVisitor {
35 public:
36     /// structures representing the federate object model data
37     struct Attribute : public SGReferenced {
Attributesimgear::HLAOMTXmlVisitor::Attribute38         Attribute(const std::string& name) :
39             _name(name)
40         { }
getNamesimgear::HLAOMTXmlVisitor::Attribute41         const std::string& getName() const
42         { return _name; }
getDataTypesimgear::HLAOMTXmlVisitor::Attribute43         const std::string& getDataType() const
44         { return _dataType; }
getSharingsimgear::HLAOMTXmlVisitor::Attribute45         const std::string& getSharing() const
46         { return _sharing; }
getDimensionssimgear::HLAOMTXmlVisitor::Attribute47         const std::string& getDimensions() const
48         { return _dimensions; }
getTransportationsimgear::HLAOMTXmlVisitor::Attribute49         const std::string& getTransportation() const
50         { return _transportation; }
getOrdersimgear::HLAOMTXmlVisitor::Attribute51         const std::string& getOrder() const
52         { return _order; }
53 
getSubscriptionTypesimgear::HLAOMTXmlVisitor::Attribute54         HLASubscriptionType getSubscriptionType() const
55         {
56             if (_sharing.find("Subscribe") != std::string::npos)
57                 return HLASubscribedActive;
58             else
59                 return HLAUnsubscribed;
60         }
61 
getPublicationTypesimgear::HLAOMTXmlVisitor::Attribute62         HLAPublicationType getPublicationType() const
63         {
64             if (_sharing.find("Publish") != std::string::npos)
65                 return HLAPublished;
66             else
67                 return HLAUnpublished;
68         }
69 
getUpdateTypesimgear::HLAOMTXmlVisitor::Attribute70         HLAUpdateType getUpdateType() const
71         {
72             if (_updateType == "Periodic")
73                 return HLAPeriodicUpdate;
74             else if (_updateType == "Static")
75                 return HLAStaticUpdate;
76             else if (_updateType == "Conditional")
77                 return HLAConditionalUpdate;
78             else
79                 return HLAUndefinedUpdate;
80         }
81 
82         std::string _name;
83         std::string _dataType;
84         std::string _updateType;
85         std::string _updateCondition;
86         std::string _ownership;
87         std::string _sharing;
88         std::string _dimensions;
89         std::string _transportation;
90         std::string _order;
91         friend class HLAOMTXmlVisitor;
92     };
93     typedef std::vector<SGSharedPtr<Attribute> > AttributeList;
94 
95     struct ObjectClass : public SGReferenced {
96         ObjectClass(const std::string& name, const std::string& sharing);
97         ~ObjectClass();
98 
99         const std::string& getName() const;
100         const std::string& getSharing() const;
101 
102         unsigned getNumAttributes() const;
103         const Attribute* getAttribute(unsigned index) const;
104 
105         const ObjectClass* getParentObjectClass() const;
106 
107     private:
108         friend class HLAOMTXmlVisitor;
109         std::string _name;
110         std::string _sharing;
111         AttributeList _attributes;
112         SGSharedPtr<ObjectClass> _parentObjectClass;
113     };
114     typedef std::vector<SGSharedPtr<ObjectClass> > ObjectClassList;
115 
116     struct Parameter : public SGReferenced {
Parametersimgear::HLAOMTXmlVisitor::Parameter117         Parameter(const std::string& name) :
118             _name(name)
119         { }
getNamesimgear::HLAOMTXmlVisitor::Parameter120         const std::string& getName() const
121         { return _name; }
getDataTypesimgear::HLAOMTXmlVisitor::Parameter122         const std::string& getDataType() const
123         { return _dataType; }
124 
125     private:
126         std::string _name;
127         std::string _dataType;
128         friend class HLAOMTXmlVisitor;
129     };
130     typedef std::vector<SGSharedPtr<Parameter> > ParameterList;
131 
132     struct InteractionClass : public SGReferenced {
133         InteractionClass(const std::string& name);
134         ~InteractionClass();
135 
136         const std::string& getName() const;
137         const std::string& getDimensions() const;
138         const std::string& getSharing() const;
139         const std::string& getTransportation() const;
140         const std::string& getOrder() const;
141 
getSubscriptionTypesimgear::HLAOMTXmlVisitor::InteractionClass142         HLASubscriptionType getSubscriptionType() const
143         {
144             if (_sharing.find("Subscribe") != std::string::npos)
145                 return HLASubscribedActive;
146             else
147                 return HLAUnsubscribed;
148         }
149 
getPublicationTypesimgear::HLAOMTXmlVisitor::InteractionClass150         HLAPublicationType getPublicationType() const
151         {
152             if (_sharing.find("Publish") != std::string::npos)
153                 return HLAPublished;
154             else
155                 return HLAUnpublished;
156         }
157 
158         unsigned getNumParameters() const;
159         const Parameter* getParameter(unsigned index) const;
160 
161         const InteractionClass* getParentInteractionClass() const;
162 
163     private:
164         friend class HLAOMTXmlVisitor;
165         std::string _name;
166         std::string _dimensions;
167         std::string _sharing;
168         std::string _transportation;
169         std::string _order;
170         ParameterList _parameters;
171         SGSharedPtr<InteractionClass> _parentInteractionClass;
172     };
173     typedef std::vector<SGSharedPtr<InteractionClass> > InteractionClassList;
174 
175     HLAOMTXmlVisitor();
176     ~HLAOMTXmlVisitor();
177 
178     void setDataTypesToFederate(HLAFederate& federate);
179     void setToFederate(HLAFederate& federate);
180 
181     unsigned getNumObjectClasses() const;
182     const ObjectClass* getObjectClass(unsigned i) const;
183 
184     unsigned getNumInteractionClasses() const;
185     const InteractionClass* getInteractionClass(unsigned i) const;
186 
187 private:
188     SGSharedPtr<HLADataType> getDataType(const std::string& dataTypeName);
189     SGSharedPtr<HLABasicDataType> getBasicDataType(const std::string& dataTypeName);
190     SGSharedPtr<HLADataType> getSimpleDataType(const std::string& dataTypeName);
191     SGSharedPtr<HLAEnumeratedDataType> getEnumeratedDataType(const std::string& dataTypeName);
192     SGSharedPtr<HLADataType> getArrayDataType(const std::string& dataTypeName);
193     SGSharedPtr<HLAFixedRecordDataType> getFixedRecordDataType(const std::string& dataTypeName);
194     SGSharedPtr<HLAVariantRecordDataType> getVariantRecordDataType(const std::string& dataTypeName);
195 
196     enum Mode {
197         UnknownMode,
198 
199         ObjectModelMode,
200 
201         ObjectsMode,
202         ObjectClassMode,
203         AttributeMode,
204 
205         InteractionsMode,
206         InteractionClassMode,
207         ParameterMode,
208 
209         DataTypesMode,
210         BasicDataRepresentationsMode,
211         BasicDataMode,
212         SimpleDataTypesMode,
213         SimpleDataMode,
214         EnumeratedDataTypesMode,
215         EnumeratedDataMode,
216         EnumeratorMode,
217         ArrayDataTypesMode,
218         ArrayDataMode,
219         FixedRecordDataTypesMode,
220         FixedRecordDataMode,
221         FieldMode,
222         VariantRecordDataTypesMode,
223         VariantRecordDataMode,
224         AlternativeDataMode
225     };
226 
227     Mode getCurrentMode();
228     void pushMode(Mode mode);
229     void popMode();
230 
231     virtual void startXML();
232     virtual void endXML ();
233     virtual void startElement(const char* name, const XMLAttributes& atts);
234     virtual void endElement(const char* name);
235 
236     static std::string getAttribute(const char* name, const XMLAttributes& atts);
237     static std::string getAttribute(const std::string& name, const XMLAttributes& atts);
238 
239     struct BasicData {
240         // std::string _name;
241         std::string _size;
242         std::string _endian;
243     };
244     typedef std::map<std::string, BasicData> BasicDataMap;
245 
246     struct SimpleData {
247         // std::string _name;
248         std::string _representation;
249         std::string _units;
250         std::string _resolution;
251         std::string _accuracy;
252     };
253     typedef std::map<std::string, SimpleData> SimpleDataMap;
254 
255     struct Enumerator {
256         std::string _name;
257         std::string _values;
258     };
259     typedef std::vector<Enumerator> EnumeratorList;
260 
261     struct EnumeratedData {
262         // std::string _name;
263         std::string _representation;
264         EnumeratorList _enumeratorList;
265     };
266     typedef std::map<std::string, EnumeratedData> EnumeratedDataMap;
267 
268     struct ArrayData {
269         // std::string _name;
270         std::string _dataType;
271         std::string _cardinality;
272         std::string _encoding;
273     };
274     typedef std::map<std::string, ArrayData> ArrayDataMap;
275 
276     struct Field {
277         std::string _name;
278         std::string _dataType;
279     };
280     typedef std::vector<Field> FieldList;
281 
282     struct FixedRecordData {
283         // std::string _name;
284         std::string _encoding;
285         FieldList _fieldList;
286     };
287     typedef std::map<std::string, FixedRecordData> FixedRecordDataMap;
288 
289     struct Alternative {
290         std::string _name;
291         std::string _dataType;
292         std::string _semantics;
293         std::string _enumerator;
294     };
295     typedef std::vector<Alternative> AlternativeList;
296 
297     struct VariantRecordData {
298         // std::string _name;
299         std::string _encoding;
300         std::string _dataType;
301         std::string _discriminant;
302         std::string _semantics;
303         AlternativeList _alternativeList;
304     };
305     typedef std::map<std::string, VariantRecordData> VariantRecordDataMap;
306 
307     std::vector<Mode> _modeStack;
308 
309     /// The total list of object classes
310     ObjectClassList _objectClassList;
311     ObjectClassList _objectClassStack;
312 
313     /// The total list of interaction classes
314     InteractionClassList _interactionClassList;
315     InteractionClassList _interactionClassStack;
316 
317     typedef std::map<std::string, SGSharedPtr<HLADataType> > StringDataTypeMap;
318     StringDataTypeMap _dataTypeMap;
319 
320     /// DataType definitions
321     BasicDataMap _basicDataMap;
322     SimpleDataMap _simpleDataMap;
323     std::string _enumeratedDataName;
324     EnumeratedDataMap _enumeratedDataMap;
325     ArrayDataMap _arrayDataMap;
326     std::string _fixedRecordDataName;
327     FixedRecordDataMap _fixedRecordDataMap;
328     std::string _variantRecordDataName;
329     VariantRecordDataMap _variantRecordDataMap;
330 };
331 
332 } // namespace simgear
333 
334 #endif
335