Skip to content

Commit 11ebe32

Browse files
committed
[feat] add mutable tutorial, add element(*) function
1 parent 6b0478e commit 11ebe32

10 files changed

Lines changed: 153 additions & 17 deletions

File tree

src/GraphCtrl/GraphElement/GElement.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ GElementPtr GElement::setName(const std::string& name) {
4848

4949
GElementPtr GElement::setLoop(CSize loop) {
5050
// 由于运行机制问题,loop执行的element,不支持异步执行
51-
CGRAPH_ASSERT_INIT_THROW_ERROR(false)
51+
CGRAPH_ASSERT_MUTABLE_INIT_THROW_ERROR(false)
5252
CGRAPH_THROW_EXCEPTION_BY_CONDITION((timeout_ > CGRAPH_DEFAULT_ELEMENT_TIMEOUT && loop != CGRAPH_DEFAULT_LOOP_TIMES), \
5353
"cannot set loop value when timeout is bigger than 0")
5454

@@ -97,15 +97,21 @@ GElementPtr GElement::setTimeout(CMSec timeout, GElementTimeoutStrategy strategy
9797
}
9898

9999

100-
GElementRef GElement::operator--(int) {
100+
GElementRef GElement::operator--(int) noexcept {
101+
try {
102+
this->setVisible(true);
103+
} catch (const CException& ex) {
104+
CGRAPH_ECHO("[warning] default set visible failed.");
105+
}
106+
101107
return (*this);
102108
}
103109

104110

105111
GElementRef GElement::operator>(GElementPtr element) {
106112
CGRAPH_FUNCTION_BEGIN
107-
CGRAPH_ASSERT_MUTABLE_INIT_THROW_ERROR(false)
108113
CGRAPH_ASSERT_NOT_NULL_THROW_ERROR(element)
114+
CGRAPH_ASSERT_MUTABLE_INIT_THROW_ERROR(false)
109115
CGRAPH_THROW_EXCEPTION_BY_STATUS(element->addDependGElements({this}))
110116

111117
// 默认通过 这种方式注入的内容,都设置成 visible 的状态
@@ -120,6 +126,17 @@ GElementRef GElement::operator&(GElementPtr element) {
120126
}
121127

122128

129+
GElement& GElement::operator*(CSize loop) {
130+
try {
131+
this->setLoop(loop);
132+
} catch (const CException& ex) {
133+
CGRAPH_ECHO("[warning] default set loop failed.");
134+
}
135+
136+
return (*this);
137+
}
138+
139+
123140
CBool GElement::isRunnable() const {
124141
return 0 >= this->left_depend_ && !this->done_;
125142
}

src/GraphCtrl/GraphElement/GElement.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ class GElement : public GElementObject,
126126
* (*c)-->d;
127127
* @return
128128
*/
129-
GElement& operator--(int);
129+
GElement& operator--(int) noexcept;
130130
GElement& operator>(GElement* element);
131131
GElement& operator&(GElement* element);
132+
GElement& operator*(CSize loop);
132133

133134
protected:
134135
/**

src/GraphCtrl/GraphElement/GElementManager.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,15 @@ CBool GElementManager::checkSerializable() {
200200
return (1 == frontSize) && (1 == tailSize);
201201
}
202202

203+
204+
CStatus GElementManager::process(const GSortedGElementPtrSet& elements) {
205+
CGRAPH_FUNCTION_BEGIN
206+
CGRAPH_ASSERT_NOT_NULL(engine_)
207+
208+
// 主要是给 mutable 使用
209+
status += engine_->setup(elements);
210+
status += run();
211+
CGRAPH_FUNCTION_END
212+
}
213+
203214
CGRAPH_NAMESPACE_END

src/GraphCtrl/GraphElement/GElementManager.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ class GElementManager : public GElementObject,
9393
*/
9494
CBool checkSerializable();
9595

96+
/**
97+
* 加入数据,并且执行
98+
* @param elements
99+
* @return
100+
*/
101+
CStatus process(const GSortedGElementPtrSet& elements);
102+
96103
private:
97104
GSortedGElementPtrSet manager_elements_; // 保存节点信息的内容
98105
GEnginePtr engine_ = nullptr; // 执行引擎

src/GraphCtrl/GraphElement/GGroup/GGroup.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ CStatus GGroup::init() {
2424
CGRAPH_FUNCTION_CHECK_STATUS
2525

2626
is_init_ = true;
27+
trigger_times_ = 0;
2728
CGRAPH_FUNCTION_END
2829
}
2930

@@ -38,6 +39,7 @@ CStatus GGroup::destroy() {
3839
CGRAPH_FUNCTION_CHECK_STATUS
3940

4041
is_init_ = false;
42+
trigger_times_ = 0;
4143
CGRAPH_FUNCTION_END
4244
}
4345

src/GraphCtrl/GraphElement/GGroup/GMutable/GMutable.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,27 @@ CStatus GMutable::addElement(GElementPtr element) {
3333
CStatus GMutable::init() {
3434
CGRAPH_FUNCTION_BEGIN
3535
manager_->setThreadPool(thread_pool_);
36-
status = manager_->init();
36+
status = manager_->initEngine();
37+
CGRAPH_FUNCTION_CHECK_STATUS
38+
39+
status = GGroup::init();
3740
CGRAPH_FUNCTION_END
3841
}
3942

4043

4144
CStatus GMutable::run() {
4245
CGRAPH_FUNCTION_BEGIN
43-
CGRAPH_ASSERT_NOT_NULL(manager_, manager_->engine_);
46+
CGRAPH_ASSERT_NOT_NULL(manager_)
4447

4548
/**
46-
* 1. 取消所有依赖关系,将element设置为不可见
49+
* 1. 初始化内容
4750
* 2. 通过外部复写 translate(),来实现关系设定。其中,通过 --> 设定的,是会自动恢复visible的
4851
* 3. 通过 manager 执行
4952
*/
50-
for (auto* element : group_elements_arr_) {
51-
element->run_before_.clear();
52-
element->dependence_.clear();
53-
element->setVisible(false);
54-
}
55-
status = translate(group_elements_arr_);
56-
CGRAPH_FUNCTION_CHECK_STATUS
53+
setup();
54+
convert(group_elements_arr_);
5755

58-
status += manager_->engine_->setup({group_elements_arr_.begin(), group_elements_arr_.end()});
59-
status += manager_->run();
56+
status = manager_->process({group_elements_arr_.begin(), group_elements_arr_.end()});
6057
CGRAPH_FUNCTION_END
6158
}
6259

@@ -72,4 +69,13 @@ CStatus GMutable::destroy() {
7269
CGRAPH_FUNCTION_END
7370
}
7471

72+
73+
CVoid GMutable::setup() {
74+
for (auto* element : group_elements_arr_) {
75+
element->run_before_.clear();
76+
element->dependence_.clear();
77+
element->setVisible(false);
78+
}
79+
}
80+
7581
CGRAPH_NAMESPACE_END

src/GraphCtrl/GraphElement/GGroup/GMutable/GMutable.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ class GMutable : public GGroup {
2121
* @param elements
2222
* @return
2323
*/
24-
virtual CStatus translate(GElementPtrArr& elements) = 0;
24+
virtual CVoid convert(GElementPtrArr& elements) = 0;
2525

2626
explicit GMutable();
2727

2828
~GMutable() override;
2929

30+
private:
3031
CStatus addElement(GElementPtr element) final;
3132

3233
CStatus init() final;
@@ -35,6 +36,12 @@ class GMutable : public GGroup {
3536

3637
CStatus destroy() final;
3738

39+
/**
40+
* 将数据进行恢复
41+
* @return
42+
*/
43+
CVoid setup();
44+
3845
private:
3946
GElementManagerPtr manager_ = nullptr;
4047

tutorial/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ set(CGRAPH_TUTORIAL_LIST
2727
T23-Some
2828
T24-Fence
2929
T25-Coordinator
30+
T26-Mutable
3031
)
3132

3233

tutorial/MyGMutable/MyMutable.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/***************************
2+
@Author: Chunel
3+
@Contact: chunel@foxmail.com
4+
@File: MyMutable.h
5+
@Time: 2023/11/15 21:22
6+
@Desc:
7+
***************************/
8+
9+
#ifndef CGRAPH_MYMUTABLE_H
10+
#define CGRAPH_MYMUTABLE_H
11+
12+
#include "CGraph.h"
13+
#include "../MyParams/MyParam.h"
14+
15+
class MyMutable : public CGraph::GMutable {
16+
public:
17+
CVoid convert(CGraph::GElementPtrArr& elements) override {
18+
auto param = CGRAPH_GET_GPARAM_WITH_NO_EMPTY(MyParam, "param1")
19+
int count = param->iCount % 4;
20+
if (0 == count) {
21+
CGraph::CGRAPH_ECHO("---- run as a->[b,c]");
22+
(*elements[0])-->elements[1] & elements[2];
23+
} else if (1 == count) {
24+
CGraph::CGRAPH_ECHO("---- run as c(*3)->b->a");
25+
(*elements[2])--*3>elements[1]; // (*elements[2]) 循环3次,并且在 elements[1] 之前执行
26+
(*elements[1])-->elements[0];
27+
} else if (2 == count) {
28+
// 只有在这里,显示描述的element,被激活执行。这里, elements[1] 不执行
29+
CGraph::CGRAPH_ECHO("---- run as a->c, do not run b");
30+
(*elements[0])-->elements[2];
31+
} else if (3 == count) {
32+
CGraph::CGRAPH_ECHO("---- run as [a,b(*2),c]");
33+
(*elements[0])--;
34+
(*elements[1])--*2;
35+
(*elements[2])--;
36+
} else {
37+
CGraph::CGRAPH_ECHO("---- run nothing"); // 理论不会进入这种情况
38+
}
39+
}
40+
};
41+
42+
#endif //CGRAPH_MYMUTABLE_H

tutorial/T26-Mutable.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/***************************
2+
@Author: Chunel
3+
@Contact: chunel@foxmail.com
4+
@File: T26-Mutable.cpp
5+
@Time: 2023/11/15 21:18
6+
@Desc:
7+
***************************/
8+
9+
#include "MyGMutable/MyMutable.h"
10+
#include "MyGNode/MyNode1.h"
11+
#include "MyGNode/MyNode2.h"
12+
#include "MyGNode/MyWriteParamNode.h"
13+
14+
using namespace CGraph;
15+
16+
void tutorial_mutable() {
17+
GPipelinePtr pipeline = GPipelineFactory::create();
18+
GElementPtr a, b_mutable, c, d = nullptr;
19+
20+
// 创建一个 mutable(异变),在其中注入三个node
21+
// ps:这里设定的依赖信息关系,是无效的
22+
b_mutable = pipeline->createGGroup<MyMutable>({
23+
pipeline->createGNode<MyNode1>(GNodeInfo("nodeB1")),
24+
pipeline->createGNode<MyNode2>(GNodeInfo("nodeB2")),
25+
pipeline->createGNode<MyNode1>(GNodeInfo("nodeB3"))
26+
});
27+
28+
pipeline->registerGElement<MyNode1>(&a, {}, "nodeA", 1);
29+
pipeline->registerGGroup(&b_mutable, {a}, "mutableB", 1);
30+
pipeline->registerGElement<MyNode2>(&c, {a}, "nodeC", 1);
31+
pipeline->registerGElement<MyWriteParamNode>(&d, {b_mutable, c}, "nodeD", 1);
32+
33+
// 执行多次,查看 b_mutable 中的执行结果。每次都是不同的
34+
pipeline->process(6);
35+
GPipelineFactory::remove(pipeline);
36+
}
37+
38+
39+
int main () {
40+
tutorial_mutable();
41+
return 0;
42+
}

0 commit comments

Comments
 (0)