##// END OF EJS Templates
Implements canExecute() method of syn/desync operations
Alexandre Leroux -
r1239:4bcf5995377c
parent child
Show More
@@ -1,38 +1,50
1 1 #include "FuzzingDefs.h"
2 2
3 3 const QString NB_MAX_OPERATIONS_PROPERTY = QStringLiteral("component");
4 4 const QString NB_MAX_SYNC_GROUPS_PROPERTY = QStringLiteral("nbSyncGroups");
5 5 const QString NB_MAX_VARIABLES_PROPERTY = QStringLiteral("nbMaxVariables");
6 6 const QString AVAILABLE_OPERATIONS_PROPERTY = QStringLiteral("availableOperations");
7 7 const QString CACHE_TOLERANCE_PROPERTY = QStringLiteral("cacheTolerance");
8 8 const QString INITIAL_RANGE_PROPERTY = QStringLiteral("initialRange");
9 9 const QString MAX_RANGE_PROPERTY = QStringLiteral("maxRange");
10 10 const QString METADATA_POOL_PROPERTY = QStringLiteral("metadataPool");
11 11 const QString PROVIDER_PROPERTY = QStringLiteral("provider");
12 12 const QString OPERATION_DELAY_PROPERTY = QStringLiteral("operationDelay");
13 13 const QString VALIDATORS_PROPERTY = QStringLiteral("validators");
14 14
15 15 // //////////// //
16 16 // FuzzingState //
17 17 // //////////// //
18 18
19 19 const SyncGroup &FuzzingState::syncGroup(SyncGroupId id) const
20 20 {
21 21 return m_SyncGroupsPool.at(id);
22 22 }
23 23
24 24 SyncGroup &FuzzingState::syncGroup(SyncGroupId id)
25 25 {
26 26 return m_SyncGroupsPool.at(id);
27 27 }
28 28
29 29 const VariableState &FuzzingState::variableState(VariableId id) const
30 30 {
31 31 return m_VariablesPool.at(id);
32 32 }
33 33
34 34 VariableState &FuzzingState::variableState(VariableId id)
35 35 {
36 36 return m_VariablesPool.at(id);
37 37 }
38 38
39 SyncGroupId FuzzingState::syncGroupId(VariableId variableId) const
40 {
41 auto end = m_SyncGroupsPool.cend();
42 auto it
43 = std::find_if(m_SyncGroupsPool.cbegin(), end, [&variableId](const auto &syncGroupEntry) {
44 const auto &syncGroup = syncGroupEntry.second;
45 return syncGroup.m_Variables.find(variableId) != syncGroup.m_Variables.end();
46 });
47
48 return it != end ? it->first : SyncGroupId{};
49 }
50
@@ -1,101 +1,105
1 1 #ifndef SCIQLOP_FUZZINGDEFS_H
2 2 #define SCIQLOP_FUZZINGDEFS_H
3 3
4 4 #include <Data/SqpRange.h>
5 5
6 6 #include <QString>
7 7 #include <QUuid>
8 8 #include <QVariantHash>
9 9
10 10 #include <memory>
11 11 #include <set>
12 12
13 13 // /////// //
14 14 // Aliases //
15 15 // /////// //
16 16
17 17 using MetadataPool = std::vector<QVariantHash>;
18 18 Q_DECLARE_METATYPE(MetadataPool)
19 19
20 20 using Properties = QVariantHash;
21 21
22 22 // ///////// //
23 23 // Constants //
24 24 // ///////// //
25 25
26 26 /// Max number of operations to generate
27 27 extern const QString NB_MAX_OPERATIONS_PROPERTY;
28 28
29 29 /// Max number of sync groups to create through operations
30 30 extern const QString NB_MAX_SYNC_GROUPS_PROPERTY;
31 31
32 32 /// Max number of variables to manipulate through operations
33 33 extern const QString NB_MAX_VARIABLES_PROPERTY;
34 34
35 35 /// Set of operations available for the test
36 36 extern const QString AVAILABLE_OPERATIONS_PROPERTY;
37 37
38 38 /// Tolerance used for variable's cache (in ratio)
39 39 extern const QString CACHE_TOLERANCE_PROPERTY;
40 40
41 41 /// Range with which the timecontroller is initialized
42 42 extern const QString INITIAL_RANGE_PROPERTY;
43 43
44 44 /// Max range that an operation can reach
45 45 extern const QString MAX_RANGE_PROPERTY;
46 46
47 47 /// Set of metadata that can be associated to a variable
48 48 extern const QString METADATA_POOL_PROPERTY;
49 49
50 50 /// Provider used to retrieve data
51 51 extern const QString PROVIDER_PROPERTY;
52 52
53 53 /// Time left for an operation to execute
54 54 extern const QString OPERATION_DELAY_PROPERTY;
55 55
56 56 /// Validators used to validate an operation
57 57 extern const QString VALIDATORS_PROPERTY;
58 58
59 59 // /////// //
60 60 // Structs //
61 61 // /////// //
62 62
63 63 class Variable;
64 64 struct VariableState {
65 65 std::shared_ptr<Variable> m_Variable{nullptr};
66 66 SqpRange m_Range{INVALID_RANGE};
67 67 };
68 68
69 69 using VariableId = int;
70 70 using VariablesPool = std::map<VariableId, VariableState>;
71 71
72 72 /**
73 73 * Defines a synchronization group for a fuzzing state. A group reports the variables synchronized
74 74 * with each other, and the current range of the group (i.e. range of the last synchronized variable
75 75 * that has been moved)
76 76 */
77 77 struct SyncGroup {
78 78 std::set<VariableId> m_Variables{};
79 79 SqpRange m_Range{INVALID_RANGE};
80 80 };
81 81
82 82 using SyncGroupId = QUuid;
83 83 using SyncGroupsPool = std::map<SyncGroupId, SyncGroup>;
84 84
85 85 /**
86 86 * Defines a current state during a fuzzing state. It contains all the variables manipulated during
87 87 * the test, as well as the synchronization status of these variables.
88 88 */
89 89 struct FuzzingState {
90 90 const SyncGroup &syncGroup(SyncGroupId id) const;
91 91 SyncGroup &syncGroup(SyncGroupId id);
92 92
93 93 const VariableState &variableState(VariableId id) const;
94 94 VariableState &variableState(VariableId id);
95 95
96 /// @return the identifier of the synchronization group in which the variable passed in
97 /// parameter is located. If the variable is not in any group, returns an invalid identifier
98 SyncGroupId syncGroupId(VariableId variableId) const;
99
96 100
97 101 VariablesPool m_VariablesPool;
98 102 SyncGroupsPool m_SyncGroupsPool;
99 103 };
100 104
101 105 #endif // SCIQLOP_FUZZINGDEFS_H
@@ -1,229 +1,234
1 1 #include "FuzzingOperations.h"
2 2 #include "FuzzingUtils.h"
3 3
4 4 #include <Data/IDataProvider.h>
5 5
6 6 #include <Variable/Variable.h>
7 7 #include <Variable/VariableController.h>
8 8
9 9 #include <QUuid>
10 10
11 11 #include <functional>
12 12
13 13 Q_LOGGING_CATEGORY(LOG_FuzzingOperations, "FuzzingOperations")
14 14
15 15 namespace {
16 16
17 17 struct CreateOperation : public IFuzzingOperation {
18 18 bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override
19 19 {
20 20 // A variable can be created only if it doesn't exist yet
21 21 return fuzzingState.variableState(variableId).m_Variable == nullptr;
22 22 }
23 23
24 24 void execute(VariableId variableId, FuzzingState &fuzzingState,
25 25 VariableController &variableController,
26 26 const Properties &properties) const override
27 27 {
28 28 // Retrieves metadata pool from properties, and choose one of the metadata entries to
29 29 // associate it with the variable
30 30 auto metaDataPool = properties.value(METADATA_POOL_PROPERTY).value<MetadataPool>();
31 31 auto variableMetadata = RandomGenerator::instance().randomChoice(metaDataPool);
32 32
33 33 // Retrieves provider
34 34 auto variableProvider
35 35 = properties.value(PROVIDER_PROPERTY).value<std::shared_ptr<IDataProvider> >();
36 36
37 37 auto variableName = QString{"Var_%1"}.arg(QUuid::createUuid().toString());
38 38 qCInfo(LOG_FuzzingOperations()).noquote()
39 39 << "Creating variable" << variableName << "(metadata:" << variableMetadata << ")...";
40 40
41 41 auto newVariable
42 42 = variableController.createVariable(variableName, variableMetadata, variableProvider);
43 43
44 44 // Updates variable's state
45 45 auto &variableState = fuzzingState.variableState(variableId);
46 46 variableState.m_Range = properties.value(INITIAL_RANGE_PROPERTY).value<SqpRange>();
47 47 std::swap(variableState.m_Variable, newVariable);
48 48 }
49 49 };
50 50
51 51 struct DeleteOperation : public IFuzzingOperation {
52 52 bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override
53 53 {
54 54 // A variable can be delete only if it exists
55 55 return fuzzingState.variableState(variableId).m_Variable != nullptr;
56 56 }
57 57
58 58 void execute(VariableId variableId, FuzzingState &fuzzingState,
59 59 VariableController &variableController, const Properties &) const override
60 60 {
61 61 auto &variableState = fuzzingState.variableState(variableId);
62 62
63 63 qCInfo(LOG_FuzzingOperations()).noquote()
64 64 << "Deleting variable" << variableState.m_Variable->name() << "...";
65 65 variableController.deleteVariable(variableState.m_Variable);
66 66
67 67 // Updates variable's state
68 68 variableState.m_Range = INVALID_RANGE;
69 69 variableState.m_Variable = nullptr;
70 70 }
71 71 };
72 72
73 73 /**
74 74 * Defines a move operation through a range.
75 75 *
76 76 * A move operation is determined by three functions:
77 77 * - Two 'move' functions, used to indicate in which direction the beginning and the end of a range
78 78 * are going during the operation. These functions will be:
79 79 * -- {<- / <-} for pan left
80 80 * -- {-> / ->} for pan right
81 81 * -- {-> / <-} for zoom in
82 82 * -- {<- / ->} for zoom out
83 83 * - One 'max move' functions, used to compute the max delta at which the operation can move a
84 84 * range, according to a max range. For exemple, for a range of {1, 5} and a max range of {0, 10},
85 85 * max deltas will be:
86 86 * -- {0, 4} for pan left
87 87 * -- {6, 10} for pan right
88 88 * -- {3, 3} for zoom in
89 89 * -- {0, 6} for zoom out (same spacing left and right)
90 90 */
91 91 struct MoveOperation : public IFuzzingOperation {
92 92 using MoveFunction = std::function<double(double currentValue, double maxValue)>;
93 93 using MaxMoveFunction = std::function<double(const SqpRange &range, const SqpRange &maxRange)>;
94 94
95 95 explicit MoveOperation(MoveFunction rangeStartMoveFun, MoveFunction rangeEndMoveFun,
96 96 MaxMoveFunction maxMoveFun,
97 97 const QString &label = QStringLiteral("Move operation"))
98 98 : m_RangeStartMoveFun{std::move(rangeStartMoveFun)},
99 99 m_RangeEndMoveFun{std::move(rangeEndMoveFun)},
100 100 m_MaxMoveFun{std::move(maxMoveFun)},
101 101 m_Label{label}
102 102 {
103 103 }
104 104
105 105 bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override
106 106 {
107 107 return fuzzingState.variableState(variableId).m_Variable != nullptr;
108 108 }
109 109
110 110 void execute(VariableId variableId, FuzzingState &fuzzingState,
111 111 VariableController &variableController,
112 112 const Properties &properties) const override
113 113 {
114 114 auto &variableState = fuzzingState.variableState(variableId);
115 115 auto variable = variableState.m_Variable;
116 116
117 117 // Gets the max range defined
118 118 auto maxRange = properties.value(MAX_RANGE_PROPERTY, QVariant::fromValue(INVALID_RANGE))
119 119 .value<SqpRange>();
120 120 auto variableRange = variable->range();
121 121
122 122 if (maxRange == INVALID_RANGE || variableRange.m_TStart < maxRange.m_TStart
123 123 || variableRange.m_TEnd > maxRange.m_TEnd) {
124 124 qCWarning(LOG_FuzzingOperations()) << "Can't execute operation: invalid max range";
125 125 return;
126 126 }
127 127
128 128 // Computes the max delta at which the variable can move, up to the limits of the max range
129 129 auto deltaMax = m_MaxMoveFun(variable->range(), maxRange);
130 130
131 131 // Generates random delta that will be used to move variable
132 132 auto delta = RandomGenerator::instance().generateDouble(0, deltaMax);
133 133
134 134 // Moves variable to its new range
135 135 auto newVariableRange = SqpRange{m_RangeStartMoveFun(variableRange.m_TStart, delta),
136 136 m_RangeEndMoveFun(variableRange.m_TEnd, delta)};
137 137 qCInfo(LOG_FuzzingOperations()).noquote()
138 138 << "Performing" << m_Label << "on" << variable->name() << "(from" << variableRange
139 139 << "to" << newVariableRange << ")...";
140 140 variableController.onRequestDataLoading({variable}, newVariableRange, false);
141 141
142 142 // Updates variable's state
143 143 variableState.m_Range = newVariableRange;
144 144 }
145 145
146 146 MoveFunction m_RangeStartMoveFun;
147 147 MoveFunction m_RangeEndMoveFun;
148 148 MaxMoveFunction m_MaxMoveFun;
149 149 QString m_Label;
150 150 };
151 151
152 152 struct SynchronizeOperation : public IFuzzingOperation {
153 153 bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override
154 154 {
155 auto variable = fuzzingState.variableState(variableId).m_Variable;
156 return variable != nullptr && !fuzzingState.m_SyncGroupsPool.empty()
157 && fuzzingState.syncGroupId(variableId).isNull();
155 158 }
156 159
157 160 void execute(VariableId variableId, FuzzingState &fuzzingState,
158 161 VariableController &variableController, const Properties &) const override
159 162 {
160 163 }
161 164 };
162 165
163 166 struct DesynchronizeOperation : public IFuzzingOperation {
164 167 bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override
165 168 {
169 auto variable = fuzzingState.variableState(variableId).m_Variable;
170 return variable != nullptr && !fuzzingState.syncGroupId(variableId).isNull();
166 171 }
167 172
168 173 void execute(VariableId variableId, FuzzingState &fuzzingState,
169 174 VariableController &variableController, const Properties &) const override
170 175 {
171 176 struct UnknownOperation : public IFuzzingOperation {
172 177 bool canExecute(VariableId, const FuzzingState &) const override { return false; }
173 178
174 179 void execute(VariableId, FuzzingState &, VariableController &,
175 180 const Properties &) const override
176 181 {
177 182 }
178 183 };
179 184
180 185 } // namespace
181 186
182 187 std::unique_ptr<IFuzzingOperation> FuzzingOperationFactory::create(FuzzingOperationType type)
183 188 {
184 189 switch (type) {
185 190 case FuzzingOperationType::CREATE:
186 191 return std::make_unique<CreateOperation>();
187 192 case FuzzingOperationType::DELETE:
188 193 return std::make_unique<DeleteOperation>();
189 194 case FuzzingOperationType::PAN_LEFT:
190 195 return std::make_unique<MoveOperation>(
191 196 std::minus<double>(), std::minus<double>(),
192 197 [](const SqpRange &range, const SqpRange &maxRange) {
193 198 return range.m_TStart - maxRange.m_TStart;
194 199 },
195 200 QStringLiteral("Pan left operation"));
196 201 case FuzzingOperationType::PAN_RIGHT:
197 202 return std::make_unique<MoveOperation>(
198 203 std::plus<double>(), std::plus<double>(),
199 204 [](const SqpRange &range, const SqpRange &maxRange) {
200 205 return maxRange.m_TEnd - range.m_TEnd;
201 206 },
202 207 QStringLiteral("Pan right operation"));
203 208 case FuzzingOperationType::ZOOM_IN:
204 209 return std::make_unique<MoveOperation>(
205 210 std::plus<double>(), std::minus<double>(),
206 211 [](const SqpRange &range, const SqpRange &maxRange) {
207 212 Q_UNUSED(maxRange)
208 213 return range.m_TEnd - (range.m_TStart + range.m_TEnd) / 2.;
209 214 },
210 215 QStringLiteral("Zoom in operation"));
211 216 case FuzzingOperationType::ZOOM_OUT:
212 217 return std::make_unique<MoveOperation>(
213 218 std::minus<double>(), std::plus<double>(),
214 219 [](const SqpRange &range, const SqpRange &maxRange) {
215 220 return std::min(range.m_TStart - maxRange.m_TStart,
216 221 maxRange.m_TEnd - range.m_TEnd);
217 222 },
218 223 QStringLiteral("Zoom out operation"));
219 224 case FuzzingOperationType::SYNCHRONIZE:
220 225 return std::make_unique<SynchronizeOperation>();
221 226 case FuzzingOperationType::DESYNCHRONIZE:
222 227 return std::make_unique<DesynchronizeOperation>();
223 228 default:
224 229 // Default case returns unknown operation
225 230 break;
226 231 }
227 232
228 233 return std::make_unique<UnknownOperation>();
229 234 }
General Comments 0
You need to be logged in to leave comments. Login now