Skip to content

Commit

Permalink
fix(): use direct module reference instead of strings
Browse files Browse the repository at this point in the history
Closes: #1
  • Loading branch information
sarat1669 committed Nov 6, 2018
1 parent 915551c commit 5c6cece
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 62 deletions.
68 changes: 34 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Lets look at a sample workflow:

workflow = Graph.new(type: :directed)
|> Graph.add_edge(
%Node{ module: "Virta.Core.In", id: 0 },
%Node{ module: Virta.Core.In, id: 0 },
%Node{ module: "Virta.Sample.Echo", id: 1 },
label: %EdgeData{ from: :data, to: :data }
)
Expand All @@ -79,7 +79,7 @@ alias Virta.Instance

sample = Graph.new(type: :directed)
|> Graph.add_edge(
%Node{ module: "Virta.Core.In", id: 0 },
%Node{ module: Virta.Core.In, id: 0 },
%Node{ module: "Virta.Sample.Echo", id: 1 },
label: %EdgeData{ from: :data, to: :data }
)
Expand All @@ -88,7 +88,7 @@ sample = Graph.new(type: :directed)
{ :ok, pid } = Instance.start_link(sample)

data = %{
%Node{ module: "Virta.Core.In", id: 0 } => [{ 1, :data, 10 }]
%Node{ module: Virta.Core.In, id: 0 } => [{ 1, :data, 10 }]
}

# Send the message with value `10` to port `:data` with request_id `1`
Expand All @@ -114,18 +114,18 @@ 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 },
%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 },
%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 },
%Node{ module: Virta.Math.Add, id: 1 },
%Node{ module: Virta.Core.Out, id: 2 },
label: %EdgeData{ from: :sum, to: :sum }
)

Expand All @@ -146,7 +146,7 @@ Registry.get("adder")
#=> #PID<0.167.0>

data = %{
%Node{ module: "Virta.Core.In", id: 0 } => [
%Node{ module: Virta.Core.In, id: 0 } => [
{ 1, :augend, 10 }, { 1, :addend, 20 }
]
}
Expand All @@ -164,7 +164,7 @@ end)
`Virta.Core.Worflow` is a special component which allows us to invoke a different workflow from the current workflow. This allows us to reuse workflows.

A workflow node can be represented as
`%Node{ module: "Virta.Core.Workflow", id: 1, ref: "adder" }`
`%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:
Expand All @@ -176,62 +176,62 @@ 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 },
%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 },
%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 },
%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 },
%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 },
%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 },
%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" },
%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" },
%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" },
%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" },
%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 },
%Node{ module: Virta.Core.Workflow, id: 2, ref: "multiplier" },
%Node{ module: Virta.Core.Out, id: 3 },
label: %EdgeData{ from: :product, to: :product }
)

Expand All @@ -244,7 +244,7 @@ Registry.get(name)
#=> #PID<0.572.0>

data = %{
%Node{ module: "Virta.Core.In", id: 0 } => [
%Node{ module: Virta.Core.In, id: 0 } => [
{ 1, :augend, 10 }, { 1, :addend, 20 }
]
}
Expand Down
4 changes: 2 additions & 2 deletions lib/core/workflow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ defmodule Virta.Core.Workflow do
data = message_configs
|> Enum.reduce(%{}, fn({ _from, to}, acc) ->
value = Map.get(inport_args, to)
messages = (Map.get(acc, %Node{ module: "Virta.Core.In", id: 0 }) || []) ++ [{ request_id, to, value }]
Map.put(acc, %Node{ module: "Virta.Core.In", id: 0 }, messages)
messages = (Map.get(acc, %Node{ module: Virta.Core.In, id: 0 }) || []) ++ [{ request_id, to, value }]
Map.put(acc, %Node{ module: Virta.Core.In, id: 0 }, messages)
end)

:poolboy.transaction(String.to_existing_atom(ref), fn (server) ->
Expand Down
5 changes: 2 additions & 3 deletions lib/instance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ defmodule Virta.Instance do
|> Enum.reverse
|> Enum.reduce(Map.new(), fn(node, lookup_table) ->
outport_args = get_outport_args(graph, node, lookup_table, graph)
module = Module.concat("Elixir", Map.get(node, :module))
{ :ok, pid } = Task.start_link(module, :loop, [ %{}, outport_args, self() ])
{ :ok, pid } = Task.start_link(Map.get(node, :module), :loop, [ %{}, outport_args, self() ])
Map.put(lookup_table, node, pid)
end)

Expand Down Expand Up @@ -76,7 +75,7 @@ defmodule Virta.Instance do
# Private functions

defp get_outport_args(graph, node, lookup_table, graph) do
module = Module.concat("Elixir", Map.get(node, :module))
module = Map.get(node, :module)
cond do
Keyword.has_key?(module.__info__(:functions), :final) && module.final ->
in_edges = Graph.in_edges(graph, node)
Expand Down
46 changes: 23 additions & 23 deletions test/virta_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,60 @@ defmodule VirtaTest do
test "sanity" do
adder = Graph.new(type: :directed)
|> Graph.add_edge(
%Node{ module: "Virta.Core.In", id: 0 },
%Node{ module: "Virta.Math.Add", id: 1 },
%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 },
%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 },
%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 },
%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 },
%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 },
%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" },
%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" },
%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" },
%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" },
%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 },
%Node{ module: Virta.Core.Workflow, id: 2, ref: "multiplier" },
%Node{ module: Virta.Core.Out, id: 3 },
label: %EdgeData{ from: :product, to: :output }
)

Expand All @@ -73,7 +73,7 @@ defmodule VirtaTest do
name = "complex_graph"
Registry.get(name)
data = %{
%Node{ module: "Virta.Core.In", id: 0 } => [{ 1, :augend, 1 }, { 1, :addend, 2 }]
%Node{ module: Virta.Core.In, id: 0 } => [{ 1, :augend, 1 }, { 1, :addend, 2 }]
}
:poolboy.transaction(String.to_existing_atom(name), fn (server) ->
Instance.execute(server, data)
Expand Down

0 comments on commit 5c6cece

Please sign in to comment.