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