summaryrefslogtreecommitdiffstats
path: root/parameter/DomainConfiguration.cpp
blob: fae22f21bc47f765e13cb4c3ffacb4754b9c3557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
 * Copyright (c) 2011-2014, Intel Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "DomainConfiguration.h"
#include "AreaConfiguration.h"
#include "ConfigurableElement.h"
#include "CompoundRule.h"
#include "Subsystem.h"
#include "XmlDomainSerializingContext.h"
#include "ConfigurationAccessContext.h"
#include <assert.h>
#include "RuleParser.h"

#define base CBinarySerializableElement

CDomainConfiguration::CDomainConfiguration(const string& strName) : base(strName)
{
}

CDomainConfiguration::~CDomainConfiguration()
{
    AreaConfigurationListIterator it;

    for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) {

        delete *it;
    }
}

// Class kind
string CDomainConfiguration::getKind() const
{
    return "Configuration";
}

// Child dynamic creation
bool CDomainConfiguration::childrenAreDynamic() const
{
    return true;
}

// XML configuration settings parsing
bool CDomainConfiguration::parseSettings(CXmlElement& xmlConfigurationSettingsElement, CXmlSerializingContext& serializingContext)
{
    // Actual XML context
    CXmlDomainSerializingContext& xmlDomainSerializingContext = static_cast<CXmlDomainSerializingContext&>(serializingContext);

    // Take care of configurable elements / area configurations ranks
    list<CAreaConfiguration*> areaConfigurationList;

    // Parse configurable element's configuration settings
    CXmlElement::CChildIterator it(xmlConfigurationSettingsElement);

    CXmlElement xmlConfigurableElementSettingsElement;

    while (it.next(xmlConfigurableElementSettingsElement)) {

        // Retrieve area configuration
        string strConfigurableElementPath = xmlConfigurableElementSettingsElement.getAttributeString("Path");

        CAreaConfiguration* pAreaConfiguration = findAreaConfiguration(strConfigurableElementPath);

        if (!pAreaConfiguration) {

            xmlDomainSerializingContext.setError("Configurable Element " + strConfigurableElementPath  + " referred to by Configuration " + getPath() + " not associated to Domain");

            return false;
        }
        // Ranks
        areaConfigurationList.push_back(pAreaConfiguration);

        // Parse
        if (!serializeConfigurableElementSettings(pAreaConfiguration, xmlConfigurableElementSettingsElement, xmlDomainSerializingContext, false)) {

            return false;
        }
    }

    // Reorder area configurations according to XML content
    reorderAreaConfigurations(areaConfigurationList);

    return true;
}

// XML configuration settings composing
void CDomainConfiguration::composeSettings(CXmlElement& xmlConfigurationSettingsElement, CXmlSerializingContext& serializingContext) const
{
    // Go through all are configurations
    AreaConfigurationListIterator it;

    for (it = _orderedAreaConfigurationList.begin(); it != _orderedAreaConfigurationList.end(); ++it) {

        const CAreaConfiguration* pAreaConfiguration = *it;

        // Retrieve configurable element
        const CConfigurableElement* pConfigurableElement = pAreaConfiguration->getConfigurableElement();

        // Create configurable element child element
        CXmlElement xmlConfigurableElementSettingsElement;

        xmlConfigurationSettingsElement.createChild(xmlConfigurableElementSettingsElement, "ConfigurableElement");

        // Set Path attribute
        xmlConfigurableElementSettingsElement.setAttributeString("Path", pConfigurableElement->getPath());

        // Delegate composing to area configuration
        ((CDomainConfiguration&)(*this)).serializeConfigurableElementSettings((CAreaConfiguration*)pAreaConfiguration, xmlConfigurableElementSettingsElement, serializingContext, true);
    }
}

// Serialize one configuration for one configurable element
bool CDomainConfiguration::serializeConfigurableElementSettings(CAreaConfiguration* pAreaConfiguration, CXmlElement& xmlConfigurableElementSettingsElement, CXmlSerializingContext& serializingContext, bool bSerializeOut)
{
    // Actual XML context
    CXmlDomainSerializingContext& xmlDomainSerializingContext = static_cast<CXmlDomainSerializingContext&>(serializingContext);

    // Configurable Element
    const CConfigurableElement* pConfigurableElement = pAreaConfiguration->getConfigurableElement();

    // Element content
    CXmlElement xmlConfigurableElementSettingsElementContent;

    // Deal with element itself
    if (!bSerializeOut) {

        // Check structure
        if (xmlConfigurableElementSettingsElement.getNbChildElements() != 1) {

            // Structure error
            serializingContext.setError("Struture error encountered while parsing settings of " + pConfigurableElement->getKind() + " " + pConfigurableElement->getName() + " in Configuration " + getPath());

            return false;
        }

        // Check name and kind
        if (!xmlConfigurableElementSettingsElement.getChildElement(pConfigurableElement->getKind(), pConfigurableElement->getName(), xmlConfigurableElementSettingsElementContent)) {

            serializingContext.setError("Couldn't find settings for " + pConfigurableElement->getKind() + " " + pConfigurableElement->getName() + " for Configuration " + getPath());

            return false;
        }
    } else {

        // Create child XML element
        xmlConfigurableElementSettingsElement.createChild(xmlConfigurableElementSettingsElementContent, pConfigurableElement->getKind());

        // Set Name
        xmlConfigurableElementSettingsElementContent.setNameAttribute(pConfigurableElement->getName());
    }

    // Change context type to parameter settings access
    string strError;

    // Create configuration access context
    CConfigurationAccessContext configurationAccessContext(strError, bSerializeOut);

    // Provide current value space
    configurationAccessContext.setValueSpaceRaw(xmlDomainSerializingContext.valueSpaceIsRaw());

    // Provide current output raw format
    configurationAccessContext.setOutputRawFormat(xmlDomainSerializingContext.outputRawFormatIsHex());

    // Get subsystem
    const CSubsystem* pSubsystem = pConfigurableElement->getBelongingSubsystem();

    if (pSubsystem && pSubsystem != pConfigurableElement) {

        // Element is a descendant of subsystem

        // Deal with Endianness
        configurationAccessContext.setBigEndianSubsystem(pSubsystem->isBigEndian());
    }

    // Have domain configuration parse settings for configurable element
    if (!pAreaConfiguration->serializeXmlSettings(xmlConfigurableElementSettingsElementContent, configurationAccessContext)) {

        // Forward error
        xmlDomainSerializingContext.setError(strError);

        return false;
    }
    return true;
}

// Configurable Elements association
void CDomainConfiguration::addConfigurableElement(const CConfigurableElement* pConfigurableElement, const CSyncerSet* pSyncerSet)
{
    CAreaConfiguration* pAreaConfiguration = pConfigurableElement->createAreaConfiguration(pSyncerSet);

    _areaConfigurationList.push_back(pAreaConfiguration);
    _orderedAreaConfigurationList.push_back(pAreaConfiguration);
}

void CDomainConfiguration::removeConfigurableElement(const CConfigurableElement* pConfigurableElement)
{
    CAreaConfiguration* pAreaConfigurationToRemove = getAreaConfiguration(pConfigurableElement);

    _areaConfigurationList.remove(pAreaConfigurationToRemove);
    _orderedAreaConfigurationList.remove(pAreaConfigurationToRemove);

    delete pAreaConfigurationToRemove;
}

// Sequence management
bool CDomainConfiguration::setElementSequence(const vector<string>& astrNewElementSequence, string& strError)
{
    // Build a new list of AreaConfiguration objects
    list<CAreaConfiguration*> areaConfigurationList;

    uint32_t uiConfigurableElement;

    for (uiConfigurableElement = 0; uiConfigurableElement < astrNewElementSequence.size(); uiConfigurableElement++) {

        string strConfigurableElementPath = astrNewElementSequence[uiConfigurableElement];

        CAreaConfiguration* pAreaConfiguration = findAreaConfiguration(strConfigurableElementPath);

        if (!pAreaConfiguration) {

            strError = "Element " + strConfigurableElementPath + " not found in domain";

            return false;
        }
        // Check not already present in the list
        if (findAreaConfiguration(strConfigurableElementPath, areaConfigurationList)) {

            strError = "Element " + strConfigurableElementPath + " provided more than once";

            return false;
        }

        // Store new ordered area configuration
        areaConfigurationList.push_back(pAreaConfiguration);
    }

    // Reorder area configurations according to given path list
    reorderAreaConfigurations(areaConfigurationList);

    return true;
}

void CDomainConfiguration::getElementSequence(string& strResult) const
{
    strResult = "\n";

    AreaConfigurationListIterator it;

    // List configurable element paths out of ordered area configuration list
    for (it = _orderedAreaConfigurationList.begin(); it != _orderedAreaConfigurationList.end(); ++it) {

        const CAreaConfiguration* pAreaConfiguration = *it;

        const CConfigurableElement* pConfigurableElement = pAreaConfiguration->getConfigurableElement();

        strResult += pConfigurableElement->getPath() + "\n";
    }
}

// Application rule
bool CDomainConfiguration::setApplicationRule(const string& strApplicationRule, const CSelectionCriteriaDefinition* pSelectionCriteriaDefinition, string& strError)
{
    // Parser
    CRuleParser ruleParser(strApplicationRule, pSelectionCriteriaDefinition);

    // Attempt to parse it
    if (!ruleParser.parse(NULL, strError)) {

        return false;
    }
    // Replace compound rule
    setRule(ruleParser.grabRootRule());

    return true;
}

void CDomainConfiguration::clearApplicationRule()
{
    // Replace compound rule
    setRule(NULL);
}

void CDomainConfiguration::getApplicationRule(string& strResult) const
{
    // Rule
    const CCompoundRule* pRule = getRule();

    if (pRule) {
        // Start clear
        strResult.clear();

        // Dump rule
        pRule->dump(strResult);

    } else {

        strResult = "<none>";
    }
}

/**
 * Get the Configuration Blackboard.
 *
 * Fetch the Configuration Blackboard related to the ConfigurableElement given in parameter. This
 * Element is used to retrieve the correct AreaConfiguration where the Blackboard is stored.
 *
 * @param[in] pConfigurableElement      A pointer to a ConfigurableElement that is part of the
 *                                      Domain. This must have been checked previously, as an
 *                                      assertion is performed.
 *
 * return Pointer to the Blackboard of the Configuration.
 */
CParameterBlackboard* CDomainConfiguration::getBlackboard(const CConfigurableElement* pConfigurableElement) const
{
    AreaConfigurationListIterator it;

    for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) {

        CAreaConfiguration* pAreaConfiguration = *it;

        // Check if the Element is associated with the Domain
        if (pAreaConfiguration->getConfigurableElement() == pConfigurableElement) {

            return &pAreaConfiguration->getBlackboard();
        }
    }

    assert(0);
    return NULL;
}

// Save data from current
void CDomainConfiguration::save(const CParameterBlackboard* pMainBlackboard)
{
    AreaConfigurationListIterator it;

    // Just propagate to areas
    for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) {

        CAreaConfiguration* pAreaConfiguration = *it;

        pAreaConfiguration->save(pMainBlackboard);
    }
}

// Apply data to current
bool CDomainConfiguration::restore(CParameterBlackboard* pMainBlackboard, bool bSync, list<string>* plstrError) const
{
    bool bSuccess = true;

    AreaConfigurationListIterator it;

    // Just propagate to areas
    for (it = _orderedAreaConfigurationList.begin(); it != _orderedAreaConfigurationList.end(); ++it) {

        const CAreaConfiguration* pAreaConfiguration = *it;

        bSuccess &= pAreaConfiguration->restore(pMainBlackboard, bSync, plstrError);
    }

    return bSuccess;
}

// Ensure validity for configurable element area configuration
void CDomainConfiguration::validate(const CConfigurableElement* pConfigurableElement, const CParameterBlackboard* pMainBlackboard)
{
    CAreaConfiguration* pAreaConfigurationToValidate = getAreaConfiguration(pConfigurableElement);

    // Delegate
    pAreaConfigurationToValidate->validate(pMainBlackboard);
}

// Ensure validity of all area configurations
void CDomainConfiguration::validate(const CParameterBlackboard* pMainBlackboard)
{
    AreaConfigurationListIterator it;

    for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) {

        CAreaConfiguration* pAreaConfiguration = *it;

        pAreaConfiguration->validate(pMainBlackboard);
    }
}

// Return configuration validity for given configurable element
bool CDomainConfiguration::isValid(const CConfigurableElement* pConfigurableElement) const
{
    // Get child configurable elemnt's area configuration
    CAreaConfiguration* pAreaConfiguration = getAreaConfiguration(pConfigurableElement);

    assert(pAreaConfiguration);

    return pAreaConfiguration->isValid();
}

// Ensure validity of configurable element's area configuration by copying in from a valid one
void CDomainConfiguration::validateAgainst(const CDomainConfiguration* pValidDomainConfiguration, const CConfigurableElement* pConfigurableElement)
{
    // Retrieve related area configurations
    CAreaConfiguration* pAreaConfigurationToValidate = getAreaConfiguration(pConfigurableElement);
    const CAreaConfiguration* pAreaConfigurationToValidateAgainst = pValidDomainConfiguration->getAreaConfiguration(pConfigurableElement);

    // Delegate to area
    pAreaConfigurationToValidate->validateAgainst(pAreaConfigurationToValidateAgainst);
}

// Ensure validity of all configurable element's area configuration by copying in from a valid ones
void CDomainConfiguration::validateAgainst(const CDomainConfiguration* pValidDomainConfiguration)
{
    // Copy in configuration data from against domain
    AreaConfigurationListIterator it, itAgainst;

    for (it = _areaConfigurationList.begin(), itAgainst = pValidDomainConfiguration->_areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it, ++itAgainst) {

        CAreaConfiguration* pAreaConfigurationToValidate = *it;
        const CAreaConfiguration* pAreaConfigurationToValidateAgainst = *itAgainst;

        // Delegate to area
        pAreaConfigurationToValidate->validateAgainst(pAreaConfigurationToValidateAgainst);
    }
}

// Dynamic data application
bool CDomainConfiguration::isApplicable() const
{
    const CCompoundRule* pRule = getRule();

    return pRule && pRule->matches();
}

// Merge existing configurations to given configurable element ones
void CDomainConfiguration::merge(CConfigurableElement* pToConfigurableElement, CConfigurableElement* pFromConfigurableElement)
{
    // Retrieve related area configurations
    CAreaConfiguration* pAreaConfigurationToMergeTo = getAreaConfiguration(pToConfigurableElement);
    const CAreaConfiguration* pAreaConfigurationToMergeFrom = getAreaConfiguration(pFromConfigurableElement);

    // Do the merge
    pAreaConfigurationToMergeFrom->copyToOuter(pAreaConfigurationToMergeTo);
}

// Domain splitting
void CDomainConfiguration::split(CConfigurableElement* pFromConfigurableElement)
{
    // Retrieve related area configuration
    const CAreaConfiguration* pAreaConfigurationToSplitFrom = getAreaConfiguration(pFromConfigurableElement);

    // Go through children areas to copy configuration data to them
    uint32_t uiNbConfigurableElementChildren = pFromConfigurableElement->getNbChildren();
    uint32_t uiChild;

    for (uiChild = 0; uiChild < uiNbConfigurableElementChildren; uiChild++) {

        CConfigurableElement* pToChildConfigurableElement = static_cast<CConfigurableElement*>(pFromConfigurableElement->getChild(uiChild));

        // Get child configurable elemnt's area configuration
        CAreaConfiguration* pChildAreaConfiguration = getAreaConfiguration(pToChildConfigurableElement);

        // Do the copy
        pChildAreaConfiguration->copyFromOuter(pAreaConfigurationToSplitFrom);
    }
}

// AreaConfiguration retrieval from configurable element
CAreaConfiguration* CDomainConfiguration::getAreaConfiguration(const CConfigurableElement* pConfigurableElement) const
{
    AreaConfigurationListIterator it;

    for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) {

        CAreaConfiguration* pAreaConfiguration = *it;

        if (pAreaConfiguration->getConfigurableElement() == pConfigurableElement) {

            return pAreaConfiguration;
        }
    }
    // Not found?
    assert(0);

    return NULL;
}

// AreaConfiguration retrieval from present area configurations
CAreaConfiguration* CDomainConfiguration::findAreaConfiguration(const string& strConfigurableElementPath) const
{
    return findAreaConfiguration(strConfigurableElementPath, _areaConfigurationList);
}

// AreaConfiguration retrieval from given area configuration list
CAreaConfiguration* CDomainConfiguration::findAreaConfiguration(const string& strConfigurableElementPath, const list<CAreaConfiguration*>& areaConfigurationList) const
{
    AreaConfigurationListIterator it;

    for (it = areaConfigurationList.begin(); it != areaConfigurationList.end(); ++it) {

        CAreaConfiguration* pAreaConfiguration = *it;

        if (pAreaConfiguration->getConfigurableElement()->getPath() == strConfigurableElementPath) {

            return pAreaConfiguration;
        }
    }

    // Not found
    return NULL;
}

// Area configuration ordering
void CDomainConfiguration::reorderAreaConfigurations(const list<CAreaConfiguration*>& areaConfigurationList)
{
    // Ensure elements in provided list appear first and ordered the same way in internal one

    // Remove all elements present in the provided list from the internal one
    AreaConfigurationListIterator it;

    for (it = areaConfigurationList.begin(); it != areaConfigurationList.end(); ++it) {

        _orderedAreaConfigurationList.remove(*it);
    }

    // Prepended provided elements into internal list
    _orderedAreaConfigurationList.insert(_orderedAreaConfigurationList.begin(), areaConfigurationList.begin(), areaConfigurationList.end());
}

// Find area configuration rank from regular list: for ordered list maintainance
uint32_t CDomainConfiguration::getAreaConfigurationRank(const CAreaConfiguration* pAreaConfiguration) const
{
    uint32_t uiAreaConfigurationRank;
    AreaConfigurationListIterator it;

    // Propagate request to areas
    for (it = _areaConfigurationList.begin(), uiAreaConfigurationRank = 0; it != _areaConfigurationList.end(); ++it, ++uiAreaConfigurationRank) {

        if (*it == pAreaConfiguration) {

            return uiAreaConfigurationRank;
        }
    }

    assert(0);

    return 0;
}

// Find area configuration from regular list based on rank: for ordered list maintainance
CAreaConfiguration* CDomainConfiguration::getAreaConfiguration(uint32_t uiAreaConfigurationRank) const
{
    AreaConfigurationListIterator it;
    uint32_t uiCurrentAreaConfigurationRank;

    // Propagate request to areas
    for (it = _areaConfigurationList.begin(), uiCurrentAreaConfigurationRank = 0; it != _areaConfigurationList.end(); ++it, ++uiCurrentAreaConfigurationRank) {

        if (uiCurrentAreaConfigurationRank == uiAreaConfigurationRank) {

            return *it;
        }
    }

    assert(0);

    return NULL;
}

// Rule
const CCompoundRule* CDomainConfiguration::getRule() const
{
    if (getNbChildren()) {
        // Rule created
        return static_cast<const CCompoundRule*>(getChild(ECompoundRule));
    }
    return NULL;
}

CCompoundRule* CDomainConfiguration::getRule()
{
    if (getNbChildren()) {
        // Rule created
        return static_cast<CCompoundRule*>(getChild(ECompoundRule));
    }
    return NULL;
}

void CDomainConfiguration::setRule(CCompoundRule* pRule)
{
    CCompoundRule* pOldRule = getRule();

    if (pOldRule) {
        // Remove previous rule
        removeChild(pOldRule);

        delete pOldRule;
    }

    // Set new one
    if (pRule) {
        // Chain
        addChild(pRule);
    }
}

// Serialization
void CDomainConfiguration::binarySerialize(CBinaryStream& binaryStream)
{
    AreaConfigurationListIterator it;

    // Area configurations order
    if (binaryStream.isOut()) {

        for (it = _orderedAreaConfigurationList.begin(); it != _orderedAreaConfigurationList.end(); ++it) {

            // Get rank
            uint32_t uiAreaConfigurationRank = getAreaConfigurationRank(*it);

            // Store it
            binaryStream.write((const uint8_t*)&uiAreaConfigurationRank, sizeof(uiAreaConfigurationRank));
        }
    } else {

        // Empty ordered list first
        _orderedAreaConfigurationList.resize(0);

        uint32_t uiAreaConfiguration;

        for (uiAreaConfiguration = 0; uiAreaConfiguration < _areaConfigurationList.size(); uiAreaConfiguration++) {

            // Get rank
            uint32_t uiAreaConfigurationRank;

            binaryStream.read((uint8_t*)&uiAreaConfigurationRank, sizeof(uiAreaConfigurationRank));

            _orderedAreaConfigurationList.push_back(getAreaConfiguration(uiAreaConfigurationRank));
        }
    }

    // Propagate to areas
    for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) {

        CAreaConfiguration* pAreaConfiguration = *it;

        pAreaConfiguration->serialize(binaryStream);
    }
}

// Data size
uint32_t CDomainConfiguration::getDataSize() const
{
    uint32_t uiDataSize;

    // Add necessary size to store area configurations order
    uiDataSize = _areaConfigurationList.size() * sizeof(uint32_t);

    // Propagate request to areas
    AreaConfigurationListIterator it;

    for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) {

        const CAreaConfiguration* pAreaConfiguration = *it;

        uiDataSize += pAreaConfiguration->getSize();
    }
    return uiDataSize;
}