summaryrefslogtreecommitdiffstats
path: root/parameter/ConfigurableDomain.cpp
blob: 346b1f9baa665aee4d8236dab97208a04fd430f0 (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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
/*
 * 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 "ConfigurableDomain.h"
#include "DomainConfiguration.h"
#include "ConfigurableElement.h"
#include "ConfigurationAccessContext.h"
#include "XmlDomainSerializingContext.h"
#include "XmlDomainImportContext.h"
#include "XmlDomainExportContext.h"
#include <assert.h>

#define base CBinarySerializableElement

using std::string;

CConfigurableDomain::CConfigurableDomain() :
    _bSequenceAware(false), _pLastAppliedConfiguration(NULL)
{
}

CConfigurableDomain::CConfigurableDomain(const string& strName) : base(strName), _bSequenceAware(false), _pLastAppliedConfiguration(NULL)
{
}

CConfigurableDomain::~CConfigurableDomain()
{
    // Remove all configurable elements
    ConfigurableElementListIterator it;

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

        CConfigurableElement* pConfigurableElement = *it;

        // Remove from configurable element
        pConfigurableElement->removeAttachedConfigurableDomain(this);
    }

    // Remove all associated syncer sets
    ConfigurableElementToSyncerSetMapIterator mapIt;

    for (mapIt = _configurableElementToSyncerSetMap.begin(); mapIt != _configurableElementToSyncerSetMap.end(); ++mapIt) {

        delete mapIt->second;
    }
}

string CConfigurableDomain::getKind() const
{
    return "ConfigurableDomain";
}

bool CConfigurableDomain::childrenAreDynamic() const
{
    return true;
}

// Content dumping
void CConfigurableDomain::logValue(string& strValue, CErrorContext& errorContext) const
{
    (void)errorContext;

    strValue = "{";

    // Sequence awareness
    strValue += "Sequence aware: ";
    strValue += _bSequenceAware ? "yes" : "no";

    // Last applied configuration
    strValue += ", Last applied configuration: ";
    strValue += _pLastAppliedConfiguration ? _pLastAppliedConfiguration->getName() : "<none>";

    strValue += "}";
}

// Sequence awareness
void CConfigurableDomain::setSequenceAwareness(bool bSequenceAware)
{
    if (_bSequenceAware != bSequenceAware) {

        log_info("Making domain \"%s\" sequence %s", getName().c_str(), bSequenceAware ? "aware" : "unaware");

        _bSequenceAware = bSequenceAware;
    }
}

bool CConfigurableDomain::getSequenceAwareness() const
{
    return _bSequenceAware;
}

// From IXmlSource
void CConfigurableDomain::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
{
    base::toXml(xmlElement, serializingContext);

    // Sequence awareness
    xmlElement.setAttributeBoolean("SequenceAware", _bSequenceAware);
}

void CConfigurableDomain::childrenToXml(CXmlElement& xmlElement,
                                        CXmlSerializingContext& serializingContext) const
{
    // Configurations
    composeDomainConfigurations(xmlElement, serializingContext);

    // Configurable Elements
    composeConfigurableElements(xmlElement);

    // Settings
    composeSettings(xmlElement, serializingContext);
}

// XML composing
void CConfigurableDomain::composeDomainConfigurations(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
{
    // Create Configurations element
    CXmlElement xmlConfigurationsElement;

    xmlElement.createChild(xmlConfigurationsElement, "Configurations");

    // Delegate to base
    base::childrenToXml(xmlConfigurationsElement, serializingContext);
}

void CConfigurableDomain::composeConfigurableElements(CXmlElement& xmlElement) const
{
    // Create ConfigurableElements element
    CXmlElement xmlConfigurableElementsElement;

    xmlElement.createChild(xmlConfigurableElementsElement, "ConfigurableElements");

    // Serialize out all configurable elements settings
    ConfigurableElementListIterator it;

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

        const CConfigurableElement* pConfigurableElement = *it;

        // Create corresponding XML child element
        CXmlElement xmlChildConfigurableElement;

        xmlConfigurableElementsElement.createChild(xmlChildConfigurableElement, "ConfigurableElement");

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

void CConfigurableDomain::composeSettings(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
{
    // Context
    const CXmlDomainExportContext& xmlDomainExportContext =
        static_cast<const CXmlDomainExportContext&>(serializingContext);

    if (!xmlDomainExportContext.withSettings()) {

        return;
    }

    // Create Settings element
    CXmlElement xmlSettingsElement;

    xmlElement.createChild(xmlSettingsElement, "Settings");

    // Serialize out all configurations settings
    size_t uiNbConfigurations = getNbChildren();
    size_t uiChildConfiguration;

    for (uiChildConfiguration = 0; uiChildConfiguration < uiNbConfigurations; uiChildConfiguration++) {

        const CDomainConfiguration* pDomainConfiguration = static_cast<const CDomainConfiguration*>(getChild(uiChildConfiguration));

        // Create child xml element for that configuration
        CXmlElement xmlConfigurationSettingsElement;

        xmlSettingsElement.createChild(xmlConfigurationSettingsElement, pDomainConfiguration->getKind());

        // Set its name attribute
        xmlConfigurationSettingsElement.setNameAttribute(pDomainConfiguration->getName());

        // Serialize out configuration settings
        pDomainConfiguration->composeSettings(xmlConfigurationSettingsElement, serializingContext);
    }
}

// From IXmlSink
bool CConfigurableDomain::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
{
    // Context
    CXmlDomainImportContext& xmlDomainImportContext =
        static_cast<CXmlDomainImportContext&>(serializingContext);

    // Sequence awareness (optional)
    _bSequenceAware = xmlElement.hasAttribute("SequenceAware") && xmlElement.getAttributeBoolean("SequenceAware");

    setName(xmlElement.getAttributeString("Name"));

    // Local parsing. Do not dig
    if (!parseDomainConfigurations(xmlElement, xmlDomainImportContext) ||
        !parseConfigurableElements(xmlElement, xmlDomainImportContext) ||
        !parseSettings(xmlElement, xmlDomainImportContext)) {

        return false;
    }

    // All provided configurations are parsed
    // Attempt validation on areas of non provided configurations for all configurable elements if required
    if (xmlDomainImportContext.autoValidationRequired()) {

        autoValidateAll();
    }

    return true;
}

// XML parsing
bool CConfigurableDomain::parseDomainConfigurations(const CXmlElement& xmlElement,
                                                    CXmlDomainImportContext& serializingContext)
{
    // We're supposedly clean
    assert(_configurableElementList.empty());

    // Get Configurations element
    CXmlElement xmlConfigurationsElement;

    xmlElement.getChildElement("Configurations", xmlConfigurationsElement);

    // Parse it and create domain configuration objects
    return base::fromXml(xmlConfigurationsElement, serializingContext);
}

// Parse configurable elements
bool CConfigurableDomain::parseConfigurableElements(const CXmlElement& xmlElement,
                                                    CXmlDomainImportContext& serializingContext)
{
    CSystemClass& systemClass = serializingContext.getSystemClass();

    // Get ConfigurableElements element
    CXmlElement xmlConfigurableElementsElement;
    xmlElement.getChildElement("ConfigurableElements", xmlConfigurableElementsElement);

    // Parse it and associate found configurable elements to it
    CXmlElement::CChildIterator it(xmlConfigurableElementsElement);

    CXmlElement xmlConfigurableElementElement;

    while (it.next(xmlConfigurableElementElement)) {

        // Locate configurable element
        string strConfigurableElementPath = xmlConfigurableElementElement.getAttributeString("Path");

        CPathNavigator pathNavigator(strConfigurableElementPath);
        string strError;

        // Is there an element and does it match system class name?
        if (!pathNavigator.navigateThrough(systemClass.getName(), strError)) {

            serializingContext.setError("Could not find configurable element of path " + strConfigurableElementPath + " from ConfigurableDomain description " + getName() + " (" + strError + ")");

            return false;
        }
        // Browse system class for configurable element
        CConfigurableElement* pConfigurableElement =
            static_cast<CConfigurableElement*>(systemClass.findDescendant(pathNavigator));

        if (!pConfigurableElement) {

            serializingContext.setError("Could not find configurable element of path " + strConfigurableElementPath + " from ConfigurableDomain description " + getName());

            return false;
        }
        // Add found element to domain
        if (!addConfigurableElement(pConfigurableElement, NULL, strError)) {

            serializingContext.setError(strError);

            return false;
        }
    }

    return true;
}

// Parse settings
bool CConfigurableDomain::parseSettings(const CXmlElement& xmlElement,
                                        CXmlDomainImportContext& serializingContext)
{
    // Check we actually need to parse configuration settings
    if (!serializingContext.withSettings()) {

        // No parsing required
        return true;
    }

    // Get Settings element
    CXmlElement xmlSettingsElement;
    if (!xmlElement.getChildElement("Settings", xmlSettingsElement)) {

        // No settings, bail out successfully
        return true;
    }

    // Parse configuration settings
    CXmlElement::CChildIterator it(xmlSettingsElement);

    CXmlElement xmlConfigurationSettingsElement;

    while (it.next(xmlConfigurationSettingsElement)) {
        // Get domain configuration
        CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(findChild(xmlConfigurationSettingsElement.getNameAttribute()));

        if (!pDomainConfiguration) {

            serializingContext.setError("Could not find domain configuration referred to by"
                                        " configurable domain \"" + getName() + "\".");

            return false;
        }
        // Have domain configuration parse settings for all configurable elements
        if (!pDomainConfiguration->parseSettings(xmlConfigurationSettingsElement,
                                                 serializingContext)) {

            return false;
        }
    }

    return true;
}
// Configurable elements association
bool CConfigurableDomain::addConfigurableElement(CConfigurableElement* pConfigurableElement, const CParameterBlackboard* pMainBlackboard, string& strError)
{
    // Already associated?
    if (containsConfigurableElement(pConfigurableElement)) {

        strError = "Configurable element " + pConfigurableElement->getPath() + " already associated to configuration domain " + getName();

        return false;
    }

    // Already owned?
    if (pConfigurableElement->belongsTo(this)) {

        strError = "Configurable element " + pConfigurableElement->getPath() + " already owned by configuration domain " + getName();

        return false;
    }

    // Do add
    doAddConfigurableElement(pConfigurableElement, pMainBlackboard);

    return true;
}

bool CConfigurableDomain::removeConfigurableElement(CConfigurableElement* pConfigurableElement, string& strError)
{
    // Not associated?
    if (!containsConfigurableElement(pConfigurableElement)) {

        strError = "Configurable element " + pConfigurableElement->getPath() + " not associated to configuration domain " + getName();

        return false;
    }
    log_info("Removing configurable element \"%s\" from domain \"%s\"", pConfigurableElement->getPath().c_str(), getName().c_str());

    // Do remove
    doRemoveConfigurableElement(pConfigurableElement, true);

    return true;
}

/**
* Blackboard Configuration and Base Offset retrieval.
*
* This method fetches the Blackboard associated to the ConfigurableElement
* given in parameter, for a specific Configuration. The ConfigurableElement
* must belong to the Domain. If a Blackboard is found, the base offset of
* the ConfigurableElement is returned as well. This base offset corresponds to
* the offset of the ancestor of the ConfigurableElement associated to the Configuration.
*
* @param[in] strConfiguration                           Name of the Configuration.
* @param[in] pCandidateDescendantConfigurableElement    Pointer to a CConfigurableElement that
*                                                       belongs to the Domain.
* @param[out] uiBaseOffset                              The base offset of the CConfigurableElement.
* @param[out] bIsLastApplied                            Boolean indicating that the Configuration is
*                                                       the last one applied of the Domain.
* @param[out] strError                                  Error message
*
* return Pointer to the Blackboard of the Configuration.
*/
CParameterBlackboard* CConfigurableDomain::findConfigurationBlackboard(const string& strConfiguration,
                                                                       const CConfigurableElement* pCandidateDescendantConfigurableElement,
                                                                       uint32_t& uiBaseOffset,
                                                                       bool& bIsLastApplied,
                                                                       string& strError) const
{
    // Find Configuration
    const CDomainConfiguration* pDomainConfiguration = static_cast<const CDomainConfiguration*>(findChild(strConfiguration));

    if (!pDomainConfiguration) {

        strError = "Domain configuration " + strConfiguration + " not found";

        return NULL;
    }

    // Parse all configurable elements
    ConfigurableElementListIterator it;

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

        const CConfigurableElement* pAssociatedConfigurableElement = *it;

        // Check if the the associated element is the configurable element or one of its ancestors
        if ((pCandidateDescendantConfigurableElement == pAssociatedConfigurableElement) ||
            (pCandidateDescendantConfigurableElement->isDescendantOf(pAssociatedConfigurableElement))) {

            uiBaseOffset = pAssociatedConfigurableElement->getOffset();
            bIsLastApplied = (pDomainConfiguration == _pLastAppliedConfiguration);

            return pDomainConfiguration->getBlackboard(pAssociatedConfigurableElement);
        }
    }

    strError = "Element not associated to the Domain";

    return NULL;
}

// Domain splitting
bool CConfigurableDomain::split(CConfigurableElement* pConfigurableElement, string& strError)
{
    // Not associated?
    if (!containsConfigurableElement(pConfigurableElement)) {

        strError = "Configurable element " + pConfigurableElement->getPath() + " not associated to configuration domain " + getName();

        return false;
    }
    log_info("Splitting configurable element \"%s\" domain \"%s\"", pConfigurableElement->getPath().c_str(), getName().c_str());

    // Create sub domain areas for all configurable element's children
    size_t uiNbConfigurableElementChildren = pConfigurableElement->getNbChildren();

    if (!uiNbConfigurableElementChildren) {

        strError = "Configurable element " + pConfigurableElement->getPath() + " has no children to split configurable domain to";

        return false;
    }

    size_t uiChild;

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

        CConfigurableElement* pChildConfigurableElement = static_cast<CConfigurableElement*>(pConfigurableElement->getChild(uiChild));

        doAddConfigurableElement(pChildConfigurableElement);
    }

    // Delegate to configurations
    size_t uiNbConfigurations = getNbChildren();

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

        CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild));

        pDomainConfiguration->split(pConfigurableElement);
    }

    // Remove given configurable element from this domain
    // Note: we shouldn't need to recompute the sync set in that case, as the splitted element should include the syncers of its children elements
    doRemoveConfigurableElement(pConfigurableElement, false);

    return true;
}

// Check if there is a pending configuration for this domain: i.e. an applicable configuration different from the last applied configuration
const CDomainConfiguration* CConfigurableDomain::getPendingConfiguration() const
{
    const CDomainConfiguration* pApplicableDomainConfiguration = findApplicableDomainConfiguration();

    if (pApplicableDomainConfiguration) {

        // Check not the last one before applying
        if (!_pLastAppliedConfiguration || (_pLastAppliedConfiguration != pApplicableDomainConfiguration)) {

            return pApplicableDomainConfiguration;
        }
    }

    return NULL;
}

// Configuration application if required
void CConfigurableDomain::apply(CParameterBlackboard* pParameterBlackboard, CSyncerSet* pSyncerSet, bool bForce) const
{
    // Apply configuration only if the blackboard will
    // be synchronized either now or by syncerSet.
    if(!pSyncerSet ^ _bSequenceAware) {
        // The configuration can not be syncronised
        return;
    }

    if (bForce) {
        // Force a configuration restore by forgetting about last applied configuration
        _pLastAppliedConfiguration = NULL;
    }
    const CDomainConfiguration* pApplicableDomainConfiguration = findApplicableDomainConfiguration();

    if (pApplicableDomainConfiguration) {

        // Check not the last one before applying
        if (!_pLastAppliedConfiguration || _pLastAppliedConfiguration != pApplicableDomainConfiguration) {

            log_info("Applying configuration \"%s\" from domain \"%s\"",
                     pApplicableDomainConfiguration->getName().c_str(),
                     getName().c_str());

            // Check if we need to synchronize during restore
            bool bSync = !pSyncerSet && _bSequenceAware;

            // Do the restore
            pApplicableDomainConfiguration->restore(pParameterBlackboard, bSync, NULL);

            // Record last applied configuration
            _pLastAppliedConfiguration = pApplicableDomainConfiguration;

            // Check we need to provide syncer set to caller
            if (pSyncerSet && !_bSequenceAware) {

                // Since we applied changes, add our own sync set to the given one
                *pSyncerSet += _syncerSet;
            }
        }
    }
}

// Return applicable configuration validity for given configurable element
bool CConfigurableDomain::isApplicableConfigurationValid(const CConfigurableElement* pConfigurableElement) const
{
    const CDomainConfiguration* pApplicableDomainConfiguration = findApplicableDomainConfiguration();

    return pApplicableDomainConfiguration && pApplicableDomainConfiguration->isValid(pConfigurableElement);
}

// In case configurable element was removed
void CConfigurableDomain::computeSyncSet()
{
    // Clean sync set first
    _syncerSet.clear();

    // Add syncer sets for all associated configurable elements
    ConfigurableElementToSyncerSetMapIterator mapIt;

    for (mapIt = _configurableElementToSyncerSetMap.begin(); mapIt != _configurableElementToSyncerSetMap.end(); ++mapIt) {

        const CSyncerSet* pSyncerSet = mapIt->second;

        _syncerSet += *pSyncerSet;
    }
}

// Configuration Management
bool CConfigurableDomain::createConfiguration(const string& strName, const CParameterBlackboard* pMainBlackboard, string& strError)
{
    // Already exists?
    if (findChild(strName)) {

        strError = "Already existing configuration";

        return false;
    }
    log_info("Creating domain configuration \"%s\" into domain \"%s\"", strName.c_str(), getName().c_str());

    // Creation
    CDomainConfiguration* pDomainConfiguration = new CDomainConfiguration(strName);

    // Configurable elements association
    ConfigurableElementListIterator it;

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

        const CConfigurableElement* pConfigurableElement = *it;;

        // Retrieve associated syncer set
        CSyncerSet* pSyncerSet = getSyncerSet(pConfigurableElement);

        // Associate to configuration
        pDomainConfiguration->addConfigurableElement(pConfigurableElement, pSyncerSet);
    }

    // Hierarchy
    addChild(pDomainConfiguration);

    // Ensure validity of fresh new domain configuration
    // Attempt auto validation, so that the user gets his/her own settings by defaults
    if (!autoValidateConfiguration(pDomainConfiguration)) {

        // No valid configuration found to copy in from, validate againt main blackboard (will concerned remaining invalid parts)
        pDomainConfiguration->validate(pMainBlackboard);
    }

    return true;
}

bool CConfigurableDomain::deleteConfiguration(const string& strName, string& strError)
{
    CDomainConfiguration* pDomainConfiguration = findConfiguration(strName, strError);

    if (!pDomainConfiguration) {

        return false;
    }

    log_info("Deleting configuration \"%s\" from domain \"%s\"", strName.c_str(), getName().c_str());

    // Was the last applied?
    if (pDomainConfiguration == _pLastAppliedConfiguration) {

        // Forget about it
        _pLastAppliedConfiguration = NULL;
    }

    // Hierarchy
    removeChild(pDomainConfiguration);

    // Destroy
    delete pDomainConfiguration;

    return true;
}

void CConfigurableDomain::listAssociatedToElements(string& strResult) const
{
    strResult = "\n";

    ConfigurableElementListIterator it;

    // Browse all configurable elements
    for (it = _configurableElementList.begin(); it != _configurableElementList.end(); ++it) {

        const CConfigurableElement* pConfigurableElement = *it;

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

bool CConfigurableDomain::renameConfiguration(const string& strName, const string& strNewName, string& strError)
{
    CDomainConfiguration* pDomainConfiguration = findConfiguration(strName, strError);

    if (!pDomainConfiguration) {

        return false;
    }
    log_info("Renaming domain \"%s\"'s configuration \"%s\" to \"%s\"", getName().c_str(), strName.c_str(), strNewName.c_str());

    // Rename
    return pDomainConfiguration->rename(strNewName, strError);
}

bool CConfigurableDomain::restoreConfiguration(const string& strName, CParameterBlackboard* pMainBlackboard, bool bAutoSync, std::list<string>& lstrError) const
{
    string strError;

    const CDomainConfiguration* pDomainConfiguration = findConfiguration(strName, strError);

    if (!pDomainConfiguration) {

        lstrError.push_back(strError);
        return false;
    }
    log_info("Restoring domain \"%s\"'s configuration \"%s\" to parameter blackboard", getName().c_str(), pDomainConfiguration->getName().c_str());

    // Delegate
    bool bSuccess = pDomainConfiguration->restore(pMainBlackboard, bAutoSync && _bSequenceAware, &lstrError);

    // Record last applied configuration
    _pLastAppliedConfiguration = pDomainConfiguration;

    // Synchronize
    if (bAutoSync && !_bSequenceAware) {

        bSuccess &= _syncerSet.sync(*pMainBlackboard, false, &lstrError);
    }
    return bSuccess;
}

bool CConfigurableDomain::saveConfiguration(const string& strName, const CParameterBlackboard* pMainBlackboard, string& strError)
{
    // Find Domain configuration
    CDomainConfiguration* pDomainConfiguration = findConfiguration(strName, strError);

    if (!pDomainConfiguration) {

        return false;
    }
    log_info("Saving domain \"%s\"'s configuration \"%s\" from parameter blackboard", getName().c_str(), pDomainConfiguration->getName().c_str());

    // Delegate
    pDomainConfiguration->save(pMainBlackboard);

    return true;
}

bool CConfigurableDomain::setElementSequence(const string& strConfiguration, const std::vector<string>& astrNewElementSequence, string& strError)
{
    // Find Domain configuration
    CDomainConfiguration* pDomainConfiguration = findConfiguration(strConfiguration, strError);

    if (!pDomainConfiguration) {

        return false;
    }

    // Delegate to configuration
    return pDomainConfiguration->setElementSequence(astrNewElementSequence, strError);
}

bool CConfigurableDomain::getElementSequence(const string& strConfiguration, string& strResult) const
{
    // Find Domain configuration
    const CDomainConfiguration* pDomainConfiguration = findConfiguration(strConfiguration, strResult);

    if (!pDomainConfiguration) {

        return false;
    }

    // Delegate to configuration
    pDomainConfiguration->getElementSequence(strResult);

    return true;
}

bool CConfigurableDomain::setApplicationRule(const string& strConfiguration, const string& strApplicationRule, const CSelectionCriteriaDefinition* pSelectionCriteriaDefinition, string& strError)
{
    // Find Domain configuration
    CDomainConfiguration* pDomainConfiguration = findConfiguration(strConfiguration, strError);

    if (!pDomainConfiguration) {

        return false;
    }

    // Delegate to configuration
    return pDomainConfiguration->setApplicationRule(strApplicationRule, pSelectionCriteriaDefinition, strError);
}

bool CConfigurableDomain::clearApplicationRule(const string& strConfiguration, string& strError)
{
    // Find Domain configuration
    CDomainConfiguration* pDomainConfiguration = findConfiguration(strConfiguration, strError);

    if (!pDomainConfiguration) {

        return false;
    }

    // Delegate to configuration
    pDomainConfiguration->clearApplicationRule();

    return true;
}

bool CConfigurableDomain::getApplicationRule(const string& strConfiguration, string& strResult) const
{
    // Find Domain configuration
    const CDomainConfiguration* pDomainConfiguration = findConfiguration(strConfiguration, strResult);

    if (!pDomainConfiguration) {

        return false;
    }

    // Delegate to configuration
    pDomainConfiguration->getApplicationRule(strResult);

    return true;
}

// Last applied configuration
string CConfigurableDomain::getLastAppliedConfigurationName() const
{
    if (_pLastAppliedConfiguration) {

        return _pLastAppliedConfiguration->getName();
    }
    return "<none>";
}

// Pending configuration
string CConfigurableDomain::getPendingConfigurationName() const
{
    const CDomainConfiguration* pPendingConfiguration = getPendingConfiguration();

    if (pPendingConfiguration) {

        return pPendingConfiguration->getName();
    }
    return "<none>";
}

// Ensure validity on whole domain from main blackboard
void CConfigurableDomain::validate(const CParameterBlackboard* pMainBlackboard)
{

    // Propagate
    size_t uiNbConfigurations = getNbChildren();
    size_t uiChild;

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

        CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild));

        pDomainConfiguration->validate(pMainBlackboard);
    }
}

// Ensure validity on areas related to configurable element
void CConfigurableDomain::validateAreas(const CConfigurableElement* pConfigurableElement, const CParameterBlackboard* pMainBlackboard)
{
    log_info("Validating domain \"%s\" against main blackboard for configurable element \"%s\"", getName().c_str(), pConfigurableElement->getPath().c_str());

    // Propagate
    size_t uiNbConfigurations = getNbChildren();
    size_t uiChild;

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

        CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild));

        pDomainConfiguration->validate(pConfigurableElement, pMainBlackboard);
    }
}

// Attempt validation for all configurable element's areas, relying on already existing valid configuration inside domain
void CConfigurableDomain::autoValidateAll()
{
    // Validate
    ConfigurableElementListIterator it;

    // Browse all configurable elements for configuration validation
    for (it = _configurableElementList.begin(); it != _configurableElementList.end(); ++it) {

        const CConfigurableElement* pConfigurableElement = *it;

        // Auto validate element
        autoValidateAreas(pConfigurableElement);
    }
}

// Attempt validation for configurable element's areas, relying on already existing valid configuration inside domain
void CConfigurableDomain::autoValidateAreas(const CConfigurableElement* pConfigurableElement)
{
    // Find first valid configuration for given configurable element
    const CDomainConfiguration* pValidDomainConfiguration = findValidDomainConfiguration(pConfigurableElement);

    // No valid configuration found, give up
    if (!pValidDomainConfiguration) {

        return;
    }

    // Validate all other configurations against found one, if any
    size_t uiNbConfigurations = getNbChildren();
    size_t uiChild;

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

        CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild));

        if (pDomainConfiguration != pValidDomainConfiguration && !pDomainConfiguration->isValid(pConfigurableElement)) {
            // Validate
            pDomainConfiguration->validateAgainst(pValidDomainConfiguration, pConfigurableElement);
        }
    }
}

// Attempt configuration validation for all configurable elements' areas, relying on already existing valid configuration inside domain
bool CConfigurableDomain::autoValidateConfiguration(CDomainConfiguration* pDomainConfiguration)
{
    // Find another configuration than this one, that ought to be valid!
    size_t uiNbConfigurations = getNbChildren();
    size_t uiChild;

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

        const CDomainConfiguration* pPotententialValidDomainConfiguration = static_cast<const CDomainConfiguration*>(getChild(uiChild));

        if (pPotententialValidDomainConfiguration != pDomainConfiguration) {

            // Validate against it
            pDomainConfiguration->validateAgainst(pPotententialValidDomainConfiguration);

            return true;
        }
    }
    return false;
}

// Search for a valid configuration for given configurable element
const CDomainConfiguration* CConfigurableDomain::findValidDomainConfiguration(const CConfigurableElement* pConfigurableElement) const
{
    size_t uiNbConfigurations = getNbChildren();
    size_t uiChild;

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

        const CDomainConfiguration* pDomainConfiguration = static_cast<const CDomainConfiguration*>(getChild(uiChild));

        if (pDomainConfiguration->isValid(pConfigurableElement)) {

            return pDomainConfiguration;
        }
    }
    return NULL;
}

// Search for an applicable configuration
const CDomainConfiguration* CConfigurableDomain::findApplicableDomainConfiguration() const
{
    size_t uiNbConfigurations = getNbChildren();
    size_t uiChild;

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

        const CDomainConfiguration* pDomainConfiguration = static_cast<const CDomainConfiguration*>(getChild(uiChild));

        if (pDomainConfiguration->isApplicable()) {

            return pDomainConfiguration;
        }
    }
    return NULL;
}

// Gather set of configurable elements
void CConfigurableDomain::gatherConfigurableElements(std::set<const CConfigurableElement*>& configurableElementSet) const
{
    // Insert all configurable elements
    configurableElementSet.insert(_configurableElementList.begin(), _configurableElementList.end());
}

// Check configurable element already attached
bool CConfigurableDomain::containsConfigurableElement(const CConfigurableElement* pConfigurableCandidateElement) const
{
    ConfigurableElementListIterator it;

    // Browse all configurable elements for comparison
    for (it = _configurableElementList.begin(); it != _configurableElementList.end(); ++it) {

        if (pConfigurableCandidateElement == *it) {

            return true;
        }
    }
    return false;
}

// Merge any descended configurable element to this one with this one
void CConfigurableDomain::mergeAlreadyAssociatedDescendantConfigurableElements(CConfigurableElement* pNewConfigurableElement)
{
    std::list<CConfigurableElement*> mergedConfigurableElementList;

    ConfigurableElementListIterator it;

    // Browse all configurable elements (new one not yet in the list!)
    for (it = _configurableElementList.begin(); it != _configurableElementList.end(); ++it) {

        CConfigurableElement* pConfigurablePotentialDescendantElement = *it;

        if (pConfigurablePotentialDescendantElement->isDescendantOf(pNewConfigurableElement)) {

            log_info("In domain \"%s\", merging descendant configurable element's configurations \"%s\" into its ascendant \"%s\" ones", getName().c_str(), pConfigurablePotentialDescendantElement->getName().c_str(), pNewConfigurableElement->getName().c_str());

            // Merge configuration data
            mergeConfigurations(pNewConfigurableElement, pConfigurablePotentialDescendantElement);

            // Keep track for removal
            mergedConfigurableElementList.push_back(pConfigurablePotentialDescendantElement);
        }
    }

    // Remove all merged elements (new one not yet in the list!)
    for (it = mergedConfigurableElementList.begin(); it != mergedConfigurableElementList.end(); ++it) {

        CConfigurableElement* pMergedConfigurableElement = *it;

        // Remove merged from configurable element from internal tracking list
        // Note: we shouldn't need to recompute the sync set in that case, as the merged to element should include the syncers of merged from elements
        doRemoveConfigurableElement(pMergedConfigurableElement, false);
    }
}

void CConfigurableDomain::mergeConfigurations(CConfigurableElement* pToConfigurableElement, CConfigurableElement* pFromConfigurableElement)
{
    // Propagate to domain configurations
    size_t uiNbConfigurations = getNbChildren();
    size_t uiChild;

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

        CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild));

        // Do the merge.
        pDomainConfiguration->merge(pToConfigurableElement, pFromConfigurableElement);
    }
}

// Configurable elements association
void CConfigurableDomain::doAddConfigurableElement(CConfigurableElement* pConfigurableElement, const CParameterBlackboard *pMainBlackboard)
{
    // Inform configurable element
    pConfigurableElement->addAttachedConfigurableDomain(this);

    // Create associated syncer set
    CSyncerSet* pSyncerSet = new CSyncerSet;

    // Add to sync set the configurable element one
    pConfigurableElement->fillSyncerSet(*pSyncerSet);

    // Store it
    _configurableElementToSyncerSetMap[pConfigurableElement] = pSyncerSet;

    // Add it to global one
    _syncerSet += *pSyncerSet;

    // Inform configurations
    size_t uiNbConfigurations = getNbChildren();
    size_t uiChild;

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

        CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild));

        pDomainConfiguration->addConfigurableElement(pConfigurableElement, pSyncerSet);
    }

    // Ensure area validity for that configurable element (if main blackboard provided)
    if (pMainBlackboard) {

        // Need to validate against main blackboard
        validateAreas(pConfigurableElement, pMainBlackboard);
    }

    // Already associated descendend configurable elements need a merge of their configuration data
    mergeAlreadyAssociatedDescendantConfigurableElements(pConfigurableElement);

    // Add to list
    _configurableElementList.push_back(pConfigurableElement);
}

void CConfigurableDomain::doRemoveConfigurableElement(CConfigurableElement* pConfigurableElement, bool bRecomputeSyncSet)
{
    // Remove from list
    _configurableElementList.remove(pConfigurableElement);

    // Remove associated syncer set
    CSyncerSet* pSyncerSet = getSyncerSet(pConfigurableElement);

    _configurableElementToSyncerSetMap.erase(pConfigurableElement);

    delete pSyncerSet;

    // Inform configurable element
    pConfigurableElement->removeAttachedConfigurableDomain(this);

    // Inform configurations
    size_t uiNbConfigurations = getNbChildren();
    size_t uiChild;

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

        CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(getChild(uiChild));

        pDomainConfiguration->removeConfigurableElement(pConfigurableElement);
    }
    // Recompute our sync set if needed
    if (bRecomputeSyncSet) {

        computeSyncSet();
    }
}

// Syncer set retrieval from configurable element
CSyncerSet* CConfigurableDomain::getSyncerSet(const CConfigurableElement* pConfigurableElement) const
{
    ConfigurableElementToSyncerSetMapIterator mapIt = _configurableElementToSyncerSetMap.find(pConfigurableElement);

    assert(mapIt != _configurableElementToSyncerSetMap.end());

    return mapIt->second;
}

// Configuration retrieval
CDomainConfiguration* CConfigurableDomain::findConfiguration(const string& strConfiguration, string& strError)
{
    CDomainConfiguration* pDomainConfiguration = static_cast<CDomainConfiguration*>(findChild(strConfiguration));

    if (!pDomainConfiguration) {

        strError = "Domain configuration " + strConfiguration + " not found";

        return NULL;
    }
    return pDomainConfiguration;
}

const CDomainConfiguration* CConfigurableDomain::findConfiguration(const string& strConfiguration, string& strError) const
{
    const CDomainConfiguration* pDomainConfiguration = static_cast<const CDomainConfiguration*>(findChild(strConfiguration));

    if (!pDomainConfiguration) {

        strError = "Domain configuration " + strConfiguration + " not found";

        return NULL;
    }
    return pDomainConfiguration;
}