-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathWorkspace.cpp
2980 lines (2604 loc) · 108 KB
/
Workspace.cpp
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
/***********************************************************************************************************************
* OpenStudio(R), Copyright (c) 2008-2020, Alliance for Sustainable Energy, LLC, and other contributors. 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 any contributors may be used to endorse or promote products
* derived from this software without specific prior written permission from the respective party.
*
* (4) Other than as required in clauses (1) and (2), distributions in any form of modifications or other derivative works
* may not use the "OpenStudio" trademark, "OS", "os", or any other confusingly similar designation without specific prior
* written permission from Alliance for Sustainable Energy, LLC.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND ANY 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(S), ANY CONTRIBUTORS, THE UNITED STATES GOVERNMENT, OR THE UNITED
* STATES DEPARTMENT OF ENERGY, NOR ANY OF THEIR EMPLOYEES, 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 "Workspace.hpp"
#include "Workspace_Impl.hpp"
#include "IdfFile.hpp"
#include "ValidityReport.hpp"
#include <utilities/idd/IddEnums.hxx>
#include <utilities/idd/IddFactory.hxx>
#include "../plot/ProgressBar.hpp"
#include "../core/Assert.hpp"
#include "../core/StringHelpers.hpp"
#include <boost/lexical_cast.hpp>
using namespace std;
using openstudio::istringEqual; // used for all name comparisons
namespace openstudio {
namespace detail {
// CONSTRUCTORS
Workspace_Impl::Workspace_Impl(StrictnessLevel level,IddFileType iddFileType) :
m_strictnessLevel(level),
m_iddFileAndFactoryWrapper(iddFileType),
m_fastNaming(false),
m_workspaceObjectOrder(std::shared_ptr<WorkspaceObjectOrder_Impl>(new
WorkspaceObjectOrder_Impl(HandleVector(),std::bind(&Workspace_Impl::getObject,this,std::placeholders::_1))))
{
m_workspaceObjectMap.reserve(1<<15);
m_idfReferencesMap.reserve(1<<15);
}
Workspace_Impl::Workspace_Impl(const IdfFile& idfFile,
StrictnessLevel level) :
m_strictnessLevel(level),
m_header(idfFile.header()),
m_iddFileAndFactoryWrapper(idfFile.iddFileAndFactoryWrapper()),
m_fastNaming(false),
m_workspaceObjectOrder(std::shared_ptr<WorkspaceObjectOrder_Impl>(new
WorkspaceObjectOrder_Impl(HandleVector(),std::bind(&Workspace_Impl::getObject,this,std::placeholders::_1))))
{
m_workspaceObjectMap.reserve(1<<15);
m_idfReferencesMap.reserve(1<<15);
}
Workspace_Impl::Workspace_Impl(const Workspace_Impl& other,bool keepHandles) :
m_strictnessLevel(other.m_strictnessLevel),
m_header(other.m_header),
m_iddFileAndFactoryWrapper(other.m_iddFileAndFactoryWrapper),
m_fastNaming(other.fastNaming()),
m_workspaceObjectOrder(std::shared_ptr<WorkspaceObjectOrder_Impl>(new
WorkspaceObjectOrder_Impl(std::bind(&Workspace_Impl::getObject,this,std::placeholders::_1))))
{
// m_workspaceObjectOrder
OptionalIddObjectTypeVector iddOrderVector = other.order().iddOrder();
if (iddOrderVector) {
m_workspaceObjectOrder.setIddOrder(*iddOrderVector);
}
OptionalHandleVector directOrderVector = other.order().directOrder();
if (directOrderVector) {
m_workspaceObjectOrder.setDirectOrder(*directOrderVector);
}
m_workspaceObjectMap.reserve(1<<15);
m_idfReferencesMap.reserve(1<<15);
}
Workspace_Impl::Workspace_Impl(const Workspace_Impl& other,
const std::vector<Handle>& hs,
bool keepHandles,
StrictnessLevel level) :
m_strictnessLevel(level),
m_header(), // subset of original data--discard header
m_iddFileAndFactoryWrapper(other.m_iddFileAndFactoryWrapper),
m_fastNaming(other.fastNaming()),
m_workspaceObjectOrder(std::shared_ptr<WorkspaceObjectOrder_Impl>(new
WorkspaceObjectOrder_Impl(hs,std::bind(&Workspace_Impl::getObject,this,std::placeholders::_1))))
{
// m_workspaceObjectOrder
OptionalIddObjectTypeVector iddOrderVector = other.order().iddOrder();
if (iddOrderVector) {
m_workspaceObjectOrder.setIddOrder(*iddOrderVector);
}
OptionalHandleVector directOrderVector = other.order().directOrder();
if (directOrderVector) {
// discard unused handles
HandleVector subsetOrder;
for (const Handle& h : *directOrderVector) {
if (std::find(hs.begin(),hs.end(),h) != hs.end()) { subsetOrder.push_back(h); }
}
m_workspaceObjectOrder.setDirectOrder(subsetOrder);
}
m_workspaceObjectMap.reserve(1<<15);
m_idfReferencesMap.reserve(1<<15);
}
Workspace Workspace_Impl::clone(bool keepHandles) const {
// copy everything but objects
std::shared_ptr<Workspace_Impl> cloneImpl(new Workspace_Impl(*this,keepHandles));
// clone objects
createAndAddClonedObjects(workspace().getImpl<Workspace_Impl>(),cloneImpl,keepHandles);
// wrap impl and return
Workspace result(cloneImpl);
return result;
}
Workspace Workspace_Impl::cloneSubset(const std::vector<Handle>& handles,
bool keepHandles,
StrictnessLevel level) const
{
// copy everything but objects
std::shared_ptr<Workspace_Impl> cloneImpl(new Workspace_Impl(*this,handles,keepHandles,level));
// clone objects
createAndAddSubsetClonedObjects(workspace().getImpl<Workspace_Impl>(),cloneImpl,handles,keepHandles);
// wrap impl and return
Workspace result(cloneImpl);
return result;
}
void Workspace_Impl::swap(Workspace& other) {
std::shared_ptr<Workspace_Impl> otherImpl = other.getImpl<Workspace_Impl>();
StrictnessLevel tsl = m_strictnessLevel;
m_strictnessLevel = otherImpl->m_strictnessLevel;
otherImpl->m_strictnessLevel = tsl;
std::string th = m_header;
m_header = otherImpl->m_header;
otherImpl->m_header = th;
IddFileAndFactoryWrapper tifafw = m_iddFileAndFactoryWrapper;
m_iddFileAndFactoryWrapper = otherImpl->m_iddFileAndFactoryWrapper;
otherImpl->m_iddFileAndFactoryWrapper = tifafw;
bool tfn = m_fastNaming;
m_fastNaming = otherImpl->m_fastNaming;
otherImpl->m_fastNaming = tfn;
WorkspaceObjectMap twop = m_workspaceObjectMap;
m_workspaceObjectMap = otherImpl->m_workspaceObjectMap;
otherImpl->m_workspaceObjectMap = twop;
WorkspaceObjectOrder twoo = m_workspaceObjectOrder;
m_workspaceObjectOrder = otherImpl->m_workspaceObjectOrder;
otherImpl->m_workspaceObjectOrder = twoo;
IddObjectTypeMap tiotm = m_iddObjectTypeMap;
m_iddObjectTypeMap = otherImpl->m_iddObjectTypeMap;
otherImpl->m_iddObjectTypeMap = tiotm;
IdfReferencesMap tirm = m_idfReferencesMap;
m_idfReferencesMap = otherImpl->m_idfReferencesMap;
otherImpl->m_idfReferencesMap = tirm;
}
// GETTERS
Workspace Workspace_Impl::workspace() {
return getWorkspace<Workspace>();
}
Workspace Workspace_Impl::workspace() const {
return getWorkspace<Workspace>();
}
StrictnessLevel Workspace_Impl::strictnessLevel() const {
return m_strictnessLevel;
}
VersionString Workspace_Impl::version() const {
return VersionString(m_iddFileAndFactoryWrapper.version());
}
IddFile Workspace_Impl::iddFile() const {
return m_iddFileAndFactoryWrapper.iddFile();
}
IddFileType Workspace_Impl::iddFileType() const {
return m_iddFileAndFactoryWrapper.iddFileType();
}
boost::optional<IddObject> Workspace_Impl::getIddObject(const IddObjectType& type) const {
return m_iddFileAndFactoryWrapper.getObject(type);
}
boost::optional<std::string> Workspace_Impl::name(const Handle& handle) const {
OptionalWorkspaceObject object = getObject(handle);
if (object) {
return object->name();
}
return boost::none;
}
boost::optional<WorkspaceObject> Workspace_Impl::getObject(const Handle& handle) const {
auto womIt = m_workspaceObjectMap.find(handle);
if (womIt != m_workspaceObjectMap.end()) {
return WorkspaceObject(womIt->second);
}
return boost::none;
}
std::vector<WorkspaceObject> Workspace_Impl::objects(bool sorted) const {
OptionalIddObject versionIdd = m_iddFileAndFactoryWrapper.versionObject();
if (!versionIdd) { return WorkspaceObjectVector(); }
if (sorted) {
OptionalHandleVector directOrder = order().directOrder();
if (directOrder && (directOrder->size() == numObjects())) {
WorkspaceObjectVector result;
HandleSet setToCheckUniqueness;
for (const Handle& h : *directOrder) {
OptionalWorkspaceObject owo = getObject(h);
std::pair<HandleSet::iterator,bool> insertResult = setToCheckUniqueness.insert(h);
if (owo && insertResult.second && (owo->iddObject() != versionIdd.get()) )
{
result.push_back(*owo);
}
else {
return sort(objects(false));
}
}
return result;
}
return sort(objects(false));
}
WorkspaceObjectVector result;
for (const WorkspaceObjectMap::value_type& p : m_workspaceObjectMap) {
WorkspaceObject obj = WorkspaceObject(p.second);
if (obj.iddObject() != versionIdd.get()) {
result.push_back(obj);
}
}
return result;
}
std::vector<Handle> Workspace_Impl::handles(bool sorted) const {
if (sorted) {
OptionalHandleVector directOrder = order().directOrder();
if (directOrder && (directOrder->size() == numObjects())) {
HandleSet setToCheckUniqueness;
for (const Handle& h : *directOrder) {
std::pair<HandleSet::iterator,bool> insertResult = setToCheckUniqueness.insert(h);
if (!insertResult.second) {
return sort(handles(false));
}
}
return *directOrder;
}
return sort(handles(false));
}
HandleVector result;
OptionalIddObject versionIdd = m_iddFileAndFactoryWrapper.versionObject();
if (!versionIdd) { return result; }
for (const WorkspaceObjectMap::value_type& p : m_workspaceObjectMap) {
if (p.second->iddObject() != versionIdd.get()) {
result.push_back(p.first);
}
}
return result;
}
std::vector<WorkspaceObject> Workspace_Impl::objectsWithURLFields() const {
WorkspaceObjectVector result;
for (const WorkspaceObjectMap::value_type& p : m_workspaceObjectMap) {
if( p.second->iddObject().hasURL()) {
result.push_back(WorkspaceObject(p.second));
}
}
return result;
}
boost::optional<WorkspaceObject> Workspace_Impl::versionObject() const {
OptionalWorkspaceObject result;
WorkspaceObjectVector candidates;
if (iddFileType() == IddFileType::EnergyPlus) {
candidates = getObjectsByType(IddObjectType(IddObjectType::Version));
}
else if (iddFileType() == IddFileType::OpenStudio) {
candidates = getObjectsByType(IddObjectType(IddObjectType::OS_Version));
}
else if (OptionalIddObject versionIdd = m_iddFileAndFactoryWrapper.versionObject()){
candidates = getObjectsByType(*versionIdd);
}
if (candidates.size() == 1u) {
result = candidates[0];
}
return result;
}
std::vector<WorkspaceObject> Workspace_Impl::getObjects(const std::vector<Handle>& handles) const
{
WorkspaceObjectVector result;
result.reserve(handles.size());
bool lossy = false;
// return in same order
for (const Handle& handle : handles) {
OptionalWorkspaceObject candidate = getObject(handle);
if (candidate) { result.push_back(*candidate); }
else { lossy = true; }
}
if (lossy) { LOG(Warn,"Invalid handle. Cannot return all requested objects."); }
return result;
}
std::vector<WorkspaceObject> Workspace_Impl::getObjectsByName(const std::string& name,
bool exactMatch) const
{
WorkspaceObjectVector result;
if (exactMatch) {
for (const WorkspaceObjectMap::value_type& p : m_workspaceObjectMap) {
if (OptionalString candidate = p.second->name()) {
if (istringEqual(*candidate,name)) {
result.push_back(WorkspaceObject(p.second));
}
}
}
}
else {
std::string baseName = getBaseName(name);
for (const WorkspaceObjectMap::value_type& p : m_workspaceObjectMap) {
if (OptionalString candidate = p.second->name()) {
if (baseNamesMatch(baseName, *candidate)) {
result.push_back(WorkspaceObject(p.second));
}
}
}
}
return result;
}
std::vector<WorkspaceObject> Workspace_Impl::getObjectsByType(IddObjectType objectType) const {
auto loc = m_iddObjectTypeMap.find(objectType);
if (loc == m_iddObjectTypeMap.end()) { return WorkspaceObjectVector(); }
std::vector<WorkspaceObject> result;
result.reserve(loc->second.size());
for( auto it = loc->second.begin(); it != loc->second.end(); ++it ) {
result.push_back( it->second );
}
return result;
}
std::vector<WorkspaceObject> Workspace_Impl::getObjectsByType(const IddObject& objectType) const {
WorkspaceObjectVector result;
for (const WorkspaceObject& object : objects()) {
if (object.iddObject() == objectType) {
result.push_back(object);
}
}
return result;
}
boost::optional<WorkspaceObject> Workspace_Impl::getObjectByTypeAndName(
IddObjectType objectType,const std::string& name) const
{
for (const WorkspaceObject& object : getObjectsByType(objectType)) {
OptionalString candidate = object.name();
if (candidate && istringEqual(*candidate,name)) {
return object;
}
}
return boost::none;
}
std::vector<WorkspaceObject> Workspace_Impl::getObjectsByTypeAndName(
IddObjectType objectType,
const std::string& name) const
{
WorkspaceObjectVector result;
std::string baseName = getBaseName(name);
for (const WorkspaceObject& object : getObjectsByType(objectType)) {
if (OptionalString candidate = object.name()) {
if (baseNamesMatch(baseName, *candidate)) {
result.push_back(object);
}
}
}
return result;
}
std::vector<WorkspaceObject> Workspace_Impl::getObjectsByReference(
const std::string& referenceName) const
{
auto loc = m_idfReferencesMap.find(referenceName);
if (loc == m_idfReferencesMap.end()) { return WorkspaceObjectVector(); }
std::vector<WorkspaceObject> result;
result.reserve(loc->second.size());
for( auto it = loc->second.begin(); it != loc->second.end(); ++it ) {
result.push_back( it->second );
}
return result;
}
std::vector<WorkspaceObject> Workspace_Impl::getObjectsByReference(
const std::vector<std::string>& referenceNames) const
{
WorkspaceObjectMap objectMap;
for (const std::string& referenceName : referenceNames) {
auto loc = m_idfReferencesMap.find(referenceName);
if (loc != m_idfReferencesMap.end()) {
objectMap.insert(loc->second.begin(),loc->second.end());
}
}
std::vector<WorkspaceObject> result;
result.reserve(objectMap.size());
for( WorkspaceObjectMap::const_iterator it = objectMap.begin(); it != objectMap.end(); ++it ) {
result.push_back( it->second );
}
return result;
}
boost::optional<WorkspaceObject> Workspace_Impl::getObjectByNameAndReference(
std::string name,
const std::vector<std::string>& referenceNames) const
{
for (const WorkspaceObject& object : getObjectsByReference(referenceNames)) {
OptionalString candidate = object.name();
if (candidate && istringEqual(*candidate,name)) {
return object;
}
}
return boost::none;
}
bool Workspace_Impl::fastNaming() const
{
return m_fastNaming;
}
// SETTERS
bool Workspace_Impl::setStrictnessLevel(StrictnessLevel level) {
if (isValid(level)) {
m_strictnessLevel = level;
return true;
}
return false;
}
// Helper function to start the process of adding an object to the workspace.
std::shared_ptr<WorkspaceObject_Impl> Workspace_Impl::createObject(const IdfObject& object,
bool keepHandle)
{
return WorkspaceObject_ImplPtr(new WorkspaceObject_Impl(object,this,keepHandle));
}
// Helper function to start the process of adding a cloned object to the workspace.
WorkspaceObject_ImplPtr Workspace_Impl::createObject(
const std::shared_ptr<WorkspaceObject_Impl>& originalObjectImplPtr,
bool keepHandle)
{
OS_ASSERT(originalObjectImplPtr);
return WorkspaceObject_ImplPtr(new WorkspaceObject_Impl(*originalObjectImplPtr,
this,
keepHandle));
}
std::vector<WorkspaceObject> Workspace_Impl::addObjects(
std::vector<std::shared_ptr<WorkspaceObject_Impl> >& objectImplPtrs,
bool checkNames)
{
return addObjects(objectImplPtrs, UHPointerVector(), HUPointerVector(), true, false, checkNames);
}
std::vector<WorkspaceObject> Workspace_Impl::addObjects(
std::vector<std::shared_ptr<WorkspaceObject_Impl> >& objectImplPtrs,
const std::vector<UHPointer>& pointersIntoWorkspace,
const std::vector<HUPointer>& pointersFromWorkspace,
bool driverMethod,
bool expectToLosePointers,
bool checkNames)
{
HandleVector newHandles;
WorkspaceObjectVector newObjects;
int i = 0;
int N = objectImplPtrs.size();
this->progressRange.nano_emit(0, 3*N);
this->progressValue.nano_emit(0);
this->progressCaption.nano_emit("Adding Objects");
// step 1: add to maps
bool ok = true;
for (WorkspaceObject_ImplPtr& ptr : objectImplPtrs) {
ok = ok && nominallyAddObject(ptr); // will fail if ptr already in map
if (ok) {
newHandles.push_back(ptr->handle());
LOG(Trace,"Adding object with handle " << newHandles.back());
}
else {
LOG(Error,"Tried to add two objects with the same handle: " << ptr->handle());
}
this->progressValue.nano_emit(++i);
}
// step 2: replace string pointers
if (ok){
for (WorkspaceObject_ImplPtr& ptr : objectImplPtrs) {
ptr->initializeOnAdd(expectToLosePointers);
this->progressValue.nano_emit(++i);
}
}
// step 3: handle provided relationships
if (ok && (!pointersIntoWorkspace.empty() || !pointersFromWorkspace.empty())) {
ok = ok && addProvidedRelationships(newHandles,pointersIntoWorkspace,pointersFromWorkspace);
}
// step 4: register initialization
if (ok){
for (WorkspaceObject_ImplPtr& ptr : objectImplPtrs) {
ptr->setInitialized();
newObjects.push_back(WorkspaceObject(ptr));
this->progressValue.nano_emit(++i);
}
}
// step 5: check validity
if (ok && driverMethod) {
StrictnessLevel level = strictnessLevel();
if ((objectImplPtrs.size() == numAllObjects()) || (level == StrictnessLevel::Final)) {
// check whole workspace
ok = isValid();
}
else {
// check individual objects
for (const WorkspaceObject& newObject : newObjects) {
ok = ok && newObject.isValid(level, checkNames);
if (!ok) {
break;
}
}
}
}
// step 6: rollback if necessary
if (!ok) {
LOG(Info,"Unable to add objects to Workspace. The validity report is: " <<
std::endl << validityReport());
nominallyRemoveObjects(newHandles); // no validity check
for (WorkspaceObject_ImplPtr& ptr : objectImplPtrs) {
ptr->disconnect();
}
newObjects.clear();
return newObjects;
}
// step 7: emit signals for successful completion
if (driverMethod) {
for (const WorkspaceObject& newObject : newObjects) {
registerAdditionOfObject(newObject);
}
}
return newObjects;
}
std::vector<WorkspaceObject> Workspace_Impl::addClones(
std::vector< std::shared_ptr<WorkspaceObject_Impl> >& objectImplPtrs,
const HandleMap& oldNewHandleMap,
bool collectionClone,
const std::vector<UHPointer>& pointersIntoWorkspace,
const std::vector<HUPointer>& pointersFromWorkspace,
bool driverMethod)
{
int i = 0;
int N = objectImplPtrs.size();
if (oldNewHandleMap.empty()) {
this->progressRange.nano_emit(0, 2*N);
}
else {
this->progressRange.nano_emit(0, 3*N);
}
this->progressValue.nano_emit(0);
this->progressCaption.nano_emit("Cloning Objects");
// step 1: add objects to maps
HandleVector newHandles;
for (const WorkspaceObject_ImplPtr& ptr : objectImplPtrs) {
newHandles.push_back(ptr->handle());
m_workspaceObjectMap.insert(WorkspaceObjectMap::value_type(newHandles.back(),ptr));
insertIntoIddObjectTypeMap(ptr);
insertIntoIdfReferencesMap(ptr);
this->progressValue.nano_emit(++i);
}
// step 2: apply handle map to pointers
if (!oldNewHandleMap.empty()) {
for (const WorkspaceObject_ImplPtr& ptr : objectImplPtrs) {
ptr->initializeOnClone(oldNewHandleMap);
this->progressValue.nano_emit(++i);
}
}
// step 3: apply handle map to orderer
if (!oldNewHandleMap.empty() && m_workspaceObjectOrder.isDirectOrder()) {
if (collectionClone) {
// objects in order, just under wrong handles
HandleVector directOrderVector = order().directOrder().get();
HandleVector mappedOrder = applyHandleMap(directOrderVector,oldNewHandleMap);
m_workspaceObjectOrder.setDirectOrder(mappedOrder);
}
else {
// new objects not yet in order
for (const WorkspaceObject_ImplPtr& ptr : objectImplPtrs) {
m_workspaceObjectOrder.push_back(ptr->handle());
}
}
}
// step 4: handle provided relationships
bool ok = true;
if (!pointersIntoWorkspace.empty() || !pointersFromWorkspace.empty()) {
ok = ok && addProvidedRelationships(newHandles,pointersIntoWorkspace,pointersFromWorkspace);
}
// step 5: register initialization
WorkspaceObjectVector newObjects;
for (WorkspaceObject_ImplPtr& ptr : objectImplPtrs) {
ptr->setInitialized();
newObjects.push_back(WorkspaceObject(ptr));
this->progressValue.nano_emit(++i);
}
// step 6: check validity
StrictnessLevel level = strictnessLevel();
if (ok && driverMethod && (!collectionClone || (level == StrictnessLevel::Final))) {
if (objectImplPtrs.size() == numObjects()) {
// check whole workspace
ok = isValid();
}
else {
// check individual objects
for (const WorkspaceObject& newObject : newObjects) {
ok = ok && newObject.isValid(level);
if ((level > StrictnessLevel::Draft) &&
(newObject.iddObject().properties().unique))
{
ok = ok && (numObjectsOfType(newObject.iddObject().type()) == 1u);
}
if (!ok) {
break;
}
}
}
}
// step 7: rollback if necessary
if (!ok) {
LOG(Info,"Unable to add cloned objects to Workspace. The validity report is: " <<
std::endl << validityReport());
nominallyRemoveObjects(newHandles); // no validity check
for (WorkspaceObject_ImplPtr& ptr : objectImplPtrs) {
ptr->disconnect();
}
newObjects.clear();
return newObjects;
}
// step 8: emit signals for successful completion
if (driverMethod) {
for (const WorkspaceObject& newObject : newObjects) {
registerAdditionOfObject(newObject);
}
}
return newObjects;
}
boost::optional<WorkspaceObject> Workspace_Impl::addObject(const IdfObject& idfObject) {
WorkspaceObject_ImplPtr objectImplPtr;
// handle potential name conflicts up front
IdfObject workingIdfObject = idfObject; // shallow copy
if (idfObject.iddObject().hasNameField()) {
OptionalString currentName = idfObject.name();
if (currentName && !currentName->empty()) {
bool potentialConflict = potentialNameConflict(idfObject.name().get(),idfObject.iddObject());
if (potentialConflict) {
LOG(Info,"Resetting name of " << idfObject.briefDescription()
<< " to avoid name conflict upon adding it to the Workspace.");
workingIdfObject = idfObject.clone(); // deep copy
// clear name of single object--WorkspaceObject_Impl constructor will make new one
bool ok = workingIdfObject.setName("").has_value();
OS_ASSERT(ok);
}
}
}
// create new object
objectImplPtr = this->createObject(workingIdfObject,false);
// add to workspace
WorkspaceObject_ImplPtrVector objectImplPtrs(1u,objectImplPtr);
WorkspaceObjectVector objects = addObjects(objectImplPtrs, false);
// assess result and return
if (objects.size() != 1) {
OS_ASSERT(objects.size() == 0);
return boost::none;
}
return objects[0];
}
boost::optional<WorkspaceObject> Workspace_Impl::insertObject(const IdfObject& idfObject) {
OptionalWorkspaceObject result = getEquivalentObject(idfObject);
if (!result) {
result = addObject(idfObject);
}
return result;
}
std::vector<WorkspaceObject> Workspace_Impl::addObjects(const std::vector<IdfObject>& idfObjects, bool checkNames) {
WorkspaceObjectVector result;
WorkspaceObject_ImplPtrVector newObjects;
if (idfObjects.empty()) {
return result;
}
bool keepHandles = idfObjects[0].iddObject().hasHandleField();
bool checkedForNameConflicts(false);
if (numObjects() > 0) {
// handle potential name conflicts
Workspace working = this->cloneSubset(HandleVector()); // empty clone
working.order().setDirectOrder(HandleVector()); // maintain order in vector
// call method like this one, but simpler, which expects to lose outward pointers
WorkspaceObjectVector wsObjects = working.getImpl<detail::Workspace_Impl>()->simpleAddObjects(idfObjects);
if (wsObjects.size() != idfObjects.size()) {
LOG(Error,"Unable to add idfObjects to their own Workspace as an intermediate step.");
return WorkspaceObjectVector();
}
bool changes = resolvePotentialNameConflicts(working);
checkedForNameConflicts = true;
if (changes) {
// merge data and create objects
auto it(idfObjects.begin()), itEnd(idfObjects.end());
for (const WorkspaceObject& object : wsObjects) {
OS_ASSERT(it != itEnd);
IdfObject mergedObject = object.idfObject();
mergeIdfObjectAfterPotentialNameConflictResolution(mergedObject,*it);
newObjects.push_back(this->createObject(mergedObject,keepHandles));
++it;
}
// add objects
result = addObjects(newObjects,checkNames);
return result;
}
}
// no name conflicts---directly create and add objects
OS_ASSERT(newObjects.empty());
auto it(idfObjects.begin()), itEnd(idfObjects.end());
for (; it != itEnd; ++it) {
newObjects.push_back(this->createObject(*it,keepHandles));
}
result = addObjects(newObjects,checkNames);
if (!checkedForNameConflicts) {
Workspace thisWorkspace = workspace();
resolvePotentialNameConflicts(thisWorkspace);
}
return result;
}
std::vector<WorkspaceObject> Workspace_Impl::insertObjects(const IdfObjectVector& idfObjects) {
return addAndInsertObjects(IdfObjectVector(),idfObjects);
}
std::vector<WorkspaceObject> Workspace_Impl::addAndInsertObjects(
const std::vector<IdfObject>& objectsToAdd, const std::vector<IdfObject>& objectsToInsert)
{
// merge objects into one vector
unsigned nAdd = objectsToAdd.size();
unsigned nInsert = objectsToInsert.size();
unsigned n = nAdd + nInsert;
IdfObjectVector idfObjects = objectsToAdd;
idfObjects.insert(idfObjects.end(),objectsToInsert.begin(),objectsToInsert.end());
OS_ASSERT(idfObjects.size() == n);
if (idfObjects.empty()) {
return WorkspaceObjectVector();
}
bool keepHandles = idfObjects[0].iddObject().hasHandleField();
// find equivalent objects for objectsToInsert
UnsignedVector foundObjectIndices;
UnsignedVector notFoundObjectIndices;
WorkspaceObjectVector equivalentObjects;
unsigned i = nAdd;
for (unsigned i = nAdd, n = idfObjects.size(); i < n; ++i) {
OptionalWorkspaceObject oEquivalent = getEquivalentObject(idfObjects[i]);
if (oEquivalent) {
foundObjectIndices.push_back(i);
equivalentObjects.push_back(*oEquivalent);
}
else {
notFoundObjectIndices.push_back(i);
}
}
OS_ASSERT(foundObjectIndices.size() + notFoundObjectIndices.size() == nInsert);
// handle potential name conflicts. only objects to be added need attention.
Workspace working = this->cloneSubset(HandleVector()); // empty clone
working.order().setDirectOrder(HandleVector()); // maintain order in vector
WorkspaceObjectVector wsObjects = working.addObjects(idfObjects, false);
if (wsObjects.size() != idfObjects.size()) {
LOG(Error,"Unable to add idfObjects to their own Workspace as an intermediate step.");
return WorkspaceObjectVector();
}
bool changes = resolvePotentialNameConflicts(working,foundObjectIndices);
// create objects while handling the mess we have made above
WorkspaceObject_ImplPtrVector newObjects; // objects to be added
HUPointerVector pointersFromWorkspace; // workspace object -> object to be added
HHPointerVector pointersToRollBackOnFail; // workspace object -> workspace object pointers
// directly added by this method
i = 0; // step through all of the objects
auto equivIt = equivalentObjects.begin();
UnsignedVector::const_iterator foundIndicesBegin = foundObjectIndices.begin();
UnsignedVector::const_iterator foundIndicesEnd = foundObjectIndices.end();
// loop through Workspace versions of idfObjects
for (const WorkspaceObject& object : wsObjects) {
if (std::find(foundIndicesBegin,foundIndicesEnd,i) == foundIndicesEnd) {
// no equivalent object in workspace -- create new object to add
if (changes) {
IdfObject mergedObject = object.idfObject();
mergeIdfObjectAfterPotentialNameConflictResolution(mergedObject,idfObjects[i]);
newObjects.push_back(this->createObject(mergedObject,keepHandles));
}
else {
newObjects.push_back(this->createObject(idfObjects[i],keepHandles));
}
}
else {
// there is an equivalent object in workspace
OS_ASSERT(equivIt->iddObject() == object.iddObject());
// preserve idfObject[i] -> idfObject[j]
WorkspaceObjectVector targets = object.targets();
for (const WorkspaceObject& target : targets) {
OptionalUnsigned targetIndexInWorking = working.order().indexInOrder(target.handle());
OS_ASSERT(targetIndexInWorking);
UnsignedVector fieldIndicesObjectToTarget = object.getSourceIndices(target.handle());
OS_ASSERT(!fieldIndicesObjectToTarget.empty());
auto targetInFoundObjectsIt = std::find(
foundIndicesBegin,foundIndicesEnd,*targetIndexInWorking);
if (targetInFoundObjectsIt == foundIndicesEnd) {
// target has no equivalent in this Workspace
// get index target will have in newObjects vector
unsigned newObjectsIndexOfTarget(0);
if (*targetIndexInWorking < nAdd) {
newObjectsIndexOfTarget = *targetIndexInWorking;
}
else {
UnsignedVector::const_iterator targetInNotFoundObjectsIt =
std::find(notFoundObjectIndices.begin(),
notFoundObjectIndices.end(),
*targetIndexInWorking);
OS_ASSERT(targetInNotFoundObjectsIt != notFoundObjectIndices.end());
newObjectsIndexOfTarget = nAdd +
static_cast<unsigned>(targetInNotFoundObjectsIt - notFoundObjectIndices.begin());
}
for (unsigned index : fieldIndicesObjectToTarget) {
pointersFromWorkspace.push_back(HUPointer(equivIt->handle(),index,newObjectsIndexOfTarget));
}
}
else {
// target has equivalent in this Workspace. make sure pointer is there or add it.
unsigned targetIndexInEquivalentObjects = static_cast<unsigned>(
targetInFoundObjectsIt - foundIndicesBegin);
WorkspaceObject equivalentTarget = equivalentObjects[targetIndexInEquivalentObjects];
UnsignedVector fieldIndicesFoundObjectToFoundTarget = equivIt->getSourceIndices(equivalentTarget.handle());
for (unsigned index : fieldIndicesObjectToTarget) {
if (std::find(fieldIndicesFoundObjectToFoundTarget.begin(),
fieldIndicesFoundObjectToFoundTarget.end(),
index) == fieldIndicesFoundObjectToFoundTarget.end())
{
OS_ASSERT(!(equivIt->getTarget(index)));
bool ok = equivIt->setPointer(index,equivalentTarget.handle());
if (ok) {
pointersToRollBackOnFail.push_back(HHPointer(equivIt->handle(),index,equivalentTarget.handle()));
}
else {
LOG(Warn,"Unable to move pointer from " << object.briefDescription() << " to "
<< target.briefDescription() << " from field " << index
<< " over to equivalent objects " << equivIt->briefDescription() << " and "
<< equivalentTarget.briefDescription() << ", respectively, already in Workspace.");
}
}
}
} // if (target does not have an equivalent) else
} // for each target of an object that has an equivalent
++equivIt;
} // if (object does not have an equivalent) else
++i;
} // for each object
// add objects
WorkspaceObjectVector addedObjects = addObjects(newObjects,
UHPointerVector(),
pointersFromWorkspace);
if (addedObjects.size() != newObjects.size()) {
for (const HHPointer& ptr : pointersToRollBackOnFail) {
OptionalWorkspaceObject oSource = getObject(ptr.source);
OS_ASSERT(oSource);
oSource->setPointer(ptr.fieldIndex,Handle());
}
return WorkspaceObjectVector();
}
// successful, make result vector
WorkspaceObjectVector result;
WorkspaceObjectVector::const_iterator addedIt = addedObjects.begin();
equivIt = equivalentObjects.begin();
OS_ASSERT(addedObjects.size() + equivalentObjects.size() == n);
for (unsigned i = 0; i < n; ++i) {
if (std::find(foundIndicesBegin,foundIndicesEnd,i) == foundIndicesEnd) {
// no equivalent--added
OS_ASSERT(addedIt != addedObjects.end());
result.push_back(*addedIt);
++addedIt;
}
else {
// equivalent
OS_ASSERT(equivIt != equivalentObjects.end());
result.push_back(*equivIt);
++equivIt;
}
}
OS_ASSERT(addedIt == addedObjects.end());
OS_ASSERT(equivIt == equivalentObjects.end());
return result;
}
std::vector<WorkspaceObject> Workspace_Impl::addObjects(const std::vector<WorkspaceObject>& objects, bool checkNames)
{
WorkspaceObjectVector result;
WorkspaceObject_ImplPtrVector newObjects;
HandleMap oldNewHandleMap;
UHPointerVector pointersIntoWorkspace;
if (objects.empty()) {
return result;
}
if (numObjects() > 0) {
// handle potential name conflicts
Workspace working = this->cloneSubset(HandleVector()); // empty clone
working.order().setDirectOrder(HandleVector()); // maintain order in vector
WorkspaceObjectVector wsObjects = working.addObjects(objects, false); // recursive call
if (wsObjects.size() != objects.size()) {
LOG(Error,"Unable to add objects to their own Workspace as an intermediate step.");
return result;
}
bool changes = resolvePotentialNameConflicts(working);
// create new objects, and keep track of outward pointers if
// objects[0].workspace() == workspace()
bool sameWorkspace = false;
if (objects[0].workspace() == this->workspace()) {