##// END OF EJS Templates
Defines VariableState struct (2)...
Alexandre Leroux -
r1188:ea075ddc1542
parent child
Show More
@@ -1,178 +1,183
1 #include "FuzzingOperations.h"
1 #include "FuzzingOperations.h"
2 #include "FuzzingUtils.h"
2 #include "FuzzingUtils.h"
3
3
4 #include <Data/IDataProvider.h>
4 #include <Data/IDataProvider.h>
5
5
6 #include <Variable/Variable.h>
6 #include <Variable/Variable.h>
7 #include <Variable/VariableController.h>
7 #include <Variable/VariableController.h>
8
8
9 #include <QUuid>
9 #include <QUuid>
10
10
11 #include <functional>
11 #include <functional>
12
12
13 Q_LOGGING_CATEGORY(LOG_FuzzingOperations, "FuzzingOperations")
13 Q_LOGGING_CATEGORY(LOG_FuzzingOperations, "FuzzingOperations")
14
14
15 namespace {
15 namespace {
16
16
17 struct CreateOperation : public IFuzzingOperation {
17 struct CreateOperation : public IFuzzingOperation {
18 bool canExecute(std::shared_ptr<Variable> variable) const override
18 bool canExecute(const VariableState &variableState) const override
19 {
19 {
20 // A variable can be created only if it doesn't exist yet
20 // A variable can be created only if it doesn't exist yet
21 return variable == nullptr;
21 return variableState.m_Variable == nullptr;
22 }
22 }
23
23
24 void execute(std::shared_ptr<Variable> &variable, VariableController &variableController,
24 void execute(VariableState &variableState, VariableController &variableController,
25 const Properties &properties) const override
25 const Properties &properties) const override
26 {
26 {
27 // Retrieves metadata pool from properties, and choose one of the metadata entries to
27 // Retrieves metadata pool from properties, and choose one of the metadata entries to
28 // associate it with the variable
28 // associate it with the variable
29 auto metaDataPool = properties.value(METADATA_POOL_PROPERTY).value<MetadataPool>();
29 auto metaDataPool = properties.value(METADATA_POOL_PROPERTY).value<MetadataPool>();
30 auto variableMetadata = RandomGenerator::instance().randomChoice(metaDataPool);
30 auto variableMetadata = RandomGenerator::instance().randomChoice(metaDataPool);
31
31
32 // Retrieves provider
32 // Retrieves provider
33 auto variableProvider
33 auto variableProvider
34 = properties.value(PROVIDER_PROPERTY).value<std::shared_ptr<IDataProvider> >();
34 = properties.value(PROVIDER_PROPERTY).value<std::shared_ptr<IDataProvider> >();
35
35
36 auto variableName = QString{"Var_%1"}.arg(QUuid::createUuid().toString());
36 auto variableName = QString{"Var_%1"}.arg(QUuid::createUuid().toString());
37 qCInfo(LOG_FuzzingOperations())
37 qCInfo(LOG_FuzzingOperations())
38 << "Creating variable" << variableName << "(metadata:" << variableMetadata << ")";
38 << "Creating variable" << variableName << "(metadata:" << variableMetadata << ")";
39
39
40 auto newVariable
40 auto newVariable
41 = variableController.createVariable(variableName, variableMetadata, variableProvider);
41 = variableController.createVariable(variableName, variableMetadata, variableProvider);
42 std::swap(variable, newVariable);
42 std::swap(variableState.m_Variable, newVariable);
43 }
43 }
44 };
44 };
45
45
46 /**
46 /**
47 * Defines a move operation through a range.
47 * Defines a move operation through a range.
48 *
48 *
49 * A move operation is determined by three functions:
49 * A move operation is determined by three functions:
50 * - Two 'move' functions, used to indicate in which direction the beginning and the end of a range
50 * - Two 'move' functions, used to indicate in which direction the beginning and the end of a range
51 * are going during the operation. These functions will be:
51 * are going during the operation. These functions will be:
52 * -- {<- / <-} for pan left
52 * -- {<- / <-} for pan left
53 * -- {-> / ->} for pan right
53 * -- {-> / ->} for pan right
54 * -- {-> / <-} for zoom in
54 * -- {-> / <-} for zoom in
55 * -- {<- / ->} for zoom out
55 * -- {<- / ->} for zoom out
56 * - One 'max move' functions, used to compute the max delta at which the operation can move a
56 * - One 'max move' functions, used to compute the max delta at which the operation can move a
57 * range, according to a max range. For exemple, for a range of {1, 5} and a max range of {0, 10},
57 * range, according to a max range. For exemple, for a range of {1, 5} and a max range of {0, 10},
58 * max deltas will be:
58 * max deltas will be:
59 * -- {0, 4} for pan left
59 * -- {0, 4} for pan left
60 * -- {6, 10} for pan right
60 * -- {6, 10} for pan right
61 * -- {3, 3} for zoom in
61 * -- {3, 3} for zoom in
62 * -- {0, 6} for zoom out (same spacing left and right)
62 * -- {0, 6} for zoom out (same spacing left and right)
63 */
63 */
64 struct MoveOperation : public IFuzzingOperation {
64 struct MoveOperation : public IFuzzingOperation {
65 using MoveFunction = std::function<double(double currentValue, double maxValue)>;
65 using MoveFunction = std::function<double(double currentValue, double maxValue)>;
66 using MaxMoveFunction = std::function<double(const SqpRange &range, const SqpRange &maxRange)>;
66 using MaxMoveFunction = std::function<double(const SqpRange &range, const SqpRange &maxRange)>;
67
67
68 explicit MoveOperation(MoveFunction rangeStartMoveFun, MoveFunction rangeEndMoveFun,
68 explicit MoveOperation(MoveFunction rangeStartMoveFun, MoveFunction rangeEndMoveFun,
69 MaxMoveFunction maxMoveFun,
69 MaxMoveFunction maxMoveFun,
70 const QString &label = QStringLiteral("Move operation"))
70 const QString &label = QStringLiteral("Move operation"))
71 : m_RangeStartMoveFun{std::move(rangeStartMoveFun)},
71 : m_RangeStartMoveFun{std::move(rangeStartMoveFun)},
72 m_RangeEndMoveFun{std::move(rangeEndMoveFun)},
72 m_RangeEndMoveFun{std::move(rangeEndMoveFun)},
73 m_MaxMoveFun{std::move(maxMoveFun)},
73 m_MaxMoveFun{std::move(maxMoveFun)},
74 m_Label{label}
74 m_Label{label}
75 {
75 {
76 }
76 }
77
77
78 bool canExecute(std::shared_ptr<Variable> variable) const override
78 bool canExecute(const VariableState &variableState) const override
79 {
79 {
80 return variable != nullptr;
80 return variableState.m_Variable != nullptr;
81 }
81 }
82
82
83 void execute(std::shared_ptr<Variable> &variable, VariableController &variableController,
83 void execute(VariableState &variableState, VariableController &variableController,
84 const Properties &properties) const override
84 const Properties &properties) const override
85 {
85 {
86 auto variable = variableState.m_Variable;
87
86 // Gets the max range defined
88 // Gets the max range defined
87 auto maxRange = properties.value(MAX_RANGE_PROPERTY, QVariant::fromValue(INVALID_RANGE))
89 auto maxRange = properties.value(MAX_RANGE_PROPERTY, QVariant::fromValue(INVALID_RANGE))
88 .value<SqpRange>();
90 .value<SqpRange>();
89 auto variableRange = variable->range();
91 auto variableRange = variable->range();
90
92
91 if (maxRange == INVALID_RANGE || variableRange.m_TStart < maxRange.m_TStart
93 if (maxRange == INVALID_RANGE || variableRange.m_TStart < maxRange.m_TStart
92 || variableRange.m_TEnd > maxRange.m_TEnd) {
94 || variableRange.m_TEnd > maxRange.m_TEnd) {
93 qCWarning(LOG_FuzzingOperations()) << "Can't execute operation: invalid max range";
95 qCWarning(LOG_FuzzingOperations()) << "Can't execute operation: invalid max range";
94 return;
96 return;
95 }
97 }
96
98
97 // Computes the max delta at which the variable can move, up to the limits of the max range
99 // Computes the max delta at which the variable can move, up to the limits of the max range
98 auto deltaMax = m_MaxMoveFun(variable->range(), maxRange);
100 auto deltaMax = m_MaxMoveFun(variable->range(), maxRange);
99
101
100 // Generates random delta that will be used to move variable
102 // Generates random delta that will be used to move variable
101 auto delta = RandomGenerator::instance().generateDouble(0, deltaMax);
103 auto delta = RandomGenerator::instance().generateDouble(0, deltaMax);
102
104
103 // Moves variable to its new range
105 // Moves variable to its new range
104 auto newVariableRange = SqpRange{m_RangeStartMoveFun(variableRange.m_TStart, delta),
106 auto newVariableRange = SqpRange{m_RangeStartMoveFun(variableRange.m_TStart, delta),
105 m_RangeEndMoveFun(variableRange.m_TEnd, delta)};
107 m_RangeEndMoveFun(variableRange.m_TEnd, delta)};
106 qCInfo(LOG_FuzzingOperations())
108 qCInfo(LOG_FuzzingOperations())
107 << "Performing" << m_Label << "on" << variable->name() << "(from" << variableRange
109 << "Performing" << m_Label << "on" << variable->name() << "(from" << variableRange
108 << "to" << newVariableRange << ")...";
110 << "to" << newVariableRange << ")...";
109 variableController.onRequestDataLoading({variable}, newVariableRange, false);
111 variableController.onRequestDataLoading({variable}, newVariableRange, false);
112
113 // Updates variable's state
114 variableState.m_Range = newVariableRange;
110 }
115 }
111
116
112 MoveFunction m_RangeStartMoveFun;
117 MoveFunction m_RangeStartMoveFun;
113 MoveFunction m_RangeEndMoveFun;
118 MoveFunction m_RangeEndMoveFun;
114 MaxMoveFunction m_MaxMoveFun;
119 MaxMoveFunction m_MaxMoveFun;
115 QString m_Label;
120 QString m_Label;
116 };
121 };
117
122
118 struct UnknownOperation : public IFuzzingOperation {
123 struct UnknownOperation : public IFuzzingOperation {
119 bool canExecute(std::shared_ptr<Variable> variable) const override
124 bool canExecute(const VariableState &variableState) const override
120 {
125 {
121 Q_UNUSED(variable);
126 Q_UNUSED(variableState);
122 return false;
127 return false;
123 }
128 }
124
129
125 void execute(std::shared_ptr<Variable> &variable, VariableController &variableController,
130 void execute(VariableState &variableState, VariableController &variableController,
126 const Properties &properties) const override
131 const Properties &properties) const override
127 {
132 {
128 Q_UNUSED(variable);
133 Q_UNUSED(variableState);
129 Q_UNUSED(variableController);
134 Q_UNUSED(variableController);
130 Q_UNUSED(properties);
135 Q_UNUSED(properties);
131 // Does nothing
136 // Does nothing
132 }
137 }
133 };
138 };
134
139
135 } // namespace
140 } // namespace
136
141
137 std::unique_ptr<IFuzzingOperation> FuzzingOperationFactory::create(FuzzingOperationType type)
142 std::unique_ptr<IFuzzingOperation> FuzzingOperationFactory::create(FuzzingOperationType type)
138 {
143 {
139 switch (type) {
144 switch (type) {
140 case FuzzingOperationType::CREATE:
145 case FuzzingOperationType::CREATE:
141 return std::make_unique<CreateOperation>();
146 return std::make_unique<CreateOperation>();
142 case FuzzingOperationType::PAN_LEFT:
147 case FuzzingOperationType::PAN_LEFT:
143 return std::make_unique<MoveOperation>(
148 return std::make_unique<MoveOperation>(
144 std::minus<double>(), std::minus<double>(),
149 std::minus<double>(), std::minus<double>(),
145 [](const SqpRange &range, const SqpRange &maxRange) {
150 [](const SqpRange &range, const SqpRange &maxRange) {
146 return range.m_TStart - maxRange.m_TStart;
151 return range.m_TStart - maxRange.m_TStart;
147 },
152 },
148 QStringLiteral("Pan left operation"));
153 QStringLiteral("Pan left operation"));
149 case FuzzingOperationType::PAN_RIGHT:
154 case FuzzingOperationType::PAN_RIGHT:
150 return std::make_unique<MoveOperation>(
155 return std::make_unique<MoveOperation>(
151 std::plus<double>(), std::plus<double>(),
156 std::plus<double>(), std::plus<double>(),
152 [](const SqpRange &range, const SqpRange &maxRange) {
157 [](const SqpRange &range, const SqpRange &maxRange) {
153 return maxRange.m_TEnd - range.m_TEnd;
158 return maxRange.m_TEnd - range.m_TEnd;
154 },
159 },
155 QStringLiteral("Pan right operation"));
160 QStringLiteral("Pan right operation"));
156 case FuzzingOperationType::ZOOM_IN:
161 case FuzzingOperationType::ZOOM_IN:
157 return std::make_unique<MoveOperation>(
162 return std::make_unique<MoveOperation>(
158 std::plus<double>(), std::minus<double>(),
163 std::plus<double>(), std::minus<double>(),
159 [](const SqpRange &range, const SqpRange &maxRange) {
164 [](const SqpRange &range, const SqpRange &maxRange) {
160 Q_UNUSED(maxRange)
165 Q_UNUSED(maxRange)
161 return range.m_TEnd - (range.m_TStart + range.m_TEnd) / 2.;
166 return range.m_TEnd - (range.m_TStart + range.m_TEnd) / 2.;
162 },
167 },
163 QStringLiteral("Zoom in operation"));
168 QStringLiteral("Zoom in operation"));
164 case FuzzingOperationType::ZOOM_OUT:
169 case FuzzingOperationType::ZOOM_OUT:
165 return std::make_unique<MoveOperation>(
170 return std::make_unique<MoveOperation>(
166 std::minus<double>(), std::plus<double>(),
171 std::minus<double>(), std::plus<double>(),
167 [](const SqpRange &range, const SqpRange &maxRange) {
172 [](const SqpRange &range, const SqpRange &maxRange) {
168 return std::min(range.m_TStart - maxRange.m_TStart,
173 return std::min(range.m_TStart - maxRange.m_TStart,
169 maxRange.m_TEnd - range.m_TEnd);
174 maxRange.m_TEnd - range.m_TEnd);
170 },
175 },
171 QStringLiteral("Zoom out operation"));
176 QStringLiteral("Zoom out operation"));
172 default:
177 default:
173 // Default case returns unknown operation
178 // Default case returns unknown operation
174 break;
179 break;
175 }
180 }
176
181
177 return std::make_unique<UnknownOperation>();
182 return std::make_unique<UnknownOperation>();
178 }
183 }
@@ -1,49 +1,48
1 #ifndef SCIQLOP_FUZZINGOPERATIONS_H
1 #ifndef SCIQLOP_FUZZINGOPERATIONS_H
2 #define SCIQLOP_FUZZINGOPERATIONS_H
2 #define SCIQLOP_FUZZINGOPERATIONS_H
3
3
4 #include "FuzzingDefs.h"
4 #include "FuzzingDefs.h"
5
5
6 #include <memory>
6 #include <memory>
7 #include <set>
7 #include <set>
8
8
9 #include <QLoggingCategory>
9 #include <QLoggingCategory>
10 #include <QMetaType>
10 #include <QMetaType>
11
11
12 Q_DECLARE_LOGGING_CATEGORY(LOG_FuzzingOperations)
12 Q_DECLARE_LOGGING_CATEGORY(LOG_FuzzingOperations)
13
13
14 class Variable;
15 class VariableController;
14 class VariableController;
16
15
17 /**
16 /**
18 * Enumeration of types of existing fuzzing operations
17 * Enumeration of types of existing fuzzing operations
19 */
18 */
20 enum class FuzzingOperationType { CREATE, PAN_LEFT, PAN_RIGHT, ZOOM_IN, ZOOM_OUT };
19 enum class FuzzingOperationType { CREATE, PAN_LEFT, PAN_RIGHT, ZOOM_IN, ZOOM_OUT };
21
20
22 /// Interface that represents an operation that can be executed during a fuzzing test
21 /// Interface that represents an operation that can be executed during a fuzzing test
23 struct IFuzzingOperation {
22 struct IFuzzingOperation {
24 virtual ~IFuzzingOperation() noexcept = default;
23 virtual ~IFuzzingOperation() noexcept = default;
25
24
26 /// Checks if the operation can be executed according to the current state of the variable
25 /// Checks if the operation can be executed according to the current variable state passed in
27 /// passed in parameter
26 /// parameter
28 virtual bool canExecute(std::shared_ptr<Variable> variable) const = 0;
27 virtual bool canExecute(const VariableState &variableState) const = 0;
29 /// Executes the operation on the variable passed in parameter
28 /// Executes the operation on the variable state passed in parameter
30 /// @param variable the variable on which to execute the operation
29 /// @param variableState the variable state on which to execute the operation
31 /// @param variableController the controller associated to the operation
30 /// @param variableController the controller associated to the operation
32 /// @param properties properties that can be used to configure the operation
31 /// @param properties properties that can be used to configure the operation
33 /// @remarks variable is passed as a reference because, according to the operation, it can be
32 /// @remarks variableState is passed as a reference because, according to the operation, it can
33 /// be
34 /// modified (in/out parameter)
34 /// modified (in/out parameter)
35 virtual void execute(std::shared_ptr<Variable> &variable,
35 virtual void execute(VariableState &variableState, VariableController &variableController,
36 VariableController &variableController,
37 const Properties &properties = {}) const = 0;
36 const Properties &properties = {}) const = 0;
38 };
37 };
39
38
40 /// Factory of @sa IFuzzingOperation
39 /// Factory of @sa IFuzzingOperation
41 struct FuzzingOperationFactory {
40 struct FuzzingOperationFactory {
42 /// Creates a fuzzing operation from a type
41 /// Creates a fuzzing operation from a type
43 static std::unique_ptr<IFuzzingOperation> create(FuzzingOperationType type);
42 static std::unique_ptr<IFuzzingOperation> create(FuzzingOperationType type);
44 };
43 };
45
44
46 using WeightedOperationsTypes = std::map<FuzzingOperationType, double>;
45 using WeightedOperationsTypes = std::map<FuzzingOperationType, double>;
47 Q_DECLARE_METATYPE(WeightedOperationsTypes)
46 Q_DECLARE_METATYPE(WeightedOperationsTypes)
48
47
49 #endif // SCIQLOP_FUZZINGOPERATIONS_H
48 #endif // SCIQLOP_FUZZINGOPERATIONS_H
General Comments 0
You need to be logged in to leave comments. Login now