aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model/Workflow/Workflow.hpp
blob: f26fff94c52746165bb7f7b080c582dba6a4388e (plain)
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
#pragma once

#include "Model/Workflow/Value.hpp"
#include "Utils/Vector.hpp"
#include "cplt_fwd.hpp"

#include <cstddef>
#include <cstdint>
#include <iosfwd>
#include <limits>
#include <memory>
#include <span>
#include <string>
#include <variant>
#include <vector>

class WorkflowConnection
{
public:
	static constexpr auto kInvalidId = std::numeric_limits<size_t>::max();

	enum Direction
	{
		ManyToOne,
		OneToMany,
	};

	struct ConnectionPoint
	{
		size_t Node;
		int Pin;

		bool operator==(const ConnectionPoint&) const = default;
	};

	std::vector<ConnectionPoint> MultiConnections;
	ConnectionPoint SingleConnection;
	Direction ConnectionDirection;

public:
	WorkflowConnection();

	bool IsValid() const;
	std::span<ConnectionPoint> GetSourcePoints();
	std::span<const ConnectionPoint> GetSourcePoints() const;
	std::span<ConnectionPoint> GetDestinationPoints();
	std::span<const ConnectionPoint> GetDestinationPoints() const;

	void DrawDebugInfo() const;
	void ReadFrom(std::istream& stream);
	void WriteTo(std::ostream& stream);
};

class WorkflowNode
{
public:
	static constexpr auto kInvalidId = std::numeric_limits<size_t>::max();

	enum Type
	{
		InputType,
		TransformType,
		OutputType,
	};

	enum Kind
	{
		KD_NumericAddition,
		KD_NumericSubtraction,
		KD_NumericMultiplication,
		KD_NumericDivision,
		KD_NumericExpression,
		KD_TextFormatting,
		KD_DocumentTemplateExpansion,
		KD_FormInput,
		KD_DatabaseRowsInput,

		InvalidKind,
		KindCount = InvalidKind,
	};

protected:
	struct InputPin
	{
		size_t Connection = WorkflowConnection::kInvalidId;
		BaseValue::Kind MatchingType = BaseValue::InvalidKind;
		bool ConnectionToConst = false;
		bool AllowsMultipleConnections = false;

		/// A constant connection connects from a user-specified constant value, feeding to a valid \c Destination and \c DestinationPin (i.e. input pins).
		bool IsConstantConnection() const;
		bool IsConnected() const;
		BaseValue::Kind GetMatchingType() const;
		WorkflowConnection::Direction GetSupportedDirection() const;
	};

	struct OutputPin
	{
		size_t Connection = WorkflowConnection::kInvalidId;
		BaseValue::Kind MatchingType = BaseValue::InvalidKind;
		bool AllowsMultipleConnections = false;

		bool IsConnected() const;
		BaseValue::Kind GetMatchingType() const;
		WorkflowConnection::Direction GetSupportedDirection() const;
	};

	friend class Workflow;
	friend class WorkflowEvaluationContext;

	Workflow* mWorkflow;
	size_t mId;
	std::vector<InputPin> mInputs;
	std::vector<OutputPin> mOutputs;
	Vec2i mPosition;
	Kind mKind;
	int mDepth;

public:
	static const char* FormatKind(Kind kind);
	static const char* FormatType(Type type);
	static std::unique_ptr<WorkflowNode> CreateByKind(Kind kind);

	WorkflowNode(Kind kind);
	virtual ~WorkflowNode() = default;

	WorkflowNode(const WorkflowNode&) = delete;
	WorkflowNode& operator=(const WorkflowNode&) = delete;
	WorkflowNode(WorkflowNode&&) = default;
	WorkflowNode& operator=(WorkflowNode&&) = default;

	void SetPosition(const Vec2i& position);
	Vec2i GetPosition() const;

	size_t GetId() const;
	Kind GetKind() const;
	int GetDepth() const;

	Type GetType() const;
	bool IsInputNode() const;
	bool IsOutputNode() const;

	void ConnectInput(int nodeId, WorkflowNode& output, int outputNodeId);
	void DisconnectInput(int nodeId);
	bool IsInputConnected(int nodeId) const;

	void ConnectOutput(int nodeId, WorkflowNode& input, int inputNodeId);
	void DisconnectOutput(int nodeId);
	bool IsOutputConnected(int nodeId) const;

	virtual void Evaluate(WorkflowEvaluationContext& ctx) = 0;

	void Draw();
	virtual void DrawExtra() {}

	void DrawDebugInfo() const;
	virtual void DrawExtraDebugInfo() const {}

	virtual void ReadFrom(std::istream& istream);
	virtual void WriteTo(std::ostream& ostream);

protected:
	InputPin& InsertInputPin(int atIdx);
	void RemoveInputPin(int pin);
	void SwapInputPin(int a, int b);
	OutputPin& InsertOutputPin(int atIdx);
	void RemoveOutputPin(int pin);
	void SwapOutputPin(int a, int b);

	/* For \c Workflow to invoke, override by implementations */

	void OnAttach(Workflow& workflow, size_t newId) {}
	void OnDetach() {}
};

class Workflow
{
private:
	friend class WorkflowEvaluationContext;

	std::vector<WorkflowConnection> mConnections;
	std::vector<std::unique_ptr<WorkflowNode>> mNodes;
	std::vector<std::unique_ptr<BaseValue>> mConstants;
	std::vector<std::vector<size_t>> mDepthGroups;
	int mConnectionCount;
	int mNodeCount;
	int mConstantCount;
	bool mDepthsDirty = true;

public:
	/* Graph access */

	const std::vector<WorkflowConnection>& GetConnections() const;
	std::vector<WorkflowConnection>& GetConnections();
	const std::vector<std::unique_ptr<WorkflowNode>>& GetNodes() const;
	std::vector<std::unique_ptr<WorkflowNode>>& GetNodes();
	const std::vector<std::unique_ptr<BaseValue>>& GetConstants() const;
	std::vector<std::unique_ptr<BaseValue>>& GetConstants();

	WorkflowConnection* GetConnectionById(size_t id);
	WorkflowNode* GetStepById(size_t id);
	BaseValue* GetConstantById(size_t id);

	const std::vector<std::vector<size_t>>& GetDepthGroups() const;
	bool DoesDepthNeedsUpdate() const;

	/* Graph mutation */

	void AddStep(std::unique_ptr<WorkflowNode> step);
	void RemoveStep(size_t id);

	void RemoveConnection(size_t id);

	bool Connect(WorkflowNode& sourceNode, int sourcePin, WorkflowNode& destinationNode, int destinationPin);
	bool DisconnectBySource(WorkflowNode& sourceNode, int sourcePin);
	bool DisconnectByDestination(WorkflowNode& destinationNode, int destinationPin);

	/* Graph rebuild */

	struct GraphUpdate_Success
	{
	};
	struct GraphUpdate_NoWorkToDo
	{
	};
	struct GraphUpdate_UnsatisfiedDependencies
	{
		std::vector<size_t> UnsatisfiedNodes;
	};
	struct GraphUpdate_UnreachableNodes
	{
		std::vector<size_t> UnreachableNodes;
	};

	using GraphUpdateResult = std::variant<
		GraphUpdate_Success,
		GraphUpdate_NoWorkToDo,
		GraphUpdate_UnsatisfiedDependencies,
		GraphUpdate_UnreachableNodes>;

	/// When `getInfo == false, the corresponding error code is returned but without/with empty payloads.
	GraphUpdateResult UpdateGraph(bool getInfo = true);

	/* Serialization */

	enum ReadResult
	{
		ReadSuccess,
		ReadInvalidVersion,
	};
	ReadResult ReadFrom(std::istream& stream);
	void WriteTo(std::ostream& stream);

private:
	std::pair<WorkflowConnection&, size_t> AllocWorkflowConnection();
	std::pair<std::unique_ptr<WorkflowNode>&, size_t> AllocWorkflowStep();
};