forked from h1st-ai/h1st
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
34 lines (29 loc) · 1.12 KB
/
graph.py
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
"""
This is an example of a very simple graph which prints hello for each even number x in the input stream,
using a conditional RuleBasedModel node and a HelloPrinter h1.Action.
"""
import h1st as h1
from rule_based_model import RuleBasedModel
class HelloPrinter(h1.Action):
"""Print hello to the inputs value"""
def call(self, command, inputs):
# Note that H1st does the conditional/filtering orchestration already.
# All we need to do here is just to print.
for d in inputs["predictions"]:
print("Hello world {}!".format(d["value"]))
def create_graph():
"""Create a graph which prints hello for each even number x in the input stream,
using a conditional RuleBasedModel node and a HelloPrinter h1.Action."""
graph = h1.Graph()
graph.start()\
.add(h1.Decision(RuleBasedModel(), result_field="predictions"))\
.add(yes=HelloPrinter(), no=h1.NoOp())
graph.end()
return graph
if __name__ == "__main__":
graph = create_graph()
results = graph.predict({"values": range(6)})
# Should get:
# Hello world 0!
# Hello world 2!
# Hello world 4!