Agents in AjSharp (Part 1)

I started to implement agent ideas in my AjSharp interpreter. I explored channel, queue channels and futures in previous posts:

Channels and GoRoutines in AjSharp (Part 1)
Channels and GoRoutines in AjSharp (Part 2)
AjSharp: Implementing Futures
Queue Channels in AjSharp

and a direct parallel implementation in C#:

Genetic Algorithms using GoRoutines and Channels in C#

but it would be nice to have a more clear model for parallel computation. In non-trivial programs, the management of multiple channels could be cumbersome, as I showed in the genetic algorithm implementation. So, I extend the language to support agent-like objects, towards a partial actor model, with  declarations like:

agent MyAgent {
    // ....

An agent definition is like a class, you can create agents using the new operator and constructors:

agent = new MyAgent(parameter);

The difference is: when the agent instance is created, it launches a separate background thread to process a queue, where the method calls are collected (method and its parameters). A minimal example:

agent IncrementAgent 
  sub Process(channel, value)
    channel <- value + 1;
myagent = new IncrementAgent();
channel = new Channel();
myagent.Process(channel, 1);
result = <-channel; // result == 2

The call myagent.Process(..) is put in the agent queue, and it’s processed by the agent thread. Actually, the internal queue is a queue channel, so synchronization between callers and agent call processing is automatic.

To implement agents, I added AgentFunction class, representing the method to call. It’s invocation forwards the call to the agent process:

public object Invoke(IBindingEnvironment environment, object[] arguments)
    AgentObject agent = (AgentObject)((ObjectEnvironment)environment).Object;
    agent.SendInvoke(this.function, environment, arguments);
    // TODO if function, return a Future
    return null;
    // Old direct code
    //            return this.function.Invoke(environment, arguments);

The SendInvoke call puts the invoke information in the agent internal processing queue.

There is an AgentClass, derived from DynamicClass (the normal definition of a class in AjSharp), extended with a new instance creation method:

public override object NewInstance(object[] parameters)
    AgentObject dynobj = new AgentObject(this);
    this.NewInstance(dynobj, parameters);
    return dynobj;

dynobj.Launch() launches the parallel agent processing thread.

In this way, calling an agent method is like a message passing invocation. If you know Smalltalk, remember that a method call is a message with message name symbol and arguments. In my opinion, using the usual dot invoke convention is a clear and transparent way of implementing a message passing-like behavior in “normal” syntax.

This is the current naive agent object implementation:

    public class AgentObject : DynamicClassicObject
        // TODO 100 is hard coded
        private QueueChannel channel = new QueueChannel(100);
        public AgentObject(IClass objclass) 
            : base(objclass)
        public IChannel Channel { get { return; } }
        public void Launch()
            Thread thread = new Thread(new ParameterizedThreadStart(this.Execute));
            thread.IsBackground = true;
        public void SendInvoke(ICallable function, IBindingEnvironment environment, object[] arguments)
            AgentTask task = new AgentTask() { Callable = function, Environment = environment, Arguments = arguments };
        private void Execute(object parameter)
            Machine machine = (Machine) parameter;
            while (true)
                object obj =;
                AgentTask task = (AgentTask) obj;
                task.Callable.Invoke(task.Environment, task.Arguments);
    internal class AgentTask
        internal ICallable Callable;
        internal IBindingEnvironment Environment;
        internal object[] Arguments;

As usual, the code is published at:

Next steps:

  • Explore agent processing in an example. I could port the genetic algorithm example I wrote in C#.
  • Support invocation of functions in agent: the returning result would be a future, filled by the agent parallel processing of the function call
  • A more ambitious one: distributed agents

Keep tuned!

Angel “Java” Lopez

5 thoughts on “Agents in AjSharp (Part 1)

  1. Pingback: Agents in AjSharp (Part 2) « Angel “Java” Lopez on Blog

  2. Pingback: Agentes en AjSharp (Parte 2) - Angel "Java" Lopez

  3. Pingback: Web Crawler using Agents and AjSharp « Angel “Java” Lopez on Blog

  4. Pingback: Agents in AjTalk: Introduction « Angel “Java” Lopez on Blog

  5. Pingback: Agentes en AjTalk: Introducción - Angel "Java" Lopez

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s