/
task.cpp
190 lines (164 loc) · 3.95 KB
/
task.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
#include "generator.h"
#include "deriver.h"
namespace pictcore
{
const size_t DefaultMaxRandomTries = 1000;
//
//
//
Task::Task() :
m_rootModel (NULL),
m_abortCallback (NULL),
m_generationMode(Regular),
m_maxRandomTries(DefaultMaxRandomTries),
m_workbuf (NULL)
{
Combination::ResetId();
#if ( defined(_DOUT) || defined(_FILE) )
wcerr << L"WARNING: _DOUT or _FILE are defined\n";
#endif
#if ( defined(_DOUT) && defined(_FILE) )
logfile.open( "\\generator.log", ofstream::out );
#endif
}
//
//
//
Task::~Task()
{
DeallocWorkbuf();
#if ( defined(_DOUT) && defined(_FILE) )
logfile.close();
#endif
}
//
//
//
void Task::AllocWorkbuf( int size )
{
DeallocWorkbuf();
m_workbuf = new int[ size ];
}
//
//
//
void Task::DeallocWorkbuf()
{
if( NULL != m_workbuf )
{
delete[] m_workbuf;
m_workbuf = NULL;
}
}
//
//
//
void Task::PrepareForGeneration()
{
// new models could have been added to the root
// repropagate the task pointer to all models
m_rootModel->WireTask(this);
// run the deriver on exclusions
deriveExclusions();
// propagate exclusions wiring to appropriate submodel
// add each exclusion to that model in the hierarchy which is the most suitable:
// 1. a subtree of that model must have all the parameters of the exclusion
// 2. no lower subtree satisfies the condition 1)
//
for ( auto & excl : m_exclusions )
{
Model* found = findMatchingNode(const_cast<Exclusion&>( excl ), m_rootModel);
assert( NULL != found );
found->AddExclusion( const_cast<Exclusion&>( excl ));
}
// propagate all row seeds
m_rootModel->AddRowSeeds( m_rowSeeds.begin(), m_rowSeeds.end() );
}
//
//
//
void Task::ResetResultFetching()
{
m_currentResultRow = m_rootModel->GetResults().begin();
}
//
//
//
ResultCollection::iterator Task::GetNextResultRow()
{
ResultCollection::iterator ret = m_currentResultRow;
if( m_currentResultRow != GetResults().end() )
{
++m_currentResultRow;
}
return( ret );
}
//
// for a given exclusion, this routine finds the best fitting model in a hierarchy:
// the one that contains all params of the exclusion in its subtree and
// none of its children have this property
//
Model* Task::findMatchingNode( Exclusion& exclusion, Model* root )
{
// can any of the subnodes accommodate the exclusion?
for( auto & submodel : root->GetSubmodels() )
{
Model* foundNode = findMatchingNode( exclusion, submodel );
if ( NULL != foundNode ) return( foundNode );
}
// none of the subnodes matches the exclusion so let's try the current one
bool foundParams = true;
for( auto & term : exclusion )
{
foundParams = findParamInSubtree( term.first, root );
if ( ! foundParams ) break;
}
if ( foundParams )
{
return( root );
}
else
{
return( NULL );
}
}
//
//
//
bool Task::findParamInSubtree( Parameter* parameter, Model* root )
{
// check if exists in current node collection
for( auto p : root->GetParameters() )
{
if ( p == parameter ) return( true );
}
// check if exists in any of the children
for( auto & submodel : root->GetSubmodels() )
{
bool found = findParamInSubtree( parameter, submodel );
if ( found ) return( true );
}
return( false );
}
//
//
//
void Task::deriveExclusions()
{
ExclusionDeriver deriver(this);
ParamCollection params;
m_rootModel->GetAllParameters(params);
for( auto p : params )
{
deriver.AddParameter( p );
}
for( auto & excl : m_exclusions )
{
deriver.AddExclusion( const_cast<Exclusion&>( excl ), true );
}
deriver.DeriveExclusions();
// must clear and repopulate the collection with the result of the derivation
m_exclusions.clear();
__insert( m_exclusions, deriver.GetExclusions().begin(), deriver.GetExclusions().end() );
}
}