Skip to content

Commit 8771865

Browse files
committed
feat(flow): 1.实现了IfThenAction基础功能;2.修改Action::setParent()防止Action被重复添加;3.修改IfElseAction,支持then与else rol
1 parent 5879b71 commit 8771865

12 files changed

+608
-49
lines changed

modules/flow/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ set(TBOX_FLOW_HEADERS
4444
actions/sequence_action.h
4545
actions/parallel_action.h
4646
actions/if_else_action.h
47+
actions/if_then_action.h
4748
actions/loop_action.h
4849
actions/loop_if_action.h
4950
actions/event_action.h
@@ -66,6 +67,7 @@ set(TBOX_FLOW_SOURCES
6667
actions/sequence_action.cpp
6768
actions/parallel_action.cpp
6869
actions/if_else_action.cpp
70+
actions/if_then_action.cpp
6971
actions/loop_action.cpp
7072
actions/loop_if_action.cpp
7173
actions/event_action.cpp
@@ -87,6 +89,7 @@ set(TBOX_FLOW_TEST_SOURCES
8789
actions/sequence_action_test.cpp
8890
actions/parallel_action_test.cpp
8991
actions/if_else_action_test.cpp
92+
actions/if_then_action_test.cpp
9093
actions/loop_action_test.cpp
9194
actions/loop_if_action_test.cpp
9295
actions/repeat_action_test.cpp

modules/flow/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ HEAD_FILES = \
3939
actions/sequence_action.h \
4040
actions/parallel_action.h \
4141
actions/if_else_action.h \
42+
actions/if_then_action.h \
4243
actions/loop_action.h \
4344
actions/loop_if_action.h \
4445
actions/event_action.h \
@@ -61,6 +62,7 @@ CPP_SRC_FILES = \
6162
actions/sequence_action.cpp \
6263
actions/parallel_action.cpp \
6364
actions/if_else_action.cpp \
65+
actions/if_then_action.cpp \
6466
actions/loop_action.cpp \
6567
actions/loop_if_action.cpp \
6668
actions/event_action.cpp \
@@ -85,6 +87,7 @@ TEST_CPP_SRC_FILES = \
8587
actions/sequence_action_test.cpp \
8688
actions/parallel_action_test.cpp \
8789
actions/if_else_action_test.cpp \
90+
actions/if_then_action_test.cpp \
8891
actions/loop_action_test.cpp \
8992
actions/loop_if_action_test.cpp \
9093
actions/repeat_action_test.cpp \

modules/flow/action.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,19 @@ void Action::cancelDispatchedCallback() {
327327
}
328328
}
329329

330-
void Action::setParent(Action *parent) {
330+
bool Action::setParent(Action *parent) {
331+
//! 如果之前设置过了,就不能再设置
332+
if (parent != nullptr && parent_ != nullptr) {
333+
LogWarn("%d:%s[%s] can't set %d:%s[%s] as parent. its origin parent is %d:%s[%s]",
334+
id_, type_.c_str(), label_.c_str(),
335+
parent->id_, parent->type_.c_str(), parent->label_.c_str(),
336+
parent_->id_, parent_->type_.c_str(), parent_->label_.c_str());
337+
return false;
338+
}
339+
331340
vars_.setParent(&(parent->vars_));
341+
parent_ = parent;
342+
return true;
332343
}
333344

334345
Action::Reason::Reason(const Reason &other)

modules/flow/action.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class Action {
124124
bool stop(); //!< 停止
125125
void reset(); //!< 重置,将所有的状态恢复到刚构建状态
126126

127-
void setParent(Action *parent);
127+
bool setParent(Action *parent);
128128

129129
util::Variables& vars() { return vars_; }
130130

@@ -175,6 +175,7 @@ class Action {
175175
//! 检查使用者在重写的 onStart(),onPause(),onResume(),onStop(),onFinished() 中是否调用了基类的函数
176176
//! 如果没有调用,则打警告提示
177177

178+
Action *parent_ = nullptr;
178179
util::Variables vars_;
179180
};
180181

modules/flow/action_reason.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#define ACTION_REASON_REPEAT_NO_TIMES 7 //!< "RepeatNoTimes"
3232
#define ACTION_REASON_SWITCH_FAIL 8 //!< "SwitchFail"
3333
#define ACTION_REASON_SWITCH_SKIP 9 //!< "SwitchSkip"
34+
#define ACTION_REASON_IF_THEN_SKIP 10 //!< "IfThenSkip"
3435

3536
//! 保存 1000 以内供 Action 自用,使用者自定义的 Reason 需 >= 1000
3637

modules/flow/actions/assemble_action.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ int AssembleAction::addChild(Action *)
3030
return -1;
3131
}
3232

33+
int AssembleAction::addChildAs(Action *, const std::string &)
34+
{
35+
LogWarn("%d:%s[%s] not implement this function", id(), type().c_str(), label().c_str());
36+
return -1;
37+
}
38+
3339
bool AssembleAction::setChild(Action *)
3440
{
3541
LogWarn("%d:%s[%s] not implement this function", id(), type().c_str(), label().c_str());

modules/flow/actions/assemble_action.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class AssembleAction : public Action {
3333
public:
3434
//!< 设置子动作
3535
virtual int addChild(Action *child);
36+
virtual int addChildAs(Action *child, const std::string &role);
3637
virtual bool setChild(Action *child);
3738
virtual bool setChildAs(Action *child, const std::string &role);
3839

modules/flow/actions/if_else_action.cpp

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ IfElseAction::IfElseAction(event::Loop &loop)
3434

3535
IfElseAction::~IfElseAction() {
3636
CHECK_DELETE_RESET_OBJ(if_action_);
37-
CHECK_DELETE_RESET_OBJ(succ_action_);
38-
CHECK_DELETE_RESET_OBJ(fail_action_);
37+
CHECK_DELETE_RESET_OBJ(then_action_);
38+
CHECK_DELETE_RESET_OBJ(else_action_);
3939
}
4040

4141
void IfElseAction::toJson(Json &js) const {
4242
AssembleAction::toJson(js);
4343
auto &js_children = js["children"];
4444
if_action_->toJson(js_children["0.if"]);
45-
if (succ_action_ != nullptr)
46-
succ_action_->toJson(js_children["1.succ"]);
47-
if (fail_action_ != nullptr)
48-
fail_action_->toJson(js_children["2.fail"]);
45+
if (then_action_ != nullptr)
46+
then_action_->toJson(js_children["1.succ"]);
47+
if (else_action_ != nullptr)
48+
else_action_->toJson(js_children["2.fail"]);
4949
}
5050

5151
bool IfElseAction::setChildAs(Action *child, const std::string &role) {
@@ -59,23 +59,23 @@ bool IfElseAction::setChildAs(Action *child, const std::string &role) {
5959
}
6060
return true;
6161

62-
} else if (role == "succ") {
63-
CHECK_DELETE_RESET_OBJ(succ_action_);
64-
succ_action_ = child;
65-
if (succ_action_ != nullptr) {
66-
succ_action_->setFinishCallback(std::bind(&IfElseAction::finish, this, _1, _2, _3));
67-
succ_action_->setBlockCallback(std::bind(&IfElseAction::block, this, _1, _2));
68-
succ_action_->setParent(this);
62+
} else if (role == "succ" || role == "then") {
63+
CHECK_DELETE_RESET_OBJ(then_action_);
64+
then_action_ = child;
65+
if (then_action_ != nullptr) {
66+
then_action_->setFinishCallback(std::bind(&IfElseAction::finish, this, _1, _2, _3));
67+
then_action_->setBlockCallback(std::bind(&IfElseAction::block, this, _1, _2));
68+
then_action_->setParent(this);
6969
}
7070
return true;
7171

72-
} else if (role == "fail") {
73-
CHECK_DELETE_RESET_OBJ(fail_action_);
74-
fail_action_ = child;
75-
if (fail_action_ != nullptr) {
76-
fail_action_->setFinishCallback(std::bind(&IfElseAction::finish, this, _1, _2, _3));
77-
fail_action_->setBlockCallback(std::bind(&IfElseAction::block, this, _1, _2));
78-
fail_action_->setParent(this);
72+
} else if (role == "fail" || role == "else") {
73+
CHECK_DELETE_RESET_OBJ(else_action_);
74+
else_action_ = child;
75+
if (else_action_ != nullptr) {
76+
else_action_->setFinishCallback(std::bind(&IfElseAction::finish, this, _1, _2, _3));
77+
else_action_->setBlockCallback(std::bind(&IfElseAction::block, this, _1, _2));
78+
else_action_->setParent(this);
7979
}
8080
return true;
8181
}
@@ -90,14 +90,14 @@ bool IfElseAction::isReady() const {
9090
return false;
9191
}
9292

93-
if (!succ_action_ && !fail_action_) {
93+
if (!then_action_ && !else_action_) {
9494
LogWarn("%d:%s[%s] both succ and fail func is null", id(), type().c_str(), label().c_str());
9595
return false;
9696
}
9797

9898
if ((!if_action_->isReady()) ||
99-
(succ_action_ != nullptr && !succ_action_->isReady()) ||
100-
(fail_action_ != nullptr && !fail_action_->isReady()))
99+
(then_action_ != nullptr && !then_action_->isReady()) ||
100+
(else_action_ != nullptr && !else_action_->isReady()))
101101
return false;
102102

103103
return true;
@@ -114,9 +114,9 @@ void IfElseAction::onStop() {
114114
TBOX_ASSERT(if_action_ != nullptr);
115115
if (if_action_->state() == State::kFinished) {
116116
if (if_action_->result() == Result::kSuccess) {
117-
succ_action_->stop();
117+
then_action_->stop();
118118
} else {
119-
fail_action_->stop();
119+
else_action_->stop();
120120
}
121121
} else {
122122
if_action_->stop();
@@ -129,9 +129,9 @@ void IfElseAction::onPause() {
129129
TBOX_ASSERT(if_action_ != nullptr);
130130
if (if_action_->state() == State::kFinished) {
131131
if (if_action_->result() == Result::kSuccess) {
132-
succ_action_->pause();
132+
then_action_->pause();
133133
} else {
134-
fail_action_->pause();
134+
else_action_->pause();
135135
}
136136
} else {
137137
if_action_->pause();
@@ -146,16 +146,16 @@ void IfElseAction::onResume() {
146146
TBOX_ASSERT(if_action_ != nullptr);
147147
if (if_action_->state() == State::kFinished) {
148148
if (if_action_->result() == Result::kSuccess) {
149-
if (succ_action_->state() == State::kFinished) {
150-
finish(succ_action_->result() == Result::kSuccess);
149+
if (then_action_->state() == State::kFinished) {
150+
finish(then_action_->result() == Result::kSuccess);
151151
} else {
152-
succ_action_->resume();
152+
then_action_->resume();
153153
}
154154
} else {
155-
if (fail_action_->state() == State::kFinished) {
156-
finish(fail_action_->result() == Result::kSuccess);
155+
if (else_action_->state() == State::kFinished) {
156+
finish(else_action_->result() == Result::kSuccess);
157157
} else {
158-
fail_action_->resume();
158+
else_action_->resume();
159159
}
160160
}
161161
} else {
@@ -167,25 +167,25 @@ void IfElseAction::onReset() {
167167
TBOX_ASSERT(if_action_ != nullptr);
168168
if_action_->reset();
169169

170-
if (succ_action_ != nullptr)
171-
succ_action_->reset();
170+
if (then_action_ != nullptr)
171+
then_action_->reset();
172172

173-
if (fail_action_ != nullptr)
174-
fail_action_->reset();
173+
if (else_action_ != nullptr)
174+
else_action_->reset();
175175

176176
AssembleAction::onReset();
177177
}
178178

179179
void IfElseAction::onCondActionFinished(bool is_succ, const Reason &why, const Trace &trace) {
180180
if (state() == State::kRunning) {
181181
if (is_succ) {
182-
if (succ_action_ != nullptr) {
183-
succ_action_->start();
182+
if (then_action_ != nullptr) {
183+
then_action_->start();
184184
return;
185185
}
186186
} else {
187-
if (fail_action_ != nullptr) {
188-
fail_action_->start();
187+
if (else_action_ != nullptr) {
188+
else_action_->start();
189189
return;
190190
}
191191
}

modules/flow/actions/if_else_action.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ namespace tbox {
2626
namespace flow {
2727

2828
/**
29-
* bool IfElseAction(if_action, succ_action, fail_acton) {
29+
* bool IfElseAction(if_action, then_action, else_action) {
3030
* if (if_action())
31-
* return succ_action();
31+
* return then_action();
3232
* else
33-
* return fail_acton();
33+
* return else_action();
3434
* }
3535
*/
3636
class IfElseAction : public AssembleAction {
@@ -40,7 +40,7 @@ class IfElseAction : public AssembleAction {
4040

4141
virtual void toJson(Json &js) const override;
4242

43-
//! role: "if", "succ", "fail"
43+
//! role: "if", "succ/then", "fail/else"
4444
virtual bool setChildAs(Action *child, const std::string &role) override;
4545

4646
virtual bool isReady() const override;
@@ -57,8 +57,8 @@ class IfElseAction : public AssembleAction {
5757

5858
private:
5959
Action *if_action_ = nullptr;
60-
Action *succ_action_ = nullptr;
61-
Action *fail_action_ = nullptr;
60+
Action *then_action_ = nullptr;
61+
Action *else_action_ = nullptr;
6262
};
6363

6464
}

0 commit comments

Comments
 (0)