Skip to content

(Connections) Add predicate connections #6

Description

@csparker247

M. Truszczynski suggested that it would be useful to conditionally execute a subgraph based on a predicate. This could either be an if or if-else type of situation:

// if condition
if predicate():
    signal(node)

// if-else condition
if predicate():
    signal(node0)
else:
    signal(node1)

I can easily see how this might work with the connection syntax:

OutputPort<int> op{&tgt};
InputPort<int> ip1{&src1};
InputPort<int> ip2{&src2};

// Predicate function
// Accepts argument of type T from OutputPort<T>
bool GreaterThan5(const int& i) {
    return i > 5;
}

// if condition
connect(op, ip1, GreaterThan5);

// if-else condition
// if predicate == true, post to ip1, else post to ip2
connect(op, ip1, ip2, GreaterThan5);

// Ignore the direct value, execute path if there was a node error
connect(op, ip1, [&node](int) { return node->status == Status::Error; });

What is less clear to me is how to serialize a connection like this. It's easy to serialize/deserialize the value of the predicate, but much harder to do the same for the predicate logic. This SO answer suggests registering functions in a factory, the same way we currently do our Node types. This would need a special factory that can store function references (or std::function) of different signatures. This SO answer seems like it would answer that. It's also more fun boilerplate code (#4):

smgl::RegisterFunction("GreaterThan5", GreaterThan5);
smgl::RegisterFunction("SpecificNodeError", [&node](int) { return node->status == Status::Error; });

For the lambda example, it might be useful to pass the parent node pointer to the predicate function as well.


Migrated from GitLab educelab/smgl#7. Originally opened by @csparker247 on 2020-10-08.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions