aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model/Workflow.cpp
blob: 385544ee713330930aac8fff954c3dfa9e0aa393 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "Workflow.hpp"

#include <cassert>
#include <utility>

WorkflowConnection::WorkflowConnection()
	: Source{ WorkflowNode::kInvalidId }
	, Destination{ WorkflowNode::kInvalidId }
	, SourcePin{ -1 }
	, DestinationPin{ -1 } {
}

bool WorkflowConnection::IsValid() const {
	return Source != WorkflowNode::kInvalidId;
}

bool WorkflowNode::InputPin::IsConstantConnection() const {
	return ConnectionToConst;
}

bool WorkflowNode::InputPin::IsConnected() const {
	return Connection != WorkflowConnection::kInvalidId;
}

bool WorkflowNode::OutputPin::IsConnected() const {
	return Connection != WorkflowConnection::kInvalidId;
}

WorkflowNode::WorkflowNode(Type type, Kind kind)
	: mType{ type }
	, mKind{ kind } {
}

size_t WorkflowNode::GetId() const {
	return mId;
}

WorkflowNode::Type WorkflowNode::GetType() const {
	return mType;
}

WorkflowNode::Kind WorkflowNode::GetKind() const {
	return mKind;
}

void WorkflowNode::ConnectInput(int nodeId, WorkflowNode& output, int outputNodeId) {
	mWorkflow->Connect(*this, nodeId, output, outputNodeId);
}

void WorkflowNode::DisconnectInput(int nodeId) {
	mWorkflow->DisconnectByDestination(*this, nodeId);
}

bool WorkflowNode::IsInputConnected(int nodeId) const {
	return mInputs[nodeId].IsConnected();
}

void WorkflowNode::ConnectOutput(int nodeId, WorkflowNode& input, int inputNodeId) {
	mWorkflow->Connect(input, inputNodeId, *this, nodeId);
}

void WorkflowNode::DisconnectOutput(int nodeId) {
	mWorkflow->DisconnectBySource(*this, nodeId);
}

bool WorkflowNode::IsOutputConnected(int nodeId) const {
	return mOutputs[nodeId].IsConnected();
}

WorkflowNode::InputPin& WorkflowNode::InsertInputPin(int atIdx) {
	assert(atIdx >= 0 && atIdx < mInputs.size());

	mInputs.push_back(InputPin{});
	for (int i = (int)mInputs.size() - 1, end = atIdx + 1; i >= end; --i) {
		SwapInputPin(i, i + 1);
	}

	return mInputs[atIdx];
}

void WorkflowNode::RemoveInputPin(int pin) {
	DisconnectInput(pin);
	for (int i = 0, end = (int)mInputs.size() - 1; i < end; ++i) {
		SwapInputPin(i, i + 1);
	}
	mInputs.resize(mInputs.size() - 1);
}

void WorkflowNode::SwapInputPin(int a, int b) {
	auto& pinA = mInputs[a];
	auto& pinB = mInputs[b];

	if (mWorkflow) {
		if (pinA.IsConnected() && !pinA.IsConstantConnection()) {
			auto& conn = *mWorkflow->GetConnectionById(pinA.Connection);
			conn.DestinationPin = b;
		}
		if (pinB.IsConnected() && !pinB.IsConstantConnection()) {
			auto& conn = *mWorkflow->GetConnectionById(pinB.Connection);
			conn.DestinationPin = a;
		}
	}

	std::swap(pinA, pinB);
}

WorkflowNode::OutputPin& WorkflowNode::InsertOutputPin(int atIdx) {
	assert(atIdx >= 0 && atIdx < mOutputs.size());

	mOutputs.push_back(OutputPin{});
	for (int i = (int)mOutputs.size() - 1, end = atIdx + 1; i >= end; --i) {
		SwapOutputPin(i, i + 1);
	}

	return mOutputs[atIdx];
}

void WorkflowNode::RemoveOutputPin(int pin) {
	DisconnectOutput(pin);
	for (int i = 0, end = (int)mOutputs.size() - 1; i < end; ++i) {
		SwapInputPin(i, i + 1);
	}
	mOutputs.resize(mOutputs.size() - 1);
}

void WorkflowNode::SwapOutputPin(int a, int b) {
	auto& pinA = mOutputs[a];
	auto& pinB = mOutputs[b];

	if (mWorkflow) {
		if (pinA.IsConnected()) {
			auto& conn = *mWorkflow->GetConnectionById(pinA.Connection);
			conn.SourcePin = b;
		}
		if (pinB.IsConnected()) {
			auto& conn = *mWorkflow->GetConnectionById(pinB.Connection);
			conn.SourcePin = a;
		}
	}

	std::swap(pinA, pinB);
}

WorkflowConnection* Workflow::GetConnectionById(size_t id) {
	return &mConnections[id];
}

WorkflowNode* Workflow::GetStepById(size_t id) {
	return mNodes[id].get();
}

BaseValue* Workflow::GetConstantById(size_t id) {
	return mConstants[id].get();
}

void Workflow::AddStep(std::unique_ptr<WorkflowNode> step) {
	auto [storage, id] = AllocWorkflowStep();
	storage = std::move(step);
	storage->OnAttach(*this, id);
	storage->mWorkflow = this;
	storage->mId = id;
}

void Workflow::RemoveStep(size_t id) {
	auto& step = mNodes[id];
	if (step == nullptr) return;

	step->OnDetach();
	step->mWorkflow = nullptr;
	step->mId = WorkflowNode::kInvalidId;
}

void Workflow::Connect(WorkflowNode& source, int sourceNode, WorkflowNode& destination, int destinationNode) {
	if (source.mInputs[sourceNode].IsConnected()) {
		DisconnectBySource(source, sourceNode);
	}

	auto [conn, id] = AllocWorkflowConnection();
	conn.Source = source.GetId();
	conn.SourcePin = sourceNode;
	conn.Destination = destination.GetId();
	conn.DestinationPin = destinationNode;

	source.mInputs[sourceNode].Connection = id;
	destination.mInputs[destinationNode].Connection = id;
}

void Workflow::DisconnectBySource(WorkflowNode& source, int sourceNode) {
	auto& sn = source.mOutputs[sourceNode];
	if (!sn.IsConnected()) return;

	auto& conn = mConnections[sn.Connection];
	auto& dn = mNodes[conn.Destination]->mInputs[conn.DestinationPin];

	sn.Connection = WorkflowConnection::kInvalidId;
	dn.Connection = WorkflowConnection::kInvalidId;
	conn = {};
}

void Workflow::DisconnectByDestination(WorkflowNode& destination, int destinationNode) {
	auto& dn = destination.mOutputs[destinationNode];
	if (!dn.IsConnected()) return;

	auto& conn = mConnections[dn.Connection];
	auto& sn = mNodes[conn.Source]->mInputs[conn.SourcePin];

	sn.Connection = WorkflowConnection::kInvalidId;
	dn.Connection = WorkflowConnection::kInvalidId;
	conn = {};
}

std::pair<WorkflowConnection&, size_t> Workflow::AllocWorkflowConnection() {
	for (size_t idx = 0; idx < mConnections.size(); ++idx) {
		auto& elm = mConnections[idx];
		if (!elm.IsValid()) {
			return { elm, idx };
		}
	}

	auto id = mConnections.size();
	return { mConnections.emplace_back(WorkflowConnection{}), id };
}

std::pair<std::unique_ptr<WorkflowNode>&, size_t> Workflow::AllocWorkflowStep() {
	for (size_t idx = 0; idx < mNodes.size(); ++idx) {
		auto& elm = mNodes[idx];
		if (elm == nullptr) {
			return { elm, idx };
		}
	}

	auto id = mNodes.size();
	return { mNodes.emplace_back(std::unique_ptr<WorkflowNode>()), id };
}

struct WorkflowEvaluationContext::RuntimeNode {
	WorkflowNode* Node;
};

struct WorkflowEvaluationContext::RuntimeConnection {
	WorkflowConnection* Connection;
	std::unique_ptr<BaseValue> Value;
};

WorkflowEvaluationContext::WorkflowEvaluationContext(Workflow& workflow)
	: mWorkflow{ &workflow } {
	mNodes.resize(workflow.mNodes.size());
	for (size_t i = 0; i < workflow.mNodes.size(); ++i) {
		mNodes[i].Node = workflow.mNodes[i].get();
	}

	mConnections.resize(workflow.mConnections.size());
	for (size_t i = 0; i < workflow.mConnections.size(); ++i) {
		mConnections[i].Connection = &workflow.mConnections[i];
	}
}

BaseValue* WorkflowEvaluationContext::GetConnectionValue(size_t id, bool constant) {
	if (constant) {
		return mWorkflow->GetConstantById(id);
	} else {
		return mConnections[id].Value.get();
	}
}

BaseValue* WorkflowEvaluationContext::GetConnectionValue(const WorkflowNode::InputPin& inputPin) {
	if (inputPin.IsConnected()) {
		return GetConnectionValue(inputPin.Connection, inputPin.IsConstantConnection());
	} else {
		return nullptr;
	}
}

void WorkflowEvaluationContext::SetConnectionValue(size_t id, std::unique_ptr<BaseValue> value) {
	mConnections[id].Value = std::move(value);
}

void WorkflowEvaluationContext::SetConnectionValue(const WorkflowNode::OutputPin& outputPin, std::unique_ptr<BaseValue> value) {
	if (outputPin.IsConnected()) {
		SetConnectionValue(outputPin.Connection, std::move(value));
	}
}

void WorkflowEvaluationContext::Run() {
	// TODO
}

void WorkflowEvaluationContext::ReportError(std::string message, const WorkflowNode& node, int pinId, bool inputPin) {
}

void WorkflowEvaluationContext::ReportError(std::string message, const WorkflowNode& node) {
}

void WorkflowEvaluationContext::ReportWarning(std::string message, const WorkflowNode& node, int pinId, bool inputPin) {
}

void WorkflowEvaluationContext::ReportWarning(std::string message, const WorkflowNode& node) {
}