virta v0.1.2 Virta.Core.Workflow
Virta.Core.Worflow is a special component which allows us to invoke a different workflow from the current workflow.
A workflow node can be represented as %Node{ module: Virta.Core.Workflow, id: 1, ref: “adder” }
Notice the :ref property. It refers to the registered workflow with the name adder.
Lets see a code example for a complex worflow which invokes other workflows:
alias Virta.Node
alias Virta.Registry
alias Virta.EdgeData
alias Virta.Instance
adder = Graph.new(type: :directed)
|> Graph.add_edge(
%Node{ module: Virta.Core.In, id: 0 },
%Node{ module: Virta.Math.Add, id: 1 },
label: %EdgeData{ from: :addend, to: :addend }
)
|> Graph.add_edge(
%Node{ module: Virta.Core.In, id: 0 },
%Node{ module: Virta.Math.Add, id: 1 },
label: %EdgeData{ from: :augend, to: :augend }
)
|> Graph.add_edge(
%Node{ module: Virta.Math.Add, id: 1 },
%Node{ module: Virta.Core.Out, id: 2 },
label: %EdgeData{ from: :sum, to: :sum }
)
multiplier = Graph.new(type: :directed)
|> Graph.add_edge(
%Node{ module: Virta.Core.In, id: 0 },
%Node{ module: Virta.Math.Multiply, id: 1 },
label: %EdgeData{ from: :multiplicand, to: :multiplicand }
)
|> Graph.add_edge(
%Node{ module: Virta.Core.In, id: 0 },
%Node{ module: Virta.Math.Multiply, id: 1 },
label: %EdgeData{ from: :multiplier, to: :multiplier }
)
|> Graph.add_edge(
%Node{ module: Virta.Math.Multiply, id: 1 },
%Node{ module: Virta.Core.Out, id: 2 },
label: %EdgeData{ from: :product, to: :product }
)
complex_graph = Graph.new(type: :directed)
|> Graph.add_edge(
%Node{ module: Virta.Core.In, id: 0 },
%Node{ module: Virta.Core.Workflow, id: 1, ref: "adder" },
label: %EdgeData{ from: :augend, to: :augend }
)
|> Graph.add_edge(
%Node{ module: Virta.Core.In, id: 0 },
%Node{ module: Virta.Core.Workflow, id: 1, ref: "adder" },
label: %EdgeData{ from: :addend, to: :addend }
)
|> Graph.add_edge(
%Node{ module: Virta.Core.Workflow, id: 1, ref: "adder" },
%Node{ module: Virta.Core.Workflow, id: 2, ref: "multiplier" },
label: %EdgeData{ from: :sum, to: :multiplicand }
)
|> Graph.add_edge(
%Node{ module: Virta.Core.Workflow, id: 1, ref: "adder" },
%Node{ module: Virta.Core.Workflow, id: 2, ref: "multiplier" },
label: %EdgeData{ from: :sum, to: :multiplier }
)
|> Graph.add_edge(
%Node{ module: Virta.Core.Workflow, id: 2, ref: "multiplier" },
%Node{ module: Virta.Core.Out, id: 3 },
label: %EdgeData{ from: :product, to: :product }
)
Registry.register("adder", adder)
Registry.register("multiplier", multiplier)
Registry.register("complex_graph", complex_graph)
This can then be executed as follows:
data = %{
%Node{ module: Virta.Core.In, id: 0 } => [
{ 1, :augend, 10 }, { 1, :addend, 20 }
]
}
{ requst_id, output } = Virta.Executor.call("complex_graph", data)