Skip to content

Commit 5a05810

Browse files
RB-159 adding greater/less than stream operators (#64)
* feat(operators): adding greater/less than stream operators * fix(docs): Fixing md * fix(headers): Adding missing headers file
1 parent 49f2d40 commit 5a05810

File tree

14 files changed

+223
-10
lines changed

14 files changed

+223
-10
lines changed

libs/api/src/FactoryOp.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
#include "rtbot/std/EqualTo.h"
2424
#include "rtbot/std/FiniteImpulseResponse.h"
2525
#include "rtbot/std/GreaterThan.h"
26+
#include "rtbot/std/GreaterThanStream.h"
2627
#include "rtbot/std/HermiteResampler.h"
2728
#include "rtbot/std/Identity.h"
2829
#include "rtbot/std/LessThan.h"
30+
#include "rtbot/std/LessThanStream.h"
2931
#include "rtbot/std/Linear.h"
3032
#include "rtbot/std/Minus.h"
3133
#include "rtbot/std/MovingAverage.h"
@@ -167,6 +169,26 @@ void from_json(const json& j, Or<T, V>& p) {
167169
p = Or<T, V>(j["id"].get<string>());
168170
}
169171

172+
template <class T, class V>
173+
void to_json(json& j, const GreaterThanStream<T, V>& p) {
174+
j = json{{"type", p.typeName()}, {"id", p.id}};
175+
}
176+
177+
template <class T, class V>
178+
void from_json(const json& j, GreaterThanStream<T, V>& p) {
179+
p = GreaterThanStream<T, V>(j["id"].get<string>());
180+
}
181+
182+
template <class T, class V>
183+
void to_json(json& j, const LessThanStream<T, V>& p) {
184+
j = json{{"type", p.typeName()}, {"id", p.id}};
185+
}
186+
187+
template <class T, class V>
188+
void from_json(const json& j, LessThanStream<T, V>& p) {
189+
p = LessThanStream<T, V>(j["id"].get<string>());
190+
}
191+
170192
template <class T, class V>
171193
void to_json(json& j, const Division<T, V>& p) {
172194
j = json{{"type", p.typeName()}, {"id", p.id}};
@@ -428,6 +450,8 @@ FactoryOp::FactoryOp() {
428450
op_registry_add<Variable<uint64_t, double>, json>();
429451
op_registry_add<TimeShift<uint64_t, double>, json>();
430452
op_registry_add<Pipeline<uint64_t, double>, json>();
453+
op_registry_add<GreaterThanStream<uint64_t, double>, json>();
454+
op_registry_add<LessThanStream<uint64_t, double>, json>();
431455
}
432456

433457
static FactoryOp factory;

libs/core/include/rtbot/BinaryJoin.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define BINARYJOIN_H
33

44
#include <functional>
5+
#include <optional>
56

67
#include "rtbot/Join.h"
78

@@ -12,7 +13,7 @@ using namespace std;
1213
template <class T, class V>
1314
struct BinaryJoin : public Join<T, V> {
1415
BinaryJoin() = default;
15-
BinaryJoin(string const& id, function<V(V, V)> operation) : Join<T, V>(id) {
16+
BinaryJoin(string const& id, function<optional<V>(V, V)> operation) : Join<T, V>(id) {
1617
this->operation = operation;
1718
int numPorts = 2;
1819
for (int i = 1; i <= numPorts; i++) {
@@ -28,7 +29,9 @@ struct BinaryJoin : public Join<T, V> {
2829
Message<T, V> m0 = this->getDataInputMessage("i1", 0);
2930
Message<T, V> out;
3031
out.time = m0.time;
31-
out.value = operation(m0.value, m1.value);
32+
optional<V> result = operation(m0.value, m1.value);
33+
if (!result.has_value()) return {};
34+
out.value = result.value();
3235

3336
vector<Message<T, V>> v;
3437
v.push_back(out);
@@ -38,7 +41,7 @@ struct BinaryJoin : public Join<T, V> {
3841
}
3942

4043
private:
41-
function<V(V, V)> operation;
44+
function<optional<V>(V, V)> operation;
4245
};
4346
} // namespace rtbot
4447

libs/std/include/rtbot/std/And.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ template <class T, class V>
1111
struct And : public BinaryJoin<T, V> {
1212
And() = default;
1313
And(string const &id)
14-
: BinaryJoin<T, V>(id, [](V a, V b) {
14+
: BinaryJoin<T, V>(id, [](V a, V b) -> optional<V> {
1515
bool left = (a < 0.5) ? false : true;
1616
bool right = (b >= 0.5) ? true : false;
1717
bool result = left && right;

libs/std/include/rtbot/std/Division.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ using namespace std;
1010
template <class T, class V>
1111
struct Division : public BinaryJoin<T, V> {
1212
Division() = default;
13-
Division(string const &id) : BinaryJoin<T, V>(id, [](V a, V b) { return a / b; }) {}
13+
Division(string const &id)
14+
: BinaryJoin<T, V>(id, [](V a, V b) -> optional<V> {
15+
if (b != 0) return a / b;
16+
return nullopt;
17+
}) {}
1418

1519
string typeName() const override { return "Division"; }
1620
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef GREATERTHANSTREAM_H
2+
#define GREATERTHANSTREAM_H
3+
4+
#include "rtbot/BinaryJoin.h"
5+
6+
namespace rtbot {
7+
8+
using namespace std;
9+
10+
template <class T, class V>
11+
struct GreaterThanStream : public BinaryJoin<T, V> {
12+
GreaterThanStream() = default;
13+
GreaterThanStream(string const &id)
14+
: BinaryJoin<T, V>(id, [](V a, V b) -> optional<V> {
15+
if (a > b) return a;
16+
return nullopt;
17+
}) {}
18+
19+
string typeName() const override { return "GreaterThanStream"; }
20+
};
21+
22+
} // namespace rtbot
23+
24+
#endif // GREATERTHANSTREAM_H
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
behavior:
3+
buffered: true
4+
throughput: variable
5+
view:
6+
shape: circle
7+
latex:
8+
template: |
9+
||
10+
jsonschema:
11+
type: object
12+
properties:
13+
id:
14+
type: string
15+
description: The id of the operator
16+
required: ["id"]
17+
---
18+
19+
# GreaterThanStream
20+
21+
Inputs: `i1` `i2`
22+
Outputs: `o1`
23+
24+
Synchronizes two streams (given by `i1` and `i2`) and emits the message in `i1` if its value is greater than the value of `i2` for the synchronized $t_n$.
25+
26+
The synchronization mechanism is inherited from the `Join` operator. The `GreaterThanStream` operator holds a message buffer on `i1` and `i2` respectively, after synchronization occurs it emits the message in `i1` through `o1` if it happens to be greater than the message value in `i2` and it discard the message in `i2`, if no synchronization occurs then an empty message {} is emitted through `o1`.
27+
28+
$$y(t_n) = x_1(t_n) if x_1(t_n) > x_2(t_n)$$
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef LESSTHANSTREAM_H
2+
#define LESSTHANSTREAM_H
3+
4+
#include "rtbot/BinaryJoin.h"
5+
6+
namespace rtbot {
7+
8+
using namespace std;
9+
10+
template <class T, class V>
11+
struct LessThanStream : public BinaryJoin<T, V> {
12+
LessThanStream() = default;
13+
LessThanStream(string const &id)
14+
: BinaryJoin<T, V>(id, [](V a, V b) -> optional<V> {
15+
if (a < b) return a;
16+
return nullopt;
17+
}) {}
18+
19+
string typeName() const override { return "LessThanStream"; }
20+
};
21+
22+
} // namespace rtbot
23+
24+
#endif // LESSTHANSTREAM_H
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
behavior:
3+
buffered: true
4+
throughput: variable
5+
view:
6+
shape: circle
7+
latex:
8+
template: |
9+
||
10+
jsonschema:
11+
type: object
12+
properties:
13+
id:
14+
type: string
15+
description: The id of the operator
16+
required: ["id"]
17+
---
18+
19+
# LessThanStream
20+
21+
Inputs: `i1` `i2`
22+
Outputs: `o1`
23+
24+
Synchronizes two streams (given by `i1` and `i2`) and emits the message in `i1` if its value is less than the value of `i2` for the synchronized $t_n$.
25+
26+
The synchronization mechanism is inherited from the `Join` operator. The `LessThanStream` operator holds a message buffer on `i1` and `i2` respectively, after synchronization occurs it emits the message in `i1` through `o1` if it happens to be less than the message value in `i2` and it discard the message in `i2`, if no synchronization occurs then an empty message {} is emitted through `o1`.
27+
28+
$$y(t_n) = x_1(t_n) if x_1(t_n) < x_2(t_n)$$

libs/std/include/rtbot/std/Minus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using namespace std;
1010
template <class T, class V>
1111
struct Minus : public BinaryJoin<T, V> {
1212
Minus() = default;
13-
Minus(string const &id) : BinaryJoin<T, V>(id, [](V a, V b) { return a - b; }) {}
13+
Minus(string const &id) : BinaryJoin<T, V>(id, [](V a, V b) -> optional<V> { return a - b; }) {}
1414

1515
string typeName() const override { return "Minus"; }
1616
};

libs/std/include/rtbot/std/Multiplication.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using namespace std;
1010
template <class T, class V>
1111
struct Multiplication : public BinaryJoin<T, V> {
1212
Multiplication() = default;
13-
Multiplication(string const &id) : BinaryJoin<T, V>(id, [](V a, V b) { return a * b; }) {}
13+
Multiplication(string const &id) : BinaryJoin<T, V>(id, [](V a, V b) -> optional<V> { return a * b; }) {}
1414

1515
string typeName() const override { return "Multiplication"; }
1616
};

0 commit comments

Comments
 (0)