Wednesday, May 27, 2015

In the Elixir implementation of FBP, the components and the graph that represents the data flow are each running as a separate process - Erlang process; that's the Elixir/Erlang way of doing things. All communication among these processes is via the sending of messages - Information Packets (IPs) in the flow-based terminology. If you are curious, you can see these processes using the Erlang observer tool. Try the following with the current version - as of today - of ElixirFBP:

Using a command window, cd to the ElixirFBP directory

Type iex -S mix

At the iex prompt, enter :observer.start This will open a window onto the Erlang virtual machine.

Click on the Processes tab. You'll see a list of all the processes currently executing.

Back at the iex prompt, enter Examples.Citibike.start to start the ElixirFBP version of the Streamtools Citibike example.

Go back to the observer window and click on the "Name or Initial Func." column. This will sort the processes by their name. You should see something like the following:

All the processes associated with the example start with "citibike".

If you go back to the iex prompt again and enter ElixirFBP.Network.stop and then enter ElixirFBP.Network.stop_network you will see the "citibike" processes disappear as they forced to stop.

Monday, May 25, 2015

The point of my work is not to re-invent Flow-Based Programming nor to re-invent visual programming (for a really good list of visual programming languages, visit this website). Rather, I want to explore how one can take advantage of the inherent concurrency of Elixir/Erlang processes. Impedance mismatch, when used in software development, is how hard it is to make something behave like something else. There seems to be less of an impedance mismatch with the idea of implementing flow-based or streaming systems in Elixir.

ElixirFBP is showing some life! Last week I was able to create a small graph consisting of an adder and an output component. This is what the code looks like:

The add component is initialized with the two values: 42 and 24. The output of this component is fed into a component that simply prints its message. Granted, this is not the way one would do a little bit of arithmetic but it does test many of the ElixirFBP design elements. For a more interesting demo, please read on.

I recently gave a presentation of my ideas at a Meetup of the Portland, ME Elixir group. I got some great feedback and pointers to three systems that were worth looking at:

Streamtools is being developed by the R&D group at the New York Times. They have designed a flow-based system that features a graphical front-end - similar to NoFlo's - and a backend that executes components written in the Go language. They have some interesting demos - more than the addition of two numbers! One, a poller, periodically checks the status of a bike share program in New York City. I decided to try and implement this demo using ElixirFBP. Here's what it looks like:

You may have to perform a mix deps.get first in order to load some libraries that I use.

Run iex -S mix

Type the following at the iex prompt: Examples.Citibike.start

Wait about 10 seconds. You should start seeing messages appearing roughly every 10 seconds; the messages will likely be the same, unless someone has rented a bike in the last 10 seconds!

Stop the execution by pressing Ctrl+c, twice.

You can run the Add example by entering Examples.Add1.start

The code is still a little rough. Note that the Streamtools flow components are JSON-oriented. In my implementation, once a JSON response is received, it is converted to Elixir data - maps and lists. They also feed the final result into a mask component that then connects via web sockets to a browser. I did not implement this step in the flow.

Soon I'll start connecting NoFlo's graphical front end to ElixirFBP. Then you'll be able to create and run these demos using their GUI.

Friday, May 15, 2015

The addition of the add_initial graph function triggered several changes in the type of information being stored in the internal - Erlang - graph. Here are the external and internal implementations of add_initial (recall that the graph module is a GenServer; typically functions are written in pairs):

Notice that the initial data is associated with a target port. When the graph is eventually executed, these values will be sent, as a message, to the target process. The target node will be an Elixir process at this point and will be listening for messages coming in on its input ports.If the nodes are Elixir processes, what will they look like? We can borrow some ideas from the implementations of FBP components in other programming languages. Below is the CoffeeScript version of the Math.Add component that I retrieved from one of NoFlo's component libraries:

This is neither a complete nor a final version of what an Elixir-based component would look like. Earlier, I mentioned that I would like to employ Elixir's macro facilities and create an FBP DSL.

There is now enough information being stored in the graph so that I will be able to begin trying out some designs for the execution of the graph. I hope to show some of my early attempts in the next post. In the meantime, try out some of the code, if you like.

Wednesday, May 13, 2015

As I get deeper into an understanding of how the FBP graph protocol operates, I uncover areas in my code that are deficient; this is, of course, inevitable. For example, the original design for storing FBP graph nodes couldn't deal with adding an initial value to a node input port. To do this, it was necessary to add placeholders in the graph node for these ports. Recall that Erlang's digraph facilities store nodes (vertices) with an identifier value and a label value. The label value can be used to store extra information about the node - such as FBP ports. Whereas, before the following code was used to store an FBP node in the graph:

Where do the value for inports and outports come from? They come from the component itself. Right now, the component is identified by its module name - a string, for example, "Math.Add". To support the retreival of a component's ports, a very early design for an Elixir component looks like the following:

defmodule Math.Add do
@moduledoc """
This module describes an FBP Component: Math.Add
It knows its input and outport ports and how to execute a function.
Ports are described with tuples: {name, initial value}
"""
def inports, do: [{:addend, nil}, {:augend, nil}]
def outports, do: [{:sum, nil}]
def execute do
end
end

With this in place, the port definitions can be retrieved at run-time by executing the following statements in the add_node function:

Monday, May 11, 2015

In this post, I'll continue the development of the ElixirFBP Graph process. I'll show functions to add and remove a node; and add an edge. Again, we are guided by the NoFlo Network Protocol. You can see the code and tests that I've developed so far in GitHub. I expect that these functions will change as I get a better understanding of how FBP will work in an Elixir environment.Adding a node is straight-forward; the external API and the callback implementations are:

Nothing overly complex going on here. Again, note that even though we are changing the graph, we do not need to pass back a new state. This is because, the Erlang ETS table that is behind the internal graph representation is, in fact, mutable.Here is the remove_node function:

One design aspect that remains unresolved is whether adding a node to a graph should result in the spawning of a process. There is a subset of the FBP Network protocols that deals with what is called a network. One starts and stops networks - executes the graph. So, it is possible that this is when the process should be spawned. It's clear that we will model FBP processes - instances of components - with Elixir processes, but it is unclear, at least to me, how and when this will happen. In the next post, I hope to describe and show a solution to this problem.

Thursday, May 7, 2015

Today I code! Not a great deal - but, at least it'll be a startI decided to begin the implementation of an Elixir FBP by developing the code which will be responsible for maintaining the internal representation of the FBP program. It's a graph whose nodes represent the FBP components and whose edges are the connections between the components over which Information Packets (IP's) pass. I'll use the Erlang package digraph for this representation. An FBP graph is a dynamic entity: nodes and edges can be added, changed and removed. I will model its functional interface according to NoFlo's FBP Network Protocol, in particular, the graph sub-protocol. Further on in our implementation, this graph will be able to be accessed either programmatically, through the Network Protocol, or via a DSL. The graph also needs to persist. It is a common practice in Elixir/Erlang to model a persistent entity as a separate process that maintains state and provides functionality by responding to messages. Elixir provides a GenServer behavior, which has its base in Erlang's OTP - a platform to support long-running processes along with many other facilities. According to the NoFlo's Network Protocol, a graph also has metadata associated with it, such as name, id, description, etc. Thus our first bit of code will be the definition of an Elixir structure to hold this metadata as well as the graph (digraph):

When a module uses (exhibits) the GenServer behavior, it automatically handles the starting and termination of the process, and message handling - both synchronously and asynchronously. Collectively these functions constitute the "callback" interface. But these functions do very little and are usually overridden.The Elixir FBP Graph module will also present an "external" interface - those functions that will be called by clients. Our first external function will be start_link. This function is responsible for creating the process state, and getting the process going:

Here, I create an empty Erlang digraph and add it to an instance of the Graph structure. This structure is passed to GenServer and becomes the process's state. GenServer will make this state available to all subsequent calls. The process is also registered with the name ElixirFBP.Graph - the value of __MODULE__. The process will stay alive until there is a failure or its terminate function is called.Elixir processes, including this GenServer process, do something in response to being sent a message. All messages are sent asynchronously, although a synchronous message can be effected by sending the process id of the caller along with the message. In GenServer terminology sending an asynchronous message is termed a cast and the sending of a synchronous message is termed a call. To make a GenServer process do somethin, you create "external" functions that call or cast information to it; the process will respond by calling back to handler functions that you provide. For example, to provide a function to clear a graph, you would code the following pair of functions:

Notice, in the clear function, the :clear atom. It is used in the definition of a handle_call function as a way of distinguishing one GenServer call (or cast) from another. Note that we did not create a new state, a new ElixirFBP.Graph. This is because nothing actually changed in the state. The graph did change - the edges and vertices were deleted - but this element is implemented using Erlang's ETS - a mutable internal storage facility.In the next post, I'll continue the implementation of the ElixirFBP Graph process.

Tuesday, May 5, 2015

Writing an FBP program means, essentially, describing a graph or network of components, their interconnections, and certain initial conditions. Currently, there are three ways to describe an FBP program:

Programmatically. For example, the following is an excerpt from Java program that uses the JavaFBP library of Paul Morrison:

Using a DSL. There is a language, supported by NoFlo and Morrison, that can be used to create a textual description of an FBP graph. More about the language can be found here and here. This is an example ('#' indicates a comment line):

# Read the content of "somefile.txt" and split it by line
'somefile.txt' -> SOURCE Read(ReadFile) OUT -> IN Split(SplitStr)
# Count the lines and display the result
Split() OUT -> IN Count(Counter) COUNT -> IN Display(Output)
# The read errors are also displayed
Read() ERROR -> IN Display()

No matter how it is described, the execution of an FBP program begins by instantiating components as processes. The presence of Initial Information Packets (IIP's), typically parameters or initial conditions and/or Information Packets (IP's) at a component's input ports will trigger its execution. The result of an execution is, typically, the sending of IP's to other, connected, component processes.

This process architecture suggests a way to begin the Elixir implementation: by developing the functions to create and execute a graph or network representation. Erlang has a library for just such a representation: digraph. While Elixir is syntactically different from Erlang, the Elixir programmer still has direct access to all of Erlang's many libraries. Jordan Damov, in one of his posts about Elixir, shows how one can use the digraph library. Graphs, then, will become the base for our design. The FBP ideas translate well: the nodes in the graphs will be Elixir processes and communication between the nodes will be performed asynchronously by sending messages to these processes. A final note: Morrison describes the differences between the original or "classic" FBP and the NoFlo implementation of FBP. I plan on implementing the classic version.

Sunday, May 3, 2015

This blog will be about flow-based programming (FBP) and, in particular, about an implementation of this programming paradigm using a language whose design closely matches FBP's goals. FBP has its origin in the 1970's and its designer, J. Paul Morrison, has championed it ever since. Recently, NoFlo with its browser-based diagramming tool has sparked renewed interest in FBP.
In the words of Morrison:

"Flow-Based Programming is an approach to developing applications, not in terms of the old von Neumann paradigm, but based on the concept of multiple asynchronous processes communicating by means of streams of data InformationPackets passing over BoundedBuffer connections."

Several languages - Java, C++, C#, Python and, most recently, JavaScript (Node.js) - have been used in support of an FBP style of programming. Given these implementations, it is somewhat ironic that there have been no implementations (AFAIK) of FBP in languages that match the inherent characteristics of FBP even though these languages exist. Notably, there is Erlang and its new incarnation Elixir, both of which have the linguistic facilities for creating processes that communicate via asynchronous message passing. Moreover, these processes are easily and cheaply created and the underlying system - the Erlang virtual machine - can support literally hundreds of thousands of concurrent processes. Importantly, these processes are not operating system threads.In subsequent posts, I'll talk about the design and the implementation of FBP using the Elixir language. Erlang could have been used as well, but I wish to also explore the use of Elixir's macro facilities.